2 Copyright (C) 2000-2020 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_UNIQUE_PTR
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 if (parser
->type_definition_forbidden_message
)
576 fprintf (file
, "Error message for forbidden type definitions: %s %s\n",
577 parser
->type_definition_forbidden_message
,
578 parser
->type_definition_forbidden_message_arg
579 ? parser
->type_definition_forbidden_message_arg
: "<none>");
580 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
581 fprintf (file
, "Number of class definitions in progress: %u\n",
582 parser
->num_classes_being_defined
);
583 fprintf (file
, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser
->num_template_parameter_lists
);
585 cp_debug_parser_tokens (file
, parser
, window_size
);
586 token
= parser
->lexer
->next_token
;
587 fprintf (file
, "Next token to parse:\n");
588 fprintf (file
, "\tToken: ");
589 cp_lexer_print_token (file
, token
);
590 eloc
= expand_location (token
->location
);
591 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
592 fprintf (file
, "\tLine: %d\n", eloc
.line
);
593 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
597 debug (cp_parser
&ref
)
599 cp_debug_parser (stderr
, &ref
);
603 debug (cp_parser
*ptr
)
608 fprintf (stderr
, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
614 cp_lexer_alloc (void)
616 /* Allocate the memory. */
617 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
619 /* Initially we are not debugging. */
620 lexer
->debugging_p
= false;
622 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
624 /* Create the buffer. */
625 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
634 cp_lexer_new_main (void)
638 /* It's possible that parsing the first pragma will load a PCH file,
639 which is a GC collection point. So we have to do that before
640 allocating any memory. */
641 cp_lexer_get_preprocessor_token (0, &token
);
642 cp_parser_initial_pragma (&token
);
643 c_common_no_more_pch ();
645 cp_lexer
*lexer
= cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 cp_token
*tok
= lexer
->buffer
->quick_push (token
);
649 /* Get the remaining tokens from the preprocessor. */
650 while (tok
->type
!= CPP_EOF
)
652 tok
= vec_safe_push (lexer
->buffer
, cp_token ());
653 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN
, tok
);
656 lexer
->next_token
= lexer
->buffer
->address ();
657 lexer
->last_token
= lexer
->next_token
658 + lexer
->buffer
->length ()
661 /* Subsequent preprocessor diagnostics should use compiler
662 diagnostic functions to get the compiler source location. */
665 gcc_assert (!lexer
->next_token
->purged_p
);
669 /* Create a new lexer whose token stream is primed with the tokens in
670 CACHE. When these tokens are exhausted, no new tokens will be read. */
673 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
675 cp_token
*first
= cache
->first
;
676 cp_token
*last
= cache
->last
;
677 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
679 /* We do not own the buffer. */
680 lexer
->buffer
= NULL
;
682 /* Insert an EOF token. */
683 lexer
->saved_type
= last
->type
;
684 lexer
->saved_keyword
= last
->keyword
;
685 last
->type
= CPP_EOF
;
686 last
->keyword
= RID_MAX
;
688 lexer
->next_token
= first
;
689 lexer
->last_token
= last
;
691 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
693 /* Initially we are not debugging. */
694 lexer
->debugging_p
= false;
696 gcc_assert (!lexer
->next_token
->purged_p
697 && !lexer
->last_token
->purged_p
);
701 /* Frees all resources associated with LEXER. */
704 cp_lexer_destroy (cp_lexer
*lexer
)
707 vec_free (lexer
->buffer
);
710 /* Restore the token we overwrite with EOF. */
711 lexer
->last_token
->type
= lexer
->saved_type
;
712 lexer
->last_token
->keyword
= lexer
->saved_keyword
;
714 lexer
->saved_tokens
.release ();
718 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
719 be used. The point of this flag is to help the compiler to fold away calls
720 to cp_lexer_debugging_p within this source file at compile time, when the
721 lexer is not being debugged. */
723 #define LEXER_DEBUGGING_ENABLED_P false
725 /* Returns nonzero if debugging information should be output. */
728 cp_lexer_debugging_p (cp_lexer
*lexer
)
730 if (!LEXER_DEBUGGING_ENABLED_P
)
733 return lexer
->debugging_p
;
737 static inline cp_token_position
738 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
740 return lexer
->next_token
- previous_p
;
743 static inline cp_token
*
744 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
750 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
752 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
755 static inline cp_token_position
756 cp_lexer_previous_token_position (cp_lexer
*lexer
)
758 return cp_lexer_token_position (lexer
, true);
761 static inline cp_token
*
762 cp_lexer_previous_token (cp_lexer
*lexer
)
764 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
766 /* Skip past purged tokens. */
769 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
773 return cp_lexer_token_at (lexer
, tp
);
776 /* Same as above, but return NULL when the lexer doesn't own the token
777 buffer or if the next_token is at the start of the token
778 vector or if all previous tokens are purged. */
781 cp_lexer_safe_previous_token (cp_lexer
*lexer
)
784 && lexer
->next_token
!= lexer
->buffer
->address ())
786 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
788 /* Skip past purged tokens. */
791 if (tp
== lexer
->buffer
->address ())
795 return cp_lexer_token_at (lexer
, tp
);
801 /* Overload for make_location, taking the lexer to mean the location of the
804 static inline location_t
805 make_location (location_t caret
, location_t start
, cp_lexer
*lexer
)
807 cp_token
*t
= cp_lexer_previous_token (lexer
);
808 return make_location (caret
, start
, t
->location
);
811 /* nonzero if we are presently saving tokens. */
814 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
816 return lexer
->saved_tokens
.length () != 0;
819 /* Store the next token from the preprocessor in *TOKEN. Return true
820 if we reach EOF. If LEXER is NULL, assume we are handling an
821 initial #pragma pch_preprocess, and thus want the lexer to return
822 processed strings. */
825 cp_lexer_get_preprocessor_token (unsigned flags
, cp_token
*token
)
827 static int is_extern_c
= 0;
829 /* Get a new token from the preprocessor. */
831 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
833 token
->keyword
= RID_MAX
;
834 token
->purged_p
= false;
835 token
->error_reported
= false;
836 token
->tree_check_p
= false;
838 /* On some systems, some header files are surrounded by an
839 implicit extern "C" block. Set a flag in the token if it
840 comes from such a header. */
841 is_extern_c
+= pending_lang_change
;
842 pending_lang_change
= 0;
843 token
->implicit_extern_c
= is_extern_c
> 0;
845 /* Check to see if this token is a keyword. */
846 if (token
->type
== CPP_NAME
)
848 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
850 /* Mark this token as a keyword. */
851 token
->type
= CPP_KEYWORD
;
852 /* Record which keyword. */
853 token
->keyword
= C_RID_CODE (token
->u
.value
);
857 if (warn_cxx11_compat
858 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
859 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
861 /* Warn about the C++0x keyword (but still treat it as
863 warning_at (token
->location
, OPT_Wc__11_compat
,
864 "identifier %qE is a keyword in C++11",
867 /* Clear out the C_RID_CODE so we don't warn about this
868 particular identifier-turned-keyword again. */
869 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
871 if (warn_cxx20_compat
872 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX20
873 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX20
)
875 /* Warn about the C++20 keyword (but still treat it as
877 warning_at (token
->location
, OPT_Wc__20_compat
,
878 "identifier %qE is a keyword in C++20",
881 /* Clear out the C_RID_CODE so we don't warn about this
882 particular identifier-turned-keyword again. */
883 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
886 token
->keyword
= RID_MAX
;
889 else if (token
->type
== CPP_AT_NAME
)
891 /* This only happens in Objective-C++; it must be a keyword. */
892 token
->type
= CPP_KEYWORD
;
893 switch (C_RID_CODE (token
->u
.value
))
895 /* Replace 'class' with '@class', 'private' with '@private',
896 etc. This prevents confusion with the C++ keyword
897 'class', and makes the tokens consistent with other
898 Objective-C 'AT' keywords. For example '@class' is
899 reported as RID_AT_CLASS which is consistent with
900 '@synchronized', which is reported as
903 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
904 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
905 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
906 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
907 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
908 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
909 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
910 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
911 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
916 /* Update the globals input_location and the input file stack from TOKEN. */
918 cp_lexer_set_source_position_from_token (cp_token
*token
)
920 input_location
= token
->location
;
923 /* Update the globals input_location and the input file stack from LEXER. */
925 cp_lexer_set_source_position (cp_lexer
*lexer
)
927 cp_token
*token
= cp_lexer_peek_token (lexer
);
928 cp_lexer_set_source_position_from_token (token
);
931 /* Return a pointer to the next token in the token stream, but do not
934 static inline cp_token
*
935 cp_lexer_peek_token (cp_lexer
*lexer
)
937 if (cp_lexer_debugging_p (lexer
))
939 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
940 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
941 putc ('\n', cp_lexer_debug_stream
);
943 return lexer
->next_token
;
946 /* Return true if the next token has the indicated TYPE. */
949 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
951 return cp_lexer_peek_token (lexer
)->type
== type
;
954 /* Return true if the next token does not have the indicated TYPE. */
957 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
959 return !cp_lexer_next_token_is (lexer
, type
);
962 /* Return true if the next token is the indicated KEYWORD. */
965 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
967 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
971 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
973 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
977 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
979 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
982 /* Return true if KEYWORD can start a decl-specifier. */
985 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
989 /* auto specifier: storage-class-specifier in C++,
990 simple-type-specifier in C++0x. */
992 /* Storage classes. */
998 /* Elaborated type specifiers. */
1004 /* Simple type specifiers. */
1019 /* GNU extensions. */
1022 /* C++11 extensions. */
1024 case RID_UNDERLYING_TYPE
:
1026 /* C++20 extensions. */
1032 if (keyword
>= RID_FIRST_INT_N
1033 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
1034 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
1040 /* Return true if the next token is a keyword for a decl-specifier. */
1043 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1047 token
= cp_lexer_peek_token (lexer
);
1048 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1051 /* Returns TRUE iff the token T begins a decltype type. */
1054 token_is_decltype (cp_token
*t
)
1056 return (t
->keyword
== RID_DECLTYPE
1057 || t
->type
== CPP_DECLTYPE
);
1060 /* Returns TRUE iff the next token begins a decltype type. */
1063 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1065 cp_token
*t
= cp_lexer_peek_token (lexer
);
1066 return token_is_decltype (t
);
1069 /* Called when processing a token with tree_check_value; perform or defer the
1070 associated checks and return the value. */
1073 saved_checks_value (struct tree_check
*check_value
)
1075 /* Perform any access checks that were deferred. */
1076 vec
<deferred_access_check
, va_gc
> *checks
;
1077 deferred_access_check
*chk
;
1078 checks
= check_value
->checks
;
1082 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1083 perform_or_defer_access_check (chk
->binfo
,
1085 chk
->diag_decl
, tf_warning_or_error
);
1087 /* Return the stored value. */
1088 return check_value
->value
;
1091 /* Return a pointer to the Nth token in the token stream. If N is 1,
1092 then this is precisely equivalent to cp_lexer_peek_token (except
1093 that it is not inline). One would like to disallow that case, but
1094 there is one case (cp_parser_nth_token_starts_template_id) where
1095 the caller passes a variable for N and it might be 1. */
1098 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1102 /* N is 1-based, not zero-based. */
1105 if (cp_lexer_debugging_p (lexer
))
1106 fprintf (cp_lexer_debug_stream
,
1107 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1110 token
= lexer
->next_token
;
1111 while (n
&& token
->type
!= CPP_EOF
)
1114 if (!token
->purged_p
)
1118 if (cp_lexer_debugging_p (lexer
))
1120 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1121 putc ('\n', cp_lexer_debug_stream
);
1127 /* Return the next token, and advance the lexer's next_token pointer
1128 to point to the next non-purged token. */
1131 cp_lexer_consume_token (cp_lexer
* lexer
)
1133 cp_token
*token
= lexer
->next_token
;
1137 gcc_assert (token
->type
!= CPP_EOF
);
1138 lexer
->next_token
++;
1140 while (lexer
->next_token
->purged_p
);
1142 cp_lexer_set_source_position_from_token (token
);
1144 /* Provide debugging output. */
1145 if (cp_lexer_debugging_p (lexer
))
1147 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1148 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1149 putc ('\n', cp_lexer_debug_stream
);
1155 /* Permanently remove the next token from the token stream, and
1156 advance the next_token pointer to refer to the next non-purged
1160 cp_lexer_purge_token (cp_lexer
*lexer
)
1162 cp_token
*tok
= lexer
->next_token
;
1164 gcc_assert (tok
->type
!= CPP_EOF
);
1165 tok
->purged_p
= true;
1166 tok
->location
= UNKNOWN_LOCATION
;
1167 tok
->u
.value
= NULL_TREE
;
1168 tok
->keyword
= RID_MAX
;
1172 while (tok
->purged_p
);
1173 lexer
->next_token
= tok
;
1176 /* Permanently remove all tokens after TOK, up to, but not
1177 including, the token that will be returned next by
1178 cp_lexer_peek_token. */
1181 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1183 cp_token
*peek
= lexer
->next_token
;
1185 gcc_assert (tok
< peek
);
1187 for (tok
++; tok
!= peek
; tok
++)
1189 tok
->purged_p
= true;
1190 tok
->location
= UNKNOWN_LOCATION
;
1191 tok
->u
.value
= NULL_TREE
;
1192 tok
->keyword
= RID_MAX
;
1196 /* Begin saving tokens. All tokens consumed after this point will be
1200 cp_lexer_save_tokens (cp_lexer
* lexer
)
1202 /* Provide debugging output. */
1203 if (cp_lexer_debugging_p (lexer
))
1204 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1206 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1209 /* Commit to the portion of the token stream most recently saved. */
1212 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1214 /* Provide debugging output. */
1215 if (cp_lexer_debugging_p (lexer
))
1216 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1218 lexer
->saved_tokens
.pop ();
1221 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1222 to the token stream. Stop saving tokens. */
1225 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1227 /* Provide debugging output. */
1228 if (cp_lexer_debugging_p (lexer
))
1229 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1231 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1234 /* RAII wrapper around the above functions, with sanity checking. Creating
1235 a variable saves tokens, which are committed when the variable is
1236 destroyed unless they are explicitly rolled back by calling the rollback
1239 struct saved_token_sentinel
1244 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1246 len
= lexer
->saved_tokens
.length ();
1247 cp_lexer_save_tokens (lexer
);
1251 cp_lexer_rollback_tokens (lexer
);
1254 ~saved_token_sentinel()
1257 cp_lexer_commit_tokens (lexer
);
1258 gcc_assert (lexer
->saved_tokens
.length () == len
);
1262 /* Print a representation of the TOKEN on the STREAM. */
1265 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1267 /* We don't use cpp_type2name here because the parser defines
1268 a few tokens of its own. */
1269 static const char *const token_names
[] = {
1270 /* cpplib-defined token types */
1271 #define OP(e, s) #e,
1272 #define TK(e, s) #e,
1276 /* C++ parser token types - see "Manifest constants", above. */
1279 "NESTED_NAME_SPECIFIER",
1282 /* For some tokens, print the associated data. */
1283 switch (token
->type
)
1286 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1287 For example, `struct' is mapped to an INTEGER_CST. */
1288 if (!identifier_p (token
->u
.value
))
1292 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1299 case CPP_UTF8STRING
:
1300 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1304 print_generic_expr (stream
, token
->u
.value
);
1308 /* If we have a name for the token, print it out. Otherwise, we
1309 simply give the numeric code. */
1310 if (token
->type
< ARRAY_SIZE(token_names
))
1311 fputs (token_names
[token
->type
], stream
);
1313 fprintf (stream
, "[%d]", token
->type
);
1319 debug (cp_token
&ref
)
1321 cp_lexer_print_token (stderr
, &ref
);
1322 fprintf (stderr
, "\n");
1326 debug (cp_token
*ptr
)
1331 fprintf (stderr
, "<nil>\n");
1335 /* Start emitting debugging information. */
1338 cp_lexer_start_debugging (cp_lexer
* lexer
)
1340 if (!LEXER_DEBUGGING_ENABLED_P
)
1341 fatal_error (input_location
,
1342 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1344 lexer
->debugging_p
= true;
1345 cp_lexer_debug_stream
= stderr
;
1348 /* Stop emitting debugging information. */
1351 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1353 if (!LEXER_DEBUGGING_ENABLED_P
)
1354 fatal_error (input_location
,
1355 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1357 lexer
->debugging_p
= false;
1358 cp_lexer_debug_stream
= NULL
;
1361 /* Create a new cp_token_cache, representing a range of tokens. */
1363 static cp_token_cache
*
1364 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1366 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1367 cache
->first
= first
;
1372 /* Diagnose if #pragma omp declare simd isn't followed immediately
1373 by function declaration or definition. */
1376 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1378 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1380 error ("%<#pragma omp declare %s%> not immediately followed by "
1381 "function declaration or definition",
1382 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1383 parser
->omp_declare_simd
= NULL
;
1387 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1388 and put that into "omp declare simd" attribute. */
1391 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1393 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1395 if (fndecl
== error_mark_node
)
1397 parser
->omp_declare_simd
= NULL
;
1400 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1402 cp_ensure_no_omp_declare_simd (parser
);
1408 /* Diagnose if #pragma acc routine isn't followed immediately by function
1409 declaration or definition. */
1412 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1414 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1416 error_at (parser
->oacc_routine
->loc
,
1417 "%<#pragma acc routine%> not immediately followed by "
1418 "function declaration or definition");
1419 parser
->oacc_routine
= NULL
;
1423 /* Decl-specifiers. */
1425 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1428 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1430 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1435 /* Nothing other than the parser should be creating declarators;
1436 declarators are a semi-syntactic representation of C++ entities.
1437 Other parts of the front end that need to create entities (like
1438 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1440 static cp_declarator
*make_call_declarator
1441 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
,
1442 tree
, tree
, tree
, tree
, location_t
);
1443 static cp_declarator
*make_array_declarator
1444 (cp_declarator
*, tree
);
1445 static cp_declarator
*make_pointer_declarator
1446 (cp_cv_quals
, cp_declarator
*, tree
);
1447 static cp_declarator
*make_reference_declarator
1448 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1449 static cp_declarator
*make_ptrmem_declarator
1450 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1452 /* An erroneous declarator. */
1453 static cp_declarator
*cp_error_declarator
;
1455 /* The obstack on which declarators and related data structures are
1457 static struct obstack declarator_obstack
;
1459 /* Alloc BYTES from the declarator memory pool. */
1461 static inline void *
1462 alloc_declarator (size_t bytes
)
1464 return obstack_alloc (&declarator_obstack
, bytes
);
1467 /* Allocate a declarator of the indicated KIND. Clear fields that are
1468 common to all declarators. */
1470 static cp_declarator
*
1471 make_declarator (cp_declarator_kind kind
)
1473 cp_declarator
*declarator
;
1475 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1476 declarator
->kind
= kind
;
1477 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1478 declarator
->attributes
= NULL_TREE
;
1479 declarator
->std_attributes
= NULL_TREE
;
1480 declarator
->declarator
= NULL
;
1481 declarator
->parameter_pack_p
= false;
1482 declarator
->id_loc
= UNKNOWN_LOCATION
;
1487 /* Make a declarator for a generalized identifier. If
1488 QUALIFYING_SCOPE is non-NULL, the identifier is
1489 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1490 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1493 static cp_declarator
*
1494 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1495 special_function_kind sfk
, location_t id_location
)
1497 cp_declarator
*declarator
;
1499 /* It is valid to write:
1501 class C { void f(); };
1505 The standard is not clear about whether `typedef const C D' is
1506 legal; as of 2002-09-15 the committee is considering that
1507 question. EDG 3.0 allows that syntax. Therefore, we do as
1509 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1510 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1512 gcc_assert (identifier_p (unqualified_name
)
1513 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1514 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1516 declarator
= make_declarator (cdk_id
);
1517 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1518 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1519 declarator
->u
.id
.sfk
= sfk
;
1520 declarator
->id_loc
= id_location
;
1525 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1526 of modifiers such as const or volatile to apply to the pointer
1527 type, represented as identifiers. ATTRIBUTES represent the attributes that
1528 appertain to the pointer or reference. */
1531 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1534 cp_declarator
*declarator
;
1536 declarator
= make_declarator (cdk_pointer
);
1537 declarator
->declarator
= target
;
1538 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1539 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1542 declarator
->id_loc
= target
->id_loc
;
1543 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1544 target
->parameter_pack_p
= false;
1547 declarator
->parameter_pack_p
= false;
1549 declarator
->std_attributes
= attributes
;
1554 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1555 represent the attributes that appertain to the pointer or
1559 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1560 bool rvalue_ref
, tree attributes
)
1562 cp_declarator
*declarator
;
1564 declarator
= make_declarator (cdk_reference
);
1565 declarator
->declarator
= target
;
1566 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1567 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1570 declarator
->id_loc
= target
->id_loc
;
1571 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1572 target
->parameter_pack_p
= false;
1575 declarator
->parameter_pack_p
= false;
1577 declarator
->std_attributes
= attributes
;
1582 /* Like make_pointer_declarator -- but for a pointer to a non-static
1583 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1584 appertain to the pointer or reference. */
1587 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1588 cp_declarator
*pointee
,
1591 cp_declarator
*declarator
;
1593 declarator
= make_declarator (cdk_ptrmem
);
1594 declarator
->declarator
= pointee
;
1595 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1596 declarator
->u
.pointer
.class_type
= class_type
;
1600 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1601 pointee
->parameter_pack_p
= false;
1604 declarator
->parameter_pack_p
= false;
1606 declarator
->std_attributes
= attributes
;
1611 /* Make a declarator for the function given by TARGET, with the
1612 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1613 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1614 indicates what exceptions can be thrown. */
1617 make_call_declarator (cp_declarator
*target
,
1619 cp_cv_quals cv_qualifiers
,
1620 cp_virt_specifiers virt_specifiers
,
1621 cp_ref_qualifier ref_qualifier
,
1623 tree exception_specification
,
1624 tree late_return_type
,
1625 tree requires_clause
,
1626 location_t parens_loc
)
1628 cp_declarator
*declarator
;
1630 declarator
= make_declarator (cdk_function
);
1631 declarator
->declarator
= target
;
1632 declarator
->u
.function
.parameters
= parms
;
1633 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1634 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1635 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1636 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1637 declarator
->u
.function
.exception_specification
= exception_specification
;
1638 declarator
->u
.function
.late_return_type
= late_return_type
;
1639 declarator
->u
.function
.requires_clause
= requires_clause
;
1640 declarator
->u
.function
.parens_loc
= parens_loc
;
1643 declarator
->id_loc
= target
->id_loc
;
1644 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1645 target
->parameter_pack_p
= false;
1648 declarator
->parameter_pack_p
= false;
1653 /* Make a declarator for an array of BOUNDS elements, each of which is
1654 defined by ELEMENT. */
1657 make_array_declarator (cp_declarator
*element
, tree bounds
)
1659 cp_declarator
*declarator
;
1661 declarator
= make_declarator (cdk_array
);
1662 declarator
->declarator
= element
;
1663 declarator
->u
.array
.bounds
= bounds
;
1666 declarator
->id_loc
= element
->id_loc
;
1667 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1668 element
->parameter_pack_p
= false;
1671 declarator
->parameter_pack_p
= false;
1676 /* Determine whether the declarator we've seen so far can be a
1677 parameter pack, when followed by an ellipsis. */
1679 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1681 if (declarator
&& declarator
->parameter_pack_p
)
1682 /* We already saw an ellipsis. */
1685 /* Search for a declarator name, or any other declarator that goes
1686 after the point where the ellipsis could appear in a parameter
1687 pack. If we find any of these, then this declarator cannot be
1688 made into a parameter pack. */
1690 while (declarator
&& !found
)
1692 switch ((int)declarator
->kind
)
1704 declarator
= declarator
->declarator
;
1712 cp_parameter_declarator
*no_parameters
;
1714 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1715 DECLARATOR and DEFAULT_ARGUMENT. */
1717 cp_parameter_declarator
*
1718 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1719 cp_declarator
*declarator
,
1720 tree default_argument
,
1722 bool template_parameter_pack_p
= false)
1724 cp_parameter_declarator
*parameter
;
1726 parameter
= ((cp_parameter_declarator
*)
1727 alloc_declarator (sizeof (cp_parameter_declarator
)));
1728 parameter
->next
= NULL
;
1729 if (decl_specifiers
)
1730 parameter
->decl_specifiers
= *decl_specifiers
;
1732 clear_decl_specs (¶meter
->decl_specifiers
);
1733 parameter
->declarator
= declarator
;
1734 parameter
->default_argument
= default_argument
;
1735 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1736 parameter
->loc
= loc
;
1741 /* Returns true iff DECLARATOR is a declaration for a function. */
1744 function_declarator_p (const cp_declarator
*declarator
)
1748 if (declarator
->kind
== cdk_function
1749 && declarator
->declarator
->kind
== cdk_id
)
1751 if (declarator
->kind
== cdk_id
1752 || declarator
->kind
== cdk_decomp
1753 || declarator
->kind
== cdk_error
)
1755 declarator
= declarator
->declarator
;
1765 A cp_parser parses the token stream as specified by the C++
1766 grammar. Its job is purely parsing, not semantic analysis. For
1767 example, the parser breaks the token stream into declarators,
1768 expressions, statements, and other similar syntactic constructs.
1769 It does not check that the types of the expressions on either side
1770 of an assignment-statement are compatible, or that a function is
1771 not declared with a parameter of type `void'.
1773 The parser invokes routines elsewhere in the compiler to perform
1774 semantic analysis and to build up the abstract syntax tree for the
1777 The parser (and the template instantiation code, which is, in a
1778 way, a close relative of parsing) are the only parts of the
1779 compiler that should be calling push_scope and pop_scope, or
1780 related functions. The parser (and template instantiation code)
1781 keeps track of what scope is presently active; everything else
1782 should simply honor that. (The code that generates static
1783 initializers may also need to set the scope, in order to check
1784 access control correctly when emitting the initializers.)
1789 The parser is of the standard recursive-descent variety. Upcoming
1790 tokens in the token stream are examined in order to determine which
1791 production to use when parsing a non-terminal. Some C++ constructs
1792 require arbitrary look ahead to disambiguate. For example, it is
1793 impossible, in the general case, to tell whether a statement is an
1794 expression or declaration without scanning the entire statement.
1795 Therefore, the parser is capable of "parsing tentatively." When the
1796 parser is not sure what construct comes next, it enters this mode.
1797 Then, while we attempt to parse the construct, the parser queues up
1798 error messages, rather than issuing them immediately, and saves the
1799 tokens it consumes. If the construct is parsed successfully, the
1800 parser "commits", i.e., it issues any queued error messages and
1801 the tokens that were being preserved are permanently discarded.
1802 If, however, the construct is not parsed successfully, the parser
1803 rolls back its state completely so that it can resume parsing using
1804 a different alternative.
1809 The performance of the parser could probably be improved substantially.
1810 We could often eliminate the need to parse tentatively by looking ahead
1811 a little bit. In some places, this approach might not entirely eliminate
1812 the need to parse tentatively, but it might still speed up the average
1815 /* Flags that are passed to some parsing functions. These values can
1816 be bitwise-ored together. */
1821 CP_PARSER_FLAGS_NONE
= 0x0,
1822 /* The construct is optional. If it is not present, then no error
1823 should be issued. */
1824 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1825 /* When parsing a type-specifier, treat user-defined type-names
1826 as non-type identifiers. */
1827 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1828 /* When parsing a type-specifier, do not try to parse a class-specifier
1829 or enum-specifier. */
1830 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1831 /* When parsing a decl-specifier-seq, only allow type-specifier or
1833 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1834 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1835 for C++20 consteval. */
1836 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1837 /* When parsing a decl-specifier-seq, allow missing typename. */
1838 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20,
1839 /* When parsing of the noexcept-specifier should be delayed. */
1840 CP_PARSER_FLAGS_DELAY_NOEXCEPT
= 0x40,
1841 /* When parsing a consteval declarator. */
1842 CP_PARSER_FLAGS_CONSTEVAL
= 0x80
1845 /* This type is used for parameters and variables which hold
1846 combinations of the above flags. */
1847 typedef int cp_parser_flags
;
1849 /* The different kinds of declarators we want to parse. */
1851 enum cp_parser_declarator_kind
1853 /* We want an abstract declarator. */
1854 CP_PARSER_DECLARATOR_ABSTRACT
,
1855 /* We want a named declarator. */
1856 CP_PARSER_DECLARATOR_NAMED
,
1857 /* We don't mind, but the name must be an unqualified-id. */
1858 CP_PARSER_DECLARATOR_EITHER
1861 /* The precedence values used to parse binary expressions. The minimum value
1862 of PREC must be 1, because zero is reserved to quickly discriminate
1863 binary operators from other tokens. */
1868 PREC_LOGICAL_OR_EXPRESSION
,
1869 PREC_LOGICAL_AND_EXPRESSION
,
1870 PREC_INCLUSIVE_OR_EXPRESSION
,
1871 PREC_EXCLUSIVE_OR_EXPRESSION
,
1872 PREC_AND_EXPRESSION
,
1873 PREC_EQUALITY_EXPRESSION
,
1874 PREC_RELATIONAL_EXPRESSION
,
1875 PREC_SPACESHIP_EXPRESSION
,
1876 PREC_SHIFT_EXPRESSION
,
1877 PREC_ADDITIVE_EXPRESSION
,
1878 PREC_MULTIPLICATIVE_EXPRESSION
,
1880 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1883 /* A mapping from a token type to a corresponding tree node type, with a
1884 precedence value. */
1886 struct cp_parser_binary_operations_map_node
1888 /* The token type. */
1889 enum cpp_ttype token_type
;
1890 /* The corresponding tree code. */
1891 enum tree_code tree_type
;
1892 /* The precedence of this operator. */
1893 enum cp_parser_prec prec
;
1896 struct cp_parser_expression_stack_entry
1898 /* Left hand side of the binary operation we are currently
1901 /* Original tree code for left hand side, if it was a binary
1902 expression itself (used for -Wparentheses). */
1903 enum tree_code lhs_type
;
1904 /* Tree code for the binary operation we are parsing. */
1905 enum tree_code tree_type
;
1906 /* Precedence of the binary operation we are parsing. */
1907 enum cp_parser_prec prec
;
1908 /* Location of the binary operation we are parsing. */
1912 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1913 entries because precedence levels on the stack are monotonically
1915 typedef struct cp_parser_expression_stack_entry
1916 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1920 /* Constructors and destructors. */
1922 static cp_parser_context
*cp_parser_context_new
1923 (cp_parser_context
*);
1925 /* Class variables. */
1927 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1929 /* The operator-precedence table used by cp_parser_binary_expression.
1930 Transformed into an associative array (binops_by_token) by
1933 static const cp_parser_binary_operations_map_node binops
[] = {
1934 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1935 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1937 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1938 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1939 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1941 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1942 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1944 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1945 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1947 { CPP_SPACESHIP
, SPACESHIP_EXPR
, PREC_SPACESHIP_EXPRESSION
},
1949 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1950 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1951 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1952 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1954 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1955 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1957 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1959 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1961 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1963 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1965 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1968 /* The same as binops, but initialized by cp_parser_new so that
1969 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1971 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1973 /* Constructors and destructors. */
1975 /* Construct a new context. The context below this one on the stack
1976 is given by NEXT. */
1978 static cp_parser_context
*
1979 cp_parser_context_new (cp_parser_context
* next
)
1981 cp_parser_context
*context
;
1983 /* Allocate the storage. */
1984 if (cp_parser_context_free_list
!= NULL
)
1986 /* Pull the first entry from the free list. */
1987 context
= cp_parser_context_free_list
;
1988 cp_parser_context_free_list
= context
->next
;
1989 memset (context
, 0, sizeof (*context
));
1992 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1994 /* No errors have occurred yet in this context. */
1995 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1996 /* If this is not the bottommost context, copy information that we
1997 need from the previous context. */
2000 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2001 expression, then we are parsing one in this context, too. */
2002 context
->object_type
= next
->object_type
;
2003 /* Thread the stack. */
2004 context
->next
= next
;
2010 /* Managing the unparsed function queues. */
2012 #define unparsed_funs_with_default_args \
2013 parser->unparsed_queues->last ().funs_with_default_args
2014 #define unparsed_funs_with_definitions \
2015 parser->unparsed_queues->last ().funs_with_definitions
2016 #define unparsed_nsdmis \
2017 parser->unparsed_queues->last ().nsdmis
2018 #define unparsed_noexcepts \
2019 parser->unparsed_queues->last ().noexcepts
2022 push_unparsed_function_queues (cp_parser
*parser
)
2024 cp_unparsed_functions_entry e
= { NULL
, make_tree_vector (), NULL
, NULL
};
2025 vec_safe_push (parser
->unparsed_queues
, e
);
2029 pop_unparsed_function_queues (cp_parser
*parser
)
2031 release_tree_vector (unparsed_funs_with_definitions
);
2032 parser
->unparsed_queues
->pop ();
2037 /* Constructors and destructors. */
2039 static cp_parser
*cp_parser_new
2042 /* Routines to parse various constructs.
2044 Those that return `tree' will return the error_mark_node (rather
2045 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2046 Sometimes, they will return an ordinary node if error-recovery was
2047 attempted, even though a parse error occurred. So, to check
2048 whether or not a parse error occurred, you should always use
2049 cp_parser_error_occurred. If the construct is optional (indicated
2050 either by an `_opt' in the name of the function that does the
2051 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2052 the construct is not present. */
2054 /* Lexical conventions [gram.lex] */
2056 static cp_expr cp_parser_identifier
2058 static cp_expr cp_parser_string_literal
2059 (cp_parser
*, bool, bool, bool);
2060 static cp_expr cp_parser_userdef_char_literal
2062 static tree cp_parser_userdef_string_literal
2064 static cp_expr cp_parser_userdef_numeric_literal
2067 /* Basic concepts [gram.basic] */
2069 static void cp_parser_translation_unit (cp_parser
*);
2071 /* Expressions [gram.expr] */
2073 static cp_expr cp_parser_primary_expression
2074 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2075 static cp_expr cp_parser_id_expression
2076 (cp_parser
*, bool, bool, bool *, bool, bool);
2077 static cp_expr cp_parser_unqualified_id
2078 (cp_parser
*, bool, bool, bool, bool);
2079 static tree cp_parser_nested_name_specifier_opt
2080 (cp_parser
*, bool, bool, bool, bool, bool = false);
2081 static tree cp_parser_nested_name_specifier
2082 (cp_parser
*, bool, bool, bool, bool);
2083 static tree cp_parser_qualifying_entity
2084 (cp_parser
*, bool, bool, bool, bool, bool);
2085 static cp_expr cp_parser_postfix_expression
2086 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2087 static tree cp_parser_postfix_open_square_expression
2088 (cp_parser
*, tree
, bool, bool);
2089 static tree cp_parser_postfix_dot_deref_expression
2090 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2091 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2092 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2094 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2095 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2096 static void cp_parser_pseudo_destructor_name
2097 (cp_parser
*, tree
, tree
*, tree
*);
2098 static cp_expr cp_parser_unary_expression
2099 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2100 static enum tree_code cp_parser_unary_operator
2102 static tree cp_parser_has_attribute_expression
2104 static tree cp_parser_new_expression
2106 static vec
<tree
, va_gc
> *cp_parser_new_placement
2108 static tree cp_parser_new_type_id
2109 (cp_parser
*, tree
*);
2110 static cp_declarator
*cp_parser_new_declarator_opt
2112 static cp_declarator
*cp_parser_direct_new_declarator
2114 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2116 static tree cp_parser_delete_expression
2118 static cp_expr cp_parser_cast_expression
2119 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2120 static cp_expr cp_parser_binary_expression
2121 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2122 static tree cp_parser_question_colon_clause
2123 (cp_parser
*, cp_expr
);
2124 static cp_expr cp_parser_assignment_expression
2125 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2126 static enum tree_code cp_parser_assignment_operator_opt
2128 static cp_expr cp_parser_expression
2129 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2130 static cp_expr cp_parser_constant_expression
2131 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2132 static cp_expr cp_parser_builtin_offsetof
2134 static cp_expr cp_parser_lambda_expression
2136 static void cp_parser_lambda_introducer
2137 (cp_parser
*, tree
);
2138 static bool cp_parser_lambda_declarator_opt
2139 (cp_parser
*, tree
);
2140 static void cp_parser_lambda_body
2141 (cp_parser
*, tree
);
2143 /* Statements [gram.stmt.stmt] */
2145 static void cp_parser_statement
2146 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2147 static void cp_parser_label_for_labeled_statement
2148 (cp_parser
*, tree
);
2149 static tree cp_parser_expression_statement
2150 (cp_parser
*, tree
);
2151 static tree cp_parser_compound_statement
2152 (cp_parser
*, tree
, int, bool);
2153 static void cp_parser_statement_seq_opt
2154 (cp_parser
*, tree
);
2155 static tree cp_parser_selection_statement
2156 (cp_parser
*, bool *, vec
<tree
> *);
2157 static tree cp_parser_condition
2159 static tree cp_parser_iteration_statement
2160 (cp_parser
*, bool *, bool, unsigned short);
2161 static bool cp_parser_init_statement
2162 (cp_parser
*, tree
*decl
);
2163 static tree cp_parser_for
2164 (cp_parser
*, bool, unsigned short);
2165 static tree cp_parser_c_for
2166 (cp_parser
*, tree
, tree
, bool, unsigned short);
2167 static tree cp_parser_range_for
2168 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2169 static void do_range_for_auto_deduction
2171 static tree cp_parser_perform_range_for_lookup
2172 (tree
, tree
*, tree
*);
2173 static tree cp_parser_range_for_member_function
2175 static tree cp_parser_jump_statement
2177 static void cp_parser_declaration_statement
2180 static tree cp_parser_implicitly_scoped_statement
2181 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2182 static void cp_parser_already_scoped_statement
2183 (cp_parser
*, bool *, const token_indent_info
&);
2185 /* Declarations [gram.dcl.dcl] */
2187 static void cp_parser_declaration_seq_opt
2189 static void cp_parser_declaration
2191 static void cp_parser_toplevel_declaration
2193 static void cp_parser_block_declaration
2194 (cp_parser
*, bool);
2195 static void cp_parser_simple_declaration
2196 (cp_parser
*, bool, tree
*);
2197 static void cp_parser_decl_specifier_seq
2198 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2199 static tree cp_parser_storage_class_specifier_opt
2201 static tree cp_parser_function_specifier_opt
2202 (cp_parser
*, cp_decl_specifier_seq
*);
2203 static tree cp_parser_type_specifier
2204 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2206 static tree cp_parser_simple_type_specifier
2207 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2208 static tree cp_parser_placeholder_type_specifier
2209 (cp_parser
*, location_t
, tree
, bool);
2210 static tree cp_parser_type_name
2211 (cp_parser
*, bool);
2212 static tree cp_parser_nonclass_name
2213 (cp_parser
* parser
);
2214 static tree cp_parser_elaborated_type_specifier
2215 (cp_parser
*, bool, bool);
2216 static tree cp_parser_enum_specifier
2218 static void cp_parser_enumerator_list
2219 (cp_parser
*, tree
);
2220 static void cp_parser_enumerator_definition
2221 (cp_parser
*, tree
);
2222 static tree cp_parser_namespace_name
2224 static void cp_parser_namespace_definition
2226 static void cp_parser_namespace_body
2228 static tree cp_parser_qualified_namespace_specifier
2230 static void cp_parser_namespace_alias_definition
2232 static bool cp_parser_using_declaration
2233 (cp_parser
*, bool);
2234 static void cp_parser_using_directive
2236 static tree cp_parser_alias_declaration
2238 static void cp_parser_asm_definition
2240 static void cp_parser_linkage_specification
2242 static void cp_parser_static_assert
2243 (cp_parser
*, bool);
2244 static tree cp_parser_decltype
2246 static tree cp_parser_decomposition_declaration
2247 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2249 /* Declarators [gram.dcl.decl] */
2251 static tree cp_parser_init_declarator
2252 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2253 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2254 location_t
*, tree
*);
2255 static cp_declarator
*cp_parser_declarator
2256 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2258 static cp_declarator
*cp_parser_direct_declarator
2259 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2261 static enum tree_code cp_parser_ptr_operator
2262 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2263 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2265 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2267 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2269 static tree cp_parser_tx_qualifier_opt
2271 static tree cp_parser_late_return_type_opt
2272 (cp_parser
*, cp_declarator
*, tree
&);
2273 static tree cp_parser_declarator_id
2274 (cp_parser
*, bool);
2275 static tree cp_parser_type_id
2276 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2277 static tree cp_parser_template_type_arg
2279 static tree
cp_parser_trailing_type_id (cp_parser
*);
2280 static tree cp_parser_type_id_1
2281 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2282 static void cp_parser_type_specifier_seq
2283 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2284 static tree cp_parser_parameter_declaration_clause
2285 (cp_parser
*, cp_parser_flags
);
2286 static tree cp_parser_parameter_declaration_list
2287 (cp_parser
*, cp_parser_flags
);
2288 static cp_parameter_declarator
*cp_parser_parameter_declaration
2289 (cp_parser
*, cp_parser_flags
, bool, bool *);
2290 static tree cp_parser_default_argument
2291 (cp_parser
*, bool);
2292 static void cp_parser_function_body
2293 (cp_parser
*, bool);
2294 static tree cp_parser_initializer
2295 (cp_parser
*, bool *, bool *, bool = false);
2296 static cp_expr cp_parser_initializer_clause
2297 (cp_parser
*, bool *);
2298 static cp_expr cp_parser_braced_list
2299 (cp_parser
*, bool*);
2300 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2301 (cp_parser
*, bool *, bool *);
2303 static void cp_parser_ctor_initializer_opt_and_function_body
2304 (cp_parser
*, bool);
2306 static tree cp_parser_late_parsing_omp_declare_simd
2307 (cp_parser
*, tree
);
2309 static tree cp_parser_late_parsing_oacc_routine
2310 (cp_parser
*, tree
);
2312 static tree synthesize_implicit_template_parm
2313 (cp_parser
*, tree
);
2314 static tree finish_fully_implicit_template
2315 (cp_parser
*, tree
);
2316 static void abort_fully_implicit_template
2319 /* Classes [gram.class] */
2321 static tree cp_parser_class_name
2322 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2323 static tree cp_parser_class_specifier
2325 static tree cp_parser_class_head
2326 (cp_parser
*, bool *);
2327 static enum tag_types cp_parser_class_key
2329 static void cp_parser_type_parameter_key
2330 (cp_parser
* parser
);
2331 static void cp_parser_member_specification_opt
2333 static void cp_parser_member_declaration
2335 static tree cp_parser_pure_specifier
2337 static tree cp_parser_constant_initializer
2340 /* Derived classes [gram.class.derived] */
2342 static tree cp_parser_base_clause
2344 static tree cp_parser_base_specifier
2347 /* Special member functions [gram.special] */
2349 static tree cp_parser_conversion_function_id
2351 static tree cp_parser_conversion_type_id
2353 static cp_declarator
*cp_parser_conversion_declarator_opt
2355 static void cp_parser_ctor_initializer_opt
2357 static void cp_parser_mem_initializer_list
2359 static tree cp_parser_mem_initializer
2361 static tree cp_parser_mem_initializer_id
2364 /* Overloading [gram.over] */
2366 static cp_expr cp_parser_operator_function_id
2368 static cp_expr cp_parser_operator
2369 (cp_parser
*, location_t
);
2371 /* Templates [gram.temp] */
2373 static void cp_parser_template_declaration
2374 (cp_parser
*, bool);
2375 static tree cp_parser_template_parameter_list
2377 static tree cp_parser_template_parameter
2378 (cp_parser
*, bool *, bool *);
2379 static tree cp_parser_type_parameter
2380 (cp_parser
*, bool *);
2381 static tree cp_parser_template_id
2382 (cp_parser
*, bool, bool, enum tag_types
, bool);
2383 static tree cp_parser_template_id_expr
2384 (cp_parser
*, bool, bool, bool);
2385 static tree cp_parser_template_name
2386 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2387 static tree cp_parser_template_argument_list
2389 static tree cp_parser_template_argument
2391 static void cp_parser_explicit_instantiation
2393 static void cp_parser_explicit_specialization
2396 /* Exception handling [gram.except] */
2398 static tree cp_parser_try_block
2400 static void cp_parser_function_try_block
2402 static void cp_parser_handler_seq
2404 static void cp_parser_handler
2406 static tree cp_parser_exception_declaration
2408 static tree cp_parser_throw_expression
2410 static tree cp_parser_exception_specification_opt
2411 (cp_parser
*, cp_parser_flags
);
2412 static tree cp_parser_type_id_list
2414 static tree cp_parser_noexcept_specification_opt
2415 (cp_parser
*, cp_parser_flags
, bool, bool *, bool);
2417 /* GNU Extensions */
2419 static tree cp_parser_asm_specification_opt
2421 static tree cp_parser_asm_operand_list
2423 static tree cp_parser_asm_clobber_list
2425 static tree cp_parser_asm_label_list
2427 static bool cp_next_tokens_can_be_attribute_p
2429 static bool cp_next_tokens_can_be_gnu_attribute_p
2431 static bool cp_next_tokens_can_be_std_attribute_p
2433 static bool cp_nth_tokens_can_be_std_attribute_p
2434 (cp_parser
*, size_t);
2435 static bool cp_nth_tokens_can_be_gnu_attribute_p
2436 (cp_parser
*, size_t);
2437 static bool cp_nth_tokens_can_be_attribute_p
2438 (cp_parser
*, size_t);
2439 static tree cp_parser_attributes_opt
2441 static tree cp_parser_gnu_attributes_opt
2443 static tree cp_parser_gnu_attribute_list
2444 (cp_parser
*, bool = false);
2445 static tree cp_parser_std_attribute
2446 (cp_parser
*, tree
);
2447 static tree cp_parser_std_attribute_spec
2449 static tree cp_parser_std_attribute_spec_seq
2451 static size_t cp_parser_skip_attributes_opt
2452 (cp_parser
*, size_t);
2453 static bool cp_parser_extension_opt
2454 (cp_parser
*, int *);
2455 static void cp_parser_label_declaration
2458 /* Concept Extensions */
2460 static tree cp_parser_concept_definition
2462 static tree cp_parser_constraint_expression
2464 static tree cp_parser_requires_clause_opt
2465 (cp_parser
*, bool);
2466 static tree cp_parser_requires_expression
2468 static tree cp_parser_requirement_parameter_list
2470 static tree cp_parser_requirement_body
2472 static tree cp_parser_requirement_seq
2474 static tree cp_parser_requirement
2476 static tree cp_parser_simple_requirement
2478 static tree cp_parser_compound_requirement
2480 static tree cp_parser_type_requirement
2482 static tree cp_parser_nested_requirement
2485 /* Transactional Memory Extensions */
2487 static tree cp_parser_transaction
2488 (cp_parser
*, cp_token
*);
2489 static tree cp_parser_transaction_expression
2490 (cp_parser
*, enum rid
);
2491 static void cp_parser_function_transaction
2492 (cp_parser
*, enum rid
);
2493 static tree cp_parser_transaction_cancel
2496 /* Coroutine extensions. */
2498 static tree cp_parser_yield_expression
2502 enum pragma_context
{
2509 static bool cp_parser_pragma
2510 (cp_parser
*, enum pragma_context
, bool *);
2512 /* Objective-C++ Productions */
2514 static tree cp_parser_objc_message_receiver
2516 static tree cp_parser_objc_message_args
2518 static tree cp_parser_objc_message_expression
2520 static cp_expr cp_parser_objc_encode_expression
2522 static tree cp_parser_objc_defs_expression
2524 static tree cp_parser_objc_protocol_expression
2526 static tree cp_parser_objc_selector_expression
2528 static cp_expr cp_parser_objc_expression
2530 static bool cp_parser_objc_selector_p
2532 static tree cp_parser_objc_selector
2534 static tree cp_parser_objc_protocol_refs_opt
2536 static void cp_parser_objc_declaration
2537 (cp_parser
*, tree
);
2538 static tree cp_parser_objc_statement
2540 static bool cp_parser_objc_valid_prefix_attributes
2541 (cp_parser
*, tree
*);
2542 static void cp_parser_objc_at_property_declaration
2544 static void cp_parser_objc_at_synthesize_declaration
2546 static void cp_parser_objc_at_dynamic_declaration
2548 static tree cp_parser_objc_struct_declaration
2551 /* Utility Routines */
2553 static cp_expr cp_parser_lookup_name
2554 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2555 static tree cp_parser_lookup_name_simple
2556 (cp_parser
*, tree
, location_t
);
2557 static tree cp_parser_maybe_treat_template_as_class
2559 static bool cp_parser_check_declarator_template_parameters
2560 (cp_parser
*, cp_declarator
*, location_t
);
2561 static bool cp_parser_check_template_parameters
2562 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2563 static cp_expr cp_parser_simple_cast_expression
2565 static tree cp_parser_global_scope_opt
2566 (cp_parser
*, bool);
2567 static bool cp_parser_constructor_declarator_p
2568 (cp_parser
*, cp_parser_flags
, bool);
2569 static tree cp_parser_function_definition_from_specifiers_and_declarator
2570 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2571 static tree cp_parser_function_definition_after_declarator
2572 (cp_parser
*, bool);
2573 static bool cp_parser_template_declaration_after_export
2574 (cp_parser
*, bool);
2575 static void cp_parser_perform_template_parameter_access_checks
2576 (vec
<deferred_access_check
, va_gc
> *);
2577 static tree cp_parser_single_declaration
2578 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2579 static cp_expr cp_parser_functional_cast
2580 (cp_parser
*, tree
);
2581 static tree cp_parser_save_member_function_body
2582 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2583 static tree cp_parser_save_nsdmi
2585 static tree cp_parser_enclosed_template_argument_list
2587 static void cp_parser_save_default_args
2588 (cp_parser
*, tree
);
2589 static void cp_parser_late_parsing_for_member
2590 (cp_parser
*, tree
);
2591 static tree cp_parser_late_parse_one_default_arg
2592 (cp_parser
*, tree
, tree
, tree
);
2593 static void cp_parser_late_parsing_nsdmi
2594 (cp_parser
*, tree
);
2595 static void cp_parser_late_parsing_default_args
2596 (cp_parser
*, tree
);
2597 static tree cp_parser_sizeof_operand
2598 (cp_parser
*, enum rid
);
2599 static cp_expr cp_parser_trait_expr
2600 (cp_parser
*, enum rid
);
2601 static bool cp_parser_declares_only_class_p
2603 static void cp_parser_set_storage_class
2604 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2605 static void cp_parser_set_decl_spec_type
2606 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2607 static void set_and_check_decl_spec_loc
2608 (cp_decl_specifier_seq
*decl_specs
,
2609 cp_decl_spec ds
, cp_token
*);
2610 static bool cp_parser_friend_p
2611 (const cp_decl_specifier_seq
*);
2612 static void cp_parser_required_error
2613 (cp_parser
*, required_token
, bool, location_t
);
2614 static cp_token
*cp_parser_require
2615 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2616 static cp_token
*cp_parser_require_keyword
2617 (cp_parser
*, enum rid
, required_token
);
2618 static bool cp_parser_token_starts_function_definition_p
2620 static bool cp_parser_next_token_starts_class_definition_p
2622 static bool cp_parser_next_token_ends_template_argument_p
2624 static bool cp_parser_nth_token_starts_template_argument_list_p
2625 (cp_parser
*, size_t);
2626 static enum tag_types cp_parser_token_is_class_key
2628 static enum tag_types cp_parser_token_is_type_parameter_key
2630 static void cp_parser_maybe_warn_enum_key (cp_parser
*, location_t
, tree
, rid
);
2631 static void cp_parser_check_class_key
2632 (cp_parser
*, location_t
, enum tag_types
, tree type
, bool, bool);
2633 static void cp_parser_check_access_in_redeclaration
2634 (tree type
, location_t location
);
2635 static bool cp_parser_optional_template_keyword
2637 static void cp_parser_pre_parsed_nested_name_specifier
2639 static bool cp_parser_cache_group
2640 (cp_parser
*, enum cpp_ttype
, unsigned);
2641 static tree cp_parser_cache_defarg
2642 (cp_parser
*parser
, bool nsdmi
);
2643 static void cp_parser_parse_tentatively
2645 static void cp_parser_commit_to_tentative_parse
2647 static void cp_parser_commit_to_topmost_tentative_parse
2649 static void cp_parser_abort_tentative_parse
2651 static bool cp_parser_parse_definitely
2653 static inline bool cp_parser_parsing_tentatively
2655 static bool cp_parser_uncommitted_to_tentative_parse_p
2657 static void cp_parser_error
2658 (cp_parser
*, const char *);
2659 static void cp_parser_name_lookup_error
2660 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2661 static bool cp_parser_simulate_error
2663 static bool cp_parser_check_type_definition
2665 static void cp_parser_check_for_definition_in_return_type
2666 (cp_declarator
*, tree
, location_t type_location
);
2667 static void cp_parser_check_for_invalid_template_id
2668 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2669 static bool cp_parser_non_integral_constant_expression
2670 (cp_parser
*, non_integral_constant
);
2671 static void cp_parser_diagnose_invalid_type_name
2672 (cp_parser
*, tree
, location_t
);
2673 static bool cp_parser_parse_and_diagnose_invalid_type_name
2675 static int cp_parser_skip_to_closing_parenthesis
2676 (cp_parser
*, bool, bool, bool);
2677 static void cp_parser_skip_to_end_of_statement
2679 static void cp_parser_consume_semicolon_at_end_of_statement
2681 static void cp_parser_skip_to_end_of_block_or_statement
2683 static bool cp_parser_skip_to_closing_brace
2685 static void cp_parser_skip_to_end_of_template_parameter_list
2687 static void cp_parser_skip_to_pragma_eol
2688 (cp_parser
*, cp_token
*);
2689 static bool cp_parser_error_occurred
2691 static bool cp_parser_allow_gnu_extensions_p
2693 static bool cp_parser_is_pure_string_literal
2695 static bool cp_parser_is_string_literal
2697 static bool cp_parser_is_keyword
2698 (cp_token
*, enum rid
);
2699 static tree cp_parser_make_typename_type
2700 (cp_parser
*, tree
, location_t location
);
2701 static cp_declarator
* cp_parser_make_indirect_declarator
2702 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2703 static bool cp_parser_compound_literal_p
2705 static bool cp_parser_array_designator_p
2707 static bool cp_parser_init_statement_p
2709 static bool cp_parser_skip_to_closing_square_bracket
2711 static size_t cp_parser_skip_balanced_tokens (cp_parser
*, size_t);
2713 // -------------------------------------------------------------------------- //
2714 // Unevaluated Operand Guard
2716 // Implementation of an RAII helper for unevaluated operand parsing.
2717 cp_unevaluated::cp_unevaluated ()
2719 ++cp_unevaluated_operand
;
2720 ++c_inhibit_evaluation_warnings
;
2723 cp_unevaluated::~cp_unevaluated ()
2725 --c_inhibit_evaluation_warnings
;
2726 --cp_unevaluated_operand
;
2729 // -------------------------------------------------------------------------- //
2730 // Tentative Parsing
2732 /* Returns nonzero if we are parsing tentatively. */
2735 cp_parser_parsing_tentatively (cp_parser
* parser
)
2737 return parser
->context
->next
!= NULL
;
2740 /* Returns nonzero if TOKEN is a string literal. */
2743 cp_parser_is_pure_string_literal (cp_token
* token
)
2745 return (token
->type
== CPP_STRING
||
2746 token
->type
== CPP_STRING16
||
2747 token
->type
== CPP_STRING32
||
2748 token
->type
== CPP_WSTRING
||
2749 token
->type
== CPP_UTF8STRING
);
2752 /* Returns nonzero if TOKEN is a string literal
2753 of a user-defined string literal. */
2756 cp_parser_is_string_literal (cp_token
* token
)
2758 return (cp_parser_is_pure_string_literal (token
) ||
2759 token
->type
== CPP_STRING_USERDEF
||
2760 token
->type
== CPP_STRING16_USERDEF
||
2761 token
->type
== CPP_STRING32_USERDEF
||
2762 token
->type
== CPP_WSTRING_USERDEF
||
2763 token
->type
== CPP_UTF8STRING_USERDEF
);
2766 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2769 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2771 return token
->keyword
== keyword
;
2774 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2777 static enum pragma_kind
2778 cp_parser_pragma_kind (cp_token
*token
)
2780 if (token
->type
!= CPP_PRAGMA
)
2782 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2783 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2786 /* Helper function for cp_parser_error.
2787 Having peeked a token of kind TOK1_KIND that might signify
2788 a conflict marker, peek successor tokens to determine
2789 if we actually do have a conflict marker.
2790 Specifically, we consider a run of 7 '<', '=' or '>' characters
2791 at the start of a line as a conflict marker.
2792 These come through the lexer as three pairs and a single,
2793 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2794 If it returns true, *OUT_LOC is written to with the location/range
2798 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2799 location_t
*out_loc
)
2801 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2802 if (token2
->type
!= tok1_kind
)
2804 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2805 if (token3
->type
!= tok1_kind
)
2807 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2808 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2811 /* It must be at the start of the line. */
2812 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2813 if (LOCATION_COLUMN (start_loc
) != 1)
2816 /* We have a conflict marker. Construct a location of the form:
2819 with start == caret, finishing at the end of the marker. */
2820 location_t finish_loc
= get_finish (token4
->location
);
2821 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2826 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2830 get_matching_symbol (required_token token_desc
)
2837 case RT_CLOSE_BRACE
:
2839 case RT_CLOSE_PAREN
:
2844 /* Attempt to convert TOKEN_DESC from a required_token to an
2845 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2847 static enum cpp_ttype
2848 get_required_cpp_ttype (required_token token_desc
)
2853 return CPP_SEMICOLON
;
2855 return CPP_OPEN_PAREN
;
2856 case RT_CLOSE_BRACE
:
2857 return CPP_CLOSE_BRACE
;
2859 return CPP_OPEN_BRACE
;
2860 case RT_CLOSE_SQUARE
:
2861 return CPP_CLOSE_SQUARE
;
2862 case RT_OPEN_SQUARE
:
2863 return CPP_OPEN_SQUARE
;
2868 case RT_CLOSE_PAREN
:
2869 return CPP_CLOSE_PAREN
;
2872 /* Use CPP_EOF as a "no completions possible" code. */
2878 /* Subroutine of cp_parser_error and cp_parser_required_error.
2880 Issue a diagnostic of the form
2881 FILE:LINE: MESSAGE before TOKEN
2882 where TOKEN is the next token in the input stream. MESSAGE
2883 (specified by the caller) is usually of the form "expected
2886 This bypasses the check for tentative passing, and potentially
2887 adds material needed by cp_parser_required_error.
2889 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2890 suggesting insertion of the missing token.
2892 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2893 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2897 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2898 required_token missing_token_desc
,
2899 location_t matching_location
)
2901 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2902 /* This diagnostic makes more sense if it is tagged to the line
2903 of the token we just peeked at. */
2904 cp_lexer_set_source_position_from_token (token
);
2906 if (token
->type
== CPP_PRAGMA
)
2908 error_at (token
->location
,
2909 "%<#pragma%> is not allowed here");
2910 cp_parser_skip_to_pragma_eol (parser
, token
);
2914 /* If this is actually a conflict marker, report it as such. */
2915 if (token
->type
== CPP_LSHIFT
2916 || token
->type
== CPP_RSHIFT
2917 || token
->type
== CPP_EQ_EQ
)
2920 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2922 error_at (loc
, "version control conflict marker in file");
2923 expanded_location token_exploc
= expand_location (token
->location
);
2924 /* Consume tokens until the end of the source line. */
2927 cp_lexer_consume_token (parser
->lexer
);
2928 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2929 if (next
->type
== CPP_EOF
)
2931 if (next
->location
== UNKNOWN_LOCATION
2932 || loc
== UNKNOWN_LOCATION
)
2935 expanded_location next_exploc
= expand_location (next
->location
);
2936 if (next_exploc
.file
!= token_exploc
.file
)
2938 if (next_exploc
.line
!= token_exploc
.line
)
2945 auto_diagnostic_group d
;
2946 gcc_rich_location
richloc (input_location
);
2948 bool added_matching_location
= false;
2950 if (missing_token_desc
!= RT_NONE
)
2951 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
2953 /* Potentially supply a fix-it hint, suggesting to add the
2954 missing token immediately after the *previous* token.
2955 This may move the primary location within richloc. */
2956 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2957 location_t prev_token_loc
= prev_token
->location
;
2958 maybe_suggest_missing_token_insertion (&richloc
, ttype
,
2961 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2962 Attempt to consolidate diagnostics by printing it as a
2963 secondary range within the main diagnostic. */
2964 if (matching_location
!= UNKNOWN_LOCATION
)
2965 added_matching_location
2966 = richloc
.add_location_if_nearby (matching_location
);
2969 /* If we were parsing a string-literal and there is an unknown name
2970 token right after, then check to see if that could also have been
2971 a literal string by checking the name against a list of known
2972 standard string literal constants defined in header files. If
2973 there is one, then add that as an hint to the error message. */
2975 if (token
->type
== CPP_NAME
)
2976 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
2977 if (cp_parser_is_string_literal (prev_token
))
2979 tree name
= token
->u
.value
;
2980 const char *token_name
= IDENTIFIER_POINTER (name
);
2981 const char *header_hint
2982 = get_cp_stdlib_header_for_string_macro_name (token_name
);
2983 if (header_hint
!= NULL
)
2984 h
= name_hint (NULL
, new suggest_missing_header (token
->location
,
2989 /* Actually emit the error. */
2990 c_parse_error (gmsgid
,
2991 /* Because c_parser_error does not understand
2992 CPP_KEYWORD, keywords are treated like
2994 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2995 token
->u
.value
, token
->flags
, &richloc
);
2997 if (missing_token_desc
!= RT_NONE
)
2999 /* If we weren't able to consolidate matching_location, then
3000 print it as a secondary diagnostic. */
3001 if (matching_location
!= UNKNOWN_LOCATION
3002 && !added_matching_location
)
3003 inform (matching_location
, "to match this %qs",
3004 get_matching_symbol (missing_token_desc
));
3008 /* If not parsing tentatively, issue a diagnostic of the form
3009 FILE:LINE: MESSAGE before TOKEN
3010 where TOKEN is the next token in the input stream. MESSAGE
3011 (specified by the caller) is usually of the form "expected
3015 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
3017 if (!cp_parser_simulate_error (parser
))
3018 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
3021 /* Issue an error about name-lookup failing. NAME is the
3022 IDENTIFIER_NODE DECL is the result of
3023 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3024 the thing that we hoped to find. */
3027 cp_parser_name_lookup_error (cp_parser
* parser
,
3030 name_lookup_error desired
,
3031 location_t location
)
3033 /* If name lookup completely failed, tell the user that NAME was not
3035 if (decl
== error_mark_node
)
3037 if (parser
->scope
&& parser
->scope
!= global_namespace
)
3038 error_at (location
, "%<%E::%E%> has not been declared",
3039 parser
->scope
, name
);
3040 else if (parser
->scope
== global_namespace
)
3041 error_at (location
, "%<::%E%> has not been declared", name
);
3042 else if (parser
->object_scope
3043 && !CLASS_TYPE_P (parser
->object_scope
))
3044 error_at (location
, "request for member %qE in non-class type %qT",
3045 name
, parser
->object_scope
);
3046 else if (parser
->object_scope
)
3047 error_at (location
, "%<%T::%E%> has not been declared",
3048 parser
->object_scope
, name
);
3050 error_at (location
, "%qE has not been declared", name
);
3052 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
3057 error_at (location
, "%<%E::%E%> is not a type",
3058 parser
->scope
, name
);
3061 error_at (location
, "%<%E::%E%> is not a class or namespace",
3062 parser
->scope
, name
);
3066 "%<%E::%E%> is not a class, namespace, or enumeration",
3067 parser
->scope
, name
);
3074 else if (parser
->scope
== global_namespace
)
3079 error_at (location
, "%<::%E%> is not a type", name
);
3082 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3086 "%<::%E%> is not a class, namespace, or enumeration",
3098 error_at (location
, "%qE is not a type", name
);
3101 error_at (location
, "%qE is not a class or namespace", name
);
3105 "%qE is not a class, namespace, or enumeration", name
);
3113 /* If we are parsing tentatively, remember that an error has occurred
3114 during this tentative parse. Returns true if the error was
3115 simulated; false if a message should be issued by the caller. */
3118 cp_parser_simulate_error (cp_parser
* parser
)
3120 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3122 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3128 /* This function is called when a type is defined. If type
3129 definitions are forbidden at this point, an error message is
3133 cp_parser_check_type_definition (cp_parser
* parser
)
3135 /* If types are forbidden here, issue a message. */
3136 if (parser
->type_definition_forbidden_message
)
3138 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3139 or %qs in the message need to be interpreted. */
3140 error (parser
->type_definition_forbidden_message
,
3141 parser
->type_definition_forbidden_message_arg
);
3147 /* This function is called when the DECLARATOR is processed. The TYPE
3148 was a type defined in the decl-specifiers. If it is invalid to
3149 define a type in the decl-specifiers for DECLARATOR, an error is
3150 issued. TYPE_LOCATION is the location of TYPE and is used
3151 for error reporting. */
3154 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3155 tree type
, location_t type_location
)
3157 /* [dcl.fct] forbids type definitions in return types.
3158 Unfortunately, it's not easy to know whether or not we are
3159 processing a return type until after the fact. */
3161 && (declarator
->kind
== cdk_pointer
3162 || declarator
->kind
== cdk_reference
3163 || declarator
->kind
== cdk_ptrmem
))
3164 declarator
= declarator
->declarator
;
3166 && declarator
->kind
== cdk_function
)
3168 error_at (type_location
,
3169 "new types may not be defined in a return type");
3170 inform (type_location
,
3171 "(perhaps a semicolon is missing after the definition of %qT)",
3176 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3177 "<" in any valid C++ program. If the next token is indeed "<",
3178 issue a message warning the user about what appears to be an
3179 invalid attempt to form a template-id. LOCATION is the location
3180 of the type-specifier (TYPE) */
3183 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3185 enum tag_types tag_type
,
3186 location_t location
)
3188 cp_token_position start
= 0;
3190 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3192 if (TREE_CODE (type
) == TYPE_DECL
)
3193 type
= TREE_TYPE (type
);
3194 if (TYPE_P (type
) && !template_placeholder_p (type
))
3195 error_at (location
, "%qT is not a template", type
);
3196 else if (identifier_p (type
))
3198 if (tag_type
!= none_type
)
3199 error_at (location
, "%qE is not a class template", type
);
3201 error_at (location
, "%qE is not a template", type
);
3204 error_at (location
, "invalid template-id");
3205 /* Remember the location of the invalid "<". */
3206 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3207 start
= cp_lexer_token_position (parser
->lexer
, true);
3208 /* Consume the "<". */
3209 cp_lexer_consume_token (parser
->lexer
);
3210 /* Parse the template arguments. */
3211 cp_parser_enclosed_template_argument_list (parser
);
3212 /* Permanently remove the invalid template arguments so that
3213 this error message is not issued again. */
3215 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3219 /* If parsing an integral constant-expression, issue an error message
3220 about the fact that THING appeared and return true. Otherwise,
3221 return false. In either case, set
3222 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3225 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3226 non_integral_constant thing
)
3228 parser
->non_integral_constant_expression_p
= true;
3229 if (parser
->integral_constant_expression_p
)
3231 if (!parser
->allow_non_integral_constant_expression_p
)
3233 const char *msg
= NULL
;
3237 pedwarn (input_location
, OPT_Wpedantic
,
3238 "ISO C++ forbids using a floating-point literal "
3239 "in a constant-expression");
3242 error ("a cast to a type other than an integral or "
3243 "enumeration type cannot appear in a "
3244 "constant-expression");
3247 error ("%<typeid%> operator "
3248 "cannot appear in a constant-expression");
3251 error ("non-constant compound literals "
3252 "cannot appear in a constant-expression");
3255 error ("a function call "
3256 "cannot appear in a constant-expression");
3259 error ("an increment "
3260 "cannot appear in a constant-expression");
3263 error ("an decrement "
3264 "cannot appear in a constant-expression");
3267 error ("an array reference "
3268 "cannot appear in a constant-expression");
3270 case NIC_ADDR_LABEL
:
3271 error ("the address of a label "
3272 "cannot appear in a constant-expression");
3274 case NIC_OVERLOADED
:
3275 error ("calls to overloaded operators "
3276 "cannot appear in a constant-expression");
3278 case NIC_ASSIGNMENT
:
3279 error ("an assignment cannot appear in a constant-expression");
3282 error ("a comma operator "
3283 "cannot appear in a constant-expression");
3285 case NIC_CONSTRUCTOR
:
3286 error ("a call to a constructor "
3287 "cannot appear in a constant-expression");
3289 case NIC_TRANSACTION
:
3290 error ("a transaction expression "
3291 "cannot appear in a constant-expression");
3297 msg
= "__FUNCTION__";
3299 case NIC_PRETTY_FUNC
:
3300 msg
= "__PRETTY_FUNCTION__";
3320 case NIC_PREINCREMENT
:
3323 case NIC_PREDECREMENT
:
3336 error ("%qs cannot appear in a constant-expression", msg
);
3343 /* Emit a diagnostic for an invalid type name. This function commits
3344 to the current active tentative parse, if any. (Otherwise, the
3345 problematic construct might be encountered again later, resulting
3346 in duplicate error messages.) LOCATION is the location of ID. */
3349 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3350 location_t location
)
3352 tree decl
, ambiguous_decls
;
3353 cp_parser_commit_to_tentative_parse (parser
);
3354 /* Try to lookup the identifier. */
3355 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3356 /*is_template=*/false,
3357 /*is_namespace=*/false,
3358 /*check_dependency=*/true,
3359 &ambiguous_decls
, location
);
3360 if (ambiguous_decls
)
3361 /* If the lookup was ambiguous, an error will already have
3364 /* If the lookup found a template-name, it means that the user forgot
3365 to specify an argument list. Emit a useful error message. */
3366 if (DECL_TYPE_TEMPLATE_P (decl
))
3368 auto_diagnostic_group d
;
3370 "invalid use of template-name %qE without an argument list",
3372 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3373 inform (location
, "class template argument deduction is only available "
3374 "with %<-std=c++17%> or %<-std=gnu++17%>");
3375 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3377 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3378 error_at (location
, "invalid use of destructor %qD as a type", id
);
3379 else if (TREE_CODE (decl
) == TYPE_DECL
)
3380 /* Something like 'unsigned A a;' */
3381 error_at (location
, "invalid combination of multiple type-specifiers");
3382 else if (!parser
->scope
)
3384 /* Issue an error message. */
3385 auto_diagnostic_group d
;
3387 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3388 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3389 if (const char *suggestion
= hint
.suggestion ())
3391 gcc_rich_location
richloc (location
);
3392 richloc
.add_fixit_replace (suggestion
);
3394 "%qE does not name a type; did you mean %qs?",
3398 error_at (location
, "%qE does not name a type", id
);
3399 /* If we're in a template class, it's possible that the user was
3400 referring to a type from a base class. For example:
3402 template <typename T> struct A { typedef T X; };
3403 template <typename T> struct B : public A<T> { X x; };
3405 The user should have said "typename A<T>::X". */
3406 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3407 inform (location
, "C++11 %<constexpr%> only available with "
3408 "%<-std=c++11%> or %<-std=gnu++11%>");
3409 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3410 inform (location
, "C++11 %<noexcept%> only available with "
3411 "%<-std=c++11%> or %<-std=gnu++11%>");
3412 else if (cxx_dialect
< cxx11
3413 && TREE_CODE (id
) == IDENTIFIER_NODE
3414 && id_equal (id
, "thread_local"))
3415 inform (location
, "C++11 %<thread_local%> only available with "
3416 "%<-std=c++11%> or %<-std=gnu++11%>");
3417 else if (cxx_dialect
< cxx20
&& id
== ridpointers
[(int)RID_CONSTINIT
])
3418 inform (location
, "C++20 %<constinit%> only available with "
3419 "%<-std=c++20%> or %<-std=gnu++20%>");
3420 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3421 inform (location
, "%<concept%> only available with %<-std=c++20%> or "
3423 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_REQUIRES
])
3424 inform (location
, "%<requires%> only available with %<-std=c++20%> or "
3426 else if (processing_template_decl
&& current_class_type
3427 && TYPE_BINFO (current_class_type
))
3429 for (tree b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3430 b
; b
= TREE_CHAIN (b
))
3432 tree base_type
= BINFO_TYPE (b
);
3433 if (CLASS_TYPE_P (base_type
)
3434 && dependent_type_p (base_type
))
3436 /* Go from a particular instantiation of the
3437 template (which will have an empty TYPE_FIELDs),
3438 to the main version. */
3439 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3440 for (tree field
= TYPE_FIELDS (base_type
);
3441 field
; field
= DECL_CHAIN (field
))
3442 if (TREE_CODE (field
) == TYPE_DECL
3443 && DECL_NAME (field
) == id
)
3446 "(perhaps %<typename %T::%E%> was intended)",
3447 BINFO_TYPE (b
), id
);
3455 /* Here we diagnose qualified-ids where the scope is actually correct,
3456 but the identifier does not resolve to a valid type name. */
3457 else if (parser
->scope
!= error_mark_node
)
3459 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3461 auto_diagnostic_group d
;
3463 if (decl
== error_mark_node
)
3464 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3466 const char *suggestion
= hint
.suggestion ();
3467 gcc_rich_location
richloc (location_of (id
));
3469 richloc
.add_fixit_replace (suggestion
);
3470 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3474 "%qE in namespace %qE does not name a template"
3475 " type; did you mean %qs?",
3476 id
, parser
->scope
, suggestion
);
3479 "%qE in namespace %qE does not name a template type",
3482 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3486 "%qE in namespace %qE does not name a template"
3487 " type; did you mean %qs?",
3488 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3491 "%qE in namespace %qE does not name a template"
3493 TREE_OPERAND (id
, 0), parser
->scope
);
3499 "%qE in namespace %qE does not name a type"
3500 "; did you mean %qs?",
3501 id
, parser
->scope
, suggestion
);
3504 "%qE in namespace %qE does not name a type",
3508 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3510 else if (CLASS_TYPE_P (parser
->scope
)
3511 && constructor_name_p (id
, parser
->scope
))
3514 auto_diagnostic_group d
;
3515 error_at (location
, "%<%T::%E%> names the constructor, not"
3516 " the type", parser
->scope
, id
);
3517 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3518 error_at (location
, "and %qT has no template constructors",
3521 else if (TYPE_P (parser
->scope
)
3522 && dependent_scope_p (parser
->scope
))
3524 gcc_rich_location
richloc (location
);
3525 richloc
.add_fixit_insert_before ("typename ");
3526 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3528 "need %<typename%> before %<%T::%D::%E%> because "
3529 "%<%T::%D%> is a dependent scope",
3530 TYPE_CONTEXT (parser
->scope
),
3531 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3533 TYPE_CONTEXT (parser
->scope
),
3534 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3536 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3537 "%qT is a dependent scope",
3538 parser
->scope
, id
, parser
->scope
);
3540 else if (TYPE_P (parser
->scope
))
3542 auto_diagnostic_group d
;
3543 if (!COMPLETE_TYPE_P (parser
->scope
))
3544 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3546 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3547 error_at (location_of (id
),
3548 "%qE in %q#T does not name a template type",
3550 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3551 error_at (location_of (id
),
3552 "%qE in %q#T does not name a template type",
3553 TREE_OPERAND (id
, 0), parser
->scope
);
3555 error_at (location_of (id
),
3556 "%qE in %q#T does not name a type",
3559 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3566 /* Check for a common situation where a type-name should be present,
3567 but is not, and issue a sensible error message. Returns true if an
3568 invalid type-name was detected.
3570 The situation handled by this function are variable declarations of the
3571 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3572 Usually, `ID' should name a type, but if we got here it means that it
3573 does not. We try to emit the best possible error message depending on
3574 how exactly the id-expression looks like. */
3577 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3580 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3582 /* Avoid duplicate error about ambiguous lookup. */
3583 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3585 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3586 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3590 cp_parser_parse_tentatively (parser
);
3591 id
= cp_parser_id_expression (parser
,
3592 /*template_keyword_p=*/false,
3593 /*check_dependency_p=*/true,
3594 /*template_p=*/NULL
,
3595 /*declarator_p=*/false,
3596 /*optional_p=*/false);
3597 /* If the next token is a (, this is a function with no explicit return
3598 type, i.e. constructor, destructor or conversion op. */
3599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3600 || TREE_CODE (id
) == TYPE_DECL
)
3602 cp_parser_abort_tentative_parse (parser
);
3605 if (!cp_parser_parse_definitely (parser
))
3608 /* Emit a diagnostic for the invalid type. */
3609 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3611 /* If we aren't in the middle of a declarator (i.e. in a
3612 parameter-declaration-clause), skip to the end of the declaration;
3613 there's no point in trying to process it. */
3614 if (!parser
->in_declarator_p
)
3615 cp_parser_skip_to_end_of_block_or_statement (parser
);
3619 /* Consume tokens up to, and including, the next non-nested closing `)'.
3620 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3621 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3622 found an unnested token of that type. */
3625 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3630 unsigned paren_depth
= 0;
3631 unsigned brace_depth
= 0;
3632 unsigned square_depth
= 0;
3633 unsigned condop_depth
= 0;
3635 if (recovering
&& or_ttype
== CPP_EOF
3636 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3641 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3643 /* Have we found what we're looking for before the closing paren? */
3644 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3645 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3648 switch (token
->type
)
3650 case CPP_PRAGMA_EOL
:
3651 if (!parser
->lexer
->in_pragma
)
3655 /* If we've run out of tokens, then there is no closing `)'. */
3658 /* This is good for lambda expression capture-lists. */
3659 case CPP_OPEN_SQUARE
:
3662 case CPP_CLOSE_SQUARE
:
3663 if (!square_depth
--)
3668 /* This matches the processing in skip_to_end_of_statement. */
3673 case CPP_OPEN_BRACE
:
3676 case CPP_CLOSE_BRACE
:
3681 case CPP_OPEN_PAREN
:
3686 case CPP_CLOSE_PAREN
:
3687 if (!brace_depth
&& !paren_depth
--)
3690 cp_lexer_consume_token (parser
->lexer
);
3696 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3701 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3706 /* We fell into a pragma. Skip it, and continue. */
3707 cp_parser_skip_to_pragma_eol (parser
, recovering
? token
: nullptr);
3714 /* Consume the token. */
3715 cp_lexer_consume_token (parser
->lexer
);
3719 /* Consume tokens up to, and including, the next non-nested closing `)'.
3720 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3721 are doing error recovery. Returns -1 if OR_COMMA is true and we
3722 found an unnested token of that type. */
3725 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3730 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3731 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3732 ttype
, consume_paren
);
3735 /* Consume tokens until we reach the end of the current statement.
3736 Normally, that will be just before consuming a `;'. However, if a
3737 non-nested `}' comes first, then we stop before consuming that. */
3740 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3742 unsigned nesting_depth
= 0;
3744 /* Unwind generic function template scope if necessary. */
3745 if (parser
->fully_implicit_function_template_p
)
3746 abort_fully_implicit_template (parser
);
3750 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3752 switch (token
->type
)
3754 case CPP_PRAGMA_EOL
:
3755 if (!parser
->lexer
->in_pragma
)
3759 /* If we've run out of tokens, stop. */
3763 /* If the next token is a `;', we have reached the end of the
3769 case CPP_CLOSE_BRACE
:
3770 /* If this is a non-nested '}', stop before consuming it.
3771 That way, when confronted with something like:
3775 we stop before consuming the closing '}', even though we
3776 have not yet reached a `;'. */
3777 if (nesting_depth
== 0)
3780 /* If it is the closing '}' for a block that we have
3781 scanned, stop -- but only after consuming the token.
3787 we will stop after the body of the erroneously declared
3788 function, but before consuming the following `typedef'
3790 if (--nesting_depth
== 0)
3792 cp_lexer_consume_token (parser
->lexer
);
3797 case CPP_OPEN_BRACE
:
3802 /* We fell into a pragma. Skip it, and continue or return. */
3803 cp_parser_skip_to_pragma_eol (parser
, token
);
3812 /* Consume the token. */
3813 cp_lexer_consume_token (parser
->lexer
);
3817 /* This function is called at the end of a statement or declaration.
3818 If the next token is a semicolon, it is consumed; otherwise, error
3819 recovery is attempted. */
3822 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3824 /* Look for the trailing `;'. */
3825 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3827 /* If there is additional (erroneous) input, skip to the end of
3829 cp_parser_skip_to_end_of_statement (parser
);
3830 /* If the next token is now a `;', consume it. */
3831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3832 cp_lexer_consume_token (parser
->lexer
);
3836 /* Skip tokens until we have consumed an entire block, or until we
3837 have consumed a non-nested `;'. */
3840 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3842 int nesting_depth
= 0;
3844 /* Unwind generic function template scope if necessary. */
3845 if (parser
->fully_implicit_function_template_p
)
3846 abort_fully_implicit_template (parser
);
3848 while (nesting_depth
>= 0)
3850 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3852 switch (token
->type
)
3854 case CPP_PRAGMA_EOL
:
3855 if (!parser
->lexer
->in_pragma
)
3859 /* If we've run out of tokens, stop. */
3863 /* Stop if this is an unnested ';'. */
3868 case CPP_CLOSE_BRACE
:
3869 /* Stop if this is an unnested '}', or closes the outermost
3872 if (nesting_depth
< 0)
3878 case CPP_OPEN_BRACE
:
3884 /* Skip it, and continue or return. */
3885 cp_parser_skip_to_pragma_eol (parser
, token
);
3894 /* Consume the token. */
3895 cp_lexer_consume_token (parser
->lexer
);
3899 /* Skip tokens until a non-nested closing curly brace is the next
3900 token, or there are no more tokens. Return true in the first case,
3904 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3906 unsigned nesting_depth
= 0;
3910 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3912 switch (token
->type
)
3914 case CPP_PRAGMA_EOL
:
3915 if (!parser
->lexer
->in_pragma
)
3919 /* If we've run out of tokens, stop. */
3922 case CPP_CLOSE_BRACE
:
3923 /* If the next token is a non-nested `}', then we have reached
3924 the end of the current block. */
3925 if (nesting_depth
-- == 0)
3929 case CPP_OPEN_BRACE
:
3930 /* If it the next token is a `{', then we are entering a new
3931 block. Consume the entire block. */
3939 /* Consume the token. */
3940 cp_lexer_consume_token (parser
->lexer
);
3944 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3945 parameter is the PRAGMA token, allowing us to purge the entire pragma
3946 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
3947 forwards (not error recovery). */
3950 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3956 /* The preprocessor makes sure that a PRAGMA_EOL token appears
3957 before an EOF token, even when the EOF is on the pragma line.
3958 We should never get here without being inside a deferred
3960 gcc_checking_assert (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
));
3961 token
= cp_lexer_consume_token (parser
->lexer
);
3963 while (token
->type
!= CPP_PRAGMA_EOL
);
3967 /* Ensure that the pragma is not parsed again. */
3968 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3969 parser
->lexer
->in_pragma
= false;
3973 /* Require pragma end of line, resyncing with it as necessary. The
3974 arguments are as for cp_parser_skip_to_pragma_eol. */
3977 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3979 parser
->lexer
->in_pragma
= false;
3980 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3981 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3984 /* This is a simple wrapper around make_typename_type. When the id is
3985 an unresolved identifier node, we can provide a superior diagnostic
3986 using cp_parser_diagnose_invalid_type_name. */
3989 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3990 location_t id_location
)
3993 if (identifier_p (id
))
3995 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3996 /*complain=*/tf_none
);
3997 if (result
== error_mark_node
)
3998 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
4001 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
4004 /* This is a wrapper around the
4005 make_{pointer,ptrmem,reference}_declarator functions that decides
4006 which one to call based on the CODE and CLASS_TYPE arguments. The
4007 CODE argument should be one of the values returned by
4008 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4009 appertain to the pointer or reference. */
4011 static cp_declarator
*
4012 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
4013 cp_cv_quals cv_qualifiers
,
4014 cp_declarator
*target
,
4017 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
4018 return cp_error_declarator
;
4020 if (code
== INDIRECT_REF
)
4021 if (class_type
== NULL_TREE
)
4022 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
4024 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
4025 target
, attributes
);
4026 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
4027 return make_reference_declarator (cv_qualifiers
, target
,
4029 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
4030 return make_reference_declarator (cv_qualifiers
, target
,
4035 /* Create a new C++ parser. */
4038 cp_parser_new (cp_lexer
*lexer
)
4040 /* Initialize the binops_by_token so that we can get the tree
4041 directly from the token. */
4042 for (unsigned i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
4043 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
4045 cp_parser
*parser
= ggc_cleared_alloc
<cp_parser
> ();
4046 parser
->lexer
= lexer
;
4047 parser
->context
= cp_parser_context_new (NULL
);
4049 /* For now, we always accept GNU extensions. */
4050 parser
->allow_gnu_extensions_p
= 1;
4052 /* The `>' token is a greater-than operator, not the end of a
4054 parser
->greater_than_is_operator_p
= true;
4056 parser
->default_arg_ok_p
= true;
4058 /* We are not parsing a constant-expression. */
4059 parser
->integral_constant_expression_p
= false;
4060 parser
->allow_non_integral_constant_expression_p
= false;
4061 parser
->non_integral_constant_expression_p
= false;
4063 /* Local variable names are not forbidden. */
4064 parser
->local_variables_forbidden_p
= 0;
4066 /* We are not processing an `extern "C"' declaration. */
4067 parser
->in_unbraced_linkage_specification_p
= false;
4069 /* We are not processing a declarator. */
4070 parser
->in_declarator_p
= false;
4072 /* We are not processing a template-argument-list. */
4073 parser
->in_template_argument_list_p
= false;
4075 /* We are not in an iteration statement. */
4076 parser
->in_statement
= 0;
4078 /* We are not in a switch statement. */
4079 parser
->in_switch_statement_p
= false;
4081 /* We are not parsing a type-id inside an expression. */
4082 parser
->in_type_id_in_expr_p
= false;
4084 /* String literals should be translated to the execution character set. */
4085 parser
->translate_strings_p
= true;
4087 /* We are not parsing a function body. */
4088 parser
->in_function_body
= false;
4090 /* We can correct until told otherwise. */
4091 parser
->colon_corrects_to_scope_p
= true;
4093 /* The unparsed function queue is empty. */
4094 push_unparsed_function_queues (parser
);
4096 /* There are no classes being defined. */
4097 parser
->num_classes_being_defined
= 0;
4099 /* No template parameters apply. */
4100 parser
->num_template_parameter_lists
= 0;
4102 /* Special parsing data structures. */
4103 parser
->omp_declare_simd
= NULL
;
4104 parser
->oacc_routine
= NULL
;
4106 /* Not declaring an implicit function template. */
4107 parser
->auto_is_implicit_function_template_parm_p
= false;
4108 parser
->fully_implicit_function_template_p
= false;
4109 parser
->implicit_template_parms
= 0;
4110 parser
->implicit_template_scope
= 0;
4112 /* Allow constrained-type-specifiers. */
4113 parser
->prevent_constrained_type_specifiers
= 0;
4115 /* We haven't yet seen an 'extern "C"'. */
4116 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4121 /* Create a cp_lexer structure which will emit the tokens in CACHE
4122 and push it onto the parser's lexer stack. This is used for delayed
4123 parsing of in-class method bodies and default arguments, and should
4124 not be confused with tentative parsing. */
4126 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4128 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4129 lexer
->next
= parser
->lexer
;
4130 parser
->lexer
= lexer
;
4132 /* Move the current source position to that of the first token in the
4134 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4137 /* Pop the top lexer off the parser stack. This is never used for the
4138 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4140 cp_parser_pop_lexer (cp_parser
*parser
)
4142 cp_lexer
*lexer
= parser
->lexer
;
4143 parser
->lexer
= lexer
->next
;
4144 cp_lexer_destroy (lexer
);
4146 /* Put the current source position back where it was before this
4147 lexer was pushed. */
4148 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4151 /* Lexical conventions [gram.lex] */
4153 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4157 cp_parser_identifier (cp_parser
* parser
)
4161 /* Look for the identifier. */
4162 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4163 /* Return the value. */
4165 return cp_expr (token
->u
.value
, token
->location
);
4167 return error_mark_node
;
4170 /* Parse a sequence of adjacent string constants. Returns a
4171 TREE_STRING representing the combined, nul-terminated string
4172 constant. If TRANSLATE is true, translate the string to the
4173 execution character set. If WIDE_OK is true, a wide string is
4176 C++98 [lex.string] says that if a narrow string literal token is
4177 adjacent to a wide string literal token, the behavior is undefined.
4178 However, C99 6.4.5p4 says that this results in a wide string literal.
4179 We follow C99 here, for consistency with the C front end.
4181 This code is largely lifted from lex_string() in c-lex.c.
4183 FUTURE: ObjC++ will need to handle @-strings here. */
4185 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4186 bool lookup_udlit
= true)
4190 struct obstack str_ob
;
4191 struct obstack loc_ob
;
4192 cpp_string str
, istr
, *strs
;
4194 enum cpp_ttype type
, curr_type
;
4195 int have_suffix_p
= 0;
4197 tree suffix_id
= NULL_TREE
;
4198 bool curr_tok_is_userdef_p
= false;
4200 tok
= cp_lexer_peek_token (parser
->lexer
);
4201 if (!cp_parser_is_string_literal (tok
))
4203 cp_parser_error (parser
, "expected string-literal");
4204 return error_mark_node
;
4207 location_t loc
= tok
->location
;
4209 if (cpp_userdef_string_p (tok
->type
))
4211 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4212 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4213 curr_tok_is_userdef_p
= true;
4217 string_tree
= tok
->u
.value
;
4218 curr_type
= tok
->type
;
4222 /* Try to avoid the overhead of creating and destroying an obstack
4223 for the common case of just one string. */
4224 if (!cp_parser_is_string_literal
4225 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4227 cp_lexer_consume_token (parser
->lexer
);
4229 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4230 str
.len
= TREE_STRING_LENGTH (string_tree
);
4233 if (curr_tok_is_userdef_p
)
4235 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4237 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4240 curr_type
= tok
->type
;
4246 location_t last_tok_loc
= tok
->location
;
4247 gcc_obstack_init (&str_ob
);
4248 gcc_obstack_init (&loc_ob
);
4253 cp_lexer_consume_token (parser
->lexer
);
4255 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4256 str
.len
= TREE_STRING_LENGTH (string_tree
);
4258 if (curr_tok_is_userdef_p
)
4260 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4261 if (have_suffix_p
== 0)
4263 suffix_id
= curr_suffix_id
;
4266 else if (have_suffix_p
== 1
4267 && curr_suffix_id
!= suffix_id
)
4269 error ("inconsistent user-defined literal suffixes"
4270 " %qD and %qD in string literal",
4271 suffix_id
, curr_suffix_id
);
4274 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4277 curr_type
= tok
->type
;
4279 if (type
!= curr_type
)
4281 if (type
== CPP_STRING
)
4283 else if (curr_type
!= CPP_STRING
)
4285 rich_location
rich_loc (line_table
, tok
->location
);
4286 rich_loc
.add_range (last_tok_loc
);
4287 error_at (&rich_loc
,
4288 "unsupported non-standard concatenation "
4289 "of string literals");
4293 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4294 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4296 last_tok_loc
= tok
->location
;
4298 tok
= cp_lexer_peek_token (parser
->lexer
);
4299 if (cpp_userdef_string_p (tok
->type
))
4301 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4302 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4303 curr_tok_is_userdef_p
= true;
4307 string_tree
= tok
->u
.value
;
4308 curr_type
= tok
->type
;
4309 curr_tok_is_userdef_p
= false;
4312 while (cp_parser_is_string_literal (tok
));
4314 /* A string literal built by concatenation has its caret=start at
4315 the start of the initial string, and its finish at the finish of
4316 the final string literal. */
4317 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4319 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4322 if (type
!= CPP_STRING
&& !wide_ok
)
4324 cp_parser_error (parser
, "a wide string is invalid in this context");
4328 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4329 (parse_in
, strs
, count
, &istr
, type
))
4331 value
= build_string (istr
.len
, (const char *)istr
.text
);
4332 free (CONST_CAST (unsigned char *, istr
.text
));
4335 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4336 gcc_assert (g_string_concat_db
);
4337 g_string_concat_db
->record_string_concatenation (count
, locs
);
4344 TREE_TYPE (value
) = char_array_type_node
;
4346 case CPP_UTF8STRING
:
4348 TREE_TYPE (value
) = char8_array_type_node
;
4350 TREE_TYPE (value
) = char_array_type_node
;
4353 TREE_TYPE (value
) = char16_array_type_node
;
4356 TREE_TYPE (value
) = char32_array_type_node
;
4359 TREE_TYPE (value
) = wchar_array_type_node
;
4363 value
= fix_string_type (value
);
4367 tree literal
= build_userdef_literal (suffix_id
, value
,
4368 OT_NONE
, NULL_TREE
);
4370 value
= cp_parser_userdef_string_literal (literal
);
4376 /* cpp_interpret_string has issued an error. */
4377 value
= error_mark_node
;
4381 obstack_free (&str_ob
, 0);
4382 obstack_free (&loc_ob
, 0);
4385 return cp_expr (value
, loc
);
4388 /* Look up a literal operator with the name and the exact arguments. */
4391 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4393 tree decl
= lookup_name (name
);
4394 if (!decl
|| !is_overloaded_fn (decl
))
4395 return error_mark_node
;
4397 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4401 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4407 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4408 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4410 tree tparm
= TREE_VALUE (parmtypes
);
4411 tree targ
= TREE_TYPE ((*args
)[ix
]);
4412 bool ptr
= TYPE_PTR_P (tparm
);
4413 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4414 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4416 || !same_type_p (TREE_TYPE (tparm
),
4422 && ix
== vec_safe_length (args
)
4423 /* May be this should be sufficient_parms_p instead,
4424 depending on how exactly should user-defined literals
4425 work in presence of default arguments on the literal
4426 operator parameters. */
4427 && parmtypes
== void_list_node
)
4432 return error_mark_node
;
4435 /* Parse a user-defined char constant. Returns a call to a user-defined
4436 literal operator taking the character as an argument. */
4439 cp_parser_userdef_char_literal (cp_parser
*parser
)
4441 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4442 tree literal
= token
->u
.value
;
4443 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4444 tree value
= USERDEF_LITERAL_VALUE (literal
);
4445 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4448 /* Build up a call to the user-defined operator */
4449 /* Lookup the name we got back from the id-expression. */
4451 vec_safe_push (args
, value
);
4452 decl
= lookup_literal_operator (name
, args
);
4453 if (!decl
|| decl
== error_mark_node
)
4455 error ("unable to find character literal operator %qD with %qT argument",
4456 name
, TREE_TYPE (value
));
4457 return error_mark_node
;
4459 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4463 /* A subroutine of cp_parser_userdef_numeric_literal to
4464 create a char... template parameter pack from a string node. */
4467 make_char_string_pack (tree value
)
4470 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4471 const char *str
= TREE_STRING_POINTER (value
);
4472 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4473 tree argvec
= make_tree_vec (1);
4475 /* Fill in CHARVEC with all of the parameters. */
4476 charvec
= make_tree_vec (len
);
4477 for (i
= 0; i
< len
; ++i
)
4479 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4480 cpp_string in
= { 3, s
};
4481 cpp_string out
= { 0, 0 };
4482 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4484 gcc_assert (out
.len
== 2);
4485 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4489 /* Build the argument packs. */
4490 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4492 TREE_VEC_ELT (argvec
, 0) = argpack
;
4497 /* A subroutine of cp_parser_userdef_numeric_literal to
4498 create a char... template parameter pack from a string node. */
4501 make_string_pack (tree value
)
4504 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4505 const unsigned char *str
4506 = (const unsigned char *) TREE_STRING_POINTER (value
);
4507 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4508 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4509 tree argvec
= make_tree_vec (2);
4511 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4512 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4514 /* First template parm is character type. */
4515 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4517 /* Fill in CHARVEC with all of the parameters. */
4518 charvec
= make_tree_vec (len
);
4519 for (int i
= 0; i
< len
; ++i
)
4520 TREE_VEC_ELT (charvec
, i
)
4521 = double_int_to_tree (str_char_type_node
,
4522 double_int::from_buffer (str
+ i
* sz
, sz
));
4524 /* Build the argument packs. */
4525 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4527 TREE_VEC_ELT (argvec
, 1) = argpack
;
4532 /* Parse a user-defined numeric constant. returns a call to a user-defined
4533 literal operator. */
4536 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4538 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4539 tree literal
= token
->u
.value
;
4540 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4541 tree value
= USERDEF_LITERAL_VALUE (literal
);
4542 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4543 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4544 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4547 /* Look for a literal operator taking the exact type of numeric argument
4548 as the literal value. */
4550 vec_safe_push (args
, value
);
4551 decl
= lookup_literal_operator (name
, args
);
4552 if (decl
&& decl
!= error_mark_node
)
4554 result
= finish_call_expr (decl
, &args
, false, true,
4555 tf_warning_or_error
);
4557 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4559 warning_at (token
->location
, OPT_Woverflow
,
4560 "integer literal exceeds range of %qT type",
4561 long_long_unsigned_type_node
);
4566 warning_at (token
->location
, OPT_Woverflow
,
4567 "floating literal exceeds range of %qT type",
4568 long_double_type_node
);
4569 else if (overflow
< 0)
4570 warning_at (token
->location
, OPT_Woverflow
,
4571 "floating literal truncated to zero");
4577 /* If the numeric argument didn't work, look for a raw literal
4578 operator taking a const char* argument consisting of the number
4579 in string format. */
4581 vec_safe_push (args
, num_string
);
4582 decl
= lookup_literal_operator (name
, args
);
4583 if (decl
&& decl
!= error_mark_node
)
4585 result
= finish_call_expr (decl
, &args
, false, true,
4586 tf_warning_or_error
);
4590 /* If the raw literal didn't work, look for a non-type template
4591 function with parameter pack char.... Call the function with
4592 template parameter characters representing the number. */
4594 decl
= lookup_literal_operator (name
, args
);
4595 if (decl
&& decl
!= error_mark_node
)
4597 tree tmpl_args
= make_char_string_pack (num_string
);
4598 if (tmpl_args
== NULL_TREE
)
4600 error ("failed to translate literal to execution character set %qT",
4602 return error_mark_node
;
4604 decl
= lookup_template_function (decl
, tmpl_args
);
4605 result
= finish_call_expr (decl
, &args
, false, true,
4606 tf_warning_or_error
);
4610 /* In C++14 the standard library defines complex number suffixes that
4611 conflict with GNU extensions. Prefer them if <complex> is #included. */
4612 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4613 bool i14
= (cxx_dialect
> cxx11
4614 && (id_equal (suffix_id
, "i")
4615 || id_equal (suffix_id
, "if")
4616 || id_equal (suffix_id
, "il")));
4617 diagnostic_t kind
= DK_ERROR
;
4622 tree cxlit
= lookup_qualified_name (std_node
, "complex_literals",
4623 LOOK_want::NORMAL
, false);
4624 if (cxlit
== error_mark_node
)
4626 /* No <complex>, so pedwarn and use GNU semantics. */
4628 opt
= OPT_Wpedantic
;
4633 = emit_diagnostic (kind
, input_location
, opt
,
4634 "unable to find numeric literal operator %qD", name
);
4637 /* Don't inform either. */;
4640 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4641 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4644 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4645 "GNU built-in suffix");
4648 inform (token
->location
, "use %<-fext-numeric-literals%> "
4649 "to enable more built-in suffixes");
4651 if (kind
== DK_ERROR
)
4652 value
= error_mark_node
;
4655 /* Use the built-in semantics. */
4657 if (id_equal (suffix_id
, "i"))
4659 if (TREE_CODE (value
) == INTEGER_CST
)
4660 type
= integer_type_node
;
4662 type
= double_type_node
;
4664 else if (id_equal (suffix_id
, "if"))
4665 type
= float_type_node
;
4666 else /* if (id_equal (suffix_id, "il")) */
4667 type
= long_double_type_node
;
4669 value
= build_complex (build_complex_type (type
),
4670 fold_convert (type
, integer_zero_node
),
4671 fold_convert (type
, value
));
4674 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4675 /* Avoid repeated diagnostics. */
4676 token
->u
.value
= value
;
4680 /* Parse a user-defined string constant. Returns a call to a user-defined
4681 literal operator taking a character pointer and the length of the string
4685 cp_parser_userdef_string_literal (tree literal
)
4687 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4688 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4689 tree value
= USERDEF_LITERAL_VALUE (literal
);
4690 int len
= TREE_STRING_LENGTH (value
)
4691 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4694 /* Build up a call to the user-defined operator. */
4695 /* Lookup the name we got back from the id-expression. */
4697 vec_safe_push (args
, value
);
4698 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4699 decl
= lookup_literal_operator (name
, args
);
4701 if (decl
&& decl
!= error_mark_node
)
4702 return finish_call_expr (decl
, &args
, false, true,
4703 tf_warning_or_error
);
4705 /* Look for a suitable template function, either (C++20) with a single
4706 parameter of class type, or (N3599) with typename parameter CharT and
4707 parameter pack CharT... */
4709 decl
= lookup_literal_operator (name
, args
);
4710 if (decl
&& decl
!= error_mark_node
)
4712 /* Use resolve_nondeduced_context to try to choose one form of template
4714 tree tmpl_args
= make_tree_vec (1);
4715 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4716 decl
= lookup_template_function (decl
, tmpl_args
);
4717 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4722 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4723 res
= resolve_nondeduced_context (decl
, tf_none
);
4727 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4728 TREE_OPERAND (decl
, 1) = tmpl_args
;
4729 return finish_call_expr (decl
, &args
, false, true,
4730 tf_warning_or_error
);
4733 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4734 name
, TREE_TYPE (value
), size_type_node
);
4735 return error_mark_node
;
4739 /* Basic concepts [gram.basic] */
4741 /* Parse a translation-unit.
4744 declaration-seq [opt] */
4747 cp_parser_translation_unit (cp_parser
* parser
)
4749 gcc_checking_assert (!cp_error_declarator
);
4751 /* Create the declarator obstack. */
4752 gcc_obstack_init (&declarator_obstack
);
4753 /* Create the error declarator. */
4754 cp_error_declarator
= make_declarator (cdk_error
);
4755 /* Create the empty parameter list. */
4756 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4758 /* Remember where the base of the declarator obstack lies. */
4759 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4761 push_deferring_access_checks (flag_access_control
4762 ? dk_no_deferred
: dk_no_check
);
4764 bool implicit_extern_c
= false;
4766 /* Parse until EOF. */
4769 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4771 /* If we're entering or exiting a region that's implicitly
4772 extern "C", modify the lang context appropriately. This is
4773 so horrible. Please die. */
4774 if (implicit_extern_c
4775 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4777 implicit_extern_c
= !implicit_extern_c
;
4778 if (implicit_extern_c
)
4779 push_lang_context (lang_name_c
);
4781 pop_lang_context ();
4784 if (token
->type
== CPP_EOF
)
4787 if (token
->type
== CPP_CLOSE_BRACE
)
4789 cp_parser_error (parser
, "expected declaration");
4790 cp_lexer_consume_token (parser
->lexer
);
4791 /* If the next token is now a `;', consume it. */
4792 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4793 cp_lexer_consume_token (parser
->lexer
);
4796 cp_parser_toplevel_declaration (parser
);
4799 /* Get rid of the token array; we don't need it any more. */
4800 cp_lexer_destroy (parser
->lexer
);
4801 parser
->lexer
= NULL
;
4803 /* The EOF should have reset this. */
4804 gcc_checking_assert (!implicit_extern_c
);
4806 /* Make sure the declarator obstack was fully cleaned up. */
4807 gcc_assert (obstack_next_free (&declarator_obstack
)
4808 == declarator_obstack_base
);
4811 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4812 decltype context. */
4814 static inline tsubst_flags_t
4815 complain_flags (bool decltype_p
)
4817 tsubst_flags_t complain
= tf_warning_or_error
;
4819 complain
|= tf_decltype
;
4823 /* We're about to parse a collection of statements. If we're currently
4824 parsing tentatively, set up a firewall so that any nested
4825 cp_parser_commit_to_tentative_parse won't affect the current context. */
4827 static cp_token_position
4828 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4830 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4833 cp_parser_parse_tentatively (parser
);
4834 cp_parser_commit_to_topmost_tentative_parse (parser
);
4835 return cp_lexer_token_position (parser
->lexer
, false);
4838 /* We've finished parsing the collection of statements. Wrap up the
4839 firewall and replace the relevant tokens with the parsed form. */
4842 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4848 /* Finish the firewall level. */
4849 cp_parser_parse_definitely (parser
);
4850 /* And remember the result of the parse for when we try again. */
4851 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4852 token
->type
= CPP_PREPARSED_EXPR
;
4853 token
->u
.value
= expr
;
4854 token
->keyword
= RID_MAX
;
4855 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4858 /* Like the above functions, but let the user modify the tokens. Used by
4859 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4860 later parses, so it makes sense to localize the effects of
4861 cp_parser_commit_to_tentative_parse. */
4863 struct tentative_firewall
4868 tentative_firewall (cp_parser
*p
): parser(p
)
4870 /* If we're currently parsing tentatively, start a committed level as a
4871 firewall and then an inner tentative parse. */
4872 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4874 cp_parser_parse_tentatively (parser
);
4875 cp_parser_commit_to_topmost_tentative_parse (parser
);
4876 cp_parser_parse_tentatively (parser
);
4880 ~tentative_firewall()
4884 /* Finish the inner tentative parse and the firewall, propagating any
4885 uncommitted error state to the outer tentative parse. */
4886 bool err
= cp_parser_error_occurred (parser
);
4887 cp_parser_parse_definitely (parser
);
4888 cp_parser_parse_definitely (parser
);
4890 cp_parser_simulate_error (parser
);
4895 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4896 This class is for tracking such a matching pair of symbols.
4897 In particular, it tracks the location of the first token,
4898 so that if the second token is missing, we can highlight the
4899 location of the first token when notifying the user about the
4902 template <typename traits_t
>
4906 /* token_pair's ctor. */
4907 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4909 /* If the next token is the opening symbol for this pair, consume it and
4911 Otherwise, issue an error and return false.
4912 In either case, record the location of the opening token. */
4914 bool require_open (cp_parser
*parser
)
4916 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4917 return cp_parser_require (parser
, traits_t::open_token_type
,
4918 traits_t::required_token_open
);
4921 /* Consume the next token from PARSER, recording its location as
4922 that of the opening token within the pair. */
4924 cp_token
* consume_open (cp_parser
*parser
)
4926 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4927 gcc_assert (tok
->type
== traits_t::open_token_type
);
4928 m_open_loc
= tok
->location
;
4932 /* If the next token is the closing symbol for this pair, consume it
4934 Otherwise, issue an error, highlighting the location of the
4935 corresponding opening token, and return NULL. */
4937 cp_token
*require_close (cp_parser
*parser
) const
4939 return cp_parser_require (parser
, traits_t::close_token_type
,
4940 traits_t::required_token_close
,
4944 location_t
open_location () const { return m_open_loc
; }
4947 location_t m_open_loc
;
4950 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4952 struct matching_paren_traits
4954 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4955 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4956 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4957 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4960 /* "matching_parens" is a token_pair<T> class for tracking matching
4961 pairs of parentheses. */
4963 typedef token_pair
<matching_paren_traits
> matching_parens
;
4965 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4967 struct matching_brace_traits
4969 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4970 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4971 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4972 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4975 /* "matching_braces" is a token_pair<T> class for tracking matching
4978 typedef token_pair
<matching_brace_traits
> matching_braces
;
4981 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4982 enclosing parentheses. */
4985 cp_parser_statement_expr (cp_parser
*parser
)
4987 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4989 /* Consume the '('. */
4990 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4991 matching_parens parens
;
4992 parens
.consume_open (parser
);
4993 /* Start the statement-expression. */
4994 tree expr
= begin_stmt_expr ();
4995 /* Parse the compound-statement. */
4996 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4998 expr
= finish_stmt_expr (expr
, false);
4999 /* Consume the ')'. */
5000 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5001 if (!parens
.require_close (parser
))
5002 cp_parser_skip_to_end_of_statement (parser
);
5004 cp_parser_end_tentative_firewall (parser
, start
, expr
);
5005 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
5006 return cp_expr (expr
, combined_loc
);
5009 /* Expressions [gram.expr] */
5011 /* Parse a fold-operator.
5014 - * / % ^ & | = < > << >>
5015 = -= *= /= %= ^= &= |= <<= >>=
5016 == != <= >= && || , .* ->*
5018 This returns the tree code corresponding to the matched operator
5019 as an int. When the current token matches a compound assignment
5020 operator, the resulting tree code is the negative value of the
5021 non-assignment operator. */
5024 cp_parser_fold_operator (cp_token
*token
)
5026 switch (token
->type
)
5028 case CPP_PLUS
: return PLUS_EXPR
;
5029 case CPP_MINUS
: return MINUS_EXPR
;
5030 case CPP_MULT
: return MULT_EXPR
;
5031 case CPP_DIV
: return TRUNC_DIV_EXPR
;
5032 case CPP_MOD
: return TRUNC_MOD_EXPR
;
5033 case CPP_XOR
: return BIT_XOR_EXPR
;
5034 case CPP_AND
: return BIT_AND_EXPR
;
5035 case CPP_OR
: return BIT_IOR_EXPR
;
5036 case CPP_LSHIFT
: return LSHIFT_EXPR
;
5037 case CPP_RSHIFT
: return RSHIFT_EXPR
;
5039 case CPP_EQ
: return -NOP_EXPR
;
5040 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
5041 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
5042 case CPP_MULT_EQ
: return -MULT_EXPR
;
5043 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
5044 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
5045 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
5046 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
5047 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
5048 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
5049 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
5051 case CPP_EQ_EQ
: return EQ_EXPR
;
5052 case CPP_NOT_EQ
: return NE_EXPR
;
5053 case CPP_LESS
: return LT_EXPR
;
5054 case CPP_GREATER
: return GT_EXPR
;
5055 case CPP_LESS_EQ
: return LE_EXPR
;
5056 case CPP_GREATER_EQ
: return GE_EXPR
;
5058 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
5059 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
5061 case CPP_COMMA
: return COMPOUND_EXPR
;
5063 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
5064 case CPP_DEREF_STAR
: return MEMBER_REF
;
5066 default: return ERROR_MARK
;
5070 /* Returns true if CODE indicates a binary expression, which is not allowed in
5071 the LHS of a fold-expression. More codes will need to be added to use this
5072 function in other contexts. */
5075 is_binary_op (tree_code code
)
5080 case POINTER_PLUS_EXPR
:
5083 case TRUNC_DIV_EXPR
:
5084 case TRUNC_MOD_EXPR
:
5100 case TRUTH_ANDIF_EXPR
:
5101 case TRUTH_ORIF_EXPR
:
5114 /* If the next token is a suitable fold operator, consume it and return as
5115 the function above. */
5118 cp_parser_fold_operator (cp_parser
*parser
)
5120 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5121 int code
= cp_parser_fold_operator (token
);
5122 if (code
!= ERROR_MARK
)
5123 cp_lexer_consume_token (parser
->lexer
);
5127 /* Parse a fold-expression.
5130 ( ... folding-operator cast-expression)
5131 ( cast-expression folding-operator ... )
5132 ( cast-expression folding operator ... folding-operator cast-expression)
5134 Note that the '(' and ')' are matched in primary expression. */
5137 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5142 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5145 return error_mark_node
;
5146 cp_lexer_consume_token (parser
->lexer
);
5147 int op
= cp_parser_fold_operator (parser
);
5148 if (op
== ERROR_MARK
)
5150 cp_parser_error (parser
, "expected binary operator");
5151 return error_mark_node
;
5154 tree expr
= cp_parser_cast_expression (parser
, false, false,
5156 if (expr
== error_mark_node
)
5157 return error_mark_node
;
5158 return finish_left_unary_fold_expr (expr
, op
);
5161 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5162 int op
= cp_parser_fold_operator (parser
);
5163 if (op
== ERROR_MARK
)
5165 cp_parser_error (parser
, "expected binary operator");
5166 return error_mark_node
;
5169 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5171 cp_parser_error (parser
, "expected ...");
5172 return error_mark_node
;
5174 cp_lexer_consume_token (parser
->lexer
);
5176 /* The operands of a fold-expression are cast-expressions, so binary or
5177 conditional expressions are not allowed. We check this here to avoid
5178 tentative parsing. */
5179 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5180 /* OK, the expression was parenthesized. */;
5181 else if (is_binary_op (TREE_CODE (expr1
)))
5182 error_at (location_of (expr1
),
5183 "binary expression in operand of fold-expression");
5184 else if (TREE_CODE (expr1
) == COND_EXPR
5185 || (REFERENCE_REF_P (expr1
)
5186 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5187 error_at (location_of (expr1
),
5188 "conditional expression in operand of fold-expression");
5191 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5192 return finish_right_unary_fold_expr (expr1
, op
);
5194 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5196 cp_parser_error (parser
, "mismatched operator in fold-expression");
5197 return error_mark_node
;
5199 cp_lexer_consume_token (parser
->lexer
);
5201 // Binary left or right fold.
5202 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5203 if (expr2
== error_mark_node
)
5204 return error_mark_node
;
5205 return finish_binary_fold_expr (expr1
, expr2
, op
);
5208 /* Parse a primary-expression.
5215 lambda-expression (C++11)
5220 ( compound-statement )
5221 __builtin_va_arg ( assignment-expression , type-id )
5222 __builtin_offsetof ( type-id , offsetof-expression )
5225 __has_nothrow_assign ( type-id )
5226 __has_nothrow_constructor ( type-id )
5227 __has_nothrow_copy ( type-id )
5228 __has_trivial_assign ( type-id )
5229 __has_trivial_constructor ( type-id )
5230 __has_trivial_copy ( type-id )
5231 __has_trivial_destructor ( type-id )
5232 __has_virtual_destructor ( type-id )
5233 __is_abstract ( type-id )
5234 __is_base_of ( type-id , type-id )
5235 __is_class ( type-id )
5236 __is_empty ( type-id )
5237 __is_enum ( type-id )
5238 __is_final ( type-id )
5239 __is_literal_type ( type-id )
5240 __is_pod ( type-id )
5241 __is_polymorphic ( type-id )
5242 __is_std_layout ( type-id )
5243 __is_trivial ( type-id )
5244 __is_union ( type-id )
5246 Objective-C++ Extension:
5254 ADDRESS_P is true iff this expression was immediately preceded by
5255 "&" and therefore might denote a pointer-to-member. CAST_P is true
5256 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5257 true iff this expression is a template argument.
5259 Returns a representation of the expression. Upon return, *IDK
5260 indicates what kind of id-expression (if any) was present. */
5263 cp_parser_primary_expression (cp_parser
*parser
,
5266 bool template_arg_p
,
5270 cp_token
*token
= NULL
;
5272 /* Assume the primary expression is not an id-expression. */
5273 *idk
= CP_ID_KIND_NONE
;
5275 /* Peek at the next token. */
5276 token
= cp_lexer_peek_token (parser
->lexer
);
5277 switch ((int) token
->type
)
5286 user-defined-literal */
5293 case CPP_PREPARSED_EXPR
:
5294 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5295 return cp_parser_userdef_numeric_literal (parser
);
5296 token
= cp_lexer_consume_token (parser
->lexer
);
5297 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5299 error_at (token
->location
,
5300 "fixed-point types not supported in C++");
5301 return error_mark_node
;
5303 /* Floating-point literals are only allowed in an integral
5304 constant expression if they are cast to an integral or
5305 enumeration type. */
5306 if (TREE_CODE (token
->u
.value
) == REAL_CST
5307 && parser
->integral_constant_expression_p
5310 /* CAST_P will be set even in invalid code like "int(2.7 +
5311 ...)". Therefore, we have to check that the next token
5312 is sure to end the cast. */
5315 cp_token
*next_token
;
5317 next_token
= cp_lexer_peek_token (parser
->lexer
);
5318 if (/* The comma at the end of an
5319 enumerator-definition. */
5320 next_token
->type
!= CPP_COMMA
5321 /* The curly brace at the end of an enum-specifier. */
5322 && next_token
->type
!= CPP_CLOSE_BRACE
5323 /* The end of a statement. */
5324 && next_token
->type
!= CPP_SEMICOLON
5325 /* The end of the cast-expression. */
5326 && next_token
->type
!= CPP_CLOSE_PAREN
5327 /* The end of an array bound. */
5328 && next_token
->type
!= CPP_CLOSE_SQUARE
5329 /* The closing ">" in a template-argument-list. */
5330 && (next_token
->type
!= CPP_GREATER
5331 || parser
->greater_than_is_operator_p
)
5332 /* C++0x only: A ">>" treated like two ">" tokens,
5333 in a template-argument-list. */
5334 && (next_token
->type
!= CPP_RSHIFT
5335 || (cxx_dialect
== cxx98
)
5336 || parser
->greater_than_is_operator_p
))
5340 /* If we are within a cast, then the constraint that the
5341 cast is to an integral or enumeration type will be
5342 checked at that point. If we are not within a cast, then
5343 this code is invalid. */
5345 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5347 return (cp_expr (token
->u
.value
, token
->location
)
5348 .maybe_add_location_wrapper ());
5350 case CPP_CHAR_USERDEF
:
5351 case CPP_CHAR16_USERDEF
:
5352 case CPP_CHAR32_USERDEF
:
5353 case CPP_WCHAR_USERDEF
:
5354 case CPP_UTF8CHAR_USERDEF
:
5355 return cp_parser_userdef_char_literal (parser
);
5361 case CPP_UTF8STRING
:
5362 case CPP_STRING_USERDEF
:
5363 case CPP_STRING16_USERDEF
:
5364 case CPP_STRING32_USERDEF
:
5365 case CPP_WSTRING_USERDEF
:
5366 case CPP_UTF8STRING_USERDEF
:
5367 /* ??? Should wide strings be allowed when parser->translate_strings_p
5368 is false (i.e. in attributes)? If not, we can kill the third
5369 argument to cp_parser_string_literal. */
5370 return (cp_parser_string_literal (parser
,
5371 parser
->translate_strings_p
,
5373 .maybe_add_location_wrapper ());
5375 case CPP_OPEN_PAREN
:
5376 /* If we see `( { ' then we are looking at the beginning of
5377 a GNU statement-expression. */
5378 if (cp_parser_allow_gnu_extensions_p (parser
)
5379 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5381 /* Statement-expressions are not allowed by the standard. */
5382 pedwarn (token
->location
, OPT_Wpedantic
,
5383 "ISO C++ forbids braced-groups within expressions");
5385 /* And they're not allowed outside of a function-body; you
5386 cannot, for example, write:
5388 int i = ({ int j = 3; j + 1; });
5390 at class or namespace scope. */
5391 if (!parser
->in_function_body
5392 || parser
->in_template_argument_list_p
)
5394 error_at (token
->location
,
5395 "statement-expressions are not allowed outside "
5396 "functions nor in template-argument lists");
5397 cp_parser_skip_to_end_of_block_or_statement (parser
);
5398 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5399 cp_lexer_consume_token (parser
->lexer
);
5400 return error_mark_node
;
5403 return cp_parser_statement_expr (parser
);
5405 /* Otherwise it's a normal parenthesized expression. */
5408 bool saved_greater_than_is_operator_p
;
5410 location_t open_paren_loc
= token
->location
;
5412 /* Consume the `('. */
5413 matching_parens parens
;
5414 parens
.consume_open (parser
);
5415 /* Within a parenthesized expression, a `>' token is always
5416 the greater-than operator. */
5417 saved_greater_than_is_operator_p
5418 = parser
->greater_than_is_operator_p
;
5419 parser
->greater_than_is_operator_p
= true;
5421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5422 /* Left fold expression. */
5425 /* Parse the parenthesized expression. */
5426 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5428 token
= cp_lexer_peek_token (parser
->lexer
);
5429 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5431 expr
= cp_parser_fold_expression (parser
, expr
);
5432 if (expr
!= error_mark_node
5433 && cxx_dialect
< cxx17
)
5434 pedwarn (input_location
, 0, "fold-expressions only available "
5435 "with %<-std=c++17%> or %<-std=gnu++17%>");
5438 /* Let the front end know that this expression was
5439 enclosed in parentheses. This matters in case, for
5440 example, the expression is of the form `A::B', since
5441 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5443 expr
= finish_parenthesized_expr (expr
);
5445 /* DR 705: Wrapping an unqualified name in parentheses
5446 suppresses arg-dependent lookup. We want to pass back
5447 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5448 (c++/37862), but none of the others. */
5449 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5450 *idk
= CP_ID_KIND_NONE
;
5452 /* The `>' token might be the end of a template-id or
5453 template-parameter-list now. */
5454 parser
->greater_than_is_operator_p
5455 = saved_greater_than_is_operator_p
;
5457 /* Consume the `)'. */
5458 token
= cp_lexer_peek_token (parser
->lexer
);
5459 location_t close_paren_loc
= token
->location
;
5460 expr
.set_range (open_paren_loc
, close_paren_loc
);
5461 if (!parens
.require_close (parser
)
5462 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5463 cp_parser_skip_to_end_of_statement (parser
);
5468 case CPP_OPEN_SQUARE
:
5470 if (c_dialect_objc ())
5472 /* We might have an Objective-C++ message. */
5473 cp_parser_parse_tentatively (parser
);
5474 tree msg
= cp_parser_objc_message_expression (parser
);
5475 /* If that works out, we're done ... */
5476 if (cp_parser_parse_definitely (parser
))
5478 /* ... else, fall though to see if it's a lambda. */
5480 cp_expr lam
= cp_parser_lambda_expression (parser
);
5481 /* Don't warn about a failed tentative parse. */
5482 if (cp_parser_error_occurred (parser
))
5483 return error_mark_node
;
5484 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5488 case CPP_OBJC_STRING
:
5489 if (c_dialect_objc ())
5490 /* We have an Objective-C++ string literal. */
5491 return cp_parser_objc_expression (parser
);
5492 cp_parser_error (parser
, "expected primary-expression");
5493 return error_mark_node
;
5496 switch (token
->keyword
)
5498 /* These two are the boolean literals. */
5500 cp_lexer_consume_token (parser
->lexer
);
5501 return cp_expr (boolean_true_node
, token
->location
);
5503 cp_lexer_consume_token (parser
->lexer
);
5504 return cp_expr (boolean_false_node
, token
->location
);
5506 /* The `__null' literal. */
5508 cp_lexer_consume_token (parser
->lexer
);
5509 return cp_expr (null_node
, token
->location
);
5511 /* The `nullptr' literal. */
5513 cp_lexer_consume_token (parser
->lexer
);
5514 return cp_expr (nullptr_node
, token
->location
);
5516 /* Recognize the `this' keyword. */
5518 cp_lexer_consume_token (parser
->lexer
);
5519 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5521 error_at (token
->location
,
5522 "%<this%> may not be used in this context");
5523 return error_mark_node
;
5525 /* Pointers cannot appear in constant-expressions. */
5526 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5527 return error_mark_node
;
5528 return cp_expr (finish_this_expr (), token
->location
);
5530 /* The `operator' keyword can be the beginning of an
5535 case RID_FUNCTION_NAME
:
5536 case RID_PRETTY_FUNCTION_NAME
:
5537 case RID_C99_FUNCTION_NAME
:
5539 non_integral_constant name
;
5541 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5542 __func__ are the names of variables -- but they are
5543 treated specially. Therefore, they are handled here,
5544 rather than relying on the generic id-expression logic
5545 below. Grammatically, these names are id-expressions.
5547 Consume the token. */
5548 token
= cp_lexer_consume_token (parser
->lexer
);
5550 switch (token
->keyword
)
5552 case RID_FUNCTION_NAME
:
5553 name
= NIC_FUNC_NAME
;
5555 case RID_PRETTY_FUNCTION_NAME
:
5556 name
= NIC_PRETTY_FUNC
;
5558 case RID_C99_FUNCTION_NAME
:
5559 name
= NIC_C99_FUNC
;
5565 if (cp_parser_non_integral_constant_expression (parser
, name
))
5566 return error_mark_node
;
5568 /* Look up the name. */
5569 return finish_fname (token
->u
.value
);
5576 location_t type_location
;
5577 location_t start_loc
5578 = cp_lexer_peek_token (parser
->lexer
)->location
;
5579 /* The `__builtin_va_arg' construct is used to handle
5580 `va_arg'. Consume the `__builtin_va_arg' token. */
5581 cp_lexer_consume_token (parser
->lexer
);
5582 /* Look for the opening `('. */
5583 matching_parens parens
;
5584 parens
.require_open (parser
);
5585 /* Now, parse the assignment-expression. */
5586 expression
= cp_parser_assignment_expression (parser
);
5587 /* Look for the `,'. */
5588 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5589 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5590 /* Parse the type-id. */
5592 type_id_in_expr_sentinel
s (parser
);
5593 type
= cp_parser_type_id (parser
);
5595 /* Look for the closing `)'. */
5596 location_t finish_loc
5597 = cp_lexer_peek_token (parser
->lexer
)->location
;
5598 parens
.require_close (parser
);
5599 /* Using `va_arg' in a constant-expression is not
5601 if (cp_parser_non_integral_constant_expression (parser
,
5603 return error_mark_node
;
5604 /* Construct a location of the form:
5605 __builtin_va_arg (v, int)
5606 ~~~~~~~~~~~~~~~~~~~~~^~~~
5607 with the caret at the type, ranging from the start of the
5608 "__builtin_va_arg" token to the close paren. */
5609 location_t combined_loc
5610 = make_location (type_location
, start_loc
, finish_loc
);
5611 return build_x_va_arg (combined_loc
, expression
, type
);
5615 return cp_parser_builtin_offsetof (parser
);
5617 case RID_HAS_NOTHROW_ASSIGN
:
5618 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5619 case RID_HAS_NOTHROW_COPY
:
5620 case RID_HAS_TRIVIAL_ASSIGN
:
5621 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5622 case RID_HAS_TRIVIAL_COPY
:
5623 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5624 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5625 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5626 case RID_IS_ABSTRACT
:
5627 case RID_IS_AGGREGATE
:
5628 case RID_IS_BASE_OF
:
5633 case RID_IS_LITERAL_TYPE
:
5635 case RID_IS_POLYMORPHIC
:
5636 case RID_IS_SAME_AS
:
5637 case RID_IS_STD_LAYOUT
:
5638 case RID_IS_TRIVIAL
:
5639 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5640 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5641 case RID_IS_TRIVIALLY_COPYABLE
:
5643 case RID_IS_ASSIGNABLE
:
5644 case RID_IS_CONSTRUCTIBLE
:
5645 case RID_IS_NOTHROW_ASSIGNABLE
:
5646 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
5647 return cp_parser_trait_expr (parser
, token
->keyword
);
5651 return cp_parser_requires_expression (parser
);
5653 /* Objective-C++ expressions. */
5655 case RID_AT_PROTOCOL
:
5656 case RID_AT_SELECTOR
:
5657 return cp_parser_objc_expression (parser
);
5660 if (parser
->in_function_body
5661 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5664 error_at (token
->location
,
5665 "a template declaration cannot appear at block scope");
5666 cp_parser_skip_to_end_of_block_or_statement (parser
);
5667 return error_mark_node
;
5671 cp_parser_error (parser
, "expected primary-expression");
5672 return error_mark_node
;
5675 /* An id-expression can start with either an identifier, a
5676 `::' as the beginning of a qualified-id, or the "operator"
5680 case CPP_TEMPLATE_ID
:
5681 case CPP_NESTED_NAME_SPECIFIER
:
5684 cp_expr id_expression
;
5686 const char *error_msg
;
5689 cp_token
*id_expr_token
;
5691 /* Parse the id-expression. */
5693 = cp_parser_id_expression (parser
,
5694 /*template_keyword_p=*/false,
5695 /*check_dependency_p=*/true,
5697 /*declarator_p=*/false,
5698 /*optional_p=*/false);
5699 if (id_expression
== error_mark_node
)
5700 return error_mark_node
;
5701 id_expr_token
= token
;
5702 token
= cp_lexer_peek_token (parser
->lexer
);
5703 done
= (token
->type
!= CPP_OPEN_SQUARE
5704 && token
->type
!= CPP_OPEN_PAREN
5705 && token
->type
!= CPP_DOT
5706 && token
->type
!= CPP_DEREF
5707 && token
->type
!= CPP_PLUS_PLUS
5708 && token
->type
!= CPP_MINUS_MINUS
);
5709 /* If we have a template-id, then no further lookup is
5710 required. If the template-id was for a template-class, we
5711 will sometimes have a TYPE_DECL at this point. */
5712 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5713 || TREE_CODE (id_expression
) == TYPE_DECL
)
5714 decl
= id_expression
;
5715 /* Look up the name. */
5718 tree ambiguous_decls
;
5720 /* If we already know that this lookup is ambiguous, then
5721 we've already issued an error message; there's no reason
5723 if (id_expr_token
->type
== CPP_NAME
5724 && id_expr_token
->error_reported
)
5726 cp_parser_simulate_error (parser
);
5727 return error_mark_node
;
5730 decl
= cp_parser_lookup_name (parser
, id_expression
,
5733 /*is_namespace=*/false,
5734 /*check_dependency=*/true,
5736 id_expression
.get_location ());
5737 /* If the lookup was ambiguous, an error will already have
5739 if (ambiguous_decls
)
5740 return error_mark_node
;
5742 /* In Objective-C++, we may have an Objective-C 2.0
5743 dot-syntax for classes here. */
5744 if (c_dialect_objc ()
5745 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5746 && TREE_CODE (decl
) == TYPE_DECL
5747 && objc_is_class_name (decl
))
5750 cp_lexer_consume_token (parser
->lexer
);
5751 component
= cp_parser_identifier (parser
);
5752 if (component
== error_mark_node
)
5753 return error_mark_node
;
5755 tree result
= objc_build_class_component_ref (id_expression
,
5757 /* Build a location of the form:
5760 with caret at the start of the component name (at
5761 input_location), ranging from the start of the id_expression
5762 to the end of the component name. */
5763 location_t combined_loc
5764 = make_location (input_location
, id_expression
.get_start (),
5765 get_finish (input_location
));
5766 protected_set_expr_location (result
, combined_loc
);
5770 /* In Objective-C++, an instance variable (ivar) may be preferred
5771 to whatever cp_parser_lookup_name() found.
5772 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5773 rest of c-family, we have to do a little extra work to preserve
5774 any location information in cp_expr "decl". Given that
5775 objc_lookup_ivar is implemented in "c-family" and "objc", we
5776 have a trip through the pure "tree" type, rather than cp_expr.
5777 Naively copying it back to "decl" would implicitly give the
5778 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5779 store an EXPR_LOCATION. Hence we only update "decl" (and
5780 hence its location_t) if we get back a different tree node. */
5781 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5783 if (decl_tree
!= decl
.get_value ())
5784 decl
= cp_expr (decl_tree
);
5786 /* If name lookup gives us a SCOPE_REF, then the
5787 qualifying scope was dependent. */
5788 if (TREE_CODE (decl
) == SCOPE_REF
)
5790 /* At this point, we do not know if DECL is a valid
5791 integral constant expression. We assume that it is
5792 in fact such an expression, so that code like:
5794 template <int N> struct A {
5798 is accepted. At template-instantiation time, we
5799 will check that B<N>::i is actually a constant. */
5802 /* Check to see if DECL is a local variable in a context
5803 where that is forbidden. */
5804 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5805 && local_variable_p (decl
))
5807 error_at (id_expression
.get_location (),
5808 "local variable %qD may not appear in this context",
5810 return error_mark_node
;
5814 decl
= (finish_id_expression
5815 (id_expression
, decl
, parser
->scope
,
5817 parser
->integral_constant_expression_p
,
5818 parser
->allow_non_integral_constant_expression_p
,
5819 &parser
->non_integral_constant_expression_p
,
5820 template_p
, done
, address_p
,
5823 id_expression
.get_location ()));
5825 cp_parser_error (parser
, error_msg
);
5826 /* Build a location for an id-expression of the form:
5832 i.e. from the start of the first token to the end of the final
5833 token, with the caret at the start of the unqualified-id. */
5834 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5835 location_t start_loc
= get_start (id_expr_token
->location
);
5836 location_t finish_loc
= get_finish (id_expression
.get_location ());
5837 location_t combined_loc
5838 = make_location (caret_loc
, start_loc
, finish_loc
);
5840 decl
.set_location (combined_loc
);
5844 /* Anything else is an error. */
5846 cp_parser_error (parser
, "expected primary-expression");
5847 return error_mark_node
;
5851 static inline cp_expr
5852 cp_parser_primary_expression (cp_parser
*parser
,
5855 bool template_arg_p
,
5858 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5859 /*decltype*/false, idk
);
5862 /* Parse an id-expression.
5869 :: [opt] nested-name-specifier template [opt] unqualified-id
5871 :: operator-function-id
5874 Return a representation of the unqualified portion of the
5875 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5876 a `::' or nested-name-specifier.
5878 Often, if the id-expression was a qualified-id, the caller will
5879 want to make a SCOPE_REF to represent the qualified-id. This
5880 function does not do this in order to avoid wastefully creating
5881 SCOPE_REFs when they are not required.
5883 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5886 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5887 uninstantiated templates.
5889 If *TEMPLATE_P is non-NULL, it is set to true iff the
5890 `template' keyword is used to explicitly indicate that the entity
5891 named is a template.
5893 If DECLARATOR_P is true, the id-expression is appearing as part of
5894 a declarator, rather than as part of an expression. */
5897 cp_parser_id_expression (cp_parser
*parser
,
5898 bool template_keyword_p
,
5899 bool check_dependency_p
,
5904 bool global_scope_p
;
5905 bool nested_name_specifier_p
;
5907 /* Assume the `template' keyword was not used. */
5909 *template_p
= template_keyword_p
;
5911 /* Look for the optional `::' operator. */
5913 = (!template_keyword_p
5914 && (cp_parser_global_scope_opt (parser
,
5915 /*current_scope_valid_p=*/false)
5918 /* Look for the optional nested-name-specifier. */
5919 nested_name_specifier_p
5920 = (cp_parser_nested_name_specifier_opt (parser
,
5921 /*typename_keyword_p=*/false,
5928 /* If there is a nested-name-specifier, then we are looking at
5929 the first qualified-id production. */
5930 if (nested_name_specifier_p
)
5933 tree saved_object_scope
;
5934 tree saved_qualifying_scope
;
5935 cp_expr unqualified_id
;
5938 /* See if the next token is the `template' keyword. */
5940 template_p
= &is_template
;
5941 *template_p
= cp_parser_optional_template_keyword (parser
);
5942 /* Name lookup we do during the processing of the
5943 unqualified-id might obliterate SCOPE. */
5944 saved_scope
= parser
->scope
;
5945 saved_object_scope
= parser
->object_scope
;
5946 saved_qualifying_scope
= parser
->qualifying_scope
;
5947 /* Process the final unqualified-id. */
5948 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5951 /*optional_p=*/false);
5952 /* Restore the SAVED_SCOPE for our caller. */
5953 parser
->scope
= saved_scope
;
5954 parser
->object_scope
= saved_object_scope
;
5955 parser
->qualifying_scope
= saved_qualifying_scope
;
5957 return unqualified_id
;
5959 /* Otherwise, if we are in global scope, then we are looking at one
5960 of the other qualified-id productions. */
5961 else if (global_scope_p
)
5966 /* Peek at the next token. */
5967 token
= cp_lexer_peek_token (parser
->lexer
);
5969 /* If it's an identifier, and the next token is not a "<", then
5970 we can avoid the template-id case. This is an optimization
5971 for this common case. */
5972 if (token
->type
== CPP_NAME
5973 && !cp_parser_nth_token_starts_template_argument_list_p
5975 return cp_parser_identifier (parser
);
5977 cp_parser_parse_tentatively (parser
);
5978 /* Try a template-id. */
5979 id
= cp_parser_template_id_expr (parser
,
5980 /*template_keyword_p=*/false,
5981 /*check_dependency_p=*/true,
5983 /* If that worked, we're done. */
5984 if (cp_parser_parse_definitely (parser
))
5987 /* Peek at the next token. (Changes in the token buffer may
5988 have invalidated the pointer obtained above.) */
5989 token
= cp_lexer_peek_token (parser
->lexer
);
5991 switch (token
->type
)
5994 return cp_parser_identifier (parser
);
5997 if (token
->keyword
== RID_OPERATOR
)
5998 return cp_parser_operator_function_id (parser
);
6002 cp_parser_error (parser
, "expected id-expression");
6003 return error_mark_node
;
6007 return cp_parser_unqualified_id (parser
, template_keyword_p
,
6008 /*check_dependency_p=*/true,
6013 /* Parse an unqualified-id.
6017 operator-function-id
6018 conversion-function-id
6022 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6023 keyword, in a construct like `A::template ...'.
6025 Returns a representation of unqualified-id. For the `identifier'
6026 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6027 production a BIT_NOT_EXPR is returned; the operand of the
6028 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6029 other productions, see the documentation accompanying the
6030 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6031 names are looked up in uninstantiated templates. If DECLARATOR_P
6032 is true, the unqualified-id is appearing as part of a declarator,
6033 rather than as part of an expression. */
6036 cp_parser_unqualified_id (cp_parser
* parser
,
6037 bool template_keyword_p
,
6038 bool check_dependency_p
,
6044 /* Peek at the next token. */
6045 token
= cp_lexer_peek_token (parser
->lexer
);
6047 switch ((int) token
->type
)
6053 /* We don't know yet whether or not this will be a
6055 cp_parser_parse_tentatively (parser
);
6056 /* Try a template-id. */
6057 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6060 /* If it worked, we're done. */
6061 if (cp_parser_parse_definitely (parser
))
6063 /* Otherwise, it's an ordinary identifier. */
6064 return cp_parser_identifier (parser
);
6067 case CPP_TEMPLATE_ID
:
6068 return cp_parser_template_id_expr (parser
, template_keyword_p
,
6075 tree qualifying_scope
;
6079 location_t tilde_loc
= token
->location
;
6081 /* Consume the `~' token. */
6082 cp_lexer_consume_token (parser
->lexer
);
6083 /* Parse the class-name. The standard, as written, seems to
6086 template <typename T> struct S { ~S (); };
6087 template <typename T> S<T>::~S() {}
6089 is invalid, since `~' must be followed by a class-name, but
6090 `S<T>' is dependent, and so not known to be a class.
6091 That's not right; we need to look in uninstantiated
6092 templates. A further complication arises from:
6094 template <typename T> void f(T t) {
6098 Here, it is not possible to look up `T' in the scope of `T'
6099 itself. We must look in both the current scope, and the
6100 scope of the containing complete expression.
6102 Yet another issue is:
6111 The standard does not seem to say that the `S' in `~S'
6112 should refer to the type `S' and not the data member
6115 /* DR 244 says that we look up the name after the "~" in the
6116 same scope as we looked up the qualifying name. That idea
6117 isn't fully worked out; it's more complicated than that. */
6118 scope
= parser
->scope
;
6119 object_scope
= parser
->object_scope
;
6120 qualifying_scope
= parser
->qualifying_scope
;
6122 /* Check for invalid scopes. */
6123 if (scope
== error_mark_node
)
6125 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6126 cp_lexer_consume_token (parser
->lexer
);
6127 return error_mark_node
;
6129 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6131 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6132 error_at (token
->location
,
6133 "scope %qT before %<~%> is not a class-name",
6135 cp_parser_simulate_error (parser
);
6136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6137 cp_lexer_consume_token (parser
->lexer
);
6138 return error_mark_node
;
6140 if (template_keyword_p
)
6142 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6143 error_at (tilde_loc
, "%<template%> keyword not permitted in "
6145 cp_parser_simulate_error (parser
);
6146 return error_mark_node
;
6149 gcc_assert (!scope
|| TYPE_P (scope
));
6151 token
= cp_lexer_peek_token (parser
->lexer
);
6153 /* Create a location with caret == start at the tilde,
6154 finishing at the end of the peeked token, e.g:
6158 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6160 /* If the name is of the form "X::~X" it's OK even if X is a
6164 && token
->type
== CPP_NAME
6165 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6167 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6168 || (CLASS_TYPE_P (scope
)
6169 && constructor_name_p (token
->u
.value
, scope
))))
6171 cp_lexer_consume_token (parser
->lexer
);
6172 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6175 /* ~auto means the destructor of whatever the object is. */
6176 if (cp_parser_is_keyword (token
, RID_AUTO
))
6178 if (cxx_dialect
< cxx14
)
6180 "%<~auto%> only available with "
6181 "%<-std=c++14%> or %<-std=gnu++14%>");
6182 cp_lexer_consume_token (parser
->lexer
);
6183 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, make_auto ());
6186 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6187 declarator-id of a constructor or destructor. */
6188 if (token
->type
== CPP_TEMPLATE_ID
&& cxx_dialect
>= cxx20
)
6190 if (!cp_parser_simulate_error (parser
))
6191 error_at (tilde_loc
, "template-id not allowed for destructor");
6192 return error_mark_node
;
6195 /* If there was an explicit qualification (S::~T), first look
6196 in the scope given by the qualification (i.e., S).
6198 Note: in the calls to cp_parser_class_name below we pass
6199 typename_type so that lookup finds the injected-class-name
6200 rather than the constructor. */
6202 type_decl
= NULL_TREE
;
6205 cp_parser_parse_tentatively (parser
);
6206 type_decl
= cp_parser_class_name (parser
,
6207 /*typename_keyword_p=*/false,
6208 /*template_keyword_p=*/false,
6210 /*check_dependency=*/false,
6211 /*class_head_p=*/false,
6213 if (cp_parser_parse_definitely (parser
))
6216 /* In "N::S::~S", look in "N" as well. */
6217 if (!done
&& scope
&& qualifying_scope
)
6219 cp_parser_parse_tentatively (parser
);
6220 parser
->scope
= qualifying_scope
;
6221 parser
->object_scope
= NULL_TREE
;
6222 parser
->qualifying_scope
= NULL_TREE
;
6224 = cp_parser_class_name (parser
,
6225 /*typename_keyword_p=*/false,
6226 /*template_keyword_p=*/false,
6228 /*check_dependency=*/false,
6229 /*class_head_p=*/false,
6231 if (cp_parser_parse_definitely (parser
))
6234 /* In "p->S::~T", look in the scope given by "*p" as well. */
6235 else if (!done
&& object_scope
)
6237 cp_parser_parse_tentatively (parser
);
6238 parser
->scope
= object_scope
;
6239 parser
->object_scope
= NULL_TREE
;
6240 parser
->qualifying_scope
= NULL_TREE
;
6242 = cp_parser_class_name (parser
,
6243 /*typename_keyword_p=*/false,
6244 /*template_keyword_p=*/false,
6246 /*check_dependency=*/false,
6247 /*class_head_p=*/false,
6249 if (cp_parser_parse_definitely (parser
))
6252 /* Look in the surrounding context. */
6255 parser
->scope
= NULL_TREE
;
6256 parser
->object_scope
= NULL_TREE
;
6257 parser
->qualifying_scope
= NULL_TREE
;
6258 if (processing_template_decl
)
6259 cp_parser_parse_tentatively (parser
);
6261 = cp_parser_class_name (parser
,
6262 /*typename_keyword_p=*/false,
6263 /*template_keyword_p=*/false,
6265 /*check_dependency=*/false,
6266 /*class_head_p=*/false,
6268 if (processing_template_decl
6269 && ! cp_parser_parse_definitely (parser
))
6271 /* We couldn't find a type with this name. If we're parsing
6272 tentatively, fail and try something else. */
6273 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6275 cp_parser_simulate_error (parser
);
6276 return error_mark_node
;
6278 /* Otherwise, accept it and check for a match at instantiation
6280 type_decl
= cp_parser_identifier (parser
);
6281 if (type_decl
!= error_mark_node
)
6282 type_decl
= build_min_nt_loc (loc
, BIT_NOT_EXPR
, type_decl
);
6286 /* If an error occurred, assume that the name of the
6287 destructor is the same as the name of the qualifying
6288 class. That allows us to keep parsing after running
6289 into ill-formed destructor names. */
6290 if (type_decl
== error_mark_node
&& scope
)
6291 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6292 else if (type_decl
== error_mark_node
)
6293 return error_mark_node
;
6295 /* Check that destructor name and scope match. */
6296 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6298 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6300 "declaration of %<~%T%> as member of %qT",
6302 cp_parser_simulate_error (parser
);
6303 return error_mark_node
;
6308 A typedef-name that names a class shall not be used as the
6309 identifier in the declarator for a destructor declaration. */
6311 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6312 && !DECL_SELF_REFERENCE_P (type_decl
)
6313 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6315 "typedef-name %qD used as destructor declarator",
6318 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6322 if (token
->keyword
== RID_OPERATOR
)
6326 /* This could be a template-id, so we try that first. */
6327 cp_parser_parse_tentatively (parser
);
6328 /* Try a template-id. */
6329 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6330 /*check_dependency_p=*/true,
6332 /* If that worked, we're done. */
6333 if (cp_parser_parse_definitely (parser
))
6335 /* We still don't know whether we're looking at an
6336 operator-function-id or a conversion-function-id. */
6337 cp_parser_parse_tentatively (parser
);
6338 /* Try an operator-function-id. */
6339 id
= cp_parser_operator_function_id (parser
);
6340 /* If that didn't work, try a conversion-function-id. */
6341 if (!cp_parser_parse_definitely (parser
))
6342 id
= cp_parser_conversion_function_id (parser
);
6351 cp_parser_error (parser
, "expected unqualified-id");
6352 return error_mark_node
;
6356 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6357 be a template-id or the name shall refer to a class template or an
6361 check_template_keyword_in_nested_name_spec (tree name
)
6363 if (CLASS_TYPE_P (name
)
6364 && ((CLASSTYPE_USE_TEMPLATE (name
)
6365 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name
)))
6366 || CLASSTYPE_IS_TEMPLATE (name
)))
6369 if (TREE_CODE (name
) == TYPENAME_TYPE
6370 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name
)) == TEMPLATE_ID_EXPR
)
6372 /* Alias templates are also OK. */
6373 else if (alias_template_specialization_p (name
, nt_opaque
))
6376 permerror (input_location
, TYPE_P (name
)
6377 ? G_("%qT is not a template")
6378 : G_("%qD is not a template"),
6382 /* Parse an (optional) nested-name-specifier.
6384 nested-name-specifier: [C++98]
6385 class-or-namespace-name :: nested-name-specifier [opt]
6386 class-or-namespace-name :: template nested-name-specifier [opt]
6388 nested-name-specifier: [C++0x]
6391 nested-name-specifier identifier ::
6392 nested-name-specifier template [opt] simple-template-id ::
6394 PARSER->SCOPE should be set appropriately before this function is
6395 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6396 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6399 Sets PARSER->SCOPE to the class (TYPE) or namespace
6400 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6401 it unchanged if there is no nested-name-specifier. Returns the new
6402 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6404 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6405 part of a declaration and/or decl-specifier. */
6408 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6409 bool typename_keyword_p
,
6410 bool check_dependency_p
,
6412 bool is_declaration
,
6413 bool template_keyword_p
/* = false */)
6415 bool success
= false;
6416 cp_token_position start
= 0;
6419 /* Remember where the nested-name-specifier starts. */
6420 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
6421 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
6423 start
= cp_lexer_token_position (parser
->lexer
, false);
6424 push_deferring_access_checks (dk_deferred
);
6431 tree saved_qualifying_scope
;
6433 /* Spot cases that cannot be the beginning of a
6434 nested-name-specifier. */
6435 token
= cp_lexer_peek_token (parser
->lexer
);
6437 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6438 the already parsed nested-name-specifier. */
6439 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6441 /* Grab the nested-name-specifier and continue the loop. */
6442 cp_parser_pre_parsed_nested_name_specifier (parser
);
6443 /* If we originally encountered this nested-name-specifier
6444 with IS_DECLARATION set to false, we will not have
6445 resolved TYPENAME_TYPEs, so we must do so here. */
6447 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6449 new_scope
= resolve_typename_type (parser
->scope
,
6450 /*only_current_p=*/false);
6451 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6452 parser
->scope
= new_scope
;
6458 /* Spot cases that cannot be the beginning of a
6459 nested-name-specifier. On the second and subsequent times
6460 through the loop, we look for the `template' keyword. */
6461 if (success
&& token
->keyword
== RID_TEMPLATE
)
6463 /* A template-id can start a nested-name-specifier. */
6464 else if (token
->type
== CPP_TEMPLATE_ID
)
6466 /* DR 743: decltype can be used in a nested-name-specifier. */
6467 else if (token_is_decltype (token
))
6471 /* If the next token is not an identifier, then it is
6472 definitely not a type-name or namespace-name. */
6473 if (token
->type
!= CPP_NAME
)
6475 /* If the following token is neither a `<' (to begin a
6476 template-id), nor a `::', then we are not looking at a
6477 nested-name-specifier. */
6478 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6480 if (token
->type
== CPP_COLON
6481 && parser
->colon_corrects_to_scope_p
6482 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6484 gcc_rich_location
richloc (token
->location
);
6485 richloc
.add_fixit_replace ("::");
6487 "found %<:%> in nested-name-specifier, "
6489 token
->type
= CPP_SCOPE
;
6492 if (token
->type
!= CPP_SCOPE
6493 && !cp_parser_nth_token_starts_template_argument_list_p
6498 /* The nested-name-specifier is optional, so we parse
6500 cp_parser_parse_tentatively (parser
);
6502 /* Look for the optional `template' keyword, if this isn't the
6503 first time through the loop. */
6506 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6507 /* DR1710: "In a qualified-id used as the name in
6508 a typename-specifier, elaborated-type-specifier, using-declaration,
6509 or class-or-decltype, an optional keyword template appearing at
6510 the top level is ignored." */
6511 if (!template_keyword_p
6512 && typename_keyword_p
6513 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
6514 template_keyword_p
= true;
6517 /* Save the old scope since the name lookup we are about to do
6518 might destroy it. */
6519 old_scope
= parser
->scope
;
6520 saved_qualifying_scope
= parser
->qualifying_scope
;
6521 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6522 look up names in "X<T>::I" in order to determine that "Y" is
6523 a template. So, if we have a typename at this point, we make
6524 an effort to look through it. */
6526 && !typename_keyword_p
6528 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6529 parser
->scope
= resolve_typename_type (parser
->scope
,
6530 /*only_current_p=*/false);
6531 /* Parse the qualifying entity. */
6533 = cp_parser_qualifying_entity (parser
,
6539 /* Look for the `::' token. */
6540 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6542 /* If we found what we wanted, we keep going; otherwise, we're
6544 if (!cp_parser_parse_definitely (parser
))
6546 bool error_p
= false;
6548 /* Restore the OLD_SCOPE since it was valid before the
6549 failed attempt at finding the last
6550 class-or-namespace-name. */
6551 parser
->scope
= old_scope
;
6552 parser
->qualifying_scope
= saved_qualifying_scope
;
6554 /* If the next token is a decltype, and the one after that is a
6555 `::', then the decltype has failed to resolve to a class or
6556 enumeration type. Give this error even when parsing
6557 tentatively since it can't possibly be valid--and we're going
6558 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6559 won't get another chance.*/
6560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6561 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6564 token
= cp_lexer_consume_token (parser
->lexer
);
6565 tree dtype
= token
->u
.tree_check_value
->value
;
6566 if (dtype
!= error_mark_node
)
6567 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6568 "which is not a class or enumeration type",
6570 parser
->scope
= error_mark_node
;
6574 cp_lexer_consume_token (parser
->lexer
);
6577 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6578 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6580 /* If we have a non-type template-id followed by ::, it can't
6581 possibly be valid. */
6582 token
= cp_lexer_peek_token (parser
->lexer
);
6583 tree tid
= token
->u
.tree_check_value
->value
;
6584 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6585 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6587 tree tmpl
= NULL_TREE
;
6588 if (is_overloaded_fn (tid
))
6590 tree fns
= get_fns (tid
);
6591 if (OVL_SINGLE_P (fns
))
6592 tmpl
= OVL_FIRST (fns
);
6593 if (function_concept_p (fns
))
6594 error_at (token
->location
, "concept-id %qD "
6595 "in nested-name-specifier", tid
);
6597 error_at (token
->location
, "function template-id "
6598 "%qD in nested-name-specifier", tid
);
6602 tmpl
= TREE_OPERAND (tid
, 0);
6603 if (variable_concept_p (tmpl
)
6604 || standard_concept_p (tmpl
))
6605 error_at (token
->location
, "concept-id %qD "
6606 "in nested-name-specifier", tid
);
6609 /* Variable template. */
6610 gcc_assert (variable_template_p (tmpl
));
6611 error_at (token
->location
, "variable template-id "
6612 "%qD in nested-name-specifier", tid
);
6616 inform (DECL_SOURCE_LOCATION (tmpl
),
6617 "%qD declared here", tmpl
);
6619 parser
->scope
= error_mark_node
;
6623 cp_lexer_consume_token (parser
->lexer
);
6624 cp_lexer_consume_token (parser
->lexer
);
6628 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6630 /* If the next token is an identifier, and the one after
6631 that is a `::', then any valid interpretation would have
6632 found a class-or-namespace-name. */
6633 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6634 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6636 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6639 token
= cp_lexer_consume_token (parser
->lexer
);
6642 if (!token
->error_reported
)
6645 tree ambiguous_decls
;
6647 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6649 /*is_template=*/false,
6650 /*is_namespace=*/false,
6651 /*check_dependency=*/true,
6654 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6655 error_at (token
->location
,
6656 "%qD used without template arguments",
6658 else if (ambiguous_decls
)
6660 // cp_parser_lookup_name has the same diagnostic,
6661 // thus make sure to emit it at most once.
6662 if (cp_parser_uncommitted_to_tentative_parse_p
6665 error_at (token
->location
,
6666 "reference to %qD is ambiguous",
6668 print_candidates (ambiguous_decls
);
6670 decl
= error_mark_node
;
6674 if (cxx_dialect
!= cxx98
)
6675 cp_parser_name_lookup_error
6676 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6679 cp_parser_name_lookup_error
6680 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6684 parser
->scope
= error_mark_node
;
6686 /* Treat this as a successful nested-name-specifier
6691 If the name found is not a class-name (clause
6692 _class_) or namespace-name (_namespace.def_), the
6693 program is ill-formed. */
6696 cp_lexer_consume_token (parser
->lexer
);
6700 /* We've found one valid nested-name-specifier. */
6702 /* Name lookup always gives us a DECL. */
6703 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6704 new_scope
= TREE_TYPE (new_scope
);
6705 /* Uses of "template" must be followed by actual templates. */
6706 if (template_keyword_p
)
6707 check_template_keyword_in_nested_name_spec (new_scope
);
6708 /* If it is a class scope, try to complete it; we are about to
6709 be looking up names inside the class. */
6710 if (TYPE_P (new_scope
)
6711 /* Since checking types for dependency can be expensive,
6712 avoid doing it if the type is already complete. */
6713 && !COMPLETE_TYPE_P (new_scope
)
6714 /* Do not try to complete dependent types. */
6715 && !dependent_type_p (new_scope
))
6717 new_scope
= complete_type (new_scope
);
6718 /* If it is a typedef to current class, use the current
6719 class instead, as the typedef won't have any names inside
6721 if (!COMPLETE_TYPE_P (new_scope
)
6722 && currently_open_class (new_scope
))
6723 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6725 /* Make sure we look in the right scope the next time through
6727 parser
->scope
= new_scope
;
6730 /* If parsing tentatively, replace the sequence of tokens that makes
6731 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6732 token. That way, should we re-parse the token stream, we will
6733 not have to repeat the effort required to do the parse, nor will
6734 we issue duplicate error messages. */
6735 if (success
&& start
)
6739 token
= cp_lexer_token_at (parser
->lexer
, start
);
6740 /* Reset the contents of the START token. */
6741 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6742 /* Retrieve any deferred checks. Do not pop this access checks yet
6743 so the memory will not be reclaimed during token replacing below. */
6744 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6745 token
->tree_check_p
= true;
6746 token
->u
.tree_check_value
->value
= parser
->scope
;
6747 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6748 token
->u
.tree_check_value
->qualifying_scope
=
6749 parser
->qualifying_scope
;
6750 token
->keyword
= RID_MAX
;
6752 /* Purge all subsequent tokens. */
6753 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6757 pop_to_parent_deferring_access_checks ();
6759 return success
? parser
->scope
: NULL_TREE
;
6762 /* Parse a nested-name-specifier. See
6763 cp_parser_nested_name_specifier_opt for details. This function
6764 behaves identically, except that it will an issue an error if no
6765 nested-name-specifier is present. */
6768 cp_parser_nested_name_specifier (cp_parser
*parser
,
6769 bool typename_keyword_p
,
6770 bool check_dependency_p
,
6772 bool is_declaration
)
6776 /* Look for the nested-name-specifier. */
6777 scope
= cp_parser_nested_name_specifier_opt (parser
,
6782 /* If it was not present, issue an error message. */
6785 cp_parser_error (parser
, "expected nested-name-specifier");
6786 parser
->scope
= NULL_TREE
;
6792 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6793 this is either a class-name or a namespace-name (which corresponds
6794 to the class-or-namespace-name production in the grammar). For
6795 C++0x, it can also be a type-name that refers to an enumeration
6796 type or a simple-template-id.
6798 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6799 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6800 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6801 TYPE_P is TRUE iff the next name should be taken as a class-name,
6802 even the same name is declared to be another entity in the same
6805 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6806 specified by the class-or-namespace-name. If neither is found the
6807 ERROR_MARK_NODE is returned. */
6810 cp_parser_qualifying_entity (cp_parser
*parser
,
6811 bool typename_keyword_p
,
6812 bool template_keyword_p
,
6813 bool check_dependency_p
,
6815 bool is_declaration
)
6818 tree saved_qualifying_scope
;
6819 tree saved_object_scope
;
6822 bool successful_parse_p
;
6824 /* DR 743: decltype can appear in a nested-name-specifier. */
6825 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6827 scope
= cp_parser_decltype (parser
);
6828 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6829 && !MAYBE_CLASS_TYPE_P (scope
))
6831 cp_parser_simulate_error (parser
);
6832 return error_mark_node
;
6834 if (TYPE_NAME (scope
))
6835 scope
= TYPE_NAME (scope
);
6839 /* Before we try to parse the class-name, we must save away the
6840 current PARSER->SCOPE since cp_parser_class_name will destroy
6842 saved_scope
= parser
->scope
;
6843 saved_qualifying_scope
= parser
->qualifying_scope
;
6844 saved_object_scope
= parser
->object_scope
;
6845 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6846 there is no need to look for a namespace-name. */
6847 only_class_p
= template_keyword_p
6848 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6850 cp_parser_parse_tentatively (parser
);
6851 scope
= cp_parser_class_name (parser
,
6854 type_p
? class_type
: none_type
,
6856 /*class_head_p=*/false,
6858 /*enum_ok=*/cxx_dialect
> cxx98
);
6859 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6860 /* If that didn't work, try for a namespace-name. */
6861 if (!only_class_p
&& !successful_parse_p
)
6863 /* Restore the saved scope. */
6864 parser
->scope
= saved_scope
;
6865 parser
->qualifying_scope
= saved_qualifying_scope
;
6866 parser
->object_scope
= saved_object_scope
;
6867 /* If we are not looking at an identifier followed by the scope
6868 resolution operator, then this is not part of a
6869 nested-name-specifier. (Note that this function is only used
6870 to parse the components of a nested-name-specifier.) */
6871 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6872 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6873 return error_mark_node
;
6874 scope
= cp_parser_namespace_name (parser
);
6880 /* Return true if we are looking at a compound-literal, false otherwise. */
6883 cp_parser_compound_literal_p (cp_parser
*parser
)
6885 cp_lexer_save_tokens (parser
->lexer
);
6887 /* Skip tokens until the next token is a closing parenthesis.
6888 If we find the closing `)', and the next token is a `{', then
6889 we are looking at a compound-literal. */
6890 bool compound_literal_p
6891 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6892 /*consume_paren=*/true)
6893 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6895 /* Roll back the tokens we skipped. */
6896 cp_lexer_rollback_tokens (parser
->lexer
);
6898 return compound_literal_p
;
6901 /* Return true if EXPR is the integer constant zero or a complex constant
6902 of zero, without any folding, but ignoring location wrappers. */
6905 literal_integer_zerop (const_tree expr
)
6907 return (location_wrapper_p (expr
)
6908 && integer_zerop (TREE_OPERAND (expr
, 0)));
6911 /* Parse a postfix-expression.
6915 postfix-expression [ expression ]
6916 postfix-expression ( expression-list [opt] )
6917 simple-type-specifier ( expression-list [opt] )
6918 typename :: [opt] nested-name-specifier identifier
6919 ( expression-list [opt] )
6920 typename :: [opt] nested-name-specifier template [opt] template-id
6921 ( expression-list [opt] )
6922 postfix-expression . template [opt] id-expression
6923 postfix-expression -> template [opt] id-expression
6924 postfix-expression . pseudo-destructor-name
6925 postfix-expression -> pseudo-destructor-name
6926 postfix-expression ++
6927 postfix-expression --
6928 dynamic_cast < type-id > ( expression )
6929 static_cast < type-id > ( expression )
6930 reinterpret_cast < type-id > ( expression )
6931 const_cast < type-id > ( expression )
6932 typeid ( expression )
6938 ( type-id ) { initializer-list , [opt] }
6940 This extension is a GNU version of the C99 compound-literal
6941 construct. (The C99 grammar uses `type-name' instead of `type-id',
6942 but they are essentially the same concept.)
6944 If ADDRESS_P is true, the postfix expression is the operand of the
6945 `&' operator. CAST_P is true if this expression is the target of a
6948 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6949 class member access expressions [expr.ref].
6951 Returns a representation of the expression. */
6954 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6955 bool member_access_only_p
, bool decltype_p
,
6956 cp_id_kind
* pidk_return
)
6961 cp_id_kind idk
= CP_ID_KIND_NONE
;
6962 cp_expr postfix_expression
= NULL_TREE
;
6963 bool is_member_access
= false;
6965 /* Peek at the next token. */
6966 token
= cp_lexer_peek_token (parser
->lexer
);
6967 loc
= token
->location
;
6968 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6970 /* Some of the productions are determined by keywords. */
6971 keyword
= token
->keyword
;
6981 const char *saved_message
;
6982 bool saved_in_type_id_in_expr_p
;
6984 /* All of these can be handled in the same way from the point
6985 of view of parsing. Begin by consuming the token
6986 identifying the cast. */
6987 cp_lexer_consume_token (parser
->lexer
);
6989 /* New types cannot be defined in the cast. */
6990 saved_message
= parser
->type_definition_forbidden_message
;
6991 parser
->type_definition_forbidden_message
6992 = G_("types may not be defined in casts");
6994 /* Look for the opening `<'. */
6995 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6996 /* Parse the type to which we are casting. */
6997 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6998 parser
->in_type_id_in_expr_p
= true;
6999 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
7001 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7002 /* Look for the closing `>'. */
7003 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
7004 /* Restore the old message. */
7005 parser
->type_definition_forbidden_message
= saved_message
;
7007 bool saved_greater_than_is_operator_p
7008 = parser
->greater_than_is_operator_p
;
7009 parser
->greater_than_is_operator_p
= true;
7011 /* And the expression which is being cast. */
7012 matching_parens parens
;
7013 parens
.require_open (parser
);
7014 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
7015 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
7017 location_t end_loc
= close_paren
?
7018 close_paren
->location
: UNKNOWN_LOCATION
;
7020 parser
->greater_than_is_operator_p
7021 = saved_greater_than_is_operator_p
;
7023 /* Only type conversions to integral or enumeration types
7024 can be used in constant-expressions. */
7025 if (!cast_valid_in_integral_constant_expression_p (type
)
7026 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
7028 postfix_expression
= error_mark_node
;
7032 /* Construct a location e.g. :
7033 reinterpret_cast <int *> (expr)
7034 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7035 ranging from the start of the "*_cast" token to the final closing
7036 paren, with the caret at the start. */
7037 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
7043 = build_dynamic_cast (cp_cast_loc
, type
, expression
,
7044 tf_warning_or_error
);
7048 = build_static_cast (cp_cast_loc
, type
, expression
,
7049 tf_warning_or_error
);
7053 = build_reinterpret_cast (cp_cast_loc
, type
, expression
,
7054 tf_warning_or_error
);
7058 = build_const_cast (cp_cast_loc
, type
, expression
,
7059 tf_warning_or_error
);
7070 const char *saved_message
;
7071 bool saved_in_type_id_in_expr_p
;
7073 /* Consume the `typeid' token. */
7074 cp_lexer_consume_token (parser
->lexer
);
7075 /* Look for the `(' token. */
7076 matching_parens parens
;
7077 parens
.require_open (parser
);
7078 /* Types cannot be defined in a `typeid' expression. */
7079 saved_message
= parser
->type_definition_forbidden_message
;
7080 parser
->type_definition_forbidden_message
7081 = G_("types may not be defined in a %<typeid%> expression");
7082 /* We can't be sure yet whether we're looking at a type-id or an
7084 cp_parser_parse_tentatively (parser
);
7085 /* Try a type-id first. */
7086 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7087 parser
->in_type_id_in_expr_p
= true;
7088 type
= cp_parser_type_id (parser
);
7089 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7090 /* Look for the `)' token. Otherwise, we can't be sure that
7091 we're not looking at an expression: consider `typeid (int
7092 (3))', for example. */
7093 cp_token
*close_paren
= parens
.require_close (parser
);
7094 /* If all went well, simply lookup the type-id. */
7095 if (cp_parser_parse_definitely (parser
))
7096 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
7097 /* Otherwise, fall back to the expression variant. */
7102 /* Look for an expression. */
7103 expression
= cp_parser_expression (parser
, & idk
);
7104 /* Compute its typeid. */
7105 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
7106 /* Look for the `)' token. */
7107 close_paren
= parens
.require_close (parser
);
7109 /* Restore the saved message. */
7110 parser
->type_definition_forbidden_message
= saved_message
;
7111 /* `typeid' may not appear in an integral constant expression. */
7112 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
7113 postfix_expression
= error_mark_node
;
7115 /* Construct a location e.g. :
7118 ranging from the start of the "typeid" token to the final closing
7119 paren, with the caret at the start. */
7122 location_t typeid_loc
7123 = make_location (start_loc
, start_loc
, close_paren
->location
);
7124 postfix_expression
.set_location (typeid_loc
);
7125 postfix_expression
.maybe_add_location_wrapper ();
7133 /* The syntax permitted here is the same permitted for an
7134 elaborated-type-specifier. */
7135 ++parser
->prevent_constrained_type_specifiers
;
7136 type
= cp_parser_elaborated_type_specifier (parser
,
7137 /*is_friend=*/false,
7138 /*is_declaration=*/false);
7139 --parser
->prevent_constrained_type_specifiers
;
7140 postfix_expression
= cp_parser_functional_cast (parser
, type
);
7145 case RID_BUILTIN_SHUFFLE
:
7146 case RID_BUILTIN_LAUNDER
:
7148 vec
<tree
, va_gc
> *vec
;
7152 cp_lexer_consume_token (parser
->lexer
);
7153 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
7154 /*cast_p=*/false, /*allow_expansion_p=*/true,
7155 /*non_constant_p=*/NULL
);
7158 postfix_expression
= error_mark_node
;
7162 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7168 if (vec
->length () == 1)
7170 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7173 error_at (loc
, "wrong number of arguments to "
7174 "%<__builtin_addressof%>");
7175 postfix_expression
= error_mark_node
;
7179 case RID_BUILTIN_LAUNDER
:
7180 if (vec
->length () == 1)
7181 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7182 tf_warning_or_error
);
7185 error_at (loc
, "wrong number of arguments to "
7186 "%<__builtin_launder%>");
7187 postfix_expression
= error_mark_node
;
7191 case RID_BUILTIN_SHUFFLE
:
7192 if (vec
->length () == 2)
7194 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7195 (*vec
)[1], tf_warning_or_error
);
7196 else if (vec
->length () == 3)
7198 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7199 (*vec
)[2], tf_warning_or_error
);
7202 error_at (loc
, "wrong number of arguments to "
7203 "%<__builtin_shuffle%>");
7204 postfix_expression
= error_mark_node
;
7214 case RID_BUILTIN_CONVERTVECTOR
:
7218 /* Consume the `__builtin_convertvector' token. */
7219 cp_lexer_consume_token (parser
->lexer
);
7220 /* Look for the opening `('. */
7221 matching_parens parens
;
7222 parens
.require_open (parser
);
7223 /* Now, parse the assignment-expression. */
7224 expression
= cp_parser_assignment_expression (parser
);
7225 /* Look for the `,'. */
7226 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7227 location_t type_location
7228 = cp_lexer_peek_token (parser
->lexer
)->location
;
7229 /* Parse the type-id. */
7231 type_id_in_expr_sentinel
s (parser
);
7232 type
= cp_parser_type_id (parser
);
7234 /* Look for the closing `)'. */
7235 parens
.require_close (parser
);
7236 return cp_build_vec_convert (expression
, type_location
, type
,
7237 tf_warning_or_error
);
7244 /* If the next thing is a simple-type-specifier, we may be
7245 looking at a functional cast. We could also be looking at
7246 an id-expression. So, we try the functional cast, and if
7247 that doesn't work we fall back to the primary-expression. */
7248 cp_parser_parse_tentatively (parser
);
7249 /* Look for the simple-type-specifier. */
7250 ++parser
->prevent_constrained_type_specifiers
;
7251 type
= cp_parser_simple_type_specifier (parser
,
7252 /*decl_specs=*/NULL
,
7253 CP_PARSER_FLAGS_NONE
);
7254 --parser
->prevent_constrained_type_specifiers
;
7255 /* Parse the cast itself. */
7256 if (!cp_parser_error_occurred (parser
))
7258 = cp_parser_functional_cast (parser
, type
);
7259 /* If that worked, we're done. */
7260 if (cp_parser_parse_definitely (parser
))
7263 /* If the functional-cast didn't work out, try a
7264 compound-literal. */
7265 if (cp_parser_allow_gnu_extensions_p (parser
)
7266 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7268 cp_expr initializer
= NULL_TREE
;
7270 cp_parser_parse_tentatively (parser
);
7272 matching_parens parens
;
7273 parens
.consume_open (parser
);
7275 /* Avoid calling cp_parser_type_id pointlessly, see comment
7276 in cp_parser_cast_expression about c++/29234. */
7277 if (!cp_parser_compound_literal_p (parser
))
7278 cp_parser_simulate_error (parser
);
7281 /* Parse the type. */
7282 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7283 parser
->in_type_id_in_expr_p
= true;
7284 type
= cp_parser_type_id (parser
);
7285 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7286 parens
.require_close (parser
);
7289 /* If things aren't going well, there's no need to
7291 if (!cp_parser_error_occurred (parser
))
7293 bool non_constant_p
;
7294 /* Parse the brace-enclosed initializer list. */
7295 initializer
= cp_parser_braced_list (parser
,
7298 /* If that worked, we're definitely looking at a
7299 compound-literal expression. */
7300 if (cp_parser_parse_definitely (parser
))
7302 /* Warn the user that a compound literal is not
7303 allowed in standard C++. */
7304 pedwarn (input_location
, OPT_Wpedantic
,
7305 "ISO C++ forbids compound-literals");
7306 /* For simplicity, we disallow compound literals in
7307 constant-expressions. We could
7308 allow compound literals of integer type, whose
7309 initializer was a constant, in constant
7310 expressions. Permitting that usage, as a further
7311 extension, would not change the meaning of any
7312 currently accepted programs. (Of course, as
7313 compound literals are not part of ISO C++, the
7314 standard has nothing to say.) */
7315 if (cp_parser_non_integral_constant_expression (parser
,
7318 postfix_expression
= error_mark_node
;
7321 /* Form the representation of the compound-literal. */
7323 = finish_compound_literal (type
, initializer
,
7324 tf_warning_or_error
, fcl_c99
);
7325 postfix_expression
.set_location (initializer
.get_location ());
7330 /* It must be a primary-expression. */
7332 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7333 /*template_arg_p=*/false,
7340 /* Note that we don't need to worry about calling build_cplus_new on a
7341 class-valued CALL_EXPR in decltype when it isn't the end of the
7342 postfix-expression; unary_complex_lvalue will take care of that for
7345 /* Keep looping until the postfix-expression is complete. */
7348 if (idk
== CP_ID_KIND_UNQUALIFIED
7349 && identifier_p (postfix_expression
)
7350 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7351 /* It is not a Koenig lookup function call. */
7353 = unqualified_name_lookup_error (postfix_expression
);
7355 /* Peek at the next token. */
7356 token
= cp_lexer_peek_token (parser
->lexer
);
7358 switch (token
->type
)
7360 case CPP_OPEN_SQUARE
:
7361 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7363 cp_parser_error (parser
,
7364 "two consecutive %<[%> shall "
7365 "only introduce an attribute");
7366 return error_mark_node
;
7369 = cp_parser_postfix_open_square_expression (parser
,
7373 postfix_expression
.set_range (start_loc
,
7374 postfix_expression
.get_location ());
7376 idk
= CP_ID_KIND_NONE
;
7377 is_member_access
= false;
7380 case CPP_OPEN_PAREN
:
7381 /* postfix-expression ( expression-list [opt] ) */
7384 bool is_builtin_constant_p
;
7385 bool saved_integral_constant_expression_p
= false;
7386 bool saved_non_integral_constant_expression_p
= false;
7387 tsubst_flags_t complain
= complain_flags (decltype_p
);
7388 vec
<tree
, va_gc
> *args
;
7389 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7391 is_member_access
= false;
7393 tree stripped_expression
7394 = tree_strip_any_location_wrapper (postfix_expression
);
7395 is_builtin_constant_p
7396 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7397 if (is_builtin_constant_p
)
7399 /* The whole point of __builtin_constant_p is to allow
7400 non-constant expressions to appear as arguments. */
7401 saved_integral_constant_expression_p
7402 = parser
->integral_constant_expression_p
;
7403 saved_non_integral_constant_expression_p
7404 = parser
->non_integral_constant_expression_p
;
7405 parser
->integral_constant_expression_p
= false;
7407 args
= (cp_parser_parenthesized_expression_list
7409 /*cast_p=*/false, /*allow_expansion_p=*/true,
7410 /*non_constant_p=*/NULL
,
7411 /*close_paren_loc=*/&close_paren_loc
,
7412 /*wrap_locations_p=*/true));
7413 if (is_builtin_constant_p
)
7415 parser
->integral_constant_expression_p
7416 = saved_integral_constant_expression_p
;
7417 parser
->non_integral_constant_expression_p
7418 = saved_non_integral_constant_expression_p
;
7423 postfix_expression
= error_mark_node
;
7427 /* Function calls are not permitted in
7428 constant-expressions. */
7429 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7430 && cp_parser_non_integral_constant_expression (parser
,
7433 postfix_expression
= error_mark_node
;
7434 release_tree_vector (args
);
7439 if (idk
== CP_ID_KIND_UNQUALIFIED
7440 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7442 if (identifier_p (postfix_expression
)
7443 /* In C++20, we may need to perform ADL for a template
7445 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7446 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7448 if (!args
->is_empty ())
7451 if (!any_type_dependent_arguments_p (args
))
7453 = perform_koenig_lookup (postfix_expression
, args
,
7458 = unqualified_fn_lookup_error (postfix_expression
);
7460 /* We do not perform argument-dependent lookup if
7461 normal lookup finds a non-function, in accordance
7462 with the expected resolution of DR 218. */
7463 else if (!args
->is_empty ()
7464 && is_overloaded_fn (postfix_expression
))
7466 /* Do not do argument dependent lookup if regular
7467 lookup finds a member function or a block-scope
7468 function declaration. [basic.lookup.argdep]/3 */
7469 bool do_adl_p
= true;
7470 tree fns
= get_fns (postfix_expression
);
7471 for (lkp_iterator
iter (fns
); iter
; ++iter
)
7473 tree fn
= STRIP_TEMPLATE (*iter
);
7474 if ((TREE_CODE (fn
) == USING_DECL
7475 && DECL_DEPENDENT_P (fn
))
7476 || DECL_FUNCTION_MEMBER_P (fn
)
7477 || DECL_LOCAL_DECL_P (fn
))
7487 if (!any_type_dependent_arguments_p (args
))
7489 = perform_koenig_lookup (postfix_expression
, args
,
7495 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7497 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7498 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7500 if (processing_template_decl
7501 && (type_dependent_object_expression_p (instance
)
7502 || (!BASELINK_P (fn
)
7503 && TREE_CODE (fn
) != FIELD_DECL
)
7504 || type_dependent_expression_p (fn
)
7505 || any_type_dependent_arguments_p (args
)))
7507 maybe_generic_this_capture (instance
, fn
);
7509 = build_min_nt_call_vec (postfix_expression
, args
);
7511 else if (BASELINK_P (fn
))
7514 = (build_new_method_call
7515 (instance
, fn
, &args
, NULL_TREE
,
7516 (idk
== CP_ID_KIND_QUALIFIED
7517 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7524 = finish_call_expr (postfix_expression
, &args
,
7525 /*disallow_virtual=*/false,
7529 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7530 || TREE_CODE (postfix_expression
) == MEMBER_REF
7531 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7532 postfix_expression
= (build_offset_ref_call_from_tree
7533 (postfix_expression
, &args
,
7535 else if (idk
== CP_ID_KIND_QUALIFIED
)
7536 /* A call to a static class member, or a namespace-scope
7539 = finish_call_expr (postfix_expression
, &args
,
7540 /*disallow_virtual=*/true,
7544 /* All other function calls. */
7546 = finish_call_expr (postfix_expression
, &args
,
7547 /*disallow_virtual=*/false,
7551 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7553 location_t combined_loc
= make_location (token
->location
,
7556 postfix_expression
.set_location (combined_loc
);
7559 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7560 idk
= CP_ID_KIND_NONE
;
7562 release_tree_vector (args
);
7568 /* postfix-expression . template [opt] id-expression
7569 postfix-expression . pseudo-destructor-name
7570 postfix-expression -> template [opt] id-expression
7571 postfix-expression -> pseudo-destructor-name */
7573 /* Consume the `.' or `->' operator. */
7574 cp_lexer_consume_token (parser
->lexer
);
7577 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7581 is_member_access
= true;
7585 /* postfix-expression ++ */
7586 /* Consume the `++' token. */
7587 cp_lexer_consume_token (parser
->lexer
);
7588 /* Generate a representation for the complete expression. */
7590 = finish_increment_expr (postfix_expression
,
7591 POSTINCREMENT_EXPR
);
7592 /* Increments may not appear in constant-expressions. */
7593 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7594 postfix_expression
= error_mark_node
;
7595 idk
= CP_ID_KIND_NONE
;
7596 is_member_access
= false;
7599 case CPP_MINUS_MINUS
:
7600 /* postfix-expression -- */
7601 /* Consume the `--' token. */
7602 cp_lexer_consume_token (parser
->lexer
);
7603 /* Generate a representation for the complete expression. */
7605 = finish_increment_expr (postfix_expression
,
7606 POSTDECREMENT_EXPR
);
7607 /* Decrements may not appear in constant-expressions. */
7608 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7609 postfix_expression
= error_mark_node
;
7610 idk
= CP_ID_KIND_NONE
;
7611 is_member_access
= false;
7615 if (pidk_return
!= NULL
)
7616 * pidk_return
= idk
;
7617 if (member_access_only_p
)
7618 return is_member_access
7619 ? postfix_expression
7620 : cp_expr (error_mark_node
);
7622 return postfix_expression
;
7626 /* We should never get here. */
7628 return error_mark_node
;
7631 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7632 by cp_parser_builtin_offsetof. We're looking for
7634 postfix-expression [ expression ]
7635 postfix-expression [ braced-init-list ] (C++11)
7637 FOR_OFFSETOF is set if we're being called in that context, which
7638 changes how we deal with integer constant expressions. */
7641 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7642 tree postfix_expression
,
7646 tree index
= NULL_TREE
;
7647 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7648 bool saved_greater_than_is_operator_p
;
7650 /* Consume the `[' token. */
7651 cp_lexer_consume_token (parser
->lexer
);
7653 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7654 parser
->greater_than_is_operator_p
= true;
7656 /* Parse the index expression. */
7657 /* ??? For offsetof, there is a question of what to allow here. If
7658 offsetof is not being used in an integral constant expression context,
7659 then we *could* get the right answer by computing the value at runtime.
7660 If we are in an integral constant expression context, then we might
7661 could accept any constant expression; hard to say without analysis.
7662 Rather than open the barn door too wide right away, allow only integer
7663 constant expressions here. */
7665 index
= cp_parser_constant_expression (parser
);
7668 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7670 bool expr_nonconst_p
;
7671 cp_lexer_set_source_position (parser
->lexer
);
7672 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7673 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7676 index
= cp_parser_expression (parser
, NULL
, /*cast_p=*/false,
7677 /*decltype_p=*/false,
7678 /*warn_comma_p=*/warn_comma_subscript
);
7681 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7683 /* Look for the closing `]'. */
7684 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7686 /* Build the ARRAY_REF. */
7687 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7690 /* When not doing offsetof, array references are not permitted in
7691 constant-expressions. */
7693 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7694 postfix_expression
= error_mark_node
;
7696 return postfix_expression
;
7699 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7700 dereference of incomplete type, returns true if error_mark_node should
7701 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7702 and *DEPENDENT_P. */
7705 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7708 /* In a template, be permissive by treating an object expression
7709 of incomplete type as dependent (after a pedwarn). */
7710 diagnostic_t kind
= (processing_template_decl
7711 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7713 switch (TREE_CODE (*postfix_expression
))
7716 case REINTERPRET_CAST_EXPR
:
7717 case CONST_CAST_EXPR
:
7718 case STATIC_CAST_EXPR
:
7719 case DYNAMIC_CAST_EXPR
:
7720 case IMPLICIT_CONV_EXPR
:
7721 case VIEW_CONVERT_EXPR
:
7722 case NON_LVALUE_EXPR
:
7726 /* Don't emit any diagnostic for OVERLOADs. */
7730 /* Avoid clobbering e.g. DECLs. */
7731 if (!EXPR_P (*postfix_expression
))
7736 if (kind
== DK_IGNORED
)
7739 location_t exploc
= location_of (*postfix_expression
);
7740 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7741 if (!MAYBE_CLASS_TYPE_P (*scope
))
7743 if (kind
== DK_ERROR
)
7744 *scope
= *postfix_expression
= error_mark_node
;
7745 else if (processing_template_decl
)
7747 *dependent_p
= true;
7748 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7753 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7754 by cp_parser_builtin_offsetof. We're looking for
7756 postfix-expression . template [opt] id-expression
7757 postfix-expression . pseudo-destructor-name
7758 postfix-expression -> template [opt] id-expression
7759 postfix-expression -> pseudo-destructor-name
7761 FOR_OFFSETOF is set if we're being called in that context. That sorta
7762 limits what of the above we'll actually accept, but nevermind.
7763 TOKEN_TYPE is the "." or "->" token, which will already have been
7764 removed from the stream. */
7767 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7768 enum cpp_ttype token_type
,
7769 cp_expr postfix_expression
,
7770 bool for_offsetof
, cp_id_kind
*idk
,
7771 location_t location
)
7775 bool pseudo_destructor_p
;
7776 tree scope
= NULL_TREE
;
7777 location_t start_loc
= postfix_expression
.get_start ();
7779 /* If this is a `->' operator, dereference the pointer. */
7780 if (token_type
== CPP_DEREF
)
7781 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7782 tf_warning_or_error
);
7783 /* Check to see whether or not the expression is type-dependent and
7784 not the current instantiation. */
7785 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7786 /* The identifier following the `->' or `.' is not qualified. */
7787 parser
->scope
= NULL_TREE
;
7788 parser
->qualifying_scope
= NULL_TREE
;
7789 parser
->object_scope
= NULL_TREE
;
7790 *idk
= CP_ID_KIND_NONE
;
7792 /* Enter the scope corresponding to the type of the object
7793 given by the POSTFIX_EXPRESSION. */
7796 scope
= TREE_TYPE (postfix_expression
);
7797 /* According to the standard, no expression should ever have
7798 reference type. Unfortunately, we do not currently match
7799 the standard in this respect in that our internal representation
7800 of an expression may have reference type even when the standard
7801 says it does not. Therefore, we have to manually obtain the
7802 underlying type here. */
7803 scope
= non_reference (scope
);
7804 /* The type of the POSTFIX_EXPRESSION must be complete. */
7805 /* Unlike the object expression in other contexts, *this is not
7806 required to be of complete type for purposes of class member
7807 access (5.2.5) outside the member function body. */
7808 if (postfix_expression
!= current_class_ref
7809 && scope
!= error_mark_node
7810 && !currently_open_class (scope
))
7812 scope
= complete_type (scope
);
7813 if (!COMPLETE_TYPE_P (scope
)
7814 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7816 return error_mark_node
;
7821 /* Let the name lookup machinery know that we are processing a
7822 class member access expression. */
7823 parser
->context
->object_type
= scope
;
7824 /* If something went wrong, we want to be able to discern that case,
7825 as opposed to the case where there was no SCOPE due to the type
7826 of expression being dependent. */
7828 scope
= error_mark_node
;
7829 /* If the SCOPE was erroneous, make the various semantic analysis
7830 functions exit quickly -- and without issuing additional error
7832 if (scope
== error_mark_node
)
7833 postfix_expression
= error_mark_node
;
7839 tree type
= TREE_TYPE (postfix_expression
);
7840 /* If we don't have a (type-dependent) object of class type, use
7841 typeof to figure out the type of the object. */
7842 if (type
== NULL_TREE
)
7843 type
= finish_typeof (postfix_expression
);
7844 parser
->context
->object_type
= type
;
7847 /* Assume this expression is not a pseudo-destructor access. */
7848 pseudo_destructor_p
= false;
7850 /* If the SCOPE is a scalar type, then, if this is a valid program,
7851 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7852 is type dependent, it can be pseudo-destructor-name or something else.
7853 Try to parse it as pseudo-destructor-name first. */
7854 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7859 cp_parser_parse_tentatively (parser
);
7860 /* Parse the pseudo-destructor-name. */
7862 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7865 && (cp_parser_error_occurred (parser
)
7866 || !SCALAR_TYPE_P (type
)))
7867 cp_parser_abort_tentative_parse (parser
);
7868 else if (cp_parser_parse_definitely (parser
))
7870 pseudo_destructor_p
= true;
7872 = finish_pseudo_destructor_expr (postfix_expression
,
7877 if (!pseudo_destructor_p
)
7879 /* If the SCOPE is not a scalar type, we are looking at an
7880 ordinary class member access expression, rather than a
7881 pseudo-destructor-name. */
7883 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7884 /* Parse the id-expression. */
7885 name
= (cp_parser_id_expression
7887 cp_parser_optional_template_keyword (parser
),
7888 /*check_dependency_p=*/true,
7890 /*declarator_p=*/false,
7891 /*optional_p=*/false));
7892 /* In general, build a SCOPE_REF if the member name is qualified.
7893 However, if the name was not dependent and has already been
7894 resolved; there is no need to build the SCOPE_REF. For example;
7896 struct X { void f(); };
7897 template <typename T> void f(T* t) { t->X::f(); }
7899 Even though "t" is dependent, "X::f" is not and has been resolved
7900 to a BASELINK; there is no need to include scope information. */
7902 /* But we do need to remember that there was an explicit scope for
7903 virtual function calls. */
7905 *idk
= CP_ID_KIND_QUALIFIED
;
7907 /* If the name is a template-id that names a type, we will get a
7908 TYPE_DECL here. That is invalid code. */
7909 if (TREE_CODE (name
) == TYPE_DECL
)
7911 error_at (token
->location
, "invalid use of %qD", name
);
7912 postfix_expression
= error_mark_node
;
7916 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7918 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7920 error_at (token
->location
, "%<%D::%D%> is not a class member",
7921 parser
->scope
, name
);
7922 postfix_expression
= error_mark_node
;
7925 name
= build_qualified_name (/*type=*/NULL_TREE
,
7929 parser
->scope
= NULL_TREE
;
7930 parser
->qualifying_scope
= NULL_TREE
;
7931 parser
->object_scope
= NULL_TREE
;
7933 if (parser
->scope
&& name
&& BASELINK_P (name
))
7934 adjust_result_of_qualified_name_lookup
7935 (name
, parser
->scope
, scope
);
7937 = finish_class_member_access_expr (postfix_expression
, name
,
7939 tf_warning_or_error
);
7940 /* Build a location e.g.:
7943 where the caret is at the deref token, ranging from
7944 the start of postfix_expression to the end of the access expr. */
7945 location_t combined_loc
7946 = make_location (input_location
, start_loc
, parser
->lexer
);
7947 protected_set_expr_location (postfix_expression
, combined_loc
);
7951 /* We no longer need to look up names in the scope of the object on
7952 the left-hand side of the `.' or `->' operator. */
7953 parser
->context
->object_type
= NULL_TREE
;
7955 /* Outside of offsetof, these operators may not appear in
7956 constant-expressions. */
7958 && (cp_parser_non_integral_constant_expression
7959 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7960 postfix_expression
= error_mark_node
;
7962 return postfix_expression
;
7965 /* Parse a parenthesized expression-list.
7968 assignment-expression
7969 expression-list, assignment-expression
7974 identifier, expression-list
7976 CAST_P is true if this expression is the target of a cast.
7978 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7981 WRAP_LOCATIONS_P is true if expressions within this list for which
7982 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7983 their source locations.
7985 Returns a vector of trees. Each element is a representation of an
7986 assignment-expression. NULL is returned if the ( and or ) are
7987 missing. An empty, but allocated, vector is returned on no
7988 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7989 if we are parsing an attribute list for an attribute that wants a
7990 plain identifier argument, normal_attr for an attribute that wants
7991 an expression, or non_attr if we aren't parsing an attribute list. If
7992 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7993 not all of the expressions in the list were constant.
7994 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7995 will be written to with the location of the closing parenthesis. If
7996 an error occurs, it may or may not be written to. */
7998 static vec
<tree
, va_gc
> *
7999 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
8000 int is_attribute_list
,
8002 bool allow_expansion_p
,
8003 bool *non_constant_p
,
8004 location_t
*close_paren_loc
,
8005 bool wrap_locations_p
)
8007 vec
<tree
, va_gc
> *expression_list
;
8008 bool fold_expr_p
= is_attribute_list
!= non_attr
;
8009 tree identifier
= NULL_TREE
;
8010 bool saved_greater_than_is_operator_p
;
8012 /* Assume all the expressions will be constant. */
8014 *non_constant_p
= false;
8016 matching_parens parens
;
8017 if (!parens
.require_open (parser
))
8020 expression_list
= make_tree_vector ();
8022 /* Within a parenthesized expression, a `>' token is always
8023 the greater-than operator. */
8024 saved_greater_than_is_operator_p
8025 = parser
->greater_than_is_operator_p
;
8026 parser
->greater_than_is_operator_p
= true;
8028 cp_expr
expr (NULL_TREE
);
8030 /* Consume expressions until there are no more. */
8031 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8034 /* At the beginning of attribute lists, check to see if the
8035 next token is an identifier. */
8036 if (is_attribute_list
== id_attr
8037 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
8041 /* Consume the identifier. */
8042 token
= cp_lexer_consume_token (parser
->lexer
);
8043 /* Save the identifier. */
8044 identifier
= token
->u
.value
;
8048 bool expr_non_constant_p
;
8050 /* Parse the next assignment-expression. */
8051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8053 /* A braced-init-list. */
8054 cp_lexer_set_source_position (parser
->lexer
);
8055 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8056 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8057 if (non_constant_p
&& expr_non_constant_p
)
8058 *non_constant_p
= true;
8060 else if (non_constant_p
)
8062 expr
= (cp_parser_constant_expression
8063 (parser
, /*allow_non_constant_p=*/true,
8064 &expr_non_constant_p
));
8065 if (expr_non_constant_p
)
8066 *non_constant_p
= true;
8069 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
8073 expr
= instantiate_non_dependent_expr (expr
);
8075 /* If we have an ellipsis, then this is an expression
8077 if (allow_expansion_p
8078 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8080 /* Consume the `...'. */
8081 cp_lexer_consume_token (parser
->lexer
);
8083 /* Build the argument pack. */
8084 expr
= make_pack_expansion (expr
);
8087 if (wrap_locations_p
)
8088 expr
.maybe_add_location_wrapper ();
8090 /* Add it to the list. We add error_mark_node
8091 expressions to the list, so that we can still tell if
8092 the correct form for a parenthesized expression-list
8093 is found. That gives better errors. */
8094 vec_safe_push (expression_list
, expr
.get_value ());
8096 if (expr
== error_mark_node
)
8100 /* After the first item, attribute lists look the same as
8101 expression lists. */
8102 is_attribute_list
= non_attr
;
8105 /* If the next token isn't a `,', then we are done. */
8106 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8109 /* Otherwise, consume the `,' and keep going. */
8110 cp_lexer_consume_token (parser
->lexer
);
8113 if (close_paren_loc
)
8114 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8116 if (!parens
.require_close (parser
))
8121 /* We try and resync to an unnested comma, as that will give the
8122 user better diagnostics. */
8123 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
8124 /*recovering=*/true,
8126 /*consume_paren=*/true);
8131 parser
->greater_than_is_operator_p
8132 = saved_greater_than_is_operator_p
;
8137 parser
->greater_than_is_operator_p
8138 = saved_greater_than_is_operator_p
;
8141 vec_safe_insert (expression_list
, 0, identifier
);
8143 return expression_list
;
8146 /* Parse a pseudo-destructor-name.
8148 pseudo-destructor-name:
8149 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8150 :: [opt] nested-name-specifier template template-id :: ~ type-name
8151 :: [opt] nested-name-specifier [opt] ~ type-name
8153 If either of the first two productions is used, sets *SCOPE to the
8154 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8155 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8156 or ERROR_MARK_NODE if the parse fails. */
8159 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
8164 bool nested_name_specifier_p
;
8167 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8168 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8169 && !type_dependent_expression_p (object
))
8171 if (cxx_dialect
< cxx14
)
8172 pedwarn (input_location
, 0,
8173 "%<~auto%> only available with "
8174 "%<-std=c++14%> or %<-std=gnu++14%>");
8175 cp_lexer_consume_token (parser
->lexer
);
8176 cp_lexer_consume_token (parser
->lexer
);
8178 *type
= TREE_TYPE (object
);
8182 /* Assume that things will not work out. */
8183 *type
= error_mark_node
;
8185 /* Look for the optional `::' operator. */
8186 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8187 /* Look for the optional nested-name-specifier. */
8188 nested_name_specifier_p
8189 = (cp_parser_nested_name_specifier_opt (parser
,
8190 /*typename_keyword_p=*/false,
8191 /*check_dependency_p=*/true,
8193 /*is_declaration=*/false)
8195 /* Now, if we saw a nested-name-specifier, we might be doing the
8196 second production. */
8197 if (nested_name_specifier_p
8198 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8200 /* Consume the `template' keyword. */
8201 cp_lexer_consume_token (parser
->lexer
);
8202 /* Parse the template-id. */
8203 cp_parser_template_id (parser
,
8204 /*template_keyword_p=*/true,
8205 /*check_dependency_p=*/false,
8207 /*is_declaration=*/true);
8208 /* Look for the `::' token. */
8209 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8211 /* If the next token is not a `~', then there might be some
8212 additional qualification. */
8213 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8215 /* At this point, we're looking for "type-name :: ~". The type-name
8216 must not be a class-name, since this is a pseudo-destructor. So,
8217 it must be either an enum-name, or a typedef-name -- both of which
8218 are just identifiers. So, we peek ahead to check that the "::"
8219 and "~" tokens are present; if they are not, then we can avoid
8220 calling type_name. */
8221 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8222 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8223 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8225 cp_parser_error (parser
, "non-scalar type");
8229 /* Look for the type-name. */
8230 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8231 if (*scope
== error_mark_node
)
8234 /* Look for the `::' token. */
8235 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8240 /* Look for the `~'. */
8241 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8243 /* Once we see the ~, this has to be a pseudo-destructor. */
8244 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8245 cp_parser_commit_to_topmost_tentative_parse (parser
);
8247 /* Look for the type-name again. We are not responsible for
8248 checking that it matches the first type-name. */
8249 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8252 /* Parse a unary-expression.
8259 unary-operator cast-expression
8260 sizeof unary-expression
8262 alignof ( type-id ) [C++0x]
8269 __extension__ cast-expression
8270 __alignof__ unary-expression
8271 __alignof__ ( type-id )
8272 alignof unary-expression [C++0x]
8273 __real__ cast-expression
8274 __imag__ cast-expression
8276 sizeof ( type-id ) { initializer-list , [opt] }
8277 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8278 __alignof__ ( type-id ) { initializer-list , [opt] }
8280 ADDRESS_P is true iff the unary-expression is appearing as the
8281 operand of the `&' operator. CAST_P is true if this expression is
8282 the target of a cast.
8284 Returns a representation of the expression. */
8287 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8288 bool address_p
, bool cast_p
, bool decltype_p
)
8291 enum tree_code unary_operator
;
8293 /* Peek at the next token. */
8294 token
= cp_lexer_peek_token (parser
->lexer
);
8295 /* Some keywords give away the kind of expression. */
8296 if (token
->type
== CPP_KEYWORD
)
8298 enum rid keyword
= token
->keyword
;
8307 location_t start_loc
= token
->location
;
8309 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8310 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8312 /* Consume the token. */
8313 cp_lexer_consume_token (parser
->lexer
);
8314 /* Parse the operand. */
8315 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8317 /* Construct a location e.g. :
8320 with start == caret at the start of the "alignof"/"sizeof"
8321 token, with the endpoint at the final closing paren. */
8322 location_t compound_loc
8323 = make_location (start_loc
, start_loc
, parser
->lexer
);
8325 if (TYPE_P (operand
))
8326 ret
= cxx_sizeof_or_alignof_type (compound_loc
, operand
, op
,
8330 /* ISO C++ defines alignof only with types, not with
8331 expressions. So pedwarn if alignof is used with a non-
8332 type expression. However, __alignof__ is ok. */
8334 pedwarn (token
->location
, OPT_Wpedantic
,
8335 "ISO C++ does not allow %<alignof%> "
8338 ret
= cxx_sizeof_or_alignof_expr (compound_loc
, operand
, op
,
8341 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8342 SIZEOF_EXPR with the original operand. */
8343 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8345 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8347 if (!processing_template_decl
&& TYPE_P (operand
))
8349 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8350 build1 (NOP_EXPR
, operand
,
8352 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8355 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8356 TREE_SIDE_EFFECTS (ret
) = 0;
8357 TREE_READONLY (ret
) = 1;
8358 SET_EXPR_LOCATION (ret
, compound_loc
);
8362 cp_expr
ret_expr (ret
, compound_loc
);
8363 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8367 case RID_BUILTIN_HAS_ATTRIBUTE
:
8368 return cp_parser_has_attribute_expression (parser
);
8371 return cp_parser_new_expression (parser
);
8374 return cp_parser_delete_expression (parser
);
8378 /* The saved value of the PEDANTIC flag. */
8382 /* Save away the PEDANTIC flag. */
8383 cp_parser_extension_opt (parser
, &saved_pedantic
);
8384 /* Parse the cast-expression. */
8385 expr
= cp_parser_simple_cast_expression (parser
);
8386 /* Restore the PEDANTIC flag. */
8387 pedantic
= saved_pedantic
;
8397 /* Consume the `__real__' or `__imag__' token. */
8398 cp_lexer_consume_token (parser
->lexer
);
8399 /* Parse the cast-expression. */
8400 expression
= cp_parser_simple_cast_expression (parser
);
8401 /* Create the complete representation. */
8402 return build_x_unary_op (token
->location
,
8403 (keyword
== RID_REALPART
8404 ? REALPART_EXPR
: IMAGPART_EXPR
),
8406 tf_warning_or_error
);
8410 case RID_TRANSACTION_ATOMIC
:
8411 case RID_TRANSACTION_RELAXED
:
8412 return cp_parser_transaction_expression (parser
, keyword
);
8417 const char *saved_message
;
8418 bool saved_integral_constant_expression_p
;
8419 bool saved_non_integral_constant_expression_p
;
8420 bool saved_greater_than_is_operator_p
;
8422 location_t start_loc
= token
->location
;
8424 cp_lexer_consume_token (parser
->lexer
);
8425 matching_parens parens
;
8426 parens
.require_open (parser
);
8428 saved_message
= parser
->type_definition_forbidden_message
;
8429 parser
->type_definition_forbidden_message
8430 = G_("types may not be defined in %<noexcept%> expressions");
8432 saved_integral_constant_expression_p
8433 = parser
->integral_constant_expression_p
;
8434 saved_non_integral_constant_expression_p
8435 = parser
->non_integral_constant_expression_p
;
8436 parser
->integral_constant_expression_p
= false;
8438 saved_greater_than_is_operator_p
8439 = parser
->greater_than_is_operator_p
;
8440 parser
->greater_than_is_operator_p
= true;
8442 ++cp_unevaluated_operand
;
8443 ++c_inhibit_evaluation_warnings
;
8444 ++cp_noexcept_operand
;
8445 expr
= cp_parser_expression (parser
);
8446 --cp_noexcept_operand
;
8447 --c_inhibit_evaluation_warnings
;
8448 --cp_unevaluated_operand
;
8450 parser
->greater_than_is_operator_p
8451 = saved_greater_than_is_operator_p
;
8453 parser
->integral_constant_expression_p
8454 = saved_integral_constant_expression_p
;
8455 parser
->non_integral_constant_expression_p
8456 = saved_non_integral_constant_expression_p
;
8458 parser
->type_definition_forbidden_message
= saved_message
;
8460 parens
.require_close (parser
);
8462 /* Construct a location of the form:
8465 with start == caret, finishing at the close-paren. */
8466 location_t noexcept_loc
8467 = make_location (start_loc
, start_loc
, parser
->lexer
);
8469 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8476 location_t kw_loc
= token
->location
;
8478 /* Consume the `co_await' token. */
8479 cp_lexer_consume_token (parser
->lexer
);
8480 /* Parse its cast-expression. */
8481 expr
= cp_parser_simple_cast_expression (parser
);
8482 if (expr
== error_mark_node
)
8483 return error_mark_node
;
8485 /* Handle [expr.await]. */
8486 return cp_expr (finish_co_await_expr (kw_loc
, expr
));
8494 /* Look for the `:: new' and `:: delete', which also signal the
8495 beginning of a new-expression, or delete-expression,
8496 respectively. If the next token is `::', then it might be one of
8498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8502 /* See if the token after the `::' is one of the keywords in
8503 which we're interested. */
8504 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8505 /* If it's `new', we have a new-expression. */
8506 if (keyword
== RID_NEW
)
8507 return cp_parser_new_expression (parser
);
8508 /* Similarly, for `delete'. */
8509 else if (keyword
== RID_DELETE
)
8510 return cp_parser_delete_expression (parser
);
8513 /* Look for a unary operator. */
8514 unary_operator
= cp_parser_unary_operator (token
);
8515 /* The `++' and `--' operators can be handled similarly, even though
8516 they are not technically unary-operators in the grammar. */
8517 if (unary_operator
== ERROR_MARK
)
8519 if (token
->type
== CPP_PLUS_PLUS
)
8520 unary_operator
= PREINCREMENT_EXPR
;
8521 else if (token
->type
== CPP_MINUS_MINUS
)
8522 unary_operator
= PREDECREMENT_EXPR
;
8523 /* Handle the GNU address-of-label extension. */
8524 else if (cp_parser_allow_gnu_extensions_p (parser
)
8525 && token
->type
== CPP_AND_AND
)
8529 location_t start_loc
= token
->location
;
8531 /* Consume the '&&' token. */
8532 cp_lexer_consume_token (parser
->lexer
);
8533 /* Look for the identifier. */
8534 identifier
= cp_parser_identifier (parser
);
8535 /* Construct a location of the form:
8538 with caret==start at the "&&", finish at the end of the label. */
8539 location_t combined_loc
8540 = make_location (start_loc
, start_loc
, parser
->lexer
);
8541 /* Create an expression representing the address. */
8542 expression
= finish_label_address_expr (identifier
, combined_loc
);
8543 if (cp_parser_non_integral_constant_expression (parser
,
8545 expression
= error_mark_node
;
8549 if (unary_operator
!= ERROR_MARK
)
8551 cp_expr cast_expression
;
8552 cp_expr expression
= error_mark_node
;
8553 non_integral_constant non_constant_p
= NIC_NONE
;
8554 location_t loc
= token
->location
;
8555 tsubst_flags_t complain
= complain_flags (decltype_p
);
8557 /* Consume the operator token. */
8558 token
= cp_lexer_consume_token (parser
->lexer
);
8559 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8561 /* Parse the cast-expression. */
8563 = cp_parser_cast_expression (parser
,
8564 unary_operator
== ADDR_EXPR
,
8570 OP_TOKEN CAST_EXPRESSION
8571 ^~~~~~~~~~~~~~~~~~~~~~~~~
8572 with start==caret at the operator token, and
8573 extending to the end of the cast_expression. */
8574 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8576 /* Now, build an appropriate representation. */
8577 switch (unary_operator
)
8580 non_constant_p
= NIC_STAR
;
8581 expression
= build_x_indirect_ref (loc
, cast_expression
,
8584 /* TODO: build_x_indirect_ref does not always honor the
8585 location, so ensure it is set. */
8586 expression
.set_location (loc
);
8590 non_constant_p
= NIC_ADDR
;
8593 expression
= build_x_unary_op (loc
, unary_operator
,
8596 /* TODO: build_x_unary_op does not always honor the location,
8597 so ensure it is set. */
8598 expression
.set_location (loc
);
8601 case PREINCREMENT_EXPR
:
8602 case PREDECREMENT_EXPR
:
8603 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8604 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8607 /* Immediately fold negation of a constant, unless the constant is 0
8608 (since -0 == 0) or it would overflow. */
8609 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8612 = tree_strip_any_location_wrapper (cast_expression
);
8613 if (CONSTANT_CLASS_P (stripped_expr
)
8614 && !integer_zerop (stripped_expr
)
8615 && !TREE_OVERFLOW (stripped_expr
))
8617 tree folded
= fold_build1 (unary_operator
,
8618 TREE_TYPE (stripped_expr
),
8620 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8622 expression
= maybe_wrap_with_location (folded
, loc
);
8628 case UNARY_PLUS_EXPR
:
8629 case TRUTH_NOT_EXPR
:
8630 expression
= finish_unary_op_expr (loc
, unary_operator
,
8631 cast_expression
, complain
);
8638 if (non_constant_p
!= NIC_NONE
8639 && cp_parser_non_integral_constant_expression (parser
,
8641 expression
= error_mark_node
;
8646 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8647 /*member_access_only_p=*/false,
8652 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8653 unary-operator, the corresponding tree code is returned. */
8655 static enum tree_code
8656 cp_parser_unary_operator (cp_token
* token
)
8658 switch (token
->type
)
8661 return INDIRECT_REF
;
8667 return UNARY_PLUS_EXPR
;
8673 return TRUTH_NOT_EXPR
;
8676 return BIT_NOT_EXPR
;
8683 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8684 Returns a representation of the expression. */
8687 cp_parser_has_attribute_expression (cp_parser
*parser
)
8689 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8691 /* Consume the __builtin_has_attribute token. */
8692 cp_lexer_consume_token (parser
->lexer
);
8694 matching_parens parens
;
8695 if (!parens
.require_open (parser
))
8696 return error_mark_node
;
8698 /* Types cannot be defined in a `sizeof' expression. Save away the
8700 const char *saved_message
= parser
->type_definition_forbidden_message
;
8701 const char *saved_message_arg
8702 = parser
->type_definition_forbidden_message_arg
;
8703 parser
->type_definition_forbidden_message
8704 = G_("types may not be defined in %qs expressions");
8705 parser
->type_definition_forbidden_message_arg
8706 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
8708 /* The restrictions on constant-expressions do not apply inside
8709 sizeof expressions. */
8710 bool saved_integral_constant_expression_p
8711 = parser
->integral_constant_expression_p
;
8712 bool saved_non_integral_constant_expression_p
8713 = parser
->non_integral_constant_expression_p
;
8714 parser
->integral_constant_expression_p
= false;
8716 /* Do not actually evaluate the expression. */
8717 ++cp_unevaluated_operand
;
8718 ++c_inhibit_evaluation_warnings
;
8720 tree oper
= NULL_TREE
;
8722 /* We can't be sure yet whether we're looking at a type-id or an
8724 cp_parser_parse_tentatively (parser
);
8726 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8727 parser
->in_type_id_in_expr_p
= true;
8728 /* Look for the type-id. */
8729 oper
= cp_parser_type_id (parser
);
8730 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8732 cp_parser_parse_definitely (parser
);
8734 /* If the type-id production did not work out, then we must be
8735 looking at an expression. */
8736 if (!oper
|| oper
== error_mark_node
)
8737 oper
= cp_parser_assignment_expression (parser
);
8739 STRIP_ANY_LOCATION_WRAPPER (oper
);
8741 /* Go back to evaluating expressions. */
8742 --cp_unevaluated_operand
;
8743 --c_inhibit_evaluation_warnings
;
8745 /* And restore the old one. */
8746 parser
->type_definition_forbidden_message
= saved_message
;
8747 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
8748 parser
->integral_constant_expression_p
8749 = saved_integral_constant_expression_p
;
8750 parser
->non_integral_constant_expression_p
8751 = saved_non_integral_constant_expression_p
;
8753 /* Consume the comma if it's there. */
8754 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8756 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8757 /*consume_paren=*/true);
8758 return error_mark_node
;
8761 /* Parse the attribute specification. */
8763 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8764 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8766 if (oper
== error_mark_node
)
8768 else if (type_dependent_expression_p (oper
))
8769 sorry_at (atloc
, "%<__builtin_has_attribute%> with dependent argument "
8770 "not supported yet");
8773 /* Fold constant expressions used in attributes first. */
8774 cp_check_const_attributes (attr
);
8776 /* Finally, see if OPER has been declared with ATTR. */
8777 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8780 parens
.require_close (parser
);
8784 error_at (atloc
, "expected identifier");
8785 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8788 /* Construct a location e.g. :
8789 __builtin_has_attribute (oper, attr)
8790 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8791 with start == caret at the start of the built-in token,
8792 and with the endpoint at the final closing paren. */
8793 location_t compound_loc
8794 = make_location (start_loc
, start_loc
, parser
->lexer
);
8796 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8797 ret_expr
.set_location (compound_loc
);
8798 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8802 /* Parse a new-expression.
8805 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8806 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8808 Returns a representation of the expression. */
8811 cp_parser_new_expression (cp_parser
* parser
)
8813 bool global_scope_p
;
8814 vec
<tree
, va_gc
> *placement
;
8816 vec
<tree
, va_gc
> *initializer
;
8817 tree nelts
= NULL_TREE
;
8820 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8822 /* Look for the optional `::' operator. */
8824 = (cp_parser_global_scope_opt (parser
,
8825 /*current_scope_valid_p=*/false)
8827 /* Look for the `new' operator. */
8828 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8829 /* There's no easy way to tell a new-placement from the
8830 `( type-id )' construct. */
8831 cp_parser_parse_tentatively (parser
);
8832 /* Look for a new-placement. */
8833 placement
= cp_parser_new_placement (parser
);
8834 /* If that didn't work out, there's no new-placement. */
8835 if (!cp_parser_parse_definitely (parser
))
8837 if (placement
!= NULL
)
8838 release_tree_vector (placement
);
8842 /* If the next token is a `(', then we have a parenthesized
8844 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8847 const char *saved_message
= parser
->type_definition_forbidden_message
;
8849 /* Consume the `('. */
8850 matching_parens parens
;
8851 parens
.consume_open (parser
);
8853 /* Parse the type-id. */
8854 parser
->type_definition_forbidden_message
8855 = G_("types may not be defined in a new-expression");
8857 type_id_in_expr_sentinel
s (parser
);
8858 type
= cp_parser_type_id (parser
);
8860 parser
->type_definition_forbidden_message
= saved_message
;
8862 /* Look for the closing `)'. */
8863 parens
.require_close (parser
);
8864 token
= cp_lexer_peek_token (parser
->lexer
);
8865 /* There should not be a direct-new-declarator in this production,
8866 but GCC used to allowed this, so we check and emit a sensible error
8867 message for this case. */
8868 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8870 error_at (token
->location
,
8871 "array bound forbidden after parenthesized type-id");
8872 inform (token
->location
,
8873 "try removing the parentheses around the type-id");
8874 cp_parser_direct_new_declarator (parser
);
8877 /* Otherwise, there must be a new-type-id. */
8879 type
= cp_parser_new_type_id (parser
, &nelts
);
8881 /* If the next token is a `(' or '{', then we have a new-initializer. */
8882 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8883 if (token
->type
== CPP_OPEN_PAREN
8884 || token
->type
== CPP_OPEN_BRACE
)
8885 initializer
= cp_parser_new_initializer (parser
);
8889 /* A new-expression may not appear in an integral constant
8891 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8892 ret
= error_mark_node
;
8893 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8894 of a new-type-id or type-id of a new-expression, the new-expression shall
8895 contain a new-initializer of the form ( assignment-expression )".
8896 Additionally, consistently with the spirit of DR 1467, we want to accept
8897 'new auto { 2 }' too. */
8898 else if ((ret
= type_uses_auto (type
))
8899 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8900 && (vec_safe_length (initializer
) != 1
8901 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8902 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8904 error_at (token
->location
,
8905 "initialization of new-expression for type %<auto%> "
8906 "requires exactly one element");
8907 ret
= error_mark_node
;
8911 /* Construct a location e.g.:
8914 with caret == start at the start of the "new" token, and the end
8915 at the end of the final token we consumed. */
8916 location_t combined_loc
= make_location (start_loc
, start_loc
,
8918 /* Create a representation of the new-expression. */
8919 ret
= build_new (combined_loc
, &placement
, type
, nelts
, &initializer
,
8920 global_scope_p
, tf_warning_or_error
);
8923 if (placement
!= NULL
)
8924 release_tree_vector (placement
);
8925 if (initializer
!= NULL
)
8926 release_tree_vector (initializer
);
8931 /* Parse a new-placement.
8936 Returns the same representation as for an expression-list. */
8938 static vec
<tree
, va_gc
> *
8939 cp_parser_new_placement (cp_parser
* parser
)
8941 vec
<tree
, va_gc
> *expression_list
;
8943 /* Parse the expression-list. */
8944 expression_list
= (cp_parser_parenthesized_expression_list
8945 (parser
, non_attr
, /*cast_p=*/false,
8946 /*allow_expansion_p=*/true,
8947 /*non_constant_p=*/NULL
));
8949 if (expression_list
&& expression_list
->is_empty ())
8950 error ("expected expression-list or type-id");
8952 return expression_list
;
8955 /* Parse a new-type-id.
8958 type-specifier-seq new-declarator [opt]
8960 Returns the TYPE allocated. If the new-type-id indicates an array
8961 type, *NELTS is set to the number of elements in the last array
8962 bound; the TYPE will not include the last array bound. */
8965 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8967 cp_decl_specifier_seq type_specifier_seq
;
8968 cp_declarator
*new_declarator
;
8969 cp_declarator
*declarator
;
8970 cp_declarator
*outer_declarator
;
8971 const char *saved_message
;
8973 /* The type-specifier sequence must not contain type definitions.
8974 (It cannot contain declarations of new types either, but if they
8975 are not definitions we will catch that because they are not
8977 saved_message
= parser
->type_definition_forbidden_message
;
8978 parser
->type_definition_forbidden_message
8979 = G_("types may not be defined in a new-type-id");
8980 /* Parse the type-specifier-seq. */
8981 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8982 /*is_declaration=*/false,
8983 /*is_trailing_return=*/false,
8984 &type_specifier_seq
);
8985 /* Restore the old message. */
8986 parser
->type_definition_forbidden_message
= saved_message
;
8988 if (type_specifier_seq
.type
== error_mark_node
)
8989 return error_mark_node
;
8991 /* Parse the new-declarator. */
8992 new_declarator
= cp_parser_new_declarator_opt (parser
);
8994 /* Determine the number of elements in the last array dimension, if
8997 /* Skip down to the last array dimension. */
8998 declarator
= new_declarator
;
8999 outer_declarator
= NULL
;
9000 while (declarator
&& (declarator
->kind
== cdk_pointer
9001 || declarator
->kind
== cdk_ptrmem
))
9003 outer_declarator
= declarator
;
9004 declarator
= declarator
->declarator
;
9007 && declarator
->kind
== cdk_array
9008 && declarator
->declarator
9009 && declarator
->declarator
->kind
== cdk_array
)
9011 outer_declarator
= declarator
;
9012 declarator
= declarator
->declarator
;
9015 if (declarator
&& declarator
->kind
== cdk_array
)
9017 *nelts
= declarator
->u
.array
.bounds
;
9018 if (*nelts
== error_mark_node
)
9019 *nelts
= integer_one_node
;
9021 if (*nelts
== NULL_TREE
)
9022 /* Leave [] in the declarator. */;
9023 else if (outer_declarator
)
9024 outer_declarator
->declarator
= declarator
->declarator
;
9026 new_declarator
= NULL
;
9029 return groktypename (&type_specifier_seq
, new_declarator
, false);
9032 /* Parse an (optional) new-declarator.
9035 ptr-operator new-declarator [opt]
9036 direct-new-declarator
9038 Returns the declarator. */
9040 static cp_declarator
*
9041 cp_parser_new_declarator_opt (cp_parser
* parser
)
9043 enum tree_code code
;
9044 tree type
, std_attributes
= NULL_TREE
;
9045 cp_cv_quals cv_quals
;
9047 /* We don't know if there's a ptr-operator next, or not. */
9048 cp_parser_parse_tentatively (parser
);
9049 /* Look for a ptr-operator. */
9050 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
9051 /* If that worked, look for more new-declarators. */
9052 if (cp_parser_parse_definitely (parser
))
9054 cp_declarator
*declarator
;
9056 /* Parse another optional declarator. */
9057 declarator
= cp_parser_new_declarator_opt (parser
);
9059 declarator
= cp_parser_make_indirect_declarator
9060 (code
, type
, cv_quals
, declarator
, std_attributes
);
9065 /* If the next token is a `[', there is a direct-new-declarator. */
9066 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9067 return cp_parser_direct_new_declarator (parser
);
9072 /* Parse a direct-new-declarator.
9074 direct-new-declarator:
9076 direct-new-declarator [constant-expression]
9080 static cp_declarator
*
9081 cp_parser_direct_new_declarator (cp_parser
* parser
)
9083 cp_declarator
*declarator
= NULL
;
9084 bool first_p
= true;
9091 /* Look for the opening `['. */
9092 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9094 token
= cp_lexer_peek_token (parser
->lexer
);
9095 if (token
->type
== CPP_CLOSE_SQUARE
&& first_p
)
9096 expression
= NULL_TREE
;
9098 expression
= cp_parser_expression (parser
);
9099 /* The standard requires that the expression have integral
9100 type. DR 74 adds enumeration types. We believe that the
9101 real intent is that these expressions be handled like the
9102 expression in a `switch' condition, which also allows
9103 classes with a single conversion to integral or
9104 enumeration type. */
9105 if (expression
&& !processing_template_decl
)
9108 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
9113 error_at (token
->location
,
9114 "expression in new-declarator must have integral "
9115 "or enumeration type");
9116 expression
= error_mark_node
;
9120 /* Look for the closing `]'. */
9121 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9123 /* Add this bound to the declarator. */
9124 declarator
= make_array_declarator (declarator
, expression
);
9126 /* If the next token is not a `[', then there are no more
9128 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
9136 /* Parse a new-initializer.
9139 ( expression-list [opt] )
9142 Returns a representation of the expression-list. */
9144 static vec
<tree
, va_gc
> *
9145 cp_parser_new_initializer (cp_parser
* parser
)
9147 vec
<tree
, va_gc
> *expression_list
;
9149 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9152 bool expr_non_constant_p
;
9153 cp_lexer_set_source_position (parser
->lexer
);
9154 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9155 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9156 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
9157 expression_list
= make_tree_vector_single (t
);
9160 expression_list
= (cp_parser_parenthesized_expression_list
9161 (parser
, non_attr
, /*cast_p=*/false,
9162 /*allow_expansion_p=*/true,
9163 /*non_constant_p=*/NULL
));
9165 return expression_list
;
9168 /* Parse a delete-expression.
9171 :: [opt] delete cast-expression
9172 :: [opt] delete [ ] cast-expression
9174 Returns a representation of the expression. */
9177 cp_parser_delete_expression (cp_parser
* parser
)
9179 bool global_scope_p
;
9182 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9184 /* Look for the optional `::' operator. */
9186 = (cp_parser_global_scope_opt (parser
,
9187 /*current_scope_valid_p=*/false)
9189 /* Look for the `delete' keyword. */
9190 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9191 /* See if the array syntax is in use. */
9192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9194 /* Consume the `[' token. */
9195 cp_lexer_consume_token (parser
->lexer
);
9196 /* Look for the `]' token. */
9197 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9198 /* Remember that this is the `[]' construct. */
9204 /* Parse the cast-expression. */
9205 expression
= cp_parser_simple_cast_expression (parser
);
9207 /* A delete-expression may not appear in an integral constant
9209 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9210 return error_mark_node
;
9212 /* Construct a location e.g.:
9215 with caret == start at the start of the "delete" token, and
9216 the end at the end of the final token we consumed. */
9217 location_t combined_loc
= make_location (start_loc
, start_loc
,
9219 expression
= delete_sanity (combined_loc
, expression
, NULL_TREE
, array_p
,
9220 global_scope_p
, tf_warning_or_error
);
9225 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9226 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9230 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9232 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9233 switch (token
->type
)
9239 case CPP_CLOSE_SQUARE
:
9240 case CPP_CLOSE_PAREN
:
9241 case CPP_CLOSE_BRACE
:
9242 case CPP_OPEN_BRACE
:
9246 case CPP_DEREF_STAR
:
9254 case CPP_GREATER_EQ
:
9275 case CPP_OPEN_PAREN
:
9276 /* In ((type ()) () the last () isn't a valid cast-expression,
9277 so the whole must be parsed as postfix-expression. */
9278 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9281 case CPP_OPEN_SQUARE
:
9282 /* '[' may start a primary-expression in obj-c++ and in C++11,
9283 as a lambda-expression, eg, '(void)[]{}'. */
9284 if (cxx_dialect
>= cxx11
)
9286 return c_dialect_objc ();
9289 case CPP_MINUS_MINUS
:
9290 /* '++' and '--' may or may not start a cast-expression:
9292 struct T { void operator++(int); };
9293 void f() { (T())++; }
9306 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9307 in the order: const_cast, static_cast, reinterpret_cast.
9309 Don't suggest dynamic_cast.
9311 Return the first legal cast kind found, or NULL otherwise. */
9314 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9318 /* Reuse the parser logic by attempting to build the various kinds of
9319 cast, with "complain" disabled.
9320 Identify the first such cast that is valid. */
9322 /* Don't attempt to run such logic within template processing. */
9323 if (processing_template_decl
)
9326 /* First try const_cast. */
9327 trial
= build_const_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9328 if (trial
!= error_mark_node
)
9329 return "const_cast";
9331 /* If that fails, try static_cast. */
9332 trial
= build_static_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9333 if (trial
!= error_mark_node
)
9334 return "static_cast";
9336 /* Finally, try reinterpret_cast. */
9337 trial
= build_reinterpret_cast (input_location
, dst_type
, orig_expr
,
9339 if (trial
!= error_mark_node
)
9340 return "reinterpret_cast";
9342 /* No such cast possible. */
9346 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9347 suggesting how to convert a C-style cast of the form:
9351 to a C++-style cast.
9353 The primary range of RICHLOC is asssumed to be that of the original
9354 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9355 of the parens in the C-style cast. */
9358 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9359 location_t close_paren_loc
, tree orig_expr
,
9362 /* This function is non-trivial, so bail out now if the warning isn't
9363 going to be emitted. */
9364 if (!warn_old_style_cast
)
9367 /* Try to find a legal C++ cast, trying them in order:
9368 const_cast, static_cast, reinterpret_cast. */
9369 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9370 if (!cast_suggestion
)
9373 /* Replace the open paren with "CAST_SUGGESTION<". */
9375 pp_printf (&pp
, "%s<", cast_suggestion
);
9376 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9378 /* Replace the close paren with "> (". */
9379 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9381 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9382 rich_loc
->add_fixit_insert_after (")");
9386 /* Parse a cast-expression.
9390 ( type-id ) cast-expression
9392 ADDRESS_P is true iff the unary-expression is appearing as the
9393 operand of the `&' operator. CAST_P is true if this expression is
9394 the target of a cast.
9396 Returns a representation of the expression. */
9399 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9400 bool decltype_p
, cp_id_kind
* pidk
)
9402 /* If it's a `(', then we might be looking at a cast. */
9403 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9405 tree type
= NULL_TREE
;
9406 cp_expr
expr (NULL_TREE
);
9407 int cast_expression
= 0;
9408 const char *saved_message
;
9410 /* There's no way to know yet whether or not this is a cast.
9411 For example, `(int (3))' is a unary-expression, while `(int)
9412 3' is a cast. So, we resort to parsing tentatively. */
9413 cp_parser_parse_tentatively (parser
);
9414 /* Types may not be defined in a cast. */
9415 saved_message
= parser
->type_definition_forbidden_message
;
9416 parser
->type_definition_forbidden_message
9417 = G_("types may not be defined in casts");
9418 /* Consume the `('. */
9419 matching_parens parens
;
9420 cp_token
*open_paren
= parens
.consume_open (parser
);
9421 location_t open_paren_loc
= open_paren
->location
;
9422 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9424 /* A very tricky bit is that `(struct S) { 3 }' is a
9425 compound-literal (which we permit in C++ as an extension).
9426 But, that construct is not a cast-expression -- it is a
9427 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9428 is legal; if the compound-literal were a cast-expression,
9429 you'd need an extra set of parentheses.) But, if we parse
9430 the type-id, and it happens to be a class-specifier, then we
9431 will commit to the parse at that point, because we cannot
9432 undo the action that is done when creating a new class. So,
9433 then we cannot back up and do a postfix-expression.
9435 Another tricky case is the following (c++/29234):
9437 struct S { void operator () (); };
9444 As a type-id we parse the parenthesized S()() as a function
9445 returning a function, groktypename complains and we cannot
9446 back up in this case either.
9448 Therefore, we scan ahead to the closing `)', and check to see
9449 if the tokens after the `)' can start a cast-expression. Otherwise
9450 we are dealing with an unary-expression, a postfix-expression
9453 Yet another tricky case, in C++11, is the following (c++/54891):
9457 The issue is that usually, besides the case of lambda-expressions,
9458 the parenthesized type-id cannot be followed by '[', and, eg, we
9459 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9460 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9461 we don't commit, we try a cast-expression, then an unary-expression.
9463 Save tokens so that we can put them back. */
9464 cp_lexer_save_tokens (parser
->lexer
);
9466 /* We may be looking at a cast-expression. */
9467 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9468 /*consume_paren=*/true))
9470 = cp_parser_tokens_start_cast_expression (parser
);
9472 /* Roll back the tokens we skipped. */
9473 cp_lexer_rollback_tokens (parser
->lexer
);
9474 /* If we aren't looking at a cast-expression, simulate an error so
9475 that the call to cp_parser_error_occurred below returns true. */
9476 if (!cast_expression
)
9477 cp_parser_simulate_error (parser
);
9480 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9481 parser
->in_type_id_in_expr_p
= true;
9482 /* Look for the type-id. */
9483 type
= cp_parser_type_id (parser
);
9484 /* Look for the closing `)'. */
9485 cp_token
*close_paren
= parens
.require_close (parser
);
9487 close_paren_loc
= close_paren
->location
;
9488 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9491 /* Restore the saved message. */
9492 parser
->type_definition_forbidden_message
= saved_message
;
9494 /* At this point this can only be either a cast or a
9495 parenthesized ctor such as `(T ())' that looks like a cast to
9496 function returning T. */
9497 if (!cp_parser_error_occurred (parser
))
9499 /* Only commit if the cast-expression doesn't start with
9500 '++', '--', or '[' in C++11. */
9501 if (cast_expression
> 0)
9502 cp_parser_commit_to_topmost_tentative_parse (parser
);
9504 expr
= cp_parser_cast_expression (parser
,
9505 /*address_p=*/false,
9507 /*decltype_p=*/false,
9510 if (cp_parser_parse_definitely (parser
))
9512 /* Warn about old-style casts, if so requested. */
9513 if (warn_old_style_cast
9514 && !in_system_header_at (input_location
)
9515 && !VOID_TYPE_P (type
)
9516 && current_lang_name
!= lang_name_c
)
9518 gcc_rich_location
rich_loc (input_location
);
9519 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9521 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9522 "use of old-style cast to %q#T", type
);
9525 /* Only type conversions to integral or enumeration types
9526 can be used in constant-expressions. */
9527 if (!cast_valid_in_integral_constant_expression_p (type
)
9528 && cp_parser_non_integral_constant_expression (parser
,
9530 return error_mark_node
;
9532 /* Perform the cast. */
9536 with start==caret at the open paren, extending to the
9538 location_t cast_loc
= make_location (open_paren_loc
,
9540 expr
.get_finish ());
9541 expr
= build_c_cast (cast_loc
, type
, expr
);
9546 cp_parser_abort_tentative_parse (parser
);
9549 /* If we get here, then it's not a cast, so it must be a
9550 unary-expression. */
9551 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9552 cast_p
, decltype_p
);
9555 /* Parse a binary expression of the general form:
9559 pm-expression .* cast-expression
9560 pm-expression ->* cast-expression
9562 multiplicative-expression:
9564 multiplicative-expression * pm-expression
9565 multiplicative-expression / pm-expression
9566 multiplicative-expression % pm-expression
9568 additive-expression:
9569 multiplicative-expression
9570 additive-expression + multiplicative-expression
9571 additive-expression - multiplicative-expression
9575 shift-expression << additive-expression
9576 shift-expression >> additive-expression
9578 relational-expression:
9580 relational-expression < shift-expression
9581 relational-expression > shift-expression
9582 relational-expression <= shift-expression
9583 relational-expression >= shift-expression
9587 relational-expression:
9588 relational-expression <? shift-expression
9589 relational-expression >? shift-expression
9591 equality-expression:
9592 relational-expression
9593 equality-expression == relational-expression
9594 equality-expression != relational-expression
9598 and-expression & equality-expression
9600 exclusive-or-expression:
9602 exclusive-or-expression ^ and-expression
9604 inclusive-or-expression:
9605 exclusive-or-expression
9606 inclusive-or-expression | exclusive-or-expression
9608 logical-and-expression:
9609 inclusive-or-expression
9610 logical-and-expression && inclusive-or-expression
9612 logical-or-expression:
9613 logical-and-expression
9614 logical-or-expression || logical-and-expression
9616 All these are implemented with a single function like:
9619 simple-cast-expression
9620 binary-expression <token> binary-expression
9622 CAST_P is true if this expression is the target of a cast.
9624 The binops_by_token map is used to get the tree codes for each <token> type.
9625 binary-expressions are associated according to a precedence table. */
9627 #define TOKEN_PRECEDENCE(token) \
9628 (((token->type == CPP_GREATER \
9629 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9630 && !parser->greater_than_is_operator_p) \
9631 ? PREC_NOT_OPERATOR \
9632 : binops_by_token[token->type].prec)
9635 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9636 bool no_toplevel_fold_p
,
9638 enum cp_parser_prec prec
,
9641 cp_parser_expression_stack stack
;
9642 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9643 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9644 cp_parser_expression_stack_entry current
;
9647 enum tree_code rhs_type
;
9648 enum cp_parser_prec new_prec
, lookahead_prec
;
9651 /* Parse the first expression. */
9652 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9653 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9654 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9655 cast_p
, decltype_p
, pidk
);
9656 current
.prec
= prec
;
9658 if (cp_parser_error_occurred (parser
))
9659 return error_mark_node
;
9663 /* Get an operator token. */
9664 token
= cp_lexer_peek_token (parser
->lexer
);
9666 if (warn_cxx11_compat
9667 && token
->type
== CPP_RSHIFT
9668 && !parser
->greater_than_is_operator_p
)
9670 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9671 "%<>>%> operator is treated"
9672 " as two right angle brackets in C++11"))
9673 inform (token
->location
,
9674 "suggest parentheses around %<>>%> expression");
9677 new_prec
= TOKEN_PRECEDENCE (token
);
9678 if (new_prec
!= PREC_NOT_OPERATOR
9679 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9680 /* This is a fold-expression; handle it later. */
9681 new_prec
= PREC_NOT_OPERATOR
;
9683 /* Popping an entry off the stack means we completed a subexpression:
9684 - either we found a token which is not an operator (`>' where it is not
9685 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9686 will happen repeatedly;
9687 - or, we found an operator which has lower priority. This is the case
9688 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9690 if (new_prec
<= current
.prec
)
9699 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9700 current
.loc
= token
->location
;
9702 /* We used the operator token. */
9703 cp_lexer_consume_token (parser
->lexer
);
9705 /* For "false && x" or "true || x", x will never be executed;
9706 disable warnings while evaluating it. */
9707 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
9708 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
9709 || (current
.tree_type
== TRUTH_ORIF_EXPR
9710 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
9712 disable_warnings_sp
= sp
;
9713 ++c_inhibit_evaluation_warnings
;
9716 /* Extract another operand. It may be the RHS of this expression
9717 or the LHS of a new, higher priority expression. */
9718 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9719 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9720 rhs
= cp_parser_simple_cast_expression (parser
);
9722 /* Get another operator token. Look up its precedence to avoid
9723 building a useless (immediately popped) stack entry for common
9724 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9725 token
= cp_lexer_peek_token (parser
->lexer
);
9726 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9727 if (lookahead_prec
!= PREC_NOT_OPERATOR
9728 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9729 lookahead_prec
= PREC_NOT_OPERATOR
;
9730 if (lookahead_prec
> new_prec
)
9732 /* ... and prepare to parse the RHS of the new, higher priority
9733 expression. Since precedence levels on the stack are
9734 monotonically increasing, we do not have to care about
9739 current
.lhs_type
= rhs_type
;
9740 current
.prec
= new_prec
;
9741 new_prec
= lookahead_prec
;
9745 lookahead_prec
= new_prec
;
9746 /* If the stack is not empty, we have parsed into LHS the right side
9747 (`4' in the example above) of an expression we had suspended.
9748 We can use the information on the stack to recover the LHS (`3')
9749 from the stack together with the tree code (`MULT_EXPR'), and
9750 the precedence of the higher level subexpression
9751 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9752 which will be used to actually build the additive expression. */
9754 rhs_type
= current
.lhs_type
;
9759 /* Undo the disabling of warnings done above. */
9760 if (sp
== disable_warnings_sp
)
9762 disable_warnings_sp
= NULL
;
9763 --c_inhibit_evaluation_warnings
;
9766 if (warn_logical_not_paren
9767 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9768 && current
.lhs_type
== TRUTH_NOT_EXPR
9769 /* Avoid warning for !!x == y. */
9770 && (TREE_CODE (current
.lhs
) != NE_EXPR
9771 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9772 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9773 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9774 /* Avoid warning for !b == y where b is boolean. */
9775 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9776 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9778 /* Avoid warning for !!b == y where b is boolean. */
9779 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9780 || TREE_TYPE (current
.lhs
) == NULL_TREE
9781 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9782 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9783 current
.lhs
, maybe_constant_value (rhs
));
9787 location_t combined_loc
= make_location (current
.loc
,
9788 current
.lhs
.get_start (),
9791 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9792 ERROR_MARK for everything that is not a binary expression.
9793 This makes warn_about_parentheses miss some warnings that
9794 involve unary operators. For unary expressions we should
9795 pass the correct tree_code unless the unary expression was
9796 surrounded by parentheses.
9798 if (no_toplevel_fold_p
9799 && lookahead_prec
<= current
.prec
9802 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9803 current
.lhs
= error_mark_node
;
9806 current
.lhs
.maybe_add_location_wrapper ();
9807 rhs
.maybe_add_location_wrapper ();
9809 = build_min (current
.tree_type
,
9810 TREE_CODE_CLASS (current
.tree_type
)
9812 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9813 current
.lhs
.get_value (), rhs
.get_value ());
9814 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9819 op_location_t
op_loc (current
.loc
, combined_loc
);
9820 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9821 current
.lhs
, current
.lhs_type
,
9822 rhs
, rhs_type
, &overload
,
9823 complain_flags (decltype_p
));
9824 /* TODO: build_x_binary_op doesn't always honor the location. */
9825 current
.lhs
.set_location (combined_loc
);
9827 current
.lhs_type
= current
.tree_type
;
9829 /* If the binary operator required the use of an overloaded operator,
9830 then this expression cannot be an integral constant-expression.
9831 An overloaded operator can be used even if both operands are
9832 otherwise permissible in an integral constant-expression if at
9833 least one of the operands is of enumeration type. */
9836 && cp_parser_non_integral_constant_expression (parser
,
9838 return error_mark_node
;
9845 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9846 bool no_toplevel_fold_p
,
9847 enum cp_parser_prec prec
,
9850 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9851 /*decltype*/false, prec
, pidk
);
9854 /* Parse the `? expression : assignment-expression' part of a
9855 conditional-expression. The LOGICAL_OR_EXPR is the
9856 logical-or-expression that started the conditional-expression.
9857 Returns a representation of the entire conditional-expression.
9859 This routine is used by cp_parser_assignment_expression.
9861 ? expression : assignment-expression
9865 ? : assignment-expression */
9868 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9870 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9871 cp_expr assignment_expr
;
9872 struct cp_token
*token
;
9873 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9875 /* Consume the `?' token. */
9876 cp_lexer_consume_token (parser
->lexer
);
9877 token
= cp_lexer_peek_token (parser
->lexer
);
9878 if (cp_parser_allow_gnu_extensions_p (parser
)
9879 && token
->type
== CPP_COLON
)
9881 pedwarn (token
->location
, OPT_Wpedantic
,
9882 "ISO C++ does not allow %<?:%> with omitted middle operand");
9883 /* Implicit true clause. */
9885 c_inhibit_evaluation_warnings
+=
9886 folded_logical_or_expr
== truthvalue_true_node
;
9887 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9891 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9892 parser
->colon_corrects_to_scope_p
= false;
9893 /* Parse the expression. */
9894 c_inhibit_evaluation_warnings
+=
9895 folded_logical_or_expr
== truthvalue_false_node
;
9896 expr
= cp_parser_expression (parser
);
9897 c_inhibit_evaluation_warnings
+=
9898 ((folded_logical_or_expr
== truthvalue_true_node
)
9899 - (folded_logical_or_expr
== truthvalue_false_node
));
9900 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9903 /* The next token should be a `:'. */
9904 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9905 /* Parse the assignment-expression. */
9906 assignment_expr
= cp_parser_assignment_expression (parser
);
9907 c_inhibit_evaluation_warnings
-=
9908 folded_logical_or_expr
== truthvalue_true_node
;
9911 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9912 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9913 with the caret at the "?", ranging from the start of
9914 the logical_or_expr to the end of the assignment_expr. */
9915 loc
= make_location (loc
,
9916 logical_or_expr
.get_start (),
9917 assignment_expr
.get_finish ());
9919 /* Build the conditional-expression. */
9920 return build_x_conditional_expr (loc
, logical_or_expr
,
9923 tf_warning_or_error
);
9926 /* Parse an assignment-expression.
9928 assignment-expression:
9929 conditional-expression
9930 logical-or-expression assignment-operator assignment_expression
9934 CAST_P is true if this expression is the target of a cast.
9935 DECLTYPE_P is true if this expression is the operand of decltype.
9937 Returns a representation for the expression. */
9940 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9941 bool cast_p
, bool decltype_p
)
9945 /* If the next token is the `throw' keyword, then we're looking at
9946 a throw-expression. */
9947 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9948 expr
= cp_parser_throw_expression (parser
);
9949 /* If the next token is the `co_yield' keyword, then we're looking at
9950 a yield-expression. */
9951 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CO_YIELD
))
9952 expr
= cp_parser_yield_expression (parser
);
9953 /* Otherwise, it must be that we are looking at a
9954 logical-or-expression. */
9957 /* Parse the binary expressions (logical-or-expression). */
9958 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9960 PREC_NOT_OPERATOR
, pidk
);
9961 /* If the next token is a `?' then we're actually looking at a
9962 conditional-expression. */
9963 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9964 return cp_parser_question_colon_clause (parser
, expr
);
9967 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9969 /* If it's an assignment-operator, we're using the second
9971 enum tree_code assignment_operator
9972 = cp_parser_assignment_operator_opt (parser
);
9973 if (assignment_operator
!= ERROR_MARK
)
9975 bool non_constant_p
;
9977 /* Parse the right-hand side of the assignment. */
9978 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9981 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9982 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9984 /* An assignment may not appear in a
9985 constant-expression. */
9986 if (cp_parser_non_integral_constant_expression (parser
,
9988 return error_mark_node
;
9989 /* Build the assignment expression. Its default
9993 is the location of the '=' token as the
9994 caret, ranging from the start of the lhs to the
9996 loc
= make_location (loc
,
9999 expr
= build_x_modify_expr (loc
, expr
,
10000 assignment_operator
,
10002 complain_flags (decltype_p
));
10003 /* TODO: build_x_modify_expr doesn't honor the location,
10004 so we must set it here. */
10005 expr
.set_location (loc
);
10013 /* Parse an (optional) assignment-operator.
10015 assignment-operator: one of
10016 = *= /= %= += -= >>= <<= &= ^= |=
10020 assignment-operator: one of
10023 If the next token is an assignment operator, the corresponding tree
10024 code is returned, and the token is consumed. For example, for
10025 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10026 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10027 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10028 operator, ERROR_MARK is returned. */
10030 static enum tree_code
10031 cp_parser_assignment_operator_opt (cp_parser
* parser
)
10036 /* Peek at the next token. */
10037 token
= cp_lexer_peek_token (parser
->lexer
);
10039 switch (token
->type
)
10050 op
= TRUNC_DIV_EXPR
;
10054 op
= TRUNC_MOD_EXPR
;
10065 case CPP_RSHIFT_EQ
:
10069 case CPP_LSHIFT_EQ
:
10086 /* Nothing else is an assignment operator. */
10090 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10091 if (op
!= ERROR_MARK
10092 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10095 /* If it was an assignment operator, consume it. */
10096 if (op
!= ERROR_MARK
)
10097 cp_lexer_consume_token (parser
->lexer
);
10102 /* Parse an expression.
10105 assignment-expression
10106 expression , assignment-expression
10108 CAST_P is true if this expression is the target of a cast.
10109 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10110 except possibly parenthesized or on the RHS of a comma (N3276).
10111 WARN_COMMA_P is true if a comma should be diagnosed.
10113 Returns a representation of the expression. */
10116 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10117 bool cast_p
, bool decltype_p
, bool warn_comma_p
)
10119 cp_expr expression
= NULL_TREE
;
10120 location_t loc
= UNKNOWN_LOCATION
;
10124 cp_expr assignment_expression
;
10126 /* Parse the next assignment-expression. */
10127 assignment_expression
10128 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
10130 /* We don't create a temporary for a call that is the immediate operand
10131 of decltype or on the RHS of a comma. But when we see a comma, we
10132 need to create a temporary for a call on the LHS. */
10133 if (decltype_p
&& !processing_template_decl
10134 && TREE_CODE (assignment_expression
) == CALL_EXPR
10135 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
10136 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10137 assignment_expression
10138 = build_cplus_new (TREE_TYPE (assignment_expression
),
10139 assignment_expression
, tf_warning_or_error
);
10141 /* If this is the first assignment-expression, we can just
10144 expression
= assignment_expression
;
10147 /* Create a location with caret at the comma, ranging
10148 from the start of the LHS to the end of the RHS. */
10149 loc
= make_location (loc
,
10150 expression
.get_start (),
10151 assignment_expression
.get_finish ());
10152 expression
= build_x_compound_expr (loc
, expression
,
10153 assignment_expression
,
10154 complain_flags (decltype_p
));
10155 expression
.set_location (loc
);
10157 /* If the next token is not a comma, or we're in a fold-expression, then
10158 we are done with the expression. */
10159 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
10160 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10162 /* Consume the `,'. */
10163 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10166 /* [depr.comma.subscript]: A comma expression appearing as
10167 the expr-or-braced-init-list of a subscripting expression
10168 is deprecated. A parenthesized comma expression is not
10170 warning_at (loc
, OPT_Wcomma_subscript
,
10171 "top-level comma expression in array subscript "
10173 warn_comma_p
= false;
10175 cp_lexer_consume_token (parser
->lexer
);
10176 /* A comma operator cannot appear in a constant-expression. */
10177 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
10178 expression
= error_mark_node
;
10184 /* Parse a constant-expression.
10186 constant-expression:
10187 conditional-expression
10189 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10190 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10191 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10192 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10193 only parse a conditional-expression, otherwise parse an
10194 assignment-expression. See below for rationale. */
10197 cp_parser_constant_expression (cp_parser
* parser
,
10198 bool allow_non_constant_p
,
10199 bool *non_constant_p
,
10202 bool saved_integral_constant_expression_p
;
10203 bool saved_allow_non_integral_constant_expression_p
;
10204 bool saved_non_integral_constant_expression_p
;
10205 cp_expr expression
;
10207 /* It might seem that we could simply parse the
10208 conditional-expression, and then check to see if it were
10209 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10210 one that the compiler can figure out is constant, possibly after
10211 doing some simplifications or optimizations. The standard has a
10212 precise definition of constant-expression, and we must honor
10213 that, even though it is somewhat more restrictive.
10219 is not a legal declaration, because `(2, 3)' is not a
10220 constant-expression. The `,' operator is forbidden in a
10221 constant-expression. However, GCC's constant-folding machinery
10222 will fold this operation to an INTEGER_CST for `3'. */
10224 /* Save the old settings. */
10225 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10226 saved_allow_non_integral_constant_expression_p
10227 = parser
->allow_non_integral_constant_expression_p
;
10228 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10229 /* We are now parsing a constant-expression. */
10230 parser
->integral_constant_expression_p
= true;
10231 parser
->allow_non_integral_constant_expression_p
10232 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10233 parser
->non_integral_constant_expression_p
= false;
10234 /* Although the grammar says "conditional-expression", when not STRICT_P,
10235 we parse an "assignment-expression", which also permits
10236 "throw-expression" and the use of assignment operators. In the case
10237 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10238 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10239 actually essential that we look for an assignment-expression.
10240 For example, cp_parser_initializer_clauses uses this function to
10241 determine whether a particular assignment-expression is in fact
10245 /* Parse the binary expressions (logical-or-expression). */
10246 expression
= cp_parser_binary_expression (parser
, false, false, false,
10247 PREC_NOT_OPERATOR
, NULL
);
10248 /* If the next token is a `?' then we're actually looking at
10249 a conditional-expression; otherwise we're done. */
10250 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10251 expression
= cp_parser_question_colon_clause (parser
, expression
);
10254 expression
= cp_parser_assignment_expression (parser
);
10255 /* Restore the old settings. */
10256 parser
->integral_constant_expression_p
10257 = saved_integral_constant_expression_p
;
10258 parser
->allow_non_integral_constant_expression_p
10259 = saved_allow_non_integral_constant_expression_p
;
10260 if (cxx_dialect
>= cxx11
)
10262 /* Require an rvalue constant expression here; that's what our
10263 callers expect. Reference constant expressions are handled
10264 separately in e.g. cp_parser_template_argument. */
10265 tree decay
= expression
;
10266 if (TREE_TYPE (expression
)
10267 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10268 decay
= build_address (expression
);
10269 bool is_const
= is_rvalue_constant_expression (decay
);
10270 parser
->non_integral_constant_expression_p
= !is_const
;
10271 if (!is_const
&& !allow_non_constant_p
)
10272 require_rvalue_constant_expression (decay
);
10274 if (allow_non_constant_p
)
10275 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10276 parser
->non_integral_constant_expression_p
10277 = saved_non_integral_constant_expression_p
;
10282 /* Parse __builtin_offsetof.
10284 offsetof-expression:
10285 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10287 offsetof-member-designator:
10289 | offsetof-member-designator "." id-expression
10290 | offsetof-member-designator "[" expression "]"
10291 | offsetof-member-designator "->" id-expression */
10294 cp_parser_builtin_offsetof (cp_parser
*parser
)
10296 int save_ice_p
, save_non_ice_p
;
10301 location_t finish_loc
;
10303 /* We're about to accept non-integral-constant things, but will
10304 definitely yield an integral constant expression. Save and
10305 restore these values around our local parsing. */
10306 save_ice_p
= parser
->integral_constant_expression_p
;
10307 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10309 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10311 /* Consume the "__builtin_offsetof" token. */
10312 cp_lexer_consume_token (parser
->lexer
);
10313 /* Consume the opening `('. */
10314 matching_parens parens
;
10315 parens
.require_open (parser
);
10316 /* Parse the type-id. */
10317 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10319 const char *saved_message
= parser
->type_definition_forbidden_message
;
10320 parser
->type_definition_forbidden_message
10321 = G_("types may not be defined within %<__builtin_offsetof%>");
10322 type
= cp_parser_type_id (parser
);
10323 parser
->type_definition_forbidden_message
= saved_message
;
10325 /* Look for the `,'. */
10326 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10327 token
= cp_lexer_peek_token (parser
->lexer
);
10329 /* Build the (type *)null that begins the traditional offsetof macro. */
10331 = build_static_cast (input_location
, build_pointer_type (type
),
10332 null_pointer_node
, tf_warning_or_error
);
10334 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10335 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10336 true, &dummy
, token
->location
);
10339 token
= cp_lexer_peek_token (parser
->lexer
);
10340 switch (token
->type
)
10342 case CPP_OPEN_SQUARE
:
10343 /* offsetof-member-designator "[" expression "]" */
10344 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10349 /* offsetof-member-designator "->" identifier */
10350 expr
= grok_array_decl (token
->location
, expr
,
10351 integer_zero_node
, false);
10355 /* offsetof-member-designator "." identifier */
10356 cp_lexer_consume_token (parser
->lexer
);
10357 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10358 expr
, true, &dummy
,
10362 case CPP_CLOSE_PAREN
:
10363 /* Consume the ")" token. */
10364 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10365 cp_lexer_consume_token (parser
->lexer
);
10369 /* Error. We know the following require will fail, but
10370 that gives the proper error message. */
10371 parens
.require_close (parser
);
10372 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10373 expr
= error_mark_node
;
10379 /* Make a location of the form:
10380 __builtin_offsetof (struct s, f)
10381 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10382 with caret at the type-id, ranging from the start of the
10383 "_builtin_offsetof" token to the close paren. */
10384 loc
= make_location (loc
, start_loc
, finish_loc
);
10385 /* The result will be an INTEGER_CST, so we need to explicitly
10386 preserve the location. */
10387 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10390 parser
->integral_constant_expression_p
= save_ice_p
;
10391 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10393 expr
= expr
.maybe_add_location_wrapper ();
10397 /* Parse a trait expression.
10399 Returns a representation of the expression, the underlying type
10400 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10403 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10405 cp_trait_kind kind
;
10406 tree type1
, type2
= NULL_TREE
;
10407 bool binary
= false;
10408 bool variadic
= false;
10412 case RID_HAS_NOTHROW_ASSIGN
:
10413 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10415 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10416 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10418 case RID_HAS_NOTHROW_COPY
:
10419 kind
= CPTK_HAS_NOTHROW_COPY
;
10421 case RID_HAS_TRIVIAL_ASSIGN
:
10422 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10424 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10425 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10427 case RID_HAS_TRIVIAL_COPY
:
10428 kind
= CPTK_HAS_TRIVIAL_COPY
;
10430 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10431 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10433 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10434 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10436 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10437 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10439 case RID_IS_ABSTRACT
:
10440 kind
= CPTK_IS_ABSTRACT
;
10442 case RID_IS_AGGREGATE
:
10443 kind
= CPTK_IS_AGGREGATE
;
10445 case RID_IS_BASE_OF
:
10446 kind
= CPTK_IS_BASE_OF
;
10450 kind
= CPTK_IS_CLASS
;
10453 kind
= CPTK_IS_EMPTY
;
10456 kind
= CPTK_IS_ENUM
;
10459 kind
= CPTK_IS_FINAL
;
10461 case RID_IS_LITERAL_TYPE
:
10462 kind
= CPTK_IS_LITERAL_TYPE
;
10465 kind
= CPTK_IS_POD
;
10467 case RID_IS_POLYMORPHIC
:
10468 kind
= CPTK_IS_POLYMORPHIC
;
10470 case RID_IS_SAME_AS
:
10471 kind
= CPTK_IS_SAME_AS
;
10474 case RID_IS_STD_LAYOUT
:
10475 kind
= CPTK_IS_STD_LAYOUT
;
10477 case RID_IS_TRIVIAL
:
10478 kind
= CPTK_IS_TRIVIAL
;
10480 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10481 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10484 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10485 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10488 case RID_IS_TRIVIALLY_COPYABLE
:
10489 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10492 kind
= CPTK_IS_UNION
;
10494 case RID_UNDERLYING_TYPE
:
10495 kind
= CPTK_UNDERLYING_TYPE
;
10500 case RID_DIRECT_BASES
:
10501 kind
= CPTK_DIRECT_BASES
;
10503 case RID_IS_ASSIGNABLE
:
10504 kind
= CPTK_IS_ASSIGNABLE
;
10507 case RID_IS_CONSTRUCTIBLE
:
10508 kind
= CPTK_IS_CONSTRUCTIBLE
;
10511 case RID_IS_NOTHROW_ASSIGNABLE
:
10512 kind
= CPTK_IS_NOTHROW_ASSIGNABLE
;
10515 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
10516 kind
= CPTK_IS_NOTHROW_CONSTRUCTIBLE
;
10520 gcc_unreachable ();
10523 /* Get location of initial token. */
10524 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10526 /* Consume the token. */
10527 cp_lexer_consume_token (parser
->lexer
);
10529 matching_parens parens
;
10530 parens
.require_open (parser
);
10533 type_id_in_expr_sentinel
s (parser
);
10534 type1
= cp_parser_type_id (parser
);
10537 if (type1
== error_mark_node
)
10538 return error_mark_node
;
10542 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10545 type_id_in_expr_sentinel
s (parser
);
10546 type2
= cp_parser_type_id (parser
);
10549 if (type2
== error_mark_node
)
10550 return error_mark_node
;
10554 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10556 cp_lexer_consume_token (parser
->lexer
);
10557 tree elt
= cp_parser_type_id (parser
);
10558 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10560 cp_lexer_consume_token (parser
->lexer
);
10561 elt
= make_pack_expansion (elt
);
10563 if (elt
== error_mark_node
)
10564 return error_mark_node
;
10565 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10569 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10570 parens
.require_close (parser
);
10572 /* Construct a location of the form:
10573 __is_trivially_copyable(_Tp)
10574 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10575 with start == caret, finishing at the close-paren. */
10576 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10578 /* Complete the trait expression, which may mean either processing
10579 the trait expr now or saving it for template instantiation. */
10582 case CPTK_UNDERLYING_TYPE
:
10583 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10585 return cp_expr (finish_bases (type1
, false), trait_loc
);
10586 case CPTK_DIRECT_BASES
:
10587 return cp_expr (finish_bases (type1
, true), trait_loc
);
10589 return finish_trait_expr (trait_loc
, kind
, type1
, type2
);
10593 /* Parse a lambda expression.
10596 lambda-introducer lambda-declarator [opt] compound-statement
10598 Returns a representation of the expression. */
10601 cp_parser_lambda_expression (cp_parser
* parser
)
10603 tree lambda_expr
= build_lambda_expr ();
10606 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10607 cp_token_position start
= 0;
10609 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10611 if (cxx_dialect
>= cxx20
)
10612 /* C++20 allows lambdas in unevaluated context. */;
10613 else if (cp_unevaluated_operand
)
10615 if (!token
->error_reported
)
10617 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10618 "lambda-expression in unevaluated context"
10619 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10620 token
->error_reported
= true;
10624 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10626 if (!token
->error_reported
)
10628 error_at (token
->location
, "lambda-expression in template-argument"
10629 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10630 token
->error_reported
= true;
10635 /* We may be in the middle of deferred access check. Disable
10637 push_deferring_access_checks (dk_no_deferred
);
10639 cp_parser_lambda_introducer (parser
, lambda_expr
);
10640 if (cp_parser_error_occurred (parser
))
10641 return error_mark_node
;
10643 type
= begin_lambda_type (lambda_expr
);
10644 if (type
== error_mark_node
)
10645 return error_mark_node
;
10647 record_lambda_scope (lambda_expr
);
10649 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10650 determine_visibility (TYPE_NAME (type
));
10652 /* Now that we've started the type, add the capture fields for any
10653 explicit captures. */
10654 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10657 /* Inside the class, surrounding template-parameter-lists do not apply. */
10658 unsigned int saved_num_template_parameter_lists
10659 = parser
->num_template_parameter_lists
;
10660 unsigned char in_statement
= parser
->in_statement
;
10661 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10662 bool fully_implicit_function_template_p
10663 = parser
->fully_implicit_function_template_p
;
10664 tree implicit_template_parms
= parser
->implicit_template_parms
;
10665 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10666 bool auto_is_implicit_function_template_parm_p
10667 = parser
->auto_is_implicit_function_template_parm_p
;
10669 parser
->num_template_parameter_lists
= 0;
10670 parser
->in_statement
= 0;
10671 parser
->in_switch_statement_p
= false;
10672 parser
->fully_implicit_function_template_p
= false;
10673 parser
->implicit_template_parms
= 0;
10674 parser
->implicit_template_scope
= 0;
10675 parser
->auto_is_implicit_function_template_parm_p
= false;
10677 /* The body of a lambda in a discarded statement is not discarded. */
10678 bool discarded
= in_discarded_stmt
;
10679 in_discarded_stmt
= 0;
10681 /* By virtue of defining a local class, a lambda expression has access to
10682 the private variables of enclosing classes. */
10684 if (cp_parser_start_tentative_firewall (parser
))
10687 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10689 if (ok
&& cp_parser_error_occurred (parser
))
10694 cp_parser_lambda_body (parser
, lambda_expr
);
10696 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10698 if (cp_parser_skip_to_closing_brace (parser
))
10699 cp_lexer_consume_token (parser
->lexer
);
10702 /* The capture list was built up in reverse order; fix that now. */
10703 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10704 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10707 maybe_add_lambda_conv_op (type
);
10709 finish_struct (type
, /*attributes=*/NULL_TREE
);
10711 in_discarded_stmt
= discarded
;
10713 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10714 parser
->in_statement
= in_statement
;
10715 parser
->in_switch_statement_p
= in_switch_statement_p
;
10716 parser
->fully_implicit_function_template_p
10717 = fully_implicit_function_template_p
;
10718 parser
->implicit_template_parms
= implicit_template_parms
;
10719 parser
->implicit_template_scope
= implicit_template_scope
;
10720 parser
->auto_is_implicit_function_template_parm_p
10721 = auto_is_implicit_function_template_parm_p
;
10724 /* This field is only used during parsing of the lambda. */
10725 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10727 /* This lambda shouldn't have any proxies left at this point. */
10728 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10729 /* And now that we're done, push proxies for an enclosing lambda. */
10730 insert_pending_capture_proxies ();
10732 /* Update the lambda expression to a range. */
10733 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10738 lambda_expr
= build_lambda_object (lambda_expr
);
10740 lambda_expr
= error_mark_node
;
10742 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10744 pop_deferring_access_checks ();
10746 return lambda_expr
;
10749 /* Parse the beginning of a lambda expression.
10752 [ lambda-capture [opt] ]
10754 LAMBDA_EXPR is the current representation of the lambda expression. */
10757 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10759 /* Need commas after the first capture. */
10762 /* Eat the leading `['. */
10763 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10765 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10766 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10767 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
)
10768 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)
10769 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10770 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10771 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10772 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10774 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10776 cp_lexer_consume_token (parser
->lexer
);
10779 if (!(at_function_scope_p () || parsing_nsdmi ()))
10780 error ("non-local lambda expression cannot have a capture-default");
10783 hash_set
<tree
, true> ids
;
10784 tree first_capture_id
= NULL_TREE
;
10785 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10787 cp_token
* capture_token
;
10789 tree capture_init_expr
;
10790 cp_id_kind idk
= CP_ID_KIND_NONE
;
10791 bool explicit_init_p
= false;
10793 enum capture_kind_type
10798 enum capture_kind_type capture_kind
= BY_COPY
;
10800 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10802 error ("expected end of capture-list");
10809 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10811 /* Possibly capture `this'. */
10812 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10814 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10815 if (cxx_dialect
< cxx20
10816 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10817 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10818 "with by-copy capture default");
10819 cp_lexer_consume_token (parser
->lexer
);
10820 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10821 pedwarn (input_location
, 0,
10822 "already captured %qD in lambda expression",
10825 add_capture (lambda_expr
, /*id=*/this_identifier
,
10826 /*initializer=*/finish_this_expr (),
10827 /*by_reference_p=*/true, explicit_init_p
);
10831 /* Possibly capture `*this'. */
10832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10833 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10835 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10836 if (cxx_dialect
< cxx17
)
10837 pedwarn (loc
, 0, "%<*this%> capture only available with "
10838 "%<-std=c++17%> or %<-std=gnu++17%>");
10839 cp_lexer_consume_token (parser
->lexer
);
10840 cp_lexer_consume_token (parser
->lexer
);
10841 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10842 pedwarn (input_location
, 0,
10843 "already captured %qD in lambda expression",
10846 add_capture (lambda_expr
, /*id=*/this_identifier
,
10847 /*initializer=*/finish_this_expr (),
10848 /*by_reference_p=*/false, explicit_init_p
);
10852 /* But reject `&this'. */
10853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10854 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10856 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10857 "%<this%> cannot be captured by reference");
10858 cp_lexer_consume_token (parser
->lexer
);
10859 cp_lexer_consume_token (parser
->lexer
);
10863 /* Remember whether we want to capture as a reference or not. */
10864 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10866 capture_kind
= BY_REFERENCE
;
10867 cp_lexer_consume_token (parser
->lexer
);
10870 bool init_pack_expansion
= false;
10871 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
10872 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10874 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10875 if (cxx_dialect
< cxx20
)
10876 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
10877 "%<-std=c++20%> or %<-std=gnu++20%>");
10878 cp_lexer_consume_token (parser
->lexer
);
10879 init_pack_expansion
= true;
10882 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
10883 if (init_pack_expansion
&& capture_kind
!= BY_REFERENCE
10884 && cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10886 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
10887 0, "%<&%> should come before %<...%>");
10888 capture_kind
= BY_REFERENCE
;
10889 cp_lexer_consume_token (parser
->lexer
);
10892 /* Get the identifier. */
10893 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10894 capture_id
= cp_parser_identifier (parser
);
10896 if (capture_id
== error_mark_node
)
10897 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10898 delimiters, but I modified this to stop on unnested ']' as well. It
10899 was already changed to stop on unnested '}', so the
10900 "closing_parenthesis" name is no more misleading with my change. */
10902 cp_parser_skip_to_closing_parenthesis (parser
,
10903 /*recovering=*/true,
10905 /*consume_paren=*/true);
10909 /* Find the initializer for this capture. */
10910 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10911 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10912 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10914 bool direct
, non_constant
;
10915 /* An explicit initializer exists. */
10916 if (cxx_dialect
< cxx14
)
10917 pedwarn (input_location
, 0,
10918 "lambda capture initializers "
10919 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10920 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10921 &non_constant
, true);
10922 explicit_init_p
= true;
10923 if (capture_init_expr
== NULL_TREE
)
10925 error ("empty initializer for lambda init-capture");
10926 capture_init_expr
= error_mark_node
;
10928 if (init_pack_expansion
)
10929 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10933 const char* error_msg
;
10935 /* Turn the identifier into an id-expression. */
10937 = cp_parser_lookup_name_simple (parser
, capture_id
,
10938 capture_token
->location
);
10940 if (capture_init_expr
== error_mark_node
)
10942 unqualified_name_lookup_error (capture_id
);
10945 else if (!VAR_P (capture_init_expr
)
10946 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10948 error_at (capture_token
->location
,
10949 "capture of non-variable %qE",
10950 capture_init_expr
);
10951 if (DECL_P (capture_init_expr
))
10952 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10953 "%q#D declared here", capture_init_expr
);
10956 if (VAR_P (capture_init_expr
)
10957 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10959 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10960 "%qD with non-automatic storage duration",
10961 capture_init_expr
))
10962 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10963 "%q#D declared here", capture_init_expr
);
10968 = finish_id_expression
10973 /*integral_constant_expression_p=*/false,
10974 /*allow_non_integral_constant_expression_p=*/false,
10975 /*non_integral_constant_expression_p=*/NULL
,
10976 /*template_p=*/false,
10978 /*address_p=*/false,
10979 /*template_arg_p=*/false,
10981 capture_token
->location
);
10983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10985 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10986 cp_lexer_consume_token (parser
->lexer
);
10987 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10988 if (init_pack_expansion
)
10990 /* If what follows is an initializer, the second '...' is
10991 invalid. But for cases like [...xs...], the first one
10993 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10994 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10995 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10996 ellipsis_loc
= loc
;
10997 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
11003 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
11004 && !explicit_init_p
)
11006 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
11007 && capture_kind
== BY_COPY
)
11008 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
11009 "of %qD redundant with by-copy capture default",
11011 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
11012 && capture_kind
== BY_REFERENCE
)
11013 pedwarn (capture_token
->location
, 0, "explicit by-reference "
11014 "capture of %qD redundant with by-reference capture "
11015 "default", capture_id
);
11018 /* Check for duplicates.
11019 Optimize for the zero or one explicit captures cases and only create
11020 the hash_set after adding second capture. */
11021 bool found
= false;
11022 if (!ids
.is_empty ())
11023 found
= ids
.add (capture_id
);
11024 else if (first_capture_id
== NULL_TREE
)
11025 first_capture_id
= capture_id
;
11026 else if (capture_id
== first_capture_id
)
11030 ids
.add (first_capture_id
);
11031 ids
.add (capture_id
);
11034 pedwarn (input_location
, 0,
11035 "already captured %qD in lambda expression", capture_id
);
11037 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
11038 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
11041 /* If there is any qualification still in effect, clear it
11042 now; we will be starting fresh with the next capture. */
11043 parser
->scope
= NULL_TREE
;
11044 parser
->qualifying_scope
= NULL_TREE
;
11045 parser
->object_scope
= NULL_TREE
;
11048 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11051 /* Parse the (optional) middle of a lambda expression.
11054 < template-parameter-list [opt] >
11055 requires-clause [opt]
11056 ( parameter-declaration-clause [opt] )
11057 attribute-specifier [opt]
11058 decl-specifier-seq [opt]
11059 exception-specification [opt]
11060 lambda-return-type-clause [opt]
11061 requires-clause [opt]
11063 LAMBDA_EXPR is the current representation of the lambda expression. */
11066 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
11068 /* 5.1.1.4 of the standard says:
11069 If a lambda-expression does not include a lambda-declarator, it is as if
11070 the lambda-declarator were ().
11071 This means an empty parameter list, no attributes, and no exception
11073 tree param_list
= void_list_node
;
11074 tree std_attrs
= NULL_TREE
;
11075 tree gnu_attrs
= NULL_TREE
;
11076 tree exception_spec
= NULL_TREE
;
11077 tree template_param_list
= NULL_TREE
;
11078 tree tx_qual
= NULL_TREE
;
11079 tree return_type
= NULL_TREE
;
11080 tree trailing_requires_clause
= NULL_TREE
;
11081 cp_decl_specifier_seq lambda_specs
;
11082 clear_decl_specs (&lambda_specs
);
11083 /* A lambda op() is const unless explicitly 'mutable'. */
11084 cp_cv_quals quals
= TYPE_QUAL_CONST
;
11086 /* The template-parameter-list is optional, but must begin with
11087 an opening angle if present. */
11088 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
11090 if (cxx_dialect
< cxx14
)
11091 pedwarn (parser
->lexer
->next_token
->location
, 0,
11092 "lambda templates are only available with "
11093 "%<-std=c++14%> or %<-std=gnu++14%>");
11094 else if (cxx_dialect
< cxx20
)
11095 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
11096 "lambda templates are only available with "
11097 "%<-std=c++20%> or %<-std=gnu++20%>");
11099 cp_lexer_consume_token (parser
->lexer
);
11101 template_param_list
= cp_parser_template_parameter_list (parser
);
11102 cp_parser_skip_to_end_of_template_parameter_list (parser
);
11104 /* We may have a constrained generic lambda; parse the requires-clause
11105 immediately after the template-parameter-list and combine with any
11106 shorthand constraints present. */
11107 tree dreqs
= cp_parser_requires_clause_opt (parser
, true);
11110 tree reqs
= get_shorthand_constraints (current_template_parms
);
11112 reqs
= combine_constraint_expressions (reqs
, dreqs
);
11113 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
11116 /* We just processed one more parameter list. */
11117 ++parser
->num_template_parameter_lists
;
11120 /* Committee discussion supports allowing attributes here. */
11121 lambda_specs
.attributes
= cp_parser_attributes_opt (parser
);
11123 /* The parameter-declaration-clause is optional (unless
11124 template-parameter-list was given), but must begin with an
11125 opening parenthesis if present. */
11126 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11128 bool is_consteval
= false;
11129 /* For C++20, before parsing the parameter list check if there is
11130 a consteval specifier in the corresponding decl-specifier-seq. */
11131 if (cxx_dialect
>= cxx20
)
11133 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
11134 cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_KEYWORD
); n
++)
11136 if (cp_lexer_peek_nth_token (parser
->lexer
, n
)->keyword
11139 is_consteval
= true;
11145 matching_parens parens
;
11146 parens
.consume_open (parser
);
11148 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
11151 current_binding_level
->immediate_fn_ctx_p
= true;
11153 /* Parse parameters. */
11155 = cp_parser_parameter_declaration_clause
11156 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
11158 /* Default arguments shall not be specified in the
11159 parameter-declaration-clause of a lambda-declarator. */
11160 if (cxx_dialect
< cxx14
)
11161 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
11162 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
11163 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
11164 "default argument specified for lambda parameter");
11166 parens
.require_close (parser
);
11168 /* In the decl-specifier-seq of the lambda-declarator, each
11169 decl-specifier shall either be mutable or constexpr. */
11170 int declares_class_or_enum
;
11171 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
11172 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
11173 cp_parser_decl_specifier_seq (parser
,
11174 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
11175 &lambda_specs
, &declares_class_or_enum
);
11176 if (lambda_specs
.storage_class
== sc_mutable
)
11178 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
11179 quals
= TYPE_UNQUALIFIED
;
11180 if (lambda_specs
.conflicting_specifiers_p
)
11181 error_at (lambda_specs
.locations
[ds_storage_class
],
11182 "duplicate %<mutable%>");
11185 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
11187 /* Parse optional exception specification. */
11189 = cp_parser_exception_specification_opt (parser
, CP_PARSER_FLAGS_NONE
);
11191 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11193 /* Parse optional trailing return type. */
11194 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
11196 cp_lexer_consume_token (parser
->lexer
);
11197 return_type
= cp_parser_trailing_type_id (parser
);
11200 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11201 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
11203 /* Parse optional trailing requires clause. */
11204 trailing_requires_clause
= cp_parser_requires_clause_opt (parser
, false);
11206 /* The function parameters must be in scope all the way until after the
11207 trailing-return-type in case of decltype. */
11208 pop_bindings_and_leave_scope ();
11210 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
11211 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11213 /* Create the function call operator.
11215 Messing with declarators like this is no uglier than building up the
11216 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11219 cp_decl_specifier_seq return_type_specs
;
11220 cp_declarator
* declarator
;
11224 clear_decl_specs (&return_type_specs
);
11225 return_type_specs
.type
= make_auto ();
11227 if (lambda_specs
.locations
[ds_constexpr
])
11229 if (cxx_dialect
>= cxx17
)
11230 return_type_specs
.locations
[ds_constexpr
]
11231 = lambda_specs
.locations
[ds_constexpr
];
11233 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
11234 "lambda only available with %<-std=c++17%> or "
11235 "%<-std=gnu++17%>");
11237 if (lambda_specs
.locations
[ds_consteval
])
11238 return_type_specs
.locations
[ds_consteval
]
11239 = lambda_specs
.locations
[ds_consteval
];
11241 p
= obstack_alloc (&declarator_obstack
, 0);
11243 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
11244 LAMBDA_EXPR_LOCATION (lambda_expr
));
11246 declarator
= make_call_declarator (declarator
, param_list
, quals
,
11247 VIRT_SPEC_UNSPECIFIED
,
11252 trailing_requires_clause
,
11254 declarator
->std_attributes
= std_attrs
;
11256 fco
= grokmethod (&return_type_specs
,
11258 chainon (gnu_attrs
, lambda_specs
.attributes
));
11259 if (fco
!= error_mark_node
)
11261 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
11262 DECL_ARTIFICIAL (fco
) = 1;
11263 /* Give the object parameter a different name. */
11264 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
11265 DECL_SET_LAMBDA_FUNCTION (fco
, true);
11267 if (template_param_list
)
11269 fco
= finish_member_template_decl (fco
);
11270 finish_template_decl (template_param_list
);
11271 --parser
->num_template_parameter_lists
;
11273 else if (parser
->fully_implicit_function_template_p
)
11274 fco
= finish_fully_implicit_template (parser
, fco
);
11276 finish_member_declaration (fco
);
11278 obstack_free (&declarator_obstack
, p
);
11280 return (fco
!= error_mark_node
);
11284 /* Parse the body of a lambda expression, which is simply
11288 but which requires special handling.
11289 LAMBDA_EXPR is the current representation of the lambda expression. */
11292 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
11294 bool nested
= (current_function_decl
!= NULL_TREE
);
11295 unsigned char local_variables_forbidden_p
11296 = parser
->local_variables_forbidden_p
;
11297 bool in_function_body
= parser
->in_function_body
;
11299 /* The body of a lambda-expression is not a subexpression of the enclosing
11304 push_function_context ();
11306 /* Still increment function_depth so that we don't GC in the
11307 middle of an expression. */
11310 vec
<tree
> omp_privatization_save
;
11311 save_omp_privatization_clauses (omp_privatization_save
);
11312 /* Clear this in case we're in the middle of a default argument. */
11313 parser
->local_variables_forbidden_p
= 0;
11314 parser
->in_function_body
= true;
11317 local_specialization_stack
s (lss_copy
);
11318 tree fco
= lambda_function (lambda_expr
);
11319 tree body
= start_lambda_function (fco
, lambda_expr
);
11321 /* Originally C++11 required us to peek for 'return expr'; and
11322 process it specially here to deduce the return type. N3638
11323 removed the need for that. */
11324 cp_parser_function_body (parser
, false);
11326 finish_lambda_function (body
);
11329 restore_omp_privatization_clauses (omp_privatization_save
);
11330 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11331 parser
->in_function_body
= in_function_body
;
11333 pop_function_context();
11338 /* Statements [gram.stmt.stmt] */
11340 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11343 add_debug_begin_stmt (location_t loc
)
11345 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11347 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11348 /* A concept is never expanded normally. */
11351 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11352 SET_EXPR_LOCATION (stmt
, loc
);
11356 /* Parse a statement.
11360 expression-statement
11362 selection-statement
11363 iteration-statement
11365 declaration-statement
11372 attribute-specifier-seq (opt) expression-statement
11373 attribute-specifier-seq (opt) compound-statement
11374 attribute-specifier-seq (opt) selection-statement
11375 attribute-specifier-seq (opt) iteration-statement
11376 attribute-specifier-seq (opt) jump-statement
11377 declaration-statement
11378 attribute-specifier-seq (opt) try-block
11381 expression-statement
11389 IN_COMPOUND is true when the statement is nested inside a
11390 cp_parser_compound_statement; this matters for certain pragmas.
11392 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11393 is a (possibly labeled) if statement which is not enclosed in braces
11394 and has an else clause. This is used to implement -Wparentheses.
11396 CHAIN is a vector of if-else-if conditions. */
11399 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11400 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11401 location_t
*loc_after_labels
)
11403 tree statement
, std_attrs
= NULL_TREE
;
11405 location_t statement_location
, attrs_loc
;
11410 /* There is no statement yet. */
11411 statement
= NULL_TREE
;
11413 saved_token_sentinel
saved_tokens (parser
->lexer
);
11414 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11415 if (c_dialect_objc ())
11416 /* In obj-c++, seeing '[[' might be the either the beginning of
11417 c++11 attributes, or a nested objc-message-expression. So
11418 let's parse the c++11 attributes tentatively. */
11419 cp_parser_parse_tentatively (parser
);
11420 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11422 attrs_loc
= make_location (attrs_loc
, attrs_loc
, parser
->lexer
);
11423 if (c_dialect_objc ())
11425 if (!cp_parser_parse_definitely (parser
))
11426 std_attrs
= NULL_TREE
;
11429 /* Peek at the next token. */
11430 token
= cp_lexer_peek_token (parser
->lexer
);
11431 /* Remember the location of the first token in the statement. */
11432 cp_token
*statement_token
= token
;
11433 statement_location
= token
->location
;
11434 add_debug_begin_stmt (statement_location
);
11435 /* If this is a keyword, then that will often determine what kind of
11436 statement we have. */
11437 if (token
->type
== CPP_KEYWORD
)
11439 enum rid keyword
= token
->keyword
;
11445 /* Looks like a labeled-statement with a case label.
11446 Parse the label, and then use tail recursion to parse
11448 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11449 in_compound
= false;
11454 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11455 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11461 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11462 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11468 case RID_CO_RETURN
:
11470 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11471 statement
= cp_parser_jump_statement (parser
);
11474 /* Objective-C++ exception-handling constructs. */
11477 case RID_AT_FINALLY
:
11478 case RID_AT_SYNCHRONIZED
:
11480 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11481 statement
= cp_parser_objc_statement (parser
);
11485 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11486 statement
= cp_parser_try_block (parser
);
11489 case RID_NAMESPACE
:
11490 /* This must be a namespace alias definition. */
11491 if (std_attrs
!= NULL_TREE
)
11493 /* Attributes should be parsed as part of the
11494 declaration, so let's un-parse them. */
11495 saved_tokens
.rollback();
11496 std_attrs
= NULL_TREE
;
11498 cp_parser_declaration_statement (parser
);
11501 case RID_TRANSACTION_ATOMIC
:
11502 case RID_TRANSACTION_RELAXED
:
11503 case RID_SYNCHRONIZED
:
11504 case RID_ATOMIC_NOEXCEPT
:
11505 case RID_ATOMIC_CANCEL
:
11506 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11507 statement
= cp_parser_transaction (parser
, token
);
11509 case RID_TRANSACTION_CANCEL
:
11510 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11511 statement
= cp_parser_transaction_cancel (parser
);
11515 /* It might be a keyword like `int' that can start a
11516 declaration-statement. */
11520 else if (token
->type
== CPP_NAME
)
11522 /* If the next token is a `:', then we are looking at a
11523 labeled-statement. */
11524 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11525 if (token
->type
== CPP_COLON
)
11527 /* Looks like a labeled-statement with an ordinary label.
11528 Parse the label, and then use tail recursion to parse
11531 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11532 in_compound
= false;
11536 /* Anything that starts with a `{' must be a compound-statement. */
11537 else if (token
->type
== CPP_OPEN_BRACE
)
11539 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11540 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11542 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11543 a statement all its own. */
11544 else if (token
->type
== CPP_PRAGMA
)
11546 /* Only certain OpenMP pragmas are attached to statements, and thus
11547 are considered statements themselves. All others are not. In
11548 the context of a compound, accept the pragma as a "statement" and
11549 return so that we can check for a close brace. Otherwise we
11550 require a real statement and must go back and read one. */
11552 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11553 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11557 else if (token
->type
== CPP_EOF
)
11559 cp_parser_error (parser
, "expected statement");
11563 /* Everything else must be a declaration-statement or an
11564 expression-statement. Try for the declaration-statement
11565 first, unless we are looking at a `;', in which case we know that
11566 we have an expression-statement. */
11569 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11571 if (std_attrs
!= NULL_TREE
)
11572 /* Attributes should be parsed as part of the declaration,
11573 so let's un-parse them. */
11574 saved_tokens
.rollback();
11576 cp_parser_parse_tentatively (parser
);
11577 /* Try to parse the declaration-statement. */
11578 cp_parser_declaration_statement (parser
);
11579 /* If that worked, we're done. */
11580 if (cp_parser_parse_definitely (parser
))
11582 /* It didn't work, restore the post-attribute position. */
11584 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11586 /* All preceding labels have been parsed at this point. */
11587 if (loc_after_labels
!= NULL
)
11588 *loc_after_labels
= statement_location
;
11590 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11592 /* Look for an expression-statement instead. */
11593 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11595 /* Handle [[fallthrough]];. */
11596 if (attribute_fallthrough_p (std_attrs
))
11598 /* The next token after the fallthrough attribute is ';'. */
11599 if (statement
== NULL_TREE
)
11601 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11602 statement
= build_call_expr_internal_loc (statement_location
,
11604 void_type_node
, 0);
11605 finish_expr_stmt (statement
);
11608 warning_at (statement_location
, OPT_Wattributes
,
11609 "%<fallthrough%> attribute not followed by %<;%>");
11610 std_attrs
= NULL_TREE
;
11614 /* Set the line number for the statement. */
11615 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11616 SET_EXPR_LOCATION (statement
, statement_location
);
11618 /* Allow "[[fallthrough]];", but warn otherwise. */
11619 if (std_attrs
!= NULL_TREE
)
11620 warning_at (attrs_loc
,
11622 "attributes at the beginning of statement are ignored");
11625 /* Append ATTR to attribute list ATTRS. */
11628 attr_chainon (tree attrs
, tree attr
)
11630 if (attrs
== error_mark_node
)
11631 return error_mark_node
;
11632 if (attr
== error_mark_node
)
11633 return error_mark_node
;
11634 return chainon (attrs
, attr
);
11637 /* Parse the label for a labeled-statement, i.e.
11640 case constant-expression :
11644 case constant-expression ... constant-expression : statement
11646 When a label is parsed without errors, the label is added to the
11647 parse tree by the finish_* functions, so this function doesn't
11648 have to return the label. */
11651 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11654 tree label
= NULL_TREE
;
11655 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11657 /* The next token should be an identifier. */
11658 token
= cp_lexer_peek_token (parser
->lexer
);
11659 if (token
->type
!= CPP_NAME
11660 && token
->type
!= CPP_KEYWORD
)
11662 cp_parser_error (parser
, "expected labeled-statement");
11666 /* Remember whether this case or a user-defined label is allowed to fall
11668 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11670 parser
->colon_corrects_to_scope_p
= false;
11671 switch (token
->keyword
)
11675 tree expr
, expr_hi
;
11676 cp_token
*ellipsis
;
11678 /* Consume the `case' token. */
11679 cp_lexer_consume_token (parser
->lexer
);
11680 /* Parse the constant-expression. */
11681 expr
= cp_parser_constant_expression (parser
);
11682 if (check_for_bare_parameter_packs (expr
))
11683 expr
= error_mark_node
;
11685 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11686 if (ellipsis
->type
== CPP_ELLIPSIS
)
11688 /* Consume the `...' token. */
11689 cp_lexer_consume_token (parser
->lexer
);
11690 expr_hi
= cp_parser_constant_expression (parser
);
11691 if (check_for_bare_parameter_packs (expr_hi
))
11692 expr_hi
= error_mark_node
;
11694 /* We don't need to emit warnings here, as the common code
11695 will do this for us. */
11698 expr_hi
= NULL_TREE
;
11700 if (parser
->in_switch_statement_p
)
11702 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11703 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11705 label
= CASE_LABEL (l
);
11706 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11710 error_at (token
->location
,
11711 "case label %qE not within a switch statement",
11717 /* Consume the `default' token. */
11718 cp_lexer_consume_token (parser
->lexer
);
11720 if (parser
->in_switch_statement_p
)
11722 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11723 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11725 label
= CASE_LABEL (l
);
11726 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11730 error_at (token
->location
, "case label not within a switch statement");
11734 /* Anything else must be an ordinary label. */
11735 label
= finish_label_stmt (cp_parser_identifier (parser
));
11736 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11737 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11741 /* Require the `:' token. */
11742 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11744 /* An ordinary label may optionally be followed by attributes.
11745 However, this is only permitted if the attributes are then
11746 followed by a semicolon. This is because, for backward
11747 compatibility, when parsing
11748 lab: __attribute__ ((unused)) int i;
11749 we want the attribute to attach to "i", not "lab". */
11750 if (label
!= NULL_TREE
11751 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11754 cp_parser_parse_tentatively (parser
);
11755 attrs
= cp_parser_gnu_attributes_opt (parser
);
11756 if (attrs
== NULL_TREE
11757 /* And fallthrough always binds to the expression-statement. */
11758 || attribute_fallthrough_p (attrs
)
11759 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11760 cp_parser_abort_tentative_parse (parser
);
11761 else if (!cp_parser_parse_definitely (parser
))
11764 attributes
= attr_chainon (attributes
, attrs
);
11767 if (attributes
!= NULL_TREE
)
11768 cplus_decl_attributes (&label
, attributes
, 0);
11770 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11773 /* Parse an expression-statement.
11775 expression-statement:
11778 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11779 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11780 indicates whether this expression-statement is part of an
11781 expression statement. */
11784 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11786 tree statement
= NULL_TREE
;
11787 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11788 location_t loc
= token
->location
;
11790 /* There might be attribute fallthrough. */
11791 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11793 /* If the next token is a ';', then there is no expression
11795 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11797 statement
= cp_parser_expression (parser
);
11798 if (statement
== error_mark_node
11799 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11801 cp_parser_skip_to_end_of_block_or_statement (parser
);
11802 return error_mark_node
;
11806 /* Handle [[fallthrough]];. */
11807 if (attribute_fallthrough_p (attr
))
11809 /* The next token after the fallthrough attribute is ';'. */
11810 if (statement
== NULL_TREE
)
11811 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11812 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11813 void_type_node
, 0);
11815 warning_at (loc
, OPT_Wattributes
,
11816 "%<fallthrough%> attribute not followed by %<;%>");
11820 /* Allow "[[fallthrough]];", but warn otherwise. */
11821 if (attr
!= NULL_TREE
)
11822 warning_at (loc
, OPT_Wattributes
,
11823 "attributes at the beginning of statement are ignored");
11825 /* Give a helpful message for "A<T>::type t;" and the like. */
11826 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11827 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11829 if (TREE_CODE (statement
) == SCOPE_REF
)
11830 error_at (token
->location
, "need %<typename%> before %qE because "
11831 "%qT is a dependent scope",
11832 statement
, TREE_OPERAND (statement
, 0));
11833 else if (is_overloaded_fn (statement
)
11834 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11837 tree fn
= get_first_fn (statement
);
11838 error_at (token
->location
,
11839 "%<%T::%D%> names the constructor, not the type",
11840 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11844 /* Consume the final `;'. */
11845 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11847 if (in_statement_expr
11848 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11849 /* This is the final expression statement of a statement
11851 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11852 else if (statement
)
11853 statement
= finish_expr_stmt (statement
);
11858 /* Parse a compound-statement.
11860 compound-statement:
11861 { statement-seq [opt] }
11865 compound-statement:
11866 { label-declaration-seq [opt] statement-seq [opt] }
11868 label-declaration-seq:
11870 label-declaration-seq label-declaration
11872 Returns a tree representing the statement. */
11875 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11876 int bcs_flags
, bool function_body
)
11878 tree compound_stmt
;
11879 matching_braces braces
;
11881 /* Consume the `{'. */
11882 if (!braces
.require_open (parser
))
11883 return error_mark_node
;
11884 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11885 && !function_body
&& cxx_dialect
< cxx14
)
11886 pedwarn (input_location
, OPT_Wpedantic
,
11887 "compound-statement in %<constexpr%> function");
11888 /* Begin the compound-statement. */
11889 compound_stmt
= begin_compound_stmt (bcs_flags
);
11890 /* If the next keyword is `__label__' we have a label declaration. */
11891 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11892 cp_parser_label_declaration (parser
);
11893 /* Parse an (optional) statement-seq. */
11894 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11897 maybe_splice_retval_cleanup (compound_stmt
);
11899 /* Finish the compound-statement. */
11900 finish_compound_stmt (compound_stmt
);
11901 /* Consume the `}'. */
11902 braces
.require_close (parser
);
11904 return compound_stmt
;
11907 /* Parse an (optional) statement-seq.
11911 statement-seq [opt] statement */
11914 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11916 /* Scan statements until there aren't any more. */
11919 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11921 /* If we are looking at a `}', then we have run out of
11922 statements; the same is true if we have reached the end
11923 of file, or have stumbled upon a stray '@end'. */
11924 if (token
->type
== CPP_CLOSE_BRACE
11925 || token
->type
== CPP_EOF
11926 || token
->type
== CPP_PRAGMA_EOL
11927 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11930 /* If we are in a compound statement and find 'else' then
11931 something went wrong. */
11932 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11934 if (parser
->in_statement
& IN_IF_STMT
)
11938 token
= cp_lexer_consume_token (parser
->lexer
);
11939 error_at (token
->location
, "%<else%> without a previous %<if%>");
11943 /* Parse the statement. */
11944 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11948 /* Return true if this is the C++20 version of range-based-for with
11952 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11956 /* Save tokens so that we can put them back. */
11957 cp_lexer_save_tokens (parser
->lexer
);
11959 /* There has to be an unnested ; followed by an unnested :. */
11960 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11961 /*recovering=*/false,
11963 /*consume_paren=*/false) != -1)
11966 /* We found the semicolon, eat it now. */
11967 cp_lexer_consume_token (parser
->lexer
);
11969 /* Now look for ':' that is not nested in () or {}. */
11970 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11971 /*recovering=*/false,
11973 /*consume_paren=*/false) == -1);
11976 /* Roll back the tokens we skipped. */
11977 cp_lexer_rollback_tokens (parser
->lexer
);
11982 /* Return true if we're looking at (init; cond), false otherwise. */
11985 cp_parser_init_statement_p (cp_parser
*parser
)
11987 /* Save tokens so that we can put them back. */
11988 cp_lexer_save_tokens (parser
->lexer
);
11990 /* Look for ';' that is not nested in () or {}. */
11991 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11992 /*recovering=*/false,
11994 /*consume_paren=*/false);
11996 /* Roll back the tokens we skipped. */
11997 cp_lexer_rollback_tokens (parser
->lexer
);
12002 /* Parse a selection-statement.
12004 selection-statement:
12005 if ( init-statement [opt] condition ) statement
12006 if ( init-statement [opt] condition ) statement else statement
12007 switch ( init-statement [opt] condition ) statement
12009 Returns the new IF_STMT or SWITCH_STMT.
12011 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12012 is a (possibly labeled) if statement which is not enclosed in
12013 braces and has an else clause. This is used to implement
12016 CHAIN is a vector of if-else-if conditions. This is used to implement
12017 -Wduplicated-cond. */
12020 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
12025 token_indent_info guard_tinfo
;
12030 /* Peek at the next token. */
12031 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
12032 guard_tinfo
= get_token_indent_info (token
);
12034 /* See what kind of keyword it is. */
12035 keyword
= token
->keyword
;
12045 if (keyword
== RID_IF
12046 && cp_lexer_next_token_is_keyword (parser
->lexer
,
12050 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
12051 if (cxx_dialect
< cxx17
)
12052 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
12053 "with %<-std=c++17%> or %<-std=gnu++17%>");
12056 /* Look for the `('. */
12057 matching_parens parens
;
12058 if (!parens
.require_open (parser
))
12060 cp_parser_skip_to_end_of_statement (parser
);
12061 return error_mark_node
;
12064 /* Begin the selection-statement. */
12065 if (keyword
== RID_IF
)
12067 statement
= begin_if_stmt ();
12068 IF_STMT_CONSTEXPR_P (statement
) = cx
;
12071 statement
= begin_switch_stmt ();
12073 /* Parse the optional init-statement. */
12074 if (cp_parser_init_statement_p (parser
))
12077 if (cxx_dialect
< cxx17
)
12078 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12079 "init-statement in selection statements only available "
12080 "with %<-std=c++17%> or %<-std=gnu++17%>");
12081 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12083 /* A non-empty init-statement can have arbitrary side
12088 cp_parser_init_statement (parser
, &decl
);
12091 /* Parse the condition. */
12092 condition
= cp_parser_condition (parser
);
12093 /* Look for the `)'. */
12094 if (!parens
.require_close (parser
))
12095 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12096 /*consume_paren=*/true);
12098 if (keyword
== RID_IF
)
12101 unsigned char in_statement
;
12103 /* Add the condition. */
12104 condition
= finish_if_stmt_cond (condition
, statement
);
12106 if (warn_duplicated_cond
)
12107 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
12110 /* Parse the then-clause. */
12111 in_statement
= parser
->in_statement
;
12112 parser
->in_statement
|= IN_IF_STMT
;
12114 /* Outside a template, the non-selected branch of a constexpr
12115 if is a 'discarded statement', i.e. unevaluated. */
12116 bool was_discarded
= in_discarded_stmt
;
12117 bool discard_then
= (cx
&& !processing_template_decl
12118 && integer_zerop (condition
));
12121 in_discarded_stmt
= true;
12122 ++c_inhibit_evaluation_warnings
;
12125 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
12128 parser
->in_statement
= in_statement
;
12130 finish_then_clause (statement
);
12134 THEN_CLAUSE (statement
) = NULL_TREE
;
12135 in_discarded_stmt
= was_discarded
;
12136 --c_inhibit_evaluation_warnings
;
12139 /* If the next token is `else', parse the else-clause. */
12140 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12143 bool discard_else
= (cx
&& !processing_template_decl
12144 && integer_nonzerop (condition
));
12147 in_discarded_stmt
= true;
12148 ++c_inhibit_evaluation_warnings
;
12152 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12153 /* Consume the `else' keyword. */
12154 cp_lexer_consume_token (parser
->lexer
);
12155 if (warn_duplicated_cond
)
12157 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12161 /* We've got "if (COND) else if (COND2)". Start
12162 the condition chain and add COND as the first
12164 chain
= new vec
<tree
> ();
12165 if (!CONSTANT_CLASS_P (condition
)
12166 && !TREE_SIDE_EFFECTS (condition
))
12168 /* Wrap it in a NOP_EXPR so that we can set the
12169 location of the condition. */
12170 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
12172 SET_EXPR_LOCATION (e
, token
->location
);
12173 chain
->safe_push (e
);
12176 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
12179 /* This is if-else without subsequent if. Zap the
12180 condition chain; we would have already warned at
12186 begin_else_clause (statement
);
12187 /* Parse the else-clause. */
12188 cp_parser_implicitly_scoped_statement (parser
, NULL
,
12189 guard_tinfo
, chain
);
12191 finish_else_clause (statement
);
12193 /* If we are currently parsing a then-clause, then
12194 IF_P will not be NULL. We set it to true to
12195 indicate that this if statement has an else clause.
12196 This may trigger the Wparentheses warning below
12197 when we get back up to the parent if statement. */
12203 ELSE_CLAUSE (statement
) = NULL_TREE
;
12204 in_discarded_stmt
= was_discarded
;
12205 --c_inhibit_evaluation_warnings
;
12210 /* This if statement does not have an else clause. If
12211 NESTED_IF is true, then the then-clause has an if
12212 statement which does have an else clause. We warn
12213 about the potential ambiguity. */
12215 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
12216 "suggest explicit braces to avoid ambiguous"
12218 if (warn_duplicated_cond
)
12220 /* We don't need the condition chain anymore. */
12226 /* Now we're all done with the if-statement. */
12227 finish_if_stmt (statement
);
12231 bool in_switch_statement_p
;
12232 unsigned char in_statement
;
12234 /* Add the condition. */
12235 finish_switch_cond (condition
, statement
);
12237 /* Parse the body of the switch-statement. */
12238 in_switch_statement_p
= parser
->in_switch_statement_p
;
12239 in_statement
= parser
->in_statement
;
12240 parser
->in_switch_statement_p
= true;
12241 parser
->in_statement
|= IN_SWITCH_STMT
;
12242 cp_parser_implicitly_scoped_statement (parser
, if_p
,
12244 parser
->in_switch_statement_p
= in_switch_statement_p
;
12245 parser
->in_statement
= in_statement
;
12247 /* Now we're all done with the switch-statement. */
12248 finish_switch_stmt (statement
);
12256 cp_parser_error (parser
, "expected selection-statement");
12257 return error_mark_node
;
12261 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12262 If we have seen at least one decl-specifier, and the next token is not
12263 a parenthesis (after "int (" we might be looking at a functional cast)
12264 neither we are dealing with a concept-check expression then we must be
12265 looking at a declaration. */
12268 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
12269 cp_decl_specifier_seq
*decl_specs
)
12271 if (decl_specs
->any_specifiers_p
12272 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12273 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12274 && !cp_parser_error_occurred (parser
)
12275 && !(decl_specs
->type
12276 && TREE_CODE (decl_specs
->type
) == TYPE_DECL
12277 && is_constrained_auto (TREE_TYPE (decl_specs
->type
))))
12278 cp_parser_commit_to_tentative_parse (parser
);
12281 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12282 The declarator shall not specify a function or an array. Returns
12283 TRUE if the declarator is valid, FALSE otherwise. */
12286 cp_parser_check_condition_declarator (cp_parser
* parser
,
12287 cp_declarator
*declarator
,
12290 if (declarator
== cp_error_declarator
12291 || function_declarator_p (declarator
)
12292 || declarator
->kind
== cdk_array
)
12294 if (declarator
== cp_error_declarator
)
12295 /* Already complained. */;
12296 else if (declarator
->kind
== cdk_array
)
12297 error_at (loc
, "condition declares an array");
12299 error_at (loc
, "condition declares a function");
12300 if (parser
->fully_implicit_function_template_p
)
12301 abort_fully_implicit_template (parser
);
12302 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
12303 /*or_comma=*/false,
12304 /*consume_paren=*/false);
12311 /* Parse a condition.
12315 type-specifier-seq declarator = initializer-clause
12316 type-specifier-seq declarator braced-init-list
12321 type-specifier-seq declarator asm-specification [opt]
12322 attributes [opt] = assignment-expression
12324 Returns the expression that should be tested. */
12327 cp_parser_condition (cp_parser
* parser
)
12329 cp_decl_specifier_seq type_specifiers
;
12330 const char *saved_message
;
12331 int declares_class_or_enum
;
12333 /* Try the declaration first. */
12334 cp_parser_parse_tentatively (parser
);
12335 /* New types are not allowed in the type-specifier-seq for a
12337 saved_message
= parser
->type_definition_forbidden_message
;
12338 parser
->type_definition_forbidden_message
12339 = G_("types may not be defined in conditions");
12340 /* Parse the type-specifier-seq. */
12341 cp_parser_decl_specifier_seq (parser
,
12342 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12344 &declares_class_or_enum
);
12345 /* Restore the saved message. */
12346 parser
->type_definition_forbidden_message
= saved_message
;
12348 /* Gather the attributes that were provided with the
12349 decl-specifiers. */
12350 tree prefix_attributes
= type_specifiers
.attributes
;
12352 cp_parser_maybe_commit_to_declaration (parser
, &type_specifiers
);
12354 /* If all is well, we might be looking at a declaration. */
12355 if (!cp_parser_error_occurred (parser
))
12358 tree asm_specification
;
12360 cp_declarator
*declarator
;
12361 tree initializer
= NULL_TREE
;
12362 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12364 /* Parse the declarator. */
12365 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12366 CP_PARSER_FLAGS_NONE
,
12367 /*ctor_dtor_or_conv_p=*/NULL
,
12368 /*parenthesized_p=*/NULL
,
12369 /*member_p=*/false,
12370 /*friend_p=*/false,
12371 /*static_p=*/false);
12372 /* Parse the attributes. */
12373 attributes
= cp_parser_attributes_opt (parser
);
12374 /* Parse the asm-specification. */
12375 asm_specification
= cp_parser_asm_specification_opt (parser
);
12376 /* If the next token is not an `=' or '{', then we might still be
12377 looking at an expression. For example:
12381 looks like a decl-specifier-seq and a declarator -- but then
12382 there is no `=', so this is an expression. */
12383 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12384 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12385 cp_parser_simulate_error (parser
);
12387 /* If we did see an `=' or '{', then we are looking at a declaration
12389 if (cp_parser_parse_definitely (parser
))
12392 bool non_constant_p
= false;
12393 int flags
= LOOKUP_ONLYCONVERTING
;
12395 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12396 return error_mark_node
;
12398 /* Create the declaration. */
12399 decl
= start_decl (declarator
, &type_specifiers
,
12400 /*initialized_p=*/true,
12401 attributes
, prefix_attributes
,
12404 /* Parse the initializer. */
12405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12407 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12408 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12411 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12413 /* Consume the `='. */
12414 cp_lexer_consume_token (parser
->lexer
);
12415 initializer
= cp_parser_initializer_clause (parser
,
12420 cp_parser_error (parser
, "expected initializer");
12421 initializer
= error_mark_node
;
12423 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12424 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12426 /* Process the initializer. */
12427 cp_finish_decl (decl
,
12428 initializer
, !non_constant_p
,
12433 pop_scope (pushed_scope
);
12435 return convert_from_reference (decl
);
12438 /* If we didn't even get past the declarator successfully, we are
12439 definitely not looking at a declaration. */
12441 cp_parser_abort_tentative_parse (parser
);
12443 /* Otherwise, we are looking at an expression. */
12444 return cp_parser_expression (parser
);
12447 /* Parses a for-statement or range-for-statement until the closing ')',
12451 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12453 tree init
, scope
, decl
;
12456 /* Begin the for-statement. */
12457 scope
= begin_for_scope (&init
);
12459 /* Parse the initialization. */
12460 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12463 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12466 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12470 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12471 unsigned short unroll
)
12473 /* Normal for loop */
12474 tree condition
= NULL_TREE
;
12475 tree expression
= NULL_TREE
;
12478 stmt
= begin_for_stmt (scope
, init
);
12479 /* The init-statement has already been parsed in
12480 cp_parser_init_statement, so no work is needed here. */
12481 finish_init_stmt (stmt
);
12483 /* If there's a condition, process it. */
12484 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12485 condition
= cp_parser_condition (parser
);
12488 cp_parser_error (parser
, "missing loop condition in loop with "
12489 "%<GCC ivdep%> pragma");
12490 condition
= error_mark_node
;
12494 cp_parser_error (parser
, "missing loop condition in loop with "
12495 "%<GCC unroll%> pragma");
12496 condition
= error_mark_node
;
12498 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12499 /* Look for the `;'. */
12500 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12502 /* If there's an expression, process it. */
12503 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12504 expression
= cp_parser_expression (parser
);
12505 finish_for_expr (expression
, stmt
);
12510 /* Tries to parse a range-based for-statement:
12513 decl-specifier-seq declarator : expression
12515 The decl-specifier-seq declarator and the `:' are already parsed by
12516 cp_parser_init_statement. If processing_template_decl it returns a
12517 newly created RANGE_FOR_STMT; if not, it is converted to a
12518 regular FOR_STMT. */
12521 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12522 bool ivdep
, unsigned short unroll
, bool is_omp
)
12524 tree stmt
, range_expr
;
12525 auto_vec
<cxx_binding
*, 16> bindings
;
12526 auto_vec
<tree
, 16> names
;
12527 tree decomp_first_name
= NULL_TREE
;
12528 unsigned int decomp_cnt
= 0;
12530 /* Get the range declaration momentarily out of the way so that
12531 the range expression doesn't clash with it. */
12532 if (range_decl
!= error_mark_node
)
12534 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12536 tree v
= DECL_VALUE_EXPR (range_decl
);
12537 /* For decomposition declaration get all of the corresponding
12538 declarations out of the way. */
12539 if (TREE_CODE (v
) == ARRAY_REF
12540 && VAR_P (TREE_OPERAND (v
, 0))
12541 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12543 tree d
= range_decl
;
12544 range_decl
= TREE_OPERAND (v
, 0);
12545 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12546 decomp_first_name
= d
;
12547 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12549 tree name
= DECL_NAME (d
);
12550 names
.safe_push (name
);
12551 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12552 IDENTIFIER_BINDING (name
)
12553 = IDENTIFIER_BINDING (name
)->previous
;
12557 if (names
.is_empty ())
12559 tree name
= DECL_NAME (range_decl
);
12560 names
.safe_push (name
);
12561 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12562 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12568 bool expr_non_constant_p
;
12569 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12572 range_expr
= cp_parser_expression (parser
);
12574 /* Put the range declaration(s) back into scope. */
12575 for (unsigned int i
= 0; i
< names
.length (); i
++)
12577 cxx_binding
*binding
= bindings
[i
];
12578 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12579 IDENTIFIER_BINDING (names
[i
]) = binding
;
12582 /* finish_omp_for has its own code for the following, so just
12583 return the range_expr instead. */
12587 /* If in template, STMT is converted to a normal for-statement
12588 at instantiation. If not, it is done just ahead. */
12589 if (processing_template_decl
)
12591 if (check_for_bare_parameter_packs (range_expr
))
12592 range_expr
= error_mark_node
;
12593 stmt
= begin_range_for_stmt (scope
, init
);
12595 RANGE_FOR_IVDEP (stmt
) = 1;
12597 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12598 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12599 if (!type_dependent_expression_p (range_expr
)
12600 /* do_auto_deduction doesn't mess with template init-lists. */
12601 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12602 do_range_for_auto_deduction (range_decl
, range_expr
);
12606 stmt
= begin_for_stmt (scope
, init
);
12607 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12608 decomp_first_name
, decomp_cnt
, ivdep
,
12614 /* Subroutine of cp_convert_range_for: given the initializer expression,
12615 builds up the range temporary. */
12618 build_range_temp (tree range_expr
)
12620 tree range_type
, range_temp
;
12622 /* Find out the type deduced by the declaration
12623 `auto &&__range = range_expr'. */
12624 range_type
= cp_build_reference_type (make_auto (), true);
12625 range_type
= do_auto_deduction (range_type
, range_expr
,
12626 type_uses_auto (range_type
));
12628 /* Create the __range variable. */
12629 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12631 TREE_USED (range_temp
) = 1;
12632 DECL_ARTIFICIAL (range_temp
) = 1;
12637 /* Used by cp_parser_range_for in template context: we aren't going to
12638 do a full conversion yet, but we still need to resolve auto in the
12639 type of the for-range-declaration if present. This is basically
12640 a shortcut version of cp_convert_range_for. */
12643 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12645 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12648 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12649 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12650 iter_type
= (cp_parser_perform_range_for_lookup
12651 (range_temp
, &begin_dummy
, &end_dummy
));
12654 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12656 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12658 tf_warning_or_error
);
12659 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12660 iter_decl
, auto_node
);
12665 /* Warns when the loop variable should be changed to a reference type to
12666 avoid unnecessary copying. I.e., from
12668 for (const auto x : range)
12670 where range returns a reference, to
12672 for (const auto &x : range)
12674 if this version doesn't make a copy. DECL is the RANGE_DECL; EXPR is the
12675 *__for_begin expression.
12676 This function is never called when processing_template_decl is on. */
12679 warn_for_range_copy (tree decl
, tree expr
)
12681 if (!warn_range_loop_construct
12682 || decl
== error_mark_node
)
12685 location_t loc
= DECL_SOURCE_LOCATION (decl
);
12686 tree type
= TREE_TYPE (decl
);
12688 if (from_macro_expansion_at (loc
))
12691 if (TYPE_REF_P (type
))
12693 /* TODO: Implement reference warnings. */
12696 else if (!CP_TYPE_CONST_P (type
))
12699 /* Since small trivially copyable types are cheap to copy, we suppress the
12700 warning for them. 64B is a common size of a cache line. */
12701 if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
12702 || (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) <= 64
12703 && trivially_copyable_p (type
)))
12706 tree rtype
= cp_build_reference_type (type
, /*rval*/false);
12707 /* If we could initialize the reference directly, it wouldn't involve any
12709 if (!ref_conv_binds_directly_p (rtype
, expr
))
12712 auto_diagnostic_group d
;
12713 if (warning_at (loc
, OPT_Wrange_loop_construct
,
12714 "loop variable %qD creates a copy from type %qT",
12717 gcc_rich_location
richloc (loc
);
12718 richloc
.add_fixit_insert_before ("&");
12719 inform (&richloc
, "use reference type to prevent copying");
12723 /* Converts a range-based for-statement into a normal
12724 for-statement, as per the definition.
12726 for (RANGE_DECL : RANGE_EXPR)
12729 should be equivalent to:
12732 auto &&__range = RANGE_EXPR;
12733 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
12737 RANGE_DECL = *__begin;
12742 If RANGE_EXPR is an array:
12743 BEGIN_EXPR = __range
12744 END_EXPR = __range + ARRAY_SIZE(__range)
12745 Else if RANGE_EXPR has a member 'begin' or 'end':
12746 BEGIN_EXPR = __range.begin()
12747 END_EXPR = __range.end()
12749 BEGIN_EXPR = begin(__range)
12750 END_EXPR = end(__range);
12752 If __range has a member 'begin' but not 'end', or vice versa, we must
12753 still use the second alternative (it will surely fail, however).
12754 When calling begin()/end() in the third alternative we must use
12755 argument dependent lookup, but always considering 'std' as an associated
12759 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12760 tree decomp_first_name
, unsigned int decomp_cnt
,
12761 bool ivdep
, unsigned short unroll
)
12764 tree iter_type
, begin_expr
, end_expr
;
12765 tree condition
, expression
;
12767 range_expr
= mark_lvalue_use (range_expr
);
12769 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12770 /* If an error happened previously do nothing or else a lot of
12771 unhelpful errors would be issued. */
12772 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12777 if (VAR_P (range_expr
)
12778 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12779 /* Can't bind a reference to an array of runtime bound. */
12780 range_temp
= range_expr
;
12783 range_temp
= build_range_temp (range_expr
);
12784 pushdecl (range_temp
);
12785 cp_finish_decl (range_temp
, range_expr
,
12786 /*is_constant_init*/false, NULL_TREE
,
12787 LOOKUP_ONLYCONVERTING
);
12788 range_temp
= convert_from_reference (range_temp
);
12790 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12791 &begin_expr
, &end_expr
);
12794 /* The new for initialization statement. */
12795 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12797 TREE_USED (begin
) = 1;
12798 DECL_ARTIFICIAL (begin
) = 1;
12800 cp_finish_decl (begin
, begin_expr
,
12801 /*is_constant_init*/false, NULL_TREE
,
12802 LOOKUP_ONLYCONVERTING
);
12804 if (cxx_dialect
>= cxx17
)
12805 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12806 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12807 TREE_USED (end
) = 1;
12808 DECL_ARTIFICIAL (end
) = 1;
12810 cp_finish_decl (end
, end_expr
,
12811 /*is_constant_init*/false, NULL_TREE
,
12812 LOOKUP_ONLYCONVERTING
);
12814 finish_init_stmt (statement
);
12816 /* The new for condition. */
12817 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12820 NULL
, tf_warning_or_error
);
12821 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12823 /* The new increment expression. */
12824 expression
= finish_unary_op_expr (input_location
,
12825 PREINCREMENT_EXPR
, begin
,
12826 tf_warning_or_error
);
12827 finish_for_expr (expression
, statement
);
12829 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12830 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12832 /* The declaration is initialized with *__begin inside the loop body. */
12833 tree deref_begin
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12834 tf_warning_or_error
);
12835 cp_finish_decl (range_decl
, deref_begin
,
12836 /*is_constant_init*/false, NULL_TREE
,
12837 LOOKUP_ONLYCONVERTING
);
12838 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12839 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12841 warn_for_range_copy (range_decl
, deref_begin
);
12846 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12847 We need to solve both at the same time because the method used
12848 depends on the existence of members begin or end.
12849 Returns the type deduced for the iterator expression. */
12852 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12854 if (error_operand_p (range
))
12856 *begin
= *end
= error_mark_node
;
12857 return error_mark_node
;
12860 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12862 error ("range-based %<for%> expression of type %qT "
12863 "has incomplete type", TREE_TYPE (range
));
12864 *begin
= *end
= error_mark_node
;
12865 return error_mark_node
;
12867 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12869 /* If RANGE is an array, we will use pointer arithmetic. */
12870 *begin
= decay_conversion (range
, tf_warning_or_error
);
12871 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12873 array_type_nelts_top (TREE_TYPE (range
)),
12875 return TREE_TYPE (*begin
);
12879 /* If it is not an array, we must do a bit of magic. */
12880 tree id_begin
, id_end
;
12881 tree member_begin
, member_end
;
12883 *begin
= *end
= error_mark_node
;
12885 id_begin
= get_identifier ("begin");
12886 id_end
= get_identifier ("end");
12887 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12888 /*protect=*/2, /*want_type=*/false,
12889 tf_warning_or_error
);
12890 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12891 /*protect=*/2, /*want_type=*/false,
12892 tf_warning_or_error
);
12894 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12896 /* Use the member functions. */
12897 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12898 *end
= cp_parser_range_for_member_function (range
, id_end
);
12902 /* Use global functions with ADL. */
12905 vec_safe_push (vec
, range
);
12907 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12908 tf_warning_or_error
);
12909 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12910 tf_warning_or_error
);
12911 member_end
= perform_koenig_lookup (id_end
, vec
,
12912 tf_warning_or_error
);
12913 *end
= finish_call_expr (member_end
, &vec
, false, true,
12914 tf_warning_or_error
);
12917 /* Last common checks. */
12918 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12920 /* If one of the expressions is an error do no more checks. */
12921 *begin
= *end
= error_mark_node
;
12922 return error_mark_node
;
12924 else if (type_dependent_expression_p (*begin
)
12925 || type_dependent_expression_p (*end
))
12926 /* Can happen, when, eg, in a template context, Koenig lookup
12927 can't resolve begin/end (c++/58503). */
12931 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12932 /* The unqualified type of the __begin and __end temporaries should
12933 be the same, as required by the multiple auto declaration. */
12934 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12936 if (cxx_dialect
>= cxx17
12937 && (build_x_binary_op (input_location
, NE_EXPR
,
12938 *begin
, ERROR_MARK
,
12941 != error_mark_node
))
12942 /* P0184R0 allows __begin and __end to have different types,
12943 but make sure they are comparable so we can give a better
12946 error ("inconsistent begin/end types in range-based %<for%> "
12947 "statement: %qT and %qT",
12948 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12955 /* Helper function for cp_parser_perform_range_for_lookup.
12956 Builds a tree for RANGE.IDENTIFIER(). */
12959 cp_parser_range_for_member_function (tree range
, tree identifier
)
12963 member
= finish_class_member_access_expr (range
, identifier
,
12964 false, tf_warning_or_error
);
12965 if (member
== error_mark_node
)
12966 return error_mark_node
;
12969 res
= finish_call_expr (member
, &vec
,
12970 /*disallow_virtual=*/false,
12971 /*koenig_p=*/false,
12972 tf_warning_or_error
);
12976 /* Parse an iteration-statement.
12978 iteration-statement:
12979 while ( condition ) statement
12980 do statement while ( expression ) ;
12981 for ( init-statement condition [opt] ; expression [opt] )
12984 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12987 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12988 unsigned short unroll
)
12993 unsigned char in_statement
;
12994 token_indent_info guard_tinfo
;
12996 /* Peek at the next token. */
12997 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12999 return error_mark_node
;
13001 guard_tinfo
= get_token_indent_info (token
);
13003 /* Remember whether or not we are already within an iteration
13005 in_statement
= parser
->in_statement
;
13007 /* See what kind of keyword it is. */
13008 keyword
= token
->keyword
;
13015 /* Begin the while-statement. */
13016 statement
= begin_while_stmt ();
13017 /* Look for the `('. */
13018 matching_parens parens
;
13019 parens
.require_open (parser
);
13020 /* Parse the condition. */
13021 condition
= cp_parser_condition (parser
);
13022 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
13023 /* Look for the `)'. */
13024 parens
.require_close (parser
);
13025 /* Parse the dependent statement. */
13026 parser
->in_statement
= IN_ITERATION_STMT
;
13027 bool prev
= note_iteration_stmt_body_start ();
13028 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
13029 note_iteration_stmt_body_end (prev
);
13030 parser
->in_statement
= in_statement
;
13031 /* We're done with the while-statement. */
13032 finish_while_stmt (statement
);
13040 /* Begin the do-statement. */
13041 statement
= begin_do_stmt ();
13042 /* Parse the body of the do-statement. */
13043 parser
->in_statement
= IN_ITERATION_STMT
;
13044 bool prev
= note_iteration_stmt_body_start ();
13045 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
13046 note_iteration_stmt_body_end (prev
);
13047 parser
->in_statement
= in_statement
;
13048 finish_do_body (statement
);
13049 /* Look for the `while' keyword. */
13050 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
13051 /* Look for the `('. */
13052 matching_parens parens
;
13053 parens
.require_open (parser
);
13054 /* Parse the expression. */
13055 expression
= cp_parser_expression (parser
);
13056 /* We're done with the do-statement. */
13057 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
13058 /* Look for the `)'. */
13059 parens
.require_close (parser
);
13060 /* Look for the `;'. */
13061 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13067 /* Look for the `('. */
13068 matching_parens parens
;
13069 parens
.require_open (parser
);
13071 statement
= cp_parser_for (parser
, ivdep
, unroll
);
13073 /* Look for the `)'. */
13074 parens
.require_close (parser
);
13076 /* Parse the body of the for-statement. */
13077 parser
->in_statement
= IN_ITERATION_STMT
;
13078 bool prev
= note_iteration_stmt_body_start ();
13079 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
13080 note_iteration_stmt_body_end (prev
);
13081 parser
->in_statement
= in_statement
;
13083 /* We're done with the for-statement. */
13084 finish_for_stmt (statement
);
13089 cp_parser_error (parser
, "expected iteration-statement");
13090 statement
= error_mark_node
;
13097 /* Parse a init-statement or the declarator of a range-based-for.
13098 Returns true if a range-based-for declaration is seen.
13101 expression-statement
13102 simple-declaration */
13105 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
13107 /* If the next token is a `;', then we have an empty
13108 expression-statement. Grammatically, this is also a
13109 simple-declaration, but an invalid one, because it does not
13110 declare anything. Therefore, if we did not handle this case
13111 specially, we would issue an error message about an invalid
13113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13115 bool is_range_for
= false;
13116 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
13118 /* Try to parse the init-statement. */
13119 if (cp_parser_range_based_for_with_init_p (parser
))
13122 cp_parser_parse_tentatively (parser
);
13123 /* Parse the declaration. */
13124 cp_parser_simple_declaration (parser
,
13125 /*function_definition_allowed_p=*/false,
13127 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13128 if (!cp_parser_parse_definitely (parser
))
13129 /* That didn't work, try to parse it as an expression-statement. */
13130 cp_parser_expression_statement (parser
, NULL_TREE
);
13132 if (cxx_dialect
< cxx20
)
13134 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
13135 "range-based %<for%> loops with initializer only "
13136 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13137 *decl
= error_mark_node
;
13141 /* A colon is used in range-based for. */
13142 parser
->colon_corrects_to_scope_p
= false;
13144 /* We're going to speculatively look for a declaration, falling back
13145 to an expression, if necessary. */
13146 cp_parser_parse_tentatively (parser
);
13147 /* Parse the declaration. */
13148 cp_parser_simple_declaration (parser
,
13149 /*function_definition_allowed_p=*/false,
13151 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
13152 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13154 /* It is a range-for, consume the ':'. */
13155 cp_lexer_consume_token (parser
->lexer
);
13156 is_range_for
= true;
13157 if (cxx_dialect
< cxx11
)
13158 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
13159 "range-based %<for%> loops only available with "
13160 "%<-std=c++11%> or %<-std=gnu++11%>");
13163 /* The ';' is not consumed yet because we told
13164 cp_parser_simple_declaration not to. */
13165 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13167 if (cp_parser_parse_definitely (parser
))
13168 return is_range_for
;
13169 /* If the tentative parse failed, then we shall need to look for an
13170 expression-statement. */
13172 /* If we are here, it is an expression-statement. */
13173 cp_parser_expression_statement (parser
, NULL_TREE
);
13177 /* Parse a jump-statement.
13182 return expression [opt] ;
13183 return braced-init-list ;
13184 coroutine-return-statement;
13190 goto * expression ;
13192 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13195 cp_parser_jump_statement (cp_parser
* parser
)
13197 tree statement
= error_mark_node
;
13200 unsigned char in_statement
;
13202 /* Peek at the next token. */
13203 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
13205 return error_mark_node
;
13207 /* See what kind of keyword it is. */
13208 keyword
= token
->keyword
;
13212 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
13213 switch (in_statement
)
13216 error_at (token
->location
, "break statement not within loop or switch");
13219 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
13220 || in_statement
== IN_ITERATION_STMT
);
13221 statement
= finish_break_stmt ();
13222 if (in_statement
== IN_ITERATION_STMT
)
13223 break_maybe_infinite_loop ();
13226 error_at (token
->location
, "invalid exit from OpenMP structured block");
13229 error_at (token
->location
, "break statement used with OpenMP for loop");
13232 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13236 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
13239 error_at (token
->location
, "continue statement not within a loop");
13241 /* Fall through. */
13242 case IN_ITERATION_STMT
:
13244 statement
= finish_continue_stmt ();
13247 error_at (token
->location
, "invalid exit from OpenMP structured block");
13250 gcc_unreachable ();
13252 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13255 case RID_CO_RETURN
:
13259 bool expr_non_constant_p
;
13261 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13263 cp_lexer_set_source_position (parser
->lexer
);
13264 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13265 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13267 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13268 expr
= cp_parser_expression (parser
);
13270 /* If the next token is a `;', then there is no
13273 /* Build the return-statement, check co-return first, since type
13274 deduction is not valid there. */
13275 if (keyword
== RID_CO_RETURN
)
13276 statement
= finish_co_return_stmt (token
->location
, expr
);
13277 else if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
13278 /* Don't deduce from a discarded return statement. */;
13280 statement
= finish_return_stmt (expr
);
13281 /* Look for the final `;'. */
13282 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13287 if (parser
->in_function_body
13288 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
13290 error ("%<goto%> in %<constexpr%> function");
13291 cp_function_chain
->invalid_constexpr
= true;
13294 /* Create the goto-statement. */
13295 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
13297 /* Issue a warning about this use of a GNU extension. */
13298 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
13299 /* Consume the '*' token. */
13300 cp_lexer_consume_token (parser
->lexer
);
13301 /* Parse the dependent expression. */
13302 finish_goto_stmt (cp_parser_expression (parser
));
13305 finish_goto_stmt (cp_parser_identifier (parser
));
13306 /* Look for the final `;'. */
13307 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13311 cp_parser_error (parser
, "expected jump-statement");
13318 /* Parse a declaration-statement.
13320 declaration-statement:
13321 block-declaration */
13324 cp_parser_declaration_statement (cp_parser
* parser
)
13328 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13329 p
= obstack_alloc (&declarator_obstack
, 0);
13331 /* Parse the block-declaration. */
13332 cp_parser_block_declaration (parser
, /*statement_p=*/true);
13334 /* Free any declarators allocated. */
13335 obstack_free (&declarator_obstack
, p
);
13338 /* Some dependent statements (like `if (cond) statement'), are
13339 implicitly in their own scope. In other words, if the statement is
13340 a single statement (as opposed to a compound-statement), it is
13341 none-the-less treated as if it were enclosed in braces. Any
13342 declarations appearing in the dependent statement are out of scope
13343 after control passes that point. This function parses a statement,
13344 but ensures that is in its own scope, even if it is not a
13345 compound-statement.
13347 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13348 is a (possibly labeled) if statement which is not enclosed in
13349 braces and has an else clause. This is used to implement
13352 CHAIN is a vector of if-else-if conditions. This is used to implement
13355 Returns the new statement. */
13358 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
13359 const token_indent_info
&guard_tinfo
,
13363 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13364 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
13365 token_indent_info body_tinfo
13366 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13371 /* Mark if () ; with a special NOP_EXPR. */
13372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13374 cp_lexer_consume_token (parser
->lexer
);
13375 statement
= add_stmt (build_empty_stmt (body_loc
));
13377 if (guard_tinfo
.keyword
== RID_IF
13378 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13379 warning_at (body_loc
, OPT_Wempty_body
,
13380 "suggest braces around empty body in an %<if%> statement");
13381 else if (guard_tinfo
.keyword
== RID_ELSE
)
13382 warning_at (body_loc
, OPT_Wempty_body
,
13383 "suggest braces around empty body in an %<else%> statement");
13385 /* if a compound is opened, we simply parse the statement directly. */
13386 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13387 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13388 /* If the token is not a `{', then we must take special action. */
13391 /* Create a compound-statement. */
13392 statement
= begin_compound_stmt (0);
13393 /* Parse the dependent-statement. */
13394 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13395 &body_loc_after_labels
);
13396 /* Finish the dummy compound-statement. */
13397 finish_compound_stmt (statement
);
13400 token_indent_info next_tinfo
13401 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13402 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13404 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13405 && next_tinfo
.type
!= CPP_SEMICOLON
)
13406 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13407 guard_tinfo
.location
, guard_tinfo
.keyword
);
13409 /* Return the statement. */
13413 /* For some dependent statements (like `while (cond) statement'), we
13414 have already created a scope. Therefore, even if the dependent
13415 statement is a compound-statement, we do not want to create another
13419 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13420 const token_indent_info
&guard_tinfo
)
13422 /* If the token is a `{', then we must take special action. */
13423 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13425 token_indent_info body_tinfo
13426 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13427 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13429 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13430 &loc_after_labels
);
13431 token_indent_info next_tinfo
13432 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13433 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13435 if (loc_after_labels
!= UNKNOWN_LOCATION
13436 && next_tinfo
.type
!= CPP_SEMICOLON
)
13437 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13438 guard_tinfo
.location
,
13439 guard_tinfo
.keyword
);
13443 /* Avoid calling cp_parser_compound_statement, so that we
13444 don't create a new scope. Do everything else by hand. */
13445 matching_braces braces
;
13446 braces
.require_open (parser
);
13447 /* If the next keyword is `__label__' we have a label declaration. */
13448 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13449 cp_parser_label_declaration (parser
);
13450 /* Parse an (optional) statement-seq. */
13451 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13452 braces
.require_close (parser
);
13456 /* Declarations [gram.dcl.dcl] */
13458 /* Parse an optional declaration-sequence.
13462 declaration-seq declaration */
13465 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13469 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13471 if (token
->type
== CPP_CLOSE_BRACE
13472 || token
->type
== CPP_EOF
)
13475 cp_parser_toplevel_declaration (parser
);
13479 /* Parse a declaration.
13483 function-definition
13484 template-declaration
13485 explicit-instantiation
13486 explicit-specialization
13487 linkage-specification
13488 namespace-definition
13496 __extension__ declaration */
13499 cp_parser_declaration (cp_parser
* parser
)
13501 int saved_pedantic
;
13503 /* Check for the `__extension__' keyword. */
13504 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13506 /* Parse the qualified declaration. */
13507 cp_parser_declaration (parser
);
13508 /* Restore the PEDANTIC flag. */
13509 pedantic
= saved_pedantic
;
13514 /* Try to figure out what kind of declaration is present. */
13515 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
13516 cp_token
*token2
= (token1
->type
== CPP_EOF
13517 ? token1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
13519 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13520 void *p
= obstack_alloc (&declarator_obstack
, 0);
13522 tree attributes
= NULL_TREE
;
13524 /* If the next token is `extern' and the following token is a string
13525 literal, then we have a linkage specification. */
13526 if (token1
->keyword
== RID_EXTERN
13527 && cp_parser_is_pure_string_literal (token2
))
13528 cp_parser_linkage_specification (parser
);
13529 /* If the next token is `template', then we have either a template
13530 declaration, an explicit instantiation, or an explicit
13532 else if (token1
->keyword
== RID_TEMPLATE
)
13534 /* `template <>' indicates a template specialization. */
13535 if (token2
->type
== CPP_LESS
13536 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13537 cp_parser_explicit_specialization (parser
);
13538 /* `template <' indicates a template declaration. */
13539 else if (token2
->type
== CPP_LESS
)
13540 cp_parser_template_declaration (parser
, /*member_p=*/false);
13541 /* Anything else must be an explicit instantiation. */
13543 cp_parser_explicit_instantiation (parser
);
13545 /* If the next token is `export', then we have a template
13547 else if (token1
->keyword
== RID_EXPORT
)
13548 cp_parser_template_declaration (parser
, /*member_p=*/false);
13549 /* If the next token is `extern', 'static' or 'inline' and the one
13550 after that is `template', we have a GNU extended explicit
13551 instantiation directive. */
13552 else if (cp_parser_allow_gnu_extensions_p (parser
)
13553 && token2
->keyword
== RID_TEMPLATE
13554 && (token1
->keyword
== RID_EXTERN
13555 || token1
->keyword
== RID_STATIC
13556 || token1
->keyword
== RID_INLINE
))
13557 cp_parser_explicit_instantiation (parser
);
13558 /* If the next token is `namespace', check for a named or unnamed
13559 namespace definition. */
13560 else if (token1
->keyword
== RID_NAMESPACE
13561 && (/* A named namespace definition. */
13562 (token2
->type
== CPP_NAME
13563 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13565 || (token2
->type
== CPP_OPEN_SQUARE
13566 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13567 == CPP_OPEN_SQUARE
)
13568 /* An unnamed namespace definition. */
13569 || token2
->type
== CPP_OPEN_BRACE
13570 || token2
->keyword
== RID_ATTRIBUTE
))
13571 cp_parser_namespace_definition (parser
);
13572 /* An inline (associated) namespace definition. */
13573 else if (token2
->keyword
== RID_NAMESPACE
13574 && token1
->keyword
== RID_INLINE
)
13575 cp_parser_namespace_definition (parser
);
13576 /* Objective-C++ declaration/definition. */
13577 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
->keyword
))
13578 cp_parser_objc_declaration (parser
, NULL_TREE
);
13579 else if (c_dialect_objc ()
13580 && token1
->keyword
== RID_ATTRIBUTE
13581 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13582 cp_parser_objc_declaration (parser
, attributes
);
13583 /* At this point we may have a template declared by a concept
13585 else if (flag_concepts
13586 && cp_parser_template_declaration_after_export (parser
,
13587 /*member_p=*/false))
13590 /* Try to parse a block-declaration, or a function-definition. */
13591 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13593 /* Free any declarators allocated. */
13594 obstack_free (&declarator_obstack
, p
);
13597 /* Parse a namespace-scope declaration. */
13600 cp_parser_toplevel_declaration (cp_parser
* parser
)
13602 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13604 if (token
->type
== CPP_PRAGMA
)
13605 /* A top-level declaration can consist solely of a #pragma. A
13606 nested declaration cannot, so this is done here and not in
13607 cp_parser_declaration. (A #pragma at block scope is
13608 handled in cp_parser_statement.) */
13609 cp_parser_pragma (parser
, pragma_external
, NULL
);
13610 else if (token
->type
== CPP_SEMICOLON
)
13612 cp_lexer_consume_token (parser
->lexer
);
13613 /* A declaration consisting of a single semicolon is invalid
13614 * before C++11. Allow it unless we're being pedantic. */
13615 if (cxx_dialect
< cxx11
)
13616 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13619 /* Parse the declaration itself. */
13620 cp_parser_declaration (parser
);
13623 /* Parse a block-declaration.
13628 namespace-alias-definition
13635 __extension__ block-declaration
13640 static_assert-declaration
13642 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13643 part of a declaration-statement. */
13646 cp_parser_block_declaration (cp_parser
*parser
,
13649 int saved_pedantic
;
13651 /* Check for the `__extension__' keyword. */
13652 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13654 /* Parse the qualified declaration. */
13655 cp_parser_block_declaration (parser
, statement_p
);
13656 /* Restore the PEDANTIC flag. */
13657 pedantic
= saved_pedantic
;
13662 /* Peek at the next token to figure out which kind of declaration is
13664 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
13666 /* If the next keyword is `asm', we have an asm-definition. */
13667 if (token1
->keyword
== RID_ASM
)
13670 cp_parser_commit_to_tentative_parse (parser
);
13671 cp_parser_asm_definition (parser
);
13673 /* If the next keyword is `namespace', we have a
13674 namespace-alias-definition. */
13675 else if (token1
->keyword
== RID_NAMESPACE
)
13676 cp_parser_namespace_alias_definition (parser
);
13677 /* If the next keyword is `using', we have a
13678 using-declaration, a using-directive, or an alias-declaration. */
13679 else if (token1
->keyword
== RID_USING
)
13684 cp_parser_commit_to_tentative_parse (parser
);
13685 /* If the token after `using' is `namespace', then we have a
13686 using-directive. */
13687 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13688 if (token2
->keyword
== RID_NAMESPACE
)
13689 cp_parser_using_directive (parser
);
13690 /* If the second token after 'using' is '=', then we have an
13691 alias-declaration. */
13692 else if (cxx_dialect
>= cxx11
13693 && token2
->type
== CPP_NAME
13694 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13695 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13696 cp_parser_alias_declaration (parser
);
13697 /* Otherwise, it's a using-declaration. */
13699 cp_parser_using_declaration (parser
,
13700 /*access_declaration_p=*/false);
13702 /* If the next keyword is `__label__' we have a misplaced label
13704 else if (token1
->keyword
== RID_LABEL
)
13706 cp_lexer_consume_token (parser
->lexer
);
13707 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13708 cp_parser_skip_to_end_of_statement (parser
);
13709 /* If the next token is now a `;', consume it. */
13710 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13711 cp_lexer_consume_token (parser
->lexer
);
13713 /* If the next token is `static_assert' we have a static assertion. */
13714 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13715 cp_parser_static_assert (parser
, /*member_p=*/false);
13716 /* Anything else must be a simple-declaration. */
13718 cp_parser_simple_declaration (parser
, !statement_p
,
13719 /*maybe_range_for_decl*/NULL
);
13722 /* Parse a simple-declaration.
13724 simple-declaration:
13725 decl-specifier-seq [opt] init-declarator-list [opt] ;
13726 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13727 brace-or-equal-initializer ;
13729 init-declarator-list:
13731 init-declarator-list , init-declarator
13733 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13734 function-definition as a simple-declaration.
13736 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13737 parsed declaration if it is an uninitialized single declarator not followed
13738 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13739 if present, will not be consumed. */
13742 cp_parser_simple_declaration (cp_parser
* parser
,
13743 bool function_definition_allowed_p
,
13744 tree
*maybe_range_for_decl
)
13746 cp_decl_specifier_seq decl_specifiers
;
13747 int declares_class_or_enum
;
13748 bool saw_declarator
;
13749 location_t comma_loc
= UNKNOWN_LOCATION
;
13750 location_t init_loc
= UNKNOWN_LOCATION
;
13752 if (maybe_range_for_decl
)
13753 *maybe_range_for_decl
= NULL_TREE
;
13755 /* Defer access checks until we know what is being declared; the
13756 checks for names appearing in the decl-specifier-seq should be
13757 done as if we were in the scope of the thing being declared. */
13758 push_deferring_access_checks (dk_deferred
);
13760 /* Parse the decl-specifier-seq. We have to keep track of whether
13761 or not the decl-specifier-seq declares a named class or
13762 enumeration type, since that is the only case in which the
13763 init-declarator-list is allowed to be empty.
13767 In a simple-declaration, the optional init-declarator-list can be
13768 omitted only when declaring a class or enumeration, that is when
13769 the decl-specifier-seq contains either a class-specifier, an
13770 elaborated-type-specifier, or an enum-specifier. */
13771 cp_parser_decl_specifier_seq (parser
,
13772 CP_PARSER_FLAGS_OPTIONAL
,
13774 &declares_class_or_enum
);
13775 /* We no longer need to defer access checks. */
13776 stop_deferring_access_checks ();
13778 /* In a block scope, a valid declaration must always have a
13779 decl-specifier-seq. By not trying to parse declarators, we can
13780 resolve the declaration/expression ambiguity more quickly. */
13781 if (!function_definition_allowed_p
13782 && !decl_specifiers
.any_specifiers_p
)
13784 cp_parser_error (parser
, "expected declaration");
13788 /* If the next two tokens are both identifiers, the code is
13789 erroneous. The usual cause of this situation is code like:
13793 where "T" should name a type -- but does not. */
13794 if (!decl_specifiers
.any_type_specifiers_p
13795 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13797 /* If parsing tentatively, we should commit; we really are
13798 looking at a declaration. */
13799 cp_parser_commit_to_tentative_parse (parser
);
13804 cp_parser_maybe_commit_to_declaration (parser
, &decl_specifiers
);
13806 /* Look for C++17 decomposition declaration. */
13807 for (size_t n
= 1; ; n
++)
13808 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13809 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13811 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13812 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13813 && decl_specifiers
.any_specifiers_p
)
13816 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13817 maybe_range_for_decl
,
13820 /* The next token should be either a `,' or a `;'. */
13821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13822 /* If it's a `;', we are done. */
13823 if (token
->type
== CPP_SEMICOLON
)
13825 else if (maybe_range_for_decl
)
13827 if (*maybe_range_for_decl
== NULL_TREE
)
13828 *maybe_range_for_decl
= error_mark_node
;
13831 /* Anything else is an error. */
13834 /* If we have already issued an error message we don't need
13835 to issue another one. */
13836 if ((decl
!= error_mark_node
13837 && DECL_INITIAL (decl
) != error_mark_node
)
13838 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13839 cp_parser_error (parser
, "expected %<;%>");
13840 /* Skip tokens until we reach the end of the statement. */
13841 cp_parser_skip_to_end_of_statement (parser
);
13842 /* If the next token is now a `;', consume it. */
13843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13844 cp_lexer_consume_token (parser
->lexer
);
13852 bool auto_specifier_p
;
13853 /* NULL_TREE if both variable and function declaration are allowed,
13854 error_mark_node if function declaration are not allowed and
13855 a FUNCTION_DECL that should be diagnosed if it is followed by
13856 variable declarations. */
13857 tree auto_function_declaration
;
13859 last_type
= NULL_TREE
;
13861 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13862 auto_function_declaration
= NULL_TREE
;
13864 /* Keep going until we hit the `;' at the end of the simple
13866 saw_declarator
= false;
13867 while (cp_lexer_next_token_is_not (parser
->lexer
,
13871 bool function_definition_p
;
13873 tree auto_result
= NULL_TREE
;
13875 if (saw_declarator
)
13877 /* If we are processing next declarator, comma is expected */
13878 token
= cp_lexer_peek_token (parser
->lexer
);
13879 gcc_assert (token
->type
== CPP_COMMA
);
13880 cp_lexer_consume_token (parser
->lexer
);
13881 if (maybe_range_for_decl
)
13883 *maybe_range_for_decl
= error_mark_node
;
13884 if (comma_loc
== UNKNOWN_LOCATION
)
13885 comma_loc
= token
->location
;
13889 saw_declarator
= true;
13891 /* Parse the init-declarator. */
13892 decl
= cp_parser_init_declarator (parser
,
13893 CP_PARSER_FLAGS_NONE
,
13896 function_definition_allowed_p
,
13897 /*member_p=*/false,
13898 declares_class_or_enum
,
13899 &function_definition_p
,
13900 maybe_range_for_decl
,
13903 /* If an error occurred while parsing tentatively, exit quickly.
13904 (That usually happens when in the body of a function; each
13905 statement is treated as a declaration-statement until proven
13907 if (cp_parser_error_occurred (parser
))
13910 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13912 /* If the init-declarator-list contains more than one
13913 init-declarator, they shall all form declarations of
13915 if (auto_function_declaration
== NULL_TREE
)
13916 auto_function_declaration
13917 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13918 else if (TREE_CODE (decl
) == FUNCTION_DECL
13919 || auto_function_declaration
!= error_mark_node
)
13921 error_at (decl_specifiers
.locations
[ds_type_spec
],
13922 "non-variable %qD in declaration with more than one "
13923 "declarator with placeholder type",
13924 TREE_CODE (decl
) == FUNCTION_DECL
13925 ? decl
: auto_function_declaration
);
13926 auto_function_declaration
= error_mark_node
;
13931 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13934 && last_type
!= error_mark_node
13935 && !same_type_p (auto_result
, last_type
))
13937 /* If the list of declarators contains more than one declarator,
13938 the type of each declared variable is determined as described
13939 above. If the type deduced for the template parameter U is not
13940 the same in each deduction, the program is ill-formed. */
13941 error_at (decl_specifiers
.locations
[ds_type_spec
],
13942 "inconsistent deduction for %qT: %qT and then %qT",
13943 decl_specifiers
.type
, last_type
, auto_result
);
13944 last_type
= error_mark_node
;
13947 last_type
= auto_result
;
13950 /* Handle function definitions specially. */
13951 if (function_definition_p
)
13953 /* If the next token is a `,', then we are probably
13954 processing something like:
13958 which is erroneous. */
13959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13961 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13962 error_at (token
->location
,
13964 " declarations and function-definitions is forbidden");
13966 /* Otherwise, we're done with the list of declarators. */
13969 pop_deferring_access_checks ();
13973 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13974 *maybe_range_for_decl
= decl
;
13975 /* The next token should be either a `,' or a `;'. */
13976 token
= cp_lexer_peek_token (parser
->lexer
);
13977 /* If it's a `,', there are more declarators to come. */
13978 if (token
->type
== CPP_COMMA
)
13979 /* will be consumed next time around */;
13980 /* If it's a `;', we are done. */
13981 else if (token
->type
== CPP_SEMICOLON
)
13983 else if (maybe_range_for_decl
)
13985 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13986 permerror (decl_specifiers
.locations
[ds_type_spec
],
13987 "types may not be defined in a for-range-declaration");
13990 /* Anything else is an error. */
13993 /* If we have already issued an error message we don't need
13994 to issue another one. */
13995 if ((decl
!= error_mark_node
13996 && DECL_INITIAL (decl
) != error_mark_node
)
13997 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13998 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13999 /* Skip tokens until we reach the end of the statement. */
14000 cp_parser_skip_to_end_of_statement (parser
);
14001 /* If the next token is now a `;', consume it. */
14002 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14003 cp_lexer_consume_token (parser
->lexer
);
14006 /* After the first time around, a function-definition is not
14007 allowed -- even if it was OK at first. For example:
14012 function_definition_allowed_p
= false;
14015 /* Issue an error message if no declarators are present, and the
14016 decl-specifier-seq does not itself declare a class or
14017 enumeration: [dcl.dcl]/3. */
14018 if (!saw_declarator
)
14020 if (cp_parser_declares_only_class_p (parser
))
14022 if (!declares_class_or_enum
14023 && decl_specifiers
.type
14024 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
14025 /* Ensure an error is issued anyway when finish_decltype_type,
14026 called via cp_parser_decl_specifier_seq, returns a class or
14027 an enumeration (c++/51786). */
14028 decl_specifiers
.type
= NULL_TREE
;
14029 shadow_tag (&decl_specifiers
);
14031 /* Perform any deferred access checks. */
14032 perform_deferred_access_checks (tf_warning_or_error
);
14035 /* Consume the `;'. */
14037 if (!maybe_range_for_decl
)
14038 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14039 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14041 if (init_loc
!= UNKNOWN_LOCATION
)
14042 error_at (init_loc
, "initializer in range-based %<for%> loop");
14043 if (comma_loc
!= UNKNOWN_LOCATION
)
14044 error_at (comma_loc
,
14045 "multiple declarations in range-based %<for%> loop");
14049 pop_deferring_access_checks ();
14052 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
14053 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
14057 cp_parser_decomposition_declaration (cp_parser
*parser
,
14058 cp_decl_specifier_seq
*decl_specifiers
,
14059 tree
*maybe_range_for_decl
,
14060 location_t
*init_loc
)
14062 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
14063 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14064 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
14066 /* Parse the identifier-list. */
14067 auto_vec
<cp_expr
, 10> v
;
14068 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
14071 cp_expr e
= cp_parser_identifier (parser
);
14072 if (e
.get_value () == error_mark_node
)
14075 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14077 cp_lexer_consume_token (parser
->lexer
);
14080 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14081 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14083 end_loc
= UNKNOWN_LOCATION
;
14084 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
14086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
14087 cp_lexer_consume_token (parser
->lexer
);
14090 cp_parser_skip_to_end_of_statement (parser
);
14091 return error_mark_node
;
14095 if (cxx_dialect
< cxx17
)
14096 pedwarn (loc
, 0, "structured bindings only available with "
14097 "%<-std=c++17%> or %<-std=gnu++17%>");
14100 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
14101 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
14102 declarator
->id_loc
= loc
;
14103 if (ref_qual
!= REF_QUAL_NONE
)
14104 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
14105 ref_qual
== REF_QUAL_RVALUE
,
14107 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
14108 NULL_TREE
, decl_specifiers
->attributes
,
14110 tree orig_decl
= decl
;
14114 cp_decl_specifier_seq decl_specs
;
14115 clear_decl_specs (&decl_specs
);
14116 decl_specs
.type
= make_auto ();
14118 FOR_EACH_VEC_ELT (v
, i
, e
)
14121 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
14122 sfk_none
, e
.get_location ());
14125 declarator
->u
.id
.unqualified_name
= e
.get_value ();
14126 declarator
->id_loc
= e
.get_location ();
14128 tree elt_pushed_scope
;
14129 tree decl2
= start_decl (declarator
, &decl_specs
, SD_DECOMPOSITION
,
14130 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
14131 if (decl2
== error_mark_node
)
14132 decl
= error_mark_node
;
14133 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
14135 /* Ensure we've diagnosed redeclaration if we aren't creating
14137 gcc_assert (errorcount
);
14138 decl
= error_mark_node
;
14142 if (elt_pushed_scope
)
14143 pop_scope (elt_pushed_scope
);
14148 error_at (loc
, "empty structured binding declaration");
14149 decl
= error_mark_node
;
14152 if (maybe_range_for_decl
== NULL
14153 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14155 bool non_constant_p
= false, is_direct_init
= false;
14156 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14157 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
14159 if (initializer
== NULL_TREE
14160 || (TREE_CODE (initializer
) == TREE_LIST
14161 && TREE_CHAIN (initializer
))
14163 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
14164 && CONSTRUCTOR_NELTS (initializer
) != 1))
14166 error_at (loc
, "invalid initializer for structured binding "
14168 initializer
= error_mark_node
;
14171 if (decl
!= error_mark_node
)
14173 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
14174 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
14175 (is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
14176 cp_finish_decomp (decl
, prev
, v
.length ());
14179 else if (decl
!= error_mark_node
)
14181 *maybe_range_for_decl
= prev
;
14182 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14183 the underlying DECL. */
14184 cp_finish_decomp (decl
, prev
, v
.length ());
14188 pop_scope (pushed_scope
);
14190 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
14192 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
14193 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
14199 /* Parse a decl-specifier-seq.
14201 decl-specifier-seq:
14202 decl-specifier-seq [opt] decl-specifier
14203 decl-specifier attribute-specifier-seq [opt] (C++11)
14206 storage-class-specifier
14217 Concepts Extension:
14222 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14224 The parser flags FLAGS is used to control type-specifier parsing.
14226 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14229 1: one of the decl-specifiers is an elaborated-type-specifier
14230 (i.e., a type declaration)
14231 2: one of the decl-specifiers is an enum-specifier or a
14232 class-specifier (i.e., a type definition)
14237 cp_parser_decl_specifier_seq (cp_parser
* parser
,
14238 cp_parser_flags flags
,
14239 cp_decl_specifier_seq
*decl_specs
,
14240 int* declares_class_or_enum
)
14242 bool constructor_possible_p
= !parser
->in_declarator_p
;
14243 bool found_decl_spec
= false;
14244 cp_token
*start_token
= NULL
;
14247 /* Clear DECL_SPECS. */
14248 clear_decl_specs (decl_specs
);
14250 /* Assume no class or enumeration type is declared. */
14251 *declares_class_or_enum
= 0;
14253 /* Keep reading specifiers until there are no more to read. */
14256 bool constructor_p
;
14260 /* Peek at the next token. */
14261 token
= cp_lexer_peek_token (parser
->lexer
);
14263 /* Save the first token of the decl spec list for error
14266 start_token
= token
;
14267 /* Handle attributes. */
14268 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) == 0
14269 && cp_next_tokens_can_be_attribute_p (parser
))
14271 /* Parse the attributes. */
14272 tree attrs
= cp_parser_attributes_opt (parser
);
14274 /* In a sequence of declaration specifiers, c++11 attributes
14275 appertain to the type that precede them. In that case
14278 The attribute-specifier-seq affects the type only for
14279 the declaration it appears in, not other declarations
14280 involving the same type.
14282 But for now let's force the user to position the
14283 attribute either at the beginning of the declaration or
14284 after the declarator-id, which would clearly mean that it
14285 applies to the declarator. */
14286 if (cxx11_attribute_p (attrs
))
14288 if (!found_decl_spec
)
14289 /* The c++11 attribute is at the beginning of the
14290 declaration. It appertains to the entity being
14294 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
14296 /* This is an attribute following a
14297 class-specifier. */
14298 if (decl_specs
->type_definition_p
)
14299 warn_misplaced_attr_for_class_type (token
->location
,
14305 decl_specs
->std_attributes
14306 = attr_chainon (decl_specs
->std_attributes
, attrs
);
14307 if (decl_specs
->locations
[ds_std_attribute
] == 0)
14308 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
14314 decl_specs
->attributes
14315 = attr_chainon (decl_specs
->attributes
, attrs
);
14316 if (decl_specs
->locations
[ds_attribute
] == 0)
14317 decl_specs
->locations
[ds_attribute
] = token
->location
;
14320 /* Assume we will find a decl-specifier keyword. */
14321 found_decl_spec
= true;
14322 /* If the next token is an appropriate keyword, we can simply
14323 add it to the list. */
14324 switch (token
->keyword
)
14331 if (!at_class_scope_p ())
14333 gcc_rich_location
richloc (token
->location
);
14334 richloc
.add_fixit_remove ();
14335 error_at (&richloc
, "%<friend%> used outside of class");
14336 cp_lexer_purge_token (parser
->lexer
);
14341 /* Consume the token. */
14342 cp_lexer_consume_token (parser
->lexer
);
14346 case RID_CONSTEXPR
:
14348 cp_lexer_consume_token (parser
->lexer
);
14351 case RID_CONSTINIT
:
14353 cp_lexer_consume_token (parser
->lexer
);
14356 case RID_CONSTEVAL
:
14358 cp_lexer_consume_token (parser
->lexer
);
14363 cp_lexer_consume_token (parser
->lexer
);
14365 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14368 /* Warn for concept as a decl-specifier. We'll rewrite these as
14369 concept declarations later. */
14370 if (!flag_concepts_ts
)
14372 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
14373 if (next
->keyword
== RID_BOOL
)
14374 pedwarn (next
->location
, 0, "the %<bool%> keyword is not "
14375 "allowed in a C++20 concept definition");
14377 pedwarn (token
->location
, 0, "C++20 concept definition syntax "
14378 "is %<concept <name> = <expr>%>");
14381 /* In C++20 a concept definition is just 'concept name = expr;'
14382 Support that syntax as a TS extension by pretending we've seen
14383 the 'bool' specifier. */
14384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14385 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
)
14386 && !decl_specs
->any_type_specifiers_p
)
14388 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
14389 token
, /*type_definition*/false);
14390 decl_specs
->any_type_specifiers_p
= true;
14394 /* function-specifier:
14401 cp_parser_function_specifier_opt (parser
, decl_specs
);
14408 /* Consume the token. */
14409 cp_lexer_consume_token (parser
->lexer
);
14411 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14414 /* A constructor declarator cannot appear in a typedef. */
14415 constructor_possible_p
= false;
14416 /* The "typedef" keyword can only occur in a declaration; we
14417 may as well commit at this point. */
14418 cp_parser_commit_to_tentative_parse (parser
);
14420 if (decl_specs
->storage_class
!= sc_none
)
14421 decl_specs
->conflicting_specifiers_p
= true;
14424 /* storage-class-specifier:
14434 if (cxx_dialect
== cxx98
)
14436 /* Consume the token. */
14437 cp_lexer_consume_token (parser
->lexer
);
14439 /* Complain about `auto' as a storage specifier, if
14440 we're complaining about C++0x compatibility. */
14441 gcc_rich_location
richloc (token
->location
);
14442 richloc
.add_fixit_remove ();
14443 warning_at (&richloc
, OPT_Wc__11_compat
,
14444 "%<auto%> changes meaning in C++11; "
14445 "please remove it");
14447 /* Set the storage class anyway. */
14448 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14452 /* C++0x auto type-specifier. */
14453 found_decl_spec
= false;
14460 /* Consume the token. */
14461 cp_lexer_consume_token (parser
->lexer
);
14462 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14466 /* Consume the token. */
14468 cp_lexer_consume_token (parser
->lexer
);
14472 /* We did not yet find a decl-specifier yet. */
14473 found_decl_spec
= false;
14477 if (found_decl_spec
14478 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14479 && token
->keyword
!= RID_CONSTEXPR
)
14480 error ("%<decl-specifier%> invalid in condition");
14482 if (found_decl_spec
14483 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14484 && token
->keyword
!= RID_MUTABLE
14485 && token
->keyword
!= RID_CONSTEXPR
14486 && token
->keyword
!= RID_CONSTEVAL
)
14487 error_at (token
->location
, "%qD invalid in lambda",
14488 ridpointers
[token
->keyword
]);
14491 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14493 /* Constructors are a special case. The `S' in `S()' is not a
14494 decl-specifier; it is the beginning of the declarator. */
14496 = (!found_decl_spec
14497 && constructor_possible_p
14498 && (cp_parser_constructor_declarator_p
14499 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14502 /* If we don't have a DECL_SPEC yet, then we must be looking at
14503 a type-specifier. */
14504 if (!found_decl_spec
&& !constructor_p
)
14506 int decl_spec_declares_class_or_enum
;
14507 bool is_cv_qualifier
;
14510 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14511 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
14514 = cp_parser_type_specifier (parser
, flags
,
14516 /*is_declaration=*/true,
14517 &decl_spec_declares_class_or_enum
,
14519 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14521 /* If this type-specifier referenced a user-defined type
14522 (a typedef, class-name, etc.), then we can't allow any
14523 more such type-specifiers henceforth.
14527 The longest sequence of decl-specifiers that could
14528 possibly be a type name is taken as the
14529 decl-specifier-seq of a declaration. The sequence shall
14530 be self-consistent as described below.
14534 As a general rule, at most one type-specifier is allowed
14535 in the complete decl-specifier-seq of a declaration. The
14536 only exceptions are the following:
14538 -- const or volatile can be combined with any other
14541 -- signed or unsigned can be combined with char, long,
14549 void g (const int Pc);
14551 Here, Pc is *not* part of the decl-specifier seq; it's
14552 the declarator. Therefore, once we see a type-specifier
14553 (other than a cv-qualifier), we forbid any additional
14554 user-defined types. We *do* still allow things like `int
14555 int' to be considered a decl-specifier-seq, and issue the
14556 error message later. */
14557 if (type_spec
&& !is_cv_qualifier
)
14558 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14559 /* A constructor declarator cannot follow a type-specifier. */
14562 constructor_possible_p
= false;
14563 found_decl_spec
= true;
14564 if (!is_cv_qualifier
)
14565 decl_specs
->any_type_specifiers_p
= true;
14567 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14568 error_at (token
->location
, "type-specifier invalid in lambda");
14572 /* If we still do not have a DECL_SPEC, then there are no more
14573 decl-specifiers. */
14574 if (!found_decl_spec
)
14577 decl_specs
->any_specifiers_p
= true;
14578 /* After we see one decl-specifier, further decl-specifiers are
14579 always optional. */
14580 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14583 /* Don't allow a friend specifier with a class definition. */
14584 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14585 && (*declares_class_or_enum
& 2))
14586 error_at (decl_specs
->locations
[ds_friend
],
14587 "class definition may not be declared a friend");
14590 /* Parse an (optional) storage-class-specifier.
14592 storage-class-specifier:
14601 storage-class-specifier:
14604 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14607 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14609 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14612 if (cxx_dialect
!= cxx98
)
14614 /* Fall through for C++98. */
14615 gcc_fallthrough ();
14622 /* Consume the token. */
14623 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14630 /* Parse an (optional) function-specifier.
14632 function-specifier:
14638 explicit(constant-expression)
14640 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14641 Updates DECL_SPECS, if it is non-NULL. */
14644 cp_parser_function_specifier_opt (cp_parser
* parser
,
14645 cp_decl_specifier_seq
*decl_specs
)
14647 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14648 switch (token
->keyword
)
14651 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14655 /* 14.5.2.3 [temp.mem]
14657 A member function template shall not be virtual. */
14658 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14659 && current_class_type
)
14660 error_at (token
->location
, "templates may not be %<virtual%>");
14662 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14667 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14668 /* If we see '(', it's C++20 explicit(bool). */
14670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14672 matching_parens parens
;
14673 parens
.consume_open (parser
);
14675 /* New types are not allowed in an explicit-specifier. */
14676 const char *saved_message
14677 = parser
->type_definition_forbidden_message
;
14678 parser
->type_definition_forbidden_message
14679 = G_("types may not be defined in explicit-specifier");
14681 if (cxx_dialect
< cxx20
)
14682 pedwarn (token
->location
, 0,
14683 "%<explicit(bool)%> only available with %<-std=c++20%> "
14684 "or %<-std=gnu++20%>");
14686 /* Parse the constant-expression. */
14687 expr
= cp_parser_constant_expression (parser
);
14689 /* Restore the saved message. */
14690 parser
->type_definition_forbidden_message
= saved_message
;
14691 parens
.require_close (parser
);
14694 /* The explicit-specifier explicit without a constant-expression is
14695 equivalent to the explicit-specifier explicit(true). */
14696 expr
= boolean_true_node
;
14699 "the constant-expression, if supplied, shall be a contextually
14700 converted constant expression of type bool." */
14701 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14702 /* We could evaluate it -- mark the decl as appropriate. */
14703 if (expr
== boolean_true_node
)
14704 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14705 else if (expr
== boolean_false_node
)
14706 /* Don't mark the decl as explicit. */;
14707 else if (decl_specs
)
14708 /* The expression was value-dependent. Remember it so that we can
14709 substitute it later. */
14710 decl_specs
->explicit_specifier
= expr
;
14718 /* Consume the token. */
14719 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14722 /* Parse a linkage-specification.
14724 linkage-specification:
14725 extern string-literal { declaration-seq [opt] }
14726 extern string-literal declaration */
14729 cp_parser_linkage_specification (cp_parser
* parser
)
14733 /* Look for the `extern' keyword. */
14734 cp_token
*extern_token
14735 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14737 /* Look for the string-literal. */
14738 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14739 linkage
= cp_parser_string_literal (parser
, false, false);
14741 /* Transform the literal into an identifier. If the literal is a
14742 wide-character string, or contains embedded NULs, then we can't
14743 handle it as the user wants. */
14744 if (strlen (TREE_STRING_POINTER (linkage
))
14745 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14747 cp_parser_error (parser
, "invalid linkage-specification");
14748 /* Assume C++ linkage. */
14749 linkage
= lang_name_cplusplus
;
14752 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14754 /* We're now using the new linkage. */
14755 push_lang_context (linkage
);
14757 /* Preserve the location of the innermost linkage specification,
14758 tracking the locations of nested specifications via a local. */
14759 location_t saved_location
14760 = parser
->innermost_linkage_specification_location
;
14761 /* Construct a location ranging from the start of the "extern" to
14762 the end of the string-literal, with the caret at the start, e.g.:
14766 parser
->innermost_linkage_specification_location
14767 = make_location (extern_token
->location
,
14768 extern_token
->location
,
14769 get_finish (string_token
->location
));
14771 /* If the next token is a `{', then we're using the first
14773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14775 cp_ensure_no_omp_declare_simd (parser
);
14776 cp_ensure_no_oacc_routine (parser
);
14778 /* Consume the `{' token. */
14779 matching_braces braces
;
14780 braces
.consume_open (parser
);
14781 /* Parse the declarations. */
14782 cp_parser_declaration_seq_opt (parser
);
14783 /* Look for the closing `}'. */
14784 braces
.require_close (parser
);
14786 /* Otherwise, there's just one declaration. */
14789 bool saved_in_unbraced_linkage_specification_p
;
14791 saved_in_unbraced_linkage_specification_p
14792 = parser
->in_unbraced_linkage_specification_p
;
14793 parser
->in_unbraced_linkage_specification_p
= true;
14794 cp_parser_declaration (parser
);
14795 parser
->in_unbraced_linkage_specification_p
14796 = saved_in_unbraced_linkage_specification_p
;
14799 /* We're done with the linkage-specification. */
14800 pop_lang_context ();
14802 /* Restore location of parent linkage specification, if any. */
14803 parser
->innermost_linkage_specification_location
= saved_location
;
14806 /* Parse a static_assert-declaration.
14808 static_assert-declaration:
14809 static_assert ( constant-expression , string-literal ) ;
14810 static_assert ( constant-expression ) ; (C++17)
14812 If MEMBER_P, this static_assert is a class member. */
14815 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14818 location_t token_loc
;
14822 /* Peek at the `static_assert' token so we can keep track of exactly
14823 where the static assertion started. */
14824 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14826 /* Look for the `static_assert' keyword. */
14827 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14831 /* We know we are in a static assertion; commit to any tentative
14833 if (cp_parser_parsing_tentatively (parser
))
14834 cp_parser_commit_to_tentative_parse (parser
);
14836 /* Parse the `(' starting the static assertion condition. */
14837 matching_parens parens
;
14838 parens
.require_open (parser
);
14840 /* Parse the constant-expression. Allow a non-constant expression
14841 here in order to give better diagnostics in finish_static_assert. */
14843 cp_parser_constant_expression (parser
,
14844 /*allow_non_constant_p=*/true,
14845 /*non_constant_p=*/&dummy
);
14847 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14849 if (cxx_dialect
< cxx17
)
14850 pedwarn (input_location
, OPT_Wpedantic
,
14851 "%<static_assert%> without a message "
14852 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14854 cp_lexer_consume_token (parser
->lexer
);
14855 message
= build_string (1, "");
14856 TREE_TYPE (message
) = char_array_type_node
;
14857 fix_string_type (message
);
14861 /* Parse the separating `,'. */
14862 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14864 /* Parse the string-literal message. */
14865 message
= cp_parser_string_literal (parser
,
14866 /*translate=*/false,
14869 /* A `)' completes the static assertion. */
14870 if (!parens
.require_close (parser
))
14871 cp_parser_skip_to_closing_parenthesis (parser
,
14872 /*recovering=*/true,
14873 /*or_comma=*/false,
14874 /*consume_paren=*/true);
14877 /* A semicolon terminates the declaration. */
14878 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14880 /* Get the location for the static assertion. Use that of the
14881 condition if available, otherwise, use that of the "static_assert"
14883 location_t assert_loc
= condition
.get_location ();
14884 if (assert_loc
== UNKNOWN_LOCATION
)
14885 assert_loc
= token_loc
;
14887 /* Complete the static assertion, which may mean either processing
14888 the static assert now or saving it for template instantiation. */
14889 finish_static_assert (condition
, message
, assert_loc
, member_p
,
14890 /*show_expr_p=*/false);
14893 /* Parse the expression in decltype ( expression ). */
14896 cp_parser_decltype_expr (cp_parser
*parser
,
14897 bool &id_expression_or_member_access_p
)
14899 cp_token
*id_expr_start_token
;
14902 /* First, try parsing an id-expression. */
14903 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14904 cp_parser_parse_tentatively (parser
);
14905 expr
= cp_parser_id_expression (parser
,
14906 /*template_keyword_p=*/false,
14907 /*check_dependency_p=*/true,
14908 /*template_p=*/NULL
,
14909 /*declarator_p=*/false,
14910 /*optional_p=*/false);
14912 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14914 bool non_integral_constant_expression_p
= false;
14915 tree id_expression
= expr
;
14917 const char *error_msg
;
14919 if (identifier_p (expr
))
14920 /* Lookup the name we got back from the id-expression. */
14921 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14922 id_expr_start_token
->location
);
14924 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14925 /* A template without args is not a complete id-expression. */
14926 expr
= error_mark_node
;
14929 && expr
!= error_mark_node
14930 && TREE_CODE (expr
) != TYPE_DECL
14931 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14932 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14933 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14935 /* Complete lookup of the id-expression. */
14936 expr
= (finish_id_expression
14937 (id_expression
, expr
, parser
->scope
, &idk
,
14938 /*integral_constant_expression_p=*/false,
14939 /*allow_non_integral_constant_expression_p=*/true,
14940 &non_integral_constant_expression_p
,
14941 /*template_p=*/false,
14943 /*address_p=*/false,
14944 /*template_arg_p=*/false,
14946 id_expr_start_token
->location
));
14948 if (expr
== error_mark_node
)
14949 /* We found an id-expression, but it was something that we
14950 should not have found. This is an error, not something
14951 we can recover from, so note that we found an
14952 id-expression and we'll recover as gracefully as
14954 id_expression_or_member_access_p
= true;
14958 && expr
!= error_mark_node
14959 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14960 /* We have an id-expression. */
14961 id_expression_or_member_access_p
= true;
14964 if (!id_expression_or_member_access_p
)
14966 /* Abort the id-expression parse. */
14967 cp_parser_abort_tentative_parse (parser
);
14969 /* Parsing tentatively, again. */
14970 cp_parser_parse_tentatively (parser
);
14972 /* Parse a class member access. */
14973 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14974 /*cast_p=*/false, /*decltype*/true,
14975 /*member_access_only_p=*/true, NULL
);
14978 && expr
!= error_mark_node
14979 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14980 /* We have an id-expression. */
14981 id_expression_or_member_access_p
= true;
14984 if (id_expression_or_member_access_p
)
14985 /* We have parsed the complete id-expression or member access. */
14986 cp_parser_parse_definitely (parser
);
14989 /* Abort our attempt to parse an id-expression or member access
14991 cp_parser_abort_tentative_parse (parser
);
14993 /* Parse a full expression. */
14994 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14995 /*decltype_p=*/true);
15001 /* Parse a `decltype' type. Returns the type.
15003 decltype-specifier:
15004 decltype ( expression )
15006 decltype ( auto ) */
15009 cp_parser_decltype (cp_parser
*parser
)
15011 bool id_expression_or_member_access_p
= false;
15012 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
15014 if (start_token
->type
== CPP_DECLTYPE
)
15016 /* Already parsed. */
15017 cp_lexer_consume_token (parser
->lexer
);
15018 return saved_checks_value (start_token
->u
.tree_check_value
);
15021 /* Look for the `decltype' token. */
15022 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
15023 return error_mark_node
;
15025 /* Parse the opening `('. */
15026 matching_parens parens
;
15027 if (!parens
.require_open (parser
))
15028 return error_mark_node
;
15030 /* Since we're going to preserve any side-effects from this parse, set up a
15031 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15032 in the expression. */
15033 tentative_firewall
firewall (parser
);
15035 /* If in_declarator_p, a reparse as an expression might succeed (60361).
15036 Otherwise, commit now for better diagnostics. */
15037 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
15038 && !parser
->in_declarator_p
)
15039 cp_parser_commit_to_topmost_tentative_parse (parser
);
15041 push_deferring_access_checks (dk_deferred
);
15043 tree expr
= NULL_TREE
;
15045 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
)
15046 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
15048 /* decltype (auto) */
15049 cp_lexer_consume_token (parser
->lexer
);
15050 if (cxx_dialect
< cxx14
)
15052 error_at (start_token
->location
,
15053 "%<decltype(auto)%> type specifier only available with "
15054 "%<-std=c++14%> or %<-std=gnu++14%>");
15055 expr
= error_mark_node
;
15060 /* decltype (expression) */
15062 /* Types cannot be defined in a `decltype' expression. Save away the
15063 old message and set the new one. */
15064 const char *saved_message
= parser
->type_definition_forbidden_message
;
15065 parser
->type_definition_forbidden_message
15066 = G_("types may not be defined in %<decltype%> expressions");
15068 /* The restrictions on constant-expressions do not apply inside
15069 decltype expressions. */
15070 bool saved_integral_constant_expression_p
15071 = parser
->integral_constant_expression_p
;
15072 bool saved_non_integral_constant_expression_p
15073 = parser
->non_integral_constant_expression_p
;
15074 parser
->integral_constant_expression_p
= false;
15076 /* Within a parenthesized expression, a `>' token is always
15077 the greater-than operator. */
15078 bool saved_greater_than_is_operator_p
15079 = parser
->greater_than_is_operator_p
;
15080 parser
->greater_than_is_operator_p
= true;
15082 /* Do not actually evaluate the expression. */
15083 ++cp_unevaluated_operand
;
15085 /* Do not warn about problems with the expression. */
15086 ++c_inhibit_evaluation_warnings
;
15088 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
15089 STRIP_ANY_LOCATION_WRAPPER (expr
);
15091 /* Go back to evaluating expressions. */
15092 --cp_unevaluated_operand
;
15093 --c_inhibit_evaluation_warnings
;
15095 /* The `>' token might be the end of a template-id or
15096 template-parameter-list now. */
15097 parser
->greater_than_is_operator_p
15098 = saved_greater_than_is_operator_p
;
15100 /* Restore the old message and the integral constant expression
15102 parser
->type_definition_forbidden_message
= saved_message
;
15103 parser
->integral_constant_expression_p
15104 = saved_integral_constant_expression_p
;
15105 parser
->non_integral_constant_expression_p
15106 = saved_non_integral_constant_expression_p
;
15109 /* Parse to the closing `)'. */
15110 if (expr
== error_mark_node
|| !parens
.require_close (parser
))
15112 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15113 /*consume_paren=*/true);
15114 expr
= error_mark_node
;
15117 /* If we got a parse error while tentative, bail out now. */
15118 if (cp_parser_error_occurred (parser
))
15120 pop_deferring_access_checks ();
15121 return error_mark_node
;
15126 expr
= make_decltype_auto ();
15128 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
15129 tf_warning_or_error
);
15131 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15133 start_token
->type
= CPP_DECLTYPE
;
15134 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15135 start_token
->tree_check_p
= true;
15136 start_token
->u
.tree_check_value
->value
= expr
;
15137 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15138 start_token
->keyword
= RID_MAX
;
15140 location_t loc
= start_token
->location
;
15141 loc
= make_location (loc
, loc
, parser
->lexer
);
15142 start_token
->location
= loc
;
15144 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
15146 pop_to_parent_deferring_access_checks ();
15151 /* Special member functions [gram.special] */
15153 /* Parse a conversion-function-id.
15155 conversion-function-id:
15156 operator conversion-type-id
15158 Returns an IDENTIFIER_NODE representing the operator. */
15161 cp_parser_conversion_function_id (cp_parser
* parser
)
15165 tree saved_qualifying_scope
;
15166 tree saved_object_scope
;
15167 tree pushed_scope
= NULL_TREE
;
15169 /* Look for the `operator' token. */
15170 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15171 return error_mark_node
;
15172 /* When we parse the conversion-type-id, the current scope will be
15173 reset. However, we need that information in able to look up the
15174 conversion function later, so we save it here. */
15175 saved_scope
= parser
->scope
;
15176 saved_qualifying_scope
= parser
->qualifying_scope
;
15177 saved_object_scope
= parser
->object_scope
;
15178 /* We must enter the scope of the class so that the names of
15179 entities declared within the class are available in the
15180 conversion-type-id. For example, consider:
15187 S::operator I() { ... }
15189 In order to see that `I' is a type-name in the definition, we
15190 must be in the scope of `S'. */
15192 pushed_scope
= push_scope (saved_scope
);
15193 /* Parse the conversion-type-id. */
15194 type
= cp_parser_conversion_type_id (parser
);
15195 /* Leave the scope of the class, if any. */
15197 pop_scope (pushed_scope
);
15198 /* Restore the saved scope. */
15199 parser
->scope
= saved_scope
;
15200 parser
->qualifying_scope
= saved_qualifying_scope
;
15201 parser
->object_scope
= saved_object_scope
;
15202 /* If the TYPE is invalid, indicate failure. */
15203 if (type
== error_mark_node
)
15204 return error_mark_node
;
15205 return make_conv_op_name (type
);
15208 /* Parse a conversion-type-id:
15210 conversion-type-id:
15211 type-specifier-seq conversion-declarator [opt]
15213 Returns the TYPE specified. */
15216 cp_parser_conversion_type_id (cp_parser
* parser
)
15219 cp_decl_specifier_seq type_specifiers
;
15220 cp_declarator
*declarator
;
15221 tree type_specified
;
15222 const char *saved_message
;
15224 /* Parse the attributes. */
15225 attributes
= cp_parser_attributes_opt (parser
);
15227 saved_message
= parser
->type_definition_forbidden_message
;
15228 parser
->type_definition_forbidden_message
15229 = G_("types may not be defined in a conversion-type-id");
15231 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15232 optional in conversion-type-id. */
15233 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15234 /*is_declaration=*/false,
15235 /*is_trailing_return=*/false,
15238 parser
->type_definition_forbidden_message
= saved_message
;
15240 /* If that didn't work, stop. */
15241 if (type_specifiers
.type
== error_mark_node
)
15242 return error_mark_node
;
15243 /* Parse the conversion-declarator. */
15244 declarator
= cp_parser_conversion_declarator_opt (parser
);
15246 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
15247 /*initialized=*/0, &attributes
);
15249 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
15251 /* Don't give this error when parsing tentatively. This happens to
15252 work because we always parse this definitively once. */
15253 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
15254 && type_uses_auto (type_specified
))
15256 if (cxx_dialect
< cxx14
)
15258 error ("invalid use of %<auto%> in conversion operator");
15259 return error_mark_node
;
15261 else if (template_parm_scope_p ())
15262 warning (0, "use of %<auto%> in member template "
15263 "conversion operator can never be deduced");
15266 return type_specified
;
15269 /* Parse an (optional) conversion-declarator.
15271 conversion-declarator:
15272 ptr-operator conversion-declarator [opt]
15276 static cp_declarator
*
15277 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
15279 enum tree_code code
;
15280 tree class_type
, std_attributes
= NULL_TREE
;
15281 cp_cv_quals cv_quals
;
15283 /* We don't know if there's a ptr-operator next, or not. */
15284 cp_parser_parse_tentatively (parser
);
15285 /* Try the ptr-operator. */
15286 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
15288 /* If it worked, look for more conversion-declarators. */
15289 if (cp_parser_parse_definitely (parser
))
15291 cp_declarator
*declarator
;
15293 /* Parse another optional declarator. */
15294 declarator
= cp_parser_conversion_declarator_opt (parser
);
15296 declarator
= cp_parser_make_indirect_declarator
15297 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
15305 /* Parse an (optional) ctor-initializer.
15308 : mem-initializer-list */
15311 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
15313 /* If the next token is not a `:', then there is no
15314 ctor-initializer. */
15315 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
15317 /* Do default initialization of any bases and members. */
15318 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15319 finish_mem_initializers (NULL_TREE
);
15323 /* Consume the `:' token. */
15324 cp_lexer_consume_token (parser
->lexer
);
15325 /* And the mem-initializer-list. */
15326 cp_parser_mem_initializer_list (parser
);
15329 /* Parse a mem-initializer-list.
15331 mem-initializer-list:
15332 mem-initializer ... [opt]
15333 mem-initializer ... [opt] , mem-initializer-list */
15336 cp_parser_mem_initializer_list (cp_parser
* parser
)
15338 tree mem_initializer_list
= NULL_TREE
;
15339 tree target_ctor
= error_mark_node
;
15340 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15342 /* Let the semantic analysis code know that we are starting the
15343 mem-initializer-list. */
15344 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
15345 error_at (token
->location
,
15346 "only constructors take member initializers");
15348 /* Loop through the list. */
15351 tree mem_initializer
;
15353 token
= cp_lexer_peek_token (parser
->lexer
);
15354 /* Parse the mem-initializer. */
15355 mem_initializer
= cp_parser_mem_initializer (parser
);
15356 /* If the next token is a `...', we're expanding member initializers. */
15357 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15359 || (mem_initializer
!= error_mark_node
15360 && check_for_bare_parameter_packs (TREE_PURPOSE
15361 (mem_initializer
))))
15363 /* Consume the `...'. */
15365 cp_lexer_consume_token (parser
->lexer
);
15367 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15368 can be expanded but members cannot. */
15369 if (mem_initializer
!= error_mark_node
15370 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
15372 error_at (token
->location
,
15373 "cannot expand initializer for member %qD",
15374 TREE_PURPOSE (mem_initializer
));
15375 mem_initializer
= error_mark_node
;
15378 /* Construct the pack expansion type. */
15379 if (mem_initializer
!= error_mark_node
)
15380 mem_initializer
= make_pack_expansion (mem_initializer
);
15382 if (target_ctor
!= error_mark_node
15383 && mem_initializer
!= error_mark_node
)
15385 error ("mem-initializer for %qD follows constructor delegation",
15386 TREE_PURPOSE (mem_initializer
));
15387 mem_initializer
= error_mark_node
;
15389 /* Look for a target constructor. */
15390 if (mem_initializer
!= error_mark_node
15391 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
15392 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
15394 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
15395 if (mem_initializer_list
)
15397 error ("constructor delegation follows mem-initializer for %qD",
15398 TREE_PURPOSE (mem_initializer_list
));
15399 mem_initializer
= error_mark_node
;
15401 target_ctor
= mem_initializer
;
15403 /* Add it to the list, unless it was erroneous. */
15404 if (mem_initializer
!= error_mark_node
)
15406 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
15407 mem_initializer_list
= mem_initializer
;
15409 /* If the next token is not a `,', we're done. */
15410 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15412 /* Consume the `,' token. */
15413 cp_lexer_consume_token (parser
->lexer
);
15416 /* Perform semantic analysis. */
15417 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15418 finish_mem_initializers (mem_initializer_list
);
15421 /* Parse a mem-initializer.
15424 mem-initializer-id ( expression-list [opt] )
15425 mem-initializer-id braced-init-list
15430 ( expression-list [opt] )
15432 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15433 class) or FIELD_DECL (for a non-static data member) to initialize;
15434 the TREE_VALUE is the expression-list. An empty initialization
15435 list is represented by void_list_node. */
15438 cp_parser_mem_initializer (cp_parser
* parser
)
15440 tree mem_initializer_id
;
15441 tree expression_list
;
15443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15445 /* Find out what is being initialized. */
15446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15448 permerror (token
->location
,
15449 "anachronistic old-style base class initializer");
15450 mem_initializer_id
= NULL_TREE
;
15454 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15455 if (mem_initializer_id
== error_mark_node
)
15456 return mem_initializer_id
;
15458 member
= expand_member_init (mem_initializer_id
);
15459 if (member
&& !DECL_P (member
))
15460 in_base_initializer
= 1;
15462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15464 bool expr_non_constant_p
;
15465 cp_lexer_set_source_position (parser
->lexer
);
15466 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15467 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15468 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15469 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15473 vec
<tree
, va_gc
> *vec
;
15474 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15476 /*allow_expansion_p=*/true,
15477 /*non_constant_p=*/NULL
,
15478 /*close_paren_loc=*/NULL
,
15479 /*wrap_locations_p=*/true);
15481 return error_mark_node
;
15482 expression_list
= build_tree_list_vec (vec
);
15483 release_tree_vector (vec
);
15486 if (expression_list
== error_mark_node
)
15487 return error_mark_node
;
15488 if (!expression_list
)
15489 expression_list
= void_type_node
;
15491 in_base_initializer
= 0;
15494 return error_mark_node
;
15495 tree node
= build_tree_list (member
, expression_list
);
15497 /* We can't attach the source location of this initializer directly to
15498 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
15499 within the TREE_TYPE of the list node. */
15501 = make_location (token
->location
, token
->location
, parser
->lexer
);
15502 tree dummy
= build0 (EMPTY_CLASS_EXPR
, NULL_TREE
);
15503 SET_EXPR_LOCATION (dummy
, loc
);
15504 TREE_TYPE (node
) = dummy
;
15509 /* Parse a mem-initializer-id.
15511 mem-initializer-id:
15512 :: [opt] nested-name-specifier [opt] class-name
15513 decltype-specifier (C++11)
15516 Returns a TYPE indicating the class to be initialized for the first
15517 production (and the second in C++11). Returns an IDENTIFIER_NODE
15518 indicating the data member to be initialized for the last production. */
15521 cp_parser_mem_initializer_id (cp_parser
* parser
)
15523 bool global_scope_p
;
15524 bool nested_name_specifier_p
;
15525 bool template_p
= false;
15528 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15530 /* `typename' is not allowed in this context ([temp.res]). */
15531 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15533 error_at (token
->location
,
15534 "keyword %<typename%> not allowed in this context (a qualified "
15535 "member initializer is implicitly a type)");
15536 cp_lexer_consume_token (parser
->lexer
);
15538 /* Look for the optional `::' operator. */
15540 = (cp_parser_global_scope_opt (parser
,
15541 /*current_scope_valid_p=*/false)
15543 /* Look for the optional nested-name-specifier. The simplest way to
15548 The keyword `typename' is not permitted in a base-specifier or
15549 mem-initializer; in these contexts a qualified name that
15550 depends on a template-parameter is implicitly assumed to be a
15553 is to assume that we have seen the `typename' keyword at this
15555 nested_name_specifier_p
15556 = (cp_parser_nested_name_specifier_opt (parser
,
15557 /*typename_keyword_p=*/true,
15558 /*check_dependency_p=*/true,
15560 /*is_declaration=*/true)
15562 if (nested_name_specifier_p
)
15563 template_p
= cp_parser_optional_template_keyword (parser
);
15564 /* If there is a `::' operator or a nested-name-specifier, then we
15565 are definitely looking for a class-name. */
15566 if (global_scope_p
|| nested_name_specifier_p
)
15567 return cp_parser_class_name (parser
,
15568 /*typename_keyword_p=*/true,
15569 /*template_keyword_p=*/template_p
,
15571 /*check_dependency_p=*/true,
15572 /*class_head_p=*/false,
15573 /*is_declaration=*/true);
15574 /* Otherwise, we could also be looking for an ordinary identifier. */
15575 cp_parser_parse_tentatively (parser
);
15576 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15577 /* Try a decltype-specifier. */
15578 id
= cp_parser_decltype (parser
);
15580 /* Otherwise, try a class-name. */
15581 id
= cp_parser_class_name (parser
,
15582 /*typename_keyword_p=*/true,
15583 /*template_keyword_p=*/false,
15585 /*check_dependency_p=*/true,
15586 /*class_head_p=*/false,
15587 /*is_declaration=*/true);
15588 /* If we found one, we're done. */
15589 if (cp_parser_parse_definitely (parser
))
15591 /* Otherwise, look for an ordinary identifier. */
15592 return cp_parser_identifier (parser
);
15595 /* Overloading [gram.over] */
15597 /* Parse an operator-function-id.
15599 operator-function-id:
15602 Returns an IDENTIFIER_NODE for the operator which is a
15603 human-readable spelling of the identifier, e.g., `operator +'. */
15606 cp_parser_operator_function_id (cp_parser
* parser
)
15608 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15609 /* Look for the `operator' keyword. */
15610 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15611 return error_mark_node
;
15612 /* And then the name of the operator itself. */
15613 return cp_parser_operator (parser
, start_loc
);
15616 /* Return an identifier node for a user-defined literal operator.
15617 The suffix identifier is chained to the operator name identifier. */
15620 cp_literal_operator_id (const char* name
)
15623 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15624 + strlen (name
) + 10);
15625 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15626 identifier
= get_identifier (buffer
);
15627 XDELETEVEC (buffer
);
15632 /* Parse an operator.
15635 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15636 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15637 || ++ -- , ->* -> () []
15644 Returns an IDENTIFIER_NODE for the operator which is a
15645 human-readable spelling of the identifier, e.g., `operator +'. */
15648 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15650 tree id
= NULL_TREE
;
15654 /* Peek at the next token. */
15655 token
= cp_lexer_peek_token (parser
->lexer
);
15657 location_t end_loc
= token
->location
;
15659 /* Figure out which operator we have. */
15660 enum tree_code op
= ERROR_MARK
;
15661 bool assop
= false;
15662 bool consumed
= false;
15663 switch (token
->type
)
15667 /* The keyword should be either `new', `delete' or `co_await'. */
15668 if (token
->keyword
== RID_NEW
)
15670 else if (token
->keyword
== RID_DELETE
)
15672 else if (token
->keyword
== RID_CO_AWAIT
)
15673 op
= CO_AWAIT_EXPR
;
15677 /* Consume the `new', `delete' or co_await token. */
15678 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15680 /* Peek at the next token. */
15681 token
= cp_lexer_peek_token (parser
->lexer
);
15682 /* If it's a `[' token then this is the array variant of the
15684 if (token
->type
== CPP_OPEN_SQUARE
15685 && op
!= CO_AWAIT_EXPR
)
15687 /* Consume the `[' token. */
15688 cp_lexer_consume_token (parser
->lexer
);
15689 /* Look for the `]' token. */
15690 if (cp_token
*close_token
15691 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15692 end_loc
= close_token
->location
;
15693 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15712 op
= TRUNC_DIV_EXPR
;
15716 op
= TRUNC_MOD_EXPR
;
15736 op
= TRUTH_NOT_EXPR
;
15769 op
= TRUNC_DIV_EXPR
;
15774 op
= TRUNC_MOD_EXPR
;
15800 case CPP_LSHIFT_EQ
:
15805 case CPP_RSHIFT_EQ
:
15822 case CPP_GREATER_EQ
:
15826 case CPP_SPACESHIP
:
15827 op
= SPACESHIP_EXPR
;
15831 op
= TRUTH_ANDIF_EXPR
;
15835 op
= TRUTH_ORIF_EXPR
;
15838 case CPP_PLUS_PLUS
:
15839 op
= POSTINCREMENT_EXPR
;
15842 case CPP_MINUS_MINUS
:
15843 op
= PREDECREMENT_EXPR
;
15847 op
= COMPOUND_EXPR
;
15850 case CPP_DEREF_STAR
:
15855 op
= COMPONENT_REF
;
15860 /* Consume the `?'. */
15861 cp_lexer_consume_token (parser
->lexer
);
15862 /* Look for the matching `:'. */
15863 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
15867 case CPP_OPEN_PAREN
:
15869 /* Consume the `('. */
15870 matching_parens parens
;
15871 parens
.consume_open (parser
);
15872 /* Look for the matching `)'. */
15873 token
= parens
.require_close (parser
);
15875 end_loc
= token
->location
;
15881 case CPP_OPEN_SQUARE
:
15882 /* Consume the `['. */
15883 cp_lexer_consume_token (parser
->lexer
);
15884 /* Look for the matching `]'. */
15885 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15887 end_loc
= token
->location
;
15892 case CPP_UTF8STRING
:
15893 case CPP_UTF8STRING_USERDEF
:
15900 case CPP_STRING_USERDEF
:
15901 case CPP_WSTRING_USERDEF
:
15902 case CPP_STRING16_USERDEF
:
15903 case CPP_STRING32_USERDEF
:
15909 if (cxx_dialect
== cxx98
)
15910 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15912 /* Consume the string. */
15913 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15914 /*wide_ok=*/true, /*lookup_udlit=*/false);
15915 if (str
== error_mark_node
)
15916 return error_mark_node
;
15917 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15919 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15920 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15921 end_loc
= str
.get_location ();
15926 /* Look for the suffix identifier. */
15927 token
= cp_lexer_peek_token (parser
->lexer
);
15928 if (token
->type
== CPP_NAME
)
15930 id
= cp_parser_identifier (parser
);
15931 end_loc
= token
->location
;
15933 else if (token
->type
== CPP_KEYWORD
)
15935 error ("unexpected keyword;"
15936 " remove space between quotes and suffix identifier");
15937 return error_mark_node
;
15941 error ("expected suffix identifier");
15942 return error_mark_node
;
15945 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15946 (TREE_TYPE (TREE_TYPE (string_tree
))));
15947 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15950 error ("expected empty string after %<operator%> keyword");
15951 return error_mark_node
;
15953 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15956 error ("invalid encoding prefix in literal operator");
15957 return error_mark_node
;
15959 if (id
!= error_mark_node
)
15961 const char *name
= IDENTIFIER_POINTER (id
);
15962 id
= cp_literal_operator_id (name
);
15964 /* Generate a location of the form:
15965 "" _suffix_identifier
15966 ^~~~~~~~~~~~~~~~~~~~~
15967 with caret == start at the start token, finish at the end of the
15968 suffix identifier. */
15969 location_t combined_loc
15970 = make_location (start_loc
, start_loc
, parser
->lexer
);
15971 return cp_expr (id
, combined_loc
);
15975 /* Anything else is an error. */
15979 /* If we have selected an identifier, we need to consume the
15981 if (op
!= ERROR_MARK
)
15983 id
= ovl_op_identifier (assop
, op
);
15985 cp_lexer_consume_token (parser
->lexer
);
15987 /* Otherwise, no valid operator name was present. */
15990 cp_parser_error (parser
, "expected operator");
15991 id
= error_mark_node
;
15994 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15995 return cp_expr (id
, start_loc
);
15998 /* Parse a template-declaration.
16000 template-declaration:
16001 export [opt] template < template-parameter-list > declaration
16003 If MEMBER_P is TRUE, this template-declaration occurs within a
16006 The grammar rule given by the standard isn't correct. What
16007 is really meant is:
16009 template-declaration:
16010 export [opt] template-parameter-list-seq
16011 decl-specifier-seq [opt] init-declarator [opt] ;
16012 export [opt] template-parameter-list-seq
16013 function-definition
16015 template-parameter-list-seq:
16016 template-parameter-list-seq [opt]
16017 template < template-parameter-list >
16019 Concept Extensions:
16021 template-parameter-list-seq:
16022 template < template-parameter-list > requires-clause [opt]
16025 requires logical-or-expression */
16028 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
16030 /* Check for `export'. */
16031 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
16033 /* Consume the `export' token. */
16034 cp_lexer_consume_token (parser
->lexer
);
16035 /* Warn that this use of export is deprecated. */
16036 if (cxx_dialect
< cxx11
)
16037 warning (0, "keyword %<export%> not implemented, and will be ignored");
16038 else if (cxx_dialect
< cxx20
)
16039 warning (0, "keyword %<export%> is deprecated, and is ignored");
16041 warning (0, "keyword %<export%> not implemented, and will be ignored");
16044 cp_parser_template_declaration_after_export (parser
, member_p
);
16047 /* Parse a template-parameter-list.
16049 template-parameter-list:
16051 template-parameter-list , template-parameter
16053 Returns a TREE_LIST. Each node represents a template parameter.
16054 The nodes are connected via their TREE_CHAINs. */
16057 cp_parser_template_parameter_list (cp_parser
* parser
)
16059 tree parameter_list
= NULL_TREE
;
16061 /* Don't create wrapper nodes within a template-parameter-list,
16062 since we don't want to have different types based on the
16063 spelling location of constants and decls within them. */
16064 auto_suppress_location_wrappers sentinel
;
16066 begin_template_parm_list ();
16068 /* The loop below parses the template parms. We first need to know
16069 the total number of template parms to be able to compute proper
16070 canonical types of each dependent type. So after the loop, when
16071 we know the total number of template parms,
16072 end_template_parm_list computes the proper canonical types and
16073 fixes up the dependent types accordingly. */
16078 bool is_parameter_pack
;
16079 location_t parm_loc
;
16081 /* Parse the template-parameter. */
16082 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16083 parameter
= cp_parser_template_parameter (parser
,
16085 &is_parameter_pack
);
16086 /* Add it to the list. */
16087 if (parameter
!= error_mark_node
)
16088 parameter_list
= process_template_parm (parameter_list
,
16092 is_parameter_pack
);
16095 tree err_parm
= build_tree_list (parameter
, parameter
);
16096 parameter_list
= chainon (parameter_list
, err_parm
);
16099 /* If the next token is not a `,', we're done. */
16100 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16102 /* Otherwise, consume the `,' token. */
16103 cp_lexer_consume_token (parser
->lexer
);
16106 return end_template_parm_list (parameter_list
);
16109 /* Parse a introduction-list.
16112 introduced-parameter
16113 introduction-list , introduced-parameter
16115 introduced-parameter:
16116 ...[opt] identifier
16118 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
16119 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
16120 WILDCARD_DECL will also have DECL_NAME set and token location in
16121 DECL_SOURCE_LOCATION. */
16124 cp_parser_introduction_list (cp_parser
*parser
)
16126 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
16130 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
16132 cp_lexer_consume_token (parser
->lexer
);
16134 tree identifier
= cp_parser_identifier (parser
);
16135 if (identifier
== error_mark_node
)
16138 /* Build placeholder. */
16139 tree parm
= build_nt (WILDCARD_DECL
);
16140 DECL_SOURCE_LOCATION (parm
)
16141 = cp_lexer_peek_token (parser
->lexer
)->location
;
16142 DECL_NAME (parm
) = identifier
;
16143 WILDCARD_PACK_P (parm
) = is_pack
;
16144 vec_safe_push (introduction_vec
, parm
);
16146 /* If the next token is not a `,', we're done. */
16147 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16149 /* Otherwise, consume the `,' token. */
16150 cp_lexer_consume_token (parser
->lexer
);
16153 /* Convert the vec into a TREE_VEC. */
16154 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
16157 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
16158 TREE_VEC_ELT (introduction_list
, n
) = parm
;
16160 release_tree_vector (introduction_vec
);
16161 return introduction_list
;
16164 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16165 is an abstract declarator. */
16167 static inline cp_declarator
*
16168 get_id_declarator (cp_declarator
*declarator
)
16170 cp_declarator
*d
= declarator
;
16171 while (d
&& d
->kind
!= cdk_id
)
16176 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16177 is an abstract declarator. */
16180 get_unqualified_id (cp_declarator
*declarator
)
16182 declarator
= get_id_declarator (declarator
);
16184 return declarator
->u
.id
.unqualified_name
;
16189 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16192 is_constrained_parameter (tree type
)
16195 && TREE_CODE (type
) == TYPE_DECL
16196 && CONSTRAINED_PARM_CONCEPT (type
)
16197 && DECL_P (CONSTRAINED_PARM_CONCEPT (type
)));
16200 /* Returns true if PARM declares a constrained-parameter. */
16203 is_constrained_parameter (cp_parameter_declarator
*parm
)
16205 return is_constrained_parameter (parm
->decl_specifiers
.type
);
16208 /* Check that the type parameter is only a declarator-id, and that its
16209 type is not cv-qualified. */
16212 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
16213 cp_parameter_declarator
*parm
)
16215 if (!parm
->declarator
)
16218 if (parm
->declarator
->kind
!= cdk_id
)
16220 cp_parser_error (parser
, "invalid constrained type parameter");
16224 /* Don't allow cv-qualified type parameters. */
16225 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
16226 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
16228 cp_parser_error (parser
, "cv-qualified type parameter");
16235 /* Finish parsing/processing a template type parameter and checking
16236 various restrictions. */
16239 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
16241 cp_parameter_declarator
* parmdecl
)
16243 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
16244 return finish_template_type_parm (class_type_node
, id
);
16246 return error_mark_node
;
16250 finish_constrained_template_template_parm (tree proto
, tree id
)
16252 /* FIXME: This should probably be copied, and we may need to adjust
16253 the template parameter depths. */
16254 tree saved_parms
= current_template_parms
;
16255 begin_template_parm_list ();
16256 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
16257 end_template_parm_list ();
16259 tree parm
= finish_template_template_parm (class_type_node
, id
);
16260 current_template_parms
= saved_parms
;
16265 /* Finish parsing/processing a template template parameter by borrowing
16266 the template parameter list from the prototype parameter. */
16269 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
16272 cp_parameter_declarator
*parmdecl
)
16274 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
16275 return error_mark_node
;
16276 return finish_constrained_template_template_parm (proto
, id
);
16279 /* Create a new non-type template parameter from the given PARM
16283 cp_parser_constrained_non_type_template_parm (bool *is_non_type
,
16284 cp_parameter_declarator
*parm
)
16286 *is_non_type
= true;
16287 cp_declarator
*decl
= parm
->declarator
;
16288 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
16289 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
16290 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
16293 /* Build a constrained template parameter based on the PARMDECL
16294 declarator. The type of PARMDECL is the constrained type, which
16295 refers to the prototype template parameter that ultimately
16296 specifies the type of the declared parameter. */
16299 finish_constrained_parameter (cp_parser
*parser
,
16300 cp_parameter_declarator
*parmdecl
,
16303 tree decl
= parmdecl
->decl_specifiers
.type
;
16304 tree id
= get_unqualified_id (parmdecl
->declarator
);
16305 tree def
= parmdecl
->default_argument
;
16306 tree proto
= DECL_INITIAL (decl
);
16308 /* Build the parameter. Return an error if the declarator was invalid. */
16310 if (TREE_CODE (proto
) == TYPE_DECL
)
16311 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
16312 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
16313 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
16316 parm
= cp_parser_constrained_non_type_template_parm (is_non_type
, parmdecl
);
16317 if (parm
== error_mark_node
)
16318 return error_mark_node
;
16320 /* Finish the parameter decl and create a node attaching the
16321 default argument and constraint. */
16322 parm
= build_tree_list (def
, parm
);
16323 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
16328 /* Returns true if the parsed type actually represents the declaration
16329 of a type template-parameter. */
16332 declares_constrained_type_template_parameter (tree type
)
16334 return (is_constrained_parameter (type
)
16335 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
16338 /* Returns true if the parsed type actually represents the declaration of
16339 a template template-parameter. */
16342 declares_constrained_template_template_parameter (tree type
)
16344 return (is_constrained_parameter (type
)
16345 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
16348 /* Parse a default argument for a type template-parameter.
16349 Note that diagnostics are handled in cp_parser_template_parameter. */
16352 cp_parser_default_type_template_argument (cp_parser
*parser
)
16354 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16356 /* Consume the `=' token. */
16357 cp_lexer_consume_token (parser
->lexer
);
16359 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16361 /* Parse the default-argument. */
16362 push_deferring_access_checks (dk_no_deferred
);
16363 tree default_argument
= cp_parser_type_id (parser
,
16364 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16366 pop_deferring_access_checks ();
16368 if (flag_concepts
&& type_uses_auto (default_argument
))
16370 error_at (token
->location
,
16371 "invalid use of %<auto%> in default template argument");
16372 return error_mark_node
;
16375 return default_argument
;
16378 /* Parse a default argument for a template template-parameter. */
16381 cp_parser_default_template_template_argument (cp_parser
*parser
)
16383 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16387 /* Consume the `='. */
16388 cp_lexer_consume_token (parser
->lexer
);
16389 /* Parse the id-expression. */
16390 push_deferring_access_checks (dk_no_deferred
);
16391 /* save token before parsing the id-expression, for error
16393 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
16394 tree default_argument
16395 = cp_parser_id_expression (parser
,
16396 /*template_keyword_p=*/false,
16397 /*check_dependency_p=*/true,
16398 /*template_p=*/&is_template
,
16399 /*declarator_p=*/false,
16400 /*optional_p=*/false);
16401 if (TREE_CODE (default_argument
) == TYPE_DECL
)
16402 /* If the id-expression was a template-id that refers to
16403 a template-class, we already have the declaration here,
16404 so no further lookup is needed. */
16407 /* Look up the name. */
16409 = cp_parser_lookup_name (parser
, default_argument
,
16411 /*is_template=*/is_template
,
16412 /*is_namespace=*/false,
16413 /*check_dependency=*/true,
16414 /*ambiguous_decls=*/NULL
,
16416 /* See if the default argument is valid. */
16417 default_argument
= check_template_template_default_arg (default_argument
);
16418 pop_deferring_access_checks ();
16419 return default_argument
;
16422 /* Parse a template-parameter.
16424 template-parameter:
16426 parameter-declaration
16428 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16429 the parameter. The TREE_PURPOSE is the default value, if any.
16430 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16431 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16432 set to true iff this parameter is a parameter pack. */
16435 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
16436 bool *is_parameter_pack
)
16439 cp_parameter_declarator
*parameter_declarator
;
16442 /* Assume it is a type parameter or a template parameter. */
16443 *is_non_type
= false;
16444 /* Assume it not a parameter pack. */
16445 *is_parameter_pack
= false;
16446 /* Peek at the next token. */
16447 token
= cp_lexer_peek_token (parser
->lexer
);
16448 /* If it is `template', we have a type-parameter. */
16449 if (token
->keyword
== RID_TEMPLATE
)
16450 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16451 /* If it is `class' or `typename' we do not know yet whether it is a
16452 type parameter or a non-type parameter. Consider:
16454 template <typename T, typename T::X X> ...
16458 template <class C, class D*> ...
16460 Here, the first parameter is a type parameter, and the second is
16461 a non-type parameter. We can tell by looking at the token after
16462 the identifier -- if it is a `,', `=', or `>' then we have a type
16464 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16466 /* Peek at the token after `class' or `typename'. */
16467 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16468 /* If it's an ellipsis, we have a template type parameter
16470 if (token
->type
== CPP_ELLIPSIS
)
16471 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16472 /* If it's an identifier, skip it. */
16473 if (token
->type
== CPP_NAME
)
16474 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16475 /* Now, see if the token looks like the end of a template
16477 if (token
->type
== CPP_COMMA
16478 || token
->type
== CPP_EQ
16479 || token
->type
== CPP_GREATER
)
16480 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16483 /* Otherwise, it is a non-type parameter or a constrained parameter.
16487 When parsing a default template-argument for a non-type
16488 template-parameter, the first non-nested `>' is taken as the end
16489 of the template parameter-list rather than a greater-than
16491 parameter_declarator
16492 = cp_parser_parameter_declaration (parser
,
16493 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16494 /*template_parm_p=*/true,
16495 /*parenthesized_p=*/NULL
);
16497 if (!parameter_declarator
)
16498 return error_mark_node
;
16500 /* If the parameter declaration is marked as a parameter pack, set
16501 *IS_PARAMETER_PACK to notify the caller. */
16502 if (parameter_declarator
->template_parameter_pack_p
)
16503 *is_parameter_pack
= true;
16505 if (parameter_declarator
->default_argument
)
16507 /* Can happen in some cases of erroneous input (c++/34892). */
16508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16509 /* Consume the `...' for better error recovery. */
16510 cp_lexer_consume_token (parser
->lexer
);
16513 /* The parameter may have been constrained type parameter. */
16514 if (is_constrained_parameter (parameter_declarator
))
16515 return finish_constrained_parameter (parser
,
16516 parameter_declarator
,
16519 // Now we're sure that the parameter is a non-type parameter.
16520 *is_non_type
= true;
16522 parm
= grokdeclarator (parameter_declarator
->declarator
,
16523 ¶meter_declarator
->decl_specifiers
,
16524 TPARM
, /*initialized=*/0,
16525 /*attrlist=*/NULL
);
16526 if (parm
== error_mark_node
)
16527 return error_mark_node
;
16529 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16532 /* Parse a type-parameter.
16535 class identifier [opt]
16536 class identifier [opt] = type-id
16537 typename identifier [opt]
16538 typename identifier [opt] = type-id
16539 template < template-parameter-list > class identifier [opt]
16540 template < template-parameter-list > class identifier [opt]
16543 GNU Extension (variadic templates):
16546 class ... identifier [opt]
16547 typename ... identifier [opt]
16549 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16550 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16551 the declaration of the parameter.
16553 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16556 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16561 /* Look for a keyword to tell us what kind of parameter this is. */
16562 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16564 return error_mark_node
;
16566 switch (token
->keyword
)
16572 tree default_argument
;
16574 /* If the next token is an ellipsis, we have a template
16576 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16578 /* Consume the `...' token. */
16579 cp_lexer_consume_token (parser
->lexer
);
16580 maybe_warn_variadic_templates ();
16582 *is_parameter_pack
= true;
16585 /* If the next token is an identifier, then it names the
16587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16588 identifier
= cp_parser_identifier (parser
);
16590 identifier
= NULL_TREE
;
16592 /* Create the parameter. */
16593 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16595 /* If the next token is an `=', we have a default argument. */
16596 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16599 = cp_parser_default_type_template_argument (parser
);
16601 /* Template parameter packs cannot have default
16603 if (*is_parameter_pack
)
16606 error_at (token
->location
,
16607 "template parameter pack %qD cannot have a "
16608 "default argument", identifier
);
16610 error_at (token
->location
,
16611 "template parameter packs cannot have "
16612 "default arguments");
16613 default_argument
= NULL_TREE
;
16615 else if (check_for_bare_parameter_packs (default_argument
))
16616 default_argument
= error_mark_node
;
16619 default_argument
= NULL_TREE
;
16621 /* Create the combined representation of the parameter and the
16622 default argument. */
16623 parameter
= build_tree_list (default_argument
, parameter
);
16630 tree default_argument
;
16632 /* Look for the `<'. */
16633 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16634 /* Parse the template-parameter-list. */
16635 cp_parser_template_parameter_list (parser
);
16636 /* Look for the `>'. */
16637 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16639 /* If template requirements are present, parse them. */
16642 tree reqs
= get_shorthand_constraints (current_template_parms
);
16643 if (tree dreqs
= cp_parser_requires_clause_opt (parser
, false))
16644 reqs
= combine_constraint_expressions (reqs
, dreqs
);
16645 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16648 /* Look for the `class' or 'typename' keywords. */
16649 cp_parser_type_parameter_key (parser
);
16650 /* If the next token is an ellipsis, we have a template
16652 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16654 /* Consume the `...' token. */
16655 cp_lexer_consume_token (parser
->lexer
);
16656 maybe_warn_variadic_templates ();
16658 *is_parameter_pack
= true;
16660 /* If the next token is an `=', then there is a
16661 default-argument. If the next token is a `>', we are at
16662 the end of the parameter-list. If the next token is a `,',
16663 then we are at the end of this parameter. */
16664 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16665 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16666 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16668 identifier
= cp_parser_identifier (parser
);
16669 /* Treat invalid names as if the parameter were nameless. */
16670 if (identifier
== error_mark_node
)
16671 identifier
= NULL_TREE
;
16674 identifier
= NULL_TREE
;
16676 /* Create the template parameter. */
16677 parameter
= finish_template_template_parm (class_type_node
,
16680 /* If the next token is an `=', then there is a
16681 default-argument. */
16682 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16685 = cp_parser_default_template_template_argument (parser
);
16687 /* Template parameter packs cannot have default
16689 if (*is_parameter_pack
)
16692 error_at (token
->location
,
16693 "template parameter pack %qD cannot "
16694 "have a default argument",
16697 error_at (token
->location
, "template parameter packs cannot "
16698 "have default arguments");
16699 default_argument
= NULL_TREE
;
16703 default_argument
= NULL_TREE
;
16705 /* Create the combined representation of the parameter and the
16706 default argument. */
16707 parameter
= build_tree_list (default_argument
, parameter
);
16712 gcc_unreachable ();
16719 /* Parse a template-id.
16722 template-name < template-argument-list [opt] >
16724 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16725 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16726 returned. Otherwise, if the template-name names a function, or set
16727 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16728 names a class, returns a TYPE_DECL for the specialization.
16730 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16731 uninstantiated templates. */
16734 cp_parser_template_id (cp_parser
*parser
,
16735 bool template_keyword_p
,
16736 bool check_dependency_p
,
16737 enum tag_types tag_type
,
16738 bool is_declaration
)
16743 cp_token_position start_of_id
= 0;
16744 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16745 bool is_identifier
;
16747 /* If the next token corresponds to a template-id, there is no need
16749 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16751 if (token
->type
== CPP_TEMPLATE_ID
)
16753 cp_lexer_consume_token (parser
->lexer
);
16754 return saved_checks_value (token
->u
.tree_check_value
);
16757 /* Avoid performing name lookup if there is no possibility of
16758 finding a template-id. */
16759 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16760 || (token
->type
== CPP_NAME
16761 && !cp_parser_nth_token_starts_template_argument_list_p
16764 cp_parser_error (parser
, "expected template-id");
16765 return error_mark_node
;
16768 /* Remember where the template-id starts. */
16769 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16770 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16772 push_deferring_access_checks (dk_deferred
);
16774 /* Parse the template-name. */
16775 is_identifier
= false;
16776 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16777 check_dependency_p
,
16782 /* Push any access checks inside the firewall we're about to create. */
16783 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16784 pop_deferring_access_checks ();
16785 if (templ
== error_mark_node
|| is_identifier
)
16788 /* Since we're going to preserve any side-effects from this parse, set up a
16789 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16790 in the template arguments. */
16791 tentative_firewall
firewall (parser
);
16792 reopen_deferring_access_checks (checks
);
16794 /* If we find the sequence `[:' after a template-name, it's probably
16795 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16796 parse correctly the argument list. */
16797 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16798 == CPP_OPEN_SQUARE
)
16799 && next_token
->flags
& DIGRAPH
16800 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16802 && !(next_token_2
->flags
& PREV_WHITE
))
16804 cp_parser_parse_tentatively (parser
);
16805 /* Change `:' into `::'. */
16806 next_token_2
->type
= CPP_SCOPE
;
16807 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16809 cp_lexer_consume_token (parser
->lexer
);
16811 /* Parse the arguments. */
16812 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16813 if (!cp_parser_parse_definitely (parser
))
16815 /* If we couldn't parse an argument list, then we revert our changes
16816 and return simply an error. Maybe this is not a template-id
16818 next_token_2
->type
= CPP_COLON
;
16819 cp_parser_error (parser
, "expected %<<%>");
16820 pop_deferring_access_checks ();
16821 return error_mark_node
;
16823 /* Otherwise, emit an error about the invalid digraph, but continue
16824 parsing because we got our argument list. */
16825 if (permerror (next_token
->location
,
16826 "%<<::%> cannot begin a template-argument list"))
16828 static bool hint
= false;
16829 inform (next_token
->location
,
16830 "%<<:%> is an alternate spelling for %<[%>."
16831 " Insert whitespace between %<<%> and %<::%>");
16832 if (!hint
&& !flag_permissive
)
16834 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16835 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16836 "accept your code)");
16843 /* Look for the `<' that starts the template-argument-list. */
16844 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16846 pop_deferring_access_checks ();
16847 return error_mark_node
;
16849 /* Parse the arguments. */
16850 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16852 if ((cxx_dialect
> cxx17
)
16853 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16854 && !template_keyword_p
16855 && (cp_parser_error_occurred (parser
)
16856 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16858 /* This didn't go well. */
16859 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16861 /* C++20 says that "function-name < a;" is now ill-formed. */
16862 if (cp_parser_error_occurred (parser
))
16864 error_at (token
->location
, "invalid template-argument-list");
16865 inform (token
->location
, "function name as the left hand "
16866 "operand of %<<%> is ill-formed in C++20; wrap the "
16867 "function name in %<()%>");
16870 /* We expect "f<targs>" to be followed by "(args)". */
16871 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16872 "expected %<(%> after template-argument-list");
16874 /* Purge all subsequent tokens. */
16875 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16878 cp_parser_simulate_error (parser
);
16879 pop_deferring_access_checks ();
16880 return error_mark_node
;
16884 /* Set the location to be of the form:
16885 template-name < template-argument-list [opt] >
16886 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16887 with caret == start at the start of the template-name,
16888 ranging until the closing '>'. */
16889 location_t combined_loc
16890 = make_location (token
->location
, token
->location
, parser
->lexer
);
16892 /* Check for concepts autos where they don't belong. We could
16893 identify types in some cases of identifier TEMPL, looking ahead
16894 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16895 types. We reject them in functions, but if what we have is an
16896 identifier, even with none_type we can't conclude it's NOT a
16897 type, we have to wait for template substitution. */
16898 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16899 template_id
= error_mark_node
;
16900 /* Build a representation of the specialization. */
16901 else if (identifier_p (templ
))
16902 template_id
= build_min_nt_loc (combined_loc
,
16905 else if (DECL_TYPE_TEMPLATE_P (templ
)
16906 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16908 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16909 template (rather than some instantiation thereof) only if
16910 is not nested within some other construct. For example, in
16911 "template <typename T> void f(T) { A<T>::", A<T> is just an
16912 instantiation of A. */
16913 bool entering_scope
16914 = (template_parm_scope_p ()
16915 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16917 = finish_template_type (templ
, arguments
, entering_scope
);
16919 else if (concept_definition_p (templ
))
16921 /* The caller will decide whether this is a concept check or type
16923 template_id
= build2_loc (combined_loc
, TEMPLATE_ID_EXPR
,
16924 boolean_type_node
, templ
, arguments
);
16926 else if (variable_template_p (templ
))
16928 template_id
= lookup_template_variable (templ
, arguments
);
16929 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16930 SET_EXPR_LOCATION (template_id
, combined_loc
);
16934 /* If it's not a class-template or a template-template, it should be
16935 a function-template. */
16936 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
16938 template_id
= lookup_template_function (templ
, arguments
);
16939 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16940 SET_EXPR_LOCATION (template_id
, combined_loc
);
16943 /* If parsing tentatively, replace the sequence of tokens that makes
16944 up the template-id with a CPP_TEMPLATE_ID token. That way,
16945 should we re-parse the token stream, we will not have to repeat
16946 the effort required to do the parse, nor will we issue duplicate
16947 error messages about problems during instantiation of the
16950 /* Don't do this if we had a parse error in a declarator; re-parsing
16951 might succeed if a name changes meaning (60361). */
16952 && !(cp_parser_error_occurred (parser
)
16953 && cp_parser_parsing_tentatively (parser
)
16954 && parser
->in_declarator_p
))
16956 /* Reset the contents of the START_OF_ID token. */
16957 token
->type
= CPP_TEMPLATE_ID
;
16958 token
->location
= combined_loc
;
16960 /* Retrieve any deferred checks. Do not pop this access checks yet
16961 so the memory will not be reclaimed during token replacing below. */
16962 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16963 token
->tree_check_p
= true;
16964 token
->u
.tree_check_value
->value
= template_id
;
16965 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16966 token
->keyword
= RID_MAX
;
16968 /* Purge all subsequent tokens. */
16969 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16971 /* ??? Can we actually assume that, if template_id ==
16972 error_mark_node, we will have issued a diagnostic to the
16973 user, as opposed to simply marking the tentative parse as
16975 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16976 error_at (token
->location
, "parse error in template argument list");
16979 pop_to_parent_deferring_access_checks ();
16980 return template_id
;
16983 /* Like cp_parser_template_id, called in non-type context. */
16986 cp_parser_template_id_expr (cp_parser
*parser
,
16987 bool template_keyword_p
,
16988 bool check_dependency_p
,
16989 bool is_declaration
)
16991 tree x
= cp_parser_template_id (parser
, template_keyword_p
, check_dependency_p
,
16992 none_type
, is_declaration
);
16993 if (TREE_CODE (x
) == TEMPLATE_ID_EXPR
16994 && concept_check_p (x
))
16995 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16996 return build_concept_id (x
);
17000 /* Parse a template-name.
17005 The standard should actually say:
17009 operator-function-id
17011 A defect report has been filed about this issue.
17013 A conversion-function-id cannot be a template name because they cannot
17014 be part of a template-id. In fact, looking at this code:
17016 a.operator K<int>()
17018 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
17019 It is impossible to call a templated conversion-function-id with an
17020 explicit argument list, since the only allowed template parameter is
17021 the type to which it is converting.
17023 If TEMPLATE_KEYWORD_P is true, then we have just seen the
17024 `template' keyword, in a construction like:
17028 In that case `f' is taken to be a template-name, even though there
17029 is no way of knowing for sure.
17031 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
17032 name refers to a set of overloaded functions, at least one of which
17033 is a template, or an IDENTIFIER_NODE with the name of the template,
17034 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
17035 names are looked up inside uninstantiated templates. */
17038 cp_parser_template_name (cp_parser
* parser
,
17039 bool template_keyword_p
,
17040 bool check_dependency_p
,
17041 bool is_declaration
,
17042 enum tag_types tag_type
,
17043 bool *is_identifier
)
17047 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17049 /* If the next token is `operator', then we have either an
17050 operator-function-id or a conversion-function-id. */
17051 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
17053 /* We don't know whether we're looking at an
17054 operator-function-id or a conversion-function-id. */
17055 cp_parser_parse_tentatively (parser
);
17056 /* Try an operator-function-id. */
17057 identifier
= cp_parser_operator_function_id (parser
);
17058 /* If that didn't work, try a conversion-function-id. */
17059 if (!cp_parser_parse_definitely (parser
))
17061 cp_parser_error (parser
, "expected template-name");
17062 return error_mark_node
;
17065 /* Look for the identifier. */
17067 identifier
= cp_parser_identifier (parser
);
17069 /* If we didn't find an identifier, we don't have a template-id. */
17070 if (identifier
== error_mark_node
)
17071 return error_mark_node
;
17073 /* If the name immediately followed the `template' keyword, then it
17074 is a template-name. However, if the next token is not `<', then
17075 we do not treat it as a template-name, since it is not being used
17076 as part of a template-id. This enables us to handle constructs
17079 template <typename T> struct S { S(); };
17080 template <typename T> S<T>::S();
17082 correctly. We would treat `S' as a template -- if it were `S<T>'
17083 -- but we do not if there is no `<'. */
17085 if (processing_template_decl
17086 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
17088 /* In a declaration, in a dependent context, we pretend that the
17089 "template" keyword was present in order to improve error
17090 recovery. For example, given:
17092 template <typename T> void f(T::X<int>);
17094 we want to treat "X<int>" as a template-id. */
17096 && !template_keyword_p
17097 && parser
->scope
&& TYPE_P (parser
->scope
)
17098 && check_dependency_p
17099 && dependent_scope_p (parser
->scope
)
17100 /* Do not do this for dtors (or ctors), since they never
17101 need the template keyword before their name. */
17102 && !constructor_name_p (identifier
, parser
->scope
))
17104 cp_token_position start
= 0;
17106 /* Explain what went wrong. */
17107 error_at (token
->location
, "non-template %qD used as template",
17109 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
17110 parser
->scope
, identifier
);
17111 /* If parsing tentatively, find the location of the "<" token. */
17112 if (cp_parser_simulate_error (parser
))
17113 start
= cp_lexer_token_position (parser
->lexer
, true);
17114 /* Parse the template arguments so that we can issue error
17115 messages about them. */
17116 cp_lexer_consume_token (parser
->lexer
);
17117 cp_parser_enclosed_template_argument_list (parser
);
17118 /* Skip tokens until we find a good place from which to
17119 continue parsing. */
17120 cp_parser_skip_to_closing_parenthesis (parser
,
17121 /*recovering=*/true,
17123 /*consume_paren=*/false);
17124 /* If parsing tentatively, permanently remove the
17125 template argument list. That will prevent duplicate
17126 error messages from being issued about the missing
17127 "template" keyword. */
17129 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
17131 *is_identifier
= true;
17132 parser
->context
->object_type
= NULL_TREE
;
17136 /* If the "template" keyword is present, then there is generally
17137 no point in doing name-lookup, so we just return IDENTIFIER.
17138 But, if the qualifying scope is non-dependent then we can
17139 (and must) do name-lookup normally. */
17140 if (template_keyword_p
)
17142 tree scope
= (parser
->scope
? parser
->scope
17143 : parser
->context
->object_type
);
17144 if (scope
&& TYPE_P (scope
)
17145 && (!CLASS_TYPE_P (scope
)
17146 || (check_dependency_p
&& dependent_type_p (scope
))))
17148 /* We're optimizing away the call to cp_parser_lookup_name, but
17149 we still need to do this. */
17150 parser
->context
->object_type
= NULL_TREE
;
17156 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17157 const bool scoped_p
= ((parser
->scope
? parser
->scope
17158 : parser
->context
->object_type
) != NULL_TREE
);
17160 /* Look up the name. */
17161 decl
= cp_parser_lookup_name (parser
, identifier
,
17163 /*is_template=*/true,
17164 /*is_namespace=*/false,
17165 check_dependency_p
,
17166 /*ambiguous_decls=*/NULL
,
17169 decl
= strip_using_decl (decl
);
17171 /* If DECL is a template, then the name was a template-name. */
17172 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
17174 if (TREE_DEPRECATED (decl
)
17175 && deprecated_state
!= DEPRECATED_SUPPRESS
)
17177 tree d
= DECL_TEMPLATE_RESULT (decl
);
17179 if (TREE_CODE (d
) == TYPE_DECL
)
17180 attr
= lookup_attribute ("deprecated",
17181 TYPE_ATTRIBUTES (TREE_TYPE (d
)));
17183 attr
= lookup_attribute ("deprecated",
17184 DECL_ATTRIBUTES (d
));
17185 warn_deprecated_use (decl
, attr
);
17190 /* The standard does not explicitly indicate whether a name that
17191 names a set of overloaded declarations, some of which are
17192 templates, is a template-name. However, such a name should
17193 be a template-name; otherwise, there is no way to form a
17194 template-id for the overloaded templates. */
17195 bool found
= false;
17197 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
17198 !found
&& iter
; ++iter
)
17199 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
17203 && (cxx_dialect
> cxx17
)
17205 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
17206 && tag_type
== none_type
)
17208 /* [temp.names] says "A name is also considered to refer to a template
17209 if it is an unqualified-id followed by a < and name lookup finds
17210 either one or more functions or finds nothing." */
17212 /* The "more functions" case. Just use the OVERLOAD as normally.
17213 We don't use is_overloaded_fn here to avoid considering
17215 if (TREE_CODE (decl
) == OVERLOAD
17216 /* Name lookup found one function. */
17217 || TREE_CODE (decl
) == FUNCTION_DECL
)
17219 /* Name lookup found nothing. */
17220 else if (decl
== error_mark_node
)
17226 /* The name does not name a template. */
17227 cp_parser_error (parser
, "expected template-name");
17228 return error_mark_node
;
17235 /* Parse a template-argument-list.
17237 template-argument-list:
17238 template-argument ... [opt]
17239 template-argument-list , template-argument ... [opt]
17241 Returns a TREE_VEC containing the arguments. */
17244 cp_parser_template_argument_list (cp_parser
* parser
)
17246 tree fixed_args
[10];
17247 unsigned n_args
= 0;
17248 unsigned alloced
= 10;
17249 tree
*arg_ary
= fixed_args
;
17251 bool saved_in_template_argument_list_p
;
17253 bool saved_non_ice_p
;
17255 /* Don't create location wrapper nodes within a template-argument-list. */
17256 auto_suppress_location_wrappers sentinel
;
17258 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
17259 parser
->in_template_argument_list_p
= true;
17260 /* Even if the template-id appears in an integral
17261 constant-expression, the contents of the argument list do
17263 saved_ice_p
= parser
->integral_constant_expression_p
;
17264 parser
->integral_constant_expression_p
= false;
17265 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
17266 parser
->non_integral_constant_expression_p
= false;
17268 /* Parse the arguments. */
17274 /* Consume the comma. */
17275 cp_lexer_consume_token (parser
->lexer
);
17277 /* Parse the template-argument. */
17278 argument
= cp_parser_template_argument (parser
);
17280 /* If the next token is an ellipsis, we're expanding a template
17282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17284 if (argument
== error_mark_node
)
17286 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17287 error_at (token
->location
,
17288 "expected parameter pack before %<...%>");
17290 /* Consume the `...' token. */
17291 cp_lexer_consume_token (parser
->lexer
);
17293 /* Make the argument into a TYPE_PACK_EXPANSION or
17294 EXPR_PACK_EXPANSION. */
17295 argument
= make_pack_expansion (argument
);
17298 if (n_args
== alloced
)
17302 if (arg_ary
== fixed_args
)
17304 arg_ary
= XNEWVEC (tree
, alloced
);
17305 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
17308 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
17310 arg_ary
[n_args
++] = argument
;
17312 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
17314 vec
= make_tree_vec (n_args
);
17317 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
17319 if (arg_ary
!= fixed_args
)
17321 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
17322 parser
->integral_constant_expression_p
= saved_ice_p
;
17323 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
17325 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
17329 /* Parse a template-argument.
17332 assignment-expression
17336 The representation is that of an assignment-expression, type-id, or
17337 id-expression -- except that the qualified id-expression is
17338 evaluated, so that the value returned is either a DECL or an
17341 Although the standard says "assignment-expression", it forbids
17342 throw-expressions or assignments in the template argument.
17343 Therefore, we use "conditional-expression" instead. */
17346 cp_parser_template_argument (cp_parser
* parser
)
17351 bool maybe_type_id
= false;
17352 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
17353 location_t loc
= 0;
17356 /* There's really no way to know what we're looking at, so we just
17357 try each alternative in order.
17361 In a template-argument, an ambiguity between a type-id and an
17362 expression is resolved to a type-id, regardless of the form of
17363 the corresponding template-parameter.
17365 Therefore, we try a type-id first. */
17366 cp_parser_parse_tentatively (parser
);
17367 argument
= cp_parser_template_type_arg (parser
);
17368 /* If there was no error parsing the type-id but the next token is a
17369 '>>', our behavior depends on which dialect of C++ we're
17370 parsing. In C++98, we probably found a typo for '> >'. But there
17371 are type-id which are also valid expressions. For instance:
17373 struct X { int operator >> (int); };
17374 template <int V> struct Foo {};
17377 Here 'X()' is a valid type-id of a function type, but the user just
17378 wanted to write the expression "X() >> 5". Thus, we remember that we
17379 found a valid type-id, but we still try to parse the argument as an
17380 expression to see what happens.
17382 In C++0x, the '>>' will be considered two separate '>'
17384 if (!cp_parser_error_occurred (parser
)
17385 && cxx_dialect
== cxx98
17386 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17388 maybe_type_id
= true;
17389 cp_parser_abort_tentative_parse (parser
);
17393 /* If the next token isn't a `,' or a `>', then this argument wasn't
17394 really finished. This means that the argument is not a valid
17396 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17397 cp_parser_error (parser
, "expected template-argument");
17398 /* If that worked, we're done. */
17399 if (cp_parser_parse_definitely (parser
))
17402 /* We're still not sure what the argument will be. */
17403 cp_parser_parse_tentatively (parser
);
17404 /* Try a template. */
17405 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
17406 argument
= cp_parser_id_expression (parser
,
17407 /*template_keyword_p=*/false,
17408 /*check_dependency_p=*/true,
17410 /*declarator_p=*/false,
17411 /*optional_p=*/false);
17412 /* If the next token isn't a `,' or a `>', then this argument wasn't
17413 really finished. */
17414 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17415 cp_parser_error (parser
, "expected template-argument");
17416 if (!cp_parser_error_occurred (parser
))
17418 /* Figure out what is being referred to. If the id-expression
17419 was for a class template specialization, then we will have a
17420 TYPE_DECL at this point. There is no need to do name lookup
17421 at this point in that case. */
17422 if (TREE_CODE (argument
) != TYPE_DECL
)
17423 argument
= cp_parser_lookup_name (parser
, argument
,
17425 /*is_template=*/template_p
,
17426 /*is_namespace=*/false,
17427 /*check_dependency=*/true,
17428 /*ambiguous_decls=*/NULL
,
17429 argument_start_token
->location
);
17430 if (TREE_CODE (argument
) != TEMPLATE_DECL
17431 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
17432 cp_parser_error (parser
, "expected template-name");
17434 if (cp_parser_parse_definitely (parser
))
17436 if (TREE_DEPRECATED (argument
))
17437 warn_deprecated_use (argument
, NULL_TREE
);
17440 /* It must be a non-type argument. In C++17 any constant-expression is
17442 if (cxx_dialect
> cxx14
)
17445 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17447 -- an integral constant-expression of integral or enumeration
17450 -- the name of a non-type template-parameter; or
17452 -- the name of an object or function with external linkage...
17454 -- the address of an object or function with external linkage...
17456 -- a pointer to member... */
17457 /* Look for a non-type template parameter. */
17458 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17460 cp_parser_parse_tentatively (parser
);
17461 argument
= cp_parser_primary_expression (parser
,
17462 /*address_p=*/false,
17464 /*template_arg_p=*/true,
17466 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
17467 || !cp_parser_next_token_ends_template_argument_p (parser
))
17468 cp_parser_simulate_error (parser
);
17469 if (cp_parser_parse_definitely (parser
))
17473 /* If the next token is "&", the argument must be the address of an
17474 object or function with external linkage. */
17475 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17478 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17479 cp_lexer_consume_token (parser
->lexer
);
17481 /* See if we might have an id-expression. */
17482 token
= cp_lexer_peek_token (parser
->lexer
);
17483 if (token
->type
== CPP_NAME
17484 || token
->keyword
== RID_OPERATOR
17485 || token
->type
== CPP_SCOPE
17486 || token
->type
== CPP_TEMPLATE_ID
17487 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17489 cp_parser_parse_tentatively (parser
);
17490 argument
= cp_parser_primary_expression (parser
,
17493 /*template_arg_p=*/true,
17495 if (cp_parser_error_occurred (parser
)
17496 || !cp_parser_next_token_ends_template_argument_p (parser
))
17497 cp_parser_abort_tentative_parse (parser
);
17502 if (INDIRECT_REF_P (argument
))
17504 /* Strip the dereference temporarily. */
17505 gcc_assert (REFERENCE_REF_P (argument
));
17506 argument
= TREE_OPERAND (argument
, 0);
17509 /* If we're in a template, we represent a qualified-id referring
17510 to a static data member as a SCOPE_REF even if the scope isn't
17511 dependent so that we can check access control later. */
17513 if (TREE_CODE (probe
) == SCOPE_REF
)
17514 probe
= TREE_OPERAND (probe
, 1);
17517 /* A variable without external linkage might still be a
17518 valid constant-expression, so no error is issued here
17519 if the external-linkage check fails. */
17520 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
17521 cp_parser_simulate_error (parser
);
17523 else if (is_overloaded_fn (argument
))
17524 /* All overloaded functions are allowed; if the external
17525 linkage test does not pass, an error will be issued
17529 && (TREE_CODE (argument
) == OFFSET_REF
17530 || TREE_CODE (argument
) == SCOPE_REF
))
17531 /* A pointer-to-member. */
17533 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17536 cp_parser_simulate_error (parser
);
17538 if (cp_parser_parse_definitely (parser
))
17541 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17542 tf_warning_or_error
);
17544 argument
= convert_from_reference (argument
);
17549 /* If the argument started with "&", there are no other valid
17550 alternatives at this point. */
17553 cp_parser_error (parser
, "invalid non-type template argument");
17554 return error_mark_node
;
17558 /* If the argument wasn't successfully parsed as a type-id followed
17559 by '>>', the argument can only be a constant expression now.
17560 Otherwise, we try parsing the constant-expression tentatively,
17561 because the argument could really be a type-id. */
17563 cp_parser_parse_tentatively (parser
);
17565 if (cxx_dialect
<= cxx14
)
17566 argument
= cp_parser_constant_expression (parser
);
17569 /* In C++20, we can encounter a braced-init-list. */
17570 if (cxx_dialect
>= cxx20
17571 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17573 bool expr_non_constant_p
;
17574 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17577 /* With C++17 generalized non-type template arguments we need to handle
17578 lvalue constant expressions, too. */
17579 argument
= cp_parser_assignment_expression (parser
);
17580 require_potential_constant_expression (argument
);
17583 if (!maybe_type_id
)
17585 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17586 cp_parser_error (parser
, "expected template-argument");
17587 if (cp_parser_parse_definitely (parser
))
17589 /* We did our best to parse the argument as a non type-id, but that
17590 was the only alternative that matched (albeit with a '>' after
17591 it). We can assume it's just a typo from the user, and a
17592 diagnostic will then be issued. */
17593 return cp_parser_template_type_arg (parser
);
17596 /* Parse an explicit-instantiation.
17598 explicit-instantiation:
17599 template declaration
17601 Although the standard says `declaration', what it really means is:
17603 explicit-instantiation:
17604 template decl-specifier-seq [opt] declarator [opt] ;
17606 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17607 supposed to be allowed. A defect report has been filed about this
17612 explicit-instantiation:
17613 storage-class-specifier template
17614 decl-specifier-seq [opt] declarator [opt] ;
17615 function-specifier template
17616 decl-specifier-seq [opt] declarator [opt] ; */
17619 cp_parser_explicit_instantiation (cp_parser
* parser
)
17621 int declares_class_or_enum
;
17622 cp_decl_specifier_seq decl_specifiers
;
17623 tree extension_specifier
= NULL_TREE
;
17625 timevar_push (TV_TEMPLATE_INST
);
17627 /* Look for an (optional) storage-class-specifier or
17628 function-specifier. */
17629 if (cp_parser_allow_gnu_extensions_p (parser
))
17631 extension_specifier
17632 = cp_parser_storage_class_specifier_opt (parser
);
17633 if (!extension_specifier
)
17634 extension_specifier
17635 = cp_parser_function_specifier_opt (parser
,
17636 /*decl_specs=*/NULL
);
17639 /* Look for the `template' keyword. */
17640 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17641 /* Let the front end know that we are processing an explicit
17643 begin_explicit_instantiation ();
17644 /* [temp.explicit] says that we are supposed to ignore access
17645 control while processing explicit instantiation directives. */
17646 push_deferring_access_checks (dk_no_check
);
17647 /* Parse a decl-specifier-seq. */
17648 cp_parser_decl_specifier_seq (parser
,
17649 CP_PARSER_FLAGS_OPTIONAL
,
17651 &declares_class_or_enum
);
17652 /* If there was exactly one decl-specifier, and it declared a class,
17653 and there's no declarator, then we have an explicit type
17655 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17657 tree type
= check_tag_decl (&decl_specifiers
,
17658 /*explicit_type_instantiation_p=*/true);
17659 /* Turn access control back on for names used during
17660 template instantiation. */
17661 pop_deferring_access_checks ();
17663 do_type_instantiation (type
, extension_specifier
,
17664 /*complain=*/tf_error
);
17668 cp_declarator
*declarator
;
17671 /* Parse the declarator. */
17673 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17674 CP_PARSER_FLAGS_NONE
,
17675 /*ctor_dtor_or_conv_p=*/NULL
,
17676 /*parenthesized_p=*/NULL
,
17677 /*member_p=*/false,
17678 /*friend_p=*/false,
17679 /*static_p=*/false);
17680 if (declares_class_or_enum
& 2)
17681 cp_parser_check_for_definition_in_return_type (declarator
,
17682 decl_specifiers
.type
,
17683 decl_specifiers
.locations
[ds_type_spec
]);
17684 if (declarator
!= cp_error_declarator
)
17686 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17687 permerror (decl_specifiers
.locations
[ds_inline
],
17688 "explicit instantiation shall not use"
17689 " %<inline%> specifier");
17690 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17691 permerror (decl_specifiers
.locations
[ds_constexpr
],
17692 "explicit instantiation shall not use"
17693 " %<constexpr%> specifier");
17694 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_consteval
))
17695 permerror (decl_specifiers
.locations
[ds_consteval
],
17696 "explicit instantiation shall not use"
17697 " %<consteval%> specifier");
17699 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17700 NORMAL
, 0, &decl_specifiers
.attributes
);
17701 /* Turn access control back on for names used during
17702 template instantiation. */
17703 pop_deferring_access_checks ();
17704 /* Do the explicit instantiation. */
17705 do_decl_instantiation (decl
, extension_specifier
);
17709 pop_deferring_access_checks ();
17710 /* Skip the body of the explicit instantiation. */
17711 cp_parser_skip_to_end_of_statement (parser
);
17714 /* We're done with the instantiation. */
17715 end_explicit_instantiation ();
17717 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17719 timevar_pop (TV_TEMPLATE_INST
);
17722 /* Parse an explicit-specialization.
17724 explicit-specialization:
17725 template < > declaration
17727 Although the standard says `declaration', what it really means is:
17729 explicit-specialization:
17730 template <> decl-specifier [opt] init-declarator [opt] ;
17731 template <> function-definition
17732 template <> explicit-specialization
17733 template <> template-declaration */
17736 cp_parser_explicit_specialization (cp_parser
* parser
)
17738 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17740 /* Look for the `template' keyword. */
17741 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17742 /* Look for the `<'. */
17743 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17744 /* Look for the `>'. */
17745 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17746 /* We have processed another parameter list. */
17747 ++parser
->num_template_parameter_lists
;
17751 A template ... explicit specialization ... shall not have C
17753 bool need_lang_pop
= current_lang_name
== lang_name_c
;
17756 error_at (token
->location
, "template specialization with C linkage");
17757 maybe_show_extern_c_location ();
17759 /* Give it C++ linkage to avoid confusing other parts of the
17761 push_lang_context (lang_name_cplusplus
);
17764 /* Let the front end know that we are beginning a specialization. */
17765 if (begin_specialization ())
17767 /* If the next keyword is `template', we need to figure out
17768 whether or not we're looking a template-declaration. */
17769 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17771 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17772 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17773 cp_parser_template_declaration_after_export (parser
,
17774 /*member_p=*/false);
17776 cp_parser_explicit_specialization (parser
);
17779 /* Parse the dependent declaration. */
17780 cp_parser_single_declaration (parser
,
17782 /*member_p=*/false,
17783 /*explicit_specialization_p=*/true,
17784 /*friend_p=*/NULL
);
17787 /* We're done with the specialization. */
17788 end_specialization ();
17790 /* For the erroneous case of a template with C linkage, we pushed an
17791 implicit C++ linkage scope; exit that scope now. */
17793 pop_lang_context ();
17795 /* We're done with this parameter list. */
17796 --parser
->num_template_parameter_lists
;
17799 /* Parse a type-specifier.
17802 simple-type-specifier
17805 elaborated-type-specifier
17813 Returns a representation of the type-specifier. For a
17814 class-specifier, enum-specifier, or elaborated-type-specifier, a
17815 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17817 The parser flags FLAGS is used to control type-specifier parsing.
17819 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17820 in a decl-specifier-seq.
17822 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17823 class-specifier, enum-specifier, or elaborated-type-specifier, then
17824 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17825 if a type is declared; 2 if it is defined. Otherwise, it is set to
17828 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17829 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17830 is set to FALSE. */
17833 cp_parser_type_specifier (cp_parser
* parser
,
17834 cp_parser_flags flags
,
17835 cp_decl_specifier_seq
*decl_specs
,
17836 bool is_declaration
,
17837 int* declares_class_or_enum
,
17838 bool* is_cv_qualifier
)
17840 tree type_spec
= NULL_TREE
;
17843 cp_decl_spec ds
= ds_last
;
17845 /* Assume this type-specifier does not declare a new type. */
17846 if (declares_class_or_enum
)
17847 *declares_class_or_enum
= 0;
17848 /* And that it does not specify a cv-qualifier. */
17849 if (is_cv_qualifier
)
17850 *is_cv_qualifier
= false;
17851 /* Peek at the next token. */
17852 token
= cp_lexer_peek_token (parser
->lexer
);
17854 /* If we're looking at a keyword, we can use that to guide the
17855 production we choose. */
17856 keyword
= token
->keyword
;
17860 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17861 goto elaborated_type_specifier
;
17863 /* Look for the enum-specifier. */
17864 type_spec
= cp_parser_enum_specifier (parser
);
17865 /* If that worked, we're done. */
17868 if (declares_class_or_enum
)
17869 *declares_class_or_enum
= 2;
17871 cp_parser_set_decl_spec_type (decl_specs
,
17874 /*type_definition_p=*/true);
17878 goto elaborated_type_specifier
;
17880 /* Any of these indicate either a class-specifier, or an
17881 elaborated-type-specifier. */
17885 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17886 goto elaborated_type_specifier
;
17888 /* Parse tentatively so that we can back up if we don't find a
17889 class-specifier. */
17890 cp_parser_parse_tentatively (parser
);
17891 /* Look for the class-specifier. */
17892 type_spec
= cp_parser_class_specifier (parser
);
17893 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17894 /* If that worked, we're done. */
17895 if (cp_parser_parse_definitely (parser
))
17897 if (declares_class_or_enum
)
17898 *declares_class_or_enum
= 2;
17900 cp_parser_set_decl_spec_type (decl_specs
,
17903 /*type_definition_p=*/true);
17907 /* Fall through. */
17908 elaborated_type_specifier
:
17909 /* We're declaring (not defining) a class or enum. */
17910 if (declares_class_or_enum
)
17911 *declares_class_or_enum
= 1;
17913 /* Fall through. */
17915 /* Look for an elaborated-type-specifier. */
17917 = (cp_parser_elaborated_type_specifier
17919 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17922 cp_parser_set_decl_spec_type (decl_specs
,
17925 /*type_definition_p=*/false);
17930 if (is_cv_qualifier
)
17931 *is_cv_qualifier
= true;
17936 if (is_cv_qualifier
)
17937 *is_cv_qualifier
= true;
17942 if (is_cv_qualifier
)
17943 *is_cv_qualifier
= true;
17947 /* The `__complex__' keyword is a GNU extension. */
17955 /* Handle simple keywords. */
17960 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17961 decl_specs
->any_specifiers_p
= true;
17963 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17966 /* If we do not already have a type-specifier, assume we are looking
17967 at a simple-type-specifier. */
17968 type_spec
= cp_parser_simple_type_specifier (parser
,
17972 /* If we didn't find a type-specifier, and a type-specifier was not
17973 optional in this context, issue an error message. */
17974 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17976 cp_parser_error (parser
, "expected type specifier");
17977 return error_mark_node
;
17983 /* Parse a simple-type-specifier.
17985 simple-type-specifier:
17986 :: [opt] nested-name-specifier [opt] type-name
17987 :: [opt] nested-name-specifier template template-id
18002 simple-type-specifier:
18004 decltype ( expression )
18007 __underlying_type ( type-id )
18011 nested-name-specifier(opt) template-name
18015 simple-type-specifier:
18017 __typeof__ unary-expression
18018 __typeof__ ( type-id )
18019 __typeof__ ( type-id ) { initializer-list , [opt] }
18021 Concepts Extension:
18023 simple-type-specifier:
18024 constrained-type-specifier
18026 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
18027 appropriately updated. */
18030 cp_parser_simple_type_specifier (cp_parser
* parser
,
18031 cp_decl_specifier_seq
*decl_specs
,
18032 cp_parser_flags flags
)
18034 tree type
= NULL_TREE
;
18038 /* Peek at the next token. */
18039 token
= cp_lexer_peek_token (parser
->lexer
);
18041 /* If we're looking at a keyword, things are easy. */
18042 switch (token
->keyword
)
18046 decl_specs
->explicit_char_p
= true;
18047 type
= char_type_node
;
18050 type
= char8_type_node
;
18053 type
= char16_type_node
;
18056 type
= char32_type_node
;
18059 type
= wchar_type_node
;
18062 type
= boolean_type_node
;
18065 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
18066 type
= short_integer_type_node
;
18070 decl_specs
->explicit_int_p
= true;
18071 type
= integer_type_node
;
18077 idx
= token
->keyword
- RID_INT_N_0
;
18078 if (! int_n_enabled_p
[idx
])
18082 decl_specs
->explicit_intN_p
= true;
18083 decl_specs
->int_n_idx
= idx
;
18084 /* Check if the alternate "__intN__" form has been used instead of
18086 if (strncmp (IDENTIFIER_POINTER (token
->u
.value
)
18087 + (IDENTIFIER_LENGTH (token
->u
.value
) - 2),
18089 decl_specs
->int_n_alt
= true;
18091 type
= int_n_trees
[idx
].signed_type
;
18095 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
18096 type
= long_integer_type_node
;
18099 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
18100 type
= integer_type_node
;
18103 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
18104 type
= unsigned_type_node
;
18107 type
= float_type_node
;
18110 type
= double_type_node
;
18113 type
= void_type_node
;
18117 maybe_warn_cpp0x (CPP0X_AUTO
);
18118 if (parser
->auto_is_implicit_function_template_parm_p
)
18120 /* The 'auto' might be the placeholder return type for a function decl
18121 with trailing return type. */
18122 bool have_trailing_return_fn_decl
= false;
18124 cp_parser_parse_tentatively (parser
);
18125 cp_lexer_consume_token (parser
->lexer
);
18126 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
18127 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
18128 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
18129 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
18131 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18133 cp_lexer_consume_token (parser
->lexer
);
18134 cp_parser_skip_to_closing_parenthesis (parser
,
18135 /*recovering*/false,
18137 /*consume_paren*/true);
18141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
18143 have_trailing_return_fn_decl
= true;
18147 cp_lexer_consume_token (parser
->lexer
);
18149 cp_parser_abort_tentative_parse (parser
);
18151 if (have_trailing_return_fn_decl
)
18153 type
= make_auto ();
18157 if (cxx_dialect
>= cxx14
)
18159 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
18160 type
= TREE_TYPE (type
);
18163 type
= error_mark_node
;
18165 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
18167 if (cxx_dialect
< cxx14
)
18168 error_at (token
->location
,
18169 "use of %<auto%> in lambda parameter declaration "
18170 "only available with "
18171 "%<-std=c++14%> or %<-std=gnu++14%>");
18173 else if (cxx_dialect
< cxx14
)
18174 error_at (token
->location
,
18175 "use of %<auto%> in parameter declaration "
18176 "only available with "
18177 "%<-std=c++14%> or %<-std=gnu++14%>");
18178 else if (!flag_concepts
)
18179 pedwarn (token
->location
, 0,
18180 "use of %<auto%> in parameter declaration "
18181 "only available with %<-fconcepts-ts%>");
18184 type
= make_auto ();
18188 /* Since DR 743, decltype can either be a simple-type-specifier by
18189 itself or begin a nested-name-specifier. Parsing it will replace
18190 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18191 handling below decide what to do. */
18192 cp_parser_decltype (parser
);
18193 cp_lexer_set_token_position (parser
->lexer
, token
);
18197 /* Consume the `typeof' token. */
18198 cp_lexer_consume_token (parser
->lexer
);
18199 /* Parse the operand to `typeof'. */
18200 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
18201 /* If it is not already a TYPE, take its type. */
18202 if (!TYPE_P (type
))
18203 type
= finish_typeof (type
);
18206 cp_parser_set_decl_spec_type (decl_specs
, type
,
18208 /*type_definition_p=*/false);
18212 case RID_UNDERLYING_TYPE
:
18213 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
18215 cp_parser_set_decl_spec_type (decl_specs
, type
,
18217 /*type_definition_p=*/false);
18222 case RID_DIRECT_BASES
:
18223 type
= cp_parser_trait_expr (parser
, token
->keyword
);
18225 cp_parser_set_decl_spec_type (decl_specs
, type
,
18227 /*type_definition_p=*/false);
18233 /* If token is an already-parsed decltype not followed by ::,
18234 it's a simple-type-specifier. */
18235 if (token
->type
== CPP_DECLTYPE
18236 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
18238 type
= saved_checks_value (token
->u
.tree_check_value
);
18241 cp_parser_set_decl_spec_type (decl_specs
, type
,
18243 /*type_definition_p=*/false);
18244 /* Remember that we are handling a decltype in order to
18245 implement the resolution of DR 1510 when the argument
18246 isn't instantiation dependent. */
18247 decl_specs
->decltype_p
= true;
18249 cp_lexer_consume_token (parser
->lexer
);
18253 /* If the type-specifier was for a built-in type, we're done. */
18256 /* Record the type. */
18258 && (token
->keyword
!= RID_SIGNED
18259 && token
->keyword
!= RID_UNSIGNED
18260 && token
->keyword
!= RID_SHORT
18261 && token
->keyword
!= RID_LONG
))
18262 cp_parser_set_decl_spec_type (decl_specs
,
18265 /*type_definition_p=*/false);
18267 decl_specs
->any_specifiers_p
= true;
18269 /* Consume the token. */
18270 cp_lexer_consume_token (parser
->lexer
);
18272 if (type
== error_mark_node
)
18273 return error_mark_node
;
18275 /* There is no valid C++ program where a non-template type is
18276 followed by a "<". That usually indicates that the user thought
18277 that the type was a template. */
18278 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
18281 return TYPE_NAME (type
);
18284 /* The type-specifier must be a user-defined type. */
18285 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
18289 const bool typename_p
= (cxx_dialect
>= cxx20
18290 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
18292 /* Don't gobble tokens or issue error messages if this is an
18293 optional type-specifier. */
18294 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
18295 cp_parser_parse_tentatively (parser
);
18297 /* Remember current tentative parsing state -- if we know we need
18298 a type, we can give better diagnostics here. */
18299 bool tent
= cp_parser_parsing_tentatively (parser
);
18301 token
= cp_lexer_peek_token (parser
->lexer
);
18303 /* Look for the optional `::' operator. */
18305 = (cp_parser_global_scope_opt (parser
,
18306 /*current_scope_valid_p=*/false)
18308 /* Look for the nested-name specifier. */
18310 = (cp_parser_nested_name_specifier_opt (parser
,
18311 /*typename_keyword_p=*/false,
18312 /*check_dependency_p=*/true,
18314 /*is_declaration=*/false)
18316 /* If we have seen a nested-name-specifier, and the next token
18317 is `template', then we are using the template-id production. */
18319 && cp_parser_optional_template_keyword (parser
))
18321 /* Look for the template-id. */
18322 type
= cp_parser_template_id (parser
,
18323 /*template_keyword_p=*/true,
18324 /*check_dependency_p=*/true,
18326 /*is_declaration=*/false);
18327 /* If the template-id did not name a type, we are out of
18329 if (TREE_CODE (type
) != TYPE_DECL
)
18331 /* ...unless we pretend we have seen 'typename'. */
18333 type
= cp_parser_make_typename_type (parser
, type
,
18337 cp_parser_error (parser
, "expected template-id for type");
18338 type
= error_mark_node
;
18342 /* DR 1812: A < following a qualified-id in a typename-specifier
18343 could safely be assumed to begin a template argument list, so
18344 the template keyword should be optional. */
18345 else if (parser
->scope
18348 && cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
))
18350 cp_parser_parse_tentatively (parser
);
18352 type
= cp_parser_template_id (parser
,
18353 /*template_keyword_p=*/true,
18354 /*check_dependency_p=*/true,
18356 /*is_declaration=*/false);
18357 /* This is handled below, so back off. */
18358 if (type
&& concept_check_p (type
))
18359 cp_parser_simulate_error (parser
);
18361 if (!cp_parser_parse_definitely (parser
))
18363 else if (TREE_CODE (type
) == TEMPLATE_ID_EXPR
)
18364 type
= make_typename_type (parser
->scope
, type
, typename_type
,
18365 /*complain=*/tf_error
);
18366 else if (TREE_CODE (type
) != TYPE_DECL
)
18370 /* Otherwise, look for a type-name. */
18373 if (cxx_dialect
>= cxx17
)
18374 cp_parser_parse_tentatively (parser
);
18376 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
18378 if (cxx_dialect
>= cxx17
&& !cp_parser_parse_definitely (parser
))
18382 if (!type
&& flag_concepts
&& decl_specs
)
18384 /* Try for a type-constraint with template arguments. We check
18385 decl_specs here to avoid trying this for a functional cast. */
18387 cp_parser_parse_tentatively (parser
);
18389 type
= cp_parser_template_id (parser
,
18390 /*template_keyword_p=*/false,
18391 /*check_dependency_p=*/true,
18393 /*is_declaration=*/false);
18394 if (type
&& concept_check_p (type
))
18396 location_t loc
= EXPR_LOCATION (type
);
18397 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18399 if (tent
&& type
== error_mark_node
)
18400 /* Perhaps it's a concept-check expression. */
18401 cp_parser_simulate_error (parser
);
18404 cp_parser_simulate_error (parser
);
18406 if (!cp_parser_parse_definitely (parser
))
18410 if (!type
&& cxx_dialect
>= cxx17
)
18412 /* Try class template argument deduction or type-constraint without
18413 template arguments. */
18414 tree name
= cp_parser_identifier (parser
);
18415 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
18416 && parser
->scope
!= error_mark_node
)
18419 = cp_lexer_previous_token (parser
->lexer
)->location
;
18420 tree tmpl
= cp_parser_lookup_name (parser
, name
,
18422 /*is_template=*/false,
18423 /*is_namespace=*/false,
18424 /*check_dependency=*/true,
18425 /*ambiguous_decls=*/NULL
,
18427 if (tmpl
&& tmpl
!= error_mark_node
18428 && ctad_template_p (tmpl
))
18429 type
= make_template_placeholder (tmpl
);
18430 else if (flag_concepts
&& tmpl
&& concept_definition_p (tmpl
))
18431 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18435 type
= error_mark_node
;
18436 if (!cp_parser_simulate_error (parser
))
18437 cp_parser_name_lookup_error (parser
, name
, tmpl
,
18438 NLE_TYPE
, token
->location
);
18442 type
= error_mark_node
;
18445 /* If it didn't work out, we don't have a TYPE. */
18446 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
18447 && !cp_parser_parse_definitely (parser
))
18450 /* Keep track of all name-lookups performed in class scopes. */
18454 && TREE_CODE (type
) == TYPE_DECL
18455 && identifier_p (DECL_NAME (type
)))
18456 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
18458 if (type
&& decl_specs
)
18459 cp_parser_set_decl_spec_type (decl_specs
, type
,
18461 /*type_definition_p=*/false);
18464 /* If we didn't get a type-name, issue an error message. */
18465 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
18467 cp_parser_error (parser
, "expected type-name");
18468 return error_mark_node
;
18471 if (type
&& type
!= error_mark_node
)
18473 /* See if TYPE is an Objective-C type, and if so, parse and
18474 accept any protocol references following it. Do this before
18475 the cp_parser_check_for_invalid_template_id() call, because
18476 Objective-C types can be followed by '<...>' which would
18477 enclose protocol names rather than template arguments, and so
18478 everything is fine. */
18479 if (c_dialect_objc () && !parser
->scope
18480 && (objc_is_id (type
) || objc_is_class_name (type
)))
18482 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18483 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
18485 /* Clobber the "unqualified" type previously entered into
18486 DECL_SPECS with the new, improved protocol-qualified version. */
18488 decl_specs
->type
= qual_type
;
18493 /* There is no valid C++ program where a non-template type is
18494 followed by a "<". That usually indicates that the user
18495 thought that the type was a template. */
18496 cp_parser_check_for_invalid_template_id (parser
, type
,
18504 /* Parse the remainder of a placholder-type-specifier.
18506 placeholder-type-specifier:
18507 type-constraint_opt auto
18508 type-constraint_opt decltype(auto)
18510 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18511 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18512 parses the placeholder type, and performs some contextual syntactic analysis.
18514 LOC provides the location of the template name.
18516 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18517 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18518 might actually be a concept-check,
18520 Note that the Concepts TS allows the auto or decltype(auto) to be
18521 omitted in a constrained-type-specifier. */
18524 cp_parser_placeholder_type_specifier (cp_parser
*parser
, location_t loc
,
18525 tree tmpl
, bool tentative
)
18527 if (tmpl
== error_mark_node
)
18528 return error_mark_node
;
18530 tree orig_tmpl
= tmpl
;
18532 /* Get the arguments as written for subsequent analysis. */
18533 tree args
= NULL_TREE
;
18534 if (TREE_CODE (tmpl
) == TEMPLATE_ID_EXPR
)
18536 args
= TREE_OPERAND (tmpl
, 1);
18537 tmpl
= TREE_OPERAND (tmpl
, 0);
18539 if (args
== NULL_TREE
)
18540 /* A concept-name with no arguments can't be an expression. */
18543 tsubst_flags_t complain
= tentative
? tf_none
: tf_warning_or_error
;
18545 /* Get the concept and prototype parameter for the constraint. */
18546 tree_pair info
= finish_type_constraints (tmpl
, args
, complain
);
18547 tree con
= info
.first
;
18548 tree proto
= info
.second
;
18549 if (con
== error_mark_node
)
18550 return error_mark_node
;
18552 /* As per the standard, require auto or decltype(auto), except in some
18553 cases (template parameter lists, -fconcepts-ts enabled). */
18554 cp_token
*placeholder
= NULL
, *close_paren
= NULL
;
18555 if (cxx_dialect
>= cxx20
)
18557 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
18558 placeholder
= cp_lexer_consume_token (parser
->lexer
);
18559 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DECLTYPE
))
18561 placeholder
= cp_lexer_consume_token (parser
->lexer
);
18562 matching_parens parens
;
18563 parens
.require_open (parser
);
18564 cp_parser_require_keyword (parser
, RID_AUTO
, RT_AUTO
);
18565 close_paren
= parens
.require_close (parser
);
18569 /* A type constraint constrains a contextually determined type or type
18570 parameter pack. However, the Concepts TS does allow concepts
18571 to introduce non-type and template template parameters. */
18572 if (TREE_CODE (proto
) != TYPE_DECL
)
18574 if (!flag_concepts_ts
18575 || !processing_template_parmlist
)
18577 error_at (loc
, "%qE does not constrain a type", DECL_NAME (con
));
18578 inform (DECL_SOURCE_LOCATION (con
), "concept defined here");
18579 return error_mark_node
;
18583 /* In a template parameter list, a type-parameter can be introduced
18584 by type-constraints alone. */
18585 if (processing_template_parmlist
&& !placeholder
)
18586 return build_constrained_parameter (con
, proto
, args
);
18588 /* Diagnose issues placeholder issues. */
18589 if (!flag_concepts_ts
18590 && !parser
->in_result_type_constraint_p
18594 /* Perhaps it's a concept-check expression (c++/91073). */
18595 return error_mark_node
;
18597 tree id
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, args
);
18598 tree expr
= DECL_P (orig_tmpl
) ? DECL_NAME (con
) : id
;
18599 error_at (input_location
,
18600 "expected %<auto%> or %<decltype(auto)%> after %qE", expr
);
18601 /* Fall through. This is an error of omission. */
18603 else if (parser
->in_result_type_constraint_p
&& placeholder
)
18605 /* A trailing return type only allows type-constraints. */
18606 error_at (input_location
,
18607 "unexpected placeholder in constrained result type");
18610 /* In a parameter-declaration-clause, a placeholder-type-specifier
18611 results in an invented template parameter. */
18612 if (parser
->auto_is_implicit_function_template_parm_p
)
18616 location_t loc
= make_location (placeholder
->location
,
18617 placeholder
->location
,
18618 close_paren
->location
);
18619 error_at (loc
, "cannot declare a parameter with %<decltype(auto)%>");
18620 return error_mark_node
;
18622 tree parm
= build_constrained_parameter (con
, proto
, args
);
18623 return synthesize_implicit_template_parm (parser
, parm
);
18626 /* Determine if the type should be deduced using template argument
18627 deduction or decltype deduction. Note that the latter is always
18628 used for type-constraints in trailing return types. */
18629 bool decltype_p
= placeholder
18630 ? placeholder
->keyword
== RID_DECLTYPE
18631 : parser
->in_result_type_constraint_p
;
18633 /* Otherwise, this is the type of a variable or return type. */
18635 return make_constrained_decltype_auto (con
, args
);
18637 return make_constrained_auto (con
, args
);
18640 /* Parse a type-name.
18646 simple-template-id [in c++0x]
18663 Returns a TYPE_DECL for the type. */
18666 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
18670 /* We can't know yet whether it is a class-name or not. */
18671 cp_parser_parse_tentatively (parser
);
18672 /* Try a class-name. */
18673 type_decl
= cp_parser_class_name (parser
,
18674 typename_keyword_p
,
18675 /*template_keyword_p=*/false,
18677 /*check_dependency_p=*/true,
18678 /*class_head_p=*/false,
18679 /*is_declaration=*/false);
18680 /* If it's not a class-name, keep looking. */
18681 if (!cp_parser_parse_definitely (parser
))
18683 if (cxx_dialect
< cxx11
)
18684 /* It must be a typedef-name or an enum-name. */
18685 return cp_parser_nonclass_name (parser
);
18687 cp_parser_parse_tentatively (parser
);
18688 /* It is either a simple-template-id representing an
18689 instantiation of an alias template... */
18690 type_decl
= cp_parser_template_id (parser
,
18691 /*template_keyword_p=*/false,
18692 /*check_dependency_p=*/true,
18694 /*is_declaration=*/false);
18695 /* Note that this must be an instantiation of an alias template
18696 because [temp.names]/6 says:
18698 A template-id that names an alias template specialization
18701 Whereas [temp.names]/7 says:
18703 A simple-template-id that names a class template
18704 specialization is a class-name.
18706 With concepts, this could also be a partial-concept-id that
18707 declares a non-type template parameter. */
18708 if (type_decl
!= NULL_TREE
18709 && TREE_CODE (type_decl
) == TYPE_DECL
18710 && TYPE_DECL_ALIAS_P (type_decl
))
18711 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
18713 cp_parser_simulate_error (parser
);
18715 if (!cp_parser_parse_definitely (parser
))
18716 /* ... Or a typedef-name or an enum-name. */
18717 return cp_parser_nonclass_name (parser
);
18723 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18735 Returns a TYPE_DECL for the type. */
18738 cp_parser_nonclass_name (cp_parser
* parser
)
18743 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18744 identifier
= cp_parser_identifier (parser
);
18745 if (identifier
== error_mark_node
)
18746 return error_mark_node
;
18748 /* Look up the type-name. */
18749 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18751 type_decl
= strip_using_decl (type_decl
);
18753 if (TREE_CODE (type_decl
) != TYPE_DECL
18754 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18756 /* See if this is an Objective-C type. */
18757 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18758 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18760 type_decl
= TYPE_NAME (type
);
18763 /* Issue an error if we did not find a type-name. */
18764 if (TREE_CODE (type_decl
) != TYPE_DECL
18765 /* In Objective-C, we have the complication that class names are
18766 normally type names and start declarations (eg, the
18767 "NSObject" in "NSObject *object;"), but can be used in an
18768 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18769 is an expression. So, a classname followed by a dot is not a
18770 valid type-name. */
18771 || (objc_is_class_name (TREE_TYPE (type_decl
))
18772 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18774 if (!cp_parser_simulate_error (parser
))
18775 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18776 NLE_TYPE
, token
->location
);
18777 return error_mark_node
;
18779 /* Remember that the name was used in the definition of the
18780 current class so that we can check later to see if the
18781 meaning would have been different after the class was
18782 entirely defined. */
18783 else if (type_decl
!= error_mark_node
18785 maybe_note_name_used_in_class (identifier
, type_decl
);
18790 /* Parse an elaborated-type-specifier. Note that the grammar given
18791 here incorporates the resolution to DR68.
18793 elaborated-type-specifier:
18794 class-key :: [opt] nested-name-specifier [opt] identifier
18795 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18796 enum-key :: [opt] nested-name-specifier [opt] identifier
18797 typename :: [opt] nested-name-specifier identifier
18798 typename :: [opt] nested-name-specifier template [opt]
18803 elaborated-type-specifier:
18804 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18805 class-key attributes :: [opt] nested-name-specifier [opt]
18806 template [opt] template-id
18807 enum attributes :: [opt] nested-name-specifier [opt] identifier
18809 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18810 declared `friend'. If IS_DECLARATION is TRUE, then this
18811 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18812 something is being declared.
18814 Returns the TYPE specified. */
18817 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18819 bool is_declaration
)
18821 enum tag_types tag_type
;
18823 tree type
= NULL_TREE
;
18824 tree attributes
= NULL_TREE
;
18826 cp_token
*token
= NULL
;
18828 /* For class and enum types the location of the class-key or enum-key. */
18829 location_t key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18830 /* For a scoped enum, the 'class' or 'struct' keyword id. */
18831 rid scoped_key
= RID_MAX
;
18833 /* See if we're looking at the `enum' keyword. */
18834 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18836 /* Consume the `enum' token. */
18837 cp_lexer_consume_token (parser
->lexer
);
18838 /* Remember that it's an enumeration type. */
18839 tag_type
= enum_type
;
18840 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18841 enums) is used here. */
18842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18843 if (cp_parser_is_keyword (token
, scoped_key
= RID_CLASS
)
18844 || cp_parser_is_keyword (token
, scoped_key
= RID_STRUCT
))
18846 location_t loc
= token
->location
;
18847 gcc_rich_location
richloc (loc
);
18848 richloc
.add_range (input_location
);
18849 richloc
.add_fixit_remove ();
18850 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18851 "a scoped enum must not use the %qD keyword",
18853 /* Consume the `struct' or `class' and parse it anyway. */
18854 cp_lexer_consume_token (parser
->lexer
);
18855 /* Create a combined location for the whole scoped-enum-key. */
18856 key_loc
= make_location (key_loc
, key_loc
, loc
);
18859 scoped_key
= RID_MAX
;
18861 /* Parse the attributes. */
18862 attributes
= cp_parser_attributes_opt (parser
);
18864 /* Or, it might be `typename'. */
18865 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18868 /* Consume the `typename' token. */
18869 cp_lexer_consume_token (parser
->lexer
);
18870 /* Remember that it's a `typename' type. */
18871 tag_type
= typename_type
;
18873 /* Otherwise it must be a class-key. */
18876 key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18877 tag_type
= cp_parser_class_key (parser
);
18878 if (tag_type
== none_type
)
18879 return error_mark_node
;
18880 /* Parse the attributes. */
18881 attributes
= cp_parser_attributes_opt (parser
);
18884 /* Look for the `::' operator. */
18885 globalscope
= cp_parser_global_scope_opt (parser
,
18886 /*current_scope_valid_p=*/false);
18887 /* Look for the nested-name-specifier. */
18888 tree nested_name_specifier
;
18889 if (tag_type
== typename_type
&& !globalscope
)
18891 nested_name_specifier
18892 = cp_parser_nested_name_specifier (parser
,
18893 /*typename_keyword_p=*/true,
18894 /*check_dependency_p=*/true,
18897 if (!nested_name_specifier
)
18898 return error_mark_node
;
18901 /* Even though `typename' is not present, the proposed resolution
18902 to Core Issue 180 says that in `class A<T>::B', `B' should be
18903 considered a type-name, even if `A<T>' is dependent. */
18904 nested_name_specifier
18905 = cp_parser_nested_name_specifier_opt (parser
,
18906 /*typename_keyword_p=*/true,
18907 /*check_dependency_p=*/true,
18910 /* For everything but enumeration types, consider a template-id.
18911 For an enumeration type, consider only a plain identifier. */
18912 if (tag_type
!= enum_type
)
18914 bool template_p
= false;
18917 /* Allow the `template' keyword. */
18918 template_p
= cp_parser_optional_template_keyword (parser
);
18919 /* If we didn't see `template', we don't know if there's a
18920 template-id or not. */
18922 cp_parser_parse_tentatively (parser
);
18923 /* The `template' keyword must follow a nested-name-specifier. */
18924 else if (!nested_name_specifier
&& !globalscope
)
18926 cp_parser_error (parser
, "%<template%> must follow a nested-"
18928 return error_mark_node
;
18931 /* Parse the template-id. */
18932 token
= cp_lexer_peek_token (parser
->lexer
);
18933 decl
= cp_parser_template_id (parser
, template_p
,
18934 /*check_dependency_p=*/true,
18937 /* If we didn't find a template-id, look for an ordinary
18939 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18941 /* We can get here when cp_parser_template_id, called by
18942 cp_parser_class_name with tag_type == none_type, succeeds
18943 and caches a BASELINK. Then, when called again here,
18944 instead of failing and returning an error_mark_node
18945 returns it (see template/typename17.C in C++11).
18946 ??? Could we diagnose this earlier? */
18947 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18949 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18950 type
= error_mark_node
;
18952 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18953 in effect, then we must assume that, upon instantiation, the
18954 template will correspond to a class. */
18955 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18956 && tag_type
== typename_type
)
18957 type
= make_typename_type (parser
->scope
, decl
,
18959 /*complain=*/tf_error
);
18960 /* If the `typename' keyword is in effect and DECL is not a type
18961 decl, then type is non existent. */
18962 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18964 else if (TREE_CODE (decl
) == TYPE_DECL
)
18966 type
= check_elaborated_type_specifier (tag_type
, decl
,
18967 /*allow_template_p=*/true);
18969 /* If the next token is a semicolon, this must be a specialization,
18970 instantiation, or friend declaration. Check the scope while we
18971 still know whether or not we had a nested-name-specifier. */
18972 if (type
!= error_mark_node
18973 && !nested_name_specifier
&& !is_friend
18974 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18975 check_unqualified_spec_or_inst (type
, token
->location
);
18977 else if (decl
== error_mark_node
)
18978 type
= error_mark_node
;
18983 token
= cp_lexer_peek_token (parser
->lexer
);
18984 identifier
= cp_parser_identifier (parser
);
18986 if (identifier
== error_mark_node
)
18988 parser
->scope
= NULL_TREE
;
18989 return error_mark_node
;
18992 /* For a `typename', we needn't call xref_tag. */
18993 if (tag_type
== typename_type
18994 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18995 return cp_parser_make_typename_type (parser
, identifier
,
18998 /* Template parameter lists apply only if we are not within a
18999 function parameter list. */
19000 bool template_parm_lists_apply
19001 = parser
->num_template_parameter_lists
;
19002 if (template_parm_lists_apply
)
19003 for (cp_binding_level
*s
= current_binding_level
;
19004 s
&& s
->kind
!= sk_template_parms
;
19005 s
= s
->level_chain
)
19006 if (s
->kind
== sk_function_parms
)
19007 template_parm_lists_apply
= false;
19009 /* Look up a qualified name in the usual way. */
19013 tree ambiguous_decls
;
19015 decl
= cp_parser_lookup_name (parser
, identifier
,
19017 /*is_template=*/false,
19018 /*is_namespace=*/false,
19019 /*check_dependency=*/true,
19023 /* If the lookup was ambiguous, an error will already have been
19025 if (ambiguous_decls
)
19026 return error_mark_node
;
19028 /* If we are parsing friend declaration, DECL may be a
19029 TEMPLATE_DECL tree node here. However, we need to check
19030 whether this TEMPLATE_DECL results in valid code. Consider
19031 the following example:
19034 template <class T> class C {};
19037 template <class T> friend class N::C; // #1, valid code
19039 template <class T> class Y {
19040 friend class N::C; // #2, invalid code
19043 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
19044 name lookup of `N::C'. We see that friend declaration must
19045 be template for the code to be valid. Note that
19046 processing_template_decl does not work here since it is
19047 always 1 for the above two cases. */
19049 decl
= (cp_parser_maybe_treat_template_as_class
19050 (decl
, /*tag_name_p=*/is_friend
19051 && template_parm_lists_apply
));
19053 if (TREE_CODE (decl
) != TYPE_DECL
)
19055 cp_parser_diagnose_invalid_type_name (parser
,
19058 return error_mark_node
;
19061 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
19063 bool allow_template
= (template_parm_lists_apply
19064 || DECL_SELF_REFERENCE_P (decl
));
19065 type
= check_elaborated_type_specifier (tag_type
, decl
,
19068 if (type
== error_mark_node
)
19069 return error_mark_node
;
19072 /* Forward declarations of nested types, such as
19077 are invalid unless all components preceding the final '::'
19078 are complete. If all enclosing types are complete, these
19079 declarations become merely pointless.
19081 Invalid forward declarations of nested types are errors
19082 caught elsewhere in parsing. Those that are pointless arrive
19085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
19086 && !is_friend
&& is_declaration
19087 && !processing_explicit_instantiation
)
19088 warning (0, "declaration %qD does not declare anything", decl
);
19090 type
= TREE_TYPE (decl
);
19094 /* An elaborated-type-specifier sometimes introduces a new type and
19095 sometimes names an existing type. Normally, the rule is that it
19096 introduces a new type only if there is not an existing type of
19097 the same name already in scope. For example, given:
19100 void f() { struct S s; }
19102 the `struct S' in the body of `f' is the same `struct S' as in
19103 the global scope; the existing definition is used. However, if
19104 there were no global declaration, this would introduce a new
19105 local class named `S'.
19107 An exception to this rule applies to the following code:
19109 namespace N { struct S; }
19111 Here, the elaborated-type-specifier names a new type
19112 unconditionally; even if there is already an `S' in the
19113 containing scope this declaration names a new type.
19114 This exception only applies if the elaborated-type-specifier
19115 forms the complete declaration:
19119 A declaration consisting solely of `class-key identifier ;' is
19120 either a redeclaration of the name in the current scope or a
19121 forward declaration of the identifier as a class name. It
19122 introduces the name into the current scope.
19124 We are in this situation precisely when the next token is a `;'.
19126 An exception to the exception is that a `friend' declaration does
19127 *not* name a new type; i.e., given:
19129 struct S { friend struct T; };
19131 `T' is not a new type in the scope of `S'.
19133 Also, `new struct S' or `sizeof (struct S)' never results in the
19134 definition of a new type; a new type can only be declared in a
19135 declaration context. */
19140 /* Friends have special name lookup rules. */
19141 how
= TAG_how::HIDDEN_FRIEND
;
19142 else if (is_declaration
19143 && cp_lexer_next_token_is (parser
->lexer
,
19145 /* This is a `class-key identifier ;' */
19146 how
= TAG_how::CURRENT_ONLY
;
19148 how
= TAG_how::GLOBAL
;
19151 (template_parm_lists_apply
19152 && (cp_parser_next_token_starts_class_definition_p (parser
)
19153 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
19154 /* An unqualified name was used to reference this type, so
19155 there were no qualifying templates. */
19156 if (template_parm_lists_apply
19157 && !cp_parser_check_template_parameters (parser
,
19158 /*num_templates=*/0,
19159 /*template_id*/false,
19161 /*declarator=*/NULL
))
19162 return error_mark_node
;
19164 type
= xref_tag (tag_type
, identifier
, how
, template_p
);
19168 if (type
== error_mark_node
)
19169 return error_mark_node
;
19171 /* Allow attributes on forward declarations of classes. */
19174 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19175 warning (OPT_Wattributes
,
19176 "attributes ignored on uninstantiated type");
19177 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
19178 && ! processing_explicit_instantiation
)
19179 warning (OPT_Wattributes
,
19180 "attributes ignored on template instantiation");
19181 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
19182 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
19184 warning (OPT_Wattributes
,
19185 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19188 if (tag_type
== enum_type
)
19189 cp_parser_maybe_warn_enum_key (parser
, key_loc
, type
, scoped_key
);
19192 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19193 for alias definition. */
19194 bool decl_class
= (is_declaration
19195 && cp_parser_declares_only_class_p (parser
));
19196 cp_parser_check_class_key (parser
, key_loc
, tag_type
, type
, false,
19199 /* Indicate whether this class was declared as a `class' or as a
19201 if (CLASS_TYPE_P (type
) && !currently_open_class (type
))
19202 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
19205 /* A "<" cannot follow an elaborated type specifier. If that
19206 happens, the user was probably trying to form a template-id. */
19207 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
19213 /* Parse an enum-specifier.
19216 enum-head { enumerator-list [opt] }
19217 enum-head { enumerator-list , } [C++0x]
19220 enum-key identifier [opt] enum-base [opt]
19221 enum-key nested-name-specifier identifier enum-base [opt]
19226 enum struct [C++0x]
19229 : type-specifier-seq
19231 opaque-enum-specifier:
19232 enum-key identifier enum-base [opt] ;
19235 enum-key attributes[opt] identifier [opt] enum-base [opt]
19236 { enumerator-list [opt] }attributes[opt]
19237 enum-key attributes[opt] identifier [opt] enum-base [opt]
19238 { enumerator-list, }attributes[opt] [C++0x]
19240 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19241 if the token stream isn't an enum-specifier after all. */
19244 cp_parser_enum_specifier (cp_parser
* parser
)
19247 tree type
= NULL_TREE
;
19249 tree nested_name_specifier
= NULL_TREE
;
19251 bool scoped_enum_p
= false;
19252 bool has_underlying_type
= false;
19253 bool nested_being_defined
= false;
19254 bool new_value_list
= false;
19255 bool is_new_type
= false;
19256 bool is_unnamed
= false;
19257 tree underlying_type
= NULL_TREE
;
19258 cp_token
*type_start_token
= NULL
;
19259 temp_override
<bool> cleanup (parser
->colon_corrects_to_scope_p
, false);
19261 /* Parse tentatively so that we can back up if we don't find a
19263 cp_parser_parse_tentatively (parser
);
19265 /* Caller guarantees that the current token is 'enum', an identifier
19266 possibly follows, and the token after that is an opening brace.
19267 If we don't have an identifier, fabricate an anonymous name for
19268 the enumeration being defined. */
19269 cp_lexer_consume_token (parser
->lexer
);
19271 /* Parse the "class" or "struct", which indicates a scoped
19272 enumeration type in C++0x. */
19273 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
19274 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
19276 if (cxx_dialect
< cxx11
)
19277 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19279 /* Consume the `struct' or `class' token. */
19280 cp_lexer_consume_token (parser
->lexer
);
19282 scoped_enum_p
= true;
19285 attributes
= cp_parser_attributes_opt (parser
);
19287 /* Clear the qualification. */
19288 parser
->scope
= NULL_TREE
;
19289 parser
->qualifying_scope
= NULL_TREE
;
19290 parser
->object_scope
= NULL_TREE
;
19292 /* Figure out in what scope the declaration is being placed. */
19293 prev_scope
= current_scope ();
19295 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19297 push_deferring_access_checks (dk_no_check
);
19298 nested_name_specifier
19299 = cp_parser_nested_name_specifier_opt (parser
,
19300 /*typename_keyword_p=*/true,
19301 /*check_dependency_p=*/false,
19303 /*is_declaration=*/false);
19305 if (nested_name_specifier
)
19309 identifier
= cp_parser_identifier (parser
);
19310 name
= cp_parser_lookup_name (parser
, identifier
,
19312 /*is_template=*/false,
19313 /*is_namespace=*/false,
19314 /*check_dependency=*/true,
19315 /*ambiguous_decls=*/NULL
,
19317 if (name
&& name
!= error_mark_node
)
19319 type
= TREE_TYPE (name
);
19320 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19322 /* Are template enums allowed in ISO? */
19323 if (template_parm_scope_p ())
19324 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
19325 "%qD is an enumeration template", name
);
19326 /* ignore a typename reference, for it will be solved by name
19331 else if (nested_name_specifier
== error_mark_node
)
19332 /* We already issued an error. */;
19335 error_at (type_start_token
->location
,
19336 "%qD does not name an enumeration in %qT",
19337 identifier
, nested_name_specifier
);
19338 nested_name_specifier
= error_mark_node
;
19343 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19344 identifier
= cp_parser_identifier (parser
);
19347 identifier
= make_anon_name ();
19350 error_at (type_start_token
->location
,
19351 "unnamed scoped enum is not allowed");
19354 pop_deferring_access_checks ();
19356 /* Check for the `:' that denotes a specified underlying type in C++0x.
19357 Note that a ':' could also indicate a bitfield width, however. */
19358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19360 cp_decl_specifier_seq type_specifiers
;
19362 /* Consume the `:'. */
19363 cp_lexer_consume_token (parser
->lexer
);
19365 /* Parse the type-specifier-seq. */
19366 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
19367 /*is_declaration=*/false,
19368 /*is_trailing_return=*/false,
19371 /* At this point this is surely not elaborated type specifier. */
19372 if (!cp_parser_parse_definitely (parser
))
19375 if (cxx_dialect
< cxx11
)
19376 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19378 has_underlying_type
= true;
19380 /* If that didn't work, stop. */
19381 if (type_specifiers
.type
!= error_mark_node
)
19383 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
19384 /*initialized=*/0, NULL
);
19385 if (underlying_type
== error_mark_node
19386 || check_for_bare_parameter_packs (underlying_type
))
19387 underlying_type
= NULL_TREE
;
19391 /* Look for the `{' but don't consume it yet. */
19392 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19394 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
19396 if (has_underlying_type
)
19397 cp_parser_commit_to_tentative_parse (parser
);
19398 cp_parser_error (parser
, "expected %<{%>");
19399 if (has_underlying_type
)
19400 return error_mark_node
;
19402 /* An opaque-enum-specifier must have a ';' here. */
19403 if ((scoped_enum_p
|| underlying_type
)
19404 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19406 if (has_underlying_type
)
19407 cp_parser_commit_to_tentative_parse (parser
);
19408 cp_parser_error (parser
, "expected %<;%> or %<{%>");
19409 if (has_underlying_type
)
19410 return error_mark_node
;
19414 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
19417 if (nested_name_specifier
)
19419 if (CLASS_TYPE_P (nested_name_specifier
))
19421 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
19422 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
19423 push_scope (nested_name_specifier
);
19425 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19426 push_nested_namespace (nested_name_specifier
);
19429 /* Issue an error message if type-definitions are forbidden here. */
19430 if (!cp_parser_check_type_definition (parser
))
19431 type
= error_mark_node
;
19433 /* Create the new type. We do this before consuming the opening
19434 brace so the enum will be recorded as being on the line of its
19435 tag (or the 'enum' keyword, if there is no tag). */
19436 type
= start_enum (identifier
, type
, underlying_type
,
19437 attributes
, scoped_enum_p
, &is_new_type
);
19439 /* If the next token is not '{' it is an opaque-enum-specifier or an
19440 elaborated-type-specifier. */
19441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19443 timevar_push (TV_PARSE_ENUM
);
19444 if (nested_name_specifier
19445 && nested_name_specifier
!= error_mark_node
)
19447 /* The following catches invalid code such as:
19448 enum class S<int>::E { A, B, C }; */
19449 if (!processing_specialization
19450 && CLASS_TYPE_P (nested_name_specifier
)
19451 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
19452 error_at (type_start_token
->location
, "cannot add an enumerator "
19453 "list to a template instantiation");
19455 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
19457 error_at (type_start_token
->location
,
19458 "%<%T::%E%> has not been declared",
19459 TYPE_CONTEXT (nested_name_specifier
),
19460 nested_name_specifier
);
19461 type
= error_mark_node
;
19463 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
19464 && !CLASS_TYPE_P (nested_name_specifier
))
19466 error_at (type_start_token
->location
, "nested name specifier "
19467 "%qT for enum declaration does not name a class "
19468 "or namespace", nested_name_specifier
);
19469 type
= error_mark_node
;
19471 /* If that scope does not contain the scope in which the
19472 class was originally declared, the program is invalid. */
19473 else if (prev_scope
&& !is_ancestor (prev_scope
,
19474 nested_name_specifier
))
19476 if (at_namespace_scope_p ())
19477 error_at (type_start_token
->location
,
19478 "declaration of %qD in namespace %qD which does not "
19480 type
, prev_scope
, nested_name_specifier
);
19482 error_at (type_start_token
->location
,
19483 "declaration of %qD in %qD which does not "
19485 type
, prev_scope
, nested_name_specifier
);
19486 type
= error_mark_node
;
19488 /* If that scope is the scope where the declaration is being placed
19489 the program is invalid. */
19490 else if (CLASS_TYPE_P (nested_name_specifier
)
19491 && CLASS_TYPE_P (prev_scope
)
19492 && same_type_p (nested_name_specifier
, prev_scope
))
19494 permerror (type_start_token
->location
,
19495 "extra qualification not allowed");
19496 nested_name_specifier
= NULL_TREE
;
19501 begin_scope (sk_scoped_enum
, type
);
19503 /* Consume the opening brace. */
19504 matching_braces braces
;
19505 braces
.consume_open (parser
);
19507 if (type
== error_mark_node
)
19508 ; /* Nothing to add */
19509 else if (OPAQUE_ENUM_P (type
)
19510 || (cxx_dialect
> cxx98
&& processing_specialization
))
19512 new_value_list
= true;
19513 SET_OPAQUE_ENUM_P (type
, false);
19514 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
19518 error_at (type_start_token
->location
,
19519 "multiple definition of %q#T", type
);
19520 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
19521 "previous definition here");
19522 type
= error_mark_node
;
19525 if (type
== error_mark_node
)
19526 cp_parser_skip_to_end_of_block_or_statement (parser
);
19527 /* If the next token is not '}', then there are some enumerators. */
19528 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19530 if (is_unnamed
&& !scoped_enum_p
)
19531 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
19532 "ISO C++ forbids empty unnamed enum");
19536 /* We've seen a '{' so we know we're in an enum-specifier.
19537 Commit to any tentative parse to get syntax errors. */
19538 cp_parser_commit_to_tentative_parse (parser
);
19539 cp_parser_enumerator_list (parser
, type
);
19542 /* Consume the final '}'. */
19543 braces
.require_close (parser
);
19547 timevar_pop (TV_PARSE_ENUM
);
19551 /* If a ';' follows, then it is an opaque-enum-specifier
19552 and additional restrictions apply. */
19553 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19556 error_at (type_start_token
->location
,
19557 "opaque-enum-specifier without name");
19558 else if (nested_name_specifier
)
19559 error_at (type_start_token
->location
,
19560 "opaque-enum-specifier must use a simple identifier");
19564 /* Look for trailing attributes to apply to this enumeration, and
19565 apply them if appropriate. */
19566 if (cp_parser_allow_gnu_extensions_p (parser
))
19568 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
19569 cplus_decl_attributes (&type
,
19571 (int) ATTR_FLAG_TYPE_IN_PLACE
);
19574 /* Finish up the enumeration. */
19575 if (type
!= error_mark_node
)
19577 if (new_value_list
)
19578 finish_enum_value_list (type
);
19580 finish_enum (type
);
19583 if (nested_name_specifier
)
19585 if (CLASS_TYPE_P (nested_name_specifier
))
19587 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
19588 pop_scope (nested_name_specifier
);
19590 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19591 pop_nested_namespace (nested_name_specifier
);
19596 /* Parse an enumerator-list. The enumerators all have the indicated
19600 enumerator-definition
19601 enumerator-list , enumerator-definition */
19604 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
19608 /* Parse an enumerator-definition. */
19609 cp_parser_enumerator_definition (parser
, type
);
19611 /* If the next token is not a ',', we've reached the end of
19613 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19615 /* Otherwise, consume the `,' and keep going. */
19616 cp_lexer_consume_token (parser
->lexer
);
19617 /* If the next token is a `}', there is a trailing comma. */
19618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19620 if (cxx_dialect
< cxx11
)
19621 pedwarn (input_location
, OPT_Wpedantic
,
19622 "comma at end of enumerator list");
19628 /* Parse an enumerator-definition. The enumerator has the indicated
19631 enumerator-definition:
19633 enumerator = constant-expression
19640 enumerator-definition:
19641 enumerator attributes [opt]
19642 enumerator attributes [opt] = constant-expression */
19645 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19651 /* Save the input location because we are interested in the location
19652 of the identifier and not the location of the explicit value. */
19653 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19655 /* Look for the identifier. */
19656 identifier
= cp_parser_identifier (parser
);
19657 if (identifier
== error_mark_node
)
19660 /* Parse any specified attributes. */
19661 tree attrs
= cp_parser_attributes_opt (parser
);
19663 /* If the next token is an '=', then there is an explicit value. */
19664 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19666 /* Consume the `=' token. */
19667 cp_lexer_consume_token (parser
->lexer
);
19668 /* Parse the value. */
19669 value
= cp_parser_constant_expression (parser
);
19674 /* If we are processing a template, make sure the initializer of the
19675 enumerator doesn't contain any bare template parameter pack. */
19676 if (check_for_bare_parameter_packs (value
))
19677 value
= error_mark_node
;
19679 /* Create the enumerator. */
19680 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19683 /* Parse a namespace-name.
19686 original-namespace-name
19689 Returns the NAMESPACE_DECL for the namespace. */
19692 cp_parser_namespace_name (cp_parser
* parser
)
19695 tree namespace_decl
;
19697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19699 /* Get the name of the namespace. */
19700 identifier
= cp_parser_identifier (parser
);
19701 if (identifier
== error_mark_node
)
19702 return error_mark_node
;
19704 /* Look up the identifier in the currently active scope. Look only
19705 for namespaces, due to:
19707 [basic.lookup.udir]
19709 When looking up a namespace-name in a using-directive or alias
19710 definition, only namespace names are considered.
19714 [basic.lookup.qual]
19716 During the lookup of a name preceding the :: scope resolution
19717 operator, object, function, and enumerator names are ignored.
19719 (Note that cp_parser_qualifying_entity only calls this
19720 function if the token after the name is the scope resolution
19722 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19724 /*is_template=*/false,
19725 /*is_namespace=*/true,
19726 /*check_dependency=*/true,
19727 /*ambiguous_decls=*/NULL
,
19729 /* If it's not a namespace, issue an error. */
19730 if (namespace_decl
== error_mark_node
19731 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19733 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19735 auto_diagnostic_group d
;
19737 if (namespace_decl
== error_mark_node
19738 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19739 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19742 if (const char *suggestion
= hint
.suggestion ())
19744 gcc_rich_location
richloc (token
->location
);
19745 richloc
.add_fixit_replace (suggestion
);
19746 error_at (&richloc
,
19747 "%qD is not a namespace-name; did you mean %qs?",
19748 identifier
, suggestion
);
19751 error_at (token
->location
, "%qD is not a namespace-name",
19755 cp_parser_error (parser
, "expected namespace-name");
19756 namespace_decl
= error_mark_node
;
19759 return namespace_decl
;
19762 /* Parse a namespace-definition.
19764 namespace-definition:
19765 named-namespace-definition
19766 unnamed-namespace-definition
19768 named-namespace-definition:
19769 original-namespace-definition
19770 extension-namespace-definition
19772 original-namespace-definition:
19773 namespace identifier { namespace-body }
19775 extension-namespace-definition:
19776 namespace original-namespace-name { namespace-body }
19778 unnamed-namespace-definition:
19779 namespace { namespace-body } */
19782 cp_parser_namespace_definition (cp_parser
* parser
)
19785 int nested_definition_count
= 0;
19787 cp_ensure_no_omp_declare_simd (parser
);
19788 cp_ensure_no_oacc_routine (parser
);
19790 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19791 const bool topmost_inline_p
= is_inline
;
19795 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19796 cp_lexer_consume_token (parser
->lexer
);
19799 /* Look for the `namespace' keyword. */
19801 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19803 /* Parse any specified attributes before the identifier. */
19804 tree attribs
= cp_parser_attributes_opt (parser
);
19808 identifier
= NULL_TREE
;
19810 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19812 if (nested_inline_p
&& nested_definition_count
!= 0)
19814 if (cxx_dialect
< cxx20
)
19815 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19816 OPT_Wpedantic
, "nested inline namespace definitions only "
19817 "available with %<-std=c++20%> or %<-std=gnu++20%>");
19818 cp_lexer_consume_token (parser
->lexer
);
19821 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19823 identifier
= cp_parser_identifier (parser
);
19825 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19826 pedwarn (input_location
, OPT_Wpedantic
,
19827 "standard attributes on namespaces must precede "
19828 "the namespace name");
19830 /* Parse any attributes specified after the identifier. */
19831 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19834 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19836 /* Don't forget that the innermost namespace might have been
19837 marked as inline. Use |= because we cannot overwrite
19838 IS_INLINE in case the outermost namespace is inline, but
19839 there are no nested inlines. */
19840 is_inline
|= nested_inline_p
;
19844 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19845 pedwarn (input_location
, OPT_Wpedantic
,
19846 "nested namespace definitions only available with "
19847 "%<-std=c++17%> or %<-std=gnu++17%>");
19849 /* Nested namespace names can create new namespaces (unlike
19850 other qualified-ids). */
19851 if (int count
= (identifier
19852 ? push_namespace (identifier
, nested_inline_p
)
19854 nested_definition_count
+= count
;
19856 cp_parser_error (parser
, "nested namespace name required");
19857 cp_lexer_consume_token (parser
->lexer
);
19860 if (nested_definition_count
&& !identifier
)
19861 cp_parser_error (parser
, "namespace name required");
19863 if (nested_definition_count
&& attribs
)
19864 error_at (token
->location
,
19865 "a nested namespace definition cannot have attributes");
19866 if (nested_definition_count
&& topmost_inline_p
)
19867 error_at (token
->location
,
19868 "a nested namespace definition cannot be inline");
19870 /* Start the namespace. */
19871 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19873 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19875 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19877 /* Look for the `{' to validate starting the namespace. */
19878 matching_braces braces
;
19879 if (braces
.require_open (parser
))
19881 /* Parse the body of the namespace. */
19882 cp_parser_namespace_body (parser
);
19884 /* Look for the final `}'. */
19885 braces
.require_close (parser
);
19888 if (has_visibility
)
19889 pop_visibility (1);
19891 /* Pop the nested namespace definitions. */
19892 while (nested_definition_count
--)
19896 /* Parse a namespace-body.
19899 declaration-seq [opt] */
19902 cp_parser_namespace_body (cp_parser
* parser
)
19904 cp_parser_declaration_seq_opt (parser
);
19907 /* Parse a namespace-alias-definition.
19909 namespace-alias-definition:
19910 namespace identifier = qualified-namespace-specifier ; */
19913 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19916 tree namespace_specifier
;
19918 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19920 /* Look for the `namespace' keyword. */
19921 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19922 /* Look for the identifier. */
19923 identifier
= cp_parser_identifier (parser
);
19924 if (identifier
== error_mark_node
)
19926 /* Look for the `=' token. */
19927 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19928 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19930 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19931 /* Skip the definition. */
19932 cp_lexer_consume_token (parser
->lexer
);
19933 if (cp_parser_skip_to_closing_brace (parser
))
19934 cp_lexer_consume_token (parser
->lexer
);
19937 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19938 /* Look for the qualified-namespace-specifier. */
19939 namespace_specifier
19940 = cp_parser_qualified_namespace_specifier (parser
);
19941 cp_warn_deprecated_use_scopes (namespace_specifier
);
19942 /* Look for the `;' token. */
19943 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19945 /* Register the alias in the symbol table. */
19946 do_namespace_alias (identifier
, namespace_specifier
);
19949 /* Parse a qualified-namespace-specifier.
19951 qualified-namespace-specifier:
19952 :: [opt] nested-name-specifier [opt] namespace-name
19954 Returns a NAMESPACE_DECL corresponding to the specified
19958 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19960 /* Look for the optional `::'. */
19961 cp_parser_global_scope_opt (parser
,
19962 /*current_scope_valid_p=*/false);
19964 /* Look for the optional nested-name-specifier. */
19965 cp_parser_nested_name_specifier_opt (parser
,
19966 /*typename_keyword_p=*/false,
19967 /*check_dependency_p=*/true,
19969 /*is_declaration=*/true);
19971 return cp_parser_namespace_name (parser
);
19974 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19975 access declaration.
19978 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19979 using :: unqualified-id ;
19981 access-declaration:
19987 cp_parser_using_declaration (cp_parser
* parser
,
19988 bool access_declaration_p
)
19991 bool typename_p
= false;
19992 bool global_scope_p
;
19996 int oldcount
= errorcount
;
19997 cp_token
*diag_token
= NULL
;
19999 if (access_declaration_p
)
20001 diag_token
= cp_lexer_peek_token (parser
->lexer
);
20002 cp_parser_parse_tentatively (parser
);
20006 /* Look for the `using' keyword. */
20007 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20010 /* Peek at the next token. */
20011 token
= cp_lexer_peek_token (parser
->lexer
);
20012 /* See if it's `typename'. */
20013 if (token
->keyword
== RID_TYPENAME
)
20015 /* Remember that we've seen it. */
20017 /* Consume the `typename' token. */
20018 cp_lexer_consume_token (parser
->lexer
);
20022 /* Look for the optional global scope qualification. */
20024 = (cp_parser_global_scope_opt (parser
,
20025 /*current_scope_valid_p=*/false)
20028 /* If we saw `typename', or didn't see `::', then there must be a
20029 nested-name-specifier present. */
20030 if (typename_p
|| !global_scope_p
)
20032 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
20033 /*check_dependency_p=*/true,
20035 /*is_declaration=*/true);
20036 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
20038 cp_parser_skip_to_end_of_block_or_statement (parser
);
20042 /* Otherwise, we could be in either of the two productions. In that
20043 case, treat the nested-name-specifier as optional. */
20045 qscope
= cp_parser_nested_name_specifier_opt (parser
,
20046 /*typename_keyword_p=*/false,
20047 /*check_dependency_p=*/true,
20049 /*is_declaration=*/true);
20051 qscope
= global_namespace
;
20052 else if (UNSCOPED_ENUM_P (qscope
)
20053 && !TYPE_FUNCTION_SCOPE_P (qscope
))
20054 qscope
= CP_TYPE_CONTEXT (qscope
);
20056 cp_warn_deprecated_use_scopes (qscope
);
20058 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
20059 /* Something has already gone wrong; there's no need to parse
20060 further. Since an error has occurred, the return value of
20061 cp_parser_parse_definitely will be false, as required. */
20062 return cp_parser_parse_definitely (parser
);
20064 token
= cp_lexer_peek_token (parser
->lexer
);
20065 /* Parse the unqualified-id. */
20066 identifier
= cp_parser_unqualified_id (parser
,
20067 /*template_keyword_p=*/false,
20068 /*check_dependency_p=*/true,
20069 /*declarator_p=*/true,
20070 /*optional_p=*/false);
20072 if (access_declaration_p
)
20074 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20075 cp_parser_simulate_error (parser
);
20076 if (!cp_parser_parse_definitely (parser
))
20079 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20081 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
20082 if (cxx_dialect
< cxx17
)
20083 pedwarn (ell
->location
, 0,
20084 "pack expansion in using-declaration only available "
20085 "with %<-std=c++17%> or %<-std=gnu++17%>");
20086 qscope
= make_pack_expansion (qscope
);
20089 /* The function we call to handle a using-declaration is different
20090 depending on what scope we are in. */
20091 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
20093 else if (!identifier_p (identifier
)
20094 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
20095 /* [namespace.udecl]
20097 A using declaration shall not name a template-id. */
20098 error_at (token
->location
,
20099 "a template-id may not appear in a using-declaration");
20102 if (at_class_scope_p ())
20104 /* Create the USING_DECL. */
20105 decl
= do_class_using_decl (qscope
, identifier
);
20107 if (decl
&& typename_p
)
20108 USING_DECL_TYPENAME_P (decl
) = 1;
20110 if (check_for_bare_parameter_packs (decl
))
20112 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20116 /* Add it to the list of members in this class. */
20117 finish_member_declaration (decl
);
20120 finish_nonmember_using_decl (qscope
, identifier
);
20123 if (!access_declaration_p
20124 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20126 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
20127 if (cxx_dialect
< cxx17
)
20128 pedwarn (comma
->location
, 0,
20129 "comma-separated list in using-declaration only available "
20130 "with %<-std=c++17%> or %<-std=gnu++17%>");
20134 /* Look for the final `;'. */
20135 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20137 if (access_declaration_p
&& errorcount
== oldcount
)
20138 warning_at (diag_token
->location
, OPT_Wdeprecated
,
20139 "access declarations are deprecated "
20140 "in favour of using-declarations; "
20141 "suggestion: add the %<using%> keyword");
20146 /* Parse an alias-declaration.
20149 using identifier attribute-specifier-seq [opt] = type-id */
20152 cp_parser_alias_declaration (cp_parser
* parser
)
20154 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
20155 location_t id_location
, type_location
;
20156 cp_declarator
*declarator
;
20157 cp_decl_specifier_seq decl_specs
;
20159 const char *saved_message
= NULL
;
20161 /* Look for the `using' keyword. */
20162 cp_token
*using_token
20163 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20164 if (using_token
== NULL
)
20165 return error_mark_node
;
20167 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
20168 id
= cp_parser_identifier (parser
);
20169 if (id
== error_mark_node
)
20170 return error_mark_node
;
20172 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
20173 attributes
= cp_parser_attributes_opt (parser
);
20174 if (attributes
== error_mark_node
)
20175 return error_mark_node
;
20177 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
20179 if (cp_parser_error_occurred (parser
))
20180 return error_mark_node
;
20182 cp_parser_commit_to_tentative_parse (parser
);
20184 /* Now we are going to parse the type-id of the declaration. */
20189 "A type-specifier-seq shall not define a class or enumeration
20190 unless it appears in the type-id of an alias-declaration (7.1.3) that
20191 is not the declaration of a template-declaration."
20193 In other words, if we currently are in an alias template, the
20194 type-id should not define a type.
20196 So let's set parser->type_definition_forbidden_message in that
20197 case; cp_parser_check_type_definition (called by
20198 cp_parser_class_specifier) will then emit an error if a type is
20199 defined in the type-id. */
20200 if (parser
->num_template_parameter_lists
)
20202 saved_message
= parser
->type_definition_forbidden_message
;
20203 parser
->type_definition_forbidden_message
=
20204 G_("types may not be defined in alias template declarations");
20207 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
20210 /* Restore the error message if need be. */
20211 if (parser
->num_template_parameter_lists
)
20212 parser
->type_definition_forbidden_message
= saved_message
;
20214 if (type
== error_mark_node
20215 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
20217 cp_parser_skip_to_end_of_block_or_statement (parser
);
20218 return error_mark_node
;
20221 /* A typedef-name can also be introduced by an alias-declaration. The
20222 identifier following the using keyword becomes a typedef-name. It has
20223 the same semantics as if it were introduced by the typedef
20224 specifier. In particular, it does not define a new type and it shall
20225 not appear in the type-id. */
20227 clear_decl_specs (&decl_specs
);
20228 decl_specs
.type
= type
;
20229 if (attributes
!= NULL_TREE
)
20231 decl_specs
.attributes
= attributes
;
20232 set_and_check_decl_spec_loc (&decl_specs
,
20236 set_and_check_decl_spec_loc (&decl_specs
,
20239 set_and_check_decl_spec_loc (&decl_specs
,
20242 decl_specs
.locations
[ds_type_spec
] = type_location
;
20244 if (parser
->num_template_parameter_lists
20245 && !cp_parser_check_template_parameters (parser
,
20246 /*num_templates=*/0,
20247 /*template_id*/false,
20249 /*declarator=*/NULL
))
20250 return error_mark_node
;
20252 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
20254 member_p
= at_class_scope_p ();
20256 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
20257 NULL_TREE
, attributes
);
20259 decl
= start_decl (declarator
, &decl_specs
, 0,
20260 attributes
, NULL_TREE
, &pushed_scope
);
20261 if (decl
== error_mark_node
)
20264 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
20267 pop_scope (pushed_scope
);
20269 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20270 added into the symbol table; otherwise, return the TYPE_DECL. */
20271 if (DECL_LANG_SPECIFIC (decl
)
20272 && DECL_TEMPLATE_INFO (decl
)
20273 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
20275 decl
= DECL_TI_TEMPLATE (decl
);
20277 check_member_template (decl
);
20283 /* Parse a using-directive.
20286 using namespace :: [opt] nested-name-specifier [opt]
20287 namespace-name ; */
20290 cp_parser_using_directive (cp_parser
* parser
)
20292 tree namespace_decl
;
20295 /* Look for the `using' keyword. */
20296 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20297 /* And the `namespace' keyword. */
20298 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
20299 /* Look for the optional `::' operator. */
20300 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20301 /* And the optional nested-name-specifier. */
20302 cp_parser_nested_name_specifier_opt (parser
,
20303 /*typename_keyword_p=*/false,
20304 /*check_dependency_p=*/true,
20306 /*is_declaration=*/true);
20307 /* Get the namespace being used. */
20308 namespace_decl
= cp_parser_namespace_name (parser
);
20309 cp_warn_deprecated_use_scopes (namespace_decl
);
20310 /* And any specified attributes. */
20311 attribs
= cp_parser_attributes_opt (parser
);
20313 /* Update the symbol table. */
20314 finish_using_directive (namespace_decl
, attribs
);
20316 /* Look for the final `;'. */
20317 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20320 /* Parse an asm-definition.
20327 asm-qualifier-list:
20329 asm-qualifier-list asm-qualifier
20332 asm ( string-literal ) ;
20337 asm asm-qualifier-list [opt] ( string-literal ) ;
20338 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20339 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20340 : asm-operand-list [opt] ) ;
20341 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20342 : asm-operand-list [opt]
20343 : asm-clobber-list [opt] ) ;
20344 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20345 : asm-clobber-list [opt]
20346 : asm-goto-list ) ;
20348 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20349 contains goto, and is the only allowed form in that case. No duplicates are
20350 allowed in an asm-qualifier-list. */
20353 cp_parser_asm_definition (cp_parser
* parser
)
20356 tree outputs
= NULL_TREE
;
20357 tree inputs
= NULL_TREE
;
20358 tree clobbers
= NULL_TREE
;
20359 tree labels
= NULL_TREE
;
20361 bool extended_p
= false;
20362 bool invalid_inputs_p
= false;
20363 bool invalid_outputs_p
= false;
20364 required_token missing
= RT_NONE
;
20365 location_t asm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20367 /* Look for the `asm' keyword. */
20368 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
20370 /* In C++20, unevaluated inline assembly is permitted in constexpr
20372 if (parser
->in_function_body
20373 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
20374 && (cxx_dialect
< cxx20
))
20375 pedwarn (asm_loc
, 0, "%<asm%> in %<constexpr%> function only available "
20376 "with %<-std=c++20%> or %<-std=gnu++20%>");
20378 /* Handle the asm-qualifier-list. */
20379 location_t volatile_loc
= UNKNOWN_LOCATION
;
20380 location_t inline_loc
= UNKNOWN_LOCATION
;
20381 location_t goto_loc
= UNKNOWN_LOCATION
;
20382 location_t first_loc
= UNKNOWN_LOCATION
;
20384 if (cp_parser_allow_gnu_extensions_p (parser
))
20387 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20388 location_t loc
= token
->location
;
20389 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
20394 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20396 inform (volatile_loc
, "first seen here");
20400 if (!parser
->in_function_body
)
20401 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
20402 "outside of function body", token
->u
.value
);
20403 volatile_loc
= loc
;
20405 cp_lexer_consume_token (parser
->lexer
);
20411 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20413 inform (inline_loc
, "first seen here");
20419 cp_lexer_consume_token (parser
->lexer
);
20425 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20427 inform (goto_loc
, "first seen here");
20433 cp_lexer_consume_token (parser
->lexer
);
20438 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
20439 cp_lexer_consume_token (parser
->lexer
);
20448 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
20449 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
20450 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
20452 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
20454 error_at (first_loc
, "%<asm%> qualifier outside of function body");
20455 inline_p
= goto_p
= false;
20458 /* Look for the opening `('. */
20459 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
20461 /* Look for the string. */
20462 string
= cp_parser_string_literal (parser
, false, false);
20463 if (string
== error_mark_node
)
20465 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20466 /*consume_paren=*/true);
20470 /* If we're allowing GNU extensions, check for the extended assembly
20471 syntax. Unfortunately, the `:' tokens need not be separated by
20472 a space in C, and so, for compatibility, we tolerate that here
20473 too. Doing that means that we have to treat the `::' operator as
20475 if (cp_parser_allow_gnu_extensions_p (parser
)
20476 && parser
->in_function_body
20477 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
20478 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
20480 bool inputs_p
= false;
20481 bool clobbers_p
= false;
20482 bool labels_p
= false;
20484 /* The extended syntax was used. */
20487 /* Look for outputs. */
20488 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20490 /* Consume the `:'. */
20491 cp_lexer_consume_token (parser
->lexer
);
20492 /* Parse the output-operands. */
20493 if (cp_lexer_next_token_is_not (parser
->lexer
,
20495 && cp_lexer_next_token_is_not (parser
->lexer
,
20497 && cp_lexer_next_token_is_not (parser
->lexer
,
20501 outputs
= cp_parser_asm_operand_list (parser
);
20502 if (outputs
== error_mark_node
)
20503 invalid_outputs_p
= true;
20506 /* If the next token is `::', there are no outputs, and the
20507 next token is the beginning of the inputs. */
20508 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20509 /* The inputs are coming next. */
20512 /* Look for inputs. */
20514 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20516 /* Consume the `:' or `::'. */
20517 cp_lexer_consume_token (parser
->lexer
);
20518 /* Parse the output-operands. */
20519 if (cp_lexer_next_token_is_not (parser
->lexer
,
20521 && cp_lexer_next_token_is_not (parser
->lexer
,
20523 && cp_lexer_next_token_is_not (parser
->lexer
,
20526 inputs
= cp_parser_asm_operand_list (parser
);
20527 if (inputs
== error_mark_node
)
20528 invalid_inputs_p
= true;
20531 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20532 /* The clobbers are coming next. */
20535 /* Look for clobbers. */
20537 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20540 /* Consume the `:' or `::'. */
20541 cp_lexer_consume_token (parser
->lexer
);
20542 /* Parse the clobbers. */
20543 if (cp_lexer_next_token_is_not (parser
->lexer
,
20545 && cp_lexer_next_token_is_not (parser
->lexer
,
20547 clobbers
= cp_parser_asm_clobber_list (parser
);
20549 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20550 /* The labels are coming next. */
20553 /* Look for labels. */
20555 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
20558 /* Consume the `:' or `::'. */
20559 cp_lexer_consume_token (parser
->lexer
);
20560 /* Parse the labels. */
20561 labels
= cp_parser_asm_label_list (parser
);
20564 if (goto_p
&& !labels_p
)
20565 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
20568 missing
= RT_COLON_SCOPE
;
20570 /* Look for the closing `)'. */
20571 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
20572 missing
? missing
: RT_CLOSE_PAREN
))
20573 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20574 /*consume_paren=*/true);
20575 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20577 if (!invalid_inputs_p
&& !invalid_outputs_p
)
20579 /* Create the ASM_EXPR. */
20580 if (parser
->in_function_body
)
20582 asm_stmt
= finish_asm_stmt (asm_loc
, volatile_p
, string
, outputs
,
20583 inputs
, clobbers
, labels
, inline_p
);
20584 /* If the extended syntax was not used, mark the ASM_EXPR. */
20587 tree temp
= asm_stmt
;
20588 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
20589 temp
= TREE_OPERAND (temp
, 0);
20591 ASM_INPUT_P (temp
) = 1;
20595 symtab
->finalize_toplevel_asm (string
);
20599 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20600 type that comes from the decl-specifier-seq. */
20603 strip_declarator_types (tree type
, cp_declarator
*declarator
)
20605 for (cp_declarator
*d
= declarator
; d
;)
20615 if (TYPE_PTRMEMFUNC_P (type
))
20616 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
20617 type
= TREE_TYPE (type
);
20625 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
20626 a construct looks like a variable definition but is actually a function
20627 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
20628 is the declarator for this function declaration. */
20631 warn_about_ambiguous_parse (const cp_decl_specifier_seq
*decl_specifiers
,
20632 const cp_declarator
*declarator
)
20634 /* Only warn if we are declaring a function at block scope. */
20635 if (!at_function_scope_p ())
20638 /* And only if there is no storage class specified. */
20639 if (decl_specifiers
->storage_class
!= sc_none
20640 || decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20643 if (declarator
->kind
!= cdk_function
20644 || !declarator
->declarator
20645 || declarator
->declarator
->kind
!= cdk_id
20646 || !identifier_p (get_unqualified_id
20647 (const_cast<cp_declarator
*>(declarator
))))
20650 /* Don't warn when the whole declarator (not just the declarator-id!)
20651 was parenthesized. That is, don't warn for int(n()) but do warn
20653 if (declarator
->parenthesized
!= UNKNOWN_LOCATION
)
20657 if (decl_specifiers
->type
)
20659 type
= decl_specifiers
->type
;
20660 if (TREE_CODE (type
) == TYPE_DECL
)
20661 type
= TREE_TYPE (type
);
20663 /* If the return type is void there is no ambiguity. */
20664 if (same_type_p (type
, void_type_node
))
20669 /* Code like long f(); will have null ->type. If we have any
20670 type-specifiers, pretend we've seen int. */
20671 gcc_checking_assert (decl_specifiers
->any_type_specifiers_p
);
20672 type
= integer_type_node
;
20675 auto_diagnostic_group d
;
20676 location_t loc
= declarator
->u
.function
.parens_loc
;
20677 tree params
= declarator
->u
.function
.parameters
;
20678 const bool has_list_ctor_p
= CLASS_TYPE_P (type
) && TYPE_HAS_LIST_CTOR (type
);
20680 /* The T t() case. */
20681 if (params
== void_list_node
)
20683 if (warning_at (loc
, OPT_Wvexing_parse
,
20684 "empty parentheses were disambiguated as a function "
20687 /* () means value-initialization (C++03 and up); {} (C++11 and up)
20688 means value-initialization or aggregate-initialization, nothing
20689 means default-initialization. We can only suggest removing the
20690 parentheses/adding {} if T has a default constructor. */
20691 if (!CLASS_TYPE_P (type
) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
20693 gcc_rich_location
iloc (loc
);
20694 iloc
.add_fixit_remove ();
20695 inform (&iloc
, "remove parentheses to default-initialize "
20697 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
20699 if (CP_AGGREGATE_TYPE_P (type
))
20700 inform (loc
, "or replace parentheses with braces to "
20701 "aggregate-initialize a variable");
20703 inform (loc
, "or replace parentheses with braces to "
20704 "value-initialize a variable");
20711 /* If we had (...) or the parameter-list wasn't parenthesized,
20713 if (params
== NULL_TREE
|| !PARENTHESIZED_LIST_P (params
))
20716 /* The T t(X()) case. */
20717 if (list_length (params
) == 2)
20719 if (warning_at (loc
, OPT_Wvexing_parse
,
20720 "parentheses were disambiguated as a function "
20723 gcc_rich_location
iloc (loc
);
20724 /* {}-initialization means that we can use an initializer-list
20725 constructor if no default constructor is available, so don't
20726 suggest using {} for classes that have an initializer_list
20728 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
20730 iloc
.add_fixit_replace (get_start (loc
), "{");
20731 iloc
.add_fixit_replace (get_finish (loc
), "}");
20732 inform (&iloc
, "replace parentheses with braces to declare a "
20737 iloc
.add_fixit_insert_after (get_start (loc
), "(");
20738 iloc
.add_fixit_insert_before (get_finish (loc
), ")");
20739 inform (&iloc
, "add parentheses to declare a variable");
20743 /* The T t(X(), X()) case. */
20744 else if (warning_at (loc
, OPT_Wvexing_parse
,
20745 "parentheses were disambiguated as a function "
20748 gcc_rich_location
iloc (loc
);
20749 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
20751 iloc
.add_fixit_replace (get_start (loc
), "{");
20752 iloc
.add_fixit_replace (get_finish (loc
), "}");
20753 inform (&iloc
, "replace parentheses with braces to declare a "
20759 /* Declarators [gram.dcl.decl] */
20761 /* Parse an init-declarator.
20764 declarator initializer [opt]
20769 declarator asm-specification [opt] attributes [opt] initializer [opt]
20771 function-definition:
20772 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20774 decl-specifier-seq [opt] declarator function-try-block
20778 function-definition:
20779 __extension__ function-definition
20783 function-definition:
20784 decl-specifier-seq [opt] declarator function-transaction-block
20786 The parser flags FLAGS is used to control type-specifier parsing.
20788 The DECL_SPECIFIERS apply to this declarator. Returns a
20789 representation of the entity declared. If MEMBER_P is TRUE, then
20790 this declarator appears in a class scope. The new DECL created by
20791 this declarator is returned.
20793 The CHECKS are access checks that should be performed once we know
20794 what entity is being declared (and, therefore, what classes have
20797 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20798 for a function-definition here as well. If the declarator is a
20799 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20800 be TRUE upon return. By that point, the function-definition will
20801 have been completely parsed.
20803 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20806 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20807 parsed declaration if it is an uninitialized single declarator not followed
20808 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20809 if present, will not be consumed. If returned, this declarator will be
20810 created with SD_INITIALIZED but will not call cp_finish_decl.
20812 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20813 and there is an initializer, the pointed location_t is set to the
20814 location of the '=' or `(', or '{' in C++11 token introducing the
20818 cp_parser_init_declarator (cp_parser
* parser
,
20819 cp_parser_flags flags
,
20820 cp_decl_specifier_seq
*decl_specifiers
,
20821 vec
<deferred_access_check
, va_gc
> *checks
,
20822 bool function_definition_allowed_p
,
20824 int declares_class_or_enum
,
20825 bool* function_definition_p
,
20826 tree
* maybe_range_for_decl
,
20827 location_t
* init_loc
,
20830 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20831 *attributes_start_token
= NULL
;
20832 cp_declarator
*declarator
;
20833 tree prefix_attributes
;
20834 tree attributes
= NULL
;
20835 tree asm_specification
;
20837 tree decl
= NULL_TREE
;
20839 int is_initialized
;
20840 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20841 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20843 enum cpp_ttype initialization_kind
;
20844 bool is_direct_init
= false;
20845 bool is_non_constant_init
;
20846 int ctor_dtor_or_conv_p
;
20847 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20848 tree pushed_scope
= NULL_TREE
;
20849 bool range_for_decl_p
= false;
20850 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20851 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20853 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_consteval
))
20854 flags
|= CP_PARSER_FLAGS_CONSTEVAL
;
20856 /* Gather the attributes that were provided with the
20857 decl-specifiers. */
20858 prefix_attributes
= decl_specifiers
->attributes
;
20860 /* Assume that this is not the declarator for a function
20862 if (function_definition_p
)
20863 *function_definition_p
= false;
20865 /* Default arguments are only permitted for function parameters. */
20866 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20867 parser
->default_arg_ok_p
= false;
20869 /* Defer access checks while parsing the declarator; we cannot know
20870 what names are accessible until we know what is being
20872 resume_deferring_access_checks ();
20874 token
= cp_lexer_peek_token (parser
->lexer
);
20876 /* Parse the declarator. */
20878 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20879 flags
, &ctor_dtor_or_conv_p
,
20880 /*parenthesized_p=*/NULL
,
20881 member_p
, friend_p
, /*static_p=*/false);
20882 /* Gather up the deferred checks. */
20883 stop_deferring_access_checks ();
20885 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20887 /* If the DECLARATOR was erroneous, there's no need to go
20889 if (declarator
== cp_error_declarator
)
20890 return error_mark_node
;
20892 /* Check that the number of template-parameter-lists is OK. */
20893 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20895 return error_mark_node
;
20897 if (declares_class_or_enum
& 2)
20898 cp_parser_check_for_definition_in_return_type (declarator
,
20899 decl_specifiers
->type
,
20900 decl_specifiers
->locations
[ds_type_spec
]);
20902 /* Figure out what scope the entity declared by the DECLARATOR is
20903 located in. `grokdeclarator' sometimes changes the scope, so
20904 we compute it now. */
20905 scope
= get_scope_of_declarator (declarator
);
20907 /* Perform any lookups in the declared type which were thought to be
20908 dependent, but are not in the scope of the declarator. */
20909 decl_specifiers
->type
20910 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20912 /* If we're allowing GNU extensions, look for an
20913 asm-specification. */
20914 if (cp_parser_allow_gnu_extensions_p (parser
))
20916 /* Look for an asm-specification. */
20917 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20918 asm_specification
= cp_parser_asm_specification_opt (parser
);
20921 asm_specification
= NULL_TREE
;
20923 /* Look for attributes. */
20924 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20925 attributes
= cp_parser_attributes_opt (parser
);
20927 /* Peek at the next token. */
20928 token
= cp_lexer_peek_token (parser
->lexer
);
20930 bool bogus_implicit_tmpl
= false;
20932 if (function_declarator_p (declarator
))
20934 /* Handle C++17 deduction guides. */
20935 if (!decl_specifiers
->type
20936 && !decl_specifiers
->any_type_specifiers_p
20937 && ctor_dtor_or_conv_p
<= 0
20938 && cxx_dialect
>= cxx17
)
20940 cp_declarator
*id
= get_id_declarator (declarator
);
20941 tree name
= id
->u
.id
.unqualified_name
;
20942 parser
->scope
= id
->u
.id
.qualifying_scope
;
20943 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20945 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20946 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20948 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20949 id
->u
.id
.sfk
= sfk_deduction_guide
;
20950 ctor_dtor_or_conv_p
= 1;
20954 if (!member_p
&& !cp_parser_error_occurred (parser
))
20955 warn_about_ambiguous_parse (decl_specifiers
, declarator
);
20957 /* Check to see if the token indicates the start of a
20958 function-definition. */
20959 if (cp_parser_token_starts_function_definition_p (token
))
20961 if (!function_definition_allowed_p
)
20963 /* If a function-definition should not appear here, issue an
20965 cp_parser_error (parser
,
20966 "a function-definition is not allowed here");
20967 return error_mark_node
;
20970 location_t func_brace_location
20971 = cp_lexer_peek_token (parser
->lexer
)->location
;
20973 /* Neither attributes nor an asm-specification are allowed
20974 on a function-definition. */
20975 if (asm_specification
)
20976 error_at (asm_spec_start_token
->location
,
20977 "an %<asm%> specification is not allowed "
20978 "on a function-definition");
20980 error_at (attributes_start_token
->location
,
20981 "attributes are not allowed "
20982 "on a function-definition");
20983 /* This is a function-definition. */
20984 *function_definition_p
= true;
20986 /* Parse the function definition. */
20988 decl
= cp_parser_save_member_function_body (parser
,
20991 prefix_attributes
);
20994 (cp_parser_function_definition_from_specifiers_and_declarator
20995 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20997 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20999 /* This is where the prologue starts... */
21000 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
21001 = func_brace_location
;
21007 else if (parser
->fully_implicit_function_template_p
)
21009 /* A non-template declaration involving a function parameter list
21010 containing an implicit template parameter will be made into a
21011 template. If the resulting declaration is not going to be an
21012 actual function then finish the template scope here to prevent it.
21013 An error message will be issued once we have a decl to talk about.
21015 FIXME probably we should do type deduction rather than create an
21016 implicit template, but the standard currently doesn't allow it. */
21017 bogus_implicit_tmpl
= true;
21018 finish_fully_implicit_template (parser
, NULL_TREE
);
21023 Only in function declarations for constructors, destructors, type
21024 conversions, and deduction guides can the decl-specifier-seq be omitted.
21026 We explicitly postpone this check past the point where we handle
21027 function-definitions because we tolerate function-definitions
21028 that are missing their return types in some modes. */
21029 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
21031 cp_parser_error (parser
,
21032 "expected constructor, destructor, or type conversion");
21033 return error_mark_node
;
21036 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
21037 if (token
->type
== CPP_EQ
21038 || token
->type
== CPP_OPEN_PAREN
21039 || token
->type
== CPP_OPEN_BRACE
)
21041 is_initialized
= SD_INITIALIZED
;
21042 initialization_kind
= token
->type
;
21043 if (maybe_range_for_decl
)
21044 *maybe_range_for_decl
= error_mark_node
;
21045 tmp_init_loc
= token
->location
;
21046 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
21047 *init_loc
= tmp_init_loc
;
21049 if (token
->type
== CPP_EQ
21050 && function_declarator_p (declarator
))
21052 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21053 if (t2
->keyword
== RID_DEFAULT
)
21054 is_initialized
= SD_DEFAULTED
;
21055 else if (t2
->keyword
== RID_DELETE
)
21056 is_initialized
= SD_DELETED
;
21061 /* If the init-declarator isn't initialized and isn't followed by a
21062 `,' or `;', it's not a valid init-declarator. */
21063 if (token
->type
!= CPP_COMMA
21064 && token
->type
!= CPP_SEMICOLON
)
21066 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
21067 range_for_decl_p
= true;
21070 if (!maybe_range_for_decl
)
21071 cp_parser_error (parser
, "expected initializer");
21072 return error_mark_node
;
21075 is_initialized
= SD_UNINITIALIZED
;
21076 initialization_kind
= CPP_EOF
;
21079 /* Because start_decl has side-effects, we should only call it if we
21080 know we're going ahead. By this point, we know that we cannot
21081 possibly be looking at any other construct. */
21082 cp_parser_commit_to_tentative_parse (parser
);
21084 /* Enter the newly declared entry in the symbol table. If we're
21085 processing a declaration in a class-specifier, we wait until
21086 after processing the initializer. */
21089 if (parser
->in_unbraced_linkage_specification_p
)
21090 decl_specifiers
->storage_class
= sc_extern
;
21091 decl
= start_decl (declarator
, decl_specifiers
,
21092 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
21093 attributes
, prefix_attributes
, &pushed_scope
);
21094 cp_finalize_omp_declare_simd (parser
, decl
);
21095 cp_finalize_oacc_routine (parser
, decl
, false);
21096 /* Adjust location of decl if declarator->id_loc is more appropriate:
21097 set, and decl wasn't merged with another decl, in which case its
21098 location would be different from input_location, and more accurate. */
21100 && declarator
->id_loc
!= UNKNOWN_LOCATION
21101 && DECL_SOURCE_LOCATION (decl
) == input_location
)
21102 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
21105 /* Enter the SCOPE. That way unqualified names appearing in the
21106 initializer will be looked up in SCOPE. */
21107 pushed_scope
= push_scope (scope
);
21109 /* Perform deferred access control checks, now that we know in which
21110 SCOPE the declared entity resides. */
21111 if (!member_p
&& decl
)
21113 tree saved_current_function_decl
= NULL_TREE
;
21115 /* If the entity being declared is a function, pretend that we
21116 are in its scope. If it is a `friend', it may have access to
21117 things that would not otherwise be accessible. */
21118 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21120 saved_current_function_decl
= current_function_decl
;
21121 current_function_decl
= decl
;
21124 /* Perform access checks for template parameters. */
21125 cp_parser_perform_template_parameter_access_checks (checks
);
21127 /* Perform the access control checks for the declarator and the
21128 decl-specifiers. */
21129 perform_deferred_access_checks (tf_warning_or_error
);
21131 /* Restore the saved value. */
21132 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21133 current_function_decl
= saved_current_function_decl
;
21136 /* Parse the initializer. */
21137 initializer
= NULL_TREE
;
21138 is_direct_init
= false;
21139 is_non_constant_init
= true;
21140 if (is_initialized
)
21142 if (function_declarator_p (declarator
))
21144 if (initialization_kind
== CPP_EQ
)
21145 initializer
= cp_parser_pure_specifier (parser
);
21148 /* If the declaration was erroneous, we don't really
21149 know what the user intended, so just silently
21150 consume the initializer. */
21151 if (decl
!= error_mark_node
)
21152 error_at (tmp_init_loc
, "initializer provided for function");
21153 cp_parser_skip_to_closing_parenthesis (parser
,
21154 /*recovering=*/true,
21155 /*or_comma=*/false,
21156 /*consume_paren=*/true);
21161 /* We want to record the extra mangling scope for in-class
21162 initializers of class members and initializers of static
21163 data member templates and namespace-scope initializers.
21164 The former involves deferring parsing of the initializer
21165 until end of class as with default arguments. So right
21166 here we only handle the latter two. */
21167 bool has_lambda_scope
= false;
21169 if (decl
!= error_mark_node
21171 && (processing_template_decl
|| DECL_NAMESPACE_SCOPE_P (decl
)))
21172 has_lambda_scope
= true;
21174 if (has_lambda_scope
)
21175 start_lambda_scope (decl
);
21176 initializer
= cp_parser_initializer (parser
,
21178 &is_non_constant_init
);
21179 if (has_lambda_scope
)
21180 finish_lambda_scope ();
21181 if (initializer
== error_mark_node
)
21182 cp_parser_skip_to_end_of_statement (parser
);
21186 /* The old parser allows attributes to appear after a parenthesized
21187 initializer. Mark Mitchell proposed removing this functionality
21188 on the GCC mailing lists on 2002-08-13. This parser accepts the
21189 attributes -- but ignores them. Made a permerror in GCC 8. */
21190 if (cp_parser_allow_gnu_extensions_p (parser
)
21191 && initialization_kind
== CPP_OPEN_PAREN
21192 && cp_parser_attributes_opt (parser
)
21193 && permerror (input_location
,
21194 "attributes after parenthesized initializer ignored"))
21197 if (flag_permissive
&& !hint
)
21200 inform (input_location
,
21201 "this flexibility is deprecated and will be removed");
21205 /* And now complain about a non-function implicit template. */
21206 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
21207 error_at (DECL_SOURCE_LOCATION (decl
),
21208 "non-function %qD declared as implicit template", decl
);
21210 /* For an in-class declaration, use `grokfield' to create the
21216 pop_scope (pushed_scope
);
21217 pushed_scope
= NULL_TREE
;
21219 decl
= grokfield (declarator
, decl_specifiers
,
21220 initializer
, !is_non_constant_init
,
21221 /*asmspec=*/NULL_TREE
,
21222 attr_chainon (attributes
, prefix_attributes
));
21223 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
21224 cp_parser_save_default_args (parser
, decl
);
21225 cp_finalize_omp_declare_simd (parser
, decl
);
21226 cp_finalize_oacc_routine (parser
, decl
, false);
21229 /* Finish processing the declaration. But, skip member
21231 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
21233 cp_finish_decl (decl
,
21234 initializer
, !is_non_constant_init
,
21236 /* If the initializer is in parentheses, then this is
21237 a direct-initialization, which means that an
21238 `explicit' constructor is OK. Otherwise, an
21239 `explicit' constructor cannot be used. */
21240 ((is_direct_init
|| !is_initialized
)
21241 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
21243 else if ((cxx_dialect
!= cxx98
) && friend_p
21244 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
21245 /* Core issue #226 (C++0x only): A default template-argument
21246 shall not be specified in a friend class template
21248 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
21249 /*is_partial=*/false, /*is_friend_decl=*/1);
21251 if (!friend_p
&& pushed_scope
)
21252 pop_scope (pushed_scope
);
21254 if (function_declarator_p (declarator
)
21255 && parser
->fully_implicit_function_template_p
)
21258 decl
= finish_fully_implicit_template (parser
, decl
);
21260 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
21263 if (auto_result
&& is_initialized
&& decl_specifiers
->type
21264 && type_uses_auto (decl_specifiers
->type
))
21265 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
21270 /* Parse a declarator.
21274 ptr-operator declarator
21276 abstract-declarator:
21277 ptr-operator abstract-declarator [opt]
21278 direct-abstract-declarator
21283 attributes [opt] direct-declarator
21284 attributes [opt] ptr-operator declarator
21286 abstract-declarator:
21287 attributes [opt] ptr-operator abstract-declarator [opt]
21288 attributes [opt] direct-abstract-declarator
21290 The parser flags FLAGS is used to control type-specifier parsing.
21292 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21293 detect constructors, destructors, deduction guides, or conversion operators.
21294 It is set to -1 if the declarator is a name, and +1 if it is a
21295 function. Otherwise it is set to zero. Usually you just want to
21296 test for >0, but internally the negative value is used.
21298 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21299 a decl-specifier-seq unless it declares a constructor, destructor,
21300 or conversion. It might seem that we could check this condition in
21301 semantic analysis, rather than parsing, but that makes it difficult
21302 to handle something like `f()'. We want to notice that there are
21303 no decl-specifiers, and therefore realize that this is an
21304 expression, not a declaration.)
21306 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21307 the declarator is a direct-declarator of the form "(...)".
21309 MEMBER_P is true iff this declarator is a member-declarator.
21311 FRIEND_P is true iff this declarator is a friend.
21313 STATIC_P is true iff the keyword static was seen. */
21315 static cp_declarator
*
21316 cp_parser_declarator (cp_parser
* parser
,
21317 cp_parser_declarator_kind dcl_kind
,
21318 cp_parser_flags flags
,
21319 int* ctor_dtor_or_conv_p
,
21320 bool* parenthesized_p
,
21321 bool member_p
, bool friend_p
, bool static_p
)
21323 cp_declarator
*declarator
;
21324 enum tree_code code
;
21325 cp_cv_quals cv_quals
;
21327 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
21329 /* Assume this is not a constructor, destructor, or type-conversion
21331 if (ctor_dtor_or_conv_p
)
21332 *ctor_dtor_or_conv_p
= 0;
21334 if (cp_parser_allow_gnu_extensions_p (parser
))
21335 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
21337 /* Check for the ptr-operator production. */
21338 cp_parser_parse_tentatively (parser
);
21339 /* Parse the ptr-operator. */
21340 code
= cp_parser_ptr_operator (parser
,
21345 /* If that worked, then we have a ptr-operator. */
21346 if (cp_parser_parse_definitely (parser
))
21348 /* If a ptr-operator was found, then this declarator was not
21350 if (parenthesized_p
)
21351 *parenthesized_p
= false;
21352 /* The dependent declarator is optional if we are parsing an
21353 abstract-declarator. */
21354 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21355 cp_parser_parse_tentatively (parser
);
21357 /* Parse the dependent declarator. */
21358 declarator
= cp_parser_declarator (parser
, dcl_kind
,
21359 CP_PARSER_FLAGS_NONE
,
21360 /*ctor_dtor_or_conv_p=*/NULL
,
21361 /*parenthesized_p=*/NULL
,
21362 /*member_p=*/false,
21363 friend_p
, /*static_p=*/false);
21365 /* If we are parsing an abstract-declarator, we must handle the
21366 case where the dependent declarator is absent. */
21367 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
21368 && !cp_parser_parse_definitely (parser
))
21371 declarator
= cp_parser_make_indirect_declarator
21372 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
21374 /* Everything else is a direct-declarator. */
21377 if (parenthesized_p
)
21378 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
21380 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
21381 flags
, ctor_dtor_or_conv_p
,
21382 member_p
, friend_p
, static_p
);
21385 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
21386 declarator
->attributes
= gnu_attributes
;
21390 /* Parse a direct-declarator or direct-abstract-declarator.
21394 direct-declarator ( parameter-declaration-clause )
21395 cv-qualifier-seq [opt]
21396 ref-qualifier [opt]
21397 exception-specification [opt]
21398 direct-declarator [ constant-expression [opt] ]
21401 direct-abstract-declarator:
21402 direct-abstract-declarator [opt]
21403 ( parameter-declaration-clause )
21404 cv-qualifier-seq [opt]
21405 ref-qualifier [opt]
21406 exception-specification [opt]
21407 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21408 ( abstract-declarator )
21410 Returns a representation of the declarator. DCL_KIND is
21411 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21412 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21413 we are parsing a direct-declarator. It is
21414 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21415 of ambiguity we prefer an abstract declarator, as per
21417 The parser flags FLAGS is used to control type-specifier parsing.
21418 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21419 as for cp_parser_declarator. */
21421 static cp_declarator
*
21422 cp_parser_direct_declarator (cp_parser
* parser
,
21423 cp_parser_declarator_kind dcl_kind
,
21424 cp_parser_flags flags
,
21425 int* ctor_dtor_or_conv_p
,
21426 bool member_p
, bool friend_p
, bool static_p
)
21429 cp_declarator
*declarator
= NULL
;
21430 tree scope
= NULL_TREE
;
21431 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21432 bool saved_in_declarator_p
= parser
->in_declarator_p
;
21434 tree pushed_scope
= NULL_TREE
;
21435 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
21439 /* Peek at the next token. */
21440 token
= cp_lexer_peek_token (parser
->lexer
);
21441 if (token
->type
== CPP_OPEN_PAREN
)
21443 /* This is either a parameter-declaration-clause, or a
21444 parenthesized declarator. When we know we are parsing a
21445 named declarator, it must be a parenthesized declarator
21446 if FIRST is true. For instance, `(int)' is a
21447 parameter-declaration-clause, with an omitted
21448 direct-abstract-declarator. But `((*))', is a
21449 parenthesized abstract declarator. Finally, when T is a
21450 template parameter `(T)' is a
21451 parameter-declaration-clause, and not a parenthesized
21454 We first try and parse a parameter-declaration-clause,
21455 and then try a nested declarator (if FIRST is true).
21457 It is not an error for it not to be a
21458 parameter-declaration-clause, even when FIRST is
21464 The first is the declaration of a function while the
21465 second is the definition of a variable, including its
21468 Having seen only the parenthesis, we cannot know which of
21469 these two alternatives should be selected. Even more
21470 complex are examples like:
21475 The former is a function-declaration; the latter is a
21476 variable initialization.
21478 Thus again, we try a parameter-declaration-clause, and if
21479 that fails, we back out and return. */
21481 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21484 bool is_declarator
= false;
21488 /* In a member-declarator, the only valid interpretation
21489 of a parenthesis is the start of a
21490 parameter-declaration-clause. (It is invalid to
21491 initialize a static data member with a parenthesized
21492 initializer; only the "=" form of initialization is
21495 cp_parser_parse_tentatively (parser
);
21497 /* Consume the `('. */
21498 const location_t parens_start
= token
->location
;
21499 matching_parens parens
;
21500 parens
.consume_open (parser
);
21503 /* If this is going to be an abstract declarator, we're
21504 in a declarator and we can't have default args. */
21505 parser
->default_arg_ok_p
= false;
21506 parser
->in_declarator_p
= true;
21509 begin_scope (sk_function_parms
, NULL_TREE
);
21511 /* Signal we are in the immediate function context. */
21512 if (flags
& CP_PARSER_FLAGS_CONSTEVAL
)
21513 current_binding_level
->immediate_fn_ctx_p
= true;
21515 /* Parse the parameter-declaration-clause. */
21517 = cp_parser_parameter_declaration_clause (parser
, flags
);
21518 const location_t parens_end
21519 = cp_lexer_peek_token (parser
->lexer
)->location
;
21521 /* Consume the `)'. */
21522 parens
.require_close (parser
);
21524 /* If all went well, parse the cv-qualifier-seq,
21525 ref-qualifier and the exception-specification. */
21526 if (member_p
|| cp_parser_parse_definitely (parser
))
21528 cp_cv_quals cv_quals
;
21529 cp_virt_specifiers virt_specifiers
;
21530 cp_ref_qualifier ref_qual
;
21531 tree exception_specification
;
21534 bool memfn
= (member_p
|| (pushed_scope
21535 && CLASS_TYPE_P (pushed_scope
)));
21536 unsigned char local_variables_forbidden_p
21537 = parser
->local_variables_forbidden_p
;
21538 /* 'this' is not allowed in static member functions. */
21539 if (static_p
|| friend_p
)
21540 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
21542 is_declarator
= true;
21544 if (ctor_dtor_or_conv_p
)
21545 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
21548 /* Parse the cv-qualifier-seq. */
21549 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21550 /* Parse the ref-qualifier. */
21551 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
21552 /* Parse the tx-qualifier. */
21553 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
21555 tree save_ccp
= current_class_ptr
;
21556 tree save_ccr
= current_class_ref
;
21558 /* DR 1207: 'this' is in scope after the cv-quals. */
21559 inject_this_parameter (current_class_type
, cv_quals
);
21561 /* If it turned out that this is e.g. a pointer to a
21562 function, we don't want to delay noexcept parsing. */
21563 if (declarator
== NULL
|| declarator
->kind
!= cdk_id
)
21564 flags
&= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
21566 /* Parse the exception-specification. */
21567 exception_specification
21568 = cp_parser_exception_specification_opt (parser
,
21571 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21573 /* In here, we handle cases where attribute is used after
21574 the function declaration. For example:
21575 void func (int x) __attribute__((vector(..))); */
21576 tree gnu_attrs
= NULL_TREE
;
21577 tree requires_clause
= NULL_TREE
;
21578 late_return
= (cp_parser_late_return_type_opt
21579 (parser
, declarator
, requires_clause
));
21581 /* Parse the virt-specifier-seq. */
21582 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
21584 location_t parens_loc
= make_location (parens_start
,
21587 /* Create the function-declarator. */
21588 declarator
= make_call_declarator (declarator
,
21594 exception_specification
,
21598 declarator
->std_attributes
= attrs
;
21599 declarator
->attributes
= gnu_attrs
;
21600 /* Any subsequent parameter lists are to do with
21601 return type, so are not those of the declared
21603 parser
->default_arg_ok_p
= false;
21605 current_class_ptr
= save_ccp
;
21606 current_class_ref
= save_ccr
;
21608 /* Restore the state of local_variables_forbidden_p. */
21609 parser
->local_variables_forbidden_p
21610 = local_variables_forbidden_p
;
21613 /* Remove the function parms from scope. */
21614 pop_bindings_and_leave_scope ();
21617 /* Repeat the main loop. */
21621 /* If this is the first, we can try a parenthesized
21625 bool saved_in_type_id_in_expr_p
;
21627 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21628 parser
->in_declarator_p
= saved_in_declarator_p
;
21630 open_paren
= token
;
21631 /* Consume the `('. */
21632 matching_parens parens
;
21633 parens
.consume_open (parser
);
21634 /* Parse the nested declarator. */
21635 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
21636 parser
->in_type_id_in_expr_p
= true;
21638 = cp_parser_declarator (parser
, dcl_kind
, flags
,
21639 ctor_dtor_or_conv_p
,
21640 /*parenthesized_p=*/NULL
,
21641 member_p
, friend_p
,
21642 /*static_p=*/false);
21643 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
21645 /* Expect a `)'. */
21646 close_paren
= cp_lexer_peek_token (parser
->lexer
);
21647 if (!parens
.require_close (parser
))
21648 declarator
= cp_error_declarator
;
21649 if (declarator
== cp_error_declarator
)
21652 goto handle_declarator
;
21654 /* Otherwise, we must be done. */
21658 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21659 && token
->type
== CPP_OPEN_SQUARE
21660 && !cp_next_tokens_can_be_attribute_p (parser
))
21662 /* Parse an array-declarator. */
21663 tree bounds
, attrs
;
21665 if (ctor_dtor_or_conv_p
)
21666 *ctor_dtor_or_conv_p
= 0;
21670 parser
->default_arg_ok_p
= false;
21671 parser
->in_declarator_p
= true;
21672 /* Consume the `['. */
21673 cp_lexer_consume_token (parser
->lexer
);
21674 /* Peek at the next token. */
21675 token
= cp_lexer_peek_token (parser
->lexer
);
21676 /* If the next token is `]', then there is no
21677 constant-expression. */
21678 if (token
->type
!= CPP_CLOSE_SQUARE
)
21680 bool non_constant_p
;
21682 = cp_parser_constant_expression (parser
,
21683 /*allow_non_constant=*/true,
21685 if (!non_constant_p
)
21687 else if (error_operand_p (bounds
))
21688 /* Already gave an error. */;
21689 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21690 /* Let compute_array_index_type diagnose this. */;
21691 else if (!parser
->in_function_body
21692 || current_binding_level
->kind
== sk_function_parms
)
21694 /* Normally, the array bound must be an integral constant
21695 expression. However, as an extension, we allow VLAs
21696 in function scopes as long as they aren't part of a
21697 parameter declaration. */
21698 cp_parser_error (parser
,
21699 "array bound is not an integer constant");
21700 bounds
= error_mark_node
;
21702 else if (processing_template_decl
21703 && !type_dependent_expression_p (bounds
))
21705 /* Remember this wasn't a constant-expression. */
21706 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
21707 TREE_SIDE_EFFECTS (bounds
) = 1;
21711 bounds
= NULL_TREE
;
21712 /* Look for the closing `]'. */
21713 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21715 declarator
= cp_error_declarator
;
21719 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21720 declarator
= make_array_declarator (declarator
, bounds
);
21721 declarator
->std_attributes
= attrs
;
21723 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
21726 tree qualifying_scope
;
21727 tree unqualified_name
;
21729 special_function_kind sfk
;
21731 bool pack_expansion_p
= false;
21732 cp_token
*declarator_id_start_token
;
21734 /* Parse a declarator-id */
21735 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
21738 cp_parser_parse_tentatively (parser
);
21740 /* If we see an ellipsis, we should be looking at a
21742 if (token
->type
== CPP_ELLIPSIS
)
21744 /* Consume the `...' */
21745 cp_lexer_consume_token (parser
->lexer
);
21747 pack_expansion_p
= true;
21751 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
21753 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
21754 qualifying_scope
= parser
->scope
;
21759 if (!unqualified_name
&& pack_expansion_p
)
21761 /* Check whether an error occurred. */
21762 okay
= !cp_parser_error_occurred (parser
);
21764 /* We already consumed the ellipsis to mark a
21765 parameter pack, but we have no way to report it,
21766 so abort the tentative parse. We will be exiting
21767 immediately anyway. */
21768 cp_parser_abort_tentative_parse (parser
);
21771 okay
= cp_parser_parse_definitely (parser
);
21774 unqualified_name
= error_mark_node
;
21775 else if (unqualified_name
21776 && (qualifying_scope
21777 || (!identifier_p (unqualified_name
))))
21779 cp_parser_error (parser
, "expected unqualified-id");
21780 unqualified_name
= error_mark_node
;
21784 if (!unqualified_name
)
21786 if (unqualified_name
== error_mark_node
)
21788 declarator
= cp_error_declarator
;
21789 pack_expansion_p
= false;
21790 declarator
->parameter_pack_p
= false;
21794 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21796 if (qualifying_scope
&& at_namespace_scope_p ()
21797 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
21799 /* In the declaration of a member of a template class
21800 outside of the class itself, the SCOPE will sometimes
21801 be a TYPENAME_TYPE. For example, given:
21803 template <typename T>
21804 int S<T>::R::i = 3;
21806 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21807 this context, we must resolve S<T>::R to an ordinary
21808 type, rather than a typename type.
21810 The reason we normally avoid resolving TYPENAME_TYPEs
21811 is that a specialization of `S' might render
21812 `S<T>::R' not a type. However, if `S' is
21813 specialized, then this `i' will not be used, so there
21814 is no harm in resolving the types here. */
21817 /* Resolve the TYPENAME_TYPE. */
21818 type
= resolve_typename_type (qualifying_scope
,
21819 /*only_current_p=*/false);
21820 /* If that failed, the declarator is invalid. */
21821 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21823 if (typedef_variant_p (type
))
21824 error_at (declarator_id_start_token
->location
,
21825 "cannot define member of dependent typedef "
21828 error_at (declarator_id_start_token
->location
,
21829 "%<%T::%E%> is not a type",
21830 TYPE_CONTEXT (qualifying_scope
),
21831 TYPE_IDENTIFIER (qualifying_scope
));
21833 qualifying_scope
= type
;
21838 if (unqualified_name
)
21842 if (qualifying_scope
21843 && CLASS_TYPE_P (qualifying_scope
))
21844 class_type
= qualifying_scope
;
21846 class_type
= current_class_type
;
21848 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21850 tree name_type
= TREE_TYPE (unqualified_name
);
21852 if (!class_type
|| !same_type_p (name_type
, class_type
))
21854 /* We do not attempt to print the declarator
21855 here because we do not have enough
21856 information about its original syntactic
21858 cp_parser_error (parser
, "invalid declarator");
21859 declarator
= cp_error_declarator
;
21862 else if (qualifying_scope
21863 && CLASSTYPE_USE_TEMPLATE (name_type
))
21865 error_at (declarator_id_start_token
->location
,
21866 "invalid use of constructor as a template");
21867 inform (declarator_id_start_token
->location
,
21868 "use %<%T::%D%> instead of %<%T::%D%> to "
21869 "name the constructor in a qualified name",
21871 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21872 class_type
, name_type
);
21873 declarator
= cp_error_declarator
;
21876 unqualified_name
= constructor_name (class_type
);
21881 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21882 sfk
= sfk_destructor
;
21883 else if (identifier_p (unqualified_name
)
21884 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21885 sfk
= sfk_conversion
;
21886 else if (/* There's no way to declare a constructor
21887 for an unnamed type, even if the type
21888 got a name for linkage purposes. */
21889 !TYPE_WAS_UNNAMED (class_type
)
21890 /* Handle correctly (c++/19200):
21904 friend void N::S();
21906 && (!friend_p
|| class_type
== qualifying_scope
)
21907 && constructor_name_p (unqualified_name
,
21909 sfk
= sfk_constructor
;
21910 else if (is_overloaded_fn (unqualified_name
)
21911 && DECL_CONSTRUCTOR_P (get_first_fn
21912 (unqualified_name
)))
21913 sfk
= sfk_constructor
;
21915 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21916 *ctor_dtor_or_conv_p
= -1;
21919 declarator
= make_id_declarator (qualifying_scope
,
21921 sfk
, token
->location
);
21922 declarator
->std_attributes
= attrs
;
21923 declarator
->parameter_pack_p
= pack_expansion_p
;
21925 if (pack_expansion_p
)
21926 maybe_warn_variadic_templates ();
21928 /* We're looking for this case in [temp.res]:
21929 A qualified-id is assumed to name a type if [...]
21930 - it is a decl-specifier of the decl-specifier-seq of a
21931 parameter-declaration in a declarator of a function or
21932 function template declaration, ... */
21933 if (cxx_dialect
>= cxx20
21934 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21935 && declarator
->kind
== cdk_id
21936 && !at_class_scope_p ()
21937 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21939 /* ...whose declarator-id is qualified. If it isn't, never
21940 assume the parameters to refer to types. */
21941 if (qualifying_scope
== NULL_TREE
)
21942 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21945 /* Now we have something like
21946 template <typename T> int C::x(S::p);
21947 which can be a function template declaration or a
21948 variable template definition. If name lookup for
21949 the declarator-id C::x finds one or more function
21950 templates, assume S::p to name a type. Otherwise,
21953 = cp_parser_lookup_name (parser
, unqualified_name
,
21955 /*is_template=*/false,
21956 /*is_namespace=*/false,
21957 /*check_dependency=*/false,
21958 /*ambiguous_decls=*/NULL
,
21961 if (!is_overloaded_fn (decl
)
21963 template<typename T>
21964 A<T>::A(T::type) { } */
21965 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
21966 && constructor_name_p (unqualified_name
,
21967 qualifying_scope
)))
21968 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21973 handle_declarator
:;
21974 scope
= get_scope_of_declarator (declarator
);
21977 /* Any names that appear after the declarator-id for a
21978 member are looked up in the containing scope. */
21979 if (at_function_scope_p ())
21981 /* But declarations with qualified-ids can't appear in a
21983 cp_parser_error (parser
, "qualified-id in declaration");
21984 declarator
= cp_error_declarator
;
21987 pushed_scope
= push_scope (scope
);
21989 parser
->in_declarator_p
= true;
21990 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21991 || (declarator
&& declarator
->kind
== cdk_id
))
21992 /* Default args are only allowed on function
21994 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21996 parser
->default_arg_ok_p
= false;
22005 /* For an abstract declarator, we might wind up with nothing at this
22006 point. That's an error; the declarator is not optional. */
22008 cp_parser_error (parser
, "expected declarator");
22009 else if (open_paren
)
22011 /* Record overly parenthesized declarator so we can give a
22012 diagnostic about confusing decl/expr disambiguation. */
22013 if (declarator
->kind
== cdk_array
)
22015 /* If the open and close parens are on different lines, this
22016 is probably a formatting thing, so ignore. */
22017 expanded_location open
= expand_location (open_paren
->location
);
22018 expanded_location close
= expand_location (close_paren
->location
);
22019 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
22023 declarator
->parenthesized
= make_location (open_paren
->location
,
22024 open_paren
->location
,
22025 close_paren
->location
);
22028 /* If we entered a scope, we must exit it now. */
22030 pop_scope (pushed_scope
);
22032 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22033 parser
->in_declarator_p
= saved_in_declarator_p
;
22038 /* Parse a ptr-operator.
22041 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22042 * cv-qualifier-seq [opt]
22044 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
22045 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22050 & cv-qualifier-seq [opt]
22052 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
22053 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
22054 an rvalue reference. In the case of a pointer-to-member, *TYPE is
22055 filled in with the TYPE containing the member. *CV_QUALS is
22056 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
22057 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
22058 Note that the tree codes returned by this function have nothing
22059 to do with the types of trees that will be eventually be created
22060 to represent the pointer or reference type being parsed. They are
22061 just constants with suggestive names. */
22062 static enum tree_code
22063 cp_parser_ptr_operator (cp_parser
* parser
,
22065 cp_cv_quals
*cv_quals
,
22068 enum tree_code code
= ERROR_MARK
;
22070 tree attrs
= NULL_TREE
;
22072 /* Assume that it's not a pointer-to-member. */
22074 /* And that there are no cv-qualifiers. */
22075 *cv_quals
= TYPE_UNQUALIFIED
;
22077 /* Peek at the next token. */
22078 token
= cp_lexer_peek_token (parser
->lexer
);
22080 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
22081 if (token
->type
== CPP_MULT
)
22082 code
= INDIRECT_REF
;
22083 else if (token
->type
== CPP_AND
)
22085 else if ((cxx_dialect
!= cxx98
) &&
22086 token
->type
== CPP_AND_AND
) /* C++0x only */
22087 code
= NON_LVALUE_EXPR
;
22089 if (code
!= ERROR_MARK
)
22091 /* Consume the `*', `&' or `&&'. */
22092 cp_lexer_consume_token (parser
->lexer
);
22094 /* A `*' can be followed by a cv-qualifier-seq, and so can a
22095 `&', if we are allowing GNU extensions. (The only qualifier
22096 that can legally appear after `&' is `restrict', but that is
22097 enforced during semantic analysis. */
22098 if (code
== INDIRECT_REF
22099 || cp_parser_allow_gnu_extensions_p (parser
))
22100 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
22102 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22103 if (attributes
!= NULL
)
22104 *attributes
= attrs
;
22108 /* Try the pointer-to-member case. */
22109 cp_parser_parse_tentatively (parser
);
22110 /* Look for the optional `::' operator. */
22111 cp_parser_global_scope_opt (parser
,
22112 /*current_scope_valid_p=*/false);
22113 /* Look for the nested-name specifier. */
22114 token
= cp_lexer_peek_token (parser
->lexer
);
22115 cp_parser_nested_name_specifier (parser
,
22116 /*typename_keyword_p=*/false,
22117 /*check_dependency_p=*/true,
22119 /*is_declaration=*/false);
22120 /* If we found it, and the next token is a `*', then we are
22121 indeed looking at a pointer-to-member operator. */
22122 if (!cp_parser_error_occurred (parser
)
22123 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
22125 /* Indicate that the `*' operator was used. */
22126 code
= INDIRECT_REF
;
22128 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
22129 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
22130 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
22131 error_at (token
->location
, "cannot form pointer to member of "
22132 "non-class %q#T", parser
->scope
);
22135 /* The type of which the member is a member is given by the
22137 *type
= parser
->scope
;
22138 /* The next name will not be qualified. */
22139 parser
->scope
= NULL_TREE
;
22140 parser
->qualifying_scope
= NULL_TREE
;
22141 parser
->object_scope
= NULL_TREE
;
22142 /* Look for optional c++11 attributes. */
22143 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22144 if (attributes
!= NULL
)
22145 *attributes
= attrs
;
22146 /* Look for the optional cv-qualifier-seq. */
22147 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
22150 /* If that didn't work we don't have a ptr-operator. */
22151 if (!cp_parser_parse_definitely (parser
))
22152 cp_parser_error (parser
, "expected ptr-operator");
22158 /* Parse an (optional) cv-qualifier-seq.
22161 cv-qualifier cv-qualifier-seq [opt]
22172 Returns a bitmask representing the cv-qualifiers. */
22175 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
22177 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
22182 cp_cv_quals cv_qualifier
;
22184 /* Peek at the next token. */
22185 token
= cp_lexer_peek_token (parser
->lexer
);
22186 /* See if it's a cv-qualifier. */
22187 switch (token
->keyword
)
22190 cv_qualifier
= TYPE_QUAL_CONST
;
22194 cv_qualifier
= TYPE_QUAL_VOLATILE
;
22198 cv_qualifier
= TYPE_QUAL_RESTRICT
;
22202 cv_qualifier
= TYPE_UNQUALIFIED
;
22209 if (cv_quals
& cv_qualifier
)
22211 gcc_rich_location
richloc (token
->location
);
22212 richloc
.add_fixit_remove ();
22213 error_at (&richloc
, "duplicate cv-qualifier");
22214 cp_lexer_purge_token (parser
->lexer
);
22218 cp_lexer_consume_token (parser
->lexer
);
22219 cv_quals
|= cv_qualifier
;
22226 /* Parse an (optional) ref-qualifier
22232 Returns cp_ref_qualifier representing ref-qualifier. */
22234 static cp_ref_qualifier
22235 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
22237 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
22239 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
22240 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
22245 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
22246 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22248 switch (token
->type
)
22251 curr_ref_qual
= REF_QUAL_LVALUE
;
22255 curr_ref_qual
= REF_QUAL_RVALUE
;
22259 curr_ref_qual
= REF_QUAL_NONE
;
22263 if (!curr_ref_qual
)
22267 error_at (token
->location
, "multiple ref-qualifiers");
22268 cp_lexer_purge_token (parser
->lexer
);
22272 ref_qual
= curr_ref_qual
;
22273 cp_lexer_consume_token (parser
->lexer
);
22280 /* Parse an optional tx-qualifier.
22284 transaction_safe_dynamic */
22287 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
22289 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22290 if (token
->type
== CPP_NAME
)
22292 tree name
= token
->u
.value
;
22293 const char *p
= IDENTIFIER_POINTER (name
);
22294 const int len
= strlen ("transaction_safe");
22295 if (!strncmp (p
, "transaction_safe", len
))
22299 || !strcmp (p
, "_dynamic"))
22301 cp_lexer_consume_token (parser
->lexer
);
22304 error ("%qE requires %<-fgnu-tm%>", name
);
22315 /* Parse an (optional) virt-specifier-seq.
22317 virt-specifier-seq:
22318 virt-specifier virt-specifier-seq [opt]
22324 Returns a bitmask representing the virt-specifiers. */
22326 static cp_virt_specifiers
22327 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
22329 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22334 cp_virt_specifiers virt_specifier
;
22336 /* Peek at the next token. */
22337 token
= cp_lexer_peek_token (parser
->lexer
);
22338 /* See if it's a virt-specifier-qualifier. */
22339 if (token
->type
!= CPP_NAME
)
22341 if (id_equal (token
->u
.value
, "override"))
22343 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
22344 virt_specifier
= VIRT_SPEC_OVERRIDE
;
22346 else if (id_equal (token
->u
.value
, "final"))
22348 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
22349 virt_specifier
= VIRT_SPEC_FINAL
;
22351 else if (id_equal (token
->u
.value
, "__final"))
22353 virt_specifier
= VIRT_SPEC_FINAL
;
22358 if (virt_specifiers
& virt_specifier
)
22360 gcc_rich_location
richloc (token
->location
);
22361 richloc
.add_fixit_remove ();
22362 error_at (&richloc
, "duplicate virt-specifier");
22363 cp_lexer_purge_token (parser
->lexer
);
22367 cp_lexer_consume_token (parser
->lexer
);
22368 virt_specifiers
|= virt_specifier
;
22371 return virt_specifiers
;
22374 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22375 is in scope even though it isn't real. */
22378 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
22382 if (current_class_ptr
)
22384 /* We don't clear this between NSDMIs. Is it already what we want? */
22385 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
22386 if (DECL_P (current_class_ptr
)
22387 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
22388 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
22389 && cp_type_quals (type
) == quals
)
22393 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
22394 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22395 current_class_ptr
= NULL_TREE
;
22397 = cp_build_fold_indirect_ref (this_parm
);
22398 current_class_ptr
= this_parm
;
22401 /* Return true iff our current scope is a non-static data member
22405 parsing_nsdmi (void)
22407 /* We recognize NSDMI context by the context-less 'this' pointer set up
22408 by the function above. */
22409 if (current_class_ptr
22410 && TREE_CODE (current_class_ptr
) == PARM_DECL
22411 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
22416 /* Parse a late-specified return type, if any. This is not a separate
22417 non-terminal, but part of a function declarator, which looks like
22419 -> trailing-type-specifier-seq abstract-declarator(opt)
22421 Returns the type indicated by the type-id.
22423 In addition to this, parse any queued up #pragma omp declare simd
22424 clauses, and #pragma acc routine clauses.
22426 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22430 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
22431 tree
& requires_clause
)
22434 tree type
= NULL_TREE
;
22435 bool declare_simd_p
= (parser
->omp_declare_simd
22437 && declarator
->kind
== cdk_id
);
22439 bool oacc_routine_p
= (parser
->oacc_routine
22441 && declarator
->kind
== cdk_id
);
22443 /* Peek at the next token. */
22444 token
= cp_lexer_peek_token (parser
->lexer
);
22445 /* A late-specified return type is indicated by an initial '->'. */
22446 if (token
->type
!= CPP_DEREF
22447 && token
->keyword
!= RID_REQUIRES
22448 && !(token
->type
== CPP_NAME
22449 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
22450 && !(declare_simd_p
|| oacc_routine_p
))
22453 if (token
->type
== CPP_DEREF
)
22455 /* Consume the ->. */
22456 cp_lexer_consume_token (parser
->lexer
);
22458 type
= cp_parser_trailing_type_id (parser
);
22461 /* Function declarations may be followed by a trailing
22462 requires-clause. */
22463 requires_clause
= cp_parser_requires_clause_opt (parser
, false);
22465 if (declare_simd_p
)
22466 declarator
->attributes
22467 = cp_parser_late_parsing_omp_declare_simd (parser
,
22468 declarator
->attributes
);
22469 if (oacc_routine_p
)
22470 declarator
->attributes
22471 = cp_parser_late_parsing_oacc_routine (parser
,
22472 declarator
->attributes
);
22477 /* Parse a declarator-id.
22481 :: [opt] nested-name-specifier [opt] type-name
22483 In the `id-expression' case, the value returned is as for
22484 cp_parser_id_expression if the id-expression was an unqualified-id.
22485 If the id-expression was a qualified-id, then a SCOPE_REF is
22486 returned. The first operand is the scope (either a NAMESPACE_DECL
22487 or TREE_TYPE), but the second is still just a representation of an
22491 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
22494 /* The expression must be an id-expression. Assume that qualified
22495 names are the names of types so that:
22498 int S<T>::R::i = 3;
22500 will work; we must treat `S<T>::R' as the name of a type.
22501 Similarly, assume that qualified names are templates, where
22505 int S<T>::R<T>::i = 3;
22508 id
= cp_parser_id_expression (parser
,
22509 /*template_keyword_p=*/false,
22510 /*check_dependency_p=*/false,
22511 /*template_p=*/NULL
,
22512 /*declarator_p=*/true,
22514 if (id
&& BASELINK_P (id
))
22515 id
= BASELINK_FUNCTIONS (id
);
22519 /* Parse a type-id.
22522 type-specifier-seq abstract-declarator [opt]
22524 The parser flags FLAGS is used to control type-specifier parsing.
22526 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22528 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22529 i.e. we've just seen "->".
22531 Returns the TYPE specified. */
22534 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
22535 bool is_template_arg
, bool is_trailing_return
,
22536 location_t
*type_location
)
22538 cp_decl_specifier_seq type_specifier_seq
;
22539 cp_declarator
*abstract_declarator
;
22541 /* Parse the type-specifier-seq. */
22542 cp_parser_type_specifier_seq (parser
, flags
,
22543 /*is_declaration=*/false,
22544 is_trailing_return
,
22545 &type_specifier_seq
);
22547 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
22549 if (is_template_arg
&& type_specifier_seq
.type
22550 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
22551 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
22552 /* A bare template name as a template argument is a template template
22553 argument, not a placeholder, so fail parsing it as a type argument. */
22555 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
22556 cp_parser_simulate_error (parser
);
22557 return error_mark_node
;
22559 if (type_specifier_seq
.type
== error_mark_node
)
22560 return error_mark_node
;
22562 /* There might or might not be an abstract declarator. */
22563 cp_parser_parse_tentatively (parser
);
22564 /* Look for the declarator. */
22565 abstract_declarator
22566 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
22567 CP_PARSER_FLAGS_NONE
, NULL
,
22568 /*parenthesized_p=*/NULL
,
22569 /*member_p=*/false,
22570 /*friend_p=*/false,
22571 /*static_p=*/false);
22572 /* Check to see if there really was a declarator. */
22573 if (!cp_parser_parse_definitely (parser
))
22574 abstract_declarator
= NULL
;
22576 bool auto_typeid_ok
= false;
22577 /* The concepts TS allows 'auto' as a type-id. */
22578 if (flag_concepts_ts
)
22579 auto_typeid_ok
= !parser
->in_type_id_in_expr_p
;
22580 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
22581 outside the template-argument-list context here only for the sake of
22582 diagnostic: grokdeclarator then can emit a better error message for
22583 e.g. using T = auto. */
22584 else if (flag_concepts
)
22585 auto_typeid_ok
= (!parser
->in_type_id_in_expr_p
22586 && !parser
->in_template_argument_list_p
);
22588 if (type_specifier_seq
.type
22590 /* None of the valid uses of 'auto' in C++14 involve the type-id
22591 nonterminal, but it is valid in a trailing-return-type. */
22592 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
22593 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
22595 /* A type-id with type 'auto' is only ok if the abstract declarator
22596 is a function declarator with a late-specified return type.
22598 A type-id with 'auto' is also valid in a trailing-return-type
22599 in a compound-requirement. */
22600 if (abstract_declarator
22601 && abstract_declarator
->kind
== cdk_function
22602 && abstract_declarator
->u
.function
.late_return_type
)
22604 else if (parser
->in_result_type_constraint_p
)
22608 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
22609 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
22611 error_at (loc
, "missing template arguments after %qT",
22613 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
22616 else if (parser
->in_template_argument_list_p
)
22617 error_at (loc
, "%qT not permitted in template argument",
22620 error_at (loc
, "invalid use of %qT", auto_node
);
22621 return error_mark_node
;
22625 return groktypename (&type_specifier_seq
, abstract_declarator
,
22629 /* Wrapper for cp_parser_type_id_1. */
22632 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
22633 location_t
*type_location
)
22635 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
22638 /* Wrapper for cp_parser_type_id_1. */
22641 cp_parser_template_type_arg (cp_parser
*parser
)
22644 const char *saved_message
= parser
->type_definition_forbidden_message
;
22645 parser
->type_definition_forbidden_message
22646 = G_("types may not be defined in template arguments");
22647 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
22648 parser
->type_definition_forbidden_message
= saved_message
;
22649 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
22651 error ("invalid use of %<auto%> in template argument");
22652 r
= error_mark_node
;
22657 /* Wrapper for cp_parser_type_id_1. */
22660 cp_parser_trailing_type_id (cp_parser
*parser
)
22662 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
22663 false, true, NULL
);
22666 /* Parse a type-specifier-seq.
22668 type-specifier-seq:
22669 type-specifier type-specifier-seq [opt]
22673 type-specifier-seq:
22674 attributes type-specifier-seq [opt]
22676 The parser flags FLAGS is used to control type-specifier parsing.
22678 If IS_DECLARATION is true, we are at the start of a "condition" or
22679 exception-declaration, so we might be followed by a declarator-id.
22681 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22682 i.e. we've just seen "->".
22684 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22687 cp_parser_type_specifier_seq (cp_parser
* parser
,
22688 cp_parser_flags flags
,
22689 bool is_declaration
,
22690 bool is_trailing_return
,
22691 cp_decl_specifier_seq
*type_specifier_seq
)
22693 bool seen_type_specifier
= false;
22694 cp_token
*start_token
= NULL
;
22696 /* Clear the TYPE_SPECIFIER_SEQ. */
22697 clear_decl_specs (type_specifier_seq
);
22699 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
22700 /* In the context of a trailing return type, enum E { } is an
22701 elaborated-type-specifier followed by a function-body, not an
22703 if (is_trailing_return
)
22704 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
22706 /* Parse the type-specifiers and attributes. */
22709 tree type_specifier
;
22710 bool is_cv_qualifier
;
22712 /* Check for attributes first. */
22713 if (cp_next_tokens_can_be_attribute_p (parser
))
22715 /* GNU attributes at the end of a declaration apply to the
22716 declaration as a whole, not to the trailing return type. So look
22717 ahead to see if these attributes are at the end. */
22718 if (seen_type_specifier
&& is_trailing_return
22719 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
22721 size_t n
= cp_parser_skip_attributes_opt (parser
, 1);
22722 cp_token
*tok
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22723 if (tok
->type
== CPP_SEMICOLON
|| tok
->type
== CPP_COMMA
22724 || tok
->type
== CPP_EQ
|| tok
->type
== CPP_OPEN_BRACE
)
22727 type_specifier_seq
->attributes
22728 = attr_chainon (type_specifier_seq
->attributes
,
22729 cp_parser_attributes_opt (parser
));
22733 /* record the token of the beginning of the type specifier seq,
22734 for error reporting purposes*/
22736 start_token
= cp_lexer_peek_token (parser
->lexer
);
22738 /* Look for the type-specifier. */
22739 type_specifier
= cp_parser_type_specifier (parser
,
22741 type_specifier_seq
,
22742 /*is_declaration=*/false,
22745 if (!type_specifier
)
22747 /* If the first type-specifier could not be found, this is not a
22748 type-specifier-seq at all. */
22749 if (!seen_type_specifier
)
22751 /* Set in_declarator_p to avoid skipping to the semicolon. */
22752 int in_decl
= parser
->in_declarator_p
;
22753 parser
->in_declarator_p
= true;
22755 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
22756 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22757 cp_parser_error (parser
, "expected type-specifier");
22759 parser
->in_declarator_p
= in_decl
;
22761 type_specifier_seq
->type
= error_mark_node
;
22764 /* If subsequent type-specifiers could not be found, the
22765 type-specifier-seq is complete. */
22769 seen_type_specifier
= true;
22770 /* The standard says that a condition can be:
22772 type-specifier-seq declarator = assignment-expression
22779 we should treat the "S" as a declarator, not as a
22780 type-specifier. The standard doesn't say that explicitly for
22781 type-specifier-seq, but it does say that for
22782 decl-specifier-seq in an ordinary declaration. Perhaps it
22783 would be clearer just to allow a decl-specifier-seq here, and
22784 then add a semantic restriction that if any decl-specifiers
22785 that are not type-specifiers appear, the program is invalid. */
22786 if (is_declaration
&& !is_cv_qualifier
)
22787 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
22791 /* Return whether the function currently being declared has an associated
22792 template parameter list. */
22795 function_being_declared_is_template_p (cp_parser
* parser
)
22797 if (!current_template_parms
|| processing_template_parmlist
)
22800 if (parser
->implicit_template_scope
)
22803 if (at_class_scope_p ()
22804 && TYPE_BEING_DEFINED (current_class_type
))
22805 return parser
->num_template_parameter_lists
!= 0;
22807 return ((int) parser
->num_template_parameter_lists
> template_class_depth
22808 (current_class_type
));
22811 /* Parse a parameter-declaration-clause.
22813 parameter-declaration-clause:
22814 parameter-declaration-list [opt] ... [opt]
22815 parameter-declaration-list , ...
22817 The parser flags FLAGS is used to control type-specifier parsing.
22819 Returns a representation for the parameter declarations. A return
22820 value of NULL indicates a parameter-declaration-clause consisting
22821 only of an ellipsis. */
22824 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
22825 cp_parser_flags flags
)
22831 temp_override
<bool> cleanup
22832 (parser
->auto_is_implicit_function_template_parm_p
);
22834 if (!processing_specialization
22835 && !processing_template_parmlist
22836 && !processing_explicit_instantiation
22837 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22838 actual function or a random abstract declarator. */
22839 && parser
->default_arg_ok_p
)
22840 if (!current_function_decl
22841 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
22842 parser
->auto_is_implicit_function_template_parm_p
= true;
22844 /* Peek at the next token. */
22845 token
= cp_lexer_peek_token (parser
->lexer
);
22846 /* Check for trivial parameter-declaration-clauses. */
22847 if (token
->type
== CPP_ELLIPSIS
)
22849 /* Consume the `...' token. */
22850 cp_lexer_consume_token (parser
->lexer
);
22853 else if (token
->type
== CPP_CLOSE_PAREN
)
22854 /* There are no parameters. */
22855 return void_list_node
;
22856 /* Check for `(void)', too, which is a special case. */
22857 else if (token
->keyword
== RID_VOID
22858 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22859 == CPP_CLOSE_PAREN
))
22861 /* Consume the `void' token. */
22862 cp_lexer_consume_token (parser
->lexer
);
22863 /* There are no parameters. */
22864 return explicit_void_list_node
;
22867 /* Parse the parameter-declaration-list. */
22868 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22869 /* If a parse error occurred while parsing the
22870 parameter-declaration-list, then the entire
22871 parameter-declaration-clause is erroneous. */
22872 if (parameters
== error_mark_node
)
22875 /* Peek at the next token. */
22876 token
= cp_lexer_peek_token (parser
->lexer
);
22877 /* If it's a `,', the clause should terminate with an ellipsis. */
22878 if (token
->type
== CPP_COMMA
)
22880 /* Consume the `,'. */
22881 cp_lexer_consume_token (parser
->lexer
);
22882 /* Expect an ellipsis. */
22884 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22886 /* It might also be `...' if the optional trailing `,' was
22888 else if (token
->type
== CPP_ELLIPSIS
)
22890 /* Consume the `...' token. */
22891 cp_lexer_consume_token (parser
->lexer
);
22892 /* And remember that we saw it. */
22896 ellipsis_p
= false;
22898 /* Finish the parameter list. */
22900 parameters
= chainon (parameters
, void_list_node
);
22905 /* Parse a parameter-declaration-list.
22907 parameter-declaration-list:
22908 parameter-declaration
22909 parameter-declaration-list , parameter-declaration
22911 The parser flags FLAGS is used to control type-specifier parsing.
22913 Returns a representation of the parameter-declaration-list, as for
22914 cp_parser_parameter_declaration_clause. However, the
22915 `void_list_node' is never appended to the list. */
22918 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22920 tree parameters
= NULL_TREE
;
22921 tree
*tail
= ¶meters
;
22922 bool saved_in_unbraced_linkage_specification_p
;
22925 /* The special considerations that apply to a function within an
22926 unbraced linkage specifications do not apply to the parameters
22927 to the function. */
22928 saved_in_unbraced_linkage_specification_p
22929 = parser
->in_unbraced_linkage_specification_p
;
22930 parser
->in_unbraced_linkage_specification_p
= false;
22932 /* Look for more parameters. */
22935 cp_parameter_declarator
*parameter
;
22936 tree decl
= error_mark_node
;
22937 bool parenthesized_p
= false;
22939 /* Parse the parameter. */
22941 = cp_parser_parameter_declaration (parser
, flags
,
22942 /*template_parm_p=*/false,
22945 /* We don't know yet if the enclosing context is deprecated, so wait
22946 and warn in grokparms if appropriate. */
22947 deprecated_state
= DEPRECATED_SUPPRESS
;
22951 decl
= grokdeclarator (parameter
->declarator
,
22952 ¶meter
->decl_specifiers
,
22954 parameter
->default_argument
!= NULL_TREE
,
22955 ¶meter
->decl_specifiers
.attributes
);
22956 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22957 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22960 deprecated_state
= DEPRECATED_NORMAL
;
22962 /* If a parse error occurred parsing the parameter declaration,
22963 then the entire parameter-declaration-list is erroneous. */
22964 if (decl
== error_mark_node
)
22966 parameters
= error_mark_node
;
22970 if (parameter
->decl_specifiers
.attributes
)
22971 cplus_decl_attributes (&decl
,
22972 parameter
->decl_specifiers
.attributes
,
22974 if (DECL_NAME (decl
))
22975 decl
= pushdecl (decl
);
22977 if (decl
!= error_mark_node
)
22979 retrofit_lang_decl (decl
);
22980 DECL_PARM_INDEX (decl
) = ++index
;
22981 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22984 /* Add the new parameter to the list. */
22985 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22986 tail
= &TREE_CHAIN (*tail
);
22988 /* If the parameters were parenthesized, it's the case of
22989 T foo(X(x)) which looks like a variable definition but
22990 is a function declaration. */
22991 if (index
== 1 || PARENTHESIZED_LIST_P (parameters
))
22992 PARENTHESIZED_LIST_P (parameters
) = parenthesized_p
;
22994 /* Peek at the next token. */
22995 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22996 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22997 /* These are for Objective-C++ */
22998 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22999 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23000 /* The parameter-declaration-list is complete. */
23002 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23006 /* Peek at the next token. */
23007 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23008 /* If it's an ellipsis, then the list is complete. */
23009 if (token
->type
== CPP_ELLIPSIS
)
23011 /* Otherwise, there must be more parameters. Consume the
23013 cp_lexer_consume_token (parser
->lexer
);
23014 /* When parsing something like:
23016 int i(float f, double d)
23018 we can tell after seeing the declaration for "f" that we
23019 are not looking at an initialization of a variable "i",
23020 but rather at the declaration of a function "i".
23022 Due to the fact that the parsing of template arguments
23023 (as specified to a template-id) requires backtracking we
23024 cannot use this technique when inside a template argument
23026 if (!parser
->in_template_argument_list_p
23027 && !parser
->in_type_id_in_expr_p
23028 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
23029 /* However, a parameter-declaration of the form
23030 "float(f)" (which is a valid declaration of a
23031 parameter "f") can also be interpreted as an
23032 expression (the conversion of "f" to "float"). */
23033 && !parenthesized_p
)
23034 cp_parser_commit_to_tentative_parse (parser
);
23038 cp_parser_error (parser
, "expected %<,%> or %<...%>");
23039 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
23040 cp_parser_skip_to_closing_parenthesis (parser
,
23041 /*recovering=*/true,
23042 /*or_comma=*/false,
23043 /*consume_paren=*/false);
23048 parser
->in_unbraced_linkage_specification_p
23049 = saved_in_unbraced_linkage_specification_p
;
23051 /* Reset implicit_template_scope if we are about to leave the function
23052 parameter list that introduced it. Note that for out-of-line member
23053 definitions, there will be one or more class scopes before we get to
23054 the template parameter scope. */
23056 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
23057 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
23059 while (maybe_its
->kind
== sk_class
)
23060 maybe_its
= maybe_its
->level_chain
;
23061 if (maybe_its
== its
)
23063 parser
->implicit_template_parms
= 0;
23064 parser
->implicit_template_scope
= 0;
23071 /* Parse a parameter declaration.
23073 parameter-declaration:
23074 decl-specifier-seq ... [opt] declarator
23075 decl-specifier-seq declarator = assignment-expression
23076 decl-specifier-seq ... [opt] abstract-declarator [opt]
23077 decl-specifier-seq abstract-declarator [opt] = assignment-expression
23079 The parser flags FLAGS is used to control type-specifier parsing.
23081 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
23082 declares a template parameter. (In that case, a non-nested `>'
23083 token encountered during the parsing of the assignment-expression
23084 is not interpreted as a greater-than operator.)
23086 Returns a representation of the parameter, or NULL if an error
23087 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
23088 true iff the declarator is of the form "(p)". */
23090 static cp_parameter_declarator
*
23091 cp_parser_parameter_declaration (cp_parser
*parser
,
23092 cp_parser_flags flags
,
23093 bool template_parm_p
,
23094 bool *parenthesized_p
)
23096 int declares_class_or_enum
;
23097 cp_decl_specifier_seq decl_specifiers
;
23098 cp_declarator
*declarator
;
23099 tree default_argument
;
23100 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
23101 const char *saved_message
;
23102 bool template_parameter_pack_p
= false;
23104 /* In a template parameter, `>' is not an operator.
23108 When parsing a default template-argument for a non-type
23109 template-parameter, the first non-nested `>' is taken as the end
23110 of the template parameter-list rather than a greater-than
23113 /* Type definitions may not appear in parameter types. */
23114 saved_message
= parser
->type_definition_forbidden_message
;
23115 parser
->type_definition_forbidden_message
23116 = G_("types may not be defined in parameter types");
23118 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
23119 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
23120 (current_template_parms
)) : 0);
23122 /* Parse the declaration-specifiers. */
23123 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23124 cp_parser_decl_specifier_seq (parser
,
23127 &declares_class_or_enum
);
23129 /* Complain about missing 'typename' or other invalid type names. */
23130 if (!decl_specifiers
.any_type_specifiers_p
23131 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23132 decl_specifiers
.type
= error_mark_node
;
23134 /* If an error occurred, there's no reason to attempt to parse the
23135 rest of the declaration. */
23136 if (cp_parser_error_occurred (parser
))
23138 parser
->type_definition_forbidden_message
= saved_message
;
23142 /* Peek at the next token. */
23143 token
= cp_lexer_peek_token (parser
->lexer
);
23145 /* If the next token is a `)', `,', `=', `>', or `...', then there
23146 is no declarator. However, when variadic templates are enabled,
23147 there may be a declarator following `...'. */
23148 if (token
->type
== CPP_CLOSE_PAREN
23149 || token
->type
== CPP_COMMA
23150 || token
->type
== CPP_EQ
23151 || token
->type
== CPP_GREATER
)
23154 if (parenthesized_p
)
23155 *parenthesized_p
= false;
23157 /* Otherwise, there should be a declarator. */
23160 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
23161 parser
->default_arg_ok_p
= false;
23163 /* After seeing a decl-specifier-seq, if the next token is not a
23164 "(", there is no possibility that the code is a valid
23165 expression. Therefore, if parsing tentatively, we commit at
23167 if (!parser
->in_template_argument_list_p
23168 /* In an expression context, having seen:
23172 we cannot be sure whether we are looking at a
23173 function-type (taking a "char" as a parameter) or a cast
23174 of some object of type "char" to "int". */
23175 && !parser
->in_type_id_in_expr_p
23176 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
23177 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
23178 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
23179 cp_parser_commit_to_tentative_parse (parser
);
23180 /* Parse the declarator. */
23181 declarator_token_start
= token
;
23182 declarator
= cp_parser_declarator (parser
,
23183 CP_PARSER_DECLARATOR_EITHER
,
23184 CP_PARSER_FLAGS_NONE
,
23185 /*ctor_dtor_or_conv_p=*/NULL
,
23187 /*member_p=*/false,
23188 /*friend_p=*/false,
23189 /*static_p=*/false);
23190 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23191 /* After the declarator, allow more attributes. */
23192 decl_specifiers
.attributes
23193 = attr_chainon (decl_specifiers
.attributes
,
23194 cp_parser_attributes_opt (parser
));
23196 /* If the declarator is a template parameter pack, remember that and
23197 clear the flag in the declarator itself so we don't get errors
23198 from grokdeclarator. */
23199 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
23201 declarator
->parameter_pack_p
= false;
23202 template_parameter_pack_p
= true;
23206 /* If the next token is an ellipsis, and we have not seen a declarator
23207 name, and if either the type of the declarator contains parameter
23208 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
23209 for, eg, abbreviated integral type names), then we actually have a
23210 parameter pack expansion expression. Otherwise, leave the ellipsis
23211 for a C-style variadic function. */
23212 token
= cp_lexer_peek_token (parser
->lexer
);
23214 /* If a function parameter pack was specified and an implicit template
23215 parameter was introduced during cp_parser_parameter_declaration,
23216 change any implicit parameters introduced into packs. */
23217 if (parser
->implicit_template_parms
23218 && ((token
->type
== CPP_ELLIPSIS
23219 && declarator_can_be_parameter_pack (declarator
))
23220 || (declarator
&& declarator
->parameter_pack_p
)))
23222 int latest_template_parm_idx
= TREE_VEC_LENGTH
23223 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
23225 if (latest_template_parm_idx
!= template_parm_idx
)
23226 decl_specifiers
.type
= convert_generic_types_to_packs
23227 (decl_specifiers
.type
,
23228 template_parm_idx
, latest_template_parm_idx
);
23231 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23233 tree type
= decl_specifiers
.type
;
23235 if (type
&& DECL_P (type
))
23236 type
= TREE_TYPE (type
);
23239 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
23240 && (template_parm_p
|| uses_parameter_packs (type
)))
23241 || (!type
&& template_parm_p
))
23242 && declarator_can_be_parameter_pack (declarator
))
23244 /* Consume the `...'. */
23245 cp_lexer_consume_token (parser
->lexer
);
23246 maybe_warn_variadic_templates ();
23248 /* Build a pack expansion type */
23249 if (template_parm_p
)
23250 template_parameter_pack_p
= true;
23251 else if (declarator
)
23252 declarator
->parameter_pack_p
= true;
23254 decl_specifiers
.type
= make_pack_expansion (type
);
23258 /* The restriction on defining new types applies only to the type
23259 of the parameter, not to the default argument. */
23260 parser
->type_definition_forbidden_message
= saved_message
;
23262 /* If the next token is `=', then process a default argument. */
23263 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23265 tree type
= decl_specifiers
.type
;
23266 token
= cp_lexer_peek_token (parser
->lexer
);
23267 /* If we are defining a class, then the tokens that make up the
23268 default argument must be saved and processed later. */
23269 if (!template_parm_p
&& at_class_scope_p ()
23270 && TYPE_BEING_DEFINED (current_class_type
)
23271 && !LAMBDA_TYPE_P (current_class_type
))
23272 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
23274 /* A constrained-type-specifier may declare a type
23275 template-parameter. */
23276 else if (declares_constrained_type_template_parameter (type
))
23278 = cp_parser_default_type_template_argument (parser
);
23280 /* A constrained-type-specifier may declare a
23281 template-template-parameter. */
23282 else if (declares_constrained_template_template_parameter (type
))
23284 = cp_parser_default_template_template_argument (parser
);
23286 /* Outside of a class definition, we can just parse the
23287 assignment-expression. */
23290 = cp_parser_default_argument (parser
, template_parm_p
);
23292 if (!parser
->default_arg_ok_p
)
23294 permerror (token
->location
,
23295 "default arguments are only "
23296 "permitted for function parameters");
23298 else if ((declarator
&& declarator
->parameter_pack_p
)
23299 || template_parameter_pack_p
23300 || (decl_specifiers
.type
23301 && PACK_EXPANSION_P (decl_specifiers
.type
)))
23303 /* Find the name of the parameter pack. */
23304 cp_declarator
*id_declarator
= declarator
;
23305 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
23306 id_declarator
= id_declarator
->declarator
;
23308 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
23309 error_at (declarator_token_start
->location
,
23311 ? G_("template parameter pack %qD "
23312 "cannot have a default argument")
23313 : G_("parameter pack %qD cannot have "
23314 "a default argument"),
23315 id_declarator
->u
.id
.unqualified_name
);
23317 error_at (declarator_token_start
->location
,
23319 ? G_("template parameter pack cannot have "
23320 "a default argument")
23321 : G_("parameter pack cannot have a "
23322 "default argument"));
23324 default_argument
= NULL_TREE
;
23328 default_argument
= NULL_TREE
;
23330 if (default_argument
)
23331 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
23333 /* Generate a location for the parameter, ranging from the start of the
23334 initial token to the end of the final token (using input_location for
23335 the latter, set up by cp_lexer_set_source_position_from_token when
23338 If we have a identifier, then use it for the caret location, e.g.
23340 extern int callee (int one, int (*two)(int, int), float three);
23341 ~~~~~~^~~~~~~~~~~~~~
23343 otherwise, reuse the start location for the caret location e.g.:
23345 extern int callee (int one, int (*)(int, int), float three);
23349 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
23350 ? declarator
->id_loc
23351 : decl_spec_token_start
->location
);
23352 location_t param_loc
= make_location (caret_loc
,
23353 decl_spec_token_start
->location
,
23356 return make_parameter_declarator (&decl_specifiers
,
23360 template_parameter_pack_p
);
23363 /* Parse a default argument and return it.
23365 TEMPLATE_PARM_P is true if this is a default argument for a
23366 non-type template parameter. */
23368 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
23370 tree default_argument
= NULL_TREE
;
23371 bool saved_greater_than_is_operator_p
;
23372 unsigned char saved_local_variables_forbidden_p
;
23373 bool non_constant_p
, is_direct_init
;
23375 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23377 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
23378 parser
->greater_than_is_operator_p
= !template_parm_p
;
23379 /* Local variable names (and the `this' keyword) may not
23380 appear in a default argument. */
23381 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23382 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
23383 /* Parse the assignment-expression. */
23384 if (template_parm_p
)
23385 push_deferring_access_checks (dk_no_deferred
);
23386 tree saved_class_ptr
= NULL_TREE
;
23387 tree saved_class_ref
= NULL_TREE
;
23388 /* The "this" pointer is not valid in a default argument. */
23391 saved_class_ptr
= current_class_ptr
;
23392 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
23393 saved_class_ref
= current_class_ref
;
23394 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
23397 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
23398 /* Restore the "this" pointer. */
23401 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
23402 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
23404 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
23405 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23406 if (template_parm_p
)
23407 pop_deferring_access_checks ();
23408 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
23409 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23411 return default_argument
;
23414 /* Parse a function-body.
23417 compound_statement */
23420 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
23422 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
23423 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
23427 /* Parse a ctor-initializer-opt followed by a function-body. Return
23428 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23429 is true we are parsing a function-try-block. */
23432 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
23433 bool in_function_try_block
)
23436 const bool check_body_p
23437 = (DECL_CONSTRUCTOR_P (current_function_decl
)
23438 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
23441 if (in_function_try_block
23442 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
23443 && cxx_dialect
< cxx20
)
23445 if (DECL_CONSTRUCTOR_P (current_function_decl
))
23446 pedwarn (input_location
, 0,
23447 "function-try-block body of %<constexpr%> constructor only "
23448 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23450 pedwarn (input_location
, 0,
23451 "function-try-block body of %<constexpr%> function only "
23452 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23455 /* Begin the function body. */
23456 body
= begin_function_body ();
23457 /* Parse the optional ctor-initializer. */
23458 cp_parser_ctor_initializer_opt (parser
);
23460 /* If we're parsing a constexpr constructor definition, we need
23461 to check that the constructor body is indeed empty. However,
23462 before we get to cp_parser_function_body lot of junk has been
23463 generated, so we can't just check that we have an empty block.
23464 Rather we take a snapshot of the outermost block, and check whether
23465 cp_parser_function_body changed its state. */
23468 list
= cur_stmt_list
;
23469 if (STATEMENT_LIST_TAIL (list
))
23470 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
23472 /* Parse the function-body. */
23473 cp_parser_function_body (parser
, in_function_try_block
);
23475 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
23476 /* Finish the function body. */
23477 finish_function_body (body
);
23480 /* Parse an initializer.
23483 = initializer-clause
23484 ( expression-list )
23486 Returns an expression representing the initializer. If no
23487 initializer is present, NULL_TREE is returned.
23489 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
23490 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
23491 set to TRUE if there is no initializer present. If there is an
23492 initializer, and it is not a constant-expression, *NON_CONSTANT_P
23493 is set to true; otherwise it is set to false. */
23496 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
23497 bool* non_constant_p
, bool subexpression_p
)
23502 /* Peek at the next token. */
23503 token
= cp_lexer_peek_token (parser
->lexer
);
23505 /* Let our caller know whether or not this initializer was
23507 *is_direct_init
= (token
->type
!= CPP_EQ
);
23508 /* Assume that the initializer is constant. */
23509 *non_constant_p
= false;
23511 if (token
->type
== CPP_EQ
)
23513 /* Consume the `='. */
23514 cp_lexer_consume_token (parser
->lexer
);
23515 /* Parse the initializer-clause. */
23516 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
23518 else if (token
->type
== CPP_OPEN_PAREN
)
23520 vec
<tree
, va_gc
> *vec
;
23521 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23523 /*allow_expansion_p=*/true,
23526 return error_mark_node
;
23527 init
= build_tree_list_vec (vec
);
23528 release_tree_vector (vec
);
23530 else if (token
->type
== CPP_OPEN_BRACE
)
23532 cp_lexer_set_source_position (parser
->lexer
);
23533 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23534 init
= cp_parser_braced_list (parser
, non_constant_p
);
23535 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
23539 /* Anything else is an error. */
23540 cp_parser_error (parser
, "expected initializer");
23541 init
= error_mark_node
;
23544 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
23545 init
= error_mark_node
;
23550 /* Parse an initializer-clause.
23552 initializer-clause:
23553 assignment-expression
23556 Returns an expression representing the initializer.
23558 If the `assignment-expression' production is used the value
23559 returned is simply a representation for the expression.
23561 Otherwise, calls cp_parser_braced_list. */
23564 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
23566 cp_expr initializer
;
23568 /* Assume the expression is constant. */
23569 *non_constant_p
= false;
23571 /* If it is not a `{', then we are looking at an
23572 assignment-expression. */
23573 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23576 = cp_parser_constant_expression (parser
,
23577 /*allow_non_constant_p=*/true,
23581 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
23583 return initializer
;
23586 /* Parse a brace-enclosed initializer list.
23589 { initializer-list , [opt] }
23590 { designated-initializer-list , [opt] }
23593 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23594 the elements of the initializer-list (or NULL, if the last
23595 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23596 NULL_TREE. There is no way to detect whether or not the optional
23597 trailing `,' was provided. NON_CONSTANT_P is as for
23598 cp_parser_initializer. */
23601 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
23604 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23606 /* Consume the `{' token. */
23607 matching_braces braces
;
23608 braces
.require_open (parser
);
23609 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23610 initializer
= make_node (CONSTRUCTOR
);
23611 /* If it's not a `}', then there is a non-trivial initializer. */
23612 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
23615 /* Parse the initializer list. */
23616 CONSTRUCTOR_ELTS (initializer
)
23617 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
23618 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
23619 /* A trailing `,' token is allowed. */
23620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23621 cp_lexer_consume_token (parser
->lexer
);
23624 *non_constant_p
= false;
23625 /* Now, there should be a trailing `}'. */
23626 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23627 braces
.require_close (parser
);
23628 TREE_TYPE (initializer
) = init_list_type_node
;
23630 cp_expr
result (initializer
);
23631 /* Build a location of the form:
23634 with caret==start at the open brace, finish at the close brace. */
23635 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
23636 result
.set_location (combined_loc
);
23640 /* Consume tokens up to, and including, the next non-nested closing `]'.
23641 Returns true iff we found a closing `]'. */
23644 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
23646 unsigned square_depth
= 0;
23650 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
23652 switch (token
->type
)
23654 case CPP_PRAGMA_EOL
:
23655 if (!parser
->lexer
->in_pragma
)
23659 /* If we've run out of tokens, then there is no closing `]'. */
23662 case CPP_OPEN_SQUARE
:
23666 case CPP_CLOSE_SQUARE
:
23667 if (!square_depth
--)
23669 cp_lexer_consume_token (parser
->lexer
);
23678 /* Consume the token. */
23679 cp_lexer_consume_token (parser
->lexer
);
23683 /* Return true if we are looking at an array-designator, false otherwise. */
23686 cp_parser_array_designator_p (cp_parser
*parser
)
23688 /* Consume the `['. */
23689 cp_lexer_consume_token (parser
->lexer
);
23691 cp_lexer_save_tokens (parser
->lexer
);
23693 /* Skip tokens until the next token is a closing square bracket.
23694 If we find the closing `]', and the next token is a `=', then
23695 we are looking at an array designator. */
23696 bool array_designator_p
23697 = (cp_parser_skip_to_closing_square_bracket (parser
)
23698 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
23700 /* Roll back the tokens we skipped. */
23701 cp_lexer_rollback_tokens (parser
->lexer
);
23703 return array_designator_p
;
23706 /* Parse an initializer-list.
23709 initializer-clause ... [opt]
23710 initializer-list , initializer-clause ... [opt]
23714 designated-initializer-list:
23715 designated-initializer-clause
23716 designated-initializer-list , designated-initializer-clause
23718 designated-initializer-clause:
23719 designator brace-or-equal-initializer
23727 designation initializer-clause ...[opt]
23728 initializer-list , designation initializer-clause ...[opt]
23733 [ constant-expression ] =
23735 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23736 for the initializer. If the INDEX of the elt is non-NULL, it is the
23737 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23738 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23739 are any designators. */
23741 static vec
<constructor_elt
, va_gc
> *
23742 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
23745 vec
<constructor_elt
, va_gc
> *v
= NULL
;
23746 bool first_p
= true;
23747 tree first_designator
= NULL_TREE
;
23749 /* Assume all of the expressions are constant. */
23750 *non_constant_p
= false;
23752 unsigned nelts
= 0;
23753 int suppress
= suppress_location_wrappers
;
23755 /* Parse the rest of the list. */
23761 bool clause_non_constant_p
;
23762 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23764 /* Handle the C++20 syntax, '. id ='. */
23765 if ((cxx_dialect
>= cxx20
23766 || cp_parser_allow_gnu_extensions_p (parser
))
23767 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
23768 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
23769 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
23770 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
23771 == CPP_OPEN_BRACE
)))
23773 if (cxx_dialect
< cxx20
)
23774 pedwarn (loc
, OPT_Wpedantic
,
23775 "C++ designated initializers only available with "
23776 "%<-std=c++20%> or %<-std=gnu++20%>");
23777 /* Consume the `.'. */
23778 cp_lexer_consume_token (parser
->lexer
);
23779 /* Consume the identifier. */
23780 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23782 /* Consume the `='. */
23783 cp_lexer_consume_token (parser
->lexer
);
23785 /* Also, if the next token is an identifier and the following one is a
23786 colon, we are looking at the GNU designated-initializer
23788 else if (cp_parser_allow_gnu_extensions_p (parser
)
23789 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
23790 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23793 /* Warn the user that they are using an extension. */
23794 pedwarn (loc
, OPT_Wpedantic
,
23795 "ISO C++ does not allow GNU designated initializers");
23796 /* Consume the identifier. */
23797 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23798 /* Consume the `:'. */
23799 cp_lexer_consume_token (parser
->lexer
);
23801 /* Also handle C99 array designators, '[ const ] ='. */
23802 else if (cp_parser_allow_gnu_extensions_p (parser
)
23803 && !c_dialect_objc ()
23804 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23806 /* In C++11, [ could start a lambda-introducer. */
23807 bool non_const
= false;
23809 cp_parser_parse_tentatively (parser
);
23811 if (!cp_parser_array_designator_p (parser
))
23813 cp_parser_simulate_error (parser
);
23814 designator
= NULL_TREE
;
23818 designator
= cp_parser_constant_expression (parser
, true,
23820 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23821 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23824 if (!cp_parser_parse_definitely (parser
))
23825 designator
= NULL_TREE
;
23827 && (!require_potential_rvalue_constant_expression
23829 designator
= NULL_TREE
;
23831 /* Warn the user that they are using an extension. */
23832 pedwarn (loc
, OPT_Wpedantic
,
23833 "ISO C++ does not allow C99 designated initializers");
23836 designator
= NULL_TREE
;
23840 first_designator
= designator
;
23843 else if (cxx_dialect
>= cxx20
23844 && first_designator
!= error_mark_node
23845 && (!first_designator
!= !designator
))
23847 error_at (loc
, "either all initializer clauses should be designated "
23848 "or none of them should be");
23849 first_designator
= error_mark_node
;
23851 else if (cxx_dialect
< cxx20
&& !first_designator
)
23852 first_designator
= designator
;
23854 /* Parse the initializer. */
23855 initializer
= cp_parser_initializer_clause (parser
,
23856 &clause_non_constant_p
);
23857 /* If any clause is non-constant, so is the entire initializer. */
23858 if (clause_non_constant_p
)
23859 *non_constant_p
= true;
23861 /* If we have an ellipsis, this is an initializer pack
23863 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23865 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23867 /* Consume the `...'. */
23868 cp_lexer_consume_token (parser
->lexer
);
23870 if (designator
&& cxx_dialect
>= cxx20
)
23872 "%<...%> not allowed in designated initializer list");
23874 /* Turn the initializer into an initializer expansion. */
23875 initializer
= make_pack_expansion (initializer
);
23878 /* Add it to the vector. */
23879 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23881 /* If the next token is not a comma, we have reached the end of
23883 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23886 /* Peek at the next token. */
23887 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23888 /* If the next token is a `}', then we're still done. An
23889 initializer-clause can have a trailing `,' after the
23890 initializer-list and before the closing `}'. */
23891 if (token
->type
== CPP_CLOSE_BRACE
)
23894 /* Suppress location wrappers in a long initializer to save memory
23895 (14179). The cutoff is chosen arbitrarily. */
23896 const unsigned loc_max
= 256;
23898 if (TREE_CODE (initializer
) == CONSTRUCTOR
)
23899 /* Look one level down because it's easy. Looking deeper would require
23900 passing down a nelts pointer, and I don't think multi-level massive
23901 initializers are common enough to justify this. */
23902 incr
= CONSTRUCTOR_NELTS (initializer
);
23904 if (nelts
>= loc_max
&& (nelts
- incr
) < loc_max
)
23905 ++suppress_location_wrappers
;
23907 /* Consume the `,' token. */
23908 cp_lexer_consume_token (parser
->lexer
);
23911 /* The same identifier shall not appear in multiple designators
23912 of a designated-initializer-list. */
23913 if (first_designator
)
23916 tree designator
, val
;
23917 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23918 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23920 if (IDENTIFIER_MARKED (designator
))
23922 error_at (cp_expr_loc_or_input_loc (val
),
23923 "%<.%s%> designator used multiple times in "
23924 "the same initializer list",
23925 IDENTIFIER_POINTER (designator
));
23926 (*v
)[i
].index
= error_mark_node
;
23929 IDENTIFIER_MARKED (designator
) = 1;
23931 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23932 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23933 IDENTIFIER_MARKED (designator
) = 0;
23936 suppress_location_wrappers
= suppress
;
23938 *designated
= first_designator
!= NULL_TREE
;
23942 /* Classes [gram.class] */
23944 /* Parse a class-name.
23950 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23951 to indicate that names looked up in dependent types should be
23952 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23953 keyword has been used to indicate that the name that appears next
23954 is a template. TAG_TYPE indicates the explicit tag given before
23955 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23956 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23957 is the class being defined in a class-head. If ENUM_OK is TRUE,
23958 enum-names are also accepted.
23960 Returns the TYPE_DECL representing the class. */
23963 cp_parser_class_name (cp_parser
*parser
,
23964 bool typename_keyword_p
,
23965 bool template_keyword_p
,
23966 enum tag_types tag_type
,
23967 bool check_dependency_p
,
23969 bool is_declaration
,
23973 tree identifier
= NULL_TREE
;
23975 /* All class-names start with an identifier. */
23976 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23977 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23979 cp_parser_error (parser
, "expected class-name");
23980 return error_mark_node
;
23983 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23984 to a template-id, so we save it here. Consider object scope too,
23985 so that make_typename_type below can use it (cp_parser_template_name
23986 considers object scope also). This may happen with code like
23988 p->template A<T>::a()
23990 where we first want to look up A<T>::a in the class of the object
23991 expression, as per [basic.lookup.classref]. */
23992 tree scope
= parser
->scope
? parser
->scope
: parser
->context
->object_type
;
23993 if (scope
== error_mark_node
)
23994 return error_mark_node
;
23996 /* Any name names a type if we're following the `typename' keyword
23997 in a qualified name where the enclosing scope is type-dependent. */
23998 const bool typename_p
= (typename_keyword_p
24000 && TYPE_P (parser
->scope
)
24001 && dependent_type_p (parser
->scope
));
24002 /* Handle the common case (an identifier, but not a template-id)
24004 if (token
->type
== CPP_NAME
24005 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
24007 cp_token
*identifier_token
;
24010 /* Look for the identifier. */
24011 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
24012 ambiguous_p
= identifier_token
->error_reported
;
24013 identifier
= cp_parser_identifier (parser
);
24014 /* If the next token isn't an identifier, we are certainly not
24015 looking at a class-name. */
24016 if (identifier
== error_mark_node
)
24017 decl
= error_mark_node
;
24018 /* If we know this is a type-name, there's no need to look it
24020 else if (typename_p
)
24024 tree ambiguous_decls
;
24025 /* If we already know that this lookup is ambiguous, then
24026 we've already issued an error message; there's no reason
24030 cp_parser_simulate_error (parser
);
24031 return error_mark_node
;
24033 /* If the next token is a `::', then the name must be a type
24036 [basic.lookup.qual]
24038 During the lookup for a name preceding the :: scope
24039 resolution operator, object, function, and enumerator
24040 names are ignored. */
24041 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
24042 tag_type
= scope_type
;
24043 /* Look up the name. */
24044 decl
= cp_parser_lookup_name (parser
, identifier
,
24046 /*is_template=*/false,
24047 /*is_namespace=*/false,
24048 check_dependency_p
,
24050 identifier_token
->location
);
24051 if (ambiguous_decls
)
24053 if (cp_parser_parsing_tentatively (parser
))
24054 cp_parser_simulate_error (parser
);
24055 return error_mark_node
;
24061 /* Try a template-id. */
24062 decl
= cp_parser_template_id (parser
, template_keyword_p
,
24063 check_dependency_p
,
24066 if (decl
== error_mark_node
)
24067 return error_mark_node
;
24070 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
24072 /* If this is a typename, create a TYPENAME_TYPE. */
24074 && decl
!= error_mark_node
24075 && !is_overloaded_fn (decl
))
24077 decl
= make_typename_type (scope
, decl
, typename_type
,
24078 /*complain=*/tf_error
);
24079 if (decl
!= error_mark_node
)
24080 decl
= TYPE_NAME (decl
);
24083 decl
= strip_using_decl (decl
);
24085 /* Check to see that it is really the name of a class. */
24086 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
24087 && identifier_p (TREE_OPERAND (decl
, 0))
24088 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
24089 /* Situations like this:
24091 template <typename T> struct A {
24092 typename T::template X<int>::I i;
24095 are problematic. Is `T::template X<int>' a class-name? The
24096 standard does not seem to be definitive, but there is no other
24097 valid interpretation of the following `::'. Therefore, those
24098 names are considered class-names. */
24100 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
24101 if (decl
!= error_mark_node
)
24102 decl
= TYPE_NAME (decl
);
24104 else if (TREE_CODE (decl
) != TYPE_DECL
24105 || TREE_TYPE (decl
) == error_mark_node
24106 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
24107 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
24108 /* In Objective-C 2.0, a classname followed by '.' starts a
24109 dot-syntax expression, and it's not a type-name. */
24110 || (c_dialect_objc ()
24111 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
24112 && objc_is_class_name (decl
)))
24113 decl
= error_mark_node
;
24115 if (decl
== error_mark_node
)
24116 cp_parser_error (parser
, "expected class-name");
24117 else if (identifier
&& !parser
->scope
)
24118 maybe_note_name_used_in_class (identifier
, decl
);
24123 /* Make sure that any member-function parameters are in scope.
24124 For instance, a function's noexcept-specifier can use the function's
24128 void fn (int p) noexcept(noexcept(p));
24131 so we need to make sure name lookup can find them. This is used
24132 when we delay parsing of the noexcept-specifier. */
24135 inject_parm_decls (tree decl
)
24137 begin_scope (sk_function_parms
, decl
);
24138 tree args
= DECL_ARGUMENTS (decl
);
24140 do_push_parm_decls (decl
, args
, /*nonparms=*/NULL
);
24142 if (args
&& is_this_parameter (args
))
24144 gcc_checking_assert (current_class_ptr
== NULL_TREE
);
24145 current_class_ptr
= NULL_TREE
;
24146 current_class_ref
= cp_build_fold_indirect_ref (args
);
24147 current_class_ptr
= args
;
24151 /* Undo the effects of inject_parm_decls. */
24154 pop_injected_parms (void)
24156 pop_bindings_and_leave_scope ();
24157 current_class_ptr
= current_class_ref
= NULL_TREE
;
24160 /* Parse a class-specifier.
24163 class-head { member-specification [opt] }
24165 Returns the TREE_TYPE representing the class. */
24168 cp_parser_class_specifier_1 (cp_parser
* parser
)
24171 tree attributes
= NULL_TREE
;
24172 bool nested_name_specifier_p
;
24173 unsigned saved_num_template_parameter_lists
;
24174 bool saved_in_function_body
;
24175 unsigned char in_statement
;
24176 bool in_switch_statement_p
;
24177 bool saved_in_unbraced_linkage_specification_p
;
24178 tree old_scope
= NULL_TREE
;
24179 tree scope
= NULL_TREE
;
24180 cp_token
*closing_brace
;
24182 push_deferring_access_checks (dk_no_deferred
);
24184 /* Parse the class-head. */
24185 type
= cp_parser_class_head (parser
,
24186 &nested_name_specifier_p
);
24187 /* If the class-head was a semantic disaster, skip the entire body
24191 cp_parser_skip_to_end_of_block_or_statement (parser
);
24192 pop_deferring_access_checks ();
24193 return error_mark_node
;
24196 /* Look for the `{'. */
24197 matching_braces braces
;
24198 if (!braces
.require_open (parser
))
24200 pop_deferring_access_checks ();
24201 return error_mark_node
;
24204 cp_ensure_no_omp_declare_simd (parser
);
24205 cp_ensure_no_oacc_routine (parser
);
24207 /* Issue an error message if type-definitions are forbidden here. */
24208 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
24209 /* Remember that we are defining one more class. */
24210 ++parser
->num_classes_being_defined
;
24211 /* Inside the class, surrounding template-parameter-lists do not
24213 saved_num_template_parameter_lists
24214 = parser
->num_template_parameter_lists
;
24215 parser
->num_template_parameter_lists
= 0;
24216 /* We are not in a function body. */
24217 saved_in_function_body
= parser
->in_function_body
;
24218 parser
->in_function_body
= false;
24219 /* Or in a loop. */
24220 in_statement
= parser
->in_statement
;
24221 parser
->in_statement
= 0;
24222 /* Or in a switch. */
24223 in_switch_statement_p
= parser
->in_switch_statement_p
;
24224 parser
->in_switch_statement_p
= false;
24225 /* We are not immediately inside an extern "lang" block. */
24226 saved_in_unbraced_linkage_specification_p
24227 = parser
->in_unbraced_linkage_specification_p
;
24228 parser
->in_unbraced_linkage_specification_p
= false;
24230 /* Start the class. */
24231 if (nested_name_specifier_p
)
24233 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
24234 /* SCOPE must be a scope nested inside current scope. */
24235 if (is_nested_namespace (current_namespace
,
24236 decl_namespace_context (scope
)))
24237 old_scope
= push_inner_scope (scope
);
24239 nested_name_specifier_p
= false;
24241 type
= begin_class_definition (type
);
24243 if (type
== error_mark_node
)
24244 /* If the type is erroneous, skip the entire body of the class. */
24245 cp_parser_skip_to_closing_brace (parser
);
24247 /* Parse the member-specification. */
24248 cp_parser_member_specification_opt (parser
);
24250 /* Look for the trailing `}'. */
24251 closing_brace
= braces
.require_close (parser
);
24252 /* Look for trailing attributes to apply to this class. */
24253 if (cp_parser_allow_gnu_extensions_p (parser
))
24254 attributes
= cp_parser_gnu_attributes_opt (parser
);
24255 if (type
!= error_mark_node
)
24256 type
= finish_struct (type
, attributes
);
24257 if (nested_name_specifier_p
)
24258 pop_inner_scope (old_scope
, scope
);
24260 /* We've finished a type definition. Check for the common syntax
24261 error of forgetting a semicolon after the definition. We need to
24262 be careful, as we can't just check for not-a-semicolon and be done
24263 with it; the user might have typed:
24265 class X { } c = ...;
24266 class X { } *p = ...;
24268 and so forth. Instead, enumerate all the possible tokens that
24269 might follow this production; if we don't see one of them, then
24270 complain and silently insert the semicolon. */
24272 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24273 bool want_semicolon
= true;
24275 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24276 /* Don't try to parse c++11 attributes here. As per the
24277 grammar, that should be a task for
24278 cp_parser_decl_specifier_seq. */
24279 want_semicolon
= false;
24281 switch (token
->type
)
24284 case CPP_SEMICOLON
:
24287 case CPP_OPEN_PAREN
:
24288 case CPP_CLOSE_PAREN
:
24290 want_semicolon
= false;
24293 /* While it's legal for type qualifiers and storage class
24294 specifiers to follow type definitions in the grammar, only
24295 compiler testsuites contain code like that. Assume that if
24296 we see such code, then what we're really seeing is a case
24300 const <type> var = ...;
24305 static <type> func (...) ...
24307 i.e. the qualifier or specifier applies to the next
24308 declaration. To do so, however, we need to look ahead one
24309 more token to see if *that* token is a type specifier.
24311 This code could be improved to handle:
24314 static const <type> var = ...; */
24316 if (keyword_is_decl_specifier (token
->keyword
))
24318 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24320 /* Handling user-defined types here would be nice, but very
24323 = (lookahead
->type
== CPP_KEYWORD
24324 && keyword_begins_type_specifier (lookahead
->keyword
));
24331 /* If we don't have a type, then something is very wrong and we
24332 shouldn't try to do anything clever. Likewise for not seeing the
24334 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
24336 /* Locate the closing brace. */
24337 cp_token_position prev
24338 = cp_lexer_previous_token_position (parser
->lexer
);
24339 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
24340 location_t loc
= prev_token
->location
;
24342 /* We want to suggest insertion of a ';' immediately *after* the
24343 closing brace, so, if we can, offset the location by 1 column. */
24344 location_t next_loc
= loc
;
24345 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
24346 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
24348 rich_location
richloc (line_table
, next_loc
);
24350 /* If we successfully offset the location, suggest the fix-it. */
24351 if (next_loc
!= loc
)
24352 richloc
.add_fixit_insert_before (next_loc
, ";");
24354 if (CLASSTYPE_DECLARED_CLASS (type
))
24355 error_at (&richloc
,
24356 "expected %<;%> after class definition");
24357 else if (TREE_CODE (type
) == RECORD_TYPE
)
24358 error_at (&richloc
,
24359 "expected %<;%> after struct definition");
24360 else if (TREE_CODE (type
) == UNION_TYPE
)
24361 error_at (&richloc
,
24362 "expected %<;%> after union definition");
24364 gcc_unreachable ();
24366 /* Unget one token and smash it to look as though we encountered
24367 a semicolon in the input stream. */
24368 cp_lexer_set_token_position (parser
->lexer
, prev
);
24369 token
= cp_lexer_peek_token (parser
->lexer
);
24370 token
->type
= CPP_SEMICOLON
;
24371 token
->keyword
= RID_MAX
;
24375 /* If this class is not itself within the scope of another class,
24376 then we need to parse the bodies of all of the queued function
24377 definitions. Note that the queued functions defined in a class
24378 are not always processed immediately following the
24379 class-specifier for that class. Consider:
24382 struct B { void f() { sizeof (A); } };
24385 If `f' were processed before the processing of `A' were
24386 completed, there would be no way to compute the size of `A'.
24387 Note that the nesting we are interested in here is lexical --
24388 not the semantic nesting given by TYPE_CONTEXT. In particular,
24391 struct A { struct B; };
24392 struct A::B { void f() { } };
24394 there is no need to delay the parsing of `A::B::f'. */
24395 if (--parser
->num_classes_being_defined
== 0)
24398 tree class_type
= NULL_TREE
;
24399 tree pushed_scope
= NULL_TREE
;
24401 cp_default_arg_entry
*e
;
24402 tree save_ccp
, save_ccr
;
24404 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
24406 /* Skip default arguments, NSDMIs, etc, in order to improve
24407 error recovery (c++/71169, c++/71832). */
24408 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
24409 vec_safe_truncate (unparsed_nsdmis
, 0);
24410 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
24413 /* In a first pass, parse default arguments to the functions.
24414 Then, in a second pass, parse the bodies of the functions.
24415 This two-phased approach handles cases like:
24423 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
24426 /* If there are default arguments that have not yet been processed,
24427 take care of them now. */
24428 if (class_type
!= e
->class_type
)
24431 pop_scope (pushed_scope
);
24432 class_type
= e
->class_type
;
24433 pushed_scope
= push_scope (class_type
);
24435 /* Make sure that any template parameters are in scope. */
24436 maybe_begin_member_template_processing (decl
);
24437 /* Parse the default argument expressions. */
24438 cp_parser_late_parsing_default_args (parser
, decl
);
24439 /* Remove any template parameters from the symbol table. */
24440 maybe_end_member_template_processing ();
24442 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
24443 /* Now parse any NSDMIs. */
24444 save_ccp
= current_class_ptr
;
24445 save_ccr
= current_class_ref
;
24446 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
24448 if (class_type
!= DECL_CONTEXT (decl
))
24451 pop_scope (pushed_scope
);
24452 class_type
= DECL_CONTEXT (decl
);
24453 pushed_scope
= push_scope (class_type
);
24455 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
24456 cp_parser_late_parsing_nsdmi (parser
, decl
);
24458 vec_safe_truncate (unparsed_nsdmis
, 0);
24459 current_class_ptr
= save_ccp
;
24460 current_class_ref
= save_ccr
;
24462 pop_scope (pushed_scope
);
24464 /* If there are noexcept-specifiers that have not yet been processed,
24465 take care of them now. */
24466 class_type
= NULL_TREE
;
24467 pushed_scope
= NULL_TREE
;
24468 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts
, ix
, decl
)
24470 tree ctx
= DECL_CONTEXT (decl
);
24471 if (class_type
!= ctx
)
24474 pop_scope (pushed_scope
);
24476 pushed_scope
= push_scope (class_type
);
24479 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
24480 spec
= TREE_PURPOSE (spec
);
24482 /* Make sure that any template parameters are in scope. */
24483 maybe_begin_member_template_processing (decl
);
24485 /* Make sure that any member-function parameters are in scope. */
24486 inject_parm_decls (decl
);
24488 /* 'this' is not allowed in static member functions. */
24489 unsigned char local_variables_forbidden_p
24490 = parser
->local_variables_forbidden_p
;
24491 if (DECL_THIS_STATIC (decl
))
24492 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
24494 /* Now we can parse the noexcept-specifier. */
24495 spec
= cp_parser_late_noexcept_specifier (parser
, spec
);
24497 if (spec
== error_mark_node
)
24500 /* Update the fn's type directly -- it might have escaped
24501 beyond this decl :( */
24502 fixup_deferred_exception_variants (TREE_TYPE (decl
), spec
);
24504 /* Restore the state of local_variables_forbidden_p. */
24505 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
24507 /* The finish_struct call above performed various override checking,
24508 but it skipped unparsed noexcept-specifier operands. Now that we
24509 have resolved them, check again. */
24510 noexcept_override_late_checks (type
, decl
);
24512 /* Remove any member-function parameters from the symbol table. */
24513 pop_injected_parms ();
24515 /* Remove any template parameters from the symbol table. */
24516 maybe_end_member_template_processing ();
24518 vec_safe_truncate (unparsed_noexcepts
, 0);
24520 pop_scope (pushed_scope
);
24522 /* Now parse the body of the functions. */
24525 /* OpenMP UDRs need to be parsed before all other functions. */
24526 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
24527 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
24528 cp_parser_late_parsing_for_member (parser
, decl
);
24529 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
24530 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
24531 cp_parser_late_parsing_for_member (parser
, decl
);
24534 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
24535 cp_parser_late_parsing_for_member (parser
, decl
);
24536 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
24539 /* Put back any saved access checks. */
24540 pop_deferring_access_checks ();
24542 /* Restore saved state. */
24543 parser
->in_switch_statement_p
= in_switch_statement_p
;
24544 parser
->in_statement
= in_statement
;
24545 parser
->in_function_body
= saved_in_function_body
;
24546 parser
->num_template_parameter_lists
24547 = saved_num_template_parameter_lists
;
24548 parser
->in_unbraced_linkage_specification_p
24549 = saved_in_unbraced_linkage_specification_p
;
24555 cp_parser_class_specifier (cp_parser
* parser
)
24558 timevar_push (TV_PARSE_STRUCT
);
24559 ret
= cp_parser_class_specifier_1 (parser
);
24560 timevar_pop (TV_PARSE_STRUCT
);
24564 /* Parse a class-head.
24567 class-key identifier [opt] base-clause [opt]
24568 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
24569 class-key nested-name-specifier [opt] template-id
24572 class-virt-specifier:
24576 class-key attributes identifier [opt] base-clause [opt]
24577 class-key attributes nested-name-specifier identifier base-clause [opt]
24578 class-key attributes nested-name-specifier [opt] template-id
24581 Upon return BASES is initialized to the list of base classes (or
24582 NULL, if there are none) in the same form returned by
24583 cp_parser_base_clause.
24585 Returns the TYPE of the indicated class. Sets
24586 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
24587 involving a nested-name-specifier was used, and FALSE otherwise.
24589 Returns error_mark_node if this is not a class-head.
24591 Returns NULL_TREE if the class-head is syntactically valid, but
24592 semantically invalid in a way that means we should skip the entire
24593 body of the class. */
24596 cp_parser_class_head (cp_parser
* parser
,
24597 bool* nested_name_specifier_p
)
24599 tree nested_name_specifier
;
24600 enum tag_types class_key
;
24601 tree id
= NULL_TREE
;
24602 tree type
= NULL_TREE
;
24605 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
24606 bool template_id_p
= false;
24607 bool qualified_p
= false;
24608 bool invalid_nested_name_p
= false;
24609 bool invalid_explicit_specialization_p
= false;
24610 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24611 tree pushed_scope
= NULL_TREE
;
24612 unsigned num_templates
;
24613 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
24614 /* Assume no nested-name-specifier will be present. */
24615 *nested_name_specifier_p
= false;
24616 /* Assume no template parameter lists will be used in defining the
24619 parser
->colon_corrects_to_scope_p
= false;
24621 /* Look for the class-key. */
24622 class_key
= cp_parser_class_key (parser
);
24623 if (class_key
== none_type
)
24624 return error_mark_node
;
24626 location_t class_head_start_location
= input_location
;
24628 /* Parse the attributes. */
24629 attributes
= cp_parser_attributes_opt (parser
);
24631 /* If the next token is `::', that is invalid -- but sometimes
24632 people do try to write:
24636 Handle this gracefully by accepting the extra qualifier, and then
24637 issuing an error about it later if this really is a
24638 class-head. If it turns out just to be an elaborated type
24639 specifier, remain silent. */
24640 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
24641 qualified_p
= true;
24643 push_deferring_access_checks (dk_no_check
);
24645 /* Determine the name of the class. Begin by looking for an
24646 optional nested-name-specifier. */
24647 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
24648 nested_name_specifier
24649 = cp_parser_nested_name_specifier_opt (parser
,
24650 /*typename_keyword_p=*/false,
24651 /*check_dependency_p=*/false,
24653 /*is_declaration=*/false);
24654 /* If there was a nested-name-specifier, then there *must* be an
24657 cp_token
*bad_template_keyword
= NULL
;
24659 if (nested_name_specifier
)
24661 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24662 /* Although the grammar says `identifier', it really means
24663 `class-name' or `template-name'. You are only allowed to
24664 define a class that has already been declared with this
24667 The proposed resolution for Core Issue 180 says that wherever
24668 you see `class T::X' you should treat `X' as a type-name.
24670 It is OK to define an inaccessible class; for example:
24672 class A { class B; };
24675 We do not know if we will see a class-name, or a
24676 template-name. We look for a class-name first, in case the
24677 class-name is a template-id; if we looked for the
24678 template-name first we would stop after the template-name. */
24679 cp_parser_parse_tentatively (parser
);
24680 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24681 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
24682 type
= cp_parser_class_name (parser
,
24683 /*typename_keyword_p=*/false,
24684 /*template_keyword_p=*/false,
24686 /*check_dependency_p=*/false,
24687 /*class_head_p=*/true,
24688 /*is_declaration=*/false);
24689 /* If that didn't work, ignore the nested-name-specifier. */
24690 if (!cp_parser_parse_definitely (parser
))
24692 invalid_nested_name_p
= true;
24693 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24694 id
= cp_parser_identifier (parser
);
24695 if (id
== error_mark_node
)
24698 /* If we could not find a corresponding TYPE, treat this
24699 declaration like an unqualified declaration. */
24700 if (type
== error_mark_node
)
24701 nested_name_specifier
= NULL_TREE
;
24702 /* Otherwise, count the number of templates used in TYPE and its
24703 containing scopes. */
24705 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
24707 /* Otherwise, the identifier is optional. */
24710 /* We don't know whether what comes next is a template-id,
24711 an identifier, or nothing at all. */
24712 cp_parser_parse_tentatively (parser
);
24713 /* Check for a template-id. */
24714 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24715 id
= cp_parser_template_id (parser
,
24716 /*template_keyword_p=*/false,
24717 /*check_dependency_p=*/true,
24719 /*is_declaration=*/true);
24720 /* If that didn't work, it could still be an identifier. */
24721 if (!cp_parser_parse_definitely (parser
))
24723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
24725 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24726 id
= cp_parser_identifier (parser
);
24733 template_id_p
= true;
24738 pop_deferring_access_checks ();
24742 cp_parser_check_for_invalid_template_id (parser
, id
,
24744 type_start_token
->location
);
24746 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
24748 /* If it's not a `:' or a `{' then we can't really be looking at a
24749 class-head, since a class-head only appears as part of a
24750 class-specifier. We have to detect this situation before calling
24751 xref_tag, since that has irreversible side-effects. */
24752 if (!cp_parser_next_token_starts_class_definition_p (parser
))
24754 cp_parser_error (parser
, "expected %<{%> or %<:%>");
24755 type
= error_mark_node
;
24759 /* At this point, we're going ahead with the class-specifier, even
24760 if some other problem occurs. */
24761 cp_parser_commit_to_tentative_parse (parser
);
24762 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
24764 cp_parser_error (parser
,
24765 "cannot specify %<override%> for a class");
24766 type
= error_mark_node
;
24769 /* Issue the error about the overly-qualified name now. */
24772 cp_parser_error (parser
,
24773 "global qualification of class name is invalid");
24774 type
= error_mark_node
;
24777 else if (invalid_nested_name_p
)
24779 cp_parser_error (parser
,
24780 "qualified name does not name a class");
24781 type
= error_mark_node
;
24784 else if (nested_name_specifier
)
24788 if (bad_template_keyword
)
24789 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24790 keyword template shall not appear at the top level. */
24791 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
24792 "keyword %<template%> not allowed in class-head-name");
24794 /* Reject typedef-names in class heads. */
24795 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
24797 error_at (type_start_token
->location
,
24798 "invalid class name in declaration of %qD",
24804 /* Figure out in what scope the declaration is being placed. */
24805 scope
= current_scope ();
24806 /* If that scope does not contain the scope in which the
24807 class was originally declared, the program is invalid. */
24808 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
24810 if (at_namespace_scope_p ())
24811 error_at (type_start_token
->location
,
24812 "declaration of %qD in namespace %qD which does not "
24814 type
, scope
, nested_name_specifier
);
24816 error_at (type_start_token
->location
,
24817 "declaration of %qD in %qD which does not enclose %qD",
24818 type
, scope
, nested_name_specifier
);
24824 A declarator-id shall not be qualified except for the
24825 definition of a ... nested class outside of its class
24826 ... [or] the definition or explicit instantiation of a
24827 class member of a namespace outside of its namespace. */
24828 if (scope
== nested_name_specifier
)
24829 permerror (nested_name_specifier_token_start
->location
,
24830 "extra qualification not allowed");
24832 /* An explicit-specialization must be preceded by "template <>". If
24833 it is not, try to recover gracefully. */
24834 if (at_namespace_scope_p ()
24835 && parser
->num_template_parameter_lists
== 0
24836 && !processing_template_parmlist
24839 /* Build a location of this form:
24840 struct typename <ARGS>
24841 ^~~~~~~~~~~~~~~~~~~~~~
24842 with caret==start at the start token, and
24843 finishing at the end of the type. */
24844 location_t reported_loc
24845 = make_location (class_head_start_location
,
24846 class_head_start_location
,
24847 get_finish (type_start_token
->location
));
24848 rich_location
richloc (line_table
, reported_loc
);
24849 richloc
.add_fixit_insert_before (class_head_start_location
,
24851 error_at (&richloc
,
24852 "an explicit specialization must be preceded by"
24853 " %<template <>%>");
24854 invalid_explicit_specialization_p
= true;
24855 /* Take the same action that would have been taken by
24856 cp_parser_explicit_specialization. */
24857 ++parser
->num_template_parameter_lists
;
24858 begin_specialization ();
24860 /* There must be no "return" statements between this point and the
24861 end of this function; set "type "to the correct return value and
24862 use "goto done;" to return. */
24863 /* Make sure that the right number of template parameters were
24865 if (!cp_parser_check_template_parameters (parser
, num_templates
,
24867 type_start_token
->location
,
24868 /*declarator=*/NULL
))
24870 /* If something went wrong, there is no point in even trying to
24871 process the class-definition. */
24876 /* Look up the type. */
24879 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
24880 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
24881 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
24883 error_at (type_start_token
->location
,
24884 "function template %qD redeclared as a class template", id
);
24885 type
= error_mark_node
;
24889 type
= TREE_TYPE (id
);
24890 type
= maybe_process_partial_specialization (type
);
24892 /* Check the scope while we still know whether or not we had a
24893 nested-name-specifier. */
24894 if (type
!= error_mark_node
)
24895 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
24897 if (nested_name_specifier
)
24898 pushed_scope
= push_scope (nested_name_specifier
);
24900 else if (nested_name_specifier
)
24906 template <typename T> struct S { struct T };
24907 template <typename T> struct S<T>::T { };
24909 we will get a TYPENAME_TYPE when processing the definition of
24910 `S::T'. We need to resolve it to the actual type before we
24911 try to define it. */
24912 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
24914 class_type
= resolve_typename_type (TREE_TYPE (type
),
24915 /*only_current_p=*/false);
24916 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
24917 type
= TYPE_NAME (class_type
);
24920 cp_parser_error (parser
, "could not resolve typename type");
24921 type
= error_mark_node
;
24925 if (maybe_process_partial_specialization (TREE_TYPE (type
))
24926 == error_mark_node
)
24932 class_type
= current_class_type
;
24933 /* Enter the scope indicated by the nested-name-specifier. */
24934 pushed_scope
= push_scope (nested_name_specifier
);
24935 /* Get the canonical version of this type. */
24936 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
24937 /* Call push_template_decl if it seems like we should be defining a
24938 template either from the template headers or the type we're
24939 defining, so that we diagnose both extra and missing headers. */
24940 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24941 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
24942 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
24944 type
= push_template_decl (type
);
24945 if (type
== error_mark_node
)
24952 type
= TREE_TYPE (type
);
24953 *nested_name_specifier_p
= true;
24955 else /* The name is not a nested name. */
24957 /* If the class was unnamed, create a dummy name. */
24959 id
= make_anon_name ();
24960 TAG_how how
= (parser
->in_type_id_in_expr_p
24961 ? TAG_how::INNERMOST_NON_CLASS
24962 : TAG_how::CURRENT_ONLY
);
24963 type
= xref_tag (class_key
, id
, how
,
24964 parser
->num_template_parameter_lists
);
24967 /* Diagnose class/struct/union mismatches. */
24968 cp_parser_check_class_key (parser
, UNKNOWN_LOCATION
, class_key
, type
,
24971 /* Indicate whether this class was declared as a `class' or as a
24973 if (TREE_CODE (type
) == RECORD_TYPE
)
24974 CLASSTYPE_DECLARED_CLASS (type
) = class_key
== class_type
;
24976 /* If this type was already complete, and we see another definition,
24977 that's an error. Likewise if the type is already being defined:
24978 this can happen, eg, when it's defined from within an expression
24980 if (type
!= error_mark_node
24981 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
24983 error_at (type_start_token
->location
, "redefinition of %q#T",
24985 inform (location_of (type
), "previous definition of %q#T",
24990 else if (type
== error_mark_node
)
24995 /* Apply attributes now, before any use of the class as a template
24996 argument in its base list. */
24997 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24998 fixup_attribute_variants (type
);
25001 /* Associate constraints with the type. */
25003 type
= associate_classtype_constraints (type
);
25005 /* We will have entered the scope containing the class; the names of
25006 base classes should be looked up in that context. For example:
25008 struct A { struct B {}; struct C; };
25009 struct A::C : B {};
25013 /* Get the list of base-classes, if there is one. */
25014 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
25016 /* PR59482: enter the class scope so that base-specifiers are looked
25020 bases
= cp_parser_base_clause (parser
);
25021 /* PR59482: get out of the previously pushed class scope so that the
25022 subsequent pops pop the right thing. */
25029 /* If we're really defining a class, process the base classes.
25030 If they're invalid, fail. */
25031 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25032 xref_basetypes (type
, bases
);
25035 /* Leave the scope given by the nested-name-specifier. We will
25036 enter the class scope itself while processing the members. */
25038 pop_scope (pushed_scope
);
25040 if (invalid_explicit_specialization_p
)
25042 end_specialization ();
25043 --parser
->num_template_parameter_lists
;
25047 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
25048 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
25049 CLASSTYPE_FINAL (type
) = 1;
25051 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
25055 /* Parse a class-key.
25062 Returns the kind of class-key specified, or none_type to indicate
25065 static enum tag_types
25066 cp_parser_class_key (cp_parser
* parser
)
25069 enum tag_types tag_type
;
25071 /* Look for the class-key. */
25072 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
25076 /* Check to see if the TOKEN is a class-key. */
25077 tag_type
= cp_parser_token_is_class_key (token
);
25079 cp_parser_error (parser
, "expected class-key");
25083 /* Parse a type-parameter-key.
25085 type-parameter-key:
25091 cp_parser_type_parameter_key (cp_parser
* parser
)
25093 /* Look for the type-parameter-key. */
25094 enum tag_types tag_type
= none_type
;
25095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25096 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
25098 cp_lexer_consume_token (parser
->lexer
);
25099 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
25100 /* typename is not allowed in a template template parameter
25101 by the standard until C++17. */
25102 pedwarn (token
->location
, OPT_Wpedantic
,
25103 "ISO C++ forbids typename key in template template parameter;"
25104 " use %<-std=c++17%> or %<-std=gnu++17%>");
25107 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
25112 /* Parse an (optional) member-specification.
25114 member-specification:
25115 member-declaration member-specification [opt]
25116 access-specifier : member-specification [opt] */
25119 cp_parser_member_specification_opt (cp_parser
* parser
)
25126 /* Peek at the next token. */
25127 token
= cp_lexer_peek_token (parser
->lexer
);
25128 /* If it's a `}', or EOF then we've seen all the members. */
25129 if (token
->type
== CPP_CLOSE_BRACE
25130 || token
->type
== CPP_EOF
25131 || token
->type
== CPP_PRAGMA_EOL
)
25134 /* See if this token is a keyword. */
25135 keyword
= token
->keyword
;
25139 case RID_PROTECTED
:
25141 /* Consume the access-specifier. */
25142 cp_lexer_consume_token (parser
->lexer
);
25143 /* Remember which access-specifier is active. */
25144 current_access_specifier
= token
->u
.value
;
25145 /* Look for the `:'. */
25146 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25150 /* Accept #pragmas at class scope. */
25151 if (token
->type
== CPP_PRAGMA
)
25153 cp_parser_pragma (parser
, pragma_member
, NULL
);
25157 /* Otherwise, the next construction must be a
25158 member-declaration. */
25159 cp_parser_member_declaration (parser
);
25164 /* Parse a member-declaration.
25166 member-declaration:
25167 decl-specifier-seq [opt] member-declarator-list [opt] ;
25168 function-definition ; [opt]
25169 :: [opt] nested-name-specifier template [opt] unqualified-id ;
25171 template-declaration
25174 member-declarator-list:
25176 member-declarator-list , member-declarator
25179 declarator pure-specifier [opt]
25180 declarator constant-initializer [opt]
25181 identifier [opt] : constant-expression
25185 member-declaration:
25186 __extension__ member-declaration
25189 declarator attributes [opt] pure-specifier [opt]
25190 declarator attributes [opt] constant-initializer [opt]
25191 identifier [opt] attributes [opt] : constant-expression
25195 member-declaration:
25196 static_assert-declaration */
25199 cp_parser_member_declaration (cp_parser
* parser
)
25201 cp_decl_specifier_seq decl_specifiers
;
25202 tree prefix_attributes
;
25204 int declares_class_or_enum
;
25206 cp_token
*token
= NULL
;
25207 cp_token
*decl_spec_token_start
= NULL
;
25208 cp_token
*initializer_token_start
= NULL
;
25209 int saved_pedantic
;
25210 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
25212 /* Check for the `__extension__' keyword. */
25213 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
25216 cp_parser_member_declaration (parser
);
25217 /* Restore the old value of the PEDANTIC flag. */
25218 pedantic
= saved_pedantic
;
25223 /* Check for a template-declaration. */
25224 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25226 /* An explicit specialization here is an error condition, and we
25227 expect the specialization handler to detect and report this. */
25228 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
25229 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
25230 cp_parser_explicit_specialization (parser
);
25232 cp_parser_template_declaration (parser
, /*member_p=*/true);
25236 /* Check for a template introduction. */
25237 else if (cp_parser_template_declaration_after_export (parser
, true))
25240 /* Check for a using-declaration. */
25241 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25243 if (cxx_dialect
< cxx11
)
25245 /* Parse the using-declaration. */
25246 cp_parser_using_declaration (parser
,
25247 /*access_declaration_p=*/false);
25253 bool alias_decl_expected
;
25254 cp_parser_parse_tentatively (parser
);
25255 decl
= cp_parser_alias_declaration (parser
);
25256 /* Note that if we actually see the '=' token after the
25257 identifier, cp_parser_alias_declaration commits the
25258 tentative parse. In that case, we really expect an
25259 alias-declaration. Otherwise, we expect a using
25261 alias_decl_expected
=
25262 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
25263 cp_parser_parse_definitely (parser
);
25265 if (alias_decl_expected
)
25266 finish_member_declaration (decl
);
25268 cp_parser_using_declaration (parser
,
25269 /*access_declaration_p=*/false);
25274 /* Check for @defs. */
25275 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
25278 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
25279 ivar
= ivar_chains
;
25283 ivar
= TREE_CHAIN (member
);
25284 TREE_CHAIN (member
) = NULL_TREE
;
25285 finish_member_declaration (member
);
25290 /* If the next token is `static_assert' we have a static assertion. */
25291 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
25293 cp_parser_static_assert (parser
, /*member_p=*/true);
25297 parser
->colon_corrects_to_scope_p
= false;
25299 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
25302 /* Parse the decl-specifier-seq. */
25303 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
25304 cp_parser_decl_specifier_seq (parser
,
25305 (CP_PARSER_FLAGS_OPTIONAL
25306 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
25308 &declares_class_or_enum
);
25309 /* Check for an invalid type-name. */
25310 if (!decl_specifiers
.any_type_specifiers_p
25311 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
25313 /* If there is no declarator, then the decl-specifier-seq should
25315 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25317 /* If there was no decl-specifier-seq, and the next token is a
25318 `;', then we have something like:
25324 Each member-declaration shall declare at least one member
25325 name of the class. */
25326 if (!decl_specifiers
.any_specifiers_p
)
25328 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25329 if (!in_system_header_at (token
->location
))
25331 gcc_rich_location
richloc (token
->location
);
25332 richloc
.add_fixit_remove ();
25333 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
25338 /* See if this declaration is a friend. */
25339 friend_p
= cp_parser_friend_p (&decl_specifiers
);
25340 /* If there were decl-specifiers, check to see if there was
25341 a class-declaration. */
25342 tree type
= check_tag_decl (&decl_specifiers
,
25343 /*explicit_type_instantiation_p=*/false);
25344 /* Nested classes have already been added to the class, but
25345 a `friend' needs to be explicitly registered. */
25348 /* If the `friend' keyword was present, the friend must
25349 be introduced with a class-key. */
25350 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
25351 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
25352 "in C++03 a class-key must be used "
25353 "when declaring a friend");
25356 template <typename T> struct A {
25357 friend struct A<T>::B;
25360 A<T>::B will be represented by a TYPENAME_TYPE, and
25361 therefore not recognized by check_tag_decl. */
25364 type
= decl_specifiers
.type
;
25365 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
25366 type
= TREE_TYPE (type
);
25368 if (!type
|| !TYPE_P (type
))
25369 error_at (decl_spec_token_start
->location
,
25370 "friend declaration does not name a class or "
25373 make_friend_class (current_class_type
, type
,
25374 /*complain=*/true);
25376 /* If there is no TYPE, an error message will already have
25378 else if (!type
|| type
== error_mark_node
)
25380 /* An anonymous aggregate has to be handled specially; such
25381 a declaration really declares a data member (with a
25382 particular type), as opposed to a nested class. */
25383 else if (ANON_AGGR_TYPE_P (type
))
25386 if (decl_specifiers
.storage_class
!= sc_none
)
25387 error_at (decl_spec_token_start
->location
,
25388 "a storage class on an anonymous aggregate "
25389 "in class scope is not allowed");
25391 /* Remove constructors and such from TYPE, now that we
25392 know it is an anonymous aggregate. */
25393 fixup_anonymous_aggr (type
);
25394 /* And make the corresponding data member. */
25395 decl
= build_decl (decl_spec_token_start
->location
,
25396 FIELD_DECL
, NULL_TREE
, type
);
25397 /* Add it to the class. */
25398 finish_member_declaration (decl
);
25401 cp_parser_check_access_in_redeclaration
25403 decl_spec_token_start
->location
);
25408 bool assume_semicolon
= false;
25410 /* Clear attributes from the decl_specifiers but keep them
25411 around as prefix attributes that apply them to the entity
25413 prefix_attributes
= decl_specifiers
.attributes
;
25414 decl_specifiers
.attributes
= NULL_TREE
;
25416 /* See if these declarations will be friends. */
25417 friend_p
= cp_parser_friend_p (&decl_specifiers
);
25419 /* Keep going until we hit the `;' at the end of the
25421 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25423 tree attributes
= NULL_TREE
;
25424 tree first_attribute
;
25426 bool named_bitfld
= false;
25428 /* Peek at the next token. */
25429 token
= cp_lexer_peek_token (parser
->lexer
);
25431 /* The following code wants to know early if it is a bit-field
25432 or some other declaration. Attributes can appear before
25433 the `:' token. Skip over them without consuming any tokens
25434 to peek if they are followed by `:'. */
25435 if (cp_next_tokens_can_be_attribute_p (parser
)
25436 || (token
->type
== CPP_NAME
25437 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
25438 && (named_bitfld
= true)))
25441 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
25442 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25445 /* Check for a bitfield declaration. */
25446 if (token
->type
== CPP_COLON
25447 || (token
->type
== CPP_NAME
25448 && token
== cp_lexer_peek_token (parser
->lexer
)
25449 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
25450 && (named_bitfld
= true)))
25454 tree late_attributes
= NULL_TREE
;
25455 location_t id_location
25456 = cp_lexer_peek_token (parser
->lexer
)->location
;
25459 identifier
= cp_parser_identifier (parser
);
25461 identifier
= NULL_TREE
;
25463 /* Look for attributes that apply to the bitfield. */
25464 attributes
= cp_parser_attributes_opt (parser
);
25466 /* Consume the `:' token. */
25467 cp_lexer_consume_token (parser
->lexer
);
25469 /* Get the width of the bitfield. */
25470 width
= cp_parser_constant_expression (parser
, false, NULL
,
25471 cxx_dialect
>= cxx11
);
25473 /* In C++20 and as extension for C++11 and above we allow
25474 default member initializers for bit-fields. */
25475 initializer
= NULL_TREE
;
25476 if (cxx_dialect
>= cxx11
25477 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
25478 || cp_lexer_next_token_is (parser
->lexer
,
25482 = cp_lexer_peek_token (parser
->lexer
)->location
;
25483 if (cxx_dialect
< cxx20
25484 && identifier
!= NULL_TREE
)
25486 "default member initializers for bit-fields "
25487 "only available with %<-std=c++20%> or "
25488 "%<-std=gnu++20%>");
25490 initializer
= cp_parser_save_nsdmi (parser
);
25491 if (identifier
== NULL_TREE
)
25493 error_at (loc
, "default member initializer for "
25494 "unnamed bit-field");
25495 initializer
= NULL_TREE
;
25500 /* Look for attributes that apply to the bitfield after
25501 the `:' token and width. This is where GCC used to
25502 parse attributes in the past, pedwarn if there is
25503 a std attribute. */
25504 if (cp_next_tokens_can_be_std_attribute_p (parser
))
25505 pedwarn (input_location
, OPT_Wpedantic
,
25506 "ISO C++ allows bit-field attributes only "
25507 "before the %<:%> token");
25509 late_attributes
= cp_parser_attributes_opt (parser
);
25512 attributes
= attr_chainon (attributes
, late_attributes
);
25514 /* Remember which attributes are prefix attributes and
25516 first_attribute
= attributes
;
25517 /* Combine the attributes. */
25518 attributes
= attr_chainon (prefix_attributes
, attributes
);
25520 /* Create the bitfield declaration. */
25521 decl
= grokbitfield (identifier
25522 ? make_id_declarator (NULL_TREE
,
25528 width
, initializer
,
25533 cp_declarator
*declarator
;
25534 tree asm_specification
;
25535 int ctor_dtor_or_conv_p
;
25536 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
25537 cp_parser_flags flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
25538 /* We can't delay parsing for friends,
25539 alias-declarations, and typedefs, even though the
25540 standard seems to require it. */
25542 && !decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
25543 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
25545 /* Parse the declarator. */
25547 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25549 &ctor_dtor_or_conv_p
,
25550 /*parenthesized_p=*/NULL
,
25552 friend_p
, static_p
);
25554 /* If something went wrong parsing the declarator, make sure
25555 that we at least consume some tokens. */
25556 if (declarator
== cp_error_declarator
)
25558 /* Skip to the end of the statement. */
25559 cp_parser_skip_to_end_of_statement (parser
);
25560 /* If the next token is not a semicolon, that is
25561 probably because we just skipped over the body of
25562 a function. So, we consume a semicolon if
25563 present, but do not issue an error message if it
25565 if (cp_lexer_next_token_is (parser
->lexer
,
25567 cp_lexer_consume_token (parser
->lexer
);
25571 if (declares_class_or_enum
& 2)
25572 cp_parser_check_for_definition_in_return_type
25573 (declarator
, decl_specifiers
.type
,
25574 decl_specifiers
.locations
[ds_type_spec
]);
25576 /* Look for an asm-specification. */
25577 asm_specification
= cp_parser_asm_specification_opt (parser
);
25578 /* Look for attributes that apply to the declaration. */
25579 attributes
= cp_parser_attributes_opt (parser
);
25580 /* Remember which attributes are prefix attributes and
25582 first_attribute
= attributes
;
25583 /* Combine the attributes. */
25584 attributes
= attr_chainon (prefix_attributes
, attributes
);
25586 /* If it's an `=', then we have a constant-initializer or a
25587 pure-specifier. It is not correct to parse the
25588 initializer before registering the member declaration
25589 since the member declaration should be in scope while
25590 its initializer is processed. However, the rest of the
25591 front end does not yet provide an interface that allows
25592 us to handle this correctly. */
25593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25597 A pure-specifier shall be used only in the declaration of
25598 a virtual function.
25600 A member-declarator can contain a constant-initializer
25601 only if it declares a static member of integral or
25604 Therefore, if the DECLARATOR is for a function, we look
25605 for a pure-specifier; otherwise, we look for a
25606 constant-initializer. When we call `grokfield', it will
25607 perform more stringent semantics checks. */
25608 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
25609 if (function_declarator_p (declarator
)
25610 || (decl_specifiers
.type
25611 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
25612 && declarator
->kind
== cdk_id
25613 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
25614 == FUNCTION_TYPE
)))
25615 initializer
= cp_parser_pure_specifier (parser
);
25616 else if (decl_specifiers
.storage_class
!= sc_static
)
25617 initializer
= cp_parser_save_nsdmi (parser
);
25618 else if (cxx_dialect
>= cxx11
)
25621 /* Don't require a constant rvalue in C++11, since we
25622 might want a reference constant. We'll enforce
25623 constancy later. */
25624 cp_lexer_consume_token (parser
->lexer
);
25625 /* Parse the initializer. */
25626 initializer
= cp_parser_initializer_clause (parser
,
25630 /* Parse the initializer. */
25631 initializer
= cp_parser_constant_initializer (parser
);
25633 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
25634 && !function_declarator_p (declarator
))
25637 if (decl_specifiers
.storage_class
!= sc_static
)
25638 initializer
= cp_parser_save_nsdmi (parser
);
25640 initializer
= cp_parser_initializer (parser
, &x
, &x
);
25642 /* Detect invalid bit-field cases such as
25648 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
25649 /* If there were no type specifiers, it was a
25651 && decl_specifiers
.any_type_specifiers_p
)
25653 /* This is called for a decent diagnostic only. */
25654 tree d
= grokdeclarator (declarator
, &decl_specifiers
,
25655 BITFIELD
, /*initialized=*/false,
25657 if (!error_operand_p (d
))
25658 error_at (DECL_SOURCE_LOCATION (d
),
25659 "bit-field %qD has non-integral type %qT",
25661 cp_parser_skip_to_end_of_statement (parser
);
25662 /* Avoid "extra ;" pedwarns. */
25663 if (cp_lexer_next_token_is (parser
->lexer
,
25665 cp_lexer_consume_token (parser
->lexer
);
25668 /* Otherwise, there is no initializer. */
25670 initializer
= NULL_TREE
;
25672 /* See if we are probably looking at a function
25673 definition. We are certainly not looking at a
25674 member-declarator. Calling `grokfield' has
25675 side-effects, so we must not do it unless we are sure
25676 that we are looking at a member-declarator. */
25677 if (cp_parser_token_starts_function_definition_p
25678 (cp_lexer_peek_token (parser
->lexer
)))
25680 /* The grammar does not allow a pure-specifier to be
25681 used when a member function is defined. (It is
25682 possible that this fact is an oversight in the
25683 standard, since a pure function may be defined
25684 outside of the class-specifier. */
25685 if (initializer
&& initializer_token_start
)
25686 error_at (initializer_token_start
->location
,
25687 "pure-specifier on function-definition");
25688 decl
= cp_parser_save_member_function_body (parser
,
25692 if (parser
->fully_implicit_function_template_p
)
25693 decl
= finish_fully_implicit_template (parser
, decl
);
25694 /* If the member was not a friend, declare it here. */
25696 finish_member_declaration (decl
);
25697 /* Peek at the next token. */
25698 token
= cp_lexer_peek_token (parser
->lexer
);
25699 /* If the next token is a semicolon, consume it. */
25700 if (token
->type
== CPP_SEMICOLON
)
25702 location_t semicolon_loc
25703 = cp_lexer_consume_token (parser
->lexer
)->location
;
25704 gcc_rich_location
richloc (semicolon_loc
);
25705 richloc
.add_fixit_remove ();
25706 warning_at (&richloc
, OPT_Wextra_semi
,
25707 "extra %<;%> after in-class "
25708 "function definition");
25713 if (declarator
->kind
== cdk_function
)
25714 declarator
->id_loc
= token
->location
;
25715 /* Create the declaration. */
25716 decl
= grokfield (declarator
, &decl_specifiers
,
25717 initializer
, /*init_const_expr_p=*/true,
25718 asm_specification
, attributes
);
25719 if (parser
->fully_implicit_function_template_p
)
25722 finish_fully_implicit_template (parser
, 0);
25724 decl
= finish_fully_implicit_template (parser
, decl
);
25728 cp_finalize_omp_declare_simd (parser
, decl
);
25729 cp_finalize_oacc_routine (parser
, decl
, false);
25731 /* Reset PREFIX_ATTRIBUTES. */
25732 if (attributes
!= error_mark_node
)
25734 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25735 attributes
= TREE_CHAIN (attributes
);
25737 TREE_CHAIN (attributes
) = NULL_TREE
;
25740 /* If there is any qualification still in effect, clear it
25741 now; we will be starting fresh with the next declarator. */
25742 parser
->scope
= NULL_TREE
;
25743 parser
->qualifying_scope
= NULL_TREE
;
25744 parser
->object_scope
= NULL_TREE
;
25745 /* If it's a `,', then there are more declarators. */
25746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25748 cp_lexer_consume_token (parser
->lexer
);
25749 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25751 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
25752 gcc_rich_location
richloc (token
->location
);
25753 richloc
.add_fixit_remove ();
25754 error_at (&richloc
, "stray %<,%> at end of "
25755 "member declaration");
25758 /* If the next token isn't a `;', then we have a parse error. */
25759 else if (cp_lexer_next_token_is_not (parser
->lexer
,
25762 /* The next token might be a ways away from where the
25763 actual semicolon is missing. Find the previous token
25764 and use that for our error position. */
25765 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
25766 gcc_rich_location
richloc (token
->location
);
25767 richloc
.add_fixit_insert_after (";");
25768 error_at (&richloc
, "expected %<;%> at end of "
25769 "member declaration");
25771 /* Assume that the user meant to provide a semicolon. If
25772 we were to cp_parser_skip_to_end_of_statement, we might
25773 skip to a semicolon inside a member function definition
25774 and issue nonsensical error messages. */
25775 assume_semicolon
= true;
25780 /* Add DECL to the list of members. */
25782 /* Explicitly include, eg, NSDMIs, for better error
25783 recovery (c++/58650). */
25784 || !DECL_DECLARES_FUNCTION_P (decl
))
25785 finish_member_declaration (decl
);
25787 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25788 cp_parser_save_default_args (parser
, decl
);
25789 else if (TREE_CODE (decl
) == FIELD_DECL
25790 && DECL_INITIAL (decl
))
25791 /* Add DECL to the queue of NSDMI to be parsed later. */
25792 vec_safe_push (unparsed_nsdmis
, decl
);
25795 if (assume_semicolon
)
25800 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25802 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
25805 /* Parse a pure-specifier.
25810 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25811 Otherwise, ERROR_MARK_NODE is returned. */
25814 cp_parser_pure_specifier (cp_parser
* parser
)
25818 /* Look for the `=' token. */
25819 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25820 return error_mark_node
;
25821 /* Look for the `0' token. */
25822 token
= cp_lexer_peek_token (parser
->lexer
);
25824 if (token
->type
== CPP_EOF
25825 || token
->type
== CPP_PRAGMA_EOL
)
25826 return error_mark_node
;
25828 cp_lexer_consume_token (parser
->lexer
);
25830 /* Accept = default or = delete in c++0x mode. */
25831 if (token
->keyword
== RID_DEFAULT
25832 || token
->keyword
== RID_DELETE
)
25834 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
25835 return token
->u
.value
;
25838 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25839 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
25841 cp_parser_error (parser
,
25842 "invalid pure specifier (only %<= 0%> is allowed)");
25843 cp_parser_skip_to_end_of_statement (parser
);
25844 return error_mark_node
;
25846 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25848 error_at (token
->location
, "templates may not be %<virtual%>");
25849 return error_mark_node
;
25852 return integer_zero_node
;
25855 /* Parse a constant-initializer.
25857 constant-initializer:
25858 = constant-expression
25860 Returns a representation of the constant-expression. */
25863 cp_parser_constant_initializer (cp_parser
* parser
)
25865 /* Look for the `=' token. */
25866 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25867 return error_mark_node
;
25869 /* It is invalid to write:
25871 struct S { static const int i = { 7 }; };
25874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25876 cp_parser_error (parser
,
25877 "a brace-enclosed initializer is not allowed here");
25878 /* Consume the opening brace. */
25879 matching_braces braces
;
25880 braces
.consume_open (parser
);
25881 /* Skip the initializer. */
25882 cp_parser_skip_to_closing_brace (parser
);
25883 /* Look for the trailing `}'. */
25884 braces
.require_close (parser
);
25886 return error_mark_node
;
25889 return cp_parser_constant_expression (parser
);
25892 /* Derived classes [gram.class.derived] */
25894 /* Parse a base-clause.
25897 : base-specifier-list
25899 base-specifier-list:
25900 base-specifier ... [opt]
25901 base-specifier-list , base-specifier ... [opt]
25903 Returns a TREE_LIST representing the base-classes, in the order in
25904 which they were declared. The representation of each node is as
25905 described by cp_parser_base_specifier.
25907 In the case that no bases are specified, this function will return
25908 NULL_TREE, not ERROR_MARK_NODE. */
25911 cp_parser_base_clause (cp_parser
* parser
)
25913 tree bases
= NULL_TREE
;
25915 /* Look for the `:' that begins the list. */
25916 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25918 /* Scan the base-specifier-list. */
25923 bool pack_expansion_p
= false;
25925 /* Look for the base-specifier. */
25926 base
= cp_parser_base_specifier (parser
);
25927 /* Look for the (optional) ellipsis. */
25928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25930 /* Consume the `...'. */
25931 cp_lexer_consume_token (parser
->lexer
);
25933 pack_expansion_p
= true;
25936 /* Add BASE to the front of the list. */
25937 if (base
&& base
!= error_mark_node
)
25939 if (pack_expansion_p
)
25940 /* Make this a pack expansion type. */
25941 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
25943 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
25945 TREE_CHAIN (base
) = bases
;
25949 /* Peek at the next token. */
25950 token
= cp_lexer_peek_token (parser
->lexer
);
25951 /* If it's not a comma, then the list is complete. */
25952 if (token
->type
!= CPP_COMMA
)
25954 /* Consume the `,'. */
25955 cp_lexer_consume_token (parser
->lexer
);
25958 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25959 base class had a qualified name. However, the next name that
25960 appears is certainly not qualified. */
25961 parser
->scope
= NULL_TREE
;
25962 parser
->qualifying_scope
= NULL_TREE
;
25963 parser
->object_scope
= NULL_TREE
;
25965 return nreverse (bases
);
25968 /* Parse a base-specifier.
25971 :: [opt] nested-name-specifier [opt] class-name
25972 virtual access-specifier [opt] :: [opt] nested-name-specifier
25974 access-specifier virtual [opt] :: [opt] nested-name-specifier
25977 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25978 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25979 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25980 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25983 cp_parser_base_specifier (cp_parser
* parser
)
25987 bool virtual_p
= false;
25988 bool duplicate_virtual_error_issued_p
= false;
25989 bool duplicate_access_error_issued_p
= false;
25990 bool class_scope_p
, template_p
;
25991 tree access
= access_default_node
;
25994 /* Process the optional `virtual' and `access-specifier'. */
25997 /* Peek at the next token. */
25998 token
= cp_lexer_peek_token (parser
->lexer
);
25999 /* Process `virtual'. */
26000 switch (token
->keyword
)
26003 /* If `virtual' appears more than once, issue an error. */
26004 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
26006 cp_parser_error (parser
,
26007 "%<virtual%> specified more than once in base-specifier");
26008 duplicate_virtual_error_issued_p
= true;
26013 /* Consume the `virtual' token. */
26014 cp_lexer_consume_token (parser
->lexer
);
26019 case RID_PROTECTED
:
26021 /* If more than one access specifier appears, issue an
26023 if (access
!= access_default_node
26024 && !duplicate_access_error_issued_p
)
26026 cp_parser_error (parser
,
26027 "more than one access specifier in base-specifier");
26028 duplicate_access_error_issued_p
= true;
26031 access
= ridpointers
[(int) token
->keyword
];
26033 /* Consume the access-specifier. */
26034 cp_lexer_consume_token (parser
->lexer
);
26043 /* It is not uncommon to see programs mechanically, erroneously, use
26044 the 'typename' keyword to denote (dependent) qualified types
26045 as base classes. */
26046 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26048 token
= cp_lexer_peek_token (parser
->lexer
);
26049 if (!processing_template_decl
)
26050 error_at (token
->location
,
26051 "keyword %<typename%> not allowed outside of templates");
26053 error_at (token
->location
,
26054 "keyword %<typename%> not allowed in this context "
26055 "(the base class is implicitly a type)");
26056 cp_lexer_consume_token (parser
->lexer
);
26059 /* Look for the optional `::' operator. */
26060 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
26061 /* Look for the nested-name-specifier. The simplest way to
26066 The keyword `typename' is not permitted in a base-specifier or
26067 mem-initializer; in these contexts a qualified name that
26068 depends on a template-parameter is implicitly assumed to be a
26071 is to pretend that we have seen the `typename' keyword at this
26073 cp_parser_nested_name_specifier_opt (parser
,
26074 /*typename_keyword_p=*/true,
26075 /*check_dependency_p=*/true,
26077 /*is_declaration=*/true);
26078 /* If the base class is given by a qualified name, assume that names
26079 we see are type names or templates, as appropriate. */
26080 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
26081 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
26084 && cp_lexer_next_token_is_decltype (parser
->lexer
))
26085 /* DR 950 allows decltype as a base-specifier. */
26086 type
= cp_parser_decltype (parser
);
26089 /* Otherwise, look for the class-name. */
26090 type
= cp_parser_class_name (parser
,
26094 /*check_dependency_p=*/true,
26095 /*class_head_p=*/false,
26096 /*is_declaration=*/true);
26097 type
= TREE_TYPE (type
);
26100 if (type
== error_mark_node
)
26101 return error_mark_node
;
26103 return finish_base_specifier (type
, access
, virtual_p
);
26106 /* Exception handling [gram.exception] */
26108 /* Save the tokens that make up the noexcept-specifier for a member-function.
26109 Returns a DEFERRED_PARSE. */
26112 cp_parser_save_noexcept (cp_parser
*parser
)
26114 cp_token
*first
= parser
->lexer
->next_token
;
26115 /* We want everything up to, including, the final ')'. */
26116 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0);
26117 cp_token
*last
= parser
->lexer
->next_token
;
26119 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
26120 to carry the information we will need. */
26121 tree expr
= make_node (DEFERRED_PARSE
);
26122 /* Save away the noexcept-specifier; we will process it when the
26123 class is complete. */
26124 DEFPARSE_TOKENS (expr
) = cp_token_cache_new (first
, last
);
26125 expr
= build_tree_list (expr
, NULL_TREE
);
26129 /* Used for late processing of noexcept-specifiers of member-functions.
26130 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
26131 we saved for later; parse it now. DECL is the declaration of the
26132 member function. */
26135 cp_parser_late_noexcept_specifier (cp_parser
*parser
, tree default_arg
)
26137 /* Make sure we've gotten something that hasn't been parsed yet. */
26138 gcc_assert (TREE_CODE (default_arg
) == DEFERRED_PARSE
);
26140 push_unparsed_function_queues (parser
);
26142 /* Push the saved tokens for the noexcept-specifier onto the parser's
26144 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_arg
);
26145 cp_parser_push_lexer_for_tokens (parser
, tokens
);
26147 /* Parse the cached noexcept-specifier. */
26149 = cp_parser_noexcept_specification_opt (parser
,
26150 CP_PARSER_FLAGS_NONE
,
26151 /*require_constexpr=*/true,
26152 /*consumed_expr=*/NULL
,
26153 /*return_cond=*/false);
26155 /* Revert to the main lexer. */
26156 cp_parser_pop_lexer (parser
);
26158 /* Restore the queue. */
26159 pop_unparsed_function_queues (parser
);
26161 /* And we're done. */
26165 /* Perform late checking of overriding function with respect to their
26166 noexcept-specifiers. TYPE is the class and FNDECL is the function
26167 that potentially overrides some virtual function with the same
26171 noexcept_override_late_checks (tree type
, tree fndecl
)
26173 tree binfo
= TYPE_BINFO (type
);
26176 if (DECL_STATIC_FUNCTION_P (fndecl
))
26179 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
26181 tree basetype
= BINFO_TYPE (base_binfo
);
26183 if (!TYPE_POLYMORPHIC_P (basetype
))
26186 tree fn
= look_for_overrides_here (basetype
, fndecl
);
26188 maybe_check_overriding_exception_spec (fndecl
, fn
);
26192 /* Parse an (optional) noexcept-specification.
26194 noexcept-specification:
26195 noexcept ( constant-expression ) [opt]
26197 If no noexcept-specification is present, returns NULL_TREE.
26198 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
26199 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
26200 there are no parentheses. CONSUMED_EXPR will be set accordingly.
26201 Otherwise, returns a noexcept specification unless RETURN_COND is true,
26202 in which case a boolean condition is returned instead. The parser flags
26203 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
26204 the (member) function is `const'. */
26207 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
26208 cp_parser_flags flags
,
26209 bool require_constexpr
,
26210 bool* consumed_expr
,
26214 const char *saved_message
;
26216 /* Peek at the next token. */
26217 token
= cp_lexer_peek_token (parser
->lexer
);
26219 /* Is it a noexcept-specification? */
26220 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
26224 /* [class.mem]/6 says that a noexcept-specifer (within the
26225 member-specification of the class) is a complete-class context of
26226 a class. So, if the noexcept-specifier has the optional expression,
26227 just save the tokens, and reparse this after we're done with the
26230 if ((flags
& CP_PARSER_FLAGS_DELAY_NOEXCEPT
)
26231 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
)
26232 /* No need to delay parsing for a number literal or true/false. */
26233 && !((cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
26234 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
26235 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
))
26236 && at_class_scope_p ()
26237 && TYPE_BEING_DEFINED (current_class_type
)
26238 && !LAMBDA_TYPE_P (current_class_type
))
26239 return cp_parser_save_noexcept (parser
);
26241 cp_lexer_consume_token (parser
->lexer
);
26243 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
26245 matching_parens parens
;
26246 parens
.consume_open (parser
);
26248 if (require_constexpr
)
26250 /* Types may not be defined in an exception-specification. */
26251 saved_message
= parser
->type_definition_forbidden_message
;
26252 parser
->type_definition_forbidden_message
26253 = G_("types may not be defined in an exception-specification");
26255 bool non_constant_p
;
26257 = cp_parser_constant_expression (parser
,
26258 /*allow_non_constant=*/true,
26261 && !require_potential_rvalue_constant_expression (expr
))
26264 return_cond
= true;
26267 /* Restore the saved message. */
26268 parser
->type_definition_forbidden_message
= saved_message
;
26272 expr
= cp_parser_expression (parser
);
26273 *consumed_expr
= true;
26276 parens
.require_close (parser
);
26280 expr
= boolean_true_node
;
26281 if (!require_constexpr
)
26282 *consumed_expr
= false;
26285 /* We cannot build a noexcept-spec right away because this will check
26286 that expr is a constexpr. */
26288 return build_noexcept_spec (expr
, tf_warning_or_error
);
26296 /* Parse an (optional) exception-specification.
26298 exception-specification:
26299 throw ( type-id-list [opt] )
26301 Returns a TREE_LIST representing the exception-specification. The
26302 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26303 control parsing. QUALS are qualifiers indicating whether the (member)
26304 function is `const'. */
26307 cp_parser_exception_specification_opt (cp_parser
* parser
,
26308 cp_parser_flags flags
)
26312 const char *saved_message
;
26314 /* Peek at the next token. */
26315 token
= cp_lexer_peek_token (parser
->lexer
);
26317 /* Is it a noexcept-specification? */
26319 = cp_parser_noexcept_specification_opt (parser
, flags
,
26320 /*require_constexpr=*/true,
26321 /*consumed_expr=*/NULL
,
26322 /*return_cond=*/false);
26323 if (type_id_list
!= NULL_TREE
)
26324 return type_id_list
;
26326 /* If it's not `throw', then there's no exception-specification. */
26327 if (!cp_parser_is_keyword (token
, RID_THROW
))
26330 location_t loc
= token
->location
;
26332 /* Consume the `throw'. */
26333 cp_lexer_consume_token (parser
->lexer
);
26335 /* Look for the `('. */
26336 matching_parens parens
;
26337 parens
.require_open (parser
);
26339 /* Peek at the next token. */
26340 token
= cp_lexer_peek_token (parser
->lexer
);
26341 /* If it's not a `)', then there is a type-id-list. */
26342 if (token
->type
!= CPP_CLOSE_PAREN
)
26344 /* Types may not be defined in an exception-specification. */
26345 saved_message
= parser
->type_definition_forbidden_message
;
26346 parser
->type_definition_forbidden_message
26347 = G_("types may not be defined in an exception-specification");
26348 /* Parse the type-id-list. */
26349 type_id_list
= cp_parser_type_id_list (parser
);
26350 /* Restore the saved message. */
26351 parser
->type_definition_forbidden_message
= saved_message
;
26353 if (cxx_dialect
>= cxx17
)
26355 error_at (loc
, "ISO C++17 does not allow dynamic exception "
26357 type_id_list
= NULL_TREE
;
26359 else if (cxx_dialect
>= cxx11
)
26360 warning_at (loc
, OPT_Wdeprecated
,
26361 "dynamic exception specifications are deprecated in "
26364 /* In C++17, throw() is equivalent to noexcept (true). throw()
26365 is deprecated in C++11 and above as well, but is still widely used,
26366 so don't warn about it yet. */
26367 else if (cxx_dialect
>= cxx17
)
26368 type_id_list
= noexcept_true_spec
;
26370 type_id_list
= empty_except_spec
;
26372 /* Look for the `)'. */
26373 parens
.require_close (parser
);
26375 return type_id_list
;
26378 /* Parse an (optional) type-id-list.
26382 type-id-list , type-id ... [opt]
26384 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26385 in the order that the types were presented. */
26388 cp_parser_type_id_list (cp_parser
* parser
)
26390 tree types
= NULL_TREE
;
26397 token
= cp_lexer_peek_token (parser
->lexer
);
26399 /* Get the next type-id. */
26400 type
= cp_parser_type_id (parser
);
26401 /* Check for invalid 'auto'. */
26402 if (flag_concepts
&& type_uses_auto (type
))
26404 error_at (token
->location
,
26405 "invalid use of %<auto%> in exception-specification");
26406 type
= error_mark_node
;
26408 /* Parse the optional ellipsis. */
26409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26411 /* Consume the `...'. */
26412 cp_lexer_consume_token (parser
->lexer
);
26414 /* Turn the type into a pack expansion expression. */
26415 type
= make_pack_expansion (type
);
26417 /* Add it to the list. */
26418 types
= add_exception_specifier (types
, type
, /*complain=*/1);
26419 /* Peek at the next token. */
26420 token
= cp_lexer_peek_token (parser
->lexer
);
26421 /* If it is not a `,', we are done. */
26422 if (token
->type
!= CPP_COMMA
)
26424 /* Consume the `,'. */
26425 cp_lexer_consume_token (parser
->lexer
);
26428 return nreverse (types
);
26431 /* Parse a try-block.
26434 try compound-statement handler-seq */
26437 cp_parser_try_block (cp_parser
* parser
)
26441 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
26442 if (parser
->in_function_body
26443 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
26444 && cxx_dialect
< cxx20
)
26445 pedwarn (input_location
, 0,
26446 "%<try%> in %<constexpr%> function only "
26447 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26449 try_block
= begin_try_block ();
26450 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
26451 finish_try_block (try_block
);
26452 cp_parser_handler_seq (parser
);
26453 finish_handler_sequence (try_block
);
26458 /* Parse a function-try-block.
26460 function-try-block:
26461 try ctor-initializer [opt] function-body handler-seq */
26464 cp_parser_function_try_block (cp_parser
* parser
)
26466 tree compound_stmt
;
26469 /* Look for the `try' keyword. */
26470 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
26472 /* Let the rest of the front end know where we are. */
26473 try_block
= begin_function_try_block (&compound_stmt
);
26474 /* Parse the function-body. */
26475 cp_parser_ctor_initializer_opt_and_function_body
26476 (parser
, /*in_function_try_block=*/true);
26477 /* We're done with the `try' part. */
26478 finish_function_try_block (try_block
);
26479 /* Parse the handlers. */
26480 cp_parser_handler_seq (parser
);
26481 /* We're done with the handlers. */
26482 finish_function_handler_sequence (try_block
, compound_stmt
);
26485 /* Parse a handler-seq.
26488 handler handler-seq [opt] */
26491 cp_parser_handler_seq (cp_parser
* parser
)
26497 /* Parse the handler. */
26498 cp_parser_handler (parser
);
26499 /* Peek at the next token. */
26500 token
= cp_lexer_peek_token (parser
->lexer
);
26501 /* If it's not `catch' then there are no more handlers. */
26502 if (!cp_parser_is_keyword (token
, RID_CATCH
))
26507 /* Parse a handler.
26510 catch ( exception-declaration ) compound-statement */
26513 cp_parser_handler (cp_parser
* parser
)
26518 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
26519 handler
= begin_handler ();
26520 matching_parens parens
;
26521 parens
.require_open (parser
);
26522 declaration
= cp_parser_exception_declaration (parser
);
26523 finish_handler_parms (declaration
, handler
);
26524 parens
.require_close (parser
);
26525 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
26526 finish_handler (handler
);
26529 /* Parse an exception-declaration.
26531 exception-declaration:
26532 type-specifier-seq declarator
26533 type-specifier-seq abstract-declarator
26537 Returns a VAR_DECL for the declaration, or NULL_TREE if the
26538 ellipsis variant is used. */
26541 cp_parser_exception_declaration (cp_parser
* parser
)
26543 cp_decl_specifier_seq type_specifiers
;
26544 cp_declarator
*declarator
;
26545 const char *saved_message
;
26547 /* If it's an ellipsis, it's easy to handle. */
26548 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26550 /* Consume the `...' token. */
26551 cp_lexer_consume_token (parser
->lexer
);
26555 /* Types may not be defined in exception-declarations. */
26556 saved_message
= parser
->type_definition_forbidden_message
;
26557 parser
->type_definition_forbidden_message
26558 = G_("types may not be defined in exception-declarations");
26560 /* Parse the type-specifier-seq. */
26561 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
26562 /*is_declaration=*/true,
26563 /*is_trailing_return=*/false,
26565 /* If it's a `)', then there is no declarator. */
26566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26569 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
26570 CP_PARSER_FLAGS_NONE
,
26571 /*ctor_dtor_or_conv_p=*/NULL
,
26572 /*parenthesized_p=*/NULL
,
26573 /*member_p=*/false,
26574 /*friend_p=*/false,
26575 /*static_p=*/false);
26577 /* Restore the saved message. */
26578 parser
->type_definition_forbidden_message
= saved_message
;
26580 if (!type_specifiers
.any_specifiers_p
)
26581 return error_mark_node
;
26583 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
26586 /* Parse a throw-expression.
26589 throw assignment-expression [opt]
26591 Returns a THROW_EXPR representing the throw-expression. */
26594 cp_parser_throw_expression (cp_parser
* parser
)
26598 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26600 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
26601 token
= cp_lexer_peek_token (parser
->lexer
);
26602 /* Figure out whether or not there is an assignment-expression
26603 following the "throw" keyword. */
26604 if (token
->type
== CPP_COMMA
26605 || token
->type
== CPP_SEMICOLON
26606 || token
->type
== CPP_CLOSE_PAREN
26607 || token
->type
== CPP_CLOSE_SQUARE
26608 || token
->type
== CPP_CLOSE_BRACE
26609 || token
->type
== CPP_COLON
)
26610 expression
= NULL_TREE
;
26612 expression
= cp_parser_assignment_expression (parser
);
26614 /* Construct a location e.g.:
26617 with caret == start at the start of the "throw" token, and
26618 the end at the end of the final token we consumed. */
26619 location_t combined_loc
= make_location (start_loc
, start_loc
,
26621 expression
= build_throw (combined_loc
, expression
);
26626 /* Parse a yield-expression.
26629 co_yield assignment-expression
26630 co_yield braced-init-list
26632 Returns a CO_YIELD_EXPR representing the yield-expression. */
26635 cp_parser_yield_expression (cp_parser
* parser
)
26639 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26640 location_t kw_loc
= token
->location
; /* Save for later. */
26642 cp_parser_require_keyword (parser
, RID_CO_YIELD
, RT_CO_YIELD
);
26644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26646 bool expr_non_constant_p
;
26647 cp_lexer_set_source_position (parser
->lexer
);
26648 /* ??? : probably a moot point? */
26649 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
26650 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
26653 expr
= cp_parser_assignment_expression (parser
);
26655 if (expr
== error_mark_node
)
26658 return finish_co_yield_expr (kw_loc
, expr
);
26661 /* GNU Extensions */
26663 /* Parse an (optional) asm-specification.
26666 asm ( string-literal )
26668 If the asm-specification is present, returns a STRING_CST
26669 corresponding to the string-literal. Otherwise, returns
26673 cp_parser_asm_specification_opt (cp_parser
* parser
)
26676 tree asm_specification
;
26678 /* Peek at the next token. */
26679 token
= cp_lexer_peek_token (parser
->lexer
);
26680 /* If the next token isn't the `asm' keyword, then there's no
26681 asm-specification. */
26682 if (!cp_parser_is_keyword (token
, RID_ASM
))
26685 /* Consume the `asm' token. */
26686 cp_lexer_consume_token (parser
->lexer
);
26687 /* Look for the `('. */
26688 matching_parens parens
;
26689 parens
.require_open (parser
);
26691 /* Look for the string-literal. */
26692 asm_specification
= cp_parser_string_literal (parser
, false, false);
26694 /* Look for the `)'. */
26695 parens
.require_close (parser
);
26697 return asm_specification
;
26700 /* Parse an asm-operand-list.
26704 asm-operand-list , asm-operand
26707 string-literal ( expression )
26708 [ string-literal ] string-literal ( expression )
26710 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26711 each node is the expression. The TREE_PURPOSE is itself a
26712 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26713 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26714 is a STRING_CST for the string literal before the parenthesis. Returns
26715 ERROR_MARK_NODE if any of the operands are invalid. */
26718 cp_parser_asm_operand_list (cp_parser
* parser
)
26720 tree asm_operands
= NULL_TREE
;
26721 bool invalid_operands
= false;
26725 tree string_literal
;
26729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
26731 /* Consume the `[' token. */
26732 cp_lexer_consume_token (parser
->lexer
);
26733 /* Read the operand name. */
26734 name
= cp_parser_identifier (parser
);
26735 if (name
!= error_mark_node
)
26736 name
= build_string (IDENTIFIER_LENGTH (name
),
26737 IDENTIFIER_POINTER (name
));
26738 /* Look for the closing `]'. */
26739 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
26743 /* Look for the string-literal. */
26744 string_literal
= cp_parser_string_literal (parser
, false, false);
26746 /* Look for the `('. */
26747 matching_parens parens
;
26748 parens
.require_open (parser
);
26749 /* Parse the expression. */
26750 expression
= cp_parser_expression (parser
);
26751 /* Look for the `)'. */
26752 parens
.require_close (parser
);
26754 if (name
== error_mark_node
26755 || string_literal
== error_mark_node
26756 || expression
== error_mark_node
)
26757 invalid_operands
= true;
26759 /* Add this operand to the list. */
26760 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
26763 /* If the next token is not a `,', there are no more
26765 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26767 /* Consume the `,'. */
26768 cp_lexer_consume_token (parser
->lexer
);
26771 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
26774 /* Parse an asm-clobber-list.
26778 asm-clobber-list , string-literal
26780 Returns a TREE_LIST, indicating the clobbers in the order that they
26781 appeared. The TREE_VALUE of each node is a STRING_CST. */
26784 cp_parser_asm_clobber_list (cp_parser
* parser
)
26786 tree clobbers
= NULL_TREE
;
26790 tree string_literal
;
26792 /* Look for the string literal. */
26793 string_literal
= cp_parser_string_literal (parser
, false, false);
26794 /* Add it to the list. */
26795 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
26796 /* If the next token is not a `,', then the list is
26798 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26800 /* Consume the `,' token. */
26801 cp_lexer_consume_token (parser
->lexer
);
26807 /* Parse an asm-label-list.
26811 asm-label-list , identifier
26813 Returns a TREE_LIST, indicating the labels in the order that they
26814 appeared. The TREE_VALUE of each node is a label. */
26817 cp_parser_asm_label_list (cp_parser
* parser
)
26819 tree labels
= NULL_TREE
;
26823 tree identifier
, label
, name
;
26825 /* Look for the identifier. */
26826 identifier
= cp_parser_identifier (parser
);
26827 if (!error_operand_p (identifier
))
26829 label
= lookup_label (identifier
);
26830 if (TREE_CODE (label
) == LABEL_DECL
)
26832 TREE_USED (label
) = 1;
26833 check_goto (label
);
26834 name
= build_string (IDENTIFIER_LENGTH (identifier
),
26835 IDENTIFIER_POINTER (identifier
));
26836 labels
= tree_cons (name
, label
, labels
);
26839 /* If the next token is not a `,', then the list is
26841 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26843 /* Consume the `,' token. */
26844 cp_lexer_consume_token (parser
->lexer
);
26847 return nreverse (labels
);
26850 /* Return TRUE iff the next tokens in the stream are possibly the
26851 beginning of a GNU extension attribute. */
26854 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
26856 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
26859 /* Return TRUE iff the next tokens in the stream are possibly the
26860 beginning of a standard C++-11 attribute specifier. */
26863 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
26865 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
26868 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26869 beginning of a standard C++-11 attribute specifier. */
26872 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
26874 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26876 return (cxx_dialect
>= cxx11
26877 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
26878 || (token
->type
== CPP_OPEN_SQUARE
26879 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
26880 && token
->type
== CPP_OPEN_SQUARE
)));
26883 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26884 beginning of a GNU extension attribute. */
26887 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
26889 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26891 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
26894 /* Return true iff the next tokens can be the beginning of either a
26895 GNU attribute list, or a standard C++11 attribute sequence. */
26898 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
26900 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
26901 || cp_next_tokens_can_be_std_attribute_p (parser
));
26904 /* Return true iff the next Nth tokens can be the beginning of either
26905 a GNU attribute list, or a standard C++11 attribute sequence. */
26908 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
26910 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
26911 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
26914 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26915 of GNU attributes, or return NULL. */
26918 cp_parser_attributes_opt (cp_parser
*parser
)
26920 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
26921 return cp_parser_gnu_attributes_opt (parser
);
26922 return cp_parser_std_attribute_spec_seq (parser
);
26925 /* Parse an (optional) series of attributes.
26928 attributes attribute
26931 __attribute__ (( attribute-list [opt] ))
26933 The return value is as for cp_parser_gnu_attribute_list. */
26936 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
26938 tree attributes
= NULL_TREE
;
26940 temp_override
<bool> cleanup
26941 (parser
->auto_is_implicit_function_template_parm_p
, false);
26946 tree attribute_list
;
26949 /* Peek at the next token. */
26950 token
= cp_lexer_peek_token (parser
->lexer
);
26951 /* If it's not `__attribute__', then we're done. */
26952 if (token
->keyword
!= RID_ATTRIBUTE
)
26955 /* Consume the `__attribute__' keyword. */
26956 cp_lexer_consume_token (parser
->lexer
);
26957 /* Look for the two `(' tokens. */
26958 matching_parens outer_parens
;
26959 if (!outer_parens
.require_open (parser
))
26961 matching_parens inner_parens
;
26962 if (!inner_parens
.require_open (parser
))
26965 /* Peek at the next token. */
26966 token
= cp_lexer_peek_token (parser
->lexer
);
26967 if (token
->type
!= CPP_CLOSE_PAREN
)
26968 /* Parse the attribute-list. */
26969 attribute_list
= cp_parser_gnu_attribute_list (parser
);
26971 /* If the next token is a `)', then there is no attribute
26973 attribute_list
= NULL
;
26975 /* Look for the two `)' tokens. */
26976 if (!inner_parens
.require_close (parser
))
26978 if (!outer_parens
.require_close (parser
))
26981 cp_parser_skip_to_end_of_statement (parser
);
26983 /* Add these new attributes to the list. */
26984 attributes
= attr_chainon (attributes
, attribute_list
);
26990 /* Parse a GNU attribute-list.
26994 attribute-list , attribute
26998 identifier ( identifier )
26999 identifier ( identifier , expression-list )
27000 identifier ( expression-list )
27002 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
27003 to an attribute. The TREE_PURPOSE of each node is the identifier
27004 indicating which attribute is in use. The TREE_VALUE represents
27005 the arguments, if any. */
27008 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
27010 tree attribute_list
= NULL_TREE
;
27011 bool save_translate_strings_p
= parser
->translate_strings_p
;
27013 /* Don't create wrapper nodes within attributes: the
27014 handlers don't know how to handle them. */
27015 auto_suppress_location_wrappers sentinel
;
27017 parser
->translate_strings_p
= false;
27024 /* Look for the identifier. We also allow keywords here; for
27025 example `__attribute__ ((const))' is legal. */
27026 token
= cp_lexer_peek_token (parser
->lexer
);
27027 if (token
->type
== CPP_NAME
27028 || token
->type
== CPP_KEYWORD
)
27030 tree arguments
= NULL_TREE
;
27032 /* Consume the token, but save it since we need it for the
27033 SIMD enabled function parsing. */
27034 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
27036 /* Save away the identifier that indicates which attribute
27038 identifier
= (token
->type
== CPP_KEYWORD
)
27039 /* For keywords, use the canonical spelling, not the
27040 parsed identifier. */
27041 ? ridpointers
[(int) token
->keyword
]
27042 : id_token
->u
.value
;
27044 identifier
= canonicalize_attr_name (identifier
);
27045 attribute
= build_tree_list (identifier
, NULL_TREE
);
27047 /* Peek at the next token. */
27048 token
= cp_lexer_peek_token (parser
->lexer
);
27049 /* If it's an `(', then parse the attribute arguments. */
27050 if (token
->type
== CPP_OPEN_PAREN
)
27052 vec
<tree
, va_gc
> *vec
;
27053 int attr_flag
= (attribute_takes_identifier_p (identifier
)
27054 ? id_attr
: normal_attr
);
27055 vec
= cp_parser_parenthesized_expression_list
27056 (parser
, attr_flag
, /*cast_p=*/false,
27057 /*allow_expansion_p=*/false,
27058 /*non_constant_p=*/NULL
);
27060 arguments
= error_mark_node
;
27063 arguments
= build_tree_list_vec (vec
);
27064 release_tree_vector (vec
);
27066 /* Save the arguments away. */
27067 TREE_VALUE (attribute
) = arguments
;
27070 if (arguments
!= error_mark_node
)
27072 /* Add this attribute to the list. */
27073 TREE_CHAIN (attribute
) = attribute_list
;
27074 attribute_list
= attribute
;
27077 token
= cp_lexer_peek_token (parser
->lexer
);
27079 /* Unless EXACTLY_ONE is set look for more attributes.
27080 If the next token isn't a `,', we're done. */
27081 if (exactly_one
|| token
->type
!= CPP_COMMA
)
27084 /* Consume the comma and keep going. */
27085 cp_lexer_consume_token (parser
->lexer
);
27087 parser
->translate_strings_p
= save_translate_strings_p
;
27089 /* We built up the list in reverse order. */
27090 return nreverse (attribute_list
);
27093 /* Parse a standard C++11 attribute.
27095 The returned representation is a TREE_LIST which TREE_PURPOSE is
27096 the scoped name of the attribute, and the TREE_VALUE is its
27099 Note that the scoped name of the attribute is itself a TREE_LIST
27100 which TREE_PURPOSE is the namespace of the attribute, and
27101 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
27102 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
27103 and which TREE_PURPOSE is directly the attribute name.
27105 Clients of the attribute code should use get_attribute_namespace
27106 and get_attribute_name to get the actual namespace and name of
27107 attributes, regardless of their being GNU or C++11 attributes.
27110 attribute-token attribute-argument-clause [opt]
27114 attribute-scoped-token
27116 attribute-scoped-token:
27117 attribute-namespace :: identifier
27119 attribute-namespace:
27122 attribute-argument-clause:
27123 ( balanced-token-seq )
27125 balanced-token-seq:
27126 balanced-token [opt]
27127 balanced-token-seq balanced-token
27130 ( balanced-token-seq )
27131 [ balanced-token-seq ]
27132 { balanced-token-seq }. */
27135 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
27137 tree attribute
, attr_id
= NULL_TREE
, arguments
;
27140 temp_override
<bool> cleanup
27141 (parser
->auto_is_implicit_function_template_parm_p
, false);
27143 /* First, parse name of the attribute, a.k.a attribute-token. */
27145 token
= cp_lexer_peek_token (parser
->lexer
);
27146 if (token
->type
== CPP_NAME
)
27147 attr_id
= token
->u
.value
;
27148 else if (token
->type
== CPP_KEYWORD
)
27149 attr_id
= ridpointers
[(int) token
->keyword
];
27150 else if (token
->flags
& NAMED_OP
)
27151 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
27153 if (attr_id
== NULL_TREE
)
27156 cp_lexer_consume_token (parser
->lexer
);
27158 token
= cp_lexer_peek_token (parser
->lexer
);
27159 if (token
->type
== CPP_SCOPE
)
27161 /* We are seeing a scoped attribute token. */
27163 cp_lexer_consume_token (parser
->lexer
);
27165 error_at (token
->location
, "attribute using prefix used together "
27166 "with scoped attribute token");
27169 token
= cp_lexer_peek_token (parser
->lexer
);
27170 if (token
->type
== CPP_NAME
)
27171 attr_id
= token
->u
.value
;
27172 else if (token
->type
== CPP_KEYWORD
)
27173 attr_id
= ridpointers
[(int) token
->keyword
];
27174 else if (token
->flags
& NAMED_OP
)
27175 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
27178 error_at (token
->location
,
27179 "expected an identifier for the attribute name");
27180 return error_mark_node
;
27182 cp_lexer_consume_token (parser
->lexer
);
27184 attr_ns
= canonicalize_attr_name (attr_ns
);
27185 attr_id
= canonicalize_attr_name (attr_id
);
27186 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
27188 token
= cp_lexer_peek_token (parser
->lexer
);
27192 attr_ns
= canonicalize_attr_name (attr_ns
);
27193 attr_id
= canonicalize_attr_name (attr_id
);
27194 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
27199 attr_id
= canonicalize_attr_name (attr_id
);
27200 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
27202 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
27203 but no longer: we have to be able to tell [[noreturn]] and
27204 __attribute__((noreturn)) apart. */
27205 /* C++14 deprecated attribute is equivalent to GNU's. */
27206 if (is_attribute_p ("deprecated", attr_id
))
27207 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
27208 /* C++17 fallthrough attribute is equivalent to GNU's. */
27209 else if (is_attribute_p ("fallthrough", attr_id
))
27210 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
27211 /* Transactional Memory TS optimize_for_synchronized attribute is
27212 equivalent to GNU transaction_callable. */
27213 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
27214 TREE_PURPOSE (attribute
)
27215 = get_identifier ("transaction_callable");
27216 /* Transactional Memory attributes are GNU attributes. */
27217 else if (tm_attr_to_mask (attr_id
))
27218 TREE_PURPOSE (attribute
) = attr_id
;
27221 /* Now parse the optional argument clause of the attribute. */
27223 if (token
->type
!= CPP_OPEN_PAREN
)
27227 vec
<tree
, va_gc
> *vec
;
27228 int attr_flag
= normal_attr
;
27230 /* Maybe we don't expect to see any arguments for this attribute. */
27231 const attribute_spec
*as
27232 = lookup_attribute_spec (TREE_PURPOSE (attribute
));
27233 if (as
&& as
->max_length
== 0)
27235 error_at (token
->location
, "%qE attribute does not take any arguments",
27237 cp_parser_skip_to_closing_parenthesis (parser
,
27238 /*recovering=*/true,
27239 /*or_comma=*/false,
27240 /*consume_paren=*/true);
27241 return error_mark_node
;
27244 if (attr_ns
== gnu_identifier
27245 && attribute_takes_identifier_p (attr_id
))
27246 /* A GNU attribute that takes an identifier in parameter. */
27247 attr_flag
= id_attr
;
27251 /* For unknown attributes, just skip balanced tokens instead of
27252 trying to parse the arguments. */
27253 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1) - 1; n
; --n
)
27254 cp_lexer_consume_token (parser
->lexer
);
27258 vec
= cp_parser_parenthesized_expression_list
27259 (parser
, attr_flag
, /*cast_p=*/false,
27260 /*allow_expansion_p=*/true,
27261 /*non_constant_p=*/NULL
);
27263 arguments
= error_mark_node
;
27266 if (vec
->is_empty ())
27267 /* e.g. [[attr()]]. */
27268 error_at (token
->location
, "parentheses must be omitted if "
27269 "%qE attribute argument list is empty",
27271 arguments
= build_tree_list_vec (vec
);
27272 release_tree_vector (vec
);
27275 if (arguments
== error_mark_node
)
27276 attribute
= error_mark_node
;
27278 TREE_VALUE (attribute
) = arguments
;
27284 /* Warn if the attribute ATTRIBUTE appears more than once in the
27285 attribute-list ATTRIBUTES. This used to be enforced for certain
27286 attributes, but the restriction was removed in P2156. Note that
27287 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
27288 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
27289 found in ATTRIBUTES. */
27292 cp_parser_check_std_attribute (location_t loc
, tree attributes
, tree attribute
)
27294 static auto alist
= { "noreturn", "deprecated", "nodiscard", "maybe_unused",
27295 "likely", "unlikely", "fallthrough",
27296 "no_unique_address" };
27298 for (const auto &a
: alist
)
27299 if (is_attribute_p (a
, get_attribute_name (attribute
))
27300 && lookup_attribute (a
, attributes
))
27302 if (!from_macro_expansion_at (loc
))
27303 warning_at (loc
, OPT_Wattributes
, "attribute %qs specified "
27304 "multiple times", a
);
27310 /* Parse a list of standard C++-11 attributes.
27314 attribute-list , attribute[opt]
27316 attribute-list , attribute ...
27320 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
27322 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
27323 cp_token
*token
= NULL
;
27327 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27328 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
27329 if (attribute
== error_mark_node
)
27331 if (attribute
!= NULL_TREE
)
27333 if (cp_parser_check_std_attribute (loc
, attributes
, attribute
))
27335 TREE_CHAIN (attribute
) = attributes
;
27336 attributes
= attribute
;
27339 token
= cp_lexer_peek_token (parser
->lexer
);
27340 if (token
->type
== CPP_ELLIPSIS
)
27342 cp_lexer_consume_token (parser
->lexer
);
27343 if (attribute
== NULL_TREE
)
27344 error_at (token
->location
,
27345 "expected attribute before %<...%>");
27348 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
27349 if (pack
== error_mark_node
)
27350 return error_mark_node
;
27351 TREE_VALUE (attribute
) = pack
;
27353 token
= cp_lexer_peek_token (parser
->lexer
);
27355 if (token
->type
!= CPP_COMMA
)
27357 cp_lexer_consume_token (parser
->lexer
);
27359 attributes
= nreverse (attributes
);
27363 /* Parse a standard C++-11 attribute specifier.
27365 attribute-specifier:
27366 [ [ attribute-using-prefix [opt] attribute-list ] ]
27367 alignment-specifier
27369 attribute-using-prefix:
27370 using attribute-namespace :
27372 alignment-specifier:
27373 alignas ( type-id ... [opt] )
27374 alignas ( alignment-expression ... [opt] ). */
27377 cp_parser_std_attribute_spec (cp_parser
*parser
)
27379 tree attributes
= NULL_TREE
;
27380 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27382 if (token
->type
== CPP_OPEN_SQUARE
27383 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
27385 tree attr_ns
= NULL_TREE
;
27387 cp_lexer_consume_token (parser
->lexer
);
27388 cp_lexer_consume_token (parser
->lexer
);
27390 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27392 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
27393 if (token
->type
== CPP_NAME
)
27394 attr_ns
= token
->u
.value
;
27395 else if (token
->type
== CPP_KEYWORD
)
27396 attr_ns
= ridpointers
[(int) token
->keyword
];
27397 else if (token
->flags
& NAMED_OP
)
27398 attr_ns
= get_identifier (cpp_type2name (token
->type
,
27401 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
27403 if (cxx_dialect
< cxx17
)
27404 pedwarn (input_location
, 0,
27405 "attribute using prefix only available "
27406 "with %<-std=c++17%> or %<-std=gnu++17%>");
27408 cp_lexer_consume_token (parser
->lexer
);
27409 cp_lexer_consume_token (parser
->lexer
);
27410 cp_lexer_consume_token (parser
->lexer
);
27413 attr_ns
= NULL_TREE
;
27416 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
27418 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
27419 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
27420 cp_parser_skip_to_end_of_statement (parser
);
27422 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27423 when we are sure that we have actually parsed them. */
27424 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
27430 /* Look for an alignment-specifier. */
27432 token
= cp_lexer_peek_token (parser
->lexer
);
27434 if (token
->type
!= CPP_KEYWORD
27435 || token
->keyword
!= RID_ALIGNAS
)
27438 cp_lexer_consume_token (parser
->lexer
);
27439 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
27441 matching_parens parens
;
27442 if (!parens
.require_open (parser
))
27443 return error_mark_node
;
27445 cp_parser_parse_tentatively (parser
);
27446 alignas_expr
= cp_parser_type_id (parser
);
27448 if (!cp_parser_parse_definitely (parser
))
27450 alignas_expr
= cp_parser_assignment_expression (parser
);
27451 if (alignas_expr
== error_mark_node
)
27452 cp_parser_skip_to_end_of_statement (parser
);
27453 if (alignas_expr
== NULL_TREE
27454 || alignas_expr
== error_mark_node
)
27455 return alignas_expr
;
27458 alignas_expr
= cxx_alignas_expr (alignas_expr
);
27459 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
27461 /* Handle alignas (pack...). */
27462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27464 cp_lexer_consume_token (parser
->lexer
);
27465 alignas_expr
= make_pack_expansion (alignas_expr
);
27468 /* Something went wrong, so don't build the attribute. */
27469 if (alignas_expr
== error_mark_node
)
27470 return error_mark_node
;
27472 /* Missing ')' means the code cannot possibly be valid; go ahead
27473 and commit to make sure we issue a hard error. */
27474 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
27475 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
27476 cp_parser_commit_to_tentative_parse (parser
);
27478 if (!parens
.require_close (parser
))
27479 return error_mark_node
;
27481 /* Build the C++-11 representation of an 'aligned'
27484 = build_tree_list (build_tree_list (gnu_identifier
,
27485 aligned_identifier
), alignas_expr
);
27491 /* Parse a standard C++-11 attribute-specifier-seq.
27493 attribute-specifier-seq:
27494 attribute-specifier-seq [opt] attribute-specifier
27498 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
27500 tree attr_specs
= NULL_TREE
;
27501 tree attr_last
= NULL_TREE
;
27503 /* Don't create wrapper nodes within attributes: the
27504 handlers don't know how to handle them. */
27505 auto_suppress_location_wrappers sentinel
;
27509 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
27510 if (attr_spec
== NULL_TREE
)
27512 if (attr_spec
== error_mark_node
)
27513 return error_mark_node
;
27516 TREE_CHAIN (attr_last
) = attr_spec
;
27518 attr_specs
= attr_last
= attr_spec
;
27519 attr_last
= tree_last (attr_last
);
27525 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
27526 return index of the first token after balanced-token, or N on failure. */
27529 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
27532 int nparens
= 0, nbraces
= 0, nsquares
= 0;
27534 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
27536 case CPP_PRAGMA_EOL
:
27537 if (!parser
->lexer
->in_pragma
)
27541 /* Ran out of tokens. */
27543 case CPP_OPEN_PAREN
:
27546 case CPP_OPEN_BRACE
:
27549 case CPP_OPEN_SQUARE
:
27552 case CPP_CLOSE_PAREN
:
27555 case CPP_CLOSE_BRACE
:
27558 case CPP_CLOSE_SQUARE
:
27564 while (nparens
|| nbraces
|| nsquares
);
27568 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
27569 return index of the first token after the GNU attribute tokens, or N on
27573 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
27577 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
27578 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
27579 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
27582 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
27585 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
27592 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
27593 next token), return index of the first token after the standard C++11
27594 attribute tokens, or N on failure. */
27597 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
27601 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
27602 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
27604 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
27607 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
27611 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
27612 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
27614 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
27625 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
27626 as the next token), return index of the first token after the attribute
27627 tokens, or N on failure. */
27630 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
27632 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
27633 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
27634 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
27637 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27638 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27639 current value of the PEDANTIC flag, regardless of whether or not
27640 the `__extension__' keyword is present. The caller is responsible
27641 for restoring the value of the PEDANTIC flag. */
27644 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
27646 /* Save the old value of the PEDANTIC flag. */
27647 *saved_pedantic
= pedantic
;
27649 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
27651 /* Consume the `__extension__' token. */
27652 cp_lexer_consume_token (parser
->lexer
);
27653 /* We're not being pedantic while the `__extension__' keyword is
27663 /* Parse a label declaration.
27666 __label__ label-declarator-seq ;
27668 label-declarator-seq:
27669 identifier , label-declarator-seq
27673 cp_parser_label_declaration (cp_parser
* parser
)
27675 /* Look for the `__label__' keyword. */
27676 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
27682 /* Look for an identifier. */
27683 identifier
= cp_parser_identifier (parser
);
27684 /* If we failed, stop. */
27685 if (identifier
== error_mark_node
)
27687 /* Declare it as a label. */
27688 finish_label_decl (identifier
);
27689 /* If the next token is a `;', stop. */
27690 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27692 /* Look for the `,' separating the label declarations. */
27693 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
27696 /* Look for the final `;'. */
27697 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27700 // -------------------------------------------------------------------------- //
27701 // Concept definitions
27704 cp_parser_concept_definition (cp_parser
*parser
)
27706 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
));
27707 cp_lexer_consume_token (parser
->lexer
);
27709 cp_expr id
= cp_parser_identifier (parser
);
27710 if (id
== error_mark_node
)
27712 cp_parser_skip_to_end_of_statement (parser
);
27713 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27717 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27719 cp_parser_skip_to_end_of_statement (parser
);
27720 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27721 return error_mark_node
;
27724 processing_constraint_expression_sentinel parsing_constraint
;
27725 tree init
= cp_parser_constraint_expression (parser
);
27726 if (init
== error_mark_node
)
27727 cp_parser_skip_to_end_of_statement (parser
);
27729 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27730 but continue as if it were. */
27731 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27733 return finish_concept_definition (id
, init
);
27736 // -------------------------------------------------------------------------- //
27739 /* Diagnose an expression that should appear in ()'s within a requires-clause
27740 and suggest where to place those parentheses. */
27743 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc
)
27745 error_at (loc
, "expression must be enclosed in parentheses");
27749 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc
)
27751 gcc_rich_location
richloc (loc
);
27752 richloc
.add_fixit_insert_before ("(");
27753 richloc
.add_fixit_insert_after (")");
27754 error_at (&richloc
, "expression must be enclosed in parentheses");
27757 /* Characterizes the likely kind of expression intended by a mis-written
27758 primary constraint. */
27759 enum primary_constraint_error
27762 pce_maybe_operator
,
27766 /* Returns true if the token(s) following a primary-expression in a
27767 constraint-logical-* expression would require parentheses. */
27769 static primary_constraint_error
27770 cp_parser_constraint_requires_parens (cp_parser
*parser
, bool lambda_p
)
27772 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27773 switch (token
->type
)
27780 /* An equal sign may be part of the definition of a function,
27781 and not an assignment operator, when parsing the expression
27782 for a trailing requires-clause. For example:
27784 template<typename T>
27786 S() requires C<T> = default;
27789 Don't try to reparse this a binary operator. */
27790 if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DELETE
)
27791 || cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DEFAULT
))
27794 gcc_fallthrough ();
27797 /* Arithmetic operators. */
27803 /* Bitwise operators. */
27809 /* Relational operators. */
27815 case CPP_GREATER_EQ
:
27816 case CPP_SPACESHIP
:
27817 /* Pointer-to-member. */
27819 case CPP_DEREF_STAR
:
27820 /* Assignment operators. */
27829 case CPP_RSHIFT_EQ
:
27830 case CPP_LSHIFT_EQ
:
27831 /* Conditional operator */
27833 /* Unenclosed binary or conditional operator. */
27834 return pce_maybe_operator
;
27836 case CPP_OPEN_PAREN
:
27838 /* A primary constraint that precedes the parameter-list of a
27839 lambda expression is followed by an open paren.
27841 []<typename T> requires C (T a, T b) { ... }
27843 Don't try to re-parse this as a postfix expression. */
27847 gcc_fallthrough ();
27849 case CPP_OPEN_SQUARE
:
27851 /* A primary-constraint-expression followed by a '[[' is not a
27852 postfix expression. */
27853 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
))
27856 gcc_fallthrough ();
27858 case CPP_PLUS_PLUS
:
27859 case CPP_MINUS_MINUS
:
27862 /* Unenclosed postfix operator. */
27863 return pce_maybe_postfix
;
27867 /* Returns true if the next token begins a unary expression, preceded by
27868 an operator or keyword. */
27871 cp_parser_unary_constraint_requires_parens (cp_parser
*parser
)
27873 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27874 switch (token
->type
)
27881 case CPP_PLUS_PLUS
:
27882 case CPP_MINUS_MINUS
:
27887 switch (token
->keyword
)
27891 case RID_REINTCAST
:
27892 case RID_CONSTCAST
:
27914 /* Parse a primary expression within a constraint. */
27917 cp_parser_constraint_primary_expression (cp_parser
*parser
, bool lambda_p
)
27919 /* If this looks like a unary expression, parse it as such, but diagnose
27920 it as ill-formed; it requires parens. */
27921 if (cp_parser_unary_constraint_requires_parens (parser
))
27923 cp_expr e
= cp_parser_assignment_expression (parser
, NULL
, false, false);
27924 cp_parser_diagnose_ungrouped_constraint_rich (e
.get_location());
27928 cp_lexer_save_tokens (parser
->lexer
);
27930 location_t loc
= input_location
;
27931 cp_expr expr
= cp_parser_primary_expression (parser
,
27932 /*address_p=*/false,
27934 /*template_arg_p=*/false,
27936 expr
.maybe_add_location_wrapper ();
27938 primary_constraint_error pce
= pce_ok
;
27939 if (expr
!= error_mark_node
)
27941 /* The primary-expression could be part of an unenclosed non-logical
27942 compound expression. */
27943 pce
= cp_parser_constraint_requires_parens (parser
, lambda_p
);
27947 cp_lexer_commit_tokens (parser
->lexer
);
27948 return finish_constraint_primary_expr (expr
);
27951 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27952 error, but return the expression as if it were valid. */
27953 cp_lexer_rollback_tokens (parser
->lexer
);
27954 cp_parser_parse_tentatively (parser
);
27955 if (pce
== pce_maybe_operator
)
27956 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
27958 expr
= cp_parser_simple_cast_expression (parser
);
27959 if (cp_parser_parse_definitely (parser
))
27961 cp_parser_diagnose_ungrouped_constraint_rich (expr
.get_location());
27965 /* Otherwise, something has gone very wrong, and we can't generate a more
27966 meaningful diagnostic or recover. */
27967 cp_parser_diagnose_ungrouped_constraint_plain (loc
);
27968 return error_mark_node
;
27971 /* Parse a constraint-logical-and-expression.
27973 constraint-logical-and-expression:
27975 constraint-logical-and-expression '&&' primary-expression */
27978 cp_parser_constraint_logical_and_expression (cp_parser
*parser
, bool lambda_p
)
27980 cp_expr lhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
27981 while (cp_lexer_next_token_is (parser
->lexer
, CPP_AND_AND
))
27983 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
27984 tree rhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
27985 lhs
= finish_constraint_and_expr (op
->location
, lhs
, rhs
);
27990 /* Parse a constraint-logical-or-expression.
27992 constraint-logical-or-expression:
27993 constraint-logical-and-expression
27994 constraint-logical-or-expression '||' constraint-logical-and-expression */
27997 cp_parser_constraint_logical_or_expression (cp_parser
*parser
, bool lambda_p
)
27999 cp_expr lhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
28000 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OR_OR
))
28002 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
28003 cp_expr rhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
28004 lhs
= finish_constraint_or_expr (op
->location
, lhs
, rhs
);
28009 /* Parse the expression after a requires-clause. This has a different grammar
28010 than that in the concepts TS. */
28013 cp_parser_requires_clause_expression (cp_parser
*parser
, bool lambda_p
)
28015 processing_constraint_expression_sentinel parsing_constraint
;
28016 temp_override
<int> ovr (processing_template_decl
);
28017 if (!processing_template_decl
)
28018 /* Adjust processing_template_decl so that we always obtain template
28019 trees here. We don't do the usual ++processing_template_decl
28020 because that would skew the template parameter depth of a lambda
28021 within if we're already inside a template. */
28022 processing_template_decl
= 1;
28023 cp_expr expr
= cp_parser_constraint_logical_or_expression (parser
, lambda_p
);
28024 if (check_for_bare_parameter_packs (expr
))
28025 expr
= error_mark_node
;
28029 /* Parse a expression after a requires clause.
28031 constraint-expression:
28032 logical-or-expression
28034 The required logical-or-expression must be a constant expression. Note
28035 that we don't check that the expression is constepxr here. We defer until
28036 we analyze constraints and then, we only check atomic constraints. */
28039 cp_parser_constraint_expression (cp_parser
*parser
)
28041 processing_constraint_expression_sentinel parsing_constraint
;
28042 temp_override
<int> ovr (processing_template_decl
);
28043 if (!processing_template_decl
)
28044 /* As in cp_parser_requires_clause_expression. */
28045 processing_template_decl
= 1;
28046 cp_expr expr
= cp_parser_binary_expression (parser
, false, true,
28047 PREC_NOT_OPERATOR
, NULL
);
28048 if (check_for_bare_parameter_packs (expr
))
28049 expr
= error_mark_node
;
28050 expr
.maybe_add_location_wrapper ();
28054 /* Optionally parse a requires clause:
28057 `requires` constraint-logical-or-expression.
28059 `requires constraint-expression.
28061 LAMBDA_P is true when the requires-clause is parsed before the
28062 parameter-list of a lambda-declarator. */
28065 cp_parser_requires_clause_opt (cp_parser
*parser
, bool lambda_p
)
28067 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
28068 if (tok
->keyword
!= RID_REQUIRES
)
28070 if (!flag_concepts
&& tok
->type
== CPP_NAME
28071 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
28073 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28074 "%<requires%> only available with "
28075 "%<-std=c++20%> or %<-fconcepts%>");
28076 /* Parse and discard the requires-clause. */
28077 cp_lexer_consume_token (parser
->lexer
);
28078 cp_parser_constraint_expression (parser
);
28083 cp_token
*tok2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
28084 if (tok2
->type
== CPP_OPEN_BRACE
)
28086 /* An opening brace following the start of a requires-clause is
28087 ill-formed; the user likely forgot the second `requires' that
28088 would start a requires-expression. */
28089 gcc_rich_location
richloc (tok2
->location
);
28090 richloc
.add_fixit_insert_after (tok
->location
, " requires");
28091 error_at (&richloc
, "missing additional %<requires%> to start "
28092 "a requires-expression");
28093 /* Don't consume the `requires', so that it's reused as the start of a
28094 requires-expression. */
28097 cp_lexer_consume_token (parser
->lexer
);
28099 if (!flag_concepts_ts
)
28100 return cp_parser_requires_clause_expression (parser
, lambda_p
);
28102 return cp_parser_constraint_expression (parser
);
28105 /*---------------------------------------------------------------------------
28106 Requires expressions
28107 ---------------------------------------------------------------------------*/
28109 /* Parse a requires expression
28111 requirement-expression:
28112 'requires' requirement-parameter-list [opt] requirement-body */
28115 cp_parser_requires_expression (cp_parser
*parser
)
28117 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
28118 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
28120 /* Avoid committing to outer tentative parse. */
28121 tentative_firewall
firewall (parser
);
28123 /* This is definitely a requires-expression. */
28124 cp_parser_commit_to_tentative_parse (parser
);
28128 /* Local parameters are delared as variables within the scope
28129 of the expression. They are not visible past the end of
28130 the expression. Expressions within the requires-expression
28131 are unevaluated. */
28132 struct scope_sentinel
28136 ++cp_unevaluated_operand
;
28137 begin_scope (sk_block
, NULL_TREE
);
28142 pop_bindings_and_leave_scope ();
28143 --cp_unevaluated_operand
;
28147 /* Parse the optional parameter list. */
28148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28150 parms
= cp_parser_requirement_parameter_list (parser
);
28151 if (parms
== error_mark_node
)
28152 return error_mark_node
;
28157 /* Parse the requirement body. */
28158 temp_override
<int> ovr (processing_template_decl
);
28159 if (!processing_template_decl
)
28160 /* As in cp_parser_requires_clause_expression. */
28161 processing_template_decl
= 1;
28162 reqs
= cp_parser_requirement_body (parser
);
28163 if (reqs
== error_mark_node
)
28164 return error_mark_node
;
28167 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
28169 grokparms (parms
, &parms
);
28170 loc
= make_location (loc
, loc
, parser
->lexer
);
28171 tree expr
= finish_requires_expr (loc
, parms
, reqs
);
28172 if (!processing_template_decl
)
28174 /* Perform semantic processing now to diagnose any invalid types and
28176 int saved_errorcount
= errorcount
;
28177 tsubst_requires_expr (expr
, NULL_TREE
, tf_warning_or_error
, NULL_TREE
);
28178 if (errorcount
> saved_errorcount
)
28179 return error_mark_node
;
28184 /* Parse a parameterized requirement.
28186 requirement-parameter-list:
28187 '(' parameter-declaration-clause ')' */
28190 cp_parser_requirement_parameter_list (cp_parser
*parser
)
28192 matching_parens parens
;
28193 if (!parens
.require_open (parser
))
28194 return error_mark_node
;
28196 tree parms
= (cp_parser_parameter_declaration_clause
28197 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
28199 if (!parens
.require_close (parser
))
28200 return error_mark_node
;
28205 /* Parse the body of a requirement.
28208 '{' requirement-list '}' */
28210 cp_parser_requirement_body (cp_parser
*parser
)
28212 matching_braces braces
;
28213 if (!braces
.require_open (parser
))
28214 return error_mark_node
;
28216 tree reqs
= cp_parser_requirement_seq (parser
);
28218 if (!braces
.require_close (parser
))
28219 return error_mark_node
;
28224 /* Parse a sequence of requirements.
28228 requirement-seq requirement */
28231 cp_parser_requirement_seq (cp_parser
*parser
)
28233 tree result
= NULL_TREE
;
28236 tree req
= cp_parser_requirement (parser
);
28237 if (req
!= error_mark_node
)
28238 result
= tree_cons (NULL_TREE
, req
, result
);
28239 } while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
));
28241 /* If there are no valid requirements, this is not a valid expression. */
28243 return error_mark_node
;
28245 /* Reverse the order of requirements so they are analyzed in order. */
28246 return nreverse (result
);
28249 /* Parse a syntactic requirement or type requirement.
28253 compound-requirement
28255 nested-requirement */
28258 cp_parser_requirement (cp_parser
*parser
)
28260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28261 return cp_parser_compound_requirement (parser
);
28262 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
28263 return cp_parser_type_requirement (parser
);
28264 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
28265 return cp_parser_nested_requirement (parser
);
28267 return cp_parser_simple_requirement (parser
);
28270 /* Parse a simple requirement.
28272 simple-requirement:
28276 cp_parser_simple_requirement (cp_parser
*parser
)
28278 location_t start
= cp_lexer_peek_token (parser
->lexer
)->location
;
28279 cp_expr expr
= cp_parser_expression (parser
, NULL
, false, false);
28280 if (expr
== error_mark_node
)
28281 cp_parser_skip_to_end_of_statement (parser
);
28283 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28285 if (!expr
|| expr
== error_mark_node
)
28286 return error_mark_node
;
28288 /* Sometimes we don't get locations, so use the cached token location
28289 as a reasonable approximation. */
28290 if (expr
.get_location() == UNKNOWN_LOCATION
)
28291 expr
.set_location (start
);
28293 return finish_simple_requirement (expr
.get_location (), expr
);
28296 /* Parse a type requirement
28299 nested-name-specifier [opt] required-type-name ';'
28301 required-type-name:
28303 'template' [opt] simple-template-id */
28306 cp_parser_type_requirement (cp_parser
*parser
)
28308 cp_token
*start_tok
= cp_lexer_consume_token (parser
->lexer
);
28309 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28311 // Save the scope before parsing name specifiers.
28312 tree saved_scope
= parser
->scope
;
28313 tree saved_object_scope
= parser
->object_scope
;
28314 tree saved_qualifying_scope
= parser
->qualifying_scope
;
28315 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
28316 cp_parser_nested_name_specifier_opt (parser
,
28317 /*typename_keyword_p=*/true,
28318 /*check_dependency_p=*/false,
28320 /*is_declaration=*/false);
28323 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28325 cp_lexer_consume_token (parser
->lexer
);
28326 type
= cp_parser_template_id (parser
,
28327 /*template_keyword_p=*/true,
28328 /*check_dependency=*/false,
28329 /*tag_type=*/none_type
,
28330 /*is_declaration=*/false);
28331 type
= make_typename_type (parser
->scope
, type
, typename_type
,
28332 /*complain=*/tf_error
);
28335 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
28337 if (TREE_CODE (type
) == TYPE_DECL
)
28338 type
= TREE_TYPE (type
);
28340 parser
->scope
= saved_scope
;
28341 parser
->object_scope
= saved_object_scope
;
28342 parser
->qualifying_scope
= saved_qualifying_scope
;
28344 if (type
== error_mark_node
)
28345 cp_parser_skip_to_end_of_statement (parser
);
28347 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28349 if (type
== error_mark_node
)
28350 return error_mark_node
;
28352 loc
= make_location (loc
, start_tok
->location
, parser
->lexer
);
28353 return finish_type_requirement (loc
, type
);
28356 /* Parse a compound requirement
28358 compound-requirement:
28359 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28362 cp_parser_compound_requirement (cp_parser
*parser
)
28364 /* Parse an expression enclosed in '{ }'s. */
28365 matching_braces braces
;
28366 if (!braces
.require_open (parser
))
28367 return error_mark_node
;
28369 cp_token
*expr_token
= cp_lexer_peek_token (parser
->lexer
);
28371 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
28372 if (expr
== error_mark_node
)
28373 cp_parser_skip_to_closing_brace (parser
);
28375 if (!braces
.require_close (parser
))
28377 cp_parser_skip_to_end_of_statement (parser
);
28378 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28379 return error_mark_node
;
28382 /* If the expression was invalid, skip the remainder of the requirement. */
28383 if (!expr
|| expr
== error_mark_node
)
28385 cp_parser_skip_to_end_of_statement (parser
);
28386 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28387 return error_mark_node
;
28390 /* Parse the optional noexcept. */
28391 bool noexcept_p
= false;
28392 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
28394 cp_lexer_consume_token (parser
->lexer
);
28398 /* Parse the optional trailing return type. */
28399 tree type
= NULL_TREE
;
28400 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
28402 cp_lexer_consume_token (parser
->lexer
);
28403 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
28405 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
28406 parser
->in_result_type_constraint_p
= true;
28407 /* C++20 allows either a type-id or a type-constraint. Parsing
28408 a type-id will subsume the parsing for a type-constraint but
28409 allow for more syntactic forms (e.g., const C<T>*). */
28410 type
= cp_parser_trailing_type_id (parser
);
28411 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
28412 if (type
== error_mark_node
)
28413 return error_mark_node
;
28415 location_t type_loc
= make_location (tok
->location
, tok
->location
,
28418 /* Check that we haven't written something like 'const C<T>*'. */
28419 if (type_uses_auto (type
))
28421 if (!is_auto (type
))
28423 error_at (type_loc
,
28424 "result type is not a plain type-constraint");
28425 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28426 return error_mark_node
;
28429 else if (!flag_concepts_ts
)
28430 /* P1452R2 removed the trailing-return-type option. */
28431 error_at (type_loc
,
28432 "return-type-requirement is not a type-constraint");
28435 location_t loc
= make_location (expr_token
->location
,
28436 braces
.open_location (),
28439 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28441 if (expr
== error_mark_node
|| type
== error_mark_node
)
28442 return error_mark_node
;
28444 return finish_compound_requirement (loc
, expr
, type
, noexcept_p
);
28447 /* Parse a nested requirement. This is the same as a requires clause.
28449 nested-requirement:
28453 cp_parser_nested_requirement (cp_parser
*parser
)
28455 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
28456 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
28457 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28458 tree req
= cp_parser_constraint_expression (parser
);
28459 if (req
== error_mark_node
)
28460 cp_parser_skip_to_end_of_statement (parser
);
28461 loc
= make_location (loc
, tok
->location
, parser
->lexer
);
28462 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28463 if (req
== error_mark_node
)
28464 return error_mark_node
;
28465 return finish_nested_requirement (loc
, req
);
28468 /* Support Functions */
28470 /* Return the appropriate prefer_type argument for lookup_name based on
28473 static inline LOOK_want
28474 prefer_type_arg (tag_types tag_type
)
28478 case none_type
: return LOOK_want::NORMAL
; // No preference.
28479 case scope_type
: return LOOK_want::TYPE_NAMESPACE
; // Type or namespace.
28480 default: return LOOK_want::TYPE
; // Type only.
28484 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
28485 NAME should have one of the representations used for an
28486 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
28487 is returned. If PARSER->SCOPE is a dependent type, then a
28488 SCOPE_REF is returned.
28490 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
28491 returned; the name was already resolved when the TEMPLATE_ID_EXPR
28492 was formed. Abstractly, such entities should not be passed to this
28493 function, because they do not need to be looked up, but it is
28494 simpler to check for this special case here, rather than at the
28497 In cases not explicitly covered above, this function returns a
28498 DECL, OVERLOAD, or baselink representing the result of the lookup.
28499 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
28502 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
28503 (e.g., "struct") that was used. In that case bindings that do not
28504 refer to types are ignored.
28506 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
28509 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
28512 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
28515 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
28516 TREE_LIST of candidates if name-lookup results in an ambiguity, and
28517 NULL_TREE otherwise. */
28520 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
28521 enum tag_types tag_type
,
28524 bool check_dependency
,
28525 tree
*ambiguous_decls
,
28526 location_t name_location
)
28529 tree object_type
= parser
->context
->object_type
;
28531 /* Assume that the lookup will be unambiguous. */
28532 if (ambiguous_decls
)
28533 *ambiguous_decls
= NULL_TREE
;
28535 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
28536 no longer valid. Note that if we are parsing tentatively, and
28537 the parse fails, OBJECT_TYPE will be automatically restored. */
28538 parser
->context
->object_type
= NULL_TREE
;
28540 if (name
== error_mark_node
)
28541 return error_mark_node
;
28543 /* A template-id has already been resolved; there is no lookup to
28545 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
28547 if (BASELINK_P (name
))
28549 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
28550 == TEMPLATE_ID_EXPR
);
28554 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
28555 it should already have been checked to make sure that the name
28556 used matches the type being destroyed. */
28557 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
28561 /* Figure out to which type this destructor applies. */
28563 type
= parser
->scope
;
28564 else if (object_type
)
28565 type
= object_type
;
28567 type
= current_class_type
;
28568 /* If that's not a class type, there is no destructor. */
28569 if (!type
|| !CLASS_TYPE_P (type
))
28570 return error_mark_node
;
28572 /* In a non-static member function, check implicit this->. */
28573 if (current_class_ref
)
28574 return lookup_destructor (current_class_ref
, parser
->scope
, name
,
28575 tf_warning_or_error
);
28577 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
28578 lazily_declare_fn (sfk_destructor
, type
);
28580 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
28583 return error_mark_node
;
28586 /* By this point, the NAME should be an ordinary identifier. If
28587 the id-expression was a qualified name, the qualifying scope is
28588 stored in PARSER->SCOPE at this point. */
28589 gcc_assert (identifier_p (name
));
28591 /* Perform the lookup. */
28596 if (parser
->scope
== error_mark_node
)
28597 return error_mark_node
;
28599 /* If the SCOPE is dependent, the lookup must be deferred until
28600 the template is instantiated -- unless we are explicitly
28601 looking up names in uninstantiated templates. Even then, we
28602 cannot look up the name if the scope is not a class type; it
28603 might, for example, be a template type parameter. */
28604 dependent_p
= (TYPE_P (parser
->scope
)
28605 && dependent_scope_p (parser
->scope
));
28606 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
28608 /* Defer lookup. */
28609 decl
= error_mark_node
;
28612 tree pushed_scope
= NULL_TREE
;
28614 /* If PARSER->SCOPE is a dependent type, then it must be a
28615 class type, and we must not be checking dependencies;
28616 otherwise, we would have processed this lookup above. So
28617 that PARSER->SCOPE is not considered a dependent base by
28618 lookup_member, we must enter the scope here. */
28620 pushed_scope
= push_scope (parser
->scope
);
28622 /* If the PARSER->SCOPE is a template specialization, it
28623 may be instantiated during name lookup. In that case,
28624 errors may be issued. Even if we rollback the current
28625 tentative parse, those errors are valid. */
28626 decl
= lookup_qualified_name (parser
->scope
, name
,
28627 prefer_type_arg (tag_type
),
28628 /*complain=*/true);
28630 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
28631 lookup result and the nested-name-specifier nominates a class C:
28632 * if the name specified after the nested-name-specifier, when
28633 looked up in C, is the injected-class-name of C (Clause 9), or
28634 * if the name specified after the nested-name-specifier is the
28635 same as the identifier or the simple-template-id's template-
28636 name in the last component of the nested-name-specifier,
28637 the name is instead considered to name the constructor of
28638 class C. [ Note: for example, the constructor is not an
28639 acceptable lookup result in an elaborated-type-specifier so
28640 the constructor would not be used in place of the
28641 injected-class-name. --end note ] Such a constructor name
28642 shall be used only in the declarator-id of a declaration that
28643 names a constructor or in a using-declaration. */
28644 if (tag_type
== none_type
28645 && DECL_SELF_REFERENCE_P (decl
)
28646 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
28647 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
28648 prefer_type_arg (tag_type
),
28649 /*complain=*/true);
28652 pop_scope (pushed_scope
);
28655 /* If the scope is a dependent type and either we deferred lookup or
28656 we did lookup but didn't find the name, rememeber the name. */
28657 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
28658 && dependent_type_p (parser
->scope
))
28664 /* The resolution to Core Issue 180 says that `struct
28665 A::B' should be considered a type-name, even if `A'
28667 type
= make_typename_type (parser
->scope
, name
, tag_type
,
28668 /*complain=*/tf_error
);
28669 if (type
!= error_mark_node
)
28670 decl
= TYPE_NAME (type
);
28672 else if (is_template
28673 && (cp_parser_next_token_ends_template_argument_p (parser
)
28674 || cp_lexer_next_token_is (parser
->lexer
,
28676 decl
= make_unbound_class_template (parser
->scope
,
28678 /*complain=*/tf_error
);
28680 decl
= build_qualified_name (/*type=*/NULL_TREE
,
28681 parser
->scope
, name
,
28684 parser
->qualifying_scope
= parser
->scope
;
28685 parser
->object_scope
= NULL_TREE
;
28687 else if (object_type
)
28689 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28690 OBJECT_TYPE is not a class. */
28691 if (CLASS_TYPE_P (object_type
))
28692 /* If the OBJECT_TYPE is a template specialization, it may
28693 be instantiated during name lookup. In that case, errors
28694 may be issued. Even if we rollback the current tentative
28695 parse, those errors are valid. */
28696 decl
= lookup_member (object_type
,
28699 /*prefer_type=*/tag_type
!= none_type
,
28700 tf_warning_or_error
);
28705 /* Look it up in the enclosing context. DR 141: When looking for a
28706 template-name after -> or ., only consider class templates. */
28707 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
28708 /* DR 141: When looking in the
28709 current enclosing context for a
28710 template-name after -> or ., only
28711 consider class templates. */
28712 : is_template
? LOOK_want::TYPE
28713 : prefer_type_arg (tag_type
));
28714 parser
->object_scope
= object_type
;
28715 parser
->qualifying_scope
= NULL_TREE
;
28719 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
28720 : prefer_type_arg (tag_type
));
28721 parser
->qualifying_scope
= NULL_TREE
;
28722 parser
->object_scope
= NULL_TREE
;
28725 /* If the lookup failed, let our caller know. */
28726 if (!decl
|| decl
== error_mark_node
)
28727 return error_mark_node
;
28729 /* Pull out the template from an injected-class-name (or multiple). */
28731 decl
= maybe_get_template_decl_from_type_decl (decl
);
28733 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28734 if (TREE_CODE (decl
) == TREE_LIST
)
28736 if (ambiguous_decls
)
28737 *ambiguous_decls
= decl
;
28738 /* The error message we have to print is too complicated for
28739 cp_parser_error, so we incorporate its actions directly. */
28740 if (!cp_parser_simulate_error (parser
))
28742 error_at (name_location
, "reference to %qD is ambiguous",
28744 print_candidates (decl
);
28746 return error_mark_node
;
28749 gcc_assert (DECL_P (decl
)
28750 || TREE_CODE (decl
) == OVERLOAD
28751 || TREE_CODE (decl
) == SCOPE_REF
28752 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
28753 || BASELINK_P (decl
));
28755 /* If we have resolved the name of a member declaration, check to
28756 see if the declaration is accessible. When the name resolves to
28757 set of overloaded functions, accessibility is checked when
28758 overload resolution is done.
28760 During an explicit instantiation, access is not checked at all,
28761 as per [temp.explicit]. */
28763 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
,
28764 tf_warning_or_error
);
28766 maybe_record_typedef_use (decl
);
28768 return cp_expr (decl
, name_location
);
28771 /* Like cp_parser_lookup_name, but for use in the typical case where
28772 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28773 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28776 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
28778 return cp_parser_lookup_name (parser
, name
,
28780 /*is_template=*/false,
28781 /*is_namespace=*/false,
28782 /*check_dependency=*/true,
28783 /*ambiguous_decls=*/NULL
,
28787 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28788 the current context, return the TYPE_DECL. If TAG_NAME_P is
28789 true, the DECL indicates the class being defined in a class-head,
28790 or declared in an elaborated-type-specifier.
28792 Otherwise, return DECL. */
28795 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
28797 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28798 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28801 template <typename T> struct B;
28804 template <typename T> struct A::B {};
28806 Similarly, in an elaborated-type-specifier:
28808 namespace N { struct X{}; }
28811 template <typename T> friend struct N::X;
28814 However, if the DECL refers to a class type, and we are in
28815 the scope of the class, then the name lookup automatically
28816 finds the TYPE_DECL created by build_self_reference rather
28817 than a TEMPLATE_DECL. For example, in:
28819 template <class T> struct S {
28823 there is no need to handle such case. */
28825 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
28826 return DECL_TEMPLATE_RESULT (decl
);
28831 /* If too many, or too few, template-parameter lists apply to the
28832 declarator, issue an error message. Returns TRUE if all went well,
28833 and FALSE otherwise. */
28836 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
28837 cp_declarator
*declarator
,
28838 location_t declarator_location
)
28840 switch (declarator
->kind
)
28844 unsigned num_templates
= 0;
28845 tree scope
= declarator
->u
.id
.qualifying_scope
;
28846 bool template_id_p
= false;
28849 num_templates
= num_template_headers_for_class (scope
);
28850 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
28851 == TEMPLATE_ID_EXPR
)
28853 /* If the DECLARATOR has the form `X<y>' then it uses one
28854 additional level of template parameters. */
28856 template_id_p
= true;
28859 return cp_parser_check_template_parameters
28860 (parser
, num_templates
, template_id_p
, declarator_location
,
28867 case cdk_reference
:
28869 return (cp_parser_check_declarator_template_parameters
28870 (parser
, declarator
->declarator
, declarator_location
));
28877 gcc_unreachable ();
28882 /* NUM_TEMPLATES were used in the current declaration. If that is
28883 invalid, return FALSE and issue an error messages. Otherwise,
28884 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28885 declarator and we can print more accurate diagnostics. */
28888 cp_parser_check_template_parameters (cp_parser
* parser
,
28889 unsigned num_templates
,
28890 bool template_id_p
,
28891 location_t location
,
28892 cp_declarator
*declarator
)
28894 /* If there are the same number of template classes and parameter
28895 lists, that's OK. */
28896 if (parser
->num_template_parameter_lists
== num_templates
)
28898 /* If there are more, but only one more, and the name ends in an identifier,
28899 then we are declaring a primary template. That's OK too. */
28901 && parser
->num_template_parameter_lists
== num_templates
+ 1)
28904 if (cp_parser_simulate_error (parser
))
28907 /* If there are more template classes than parameter lists, we have
28910 template <class T> void S<T>::R<T>::f (); */
28911 if (parser
->num_template_parameter_lists
< num_templates
)
28913 if (declarator
&& !current_function_decl
)
28914 error_at (location
, "specializing member %<%T::%E%> "
28915 "requires %<template<>%> syntax",
28916 declarator
->u
.id
.qualifying_scope
,
28917 declarator
->u
.id
.unqualified_name
);
28918 else if (declarator
)
28919 error_at (location
, "invalid declaration of %<%T::%E%>",
28920 declarator
->u
.id
.qualifying_scope
,
28921 declarator
->u
.id
.unqualified_name
);
28923 error_at (location
, "too few template-parameter-lists");
28926 /* Otherwise, there are too many template parameter lists. We have
28929 template <class T> template <class U> void S::f(); */
28930 error_at (location
, "too many template-parameter-lists");
28934 /* Parse an optional `::' token indicating that the following name is
28935 from the global namespace. If so, PARSER->SCOPE is set to the
28936 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28937 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28938 Returns the new value of PARSER->SCOPE, if the `::' token is
28939 present, and NULL_TREE otherwise. */
28942 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
28946 /* Peek at the next token. */
28947 token
= cp_lexer_peek_token (parser
->lexer
);
28948 /* If we're looking at a `::' token then we're starting from the
28949 global namespace, not our current location. */
28950 if (token
->type
== CPP_SCOPE
)
28952 /* Consume the `::' token. */
28953 cp_lexer_consume_token (parser
->lexer
);
28954 /* Set the SCOPE so that we know where to start the lookup. */
28955 parser
->scope
= global_namespace
;
28956 parser
->qualifying_scope
= global_namespace
;
28957 parser
->object_scope
= NULL_TREE
;
28959 return parser
->scope
;
28961 else if (!current_scope_valid_p
)
28963 parser
->scope
= NULL_TREE
;
28964 parser
->qualifying_scope
= NULL_TREE
;
28965 parser
->object_scope
= NULL_TREE
;
28971 /* Returns TRUE if the upcoming token sequence is the start of a
28972 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28973 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28974 is used to control type-specifier parsing. */
28977 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
28980 bool constructor_p
;
28981 bool outside_class_specifier_p
;
28982 tree nested_name_specifier
;
28983 cp_token
*next_token
;
28985 /* The common case is that this is not a constructor declarator, so
28986 try to avoid doing lots of work if at all possible. It's not
28987 valid declare a constructor at function scope. */
28988 if (parser
->in_function_body
)
28990 /* And only certain tokens can begin a constructor declarator. */
28991 next_token
= cp_lexer_peek_token (parser
->lexer
);
28992 if (next_token
->type
!= CPP_NAME
28993 && next_token
->type
!= CPP_SCOPE
28994 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
28995 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
28996 declarator-id of a constructor or destructor. */
28997 && (next_token
->type
!= CPP_TEMPLATE_ID
|| cxx_dialect
>= cxx20
))
29000 /* Parse tentatively; we are going to roll back all of the tokens
29002 cp_parser_parse_tentatively (parser
);
29003 /* Assume that we are looking at a constructor declarator. */
29004 constructor_p
= true;
29006 /* Look for the optional `::' operator. */
29007 cp_parser_global_scope_opt (parser
,
29008 /*current_scope_valid_p=*/false);
29009 /* Look for the nested-name-specifier. */
29010 nested_name_specifier
29011 = (cp_parser_nested_name_specifier_opt (parser
,
29012 /*typename_keyword_p=*/false,
29013 /*check_dependency_p=*/false,
29015 /*is_declaration=*/false));
29017 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
29018 if (nested_name_specifier
29019 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
29021 tree s
= resolve_typename_type (nested_name_specifier
,
29022 /*only_current_p=*/false);
29023 if (TREE_CODE (s
) != TYPENAME_TYPE
)
29024 nested_name_specifier
= s
;
29027 outside_class_specifier_p
= (!at_class_scope_p ()
29028 || !TYPE_BEING_DEFINED (current_class_type
)
29031 /* Outside of a class-specifier, there must be a
29032 nested-name-specifier. Except in C++17 mode, where we
29033 might be declaring a guiding declaration. */
29034 if (!nested_name_specifier
&& outside_class_specifier_p
29035 && cxx_dialect
< cxx17
)
29036 constructor_p
= false;
29037 else if (nested_name_specifier
== error_mark_node
)
29038 constructor_p
= false;
29040 /* If we have a class scope, this is easy; DR 147 says that S::S always
29041 names the constructor, and no other qualified name could. */
29042 if (constructor_p
&& nested_name_specifier
29043 && CLASS_TYPE_P (nested_name_specifier
))
29045 tree id
= cp_parser_unqualified_id (parser
,
29046 /*template_keyword_p=*/false,
29047 /*check_dependency_p=*/false,
29048 /*declarator_p=*/true,
29049 /*optional_p=*/false);
29050 if (is_overloaded_fn (id
))
29051 id
= DECL_NAME (get_first_fn (id
));
29052 if (!constructor_name_p (id
, nested_name_specifier
))
29053 constructor_p
= false;
29055 /* If we still think that this might be a constructor-declarator,
29056 look for a class-name. */
29057 else if (constructor_p
)
29061 template <typename T> struct S {
29065 we must recognize that the nested `S' names a class. */
29066 if (cxx_dialect
>= cxx17
)
29067 cp_parser_parse_tentatively (parser
);
29070 type_decl
= cp_parser_class_name (parser
,
29071 /*typename_keyword_p=*/false,
29072 /*template_keyword_p=*/false,
29074 /*check_dependency_p=*/false,
29075 /*class_head_p=*/false,
29076 /*is_declaration=*/false);
29078 if (cxx_dialect
>= cxx17
29079 && !cp_parser_parse_definitely (parser
))
29081 type_decl
= NULL_TREE
;
29082 tree tmpl
= cp_parser_template_name (parser
,
29083 /*template_keyword*/false,
29084 /*check_dependency_p*/false,
29085 /*is_declaration*/false,
29087 /*is_identifier*/NULL
);
29088 if (DECL_CLASS_TEMPLATE_P (tmpl
)
29089 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29090 /* It's a deduction guide, return true. */;
29092 cp_parser_simulate_error (parser
);
29095 /* If there was no class-name, then this is not a constructor.
29096 Otherwise, if we are in a class-specifier and we aren't
29097 handling a friend declaration, check that its type matches
29098 current_class_type (c++/38313). Note: error_mark_node
29099 is left alone for error recovery purposes. */
29100 constructor_p
= (!cp_parser_error_occurred (parser
)
29101 && (outside_class_specifier_p
29102 || type_decl
== NULL_TREE
29103 || type_decl
== error_mark_node
29104 || same_type_p (current_class_type
,
29105 TREE_TYPE (type_decl
))));
29107 /* If we're still considering a constructor, we have to see a `(',
29108 to begin the parameter-declaration-clause, followed by either a
29109 `)', an `...', or a decl-specifier. We need to check for a
29110 type-specifier to avoid being fooled into thinking that:
29114 is a constructor. (It is actually a function named `f' that
29115 takes one parameter (of type `int') and returns a value of type
29118 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29119 constructor_p
= false;
29122 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
29123 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
29124 /* A parameter declaration begins with a decl-specifier,
29125 which is either the "attribute" keyword, a storage class
29126 specifier, or (usually) a type-specifier. */
29127 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
29128 /* GNU attributes can actually appear both at the start of
29129 a parameter and parenthesized declarator.
29130 S (__attribute__((unused)) int);
29131 is a constructor, but
29132 S (__attribute__((unused)) foo) (int);
29133 is a function declaration. */
29134 || (cp_parser_allow_gnu_extensions_p (parser
)
29135 && cp_next_tokens_can_be_gnu_attribute_p (parser
)))
29136 /* A parameter declaration can also begin with [[attribute]]. */
29137 && !cp_next_tokens_can_be_std_attribute_p (parser
))
29140 tree pushed_scope
= NULL_TREE
;
29141 unsigned saved_num_template_parameter_lists
;
29143 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
29145 unsigned int n
= cp_parser_skip_gnu_attributes_opt (parser
, 1);
29147 cp_lexer_consume_token (parser
->lexer
);
29150 /* Names appearing in the type-specifier should be looked up
29151 in the scope of the class. */
29152 if (current_class_type
)
29154 else if (type_decl
)
29156 type
= TREE_TYPE (type_decl
);
29157 if (TREE_CODE (type
) == TYPENAME_TYPE
)
29159 type
= resolve_typename_type (type
,
29160 /*only_current_p=*/false);
29161 if (TREE_CODE (type
) == TYPENAME_TYPE
)
29163 cp_parser_abort_tentative_parse (parser
);
29167 pushed_scope
= push_scope (type
);
29170 /* Inside the constructor parameter list, surrounding
29171 template-parameter-lists do not apply. */
29172 saved_num_template_parameter_lists
29173 = parser
->num_template_parameter_lists
;
29174 parser
->num_template_parameter_lists
= 0;
29176 /* Look for the type-specifier. It's not optional, but its typename
29177 might be. Unless this is a friend declaration; we don't want to
29180 friend S (T::fn)(int);
29182 as a constructor, but with P0634, we might assume a type when
29183 looking for the type-specifier. It is actually a function named
29184 `T::fn' that takes one parameter (of type `int') and returns a
29185 value of type `S'. Constructors can be friends, but they must
29186 use a qualified name.
29188 Parse with an empty set of declaration specifiers since we're
29189 trying to match a decl-specifier-seq of the first parameter.
29190 This must be non-null so that cp_parser_simple_type_specifier
29191 will recognize a constrained placeholder type such as:
29192 'C<int> auto' where C is a type concept. */
29193 cp_decl_specifier_seq ctor_specs
;
29194 clear_decl_specs (&ctor_specs
);
29195 cp_parser_type_specifier (parser
,
29196 (friend_p
? CP_PARSER_FLAGS_NONE
29197 : (flags
& ~CP_PARSER_FLAGS_OPTIONAL
)),
29198 /*decl_specs=*/&ctor_specs
,
29199 /*is_declarator=*/true,
29200 /*declares_class_or_enum=*/NULL
,
29201 /*is_cv_qualifier=*/NULL
);
29203 parser
->num_template_parameter_lists
29204 = saved_num_template_parameter_lists
;
29206 /* Leave the scope of the class. */
29208 pop_scope (pushed_scope
);
29210 constructor_p
= !cp_parser_error_occurred (parser
);
29214 /* We did not really want to consume any tokens. */
29215 cp_parser_abort_tentative_parse (parser
);
29217 return constructor_p
;
29220 /* Parse the definition of the function given by the DECL_SPECIFIERS,
29221 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
29222 they must be performed once we are in the scope of the function.
29224 Returns the function defined. */
29227 cp_parser_function_definition_from_specifiers_and_declarator
29228 (cp_parser
* parser
,
29229 cp_decl_specifier_seq
*decl_specifiers
,
29231 const cp_declarator
*declarator
)
29236 /* Begin the function-definition. */
29237 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
29239 /* The things we're about to see are not directly qualified by any
29240 template headers we've seen thus far. */
29241 reset_specialization ();
29243 /* If there were names looked up in the decl-specifier-seq that we
29244 did not check, check them now. We must wait until we are in the
29245 scope of the function to perform the checks, since the function
29246 might be a friend. */
29247 perform_deferred_access_checks (tf_warning_or_error
);
29251 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
29252 parser
->omp_declare_simd
= NULL
;
29253 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
29254 parser
->oacc_routine
= NULL
;
29259 /* Skip the entire function. */
29260 cp_parser_skip_to_end_of_block_or_statement (parser
);
29261 fn
= error_mark_node
;
29263 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
29265 /* Seen already, skip it. An error message has already been output. */
29266 cp_parser_skip_to_end_of_block_or_statement (parser
);
29267 fn
= current_function_decl
;
29268 current_function_decl
= NULL_TREE
;
29269 /* If this is a function from a class, pop the nested class. */
29270 if (current_class_name
)
29271 pop_nested_class ();
29276 if (DECL_DECLARED_INLINE_P (current_function_decl
))
29277 tv
= TV_PARSE_INLINE
;
29279 tv
= TV_PARSE_FUNC
;
29281 fn
= cp_parser_function_definition_after_declarator (parser
,
29282 /*inline_p=*/false);
29289 /* Parse the part of a function-definition that follows the
29290 declarator. INLINE_P is TRUE iff this function is an inline
29291 function defined within a class-specifier.
29293 Returns the function defined. */
29296 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
29300 bool saved_in_unbraced_linkage_specification_p
;
29301 bool saved_in_function_body
;
29302 unsigned saved_num_template_parameter_lists
;
29304 bool fully_implicit_function_template_p
29305 = parser
->fully_implicit_function_template_p
;
29306 parser
->fully_implicit_function_template_p
= false;
29307 tree implicit_template_parms
29308 = parser
->implicit_template_parms
;
29309 parser
->implicit_template_parms
= 0;
29310 cp_binding_level
* implicit_template_scope
29311 = parser
->implicit_template_scope
;
29312 parser
->implicit_template_scope
= 0;
29314 saved_in_function_body
= parser
->in_function_body
;
29315 parser
->in_function_body
= true;
29316 /* If the next token is `return', then the code may be trying to
29317 make use of the "named return value" extension that G++ used to
29319 token
= cp_lexer_peek_token (parser
->lexer
);
29320 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
29322 /* Consume the `return' keyword. */
29323 cp_lexer_consume_token (parser
->lexer
);
29324 /* Look for the identifier that indicates what value is to be
29326 cp_parser_identifier (parser
);
29327 /* Issue an error message. */
29328 error_at (token
->location
,
29329 "named return values are no longer supported");
29330 /* Skip tokens until we reach the start of the function body. */
29333 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29334 if (token
->type
== CPP_OPEN_BRACE
29335 || token
->type
== CPP_EOF
29336 || token
->type
== CPP_PRAGMA_EOL
)
29338 cp_lexer_consume_token (parser
->lexer
);
29341 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29342 anything declared inside `f'. */
29343 saved_in_unbraced_linkage_specification_p
29344 = parser
->in_unbraced_linkage_specification_p
;
29345 parser
->in_unbraced_linkage_specification_p
= false;
29346 /* Inside the function, surrounding template-parameter-lists do not
29348 saved_num_template_parameter_lists
29349 = parser
->num_template_parameter_lists
;
29350 parser
->num_template_parameter_lists
= 0;
29352 /* If the next token is `try', `__transaction_atomic', or
29353 `__transaction_relaxed`, then we are looking at either function-try-block
29354 or function-transaction-block. Note that all of these include the
29356 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
29357 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
29358 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
29359 RID_TRANSACTION_RELAXED
))
29360 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
29361 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
29362 cp_parser_function_try_block (parser
);
29364 cp_parser_ctor_initializer_opt_and_function_body
29365 (parser
, /*in_function_try_block=*/false);
29367 /* Finish the function. */
29368 fn
= finish_function (inline_p
);
29369 /* Generate code for it, if necessary. */
29370 expand_or_defer_fn (fn
);
29371 /* Restore the saved values. */
29372 parser
->in_unbraced_linkage_specification_p
29373 = saved_in_unbraced_linkage_specification_p
;
29374 parser
->num_template_parameter_lists
29375 = saved_num_template_parameter_lists
;
29376 parser
->in_function_body
= saved_in_function_body
;
29378 parser
->fully_implicit_function_template_p
29379 = fully_implicit_function_template_p
;
29380 parser
->implicit_template_parms
29381 = implicit_template_parms
;
29382 parser
->implicit_template_scope
29383 = implicit_template_scope
;
29385 if (parser
->fully_implicit_function_template_p
)
29386 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
29391 /* Parse a template-declaration body (following argument list). */
29394 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
29395 tree parameter_list
,
29398 tree decl
= NULL_TREE
;
29399 bool friend_p
= false;
29401 /* We just processed one more parameter list. */
29402 ++parser
->num_template_parameter_lists
;
29404 /* Get the deferred access checks from the parameter list. These
29405 will be checked once we know what is being declared, as for a
29406 member template the checks must be performed in the scope of the
29407 class containing the member. */
29408 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
29410 /* Tentatively parse for a new template parameter list, which can either be
29411 the template keyword or a template introduction. */
29412 if (cp_parser_template_declaration_after_export (parser
, member_p
))
29414 else if (cxx_dialect
>= cxx11
29415 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
29416 decl
= cp_parser_alias_declaration (parser
);
29417 else if (cxx_dialect
>= cxx20
/* Implies flag_concept. */
29418 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
)
29419 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_BOOL
))
29420 /* Allow 'concept bool' to be handled as per the TS. */
29421 decl
= cp_parser_concept_definition (parser
);
29424 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29425 decl
= cp_parser_single_declaration (parser
,
29428 /*explicit_specialization_p=*/false,
29431 /* If this is a member template declaration, let the front
29433 if (member_p
&& !friend_p
&& decl
)
29435 if (TREE_CODE (decl
) == TYPE_DECL
)
29436 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
29438 decl
= finish_member_template_decl (decl
);
29440 else if (friend_p
&& decl
29441 && DECL_DECLARES_TYPE_P (decl
))
29442 make_friend_class (current_class_type
, TREE_TYPE (decl
),
29443 /*complain=*/true);
29445 /* We are done with the current parameter list. */
29446 --parser
->num_template_parameter_lists
;
29448 pop_deferring_access_checks ();
29451 finish_template_decl (parameter_list
);
29453 /* Check the template arguments for a literal operator template. */
29455 && DECL_DECLARES_FUNCTION_P (decl
)
29456 && UDLIT_OPER_P (DECL_NAME (decl
)))
29459 if (parameter_list
== NULL_TREE
)
29463 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
29464 if (num_parms
== 1)
29466 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
29467 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
29468 if (TREE_CODE (parm
) != PARM_DECL
)
29470 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
))
29471 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
29472 /* OK, C++20 string literal operator template. We don't need
29473 to warn in lower dialects here because we will have already
29474 warned about the template parameter. */;
29475 else if (TREE_TYPE (parm
) != char_type_node
29476 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
29479 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
29481 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
29482 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
29483 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
29484 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
29485 if (TREE_CODE (parm
) != PARM_DECL
29486 || TREE_TYPE (parm
) != TREE_TYPE (type
)
29487 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
29490 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
29491 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
29492 "ISO C++ did not adopt string literal operator templa"
29493 "tes taking an argument pack of characters");
29500 if (cxx_dialect
> cxx17
)
29501 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
29502 "template %qD has invalid parameter list; expected "
29503 "non-type template parameter pack %<<char...>%> or "
29504 "single non-type parameter of class type",
29507 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
29508 "template %qD has invalid parameter list; expected "
29509 "non-type template parameter pack %<<char...>%>",
29514 /* Register member declarations. */
29515 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
29516 finish_member_declaration (decl
);
29517 /* If DECL is a function template, we must return to parse it later.
29518 (Even though there is no definition, there might be default
29519 arguments that need handling.) */
29520 if (member_p
&& decl
29521 && DECL_DECLARES_FUNCTION_P (decl
))
29522 vec_safe_push (unparsed_funs_with_definitions
, decl
);
29525 /* Parse a template introduction header for a template-declaration. Returns
29526 false if tentative parse fails. */
29529 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
29531 cp_parser_parse_tentatively (parser
);
29533 tree saved_scope
= parser
->scope
;
29534 tree saved_object_scope
= parser
->object_scope
;
29535 tree saved_qualifying_scope
= parser
->qualifying_scope
;
29537 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
29539 /* Look for the optional `::' operator. */
29540 cp_parser_global_scope_opt (parser
,
29541 /*current_scope_valid_p=*/false);
29542 /* Look for the nested-name-specifier. */
29543 cp_parser_nested_name_specifier_opt (parser
,
29544 /*typename_keyword_p=*/false,
29545 /*check_dependency_p=*/true,
29547 /*is_declaration=*/false);
29549 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29550 tree concept_name
= cp_parser_identifier (parser
);
29552 /* Look up the concept for which we will be matching
29553 template parameters. */
29554 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
29556 parser
->scope
= saved_scope
;
29557 parser
->object_scope
= saved_object_scope
;
29558 parser
->qualifying_scope
= saved_qualifying_scope
;
29560 if (concept_name
== error_mark_node
29561 || (seen_error () && !concept_definition_p (tmpl_decl
)))
29562 cp_parser_simulate_error (parser
);
29564 /* Look for opening brace for introduction. */
29565 matching_braces braces
;
29566 braces
.require_open (parser
);
29567 location_t open_loc
= input_location
;
29569 if (!cp_parser_parse_definitely (parser
))
29572 push_deferring_access_checks (dk_deferred
);
29574 /* Build vector of placeholder parameters and grab
29575 matching identifiers. */
29576 tree introduction_list
= cp_parser_introduction_list (parser
);
29578 /* Look for closing brace for introduction. */
29579 if (!braces
.require_close (parser
))
29582 /* The introduction-list shall not be empty. */
29583 int nargs
= TREE_VEC_LENGTH (introduction_list
);
29586 /* In cp_parser_introduction_list we have already issued an error. */
29590 if (tmpl_decl
== error_mark_node
)
29592 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
29597 /* Build and associate the constraint. */
29598 location_t introduction_loc
= make_location (open_loc
,
29599 start_token
->location
,
29601 tree parms
= finish_template_introduction (tmpl_decl
,
29604 if (parms
&& parms
!= error_mark_node
)
29606 if (!flag_concepts_ts
)
29607 pedwarn (introduction_loc
, 0, "template-introductions"
29608 " are not part of C++20 concepts [-fconcepts-ts]");
29610 cp_parser_template_declaration_after_parameters (parser
, parms
,
29615 if (parms
== NULL_TREE
)
29616 error_at (token
->location
, "no matching concept for template-introduction");
29621 /* Parse a normal template-declaration following the template keyword. */
29624 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
29626 tree parameter_list
;
29627 bool need_lang_pop
;
29628 location_t location
= input_location
;
29630 /* Look for the `<' token. */
29631 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
29633 if (at_class_scope_p () && current_function_decl
)
29635 /* 14.5.2.2 [temp.mem]
29637 A local class shall not have member templates. */
29638 error_at (location
,
29639 "invalid declaration of member template in local class");
29640 cp_parser_skip_to_end_of_block_or_statement (parser
);
29645 A template ... shall not have C linkage. */
29646 if (current_lang_name
== lang_name_c
)
29648 error_at (location
, "template with C linkage");
29649 maybe_show_extern_c_location ();
29650 /* Give it C++ linkage to avoid confusing other parts of the
29652 push_lang_context (lang_name_cplusplus
);
29653 need_lang_pop
= true;
29656 need_lang_pop
= false;
29658 /* We cannot perform access checks on the template parameter
29659 declarations until we know what is being declared, just as we
29660 cannot check the decl-specifier list. */
29661 push_deferring_access_checks (dk_deferred
);
29663 /* If the next token is `>', then we have an invalid
29664 specialization. Rather than complain about an invalid template
29665 parameter, issue an error message here. */
29666 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
29668 cp_parser_error (parser
, "invalid explicit specialization");
29669 begin_specialization ();
29670 parameter_list
= NULL_TREE
;
29674 /* Parse the template parameters. */
29675 parameter_list
= cp_parser_template_parameter_list (parser
);
29678 /* Look for the `>'. */
29679 cp_parser_skip_to_end_of_template_parameter_list (parser
);
29681 /* Manage template requirements */
29684 tree reqs
= get_shorthand_constraints (current_template_parms
);
29685 if (tree treqs
= cp_parser_requires_clause_opt (parser
, false))
29686 reqs
= combine_constraint_expressions (reqs
, treqs
);
29687 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
29690 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
29693 /* For the erroneous case of a template with C linkage, we pushed an
29694 implicit C++ linkage scope; exit that scope now. */
29696 pop_lang_context ();
29699 /* Parse a template-declaration, assuming that the `export' (and
29700 `extern') keywords, if present, has already been scanned. MEMBER_P
29701 is as for cp_parser_template_declaration. */
29704 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
29706 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29708 cp_lexer_consume_token (parser
->lexer
);
29709 cp_parser_explicit_template_declaration (parser
, member_p
);
29712 else if (flag_concepts
)
29713 return cp_parser_template_introduction (parser
, member_p
);
29718 /* Perform the deferred access checks from a template-parameter-list.
29719 CHECKS is a TREE_LIST of access checks, as returned by
29720 get_deferred_access_checks. */
29723 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
29725 ++processing_template_parmlist
;
29726 perform_access_checks (checks
, tf_warning_or_error
);
29727 --processing_template_parmlist
;
29730 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29731 `function-definition' sequence that follows a template header.
29732 If MEMBER_P is true, this declaration appears in a class scope.
29734 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29735 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29738 cp_parser_single_declaration (cp_parser
* parser
,
29739 vec
<deferred_access_check
, va_gc
> *checks
,
29741 bool explicit_specialization_p
,
29744 int declares_class_or_enum
;
29745 tree decl
= NULL_TREE
;
29746 cp_decl_specifier_seq decl_specifiers
;
29747 bool function_definition_p
= false;
29748 cp_token
*decl_spec_token_start
;
29750 /* This function is only used when processing a template
29752 gcc_assert (innermost_scope_kind () == sk_template_parms
29753 || innermost_scope_kind () == sk_template_spec
);
29755 /* Defer access checks until we know what is being declared. */
29756 push_deferring_access_checks (dk_deferred
);
29758 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29760 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
29761 cp_parser_decl_specifier_seq (parser
,
29762 (CP_PARSER_FLAGS_OPTIONAL
29763 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
29765 &declares_class_or_enum
);
29767 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
29769 /* There are no template typedefs. */
29770 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
29772 error_at (decl_spec_token_start
->location
,
29773 "template declaration of %<typedef%>");
29774 decl
= error_mark_node
;
29777 /* Gather up the access checks that occurred the
29778 decl-specifier-seq. */
29779 stop_deferring_access_checks ();
29781 /* Check for the declaration of a template class. */
29782 if (declares_class_or_enum
)
29784 if (cp_parser_declares_only_class_p (parser
)
29785 || (declares_class_or_enum
& 2))
29787 /* If this is a declaration, but not a definition, associate
29788 any constraints with the type declaration. Constraints
29789 are associated with definitions in cp_parser_class_specifier. */
29790 if (declares_class_or_enum
== 1)
29791 associate_classtype_constraints (decl_specifiers
.type
);
29793 decl
= shadow_tag (&decl_specifiers
);
29798 friend template <typename T> struct A<T>::B;
29801 A<T>::B will be represented by a TYPENAME_TYPE, and
29802 therefore not recognized by shadow_tag. */
29803 if (friend_p
&& *friend_p
29805 && decl_specifiers
.type
29806 && TYPE_P (decl_specifiers
.type
))
29807 decl
= decl_specifiers
.type
;
29809 if (decl
&& decl
!= error_mark_node
)
29810 decl
= TYPE_NAME (decl
);
29812 decl
= error_mark_node
;
29814 /* Perform access checks for template parameters. */
29815 cp_parser_perform_template_parameter_access_checks (checks
);
29817 /* Give a helpful diagnostic for
29818 template <class T> struct A { } a;
29819 if we aren't already recovering from an error. */
29820 if (!cp_parser_declares_only_class_p (parser
)
29823 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29824 "a class template declaration must not declare "
29826 cp_parser_skip_to_end_of_block_or_statement (parser
);
29832 /* Complain about missing 'typename' or other invalid type names. */
29833 if (!decl_specifiers
.any_type_specifiers_p
29834 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
29836 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29837 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29838 the rest of this declaration. */
29839 decl
= error_mark_node
;
29843 /* If it's not a template class, try for a template function. If
29844 the next token is a `;', then this declaration does not declare
29845 anything. But, if there were errors in the decl-specifiers, then
29846 the error might well have come from an attempted class-specifier.
29847 In that case, there's no need to warn about a missing declarator. */
29849 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
29850 || decl_specifiers
.type
!= error_mark_node
))
29852 decl
= cp_parser_init_declarator (parser
,
29853 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
29856 /*function_definition_allowed_p=*/true,
29858 declares_class_or_enum
,
29859 &function_definition_p
,
29862 /* 7.1.1-1 [dcl.stc]
29864 A storage-class-specifier shall not be specified in an explicit
29865 specialization... */
29867 && explicit_specialization_p
29868 && decl_specifiers
.storage_class
!= sc_none
)
29870 error_at (decl_spec_token_start
->location
,
29871 "explicit template specialization cannot have a storage class");
29872 decl
= error_mark_node
;
29875 if (decl
&& VAR_P (decl
))
29876 check_template_variable (decl
);
29879 /* Look for a trailing `;' after the declaration. */
29880 if (!function_definition_p
29881 && (decl
== error_mark_node
29882 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
29883 cp_parser_skip_to_end_of_block_or_statement (parser
);
29886 pop_deferring_access_checks ();
29888 /* Clear any current qualification; whatever comes next is the start
29889 of something new. */
29890 parser
->scope
= NULL_TREE
;
29891 parser
->qualifying_scope
= NULL_TREE
;
29892 parser
->object_scope
= NULL_TREE
;
29897 /* Parse a cast-expression that is not the operand of a unary "&". */
29900 cp_parser_simple_cast_expression (cp_parser
*parser
)
29902 return cp_parser_cast_expression (parser
, /*address_p=*/false,
29903 /*cast_p=*/false, /*decltype*/false, NULL
);
29906 /* Parse a functional cast to TYPE. Returns an expression
29907 representing the cast. */
29910 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
29912 vec
<tree
, va_gc
> *vec
;
29913 tree expression_list
;
29917 location_t start_loc
= input_location
;
29920 type
= error_mark_node
;
29922 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29924 cp_lexer_set_source_position (parser
->lexer
);
29925 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
29926 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
29927 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
29928 if (TREE_CODE (type
) == TYPE_DECL
)
29929 type
= TREE_TYPE (type
);
29931 cast
= finish_compound_literal (type
, expression_list
,
29932 tf_warning_or_error
, fcl_functional
);
29933 /* Create a location of the form:
29936 with caret == start at the start of the type name,
29937 finishing at the closing brace. */
29938 location_t combined_loc
= make_location (start_loc
, start_loc
,
29940 cast
.set_location (combined_loc
);
29945 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
29947 /*allow_expansion_p=*/true,
29948 /*non_constant_p=*/NULL
);
29950 expression_list
= error_mark_node
;
29953 expression_list
= build_tree_list_vec (vec
);
29954 release_tree_vector (vec
);
29957 /* Create a location of the form:
29960 with caret == start at the start of the type name,
29961 finishing at the closing paren. */
29962 location_t combined_loc
= make_location (start_loc
, start_loc
,
29964 cast
= build_functional_cast (combined_loc
, type
, expression_list
,
29965 tf_warning_or_error
);
29967 /* [expr.const]/1: In an integral constant expression "only type
29968 conversions to integral or enumeration type can be used". */
29969 if (TREE_CODE (type
) == TYPE_DECL
)
29970 type
= TREE_TYPE (type
);
29971 if (cast
!= error_mark_node
29972 && !cast_valid_in_integral_constant_expression_p (type
)
29973 && cp_parser_non_integral_constant_expression (parser
,
29975 return error_mark_node
;
29980 /* Save the tokens that make up the body of a member function defined
29981 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29982 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29983 specifiers applied to the declaration. Returns the FUNCTION_DECL
29984 for the member function. */
29987 cp_parser_save_member_function_body (cp_parser
* parser
,
29988 cp_decl_specifier_seq
*decl_specifiers
,
29989 cp_declarator
*declarator
,
29995 bool function_try_block
= false;
29997 /* Create the FUNCTION_DECL. */
29998 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
29999 cp_finalize_omp_declare_simd (parser
, fn
);
30000 cp_finalize_oacc_routine (parser
, fn
, true);
30001 /* If something went badly wrong, bail out now. */
30002 if (fn
== error_mark_node
)
30004 /* If there's a function-body, skip it. */
30005 if (cp_parser_token_starts_function_definition_p
30006 (cp_lexer_peek_token (parser
->lexer
)))
30007 cp_parser_skip_to_end_of_block_or_statement (parser
);
30008 return error_mark_node
;
30011 /* Remember it, if there are default args to post process. */
30012 cp_parser_save_default_args (parser
, fn
);
30014 /* Save away the tokens that make up the body of the
30016 first
= parser
->lexer
->next_token
;
30018 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
30019 cp_lexer_consume_token (parser
->lexer
);
30020 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
30021 RID_TRANSACTION_ATOMIC
))
30023 cp_lexer_consume_token (parser
->lexer
);
30024 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
30025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
30026 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
30027 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
30028 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
30029 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
30030 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
30032 cp_lexer_consume_token (parser
->lexer
);
30033 cp_lexer_consume_token (parser
->lexer
);
30034 cp_lexer_consume_token (parser
->lexer
);
30035 cp_lexer_consume_token (parser
->lexer
);
30036 cp_lexer_consume_token (parser
->lexer
);
30039 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
30040 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
30042 cp_lexer_consume_token (parser
->lexer
);
30043 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
30048 /* Handle function try blocks. */
30049 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
30051 cp_lexer_consume_token (parser
->lexer
);
30052 function_try_block
= true;
30054 /* We can have braced-init-list mem-initializers before the fn body. */
30055 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30057 cp_lexer_consume_token (parser
->lexer
);
30058 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
30060 /* cache_group will stop after an un-nested { } pair, too. */
30061 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
30064 /* variadic mem-inits have ... after the ')'. */
30065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30066 cp_lexer_consume_token (parser
->lexer
);
30069 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
30070 /* Handle function try blocks. */
30071 if (function_try_block
)
30072 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
30073 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
30074 last
= parser
->lexer
->next_token
;
30076 /* Save away the inline definition; we will process it when the
30077 class is complete. */
30078 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
30079 DECL_PENDING_INLINE_P (fn
) = 1;
30081 /* We need to know that this was defined in the class, so that
30082 friend templates are handled correctly. */
30083 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
30085 /* Add FN to the queue of functions to be parsed later. */
30086 vec_safe_push (unparsed_funs_with_definitions
, fn
);
30091 /* Save the tokens that make up the in-class initializer for a non-static
30092 data member. Returns a DEFERRED_PARSE. */
30095 cp_parser_save_nsdmi (cp_parser
* parser
)
30097 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
30100 /* Parse a template-argument-list, as well as the trailing ">" (but
30101 not the opening "<"). See cp_parser_template_argument_list for the
30105 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
30109 tree saved_qualifying_scope
;
30110 tree saved_object_scope
;
30111 bool saved_greater_than_is_operator_p
;
30115 When parsing a template-id, the first non-nested `>' is taken as
30116 the end of the template-argument-list rather than a greater-than
30118 saved_greater_than_is_operator_p
30119 = parser
->greater_than_is_operator_p
;
30120 parser
->greater_than_is_operator_p
= false;
30121 /* Parsing the argument list may modify SCOPE, so we save it
30123 saved_scope
= parser
->scope
;
30124 saved_qualifying_scope
= parser
->qualifying_scope
;
30125 saved_object_scope
= parser
->object_scope
;
30126 /* We need to evaluate the template arguments, even though this
30127 template-id may be nested within a "sizeof". */
30129 /* Parse the template-argument-list itself. */
30130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
30131 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
30132 arguments
= NULL_TREE
;
30134 arguments
= cp_parser_template_argument_list (parser
);
30135 /* Look for the `>' that ends the template-argument-list. If we find
30136 a '>>' instead, it's probably just a typo. */
30137 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
30139 if (cxx_dialect
!= cxx98
)
30141 /* In C++0x, a `>>' in a template argument list or cast
30142 expression is considered to be two separate `>'
30143 tokens. So, change the current token to a `>', but don't
30144 consume it: it will be consumed later when the outer
30145 template argument list (or cast expression) is parsed.
30146 Note that this replacement of `>' for `>>' is necessary
30147 even if we are parsing tentatively: in the tentative
30148 case, after calling
30149 cp_parser_enclosed_template_argument_list we will always
30150 throw away all of the template arguments and the first
30151 closing `>', either because the template argument list
30152 was erroneous or because we are replacing those tokens
30153 with a CPP_TEMPLATE_ID token. The second `>' (which will
30154 not have been thrown away) is needed either to close an
30155 outer template argument list or to complete a new-style
30157 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30158 token
->type
= CPP_GREATER
;
30160 else if (!saved_greater_than_is_operator_p
)
30162 /* If we're in a nested template argument list, the '>>' has
30163 to be a typo for '> >'. We emit the error message, but we
30164 continue parsing and we push a '>' as next token, so that
30165 the argument list will be parsed correctly. Note that the
30166 global source location is still on the token before the
30167 '>>', so we need to say explicitly where we want it. */
30168 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30169 gcc_rich_location
richloc (token
->location
);
30170 richloc
.add_fixit_replace ("> >");
30171 error_at (&richloc
, "%<>>%> should be %<> >%> "
30172 "within a nested template argument list");
30174 token
->type
= CPP_GREATER
;
30178 /* If this is not a nested template argument list, the '>>'
30179 is a typo for '>'. Emit an error message and continue.
30180 Same deal about the token location, but here we can get it
30181 right by consuming the '>>' before issuing the diagnostic. */
30182 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30183 error_at (token
->location
,
30184 "spurious %<>>%>, use %<>%> to terminate "
30185 "a template argument list");
30189 cp_parser_skip_to_end_of_template_parameter_list (parser
);
30190 /* The `>' token might be a greater-than operator again now. */
30191 parser
->greater_than_is_operator_p
30192 = saved_greater_than_is_operator_p
;
30193 /* Restore the SAVED_SCOPE. */
30194 parser
->scope
= saved_scope
;
30195 parser
->qualifying_scope
= saved_qualifying_scope
;
30196 parser
->object_scope
= saved_object_scope
;
30201 /* MEMBER_FUNCTION is a member function, or a friend. If default
30202 arguments, or the body of the function have not yet been parsed,
30206 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
30208 timevar_push (TV_PARSE_INMETH
);
30209 /* If this member is a template, get the underlying
30211 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
30212 member_function
= DECL_TEMPLATE_RESULT (member_function
);
30214 /* There should not be any class definitions in progress at this
30215 point; the bodies of members are only parsed outside of all class
30217 gcc_assert (parser
->num_classes_being_defined
== 0);
30218 /* While we're parsing the member functions we might encounter more
30219 classes. We want to handle them right away, but we don't want
30220 them getting mixed up with functions that are currently in the
30222 push_unparsed_function_queues (parser
);
30224 /* Make sure that any template parameters are in scope. */
30225 maybe_begin_member_template_processing (member_function
);
30227 /* If the body of the function has not yet been parsed, parse it
30229 if (DECL_PENDING_INLINE_P (member_function
))
30231 tree function_scope
;
30232 cp_token_cache
*tokens
;
30234 /* The function is no longer pending; we are processing it. */
30235 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
30236 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
30237 DECL_PENDING_INLINE_P (member_function
) = 0;
30239 /* If this is a local class, enter the scope of the containing
30241 function_scope
= current_function_decl
;
30242 if (function_scope
)
30243 push_function_context ();
30245 /* Push the body of the function onto the lexer stack. */
30246 cp_parser_push_lexer_for_tokens (parser
, tokens
);
30248 /* Let the front end know that we going to be defining this
30250 start_preparsed_function (member_function
, NULL_TREE
,
30251 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
30253 /* Don't do access checking if it is a templated function. */
30254 if (processing_template_decl
)
30255 push_deferring_access_checks (dk_no_check
);
30257 /* #pragma omp declare reduction needs special parsing. */
30258 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
30260 parser
->lexer
->in_pragma
= true;
30261 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
30262 finish_function (/*inline_p=*/true);
30263 cp_check_omp_declare_reduction (member_function
);
30266 /* Now, parse the body of the function. */
30267 cp_parser_function_definition_after_declarator (parser
,
30268 /*inline_p=*/true);
30270 if (processing_template_decl
)
30271 pop_deferring_access_checks ();
30273 /* Leave the scope of the containing function. */
30274 if (function_scope
)
30275 pop_function_context ();
30276 cp_parser_pop_lexer (parser
);
30279 /* Remove any template parameters from the symbol table. */
30280 maybe_end_member_template_processing ();
30282 /* Restore the queue. */
30283 pop_unparsed_function_queues (parser
);
30284 timevar_pop (TV_PARSE_INMETH
);
30287 /* If DECL contains any default args, remember it on the unparsed
30288 functions queue. */
30291 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
30295 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
30297 probe
= TREE_CHAIN (probe
))
30298 if (TREE_PURPOSE (probe
))
30300 cp_default_arg_entry entry
= {current_class_type
, decl
};
30301 vec_safe_push (unparsed_funs_with_default_args
, entry
);
30305 /* Remember if there is a noexcept-specifier to post process. */
30306 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
30307 if (UNPARSED_NOEXCEPT_SPEC_P (spec
))
30308 vec_safe_push (unparsed_noexcepts
, decl
);
30311 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30312 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30313 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30314 from the parameter-type-list. */
30317 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
30318 tree default_arg
, tree parmtype
)
30320 cp_token_cache
*tokens
;
30324 if (default_arg
== error_mark_node
)
30325 return error_mark_node
;
30327 /* Push the saved tokens for the default argument onto the parser's
30329 tokens
= DEFPARSE_TOKENS (default_arg
);
30330 cp_parser_push_lexer_for_tokens (parser
, tokens
);
30332 start_lambda_scope (decl
);
30334 /* Parse the default argument. */
30335 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
30336 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
30337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
30339 finish_lambda_scope ();
30341 if (parsed_arg
== error_mark_node
)
30342 cp_parser_skip_to_end_of_statement (parser
);
30344 if (!processing_template_decl
)
30346 /* In a non-template class, check conversions now. In a template,
30347 we'll wait and instantiate these as needed. */
30348 if (TREE_CODE (decl
) == PARM_DECL
)
30349 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
30350 tf_warning_or_error
);
30351 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
30352 parsed_arg
= error_mark_node
;
30354 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
30357 /* If the token stream has not been completely used up, then
30358 there was extra junk after the end of the default
30360 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30362 if (TREE_CODE (decl
) == PARM_DECL
)
30363 cp_parser_error (parser
, "expected %<,%>");
30365 cp_parser_error (parser
, "expected %<;%>");
30368 /* Revert to the main lexer. */
30369 cp_parser_pop_lexer (parser
);
30374 /* FIELD is a non-static data member with an initializer which we saved for
30375 later; parse it now. */
30378 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
30382 maybe_begin_member_template_processing (field
);
30384 push_unparsed_function_queues (parser
);
30385 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
30386 DECL_INITIAL (field
),
30388 pop_unparsed_function_queues (parser
);
30390 maybe_end_member_template_processing ();
30392 DECL_INITIAL (field
) = def
;
30395 /* FN is a FUNCTION_DECL which may contains a parameter with an
30396 unparsed DEFERRED_PARSE. Parse the default args now. This function
30397 assumes that the current scope is the scope in which the default
30398 argument should be processed. */
30401 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
30403 unsigned char saved_local_variables_forbidden_p
;
30404 tree parm
, parmdecl
;
30406 /* While we're parsing the default args, we might (due to the
30407 statement expression extension) encounter more classes. We want
30408 to handle them right away, but we don't want them getting mixed
30409 up with default args that are currently in the queue. */
30410 push_unparsed_function_queues (parser
);
30412 /* Local variable names (and the `this' keyword) may not appear
30413 in a default argument. */
30414 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
30415 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
30417 push_defarg_context (fn
);
30419 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
30420 parmdecl
= DECL_ARGUMENTS (fn
);
30421 parm
&& parm
!= void_list_node
;
30422 parm
= TREE_CHAIN (parm
),
30423 parmdecl
= DECL_CHAIN (parmdecl
))
30425 tree default_arg
= TREE_PURPOSE (parm
);
30427 vec
<tree
, va_gc
> *insts
;
30434 if (TREE_CODE (default_arg
) != DEFERRED_PARSE
)
30435 /* This can happen for a friend declaration for a function
30436 already declared with default arguments. */
30440 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
30442 TREE_VALUE (parm
));
30443 TREE_PURPOSE (parm
) = parsed_arg
;
30445 /* Update any instantiations we've already created. */
30446 for (insts
= DEFPARSE_INSTANTIATIONS (default_arg
), ix
= 0;
30447 vec_safe_iterate (insts
, ix
, ©
); ix
++)
30448 TREE_PURPOSE (copy
) = parsed_arg
;
30451 pop_defarg_context ();
30453 /* Make sure no default arg is missing. */
30454 check_default_args (fn
);
30456 /* Restore the state of local_variables_forbidden_p. */
30457 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
30459 /* Restore the queue. */
30460 pop_unparsed_function_queues (parser
);
30463 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
30465 sizeof ... ( identifier )
30467 where the 'sizeof' token has already been consumed. */
30470 cp_parser_sizeof_pack (cp_parser
*parser
)
30472 /* Consume the `...'. */
30473 cp_lexer_consume_token (parser
->lexer
);
30474 maybe_warn_variadic_templates ();
30476 matching_parens parens
;
30477 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
30479 parens
.consume_open (parser
);
30481 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
30482 "%<sizeof...%> argument must be surrounded by parentheses");
30484 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30485 tree name
= cp_parser_identifier (parser
);
30486 if (name
== error_mark_node
)
30487 return error_mark_node
;
30488 /* The name is not qualified. */
30489 parser
->scope
= NULL_TREE
;
30490 parser
->qualifying_scope
= NULL_TREE
;
30491 parser
->object_scope
= NULL_TREE
;
30492 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
30493 if (expr
== error_mark_node
)
30494 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
30496 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
30497 expr
= TREE_TYPE (expr
);
30498 else if (TREE_CODE (expr
) == CONST_DECL
)
30499 expr
= DECL_INITIAL (expr
);
30500 expr
= make_pack_expansion (expr
);
30501 PACK_EXPANSION_SIZEOF_P (expr
) = true;
30504 parens
.require_close (parser
);
30509 /* Parse the operand of `sizeof' (or a similar operator). Returns
30510 either a TYPE or an expression, depending on the form of the
30511 input. The KEYWORD indicates which kind of expression we have
30515 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
30517 tree expr
= NULL_TREE
;
30518 const char *saved_message
;
30519 const char *saved_message_arg
;
30520 bool saved_integral_constant_expression_p
;
30521 bool saved_non_integral_constant_expression_p
;
30523 /* If it's a `...', then we are computing the length of a parameter
30525 if (keyword
== RID_SIZEOF
30526 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30527 return cp_parser_sizeof_pack (parser
);
30529 /* Types cannot be defined in a `sizeof' expression. Save away the
30531 saved_message
= parser
->type_definition_forbidden_message
;
30532 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
30533 parser
->type_definition_forbidden_message
30534 = G_("types may not be defined in %qs expressions");
30535 parser
->type_definition_forbidden_message_arg
30536 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
30538 /* The restrictions on constant-expressions do not apply inside
30539 sizeof expressions. */
30540 saved_integral_constant_expression_p
30541 = parser
->integral_constant_expression_p
;
30542 saved_non_integral_constant_expression_p
30543 = parser
->non_integral_constant_expression_p
;
30544 parser
->integral_constant_expression_p
= false;
30546 /* Do not actually evaluate the expression. */
30547 ++cp_unevaluated_operand
;
30548 ++c_inhibit_evaluation_warnings
;
30549 /* If it's a `(', then we might be looking at the type-id
30551 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30553 tree type
= NULL_TREE
;
30555 tentative_firewall
firewall (parser
);
30557 /* We can't be sure yet whether we're looking at a type-id or an
30559 cp_parser_parse_tentatively (parser
);
30561 matching_parens parens
;
30562 parens
.consume_open (parser
);
30564 /* Note: as a GNU Extension, compound literals are considered
30565 postfix-expressions as they are in C99, so they are valid
30566 arguments to sizeof. See comment in cp_parser_cast_expression
30568 if (cp_parser_compound_literal_p (parser
))
30569 cp_parser_simulate_error (parser
);
30572 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
30573 parser
->in_type_id_in_expr_p
= true;
30574 /* Look for the type-id. */
30575 type
= cp_parser_type_id (parser
);
30576 /* Look for the closing `)'. */
30577 parens
.require_close (parser
);
30578 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
30581 /* If all went well, then we're done. */
30582 if (cp_parser_parse_definitely (parser
))
30586 /* Commit to the tentative_firewall so we get syntax errors. */
30587 cp_parser_commit_to_tentative_parse (parser
);
30589 expr
= cp_parser_unary_expression (parser
);
30593 expr
= cp_parser_unary_expression (parser
);
30595 /* Go back to evaluating expressions. */
30596 --cp_unevaluated_operand
;
30597 --c_inhibit_evaluation_warnings
;
30599 /* And restore the old one. */
30600 parser
->type_definition_forbidden_message
= saved_message
;
30601 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
30602 parser
->integral_constant_expression_p
30603 = saved_integral_constant_expression_p
;
30604 parser
->non_integral_constant_expression_p
30605 = saved_non_integral_constant_expression_p
;
30610 /* If the current declaration has no declarator, return true. */
30613 cp_parser_declares_only_class_p (cp_parser
*parser
)
30615 /* If the next token is a `;' or a `,' then there is no
30617 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30618 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
30621 /* Update the DECL_SPECS to reflect the storage class indicated by
30625 cp_parser_set_storage_class (cp_parser
*parser
,
30626 cp_decl_specifier_seq
*decl_specs
,
30630 cp_storage_class storage_class
;
30632 if (parser
->in_unbraced_linkage_specification_p
)
30634 error_at (token
->location
, "invalid use of %qD in linkage specification",
30635 ridpointers
[keyword
]);
30638 else if (decl_specs
->storage_class
!= sc_none
)
30640 decl_specs
->conflicting_specifiers_p
= true;
30644 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
30645 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
30646 && decl_specs
->gnu_thread_keyword_p
)
30648 pedwarn (decl_specs
->locations
[ds_thread
], 0,
30649 "%<__thread%> before %qD", ridpointers
[keyword
]);
30655 storage_class
= sc_auto
;
30658 storage_class
= sc_register
;
30661 storage_class
= sc_static
;
30664 storage_class
= sc_extern
;
30667 storage_class
= sc_mutable
;
30670 gcc_unreachable ();
30672 decl_specs
->storage_class
= storage_class
;
30673 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
30675 /* A storage class specifier cannot be applied alongside a typedef
30676 specifier. If there is a typedef specifier present then set
30677 conflicting_specifiers_p which will trigger an error later
30678 on in grokdeclarator. */
30679 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
30680 decl_specs
->conflicting_specifiers_p
= true;
30683 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
30684 is true, the type is a class or enum definition. */
30687 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
30690 bool type_definition_p
)
30692 decl_specs
->any_specifiers_p
= true;
30694 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30695 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30696 this is what happened. In system headers, we ignore these
30697 declarations so that G++ can work with system headers that are not
30699 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
30700 && !type_definition_p
30701 && (type_spec
== boolean_type_node
30702 || type_spec
== char8_type_node
30703 || type_spec
== char16_type_node
30704 || type_spec
== char32_type_node
30705 || type_spec
== wchar_type_node
)
30706 && (decl_specs
->type
30707 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
30708 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
30709 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
30710 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
30712 decl_specs
->redefined_builtin_type
= type_spec
;
30713 set_and_check_decl_spec_loc (decl_specs
,
30714 ds_redefined_builtin_type_spec
,
30716 if (!decl_specs
->type
)
30718 decl_specs
->type
= type_spec
;
30719 decl_specs
->type_definition_p
= false;
30720 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
30723 else if (decl_specs
->type
)
30724 decl_specs
->multiple_types_p
= true;
30727 decl_specs
->type
= type_spec
;
30728 decl_specs
->type_definition_p
= type_definition_p
;
30729 decl_specs
->redefined_builtin_type
= NULL_TREE
;
30730 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
30734 /* True iff TOKEN is the GNU keyword __thread. */
30737 token_is__thread (cp_token
*token
)
30739 gcc_assert (token
->keyword
== RID_THREAD
);
30740 return id_equal (token
->u
.value
, "__thread");
30743 /* Set the location for a declarator specifier and check if it is
30746 DECL_SPECS is the sequence of declarator specifiers onto which to
30749 DS is the single declarator specifier to set which location is to
30750 be set onto the existing sequence of declarators.
30752 LOCATION is the location for the declarator specifier to
30756 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
30757 cp_decl_spec ds
, cp_token
*token
)
30759 gcc_assert (ds
< ds_last
);
30761 if (decl_specs
== NULL
)
30764 location_t location
= token
->location
;
30766 if (decl_specs
->locations
[ds
] == 0)
30768 decl_specs
->locations
[ds
] = location
;
30769 if (ds
== ds_thread
)
30770 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
30776 if (decl_specs
->locations
[ds_long_long
] != 0)
30777 error_at (location
,
30778 "%<long long long%> is too long for GCC");
30781 decl_specs
->locations
[ds_long_long
] = location
;
30782 pedwarn_cxx98 (location
,
30784 "ISO C++ 1998 does not support %<long long%>");
30787 else if (ds
== ds_thread
)
30789 bool gnu
= token_is__thread (token
);
30790 gcc_rich_location
richloc (location
);
30791 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
30793 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
30794 error_at (&richloc
,
30795 "both %<__thread%> and %<thread_local%> specified");
30799 richloc
.add_fixit_remove ();
30800 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
30805 static const char *const decl_spec_names
[] = {
30824 gcc_rich_location
richloc (location
);
30825 richloc
.add_fixit_remove ();
30826 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
30831 /* Return true iff the declarator specifier DS is present in the
30832 sequence of declarator specifiers DECL_SPECS. */
30835 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
30838 gcc_assert (ds
< ds_last
);
30840 if (decl_specs
== NULL
)
30843 return decl_specs
->locations
[ds
] != 0;
30846 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30847 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30850 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
30852 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
30855 /* Issue an error message indicating that TOKEN_DESC was expected.
30856 If KEYWORD is true, it indicated this function is called by
30857 cp_parser_require_keword and the required token can only be
30858 a indicated keyword.
30860 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30861 within any error as the location of an "opening" token matching
30862 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30863 RT_CLOSE_PAREN). */
30866 cp_parser_required_error (cp_parser
*parser
,
30867 required_token token_desc
,
30869 location_t matching_location
)
30871 if (cp_parser_simulate_error (parser
))
30874 const char *gmsgid
= NULL
;
30875 switch (token_desc
)
30878 gmsgid
= G_("expected %<new%>");
30881 gmsgid
= G_("expected %<delete%>");
30884 gmsgid
= G_("expected %<return%>");
30887 gmsgid
= G_("expected %<while%>");
30890 gmsgid
= G_("expected %<extern%>");
30892 case RT_STATIC_ASSERT
:
30893 gmsgid
= G_("expected %<static_assert%>");
30896 gmsgid
= G_("expected %<decltype%>");
30899 gmsgid
= G_("expected %<operator%>");
30902 gmsgid
= G_("expected %<class%>");
30905 gmsgid
= G_("expected %<template%>");
30908 gmsgid
= G_("expected %<namespace%>");
30911 gmsgid
= G_("expected %<using%>");
30914 gmsgid
= G_("expected %<asm%>");
30917 gmsgid
= G_("expected %<try%>");
30920 gmsgid
= G_("expected %<catch%>");
30923 gmsgid
= G_("expected %<throw%>");
30926 gmsgid
= G_("expected %<auto%>");
30929 gmsgid
= G_("expected %<__label__%>");
30932 gmsgid
= G_("expected %<@try%>");
30934 case RT_AT_SYNCHRONIZED
:
30935 gmsgid
= G_("expected %<@synchronized%>");
30938 gmsgid
= G_("expected %<@throw%>");
30940 case RT_TRANSACTION_ATOMIC
:
30941 gmsgid
= G_("expected %<__transaction_atomic%>");
30943 case RT_TRANSACTION_RELAXED
:
30944 gmsgid
= G_("expected %<__transaction_relaxed%>");
30947 gmsgid
= G_("expected %<co_yield%>");
30953 if (!gmsgid
&& !keyword
)
30955 switch (token_desc
)
30958 gmsgid
= G_("expected %<;%>");
30960 case RT_OPEN_PAREN
:
30961 gmsgid
= G_("expected %<(%>");
30963 case RT_CLOSE_BRACE
:
30964 gmsgid
= G_("expected %<}%>");
30966 case RT_OPEN_BRACE
:
30967 gmsgid
= G_("expected %<{%>");
30969 case RT_CLOSE_SQUARE
:
30970 gmsgid
= G_("expected %<]%>");
30972 case RT_OPEN_SQUARE
:
30973 gmsgid
= G_("expected %<[%>");
30976 gmsgid
= G_("expected %<,%>");
30979 gmsgid
= G_("expected %<::%>");
30982 gmsgid
= G_("expected %<<%>");
30985 gmsgid
= G_("expected %<>%>");
30988 gmsgid
= G_("expected %<=%>");
30991 gmsgid
= G_("expected %<...%>");
30994 gmsgid
= G_("expected %<*%>");
30997 gmsgid
= G_("expected %<~%>");
31000 gmsgid
= G_("expected %<:%>");
31002 case RT_COLON_SCOPE
:
31003 gmsgid
= G_("expected %<:%> or %<::%>");
31005 case RT_CLOSE_PAREN
:
31006 gmsgid
= G_("expected %<)%>");
31008 case RT_COMMA_CLOSE_PAREN
:
31009 gmsgid
= G_("expected %<,%> or %<)%>");
31011 case RT_PRAGMA_EOL
:
31012 gmsgid
= G_("expected end of line");
31015 gmsgid
= G_("expected identifier");
31018 gmsgid
= G_("expected selection-statement");
31021 gmsgid
= G_("expected iteration-statement");
31024 gmsgid
= G_("expected jump-statement");
31027 gmsgid
= G_("expected class-key");
31029 case RT_CLASS_TYPENAME_TEMPLATE
:
31030 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
31033 gcc_unreachable ();
31038 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
31042 /* If the next token is of the indicated TYPE, consume it. Otherwise,
31043 issue an error message indicating that TOKEN_DESC was expected.
31045 Returns the token consumed, if the token had the appropriate type.
31046 Otherwise, returns NULL.
31048 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31049 within any error as the location of an "opening" token matching
31050 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31051 RT_CLOSE_PAREN). */
31054 cp_parser_require (cp_parser
* parser
,
31055 enum cpp_ttype type
,
31056 required_token token_desc
,
31057 location_t matching_location
)
31059 if (cp_lexer_next_token_is (parser
->lexer
, type
))
31060 return cp_lexer_consume_token (parser
->lexer
);
31063 /* Output the MESSAGE -- unless we're parsing tentatively. */
31064 if (!cp_parser_simulate_error (parser
))
31065 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
31066 matching_location
);
31071 /* An error message is produced if the next token is not '>'.
31072 All further tokens are skipped until the desired token is
31073 found or '{', '}', ';' or an unbalanced ')' or ']'. */
31076 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
31078 /* Current level of '< ... >'. */
31079 unsigned level
= 0;
31080 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
31081 unsigned nesting_depth
= 0;
31083 /* Are we ready, yet? If not, issue error message. */
31084 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
31087 /* Skip tokens until the desired token is found. */
31090 /* Peek at the next token. */
31091 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31094 if (!nesting_depth
)
31099 if (cxx_dialect
== cxx98
)
31100 /* C++0x views the `>>' operator as two `>' tokens, but
31103 else if (!nesting_depth
&& level
-- == 0)
31105 /* We've hit a `>>' where the first `>' closes the
31106 template argument list, and the second `>' is
31107 spurious. Just consume the `>>' and stop; we've
31108 already produced at least one error. */
31109 cp_lexer_consume_token (parser
->lexer
);
31112 /* Fall through for C++0x, so we handle the second `>' in
31114 gcc_fallthrough ();
31117 if (!nesting_depth
&& level
-- == 0)
31119 /* We've reached the token we want, consume it and stop. */
31120 cp_lexer_consume_token (parser
->lexer
);
31125 case CPP_OPEN_PAREN
:
31126 case CPP_OPEN_SQUARE
:
31130 case CPP_CLOSE_PAREN
:
31131 case CPP_CLOSE_SQUARE
:
31132 if (nesting_depth
-- == 0)
31137 case CPP_PRAGMA_EOL
:
31138 case CPP_SEMICOLON
:
31139 case CPP_OPEN_BRACE
:
31140 case CPP_CLOSE_BRACE
:
31141 /* The '>' was probably forgotten, don't look further. */
31148 /* Consume this token. */
31149 cp_lexer_consume_token (parser
->lexer
);
31153 /* If the next token is the indicated keyword, consume it. Otherwise,
31154 issue an error message indicating that TOKEN_DESC was expected.
31156 Returns the token consumed, if the token had the appropriate type.
31157 Otherwise, returns NULL. */
31160 cp_parser_require_keyword (cp_parser
* parser
,
31162 required_token token_desc
)
31164 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
31166 if (token
&& token
->keyword
!= keyword
)
31168 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
31176 /* Returns TRUE iff TOKEN is a token that can begin the body of a
31177 function-definition. */
31180 cp_parser_token_starts_function_definition_p (cp_token
* token
)
31182 return (/* An ordinary function-body begins with an `{'. */
31183 token
->type
== CPP_OPEN_BRACE
31184 /* A ctor-initializer begins with a `:'. */
31185 || token
->type
== CPP_COLON
31186 /* A function-try-block begins with `try'. */
31187 || token
->keyword
== RID_TRY
31188 /* A function-transaction-block begins with `__transaction_atomic'
31189 or `__transaction_relaxed'. */
31190 || token
->keyword
== RID_TRANSACTION_ATOMIC
31191 || token
->keyword
== RID_TRANSACTION_RELAXED
31192 /* The named return value extension begins with `return'. */
31193 || token
->keyword
== RID_RETURN
);
31196 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
31200 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
31204 token
= cp_lexer_peek_token (parser
->lexer
);
31205 return (token
->type
== CPP_OPEN_BRACE
31206 || (token
->type
== CPP_COLON
31207 && !parser
->colon_doesnt_start_class_def_p
));
31210 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
31211 C++0x) ending a template-argument. */
31214 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
31218 token
= cp_lexer_peek_token (parser
->lexer
);
31219 return (token
->type
== CPP_COMMA
31220 || token
->type
== CPP_GREATER
31221 || token
->type
== CPP_ELLIPSIS
31222 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
31225 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
31226 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
31229 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
31234 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
31235 if (token
->type
== CPP_LESS
)
31237 /* Check for the sequence `<::' in the original code. It would be lexed as
31238 `[:', where `[' is a digraph, and there is no whitespace before
31240 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
31243 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
31244 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
31250 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31251 or none_type otherwise. */
31253 static enum tag_types
31254 cp_parser_token_is_class_key (cp_token
* token
)
31256 switch (token
->keyword
)
31261 return record_type
;
31270 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31271 or none_type otherwise or if the token is null. */
31273 static enum tag_types
31274 cp_parser_token_is_type_parameter_key (cp_token
* token
)
31279 switch (token
->keyword
)
31284 return typename_type
;
31291 /* Diagnose redundant enum-keys. */
31294 cp_parser_maybe_warn_enum_key (cp_parser
*parser
, location_t key_loc
,
31295 tree type
, rid scoped_key
)
31297 if (!warn_redundant_tags
)
31300 tree type_decl
= TYPE_MAIN_DECL (type
);
31301 tree name
= DECL_NAME (type_decl
);
31302 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31303 push_deferring_access_checks (dk_no_check
);
31304 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
31305 pop_deferring_access_checks ();
31307 /* The enum-key is redundant for uses of the TYPE that are not
31308 declarations and for which name lookup returns just the type
31310 if (decl
!= type_decl
)
31313 if (scoped_key
!= RID_CLASS
31314 && scoped_key
!= RID_STRUCT
31315 && current_lang_name
!= lang_name_cplusplus
31316 && current_namespace
== global_namespace
)
31318 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31319 enum tag in shared C/C++ code in files (such as headers) included
31320 in the main source file. */
31321 const line_map_ordinary
*map
= NULL
;
31322 linemap_resolve_location (line_table
, key_loc
,
31323 LRK_MACRO_DEFINITION_LOCATION
,
31325 if (!MAIN_FILE_P (map
))
31329 gcc_rich_location
richloc (key_loc
);
31330 richloc
.add_fixit_remove (key_loc
);
31331 warning_at (&richloc
, OPT_Wredundant_tags
,
31332 "redundant enum-key %<enum%s%> in reference to %q#T",
31333 (scoped_key
== RID_CLASS
? " class"
31334 : scoped_key
== RID_STRUCT
? " struct" : ""), type
);
31337 /* Describes the set of declarations of a struct, class, or class template
31338 or its specializations. Used for -Wmismatched-tags. */
31340 class class_decl_loc_t
31344 class_decl_loc_t ()
31345 : locvec (), idxdef (), def_class_key ()
31350 /* Constructs an object for a single declaration of a class with
31351 CLASS_KEY at the current location in the current function (or
31352 at another scope). KEY_REDUNDANT is true if the class-key may
31353 be omitted in the current context without an ambiguity with
31354 another symbol with the same name.
31355 DEF_P is true for a class declaration that is a definition.
31356 CURLOC is the associated location. */
31357 class_decl_loc_t (tag_types class_key
, bool key_redundant
, bool def_p
,
31358 location_t curloc
= input_location
)
31359 : locvec (), idxdef (def_p
? 0 : UINT_MAX
), def_class_key (class_key
)
31362 class_key_loc_t
ckl (current_function_decl
, curloc
, class_key
,
31364 locvec
.quick_push (ckl
);
31367 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31368 isn't safely copyable and assignable and doesn't release storage
31370 class_decl_loc_t (const class_decl_loc_t
&rhs
)
31371 : locvec (rhs
.locvec
.copy ()), idxdef (rhs
.idxdef
),
31372 def_class_key (rhs
.def_class_key
)
31375 class_decl_loc_t
& operator= (const class_decl_loc_t
&rhs
)
31380 locvec
= rhs
.locvec
.copy ();
31381 idxdef
= rhs
.idxdef
;
31382 def_class_key
= rhs
.def_class_key
;
31386 ~class_decl_loc_t ()
31391 /* Issues -Wmismatched-tags for a single class. */
31392 void diag_mismatched_tags (tree
);
31394 /* Issues -Wmismatched-tags for all classes. */
31395 static void diag_mismatched_tags ();
31397 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31398 redundant tags (if -Wredundant-tags is enabled). */
31399 static void add (cp_parser
*, location_t
, tag_types
, tree
, bool, bool);
31401 /* Either adds this decl to the collection of class decls
31402 or diagnoses it, whichever is appropriate. */
31403 void add_or_diag_mismatched_tag (tree
, tag_types
, bool, bool);
31407 tree
function (unsigned i
) const
31409 return locvec
[i
].func
;
31412 location_t
location (unsigned i
) const
31414 return locvec
[i
].loc
;
31417 bool key_redundant (unsigned i
) const
31419 return locvec
[i
].key_redundant
;
31422 tag_types
class_key (unsigned i
) const
31424 return locvec
[i
].class_key
;
31427 /* True if a definition for the class has been seen. */
31428 bool def_p () const
31430 return idxdef
< locvec
.length ();
31433 /* The location of a single mention of a class type with the given
31435 struct class_key_loc_t
31437 class_key_loc_t (tree func
, location_t loc
, tag_types key
, bool redundant
)
31438 : func (func
), loc (loc
), class_key (key
), key_redundant (redundant
)
31441 /* The function the type is mentioned in. */
31443 /* The exact location. */
31445 /* The class-key used in the mention of the type. */
31446 tag_types class_key
;
31447 /* True when the class-key could be omitted at this location
31448 without an ambiguity with another symbol of the same name. */
31449 bool key_redundant
;
31451 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
31452 vec
<class_key_loc_t
> locvec
;
31453 /* LOCVEC index of the definition or UINT_MAX if none exists. */
31455 /* The class-key the class was last declared with or none_type when
31456 it has been declared with a mismatched key. */
31457 tag_types def_class_key
;
31459 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
31460 description above. */
31461 typedef hash_map
<tree_decl_hash
, class_decl_loc_t
> class_to_loc_map_t
;
31462 static class_to_loc_map_t class2loc
;
31465 class_decl_loc_t::class_to_loc_map_t
class_decl_loc_t::class2loc
;
31467 /* Issue an error message if the CLASS_KEY does not match the TYPE.
31468 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31469 is set for a declaration of class TYPE and clear for a reference to
31470 it that is not a declaration of it. */
31473 cp_parser_check_class_key (cp_parser
*parser
, location_t key_loc
,
31474 tag_types class_key
, tree type
, bool def_p
,
31477 if (type
== error_mark_node
)
31480 bool seen_as_union
= TREE_CODE (type
) == UNION_TYPE
;
31481 if (seen_as_union
!= (class_key
== union_type
))
31483 if (permerror (input_location
, "%qs tag used in naming %q#T",
31484 class_key
== union_type
? "union"
31485 : class_key
== record_type
? "struct" : "class",
31487 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
31488 "%q#T was previously declared here", type
);
31492 if (!warn_mismatched_tags
&& !warn_redundant_tags
)
31495 /* Only consider the true class-keys below and ignore typename_type,
31496 etc. that are not C++ class-keys. */
31497 if (class_key
!= class_type
31498 && class_key
!= record_type
31499 && class_key
!= union_type
)
31502 class_decl_loc_t::add (parser
, key_loc
, class_key
, type
, def_p
, decl_p
);
31505 /* Returns the template or specialization of one to which the RECORD_TYPE
31506 TYPE corresponds. */
31509 specialization_of (tree type
)
31513 /* Determine the template or its partial specialization to which TYPE
31515 if (tree spec
= most_specialized_partial_spec (type
, tf_none
))
31516 if (spec
!= error_mark_node
)
31517 ret
= TREE_TYPE (TREE_VALUE (spec
));
31520 ret
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type
);
31522 return TYPE_MAIN_DECL (ret
);
31526 /* Adds the class TYPE to the collection of class decls and diagnoses
31527 redundant tags (if -Wredundant-tags is enabled).
31528 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31529 is set for a (likely, based on syntactic context) declaration of class
31530 TYPE and clear for a reference to it that is not a declaration of it. */
31533 class_decl_loc_t::add (cp_parser
*parser
, location_t key_loc
,
31534 tag_types class_key
, tree type
, bool def_p
, bool decl_p
)
31536 tree type_decl
= TYPE_MAIN_DECL (type
);
31537 tree name
= DECL_NAME (type_decl
);
31538 /* Look up the NAME to see if it unambiguously refers to the TYPE
31539 and set KEY_REDUNDANT if so. */
31540 push_deferring_access_checks (dk_no_check
);
31541 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
31542 pop_deferring_access_checks ();
31544 /* The class-key is redundant for uses of the CLASS_TYPE that are
31545 neither definitions of it nor declarations, and for which name
31546 lookup returns just the type itself. */
31547 bool key_redundant
= (!def_p
&& !decl_p
31548 && (decl
== type_decl
31549 || TREE_CODE (decl
) == TEMPLATE_DECL
31550 || TYPE_BEING_DEFINED (type
)));
31553 && class_key
!= class_type
31554 && current_lang_name
!= lang_name_cplusplus
31555 && current_namespace
== global_namespace
)
31557 /* Avoid issuing the diagnostic for apparently redundant struct
31558 and union class-keys in shared C/C++ code in files (such as
31559 headers) included in the main source file. */
31560 const line_map_ordinary
*map
= NULL
;
31561 linemap_resolve_location (line_table
, key_loc
,
31562 LRK_MACRO_DEFINITION_LOCATION
,
31564 if (!MAIN_FILE_P (map
))
31565 key_redundant
= false;
31568 /* Set if a declaration of TYPE has previously been seen or if it must
31569 exist in a precompiled header. */
31571 class_decl_loc_t
*rdl
= &class2loc
.get_or_insert (type_decl
, &exist
);
31574 tree type
= TREE_TYPE (type_decl
);
31575 if (def_p
|| !COMPLETE_TYPE_P (type
))
31577 /* TYPE_DECL is the first declaration or definition of the type
31578 (outside precompiled headers -- see below). Just create
31579 a new entry for it and return unless it's a declaration
31580 involving a template that may need to be diagnosed by
31581 -Wredundant-tags. */
31582 *rdl
= class_decl_loc_t (class_key
, false, def_p
);
31583 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
31588 /* TYPE was previously defined in some unknown precompiled hdeader.
31589 Simply add a record of its definition at an unknown location and
31590 proceed below to add a reference to it at the current location.
31591 (Declarations in precompiled headers that are not definitions
31594 = CLASSTYPE_DECLARED_CLASS (type
) ? class_type
: record_type
;
31595 location_t def_loc
= DECL_SOURCE_LOCATION (type_decl
);
31596 *rdl
= class_decl_loc_t (def_key
, false, true, def_loc
);
31601 /* A prior declaration of TYPE_DECL has been seen. */
31605 gcc_rich_location
richloc (key_loc
);
31606 richloc
.add_fixit_remove (key_loc
);
31607 warning_at (&richloc
, OPT_Wredundant_tags
,
31608 "redundant class-key %qs in reference to %q#T",
31609 class_key
== union_type
? "union"
31610 : class_key
== record_type
? "struct" : "class",
31615 /* Do nothing if this is the first declaration of the type. */
31618 if (rdl
->idxdef
!= UINT_MAX
&& rdl
->def_class_key
== class_key
)
31619 /* Do nothing if the class-key in this declaration matches
31623 rdl
->add_or_diag_mismatched_tag (type_decl
, class_key
, key_redundant
,
31627 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
31628 of class decls or diagnoses it, whichever is appropriate. */
31631 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl
,
31632 tag_types class_key
,
31636 /* Reset the CLASS_KEY associated with this type on mismatch.
31637 This is an optimization that lets the diagnostic code skip
31638 over classes that use the same class-key in all declarations. */
31639 if (def_class_key
!= class_key
)
31640 def_class_key
= none_type
;
31642 /* Set IDXDEF to the index of the vector corresponding to
31645 idxdef
= locvec
.length ();
31647 /* Append a record of this declaration to the vector. */
31648 class_key_loc_t
ckl (current_function_decl
, input_location
, class_key
,
31650 locvec
.safe_push (ckl
);
31652 if (idxdef
== UINT_MAX
)
31655 /* As a space optimization diagnose declarations of a class
31656 whose definition has been seen and purge the LOCVEC of
31657 all entries except the definition. */
31658 diag_mismatched_tags (type_decl
);
31661 class_decl_loc_t::class_key_loc_t ent
= locvec
[idxdef
];
31663 locvec
.reserve (2);
31664 locvec
.safe_push (ent
);
31668 /* Pop the entry pushed above for this declaration. */
31672 /* Issues -Wmismatched-tags for a single class. */
31675 class_decl_loc_t::diag_mismatched_tags (tree type_decl
)
31677 if (!warn_mismatched_tags
)
31680 /* Number of uses of the class. */
31681 const unsigned ndecls
= locvec
.length ();
31683 /* The class (or template) declaration guiding the decisions about
31684 the diagnostic. For ordinary classes it's the same as THIS. For
31685 uses of instantiations of templates other than their declarations
31686 it points to the record for the declaration of the corresponding
31687 primary template or partial specialization. */
31688 class_decl_loc_t
*cdlguide
= this;
31690 tree type
= TREE_TYPE (type_decl
);
31691 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
))
31693 /* For implicit instantiations of a primary template look up
31694 the primary or partial specialization and use it as
31695 the expected class-key rather than using the class-key of
31696 the first reference to the instantiation. The primary must
31697 be (and inevitably is) at index zero. */
31698 tree spec
= specialization_of (type
);
31699 cdlguide
= class2loc
.get (spec
);
31700 gcc_assert (cdlguide
!= NULL
);
31704 /* Skip declarations that consistently use the same class-key. */
31705 if (def_class_key
!= none_type
)
31709 /* Set if a definition for the class has been seen. */
31710 const bool def_p
= cdlguide
->def_p ();
31712 /* The index of the declaration whose class-key this declaration
31713 is expected to match. It's either the class-key of the class
31714 definition if one exists or the first declaration otherwise. */
31715 const unsigned idxguide
= def_p
? cdlguide
->idxdef
: 0;
31717 /* The class-key the class is expected to be declared with: it's
31718 either the key used in its definition or the first declaration
31719 if no definition has been provided.
31720 For implicit instantiations of a primary template it's
31721 the class-key used to declare the primary with. The primary
31722 must be at index zero. */
31723 const tag_types xpect_key
= cdlguide
->class_key (idxguide
);
31726 /* Advance IDX to the first declaration that either is not
31727 a definition or that doesn't match the first declaration
31728 if no definition is provided. */
31729 while (class_key (idx
) == xpect_key
)
31730 if (++idx
== ndecls
)
31733 /* Save the current function before changing it below. */
31734 tree save_func
= current_function_decl
;
31735 /* Set the function declaration to print in diagnostic context. */
31736 current_function_decl
= function (idx
);
31738 const char *xmatchkstr
= xpect_key
== record_type
? "class" : "struct";
31739 const char *xpectkstr
= xpect_key
== record_type
? "struct" : "class";
31741 location_t loc
= location (idx
);
31742 bool key_redundant_p
= key_redundant (idx
);
31743 auto_diagnostic_group d
;
31744 /* Issue a warning for the first mismatched declaration.
31745 Avoid using "%#qT" since the class-key for the same type will
31746 be the same regardless of which one was used in the declaraion. */
31747 if (warning_at (loc
, OPT_Wmismatched_tags
,
31748 "%qT declared with a mismatched class-key %qs",
31749 type_decl
, xmatchkstr
))
31751 /* Suggest how to avoid the warning for each instance since
31752 the guidance may be different depending on context. */
31755 ? G_("remove the class-key or replace it with %qs")
31756 : G_("replace the class-key with %qs")),
31759 /* Also point to the first declaration or definition that guided
31760 the decision to issue the warning above. */
31761 inform (cdlguide
->location (idxguide
),
31763 ? G_("%qT defined as %qs here")
31764 : G_("%qT first declared as %qs here")),
31765 type_decl
, xpectkstr
);
31768 /* Issue warnings for the remaining inconsistent declarations. */
31769 for (unsigned i
= idx
+ 1; i
!= ndecls
; ++i
)
31771 tag_types clskey
= class_key (i
);
31772 /* Skip over the declarations that match either the definition
31773 if one was provided or the first declaration. */
31774 if (clskey
== xpect_key
)
31777 loc
= location (i
);
31778 key_redundant_p
= key_redundant (i
);
31779 /* Set the function declaration to print in diagnostic context. */
31780 current_function_decl
= function (i
);
31781 if (warning_at (loc
, OPT_Wmismatched_tags
,
31782 "%qT declared with a mismatched class-key %qs",
31783 type_decl
, xmatchkstr
))
31784 /* Suggest how to avoid the warning for each instance since
31785 the guidance may be different depending on context. */
31788 ? G_("remove the class-key or replace it with %qs")
31789 : G_("replace the class-key with %qs")),
31793 /* Restore the current function in case it was replaced above. */
31794 current_function_decl
= save_func
;
31797 /* Issues -Wmismatched-tags for all classes. Called at the end
31798 of processing a translation unit, after declarations of all class
31799 types and their uses have been recorded. */
31802 class_decl_loc_t::diag_mismatched_tags ()
31804 /* CLASS2LOC should be empty if both -Wmismatched-tags and
31805 -Wredundant-tags are disabled. */
31806 gcc_assert (warn_mismatched_tags
31807 || warn_redundant_tags
31808 || class2loc
.is_empty ());
31810 /* Save the current function before changing on return. It should
31811 be null at this point. */
31812 temp_override
<tree
> cleanup (current_function_decl
);
31814 if (warn_mismatched_tags
)
31816 /* Iterate over the collected class/struct/template declarations. */
31817 typedef class_to_loc_map_t::iterator iter_t
;
31818 for (iter_t it
= class2loc
.begin (); it
!= class2loc
.end (); ++it
)
31820 tree type_decl
= (*it
).first
;
31821 class_decl_loc_t
&recloc
= (*it
).second
;
31822 recloc
.diag_mismatched_tags (type_decl
);
31826 class2loc
.empty ();
31829 /* Issue an error message if DECL is redeclared with different
31830 access than its original declaration [class.access.spec/3].
31831 This applies to nested classes, nested class templates and
31832 enumerations [class.mem/1]. */
31835 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
31838 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
31839 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
31842 if ((TREE_PRIVATE (decl
)
31843 != (current_access_specifier
== access_private_node
))
31844 || (TREE_PROTECTED (decl
)
31845 != (current_access_specifier
== access_protected_node
)))
31846 error_at (location
, "%qD redeclared with different access", decl
);
31849 /* Look for the `template' keyword, as a syntactic disambiguator.
31850 Return TRUE iff it is present, in which case it will be
31854 cp_parser_optional_template_keyword (cp_parser
*parser
)
31856 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
31858 /* In C++98 the `template' keyword can only be used within templates;
31859 outside templates the parser can always figure out what is a
31860 template and what is not. In C++11, per the resolution of DR 468,
31861 `template' is allowed in cases where it is not strictly necessary. */
31862 if (!processing_template_decl
31863 && pedantic
&& cxx_dialect
== cxx98
)
31865 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31866 pedwarn (token
->location
, OPT_Wpedantic
,
31867 "in C++98 %<template%> (as a disambiguator) is only "
31868 "allowed within templates");
31869 /* If this part of the token stream is rescanned, the same
31870 error message would be generated. So, we purge the token
31871 from the stream. */
31872 cp_lexer_purge_token (parser
->lexer
);
31877 /* Consume the `template' keyword. */
31878 cp_lexer_consume_token (parser
->lexer
);
31885 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
31886 set PARSER->SCOPE, and perform other related actions. */
31889 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
31891 struct tree_check
*check_value
;
31893 /* Get the stored value. */
31894 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
31895 /* Set the scope from the stored value. */
31896 parser
->scope
= saved_checks_value (check_value
);
31897 parser
->qualifying_scope
= check_value
->qualifying_scope
;
31898 parser
->object_scope
= NULL_TREE
;
31901 /* Consume tokens up through a non-nested END token. Returns TRUE if we
31902 encounter the end of a block before what we were looking for. */
31905 cp_parser_cache_group (cp_parser
*parser
,
31906 enum cpp_ttype end
,
31911 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31913 /* Abort a parenthesized expression if we encounter a semicolon. */
31914 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
31915 && token
->type
== CPP_SEMICOLON
)
31917 /* If we've reached the end of the file, stop. */
31918 if (token
->type
== CPP_EOF
31919 || (end
!= CPP_PRAGMA_EOL
31920 && token
->type
== CPP_PRAGMA_EOL
))
31922 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
31923 /* We've hit the end of an enclosing block, so there's been some
31924 kind of syntax error. */
31927 /* Consume the token. */
31928 cp_lexer_consume_token (parser
->lexer
);
31929 /* See if it starts a new group. */
31930 if (token
->type
== CPP_OPEN_BRACE
)
31932 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
31933 /* In theory this should probably check end == '}', but
31934 cp_parser_save_member_function_body needs it to exit
31935 after either '}' or ')' when called with ')'. */
31939 else if (token
->type
== CPP_OPEN_PAREN
)
31941 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
31942 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
31945 else if (token
->type
== CPP_PRAGMA
)
31946 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
31947 else if (token
->type
== end
)
31952 /* Like above, for caching a default argument or NSDMI. Both of these are
31953 terminated by a non-nested comma, but it can be unclear whether or not a
31954 comma is nested in a template argument list unless we do more parsing.
31955 In order to handle this ambiguity, when we encounter a ',' after a '<'
31956 we try to parse what follows as a parameter-declaration-list (in the
31957 case of a default argument) or a member-declarator (in the case of an
31958 NSDMI). If that succeeds, then we stop caching. */
31961 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
31963 unsigned depth
= 0;
31964 int maybe_template_id
= 0;
31965 cp_token
*first_token
;
31967 tree default_argument
;
31969 /* Add tokens until we have processed the entire default
31970 argument. We add the range [first_token, token). */
31971 first_token
= cp_lexer_peek_token (parser
->lexer
);
31972 if (first_token
->type
== CPP_OPEN_BRACE
)
31974 /* For list-initialization, this is straightforward. */
31975 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
31976 token
= cp_lexer_peek_token (parser
->lexer
);
31982 /* Peek at the next token. */
31983 token
= cp_lexer_peek_token (parser
->lexer
);
31984 /* What we do depends on what token we have. */
31985 switch (token
->type
)
31987 /* In valid code, a default argument must be
31988 immediately followed by a `,' `)', or `...'. */
31990 if (depth
== 0 && maybe_template_id
)
31992 /* If we've seen a '<', we might be in a
31993 template-argument-list. Until Core issue 325 is
31994 resolved, we don't know how this situation ought
31995 to be handled, so try to DTRT. We check whether
31996 what comes after the comma is a valid parameter
31997 declaration list. If it is, then the comma ends
31998 the default argument; otherwise the default
31999 argument continues. */
32000 bool error
= false;
32003 /* Set ITALP so cp_parser_parameter_declaration_list
32004 doesn't decide to commit to this parse. */
32005 bool saved_italp
= parser
->in_template_argument_list_p
;
32006 parser
->in_template_argument_list_p
= true;
32008 cp_parser_parse_tentatively (parser
);
32012 /* Parse declarators until we reach a non-comma or
32013 somthing that cannot be an initializer.
32014 Just checking whether we're looking at a single
32015 declarator is insufficient. Consider:
32016 int var = tuple<T,U>::x;
32017 The template parameter 'U' looks exactly like a
32021 int ctor_dtor_or_conv_p
;
32022 cp_lexer_consume_token (parser
->lexer
);
32023 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
32024 CP_PARSER_FLAGS_NONE
,
32025 &ctor_dtor_or_conv_p
,
32026 /*parenthesized_p=*/NULL
,
32028 /*friend_p=*/false,
32029 /*static_p=*/false);
32030 peek
= cp_lexer_peek_token (parser
->lexer
);
32031 if (cp_parser_error_occurred (parser
))
32034 while (peek
->type
== CPP_COMMA
);
32035 /* If we met an '=' or ';' then the original comma
32036 was the end of the NSDMI. Otherwise assume
32037 we're still in the NSDMI. */
32038 error
= (peek
->type
!= CPP_EQ
32039 && peek
->type
!= CPP_SEMICOLON
);
32043 cp_lexer_consume_token (parser
->lexer
);
32044 begin_scope (sk_function_parms
, NULL_TREE
);
32045 tree t
= cp_parser_parameter_declaration_list
32046 (parser
, CP_PARSER_FLAGS_NONE
);
32047 if (t
== error_mark_node
)
32049 pop_bindings_and_leave_scope ();
32051 if (!cp_parser_error_occurred (parser
) && !error
)
32053 cp_parser_abort_tentative_parse (parser
);
32055 parser
->in_template_argument_list_p
= saved_italp
;
32059 case CPP_CLOSE_PAREN
:
32061 /* If we run into a non-nested `;', `}', or `]',
32062 then the code is invalid -- but the default
32063 argument is certainly over. */
32064 case CPP_SEMICOLON
:
32065 case CPP_CLOSE_BRACE
:
32066 case CPP_CLOSE_SQUARE
:
32068 /* Handle correctly int n = sizeof ... ( p ); */
32069 && token
->type
!= CPP_ELLIPSIS
)
32071 /* Update DEPTH, if necessary. */
32072 else if (token
->type
== CPP_CLOSE_PAREN
32073 || token
->type
== CPP_CLOSE_BRACE
32074 || token
->type
== CPP_CLOSE_SQUARE
)
32078 case CPP_OPEN_PAREN
:
32079 case CPP_OPEN_SQUARE
:
32080 case CPP_OPEN_BRACE
:
32086 /* This might be the comparison operator, or it might
32087 start a template argument list. */
32088 ++maybe_template_id
;
32092 if (cxx_dialect
== cxx98
)
32094 /* Fall through for C++0x, which treats the `>>'
32095 operator like two `>' tokens in certain
32097 gcc_fallthrough ();
32102 /* This might be an operator, or it might close a
32103 template argument list. But if a previous '<'
32104 started a template argument list, this will have
32105 closed it, so we can't be in one anymore. */
32106 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
32107 if (maybe_template_id
< 0)
32108 maybe_template_id
= 0;
32112 /* If we run out of tokens, issue an error message. */
32114 case CPP_PRAGMA_EOL
:
32115 error_at (token
->location
, "file ends in default argument");
32116 return error_mark_node
;
32120 /* In these cases, we should look for template-ids.
32121 For example, if the default argument is
32122 `X<int, double>()', we need to do name lookup to
32123 figure out whether or not `X' is a template; if
32124 so, the `,' does not end the default argument.
32126 That is not yet done. */
32133 /* If we've reached the end, stop. */
32137 /* Add the token to the token block. */
32138 token
= cp_lexer_consume_token (parser
->lexer
);
32141 /* Create a DEFERRED_PARSE to represent the unparsed default
32143 default_argument
= make_node (DEFERRED_PARSE
);
32144 DEFPARSE_TOKENS (default_argument
)
32145 = cp_token_cache_new (first_token
, token
);
32146 DEFPARSE_INSTANTIATIONS (default_argument
) = NULL
;
32148 return default_argument
;
32151 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
32154 defparse_location (tree default_argument
)
32156 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_argument
);
32157 location_t start
= tokens
->first
->location
;
32158 location_t end
= tokens
->last
->location
;
32159 return make_location (start
, start
, end
);
32162 /* Begin parsing tentatively. We always save tokens while parsing
32163 tentatively so that if the tentative parsing fails we can restore the
32167 cp_parser_parse_tentatively (cp_parser
* parser
)
32169 /* Enter a new parsing context. */
32170 parser
->context
= cp_parser_context_new (parser
->context
);
32171 /* Begin saving tokens. */
32172 cp_lexer_save_tokens (parser
->lexer
);
32173 /* In order to avoid repetitive access control error messages,
32174 access checks are queued up until we are no longer parsing
32176 push_deferring_access_checks (dk_deferred
);
32179 /* Commit to the currently active tentative parse. */
32182 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
32184 cp_parser_context
*context
;
32187 /* Mark all of the levels as committed. */
32188 lexer
= parser
->lexer
;
32189 for (context
= parser
->context
; context
->next
; context
= context
->next
)
32191 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
32193 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
32194 while (!cp_lexer_saving_tokens (lexer
))
32195 lexer
= lexer
->next
;
32196 cp_lexer_commit_tokens (lexer
);
32200 /* Commit to the topmost currently active tentative parse.
32202 Note that this function shouldn't be called when there are
32203 irreversible side-effects while in a tentative state. For
32204 example, we shouldn't create a permanent entry in the symbol
32205 table, or issue an error message that might not apply if the
32206 tentative parse is aborted. */
32209 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
32211 cp_parser_context
*context
= parser
->context
;
32212 cp_lexer
*lexer
= parser
->lexer
;
32216 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
32218 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
32220 while (!cp_lexer_saving_tokens (lexer
))
32221 lexer
= lexer
->next
;
32222 cp_lexer_commit_tokens (lexer
);
32226 /* Abort the currently active tentative parse. All consumed tokens
32227 will be rolled back, and no diagnostics will be issued. */
32230 cp_parser_abort_tentative_parse (cp_parser
* parser
)
32232 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
32233 || errorcount
> 0);
32234 cp_parser_simulate_error (parser
);
32235 /* Now, pretend that we want to see if the construct was
32236 successfully parsed. */
32237 cp_parser_parse_definitely (parser
);
32240 /* Stop parsing tentatively. If a parse error has occurred, restore the
32241 token stream. Otherwise, commit to the tokens we have consumed.
32242 Returns true if no error occurred; false otherwise. */
32245 cp_parser_parse_definitely (cp_parser
* parser
)
32247 bool error_occurred
;
32248 cp_parser_context
*context
;
32250 /* Remember whether or not an error occurred, since we are about to
32251 destroy that information. */
32252 error_occurred
= cp_parser_error_occurred (parser
);
32253 /* Remove the topmost context from the stack. */
32254 context
= parser
->context
;
32255 parser
->context
= context
->next
;
32256 /* If no parse errors occurred, commit to the tentative parse. */
32257 if (!error_occurred
)
32259 /* Commit to the tokens read tentatively, unless that was
32261 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
32262 cp_lexer_commit_tokens (parser
->lexer
);
32264 pop_to_parent_deferring_access_checks ();
32266 /* Otherwise, if errors occurred, roll back our state so that things
32267 are just as they were before we began the tentative parse. */
32270 cp_lexer_rollback_tokens (parser
->lexer
);
32271 pop_deferring_access_checks ();
32273 /* Add the context to the front of the free list. */
32274 context
->next
= cp_parser_context_free_list
;
32275 cp_parser_context_free_list
= context
;
32277 return !error_occurred
;
32280 /* Returns true if we are parsing tentatively and are not committed to
32281 this tentative parse. */
32284 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
32286 return (cp_parser_parsing_tentatively (parser
)
32287 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
32290 /* Returns nonzero iff an error has occurred during the most recent
32291 tentative parse. */
32294 cp_parser_error_occurred (cp_parser
* parser
)
32296 return (cp_parser_parsing_tentatively (parser
)
32297 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
32300 /* Returns nonzero if GNU extensions are allowed. */
32303 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
32305 return parser
->allow_gnu_extensions_p
;
32308 /* Objective-C++ Productions */
32311 /* Parse an Objective-C expression, which feeds into a primary-expression
32315 objc-message-expression
32316 objc-string-literal
32317 objc-encode-expression
32318 objc-protocol-expression
32319 objc-selector-expression
32321 Returns a tree representation of the expression. */
32324 cp_parser_objc_expression (cp_parser
* parser
)
32326 /* Try to figure out what kind of declaration is present. */
32327 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
32331 case CPP_OPEN_SQUARE
:
32332 return cp_parser_objc_message_expression (parser
);
32334 case CPP_OBJC_STRING
:
32335 kwd
= cp_lexer_consume_token (parser
->lexer
);
32336 return objc_build_string_object (kwd
->u
.value
);
32339 switch (kwd
->keyword
)
32341 case RID_AT_ENCODE
:
32342 return cp_parser_objc_encode_expression (parser
);
32344 case RID_AT_PROTOCOL
:
32345 return cp_parser_objc_protocol_expression (parser
);
32347 case RID_AT_SELECTOR
:
32348 return cp_parser_objc_selector_expression (parser
);
32355 error_at (kwd
->location
,
32356 "misplaced %<@%D%> Objective-C++ construct",
32358 cp_parser_skip_to_end_of_block_or_statement (parser
);
32361 return error_mark_node
;
32364 /* Parse an Objective-C message expression.
32366 objc-message-expression:
32367 [ objc-message-receiver objc-message-args ]
32369 Returns a representation of an Objective-C message. */
32372 cp_parser_objc_message_expression (cp_parser
* parser
)
32374 tree receiver
, messageargs
;
32376 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32377 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
32378 receiver
= cp_parser_objc_message_receiver (parser
);
32379 messageargs
= cp_parser_objc_message_args (parser
);
32380 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32381 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32383 tree result
= objc_build_message_expr (receiver
, messageargs
);
32385 /* Construct a location e.g.
32388 ranging from the '[' to the ']', with the caret at the start. */
32389 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
32390 protected_set_expr_location (result
, combined_loc
);
32395 /* Parse an objc-message-receiver.
32397 objc-message-receiver:
32399 simple-type-specifier
32401 Returns a representation of the type or expression. */
32404 cp_parser_objc_message_receiver (cp_parser
* parser
)
32408 /* An Objective-C message receiver may be either (1) a type
32409 or (2) an expression. */
32410 cp_parser_parse_tentatively (parser
);
32411 rcv
= cp_parser_expression (parser
);
32413 /* If that worked out, fine. */
32414 if (cp_parser_parse_definitely (parser
))
32417 cp_parser_parse_tentatively (parser
);
32418 rcv
= cp_parser_simple_type_specifier (parser
,
32419 /*decl_specs=*/NULL
,
32420 CP_PARSER_FLAGS_NONE
);
32422 if (cp_parser_parse_definitely (parser
))
32423 return objc_get_class_reference (rcv
);
32425 cp_parser_error (parser
, "objective-c++ message receiver expected");
32426 return error_mark_node
;
32429 /* Parse the arguments and selectors comprising an Objective-C message.
32434 objc-selector-args , objc-comma-args
32436 objc-selector-args:
32437 objc-selector [opt] : assignment-expression
32438 objc-selector-args objc-selector [opt] : assignment-expression
32441 assignment-expression
32442 objc-comma-args , assignment-expression
32444 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
32445 selector arguments and TREE_VALUE containing a list of comma
32449 cp_parser_objc_message_args (cp_parser
* parser
)
32451 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
32452 bool maybe_unary_selector_p
= true;
32453 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32455 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
32457 tree selector
= NULL_TREE
, arg
;
32459 if (token
->type
!= CPP_COLON
)
32460 selector
= cp_parser_objc_selector (parser
);
32462 /* Detect if we have a unary selector. */
32463 if (maybe_unary_selector_p
32464 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
32465 return build_tree_list (selector
, NULL_TREE
);
32467 maybe_unary_selector_p
= false;
32468 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32469 arg
= cp_parser_assignment_expression (parser
);
32472 = chainon (sel_args
,
32473 build_tree_list (selector
, arg
));
32475 token
= cp_lexer_peek_token (parser
->lexer
);
32478 /* Handle non-selector arguments, if any. */
32479 while (token
->type
== CPP_COMMA
)
32483 cp_lexer_consume_token (parser
->lexer
);
32484 arg
= cp_parser_assignment_expression (parser
);
32487 = chainon (addl_args
,
32488 build_tree_list (NULL_TREE
, arg
));
32490 token
= cp_lexer_peek_token (parser
->lexer
);
32493 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
32495 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
32496 return build_tree_list (error_mark_node
, error_mark_node
);
32499 return build_tree_list (sel_args
, addl_args
);
32502 /* Parse an Objective-C encode expression.
32504 objc-encode-expression:
32505 @encode objc-typename
32507 Returns an encoded representation of the type argument. */
32510 cp_parser_objc_encode_expression (cp_parser
* parser
)
32514 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32516 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
32517 matching_parens parens
;
32518 parens
.require_open (parser
);
32519 token
= cp_lexer_peek_token (parser
->lexer
);
32520 type
= complete_type (cp_parser_type_id (parser
));
32521 parens
.require_close (parser
);
32525 error_at (token
->location
,
32526 "%<@encode%> must specify a type as an argument");
32527 return error_mark_node
;
32530 /* This happens if we find @encode(T) (where T is a template
32531 typename or something dependent on a template typename) when
32532 parsing a template. In that case, we can't compile it
32533 immediately, but we rather create an AT_ENCODE_EXPR which will
32534 need to be instantiated when the template is used.
32536 if (dependent_type_p (type
))
32538 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
32539 TREE_READONLY (value
) = 1;
32544 /* Build a location of the form:
32547 with caret==start at the @ token, finishing at the close paren. */
32548 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
32550 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
32553 /* Parse an Objective-C @defs expression. */
32556 cp_parser_objc_defs_expression (cp_parser
*parser
)
32560 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
32561 matching_parens parens
;
32562 parens
.require_open (parser
);
32563 name
= cp_parser_identifier (parser
);
32564 parens
.require_close (parser
);
32566 return objc_get_class_ivars (name
);
32569 /* Parse an Objective-C protocol expression.
32571 objc-protocol-expression:
32572 @protocol ( identifier )
32574 Returns a representation of the protocol expression. */
32577 cp_parser_objc_protocol_expression (cp_parser
* parser
)
32580 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32582 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
32583 matching_parens parens
;
32584 parens
.require_open (parser
);
32585 proto
= cp_parser_identifier (parser
);
32586 parens
.require_close (parser
);
32588 /* Build a location of the form:
32591 with caret==start at the @ token, finishing at the close paren. */
32592 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
32593 tree result
= objc_build_protocol_expr (proto
);
32594 protected_set_expr_location (result
, combined_loc
);
32598 /* Parse an Objective-C selector expression.
32600 objc-selector-expression:
32601 @selector ( objc-method-signature )
32603 objc-method-signature:
32609 objc-selector-seq objc-selector :
32611 Returns a representation of the method selector. */
32614 cp_parser_objc_selector_expression (cp_parser
* parser
)
32616 tree sel_seq
= NULL_TREE
;
32617 bool maybe_unary_selector_p
= true;
32619 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32621 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
32622 matching_parens parens
;
32623 parens
.require_open (parser
);
32624 token
= cp_lexer_peek_token (parser
->lexer
);
32626 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
32627 || token
->type
== CPP_SCOPE
)
32629 tree selector
= NULL_TREE
;
32631 if (token
->type
!= CPP_COLON
32632 || token
->type
== CPP_SCOPE
)
32633 selector
= cp_parser_objc_selector (parser
);
32635 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
32636 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
32638 /* Detect if we have a unary selector. */
32639 if (maybe_unary_selector_p
)
32641 sel_seq
= selector
;
32642 goto finish_selector
;
32646 cp_parser_error (parser
, "expected %<:%>");
32649 maybe_unary_selector_p
= false;
32650 token
= cp_lexer_consume_token (parser
->lexer
);
32652 if (token
->type
== CPP_SCOPE
)
32655 = chainon (sel_seq
,
32656 build_tree_list (selector
, NULL_TREE
));
32658 = chainon (sel_seq
,
32659 build_tree_list (NULL_TREE
, NULL_TREE
));
32663 = chainon (sel_seq
,
32664 build_tree_list (selector
, NULL_TREE
));
32666 token
= cp_lexer_peek_token (parser
->lexer
);
32670 parens
.require_close (parser
);
32673 /* Build a location of the form:
32676 with caret==start at the @ token, finishing at the close paren. */
32677 location_t combined_loc
= make_location (loc
, loc
, parser
->lexer
);
32678 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
32679 /* TODO: objc_build_selector_expr doesn't always honor the location. */
32680 protected_set_expr_location (result
, combined_loc
);
32684 /* Parse a list of identifiers.
32686 objc-identifier-list:
32688 objc-identifier-list , identifier
32690 Returns a TREE_LIST of identifier nodes. */
32693 cp_parser_objc_identifier_list (cp_parser
* parser
)
32699 identifier
= cp_parser_identifier (parser
);
32700 if (identifier
== error_mark_node
)
32701 return error_mark_node
;
32703 list
= build_tree_list (NULL_TREE
, identifier
);
32704 sep
= cp_lexer_peek_token (parser
->lexer
);
32706 while (sep
->type
== CPP_COMMA
)
32708 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
32709 identifier
= cp_parser_identifier (parser
);
32710 if (identifier
== error_mark_node
)
32713 list
= chainon (list
, build_tree_list (NULL_TREE
,
32715 sep
= cp_lexer_peek_token (parser
->lexer
);
32721 /* Parse an Objective-C alias declaration.
32723 objc-alias-declaration:
32724 @compatibility_alias identifier identifier ;
32726 This function registers the alias mapping with the Objective-C front end.
32727 It returns nothing. */
32730 cp_parser_objc_alias_declaration (cp_parser
* parser
)
32734 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
32735 alias
= cp_parser_identifier (parser
);
32736 orig
= cp_parser_identifier (parser
);
32737 objc_declare_alias (alias
, orig
);
32738 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32741 /* Parse an Objective-C class forward-declaration.
32743 objc-class-declaration:
32744 @class objc-identifier-list ;
32746 The function registers the forward declarations with the Objective-C
32747 front end. It returns nothing. */
32750 cp_parser_objc_class_declaration (cp_parser
* parser
)
32752 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
32757 id
= cp_parser_identifier (parser
);
32758 if (id
== error_mark_node
)
32761 objc_declare_class (id
);
32763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32764 cp_lexer_consume_token (parser
->lexer
);
32768 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32771 /* Parse a list of Objective-C protocol references.
32773 objc-protocol-refs-opt:
32774 objc-protocol-refs [opt]
32776 objc-protocol-refs:
32777 < objc-identifier-list >
32779 Returns a TREE_LIST of identifiers, if any. */
32782 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
32784 tree protorefs
= NULL_TREE
;
32786 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
32788 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
32789 protorefs
= cp_parser_objc_identifier_list (parser
);
32790 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
32796 /* Parse a Objective-C visibility specification. */
32799 cp_parser_objc_visibility_spec (cp_parser
* parser
)
32801 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
32803 switch (vis
->keyword
)
32805 case RID_AT_PRIVATE
:
32806 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
32808 case RID_AT_PROTECTED
:
32809 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
32811 case RID_AT_PUBLIC
:
32812 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
32814 case RID_AT_PACKAGE
:
32815 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
32821 /* Eat '@private'/'@protected'/'@public'. */
32822 cp_lexer_consume_token (parser
->lexer
);
32825 /* Parse an Objective-C method type. Return 'true' if it is a class
32826 (+) method, and 'false' if it is an instance (-) method. */
32829 cp_parser_objc_method_type (cp_parser
* parser
)
32831 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
32837 /* Parse an Objective-C protocol qualifier. */
32840 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
32842 tree quals
= NULL_TREE
, node
;
32843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32845 node
= token
->u
.value
;
32847 while (node
&& identifier_p (node
)
32848 && (node
== ridpointers
[(int) RID_IN
]
32849 || node
== ridpointers
[(int) RID_OUT
]
32850 || node
== ridpointers
[(int) RID_INOUT
]
32851 || node
== ridpointers
[(int) RID_BYCOPY
]
32852 || node
== ridpointers
[(int) RID_BYREF
]
32853 || node
== ridpointers
[(int) RID_ONEWAY
]))
32855 quals
= tree_cons (NULL_TREE
, node
, quals
);
32856 cp_lexer_consume_token (parser
->lexer
);
32857 token
= cp_lexer_peek_token (parser
->lexer
);
32858 node
= token
->u
.value
;
32864 /* Parse an Objective-C typename. */
32867 cp_parser_objc_typename (cp_parser
* parser
)
32869 tree type_name
= NULL_TREE
;
32871 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32873 tree proto_quals
, cp_type
= NULL_TREE
;
32875 matching_parens parens
;
32876 parens
.consume_open (parser
); /* Eat '('. */
32877 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
32879 /* An ObjC type name may consist of just protocol qualifiers, in which
32880 case the type shall default to 'id'. */
32881 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
32883 cp_type
= cp_parser_type_id (parser
);
32885 /* If the type could not be parsed, an error has already
32886 been produced. For error recovery, behave as if it had
32887 not been specified, which will use the default type
32889 if (cp_type
== error_mark_node
)
32891 cp_type
= NULL_TREE
;
32892 /* We need to skip to the closing parenthesis as
32893 cp_parser_type_id() does not seem to do it for
32895 cp_parser_skip_to_closing_parenthesis (parser
,
32896 /*recovering=*/true,
32897 /*or_comma=*/false,
32898 /*consume_paren=*/false);
32902 parens
.require_close (parser
);
32903 type_name
= build_tree_list (proto_quals
, cp_type
);
32909 /* Check to see if TYPE refers to an Objective-C selector name. */
32912 cp_parser_objc_selector_p (enum cpp_ttype type
)
32914 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
32915 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
32916 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
32917 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
32918 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
32921 /* Parse an Objective-C selector. */
32924 cp_parser_objc_selector (cp_parser
* parser
)
32926 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
32928 if (!cp_parser_objc_selector_p (token
->type
))
32930 error_at (token
->location
, "invalid Objective-C++ selector name");
32931 return error_mark_node
;
32934 /* C++ operator names are allowed to appear in ObjC selectors. */
32935 switch (token
->type
)
32937 case CPP_AND_AND
: return get_identifier ("and");
32938 case CPP_AND_EQ
: return get_identifier ("and_eq");
32939 case CPP_AND
: return get_identifier ("bitand");
32940 case CPP_OR
: return get_identifier ("bitor");
32941 case CPP_COMPL
: return get_identifier ("compl");
32942 case CPP_NOT
: return get_identifier ("not");
32943 case CPP_NOT_EQ
: return get_identifier ("not_eq");
32944 case CPP_OR_OR
: return get_identifier ("or");
32945 case CPP_OR_EQ
: return get_identifier ("or_eq");
32946 case CPP_XOR
: return get_identifier ("xor");
32947 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
32948 default: return token
->u
.value
;
32952 /* Parse an Objective-C params list. */
32955 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
32957 tree params
= NULL_TREE
;
32958 bool maybe_unary_selector_p
= true;
32959 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32961 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
32963 tree selector
= NULL_TREE
, type_name
, identifier
;
32964 tree parm_attr
= NULL_TREE
;
32966 if (token
->keyword
== RID_ATTRIBUTE
)
32969 if (token
->type
!= CPP_COLON
)
32970 selector
= cp_parser_objc_selector (parser
);
32972 /* Detect if we have a unary selector. */
32973 if (maybe_unary_selector_p
32974 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
32976 params
= selector
; /* Might be followed by attributes. */
32980 maybe_unary_selector_p
= false;
32981 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32983 /* Something went quite wrong. There should be a colon
32984 here, but there is not. Stop parsing parameters. */
32987 type_name
= cp_parser_objc_typename (parser
);
32988 /* New ObjC allows attributes on parameters too. */
32989 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
32990 parm_attr
= cp_parser_attributes_opt (parser
);
32991 identifier
= cp_parser_identifier (parser
);
32995 objc_build_keyword_decl (selector
,
33000 token
= cp_lexer_peek_token (parser
->lexer
);
33003 if (params
== NULL_TREE
)
33005 cp_parser_error (parser
, "objective-c++ method declaration is expected");
33006 return error_mark_node
;
33009 /* We allow tail attributes for the method. */
33010 if (token
->keyword
== RID_ATTRIBUTE
)
33012 *attributes
= cp_parser_attributes_opt (parser
);
33013 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
33014 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33016 cp_parser_error (parser
,
33017 "method attributes must be specified at the end");
33018 return error_mark_node
;
33021 if (params
== NULL_TREE
)
33023 cp_parser_error (parser
, "objective-c++ method declaration is expected");
33024 return error_mark_node
;
33029 /* Parse the non-keyword Objective-C params. */
33032 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
33035 tree params
= make_node (TREE_LIST
);
33036 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33037 *ellipsisp
= false; /* Initially, assume no ellipsis. */
33039 while (token
->type
== CPP_COMMA
)
33041 cp_parameter_declarator
*parmdecl
;
33044 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33045 token
= cp_lexer_peek_token (parser
->lexer
);
33047 if (token
->type
== CPP_ELLIPSIS
)
33049 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
33051 token
= cp_lexer_peek_token (parser
->lexer
);
33055 /* TODO: parse attributes for tail parameters. */
33056 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
33058 parm
= grokdeclarator (parmdecl
->declarator
,
33059 &parmdecl
->decl_specifiers
,
33060 PARM
, /*initialized=*/0,
33061 /*attrlist=*/NULL
);
33063 chainon (params
, build_tree_list (NULL_TREE
, parm
));
33064 token
= cp_lexer_peek_token (parser
->lexer
);
33067 /* We allow tail attributes for the method. */
33068 if (token
->keyword
== RID_ATTRIBUTE
)
33070 if (*attributes
== NULL_TREE
)
33072 *attributes
= cp_parser_attributes_opt (parser
);
33073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
33074 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33078 /* We have an error, but parse the attributes, so that we can
33080 *attributes
= cp_parser_attributes_opt (parser
);
33082 cp_parser_error (parser
,
33083 "method attributes must be specified at the end");
33084 return error_mark_node
;
33090 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
33093 cp_parser_objc_interstitial_code (cp_parser
* parser
)
33095 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33097 /* If the next token is `extern' and the following token is a string
33098 literal, then we have a linkage specification. */
33099 if (token
->keyword
== RID_EXTERN
33100 && cp_parser_is_pure_string_literal
33101 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
33102 cp_parser_linkage_specification (parser
);
33103 /* Handle #pragma, if any. */
33104 else if (token
->type
== CPP_PRAGMA
)
33105 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
33106 /* Allow stray semicolons. */
33107 else if (token
->type
== CPP_SEMICOLON
)
33108 cp_lexer_consume_token (parser
->lexer
);
33109 /* Mark methods as optional or required, when building protocols. */
33110 else if (token
->keyword
== RID_AT_OPTIONAL
)
33112 cp_lexer_consume_token (parser
->lexer
);
33113 objc_set_method_opt (true);
33115 else if (token
->keyword
== RID_AT_REQUIRED
)
33117 cp_lexer_consume_token (parser
->lexer
);
33118 objc_set_method_opt (false);
33120 else if (token
->keyword
== RID_NAMESPACE
)
33121 cp_parser_namespace_definition (parser
);
33122 /* Other stray characters must generate errors. */
33123 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
33125 cp_lexer_consume_token (parser
->lexer
);
33126 error ("stray %qs between Objective-C++ methods",
33127 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
33129 /* Finally, try to parse a block-declaration, or a function-definition. */
33131 cp_parser_block_declaration (parser
, /*statement_p=*/false);
33134 /* Parse a method signature. */
33137 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
33139 tree rettype
, kwdparms
, optparms
;
33140 bool ellipsis
= false;
33141 bool is_class_method
;
33143 is_class_method
= cp_parser_objc_method_type (parser
);
33144 rettype
= cp_parser_objc_typename (parser
);
33145 *attributes
= NULL_TREE
;
33146 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
33147 if (kwdparms
== error_mark_node
)
33148 return error_mark_node
;
33149 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
33150 if (optparms
== error_mark_node
)
33151 return error_mark_node
;
33153 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
33157 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
33160 cp_lexer_save_tokens (parser
->lexer
);
33161 tattr
= cp_parser_attributes_opt (parser
);
33162 gcc_assert (tattr
) ;
33164 /* If the attributes are followed by a method introducer, this is not allowed.
33165 Dump the attributes and flag the situation. */
33166 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
33167 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33170 /* Otherwise, the attributes introduce some interstitial code, possibly so
33171 rewind to allow that check. */
33172 cp_lexer_rollback_tokens (parser
->lexer
);
33176 /* Parse an Objective-C method prototype list. */
33179 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
33181 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33183 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33185 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
33187 tree attributes
, sig
;
33188 bool is_class_method
;
33189 if (token
->type
== CPP_PLUS
)
33190 is_class_method
= true;
33192 is_class_method
= false;
33193 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
33194 if (sig
== error_mark_node
)
33196 cp_parser_skip_to_end_of_block_or_statement (parser
);
33197 token
= cp_lexer_peek_token (parser
->lexer
);
33200 objc_add_method_declaration (is_class_method
, sig
, attributes
);
33201 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33203 else if (token
->keyword
== RID_AT_PROPERTY
)
33204 cp_parser_objc_at_property_declaration (parser
);
33205 else if (token
->keyword
== RID_ATTRIBUTE
33206 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
33207 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33209 "prefix attributes are ignored for methods");
33211 /* Allow for interspersed non-ObjC++ code. */
33212 cp_parser_objc_interstitial_code (parser
);
33214 token
= cp_lexer_peek_token (parser
->lexer
);
33217 if (token
->type
!= CPP_EOF
)
33218 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33220 cp_parser_error (parser
, "expected %<@end%>");
33222 objc_finish_interface ();
33225 /* Parse an Objective-C method definition list. */
33228 cp_parser_objc_method_definition_list (cp_parser
* parser
)
33232 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33234 if (token
->keyword
== RID_AT_END
)
33236 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33239 else if (token
->type
== CPP_EOF
)
33241 cp_parser_error (parser
, "expected %<@end%>");
33244 else if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
33246 bool is_class_method
= token
->type
== CPP_PLUS
;
33248 push_deferring_access_checks (dk_deferred
);
33250 tree sig
= cp_parser_objc_method_signature (parser
, &attribute
);
33251 if (sig
== error_mark_node
)
33252 cp_parser_skip_to_end_of_block_or_statement (parser
);
33255 objc_start_method_definition (is_class_method
, sig
,
33256 attribute
, NULL_TREE
);
33258 /* For historical reasons, we accept an optional semicolon. */
33259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33260 cp_lexer_consume_token (parser
->lexer
);
33262 perform_deferred_access_checks (tf_warning_or_error
);
33263 stop_deferring_access_checks ();
33265 = cp_parser_function_definition_after_declarator (parser
, false);
33266 pop_deferring_access_checks ();
33267 objc_finish_method_definition (meth
);
33270 /* The following case will be removed once @synthesize is
33271 completely implemented. */
33272 else if (token
->keyword
== RID_AT_PROPERTY
)
33273 cp_parser_objc_at_property_declaration (parser
);
33274 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
33275 cp_parser_objc_at_synthesize_declaration (parser
);
33276 else if (token
->keyword
== RID_AT_DYNAMIC
)
33277 cp_parser_objc_at_dynamic_declaration (parser
);
33278 else if (token
->keyword
== RID_ATTRIBUTE
33279 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
33280 warning_at (token
->location
, OPT_Wattributes
,
33281 "prefix attributes are ignored for methods");
33283 /* Allow for interspersed non-ObjC++ code. */
33284 cp_parser_objc_interstitial_code (parser
);
33287 objc_finish_implementation ();
33290 /* Parse Objective-C ivars. */
33293 cp_parser_objc_class_ivars (cp_parser
* parser
)
33295 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33297 if (token
->type
!= CPP_OPEN_BRACE
)
33298 return; /* No ivars specified. */
33300 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
33301 token
= cp_lexer_peek_token (parser
->lexer
);
33303 while (token
->type
!= CPP_CLOSE_BRACE
33304 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33306 cp_decl_specifier_seq declspecs
;
33307 int decl_class_or_enum_p
;
33308 tree prefix_attributes
;
33310 cp_parser_objc_visibility_spec (parser
);
33312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33315 cp_parser_decl_specifier_seq (parser
,
33316 CP_PARSER_FLAGS_OPTIONAL
,
33318 &decl_class_or_enum_p
);
33320 /* auto, register, static, extern, mutable. */
33321 if (declspecs
.storage_class
!= sc_none
)
33323 cp_parser_error (parser
, "invalid type for instance variable");
33324 declspecs
.storage_class
= sc_none
;
33327 /* thread_local. */
33328 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
33330 cp_parser_error (parser
, "invalid type for instance variable");
33331 declspecs
.locations
[ds_thread
] = 0;
33335 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
33337 cp_parser_error (parser
, "invalid type for instance variable");
33338 declspecs
.locations
[ds_typedef
] = 0;
33341 prefix_attributes
= declspecs
.attributes
;
33342 declspecs
.attributes
= NULL_TREE
;
33344 /* Keep going until we hit the `;' at the end of the
33346 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33348 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
33349 cp_declarator
*declarator
= NULL
;
33350 int ctor_dtor_or_conv_p
;
33352 /* Check for a (possibly unnamed) bitfield declaration. */
33353 token
= cp_lexer_peek_token (parser
->lexer
);
33354 if (token
->type
== CPP_COLON
)
33357 if (token
->type
== CPP_NAME
33358 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
33361 /* Get the name of the bitfield. */
33362 declarator
= make_id_declarator (NULL_TREE
,
33363 cp_parser_identifier (parser
),
33364 sfk_none
, token
->location
);
33367 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
33368 /* Get the width of the bitfield. */
33370 = cp_parser_constant_expression (parser
);
33374 /* Parse the declarator. */
33376 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
33377 CP_PARSER_FLAGS_NONE
,
33378 &ctor_dtor_or_conv_p
,
33379 /*parenthesized_p=*/NULL
,
33380 /*member_p=*/false,
33381 /*friend_p=*/false,
33382 /*static_p=*/false);
33385 /* Look for attributes that apply to the ivar. */
33386 attributes
= cp_parser_attributes_opt (parser
);
33387 /* Remember which attributes are prefix attributes and
33389 first_attribute
= attributes
;
33390 /* Combine the attributes. */
33391 attributes
= attr_chainon (prefix_attributes
, attributes
);
33394 /* Create the bitfield declaration. */
33395 decl
= grokbitfield (declarator
, &declspecs
,
33396 width
, NULL_TREE
, attributes
);
33398 decl
= grokfield (declarator
, &declspecs
,
33399 NULL_TREE
, /*init_const_expr_p=*/false,
33400 NULL_TREE
, attributes
);
33402 /* Add the instance variable. */
33403 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
33404 objc_add_instance_variable (decl
);
33406 /* Reset PREFIX_ATTRIBUTES. */
33407 if (attributes
!= error_mark_node
)
33409 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
33410 attributes
= TREE_CHAIN (attributes
);
33412 TREE_CHAIN (attributes
) = NULL_TREE
;
33415 token
= cp_lexer_peek_token (parser
->lexer
);
33417 if (token
->type
== CPP_COMMA
)
33419 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33425 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33426 token
= cp_lexer_peek_token (parser
->lexer
);
33429 if (token
->keyword
== RID_AT_END
)
33430 cp_parser_error (parser
, "expected %<}%>");
33432 /* Do not consume the RID_AT_END, so it will be read again as terminating
33433 the @interface of @implementation. */
33434 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33435 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
33437 /* For historical reasons, we accept an optional semicolon. */
33438 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33439 cp_lexer_consume_token (parser
->lexer
);
33442 /* Parse an Objective-C protocol declaration. */
33445 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
33447 tree proto
, protorefs
;
33450 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
33451 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33453 tok
= cp_lexer_peek_token (parser
->lexer
);
33454 error_at (tok
->location
, "identifier expected after %<@protocol%>");
33455 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33459 /* See if we have a forward declaration or a definition. */
33460 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
33462 /* Try a forward declaration first. */
33463 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
33469 id
= cp_parser_identifier (parser
);
33470 if (id
== error_mark_node
)
33473 objc_declare_protocol (id
, attributes
);
33475 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33476 cp_lexer_consume_token (parser
->lexer
);
33480 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33483 /* Ok, we got a full-fledged definition (or at least should). */
33486 proto
= cp_parser_identifier (parser
);
33487 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
33488 objc_start_protocol (proto
, protorefs
, attributes
);
33489 cp_parser_objc_method_prototype_list (parser
);
33493 /* Parse an Objective-C superclass or category. */
33496 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
33499 tree
*categ
, bool *is_class_extension
)
33501 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
33503 *super
= *categ
= NULL_TREE
;
33504 *is_class_extension
= false;
33505 if (next
->type
== CPP_COLON
)
33507 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
33508 *super
= cp_parser_identifier (parser
);
33510 else if (next
->type
== CPP_OPEN_PAREN
)
33512 matching_parens parens
;
33513 parens
.consume_open (parser
); /* Eat '('. */
33515 /* If there is no category name, and this is an @interface, we
33516 have a class extension. */
33517 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33519 *categ
= NULL_TREE
;
33520 *is_class_extension
= true;
33523 *categ
= cp_parser_identifier (parser
);
33525 parens
.require_close (parser
);
33529 /* Parse an Objective-C class interface. */
33532 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
33534 tree name
, super
, categ
, protos
;
33535 bool is_class_extension
;
33537 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
33538 name
= cp_parser_identifier (parser
);
33539 if (name
== error_mark_node
)
33541 /* It's hard to recover because even if valid @interface stuff
33542 is to follow, we can't compile it (or validate it) if we
33543 don't even know which class it refers to. Let's assume this
33544 was a stray '@interface' token in the stream and skip it.
33548 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
33549 &is_class_extension
);
33550 protos
= cp_parser_objc_protocol_refs_opt (parser
);
33552 /* We have either a class or a category on our hands. */
33553 if (categ
|| is_class_extension
)
33554 objc_start_category_interface (name
, categ
, protos
, attributes
);
33557 objc_start_class_interface (name
, super
, protos
, attributes
);
33558 /* Handle instance variable declarations, if any. */
33559 cp_parser_objc_class_ivars (parser
);
33560 objc_continue_interface ();
33563 cp_parser_objc_method_prototype_list (parser
);
33566 /* Parse an Objective-C class implementation. */
33569 cp_parser_objc_class_implementation (cp_parser
* parser
)
33571 tree name
, super
, categ
;
33572 bool is_class_extension
;
33574 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
33575 name
= cp_parser_identifier (parser
);
33576 if (name
== error_mark_node
)
33578 /* It's hard to recover because even if valid @implementation
33579 stuff is to follow, we can't compile it (or validate it) if
33580 we don't even know which class it refers to. Let's assume
33581 this was a stray '@implementation' token in the stream and
33586 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
33587 &is_class_extension
);
33589 /* We have either a class or a category on our hands. */
33591 objc_start_category_implementation (name
, categ
);
33594 objc_start_class_implementation (name
, super
);
33595 /* Handle instance variable declarations, if any. */
33596 cp_parser_objc_class_ivars (parser
);
33597 objc_continue_implementation ();
33600 cp_parser_objc_method_definition_list (parser
);
33603 /* Consume the @end token and finish off the implementation. */
33606 cp_parser_objc_end_implementation (cp_parser
* parser
)
33608 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33609 objc_finish_implementation ();
33612 /* Parse an Objective-C declaration. */
33615 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
33617 /* Try to figure out what kind of declaration is present. */
33618 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
33621 switch (kwd
->keyword
)
33626 error_at (kwd
->location
, "attributes may not be specified before"
33627 " the %<@%D%> Objective-C++ keyword",
33631 case RID_AT_IMPLEMENTATION
:
33632 warning_at (kwd
->location
, OPT_Wattributes
,
33633 "prefix attributes are ignored before %<@%D%>",
33640 switch (kwd
->keyword
)
33643 cp_parser_objc_alias_declaration (parser
);
33646 cp_parser_objc_class_declaration (parser
);
33648 case RID_AT_PROTOCOL
:
33649 cp_parser_objc_protocol_declaration (parser
, attributes
);
33651 case RID_AT_INTERFACE
:
33652 cp_parser_objc_class_interface (parser
, attributes
);
33654 case RID_AT_IMPLEMENTATION
:
33655 cp_parser_objc_class_implementation (parser
);
33658 cp_parser_objc_end_implementation (parser
);
33661 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
33663 cp_parser_skip_to_end_of_block_or_statement (parser
);
33667 /* Parse an Objective-C try-catch-finally statement.
33669 objc-try-catch-finally-stmt:
33670 @try compound-statement objc-catch-clause-seq [opt]
33671 objc-finally-clause [opt]
33673 objc-catch-clause-seq:
33674 objc-catch-clause objc-catch-clause-seq [opt]
33677 @catch ( objc-exception-declaration ) compound-statement
33679 objc-finally-clause:
33680 @finally compound-statement
33682 objc-exception-declaration:
33683 parameter-declaration
33686 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
33690 PS: This function is identical to c_parser_objc_try_catch_finally_statement
33691 for C. Keep them in sync. */
33694 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
33696 location_t location
;
33699 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
33700 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33701 objc_maybe_warn_exceptions (location
);
33702 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
33703 node, lest it get absorbed into the surrounding block. */
33704 stmt
= push_stmt_list ();
33705 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33706 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
33708 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
33710 cp_parameter_declarator
*parm
;
33711 tree parameter_declaration
= error_mark_node
;
33712 bool seen_open_paren
= false;
33713 matching_parens parens
;
33715 cp_lexer_consume_token (parser
->lexer
);
33716 if (parens
.require_open (parser
))
33717 seen_open_paren
= true;
33718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
33720 /* We have "@catch (...)" (where the '...' are literally
33721 what is in the code). Skip the '...'.
33722 parameter_declaration is set to NULL_TREE, and
33723 objc_being_catch_clauses() knows that that means
33725 cp_lexer_consume_token (parser
->lexer
);
33726 parameter_declaration
= NULL_TREE
;
33730 /* We have "@catch (NSException *exception)" or something
33731 like that. Parse the parameter declaration. */
33732 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
33735 parameter_declaration
= error_mark_node
;
33737 parameter_declaration
= grokdeclarator (parm
->declarator
,
33738 &parm
->decl_specifiers
,
33739 PARM
, /*initialized=*/0,
33740 /*attrlist=*/NULL
);
33742 if (seen_open_paren
)
33743 parens
.require_close (parser
);
33746 /* If there was no open parenthesis, we are recovering from
33747 an error, and we are trying to figure out what mistake
33748 the user has made. */
33750 /* If there is an immediate closing parenthesis, the user
33751 probably forgot the opening one (ie, they typed "@catch
33752 NSException *e)". Parse the closing parenthesis and keep
33754 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33755 cp_lexer_consume_token (parser
->lexer
);
33757 /* If these is no immediate closing parenthesis, the user
33758 probably doesn't know that parenthesis are required at
33759 all (ie, they typed "@catch NSException *e"). So, just
33760 forget about the closing parenthesis and keep going. */
33762 objc_begin_catch_clause (parameter_declaration
);
33763 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33764 objc_finish_catch_clause ();
33766 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
33768 cp_lexer_consume_token (parser
->lexer
);
33769 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33770 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
33771 node, lest it get absorbed into the surrounding block. */
33772 stmt
= push_stmt_list ();
33773 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33774 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
33777 return objc_finish_try_stmt ();
33780 /* Parse an Objective-C synchronized statement.
33782 objc-synchronized-stmt:
33783 @synchronized ( expression ) compound-statement
33785 Returns NULL_TREE. */
33788 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
33790 location_t location
;
33793 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
33795 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33796 objc_maybe_warn_exceptions (location
);
33797 matching_parens parens
;
33798 parens
.require_open (parser
);
33799 lock
= cp_parser_expression (parser
);
33800 parens
.require_close (parser
);
33802 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
33803 node, lest it get absorbed into the surrounding block. */
33804 stmt
= push_stmt_list ();
33805 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33807 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
33810 /* Parse an Objective-C throw statement.
33813 @throw assignment-expression [opt] ;
33815 Returns a constructed '@throw' statement. */
33818 cp_parser_objc_throw_statement (cp_parser
*parser
)
33820 tree expr
= NULL_TREE
;
33821 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33823 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
33825 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33826 expr
= cp_parser_expression (parser
);
33828 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33830 return objc_build_throw_stmt (loc
, expr
);
33833 /* Parse an Objective-C statement. */
33836 cp_parser_objc_statement (cp_parser
* parser
)
33838 /* Try to figure out what kind of declaration is present. */
33839 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
33841 switch (kwd
->keyword
)
33844 return cp_parser_objc_try_catch_finally_statement (parser
);
33845 case RID_AT_SYNCHRONIZED
:
33846 return cp_parser_objc_synchronized_statement (parser
);
33848 return cp_parser_objc_throw_statement (parser
);
33850 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
33852 cp_parser_skip_to_end_of_block_or_statement (parser
);
33855 return error_mark_node
;
33858 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
33859 look ahead to see if an objc keyword follows the attributes. This
33860 is to detect the use of prefix attributes on ObjC @interface and
33864 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
33866 cp_lexer_save_tokens (parser
->lexer
);
33867 *attrib
= cp_parser_attributes_opt (parser
);
33868 gcc_assert (*attrib
);
33869 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
33871 cp_lexer_commit_tokens (parser
->lexer
);
33874 cp_lexer_rollback_tokens (parser
->lexer
);
33878 /* This routine is a minimal replacement for
33879 c_parser_struct_declaration () used when parsing the list of
33880 types/names or ObjC++ properties. For example, when parsing the
33883 @property (readonly) int a, b, c;
33885 this function is responsible for parsing "int a, int b, int c" and
33886 returning the declarations as CHAIN of DECLs.
33888 TODO: Share this code with cp_parser_objc_class_ivars. It's very
33889 similar parsing. */
33891 cp_parser_objc_struct_declaration (cp_parser
*parser
)
33893 tree decls
= NULL_TREE
;
33894 cp_decl_specifier_seq declspecs
;
33895 int decl_class_or_enum_p
;
33896 tree prefix_attributes
;
33898 cp_parser_decl_specifier_seq (parser
,
33899 CP_PARSER_FLAGS_NONE
,
33901 &decl_class_or_enum_p
);
33903 if (declspecs
.type
== error_mark_node
)
33904 return error_mark_node
;
33906 /* auto, register, static, extern, mutable. */
33907 if (declspecs
.storage_class
!= sc_none
)
33909 cp_parser_error (parser
, "invalid type for property");
33910 declspecs
.storage_class
= sc_none
;
33913 /* thread_local. */
33914 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
33916 cp_parser_error (parser
, "invalid type for property");
33917 declspecs
.locations
[ds_thread
] = 0;
33921 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
33923 cp_parser_error (parser
, "invalid type for property");
33924 declspecs
.locations
[ds_typedef
] = 0;
33927 prefix_attributes
= declspecs
.attributes
;
33928 declspecs
.attributes
= NULL_TREE
;
33930 /* Keep going until we hit the `;' at the end of the declaration. */
33931 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33933 tree attributes
, first_attribute
, decl
;
33934 cp_declarator
*declarator
;
33937 /* Parse the declarator. */
33938 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
33939 CP_PARSER_FLAGS_NONE
,
33940 NULL
, NULL
, false, false, false);
33942 /* Look for attributes that apply to the ivar. */
33943 attributes
= cp_parser_attributes_opt (parser
);
33944 /* Remember which attributes are prefix attributes and
33946 first_attribute
= attributes
;
33947 /* Combine the attributes. */
33948 attributes
= attr_chainon (prefix_attributes
, attributes
);
33950 decl
= grokfield (declarator
, &declspecs
,
33951 NULL_TREE
, /*init_const_expr_p=*/false,
33952 NULL_TREE
, attributes
);
33954 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
33955 return error_mark_node
;
33957 /* Reset PREFIX_ATTRIBUTES. */
33958 if (attributes
!= error_mark_node
)
33960 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
33961 attributes
= TREE_CHAIN (attributes
);
33963 TREE_CHAIN (attributes
) = NULL_TREE
;
33966 DECL_CHAIN (decl
) = decls
;
33969 token
= cp_lexer_peek_token (parser
->lexer
);
33970 if (token
->type
== CPP_COMMA
)
33972 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33981 /* Parse an Objective-C @property declaration. The syntax is:
33983 objc-property-declaration:
33984 '@property' objc-property-attributes[opt] struct-declaration ;
33986 objc-property-attributes:
33987 '(' objc-property-attribute-list ')'
33989 objc-property-attribute-list:
33990 objc-property-attribute
33991 objc-property-attribute-list, objc-property-attribute
33993 objc-property-attribute
33994 'getter' = identifier
33995 'setter' = identifier
34004 @property NSString *name;
34005 @property (readonly) id object;
34006 @property (retain, nonatomic, getter=getTheName) id name;
34007 @property int a, b, c;
34009 PS: This function is identical to
34010 c_parser_objc_at_property_declaration for C. Keep them in sync. */
34012 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
34014 /* Parse the optional attribute list.
34016 A list of parsed, but not verified, attributes. */
34017 vec
<property_attribute_info
*> prop_attr_list
= vNULL
;
34018 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34020 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
34022 /* Parse the optional attribute list... */
34023 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34026 matching_parens parens
;
34027 location_t attr_start
= cp_lexer_peek_token (parser
->lexer
)->location
;
34028 parens
.consume_open (parser
);
34029 bool syntax_error
= false;
34031 /* Allow empty @property attribute lists, but with a warning. */
34032 location_t attr_end
= cp_lexer_peek_token (parser
->lexer
)->location
;
34033 location_t attr_comb
;
34034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
34036 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
34037 warning_at (attr_comb
, OPT_Wattributes
,
34038 "empty property attribute list");
34043 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34044 attr_start
= token
->location
;
34045 attr_end
= get_finish (token
->location
);
34046 attr_comb
= make_location (attr_start
, attr_start
, attr_end
);
34048 if (token
->type
== CPP_CLOSE_PAREN
|| token
->type
== CPP_COMMA
)
34050 warning_at (attr_comb
, OPT_Wattributes
,
34051 "missing property attribute");
34052 if (token
->type
== CPP_CLOSE_PAREN
)
34054 cp_lexer_consume_token (parser
->lexer
);
34058 tree attr_name
= NULL_TREE
;
34059 if (identifier_p (token
->u
.value
))
34060 attr_name
= token
->u
.value
;
34063 if (token
->type
== CPP_NAME
)
34064 keyword
= C_RID_CODE (token
->u
.value
);
34065 else if (token
->type
== CPP_KEYWORD
34066 && token
->keyword
== RID_CLASS
)
34067 /* Account for accepting the 'class' keyword in this context. */
34068 keyword
= RID_CLASS
;
34070 keyword
= RID_MAX
; /* By definition, an unknown property. */
34071 cp_lexer_consume_token (parser
->lexer
);
34073 enum objc_property_attribute_kind prop_kind
34074 = objc_prop_attr_kind_for_rid (keyword
);
34075 property_attribute_info
*prop
34076 = new property_attribute_info (attr_name
, attr_comb
, prop_kind
);
34077 prop_attr_list
.safe_push (prop
);
34080 switch (prop
->prop_kind
)
34083 case OBJC_PROPERTY_ATTR_UNKNOWN
:
34085 error_at (attr_start
, "unknown property attribute %qE",
34088 error_at (attr_start
, "unknown property attribute");
34089 prop
->parse_error
= syntax_error
= true;
34092 case OBJC_PROPERTY_ATTR_GETTER
:
34093 case OBJC_PROPERTY_ATTR_SETTER
:
34094 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34096 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
34097 error_at (attr_comb
, "expected %<=%> after Objective-C %qE",
34099 prop
->parse_error
= syntax_error
= true;
34103 token
= cp_lexer_peek_token (parser
->lexer
);
34104 attr_end
= token
->location
;
34105 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
34107 if (!cp_parser_objc_selector_p
34108 (cp_lexer_peek_token (parser
->lexer
)->type
))
34110 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
34111 error_at (attr_comb
, "expected %qE selector name",
34113 prop
->parse_error
= syntax_error
= true;
34117 /* Get the end of the method name, and consume the name. */
34118 token
= cp_lexer_peek_token (parser
->lexer
);
34119 attr_end
= get_finish (token
->location
);
34120 /* Because method names may contain C++ keywords, we have a
34121 routine to fetch them (this also consumes the token). */
34122 meth_name
= cp_parser_objc_selector (parser
);
34124 if (prop
->prop_kind
== OBJC_PROPERTY_ATTR_SETTER
)
34126 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
34128 attr_comb
= make_location (attr_end
, attr_start
,
34130 error_at (attr_comb
, "setter method names must"
34131 " terminate with %<:%>");
34132 prop
->parse_error
= syntax_error
= true;
34136 attr_end
= get_finish (cp_lexer_peek_token
34137 (parser
->lexer
)->location
);
34138 cp_lexer_consume_token (parser
->lexer
);
34140 attr_comb
= make_location (attr_start
, attr_start
,
34144 attr_comb
= make_location (attr_start
, attr_start
,
34146 prop
->ident
= meth_name
;
34147 /* Updated location including all that was successfully
34149 prop
->prop_loc
= attr_comb
;
34153 /* If we see a comma here, then keep going - even if we already
34154 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
34155 this makes a more useful output and avoid spurious warnings
34156 about missing attributes that are, in fact, specified after the
34157 one with the syntax error. */
34158 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34159 cp_lexer_consume_token (parser
->lexer
);
34164 if (syntax_error
|| !parens
.require_close (parser
))
34165 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34166 /*or_comma=*/false,
34167 /*consume_paren=*/true);
34170 /* 'properties' is the list of properties that we read. Usually a
34171 single one, but maybe more (eg, in "@property int a, b, c;" there
34173 TODO: Update this parsing so that it accepts (erroneous) bitfields so
34174 that we can issue a meaningful and consistent (between C/C++) error
34175 message from objc_add_property_declaration (). */
34176 tree properties
= cp_parser_objc_struct_declaration (parser
);
34178 if (properties
== error_mark_node
)
34179 cp_parser_skip_to_end_of_statement (parser
);
34180 else if (properties
== NULL_TREE
)
34181 cp_parser_error (parser
, "expected identifier");
34184 /* Comma-separated properties are chained together in reverse order;
34185 add them one by one. */
34186 properties
= nreverse (properties
);
34187 for (; properties
; properties
= TREE_CHAIN (properties
))
34188 objc_add_property_declaration (loc
, copy_node (properties
),
34192 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34194 while (!prop_attr_list
.is_empty())
34195 delete prop_attr_list
.pop ();
34196 prop_attr_list
.release ();
34199 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
34201 objc-synthesize-declaration:
34202 @synthesize objc-synthesize-identifier-list ;
34204 objc-synthesize-identifier-list:
34205 objc-synthesize-identifier
34206 objc-synthesize-identifier-list, objc-synthesize-identifier
34208 objc-synthesize-identifier
34210 identifier = identifier
34213 @synthesize MyProperty;
34214 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
34216 PS: This function is identical to c_parser_objc_at_synthesize_declaration
34217 for C. Keep them in sync.
34220 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
34222 tree list
= NULL_TREE
;
34224 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34226 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
34229 tree property
, ivar
;
34230 property
= cp_parser_identifier (parser
);
34231 if (property
== error_mark_node
)
34233 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
34238 cp_lexer_consume_token (parser
->lexer
);
34239 ivar
= cp_parser_identifier (parser
);
34240 if (ivar
== error_mark_node
)
34242 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34248 list
= chainon (list
, build_tree_list (ivar
, property
));
34249 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34250 cp_lexer_consume_token (parser
->lexer
);
34254 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34255 objc_add_synthesize_declaration (loc
, list
);
34258 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34260 objc-dynamic-declaration:
34261 @dynamic identifier-list ;
34264 @dynamic MyProperty;
34265 @dynamic MyProperty, AnotherProperty;
34267 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34268 for C. Keep them in sync.
34271 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
34273 tree list
= NULL_TREE
;
34275 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34277 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
34281 property
= cp_parser_identifier (parser
);
34282 if (property
== error_mark_node
)
34284 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34287 list
= chainon (list
, build_tree_list (NULL
, property
));
34288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34289 cp_lexer_consume_token (parser
->lexer
);
34293 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34294 objc_add_dynamic_declaration (loc
, list
);
34298 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34300 /* Returns name of the next clause.
34301 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34302 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34303 returned and the token is consumed. */
34305 static pragma_omp_clause
34306 cp_parser_omp_clause_name (cp_parser
*parser
)
34308 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
34310 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
34311 result
= PRAGMA_OACC_CLAUSE_AUTO
;
34312 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
34313 result
= PRAGMA_OMP_CLAUSE_IF
;
34314 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
34315 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
34316 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
34317 result
= PRAGMA_OACC_CLAUSE_DELETE
;
34318 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
34319 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
34320 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34321 result
= PRAGMA_OMP_CLAUSE_FOR
;
34322 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34324 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34325 const char *p
= IDENTIFIER_POINTER (id
);
34330 if (!strcmp ("aligned", p
))
34331 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
34332 else if (!strcmp ("allocate", p
))
34333 result
= PRAGMA_OMP_CLAUSE_ALLOCATE
;
34334 else if (!strcmp ("async", p
))
34335 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
34336 else if (!strcmp ("attach", p
))
34337 result
= PRAGMA_OACC_CLAUSE_ATTACH
;
34340 if (!strcmp ("bind", p
))
34341 result
= PRAGMA_OMP_CLAUSE_BIND
;
34344 if (!strcmp ("collapse", p
))
34345 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
34346 else if (!strcmp ("copy", p
))
34347 result
= PRAGMA_OACC_CLAUSE_COPY
;
34348 else if (!strcmp ("copyin", p
))
34349 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
34350 else if (!strcmp ("copyout", p
))
34351 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
34352 else if (!strcmp ("copyprivate", p
))
34353 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
34354 else if (!strcmp ("create", p
))
34355 result
= PRAGMA_OACC_CLAUSE_CREATE
;
34358 if (!strcmp ("defaultmap", p
))
34359 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
34360 else if (!strcmp ("depend", p
))
34361 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
34362 else if (!strcmp ("detach", p
))
34363 result
= PRAGMA_OACC_CLAUSE_DETACH
;
34364 else if (!strcmp ("device", p
))
34365 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
34366 else if (!strcmp ("deviceptr", p
))
34367 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
34368 else if (!strcmp ("device_resident", p
))
34369 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
34370 else if (!strcmp ("device_type", p
))
34371 result
= PRAGMA_OMP_CLAUSE_DEVICE_TYPE
;
34372 else if (!strcmp ("dist_schedule", p
))
34373 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
34376 if (!strcmp ("final", p
))
34377 result
= PRAGMA_OMP_CLAUSE_FINAL
;
34378 else if (!strcmp ("finalize", p
))
34379 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
34380 else if (!strcmp ("firstprivate", p
))
34381 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
34382 else if (!strcmp ("from", p
))
34383 result
= PRAGMA_OMP_CLAUSE_FROM
;
34386 if (!strcmp ("gang", p
))
34387 result
= PRAGMA_OACC_CLAUSE_GANG
;
34388 else if (!strcmp ("grainsize", p
))
34389 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
34392 if (!strcmp ("hint", p
))
34393 result
= PRAGMA_OMP_CLAUSE_HINT
;
34394 else if (!strcmp ("host", p
))
34395 result
= PRAGMA_OACC_CLAUSE_HOST
;
34398 if (!strcmp ("if_present", p
))
34399 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
34400 else if (!strcmp ("in_reduction", p
))
34401 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
34402 else if (!strcmp ("inbranch", p
))
34403 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
34404 else if (!strcmp ("independent", p
))
34405 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
34406 else if (!strcmp ("is_device_ptr", p
))
34407 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
34410 if (!strcmp ("lastprivate", p
))
34411 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
34412 else if (!strcmp ("linear", p
))
34413 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
34414 else if (!strcmp ("link", p
))
34415 result
= PRAGMA_OMP_CLAUSE_LINK
;
34418 if (!strcmp ("map", p
))
34419 result
= PRAGMA_OMP_CLAUSE_MAP
;
34420 else if (!strcmp ("mergeable", p
))
34421 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
34424 if (!strcmp ("no_create", p
))
34425 result
= PRAGMA_OACC_CLAUSE_NO_CREATE
;
34426 else if (!strcmp ("nogroup", p
))
34427 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
34428 else if (!strcmp ("nontemporal", p
))
34429 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
34430 else if (!strcmp ("notinbranch", p
))
34431 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
34432 else if (!strcmp ("nowait", p
))
34433 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
34434 else if (!strcmp ("num_gangs", p
))
34435 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
34436 else if (!strcmp ("num_tasks", p
))
34437 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
34438 else if (!strcmp ("num_teams", p
))
34439 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
34440 else if (!strcmp ("num_threads", p
))
34441 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
34442 else if (!strcmp ("num_workers", p
))
34443 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
34446 if (!strcmp ("ordered", p
))
34447 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
34448 else if (!strcmp ("order", p
))
34449 result
= PRAGMA_OMP_CLAUSE_ORDER
;
34452 if (!strcmp ("parallel", p
))
34453 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
34454 else if (!strcmp ("present", p
))
34455 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
34456 else if (!strcmp ("present_or_copy", p
)
34457 || !strcmp ("pcopy", p
))
34458 result
= PRAGMA_OACC_CLAUSE_COPY
;
34459 else if (!strcmp ("present_or_copyin", p
)
34460 || !strcmp ("pcopyin", p
))
34461 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
34462 else if (!strcmp ("present_or_copyout", p
)
34463 || !strcmp ("pcopyout", p
))
34464 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
34465 else if (!strcmp ("present_or_create", p
)
34466 || !strcmp ("pcreate", p
))
34467 result
= PRAGMA_OACC_CLAUSE_CREATE
;
34468 else if (!strcmp ("priority", p
))
34469 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
34470 else if (!strcmp ("proc_bind", p
))
34471 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
34474 if (!strcmp ("reduction", p
))
34475 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
34478 if (!strcmp ("safelen", p
))
34479 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
34480 else if (!strcmp ("schedule", p
))
34481 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
34482 else if (!strcmp ("sections", p
))
34483 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
34484 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
34485 result
= PRAGMA_OACC_CLAUSE_HOST
;
34486 else if (!strcmp ("seq", p
))
34487 result
= PRAGMA_OACC_CLAUSE_SEQ
;
34488 else if (!strcmp ("shared", p
))
34489 result
= PRAGMA_OMP_CLAUSE_SHARED
;
34490 else if (!strcmp ("simd", p
))
34491 result
= PRAGMA_OMP_CLAUSE_SIMD
;
34492 else if (!strcmp ("simdlen", p
))
34493 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
34496 if (!strcmp ("task_reduction", p
))
34497 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
34498 else if (!strcmp ("taskgroup", p
))
34499 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
34500 else if (!strcmp ("thread_limit", p
))
34501 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
34502 else if (!strcmp ("threads", p
))
34503 result
= PRAGMA_OMP_CLAUSE_THREADS
;
34504 else if (!strcmp ("tile", p
))
34505 result
= PRAGMA_OACC_CLAUSE_TILE
;
34506 else if (!strcmp ("to", p
))
34507 result
= PRAGMA_OMP_CLAUSE_TO
;
34510 if (!strcmp ("uniform", p
))
34511 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
34512 else if (!strcmp ("untied", p
))
34513 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
34514 else if (!strcmp ("use_device", p
))
34515 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
34516 else if (!strcmp ("use_device_addr", p
))
34517 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
;
34518 else if (!strcmp ("use_device_ptr", p
))
34519 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
34522 if (!strcmp ("vector", p
))
34523 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
34524 else if (!strcmp ("vector_length", p
))
34525 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
34528 if (!strcmp ("wait", p
))
34529 result
= PRAGMA_OACC_CLAUSE_WAIT
;
34530 else if (!strcmp ("worker", p
))
34531 result
= PRAGMA_OACC_CLAUSE_WORKER
;
34536 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
34537 cp_lexer_consume_token (parser
->lexer
);
34542 /* Validate that a clause of the given type does not already exist. */
34545 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
34546 const char *name
, location_t location
)
34548 if (omp_find_clause (clauses
, code
))
34549 error_at (location
, "too many %qs clauses", name
);
34555 variable-list , identifier
34557 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
34558 colon). An opening parenthesis will have been consumed by the caller.
34560 If KIND is nonzero, create the appropriate node and install the decl
34561 in OMP_CLAUSE_DECL and add the node to the head of the list.
34563 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
34564 return the list created.
34566 COLON can be NULL if only closing parenthesis should end the list,
34567 or pointer to bool which will receive false if the list is terminated
34568 by closing parenthesis or true if the list is terminated by colon.
34570 The optional ALLOW_DEREF argument is true if list items can use the deref
34574 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
34575 tree list
, bool *colon
,
34576 bool allow_deref
= false)
34579 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
34582 parser
->colon_corrects_to_scope_p
= false;
34589 if (kind
== OMP_CLAUSE_DEPEND
)
34590 cp_parser_parse_tentatively (parser
);
34591 token
= cp_lexer_peek_token (parser
->lexer
);
34593 && current_class_ptr
34594 && cp_parser_is_keyword (token
, RID_THIS
))
34596 decl
= finish_this_expr ();
34597 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
34598 || CONVERT_EXPR_P (decl
))
34599 decl
= TREE_OPERAND (decl
, 0);
34600 cp_lexer_consume_token (parser
->lexer
);
34602 else if (cp_parser_is_keyword (token
, RID_FUNCTION_NAME
)
34603 || cp_parser_is_keyword (token
, RID_PRETTY_FUNCTION_NAME
)
34604 || cp_parser_is_keyword (token
, RID_C99_FUNCTION_NAME
))
34607 decl
= cp_parser_primary_expression (parser
, false, false, false,
34612 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34613 /*check_dependency_p=*/true,
34614 /*template_p=*/NULL
,
34615 /*declarator_p=*/false,
34616 /*optional_p=*/false);
34617 if (name
== error_mark_node
)
34619 if (kind
== OMP_CLAUSE_DEPEND
34620 && cp_parser_simulate_error (parser
))
34621 goto depend_lvalue
;
34625 if (identifier_p (name
))
34626 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
34629 if (decl
== error_mark_node
)
34631 if (kind
== OMP_CLAUSE_DEPEND
34632 && cp_parser_simulate_error (parser
))
34633 goto depend_lvalue
;
34634 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
34638 if (outer_automatic_var_p (decl
))
34639 decl
= process_outer_var_ref (decl
, tf_warning_or_error
);
34640 if (decl
== error_mark_node
)
34642 else if (kind
!= 0)
34646 case OMP_CLAUSE__CACHE_
:
34647 /* The OpenACC cache directive explicitly only allows "array
34648 elements or subarrays". */
34649 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
34651 error_at (token
->location
, "expected %<[%>");
34652 decl
= error_mark_node
;
34656 case OMP_CLAUSE_MAP
:
34657 case OMP_CLAUSE_FROM
:
34658 case OMP_CLAUSE_TO
:
34659 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
34661 && cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
)))
34664 = cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
34665 ? CPP_DOT
: CPP_DEREF
;
34667 = cp_lexer_peek_token (parser
->lexer
)->location
;
34668 cp_id_kind idk
= CP_ID_KIND_NONE
;
34669 cp_lexer_consume_token (parser
->lexer
);
34670 decl
= convert_from_reference (decl
);
34672 = cp_parser_postfix_dot_deref_expression (parser
, ttype
,
34677 case OMP_CLAUSE_DEPEND
:
34678 case OMP_CLAUSE_REDUCTION
:
34679 case OMP_CLAUSE_IN_REDUCTION
:
34680 case OMP_CLAUSE_TASK_REDUCTION
:
34681 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
34683 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
34685 parser
->colon_corrects_to_scope_p
= false;
34686 cp_lexer_consume_token (parser
->lexer
);
34687 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34688 low_bound
= cp_parser_expression (parser
);
34690 parser
->colon_corrects_to_scope_p
34691 = saved_colon_corrects_to_scope_p
;
34692 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
34693 length
= integer_one_node
;
34696 /* Look for `:'. */
34697 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34699 if (kind
== OMP_CLAUSE_DEPEND
34700 && cp_parser_simulate_error (parser
))
34701 goto depend_lvalue
;
34704 if (kind
== OMP_CLAUSE_DEPEND
)
34705 cp_parser_commit_to_tentative_parse (parser
);
34706 if (!cp_lexer_next_token_is (parser
->lexer
,
34708 length
= cp_parser_expression (parser
);
34710 /* Look for the closing `]'. */
34711 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
34714 if (kind
== OMP_CLAUSE_DEPEND
34715 && cp_parser_simulate_error (parser
))
34716 goto depend_lvalue
;
34720 decl
= tree_cons (low_bound
, length
, decl
);
34727 if (kind
== OMP_CLAUSE_DEPEND
)
34729 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
34730 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
34731 && cp_parser_simulate_error (parser
))
34734 cp_parser_abort_tentative_parse (parser
);
34735 decl
= cp_parser_assignment_expression (parser
, NULL
,
34739 cp_parser_parse_definitely (parser
);
34742 tree u
= build_omp_clause (token
->location
, kind
);
34743 OMP_CLAUSE_DECL (u
) = decl
;
34744 OMP_CLAUSE_CHAIN (u
) = list
;
34748 list
= tree_cons (decl
, NULL_TREE
, list
);
34751 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34753 cp_lexer_consume_token (parser
->lexer
);
34757 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34759 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34762 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34766 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
34770 /* Try to resync to an unnested comma. Copied from
34771 cp_parser_parenthesized_expression_list. */
34774 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34775 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
34776 /*recovering=*/true,
34778 /*consume_paren=*/true);
34786 /* Similarly, but expect leading and trailing parenthesis. This is a very
34787 common case for omp clauses. */
34790 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
,
34791 bool allow_deref
= false)
34793 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34794 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
,
34800 copy ( variable-list )
34801 copyin ( variable-list )
34802 copyout ( variable-list )
34803 create ( variable-list )
34804 delete ( variable-list )
34805 present ( variable-list )
34808 no_create ( variable-list )
34809 attach ( variable-list )
34810 detach ( variable-list ) */
34813 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
34816 enum gomp_map_kind kind
;
34819 case PRAGMA_OACC_CLAUSE_ATTACH
:
34820 kind
= GOMP_MAP_ATTACH
;
34822 case PRAGMA_OACC_CLAUSE_COPY
:
34823 kind
= GOMP_MAP_TOFROM
;
34825 case PRAGMA_OACC_CLAUSE_COPYIN
:
34826 kind
= GOMP_MAP_TO
;
34828 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34829 kind
= GOMP_MAP_FROM
;
34831 case PRAGMA_OACC_CLAUSE_CREATE
:
34832 kind
= GOMP_MAP_ALLOC
;
34834 case PRAGMA_OACC_CLAUSE_DELETE
:
34835 kind
= GOMP_MAP_RELEASE
;
34837 case PRAGMA_OACC_CLAUSE_DETACH
:
34838 kind
= GOMP_MAP_DETACH
;
34840 case PRAGMA_OACC_CLAUSE_DEVICE
:
34841 kind
= GOMP_MAP_FORCE_TO
;
34843 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34844 kind
= GOMP_MAP_DEVICE_RESIDENT
;
34846 case PRAGMA_OACC_CLAUSE_HOST
:
34847 kind
= GOMP_MAP_FORCE_FROM
;
34849 case PRAGMA_OACC_CLAUSE_LINK
:
34850 kind
= GOMP_MAP_LINK
;
34852 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
34853 kind
= GOMP_MAP_IF_PRESENT
;
34855 case PRAGMA_OACC_CLAUSE_PRESENT
:
34856 kind
= GOMP_MAP_FORCE_PRESENT
;
34859 gcc_unreachable ();
34862 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
, true);
34864 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34865 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34871 deviceptr ( variable-list ) */
34874 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
34876 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34879 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
34880 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
34881 variable-list must only allow for pointer variables. */
34882 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34883 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
34885 tree v
= TREE_PURPOSE (t
);
34886 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
34887 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
34888 OMP_CLAUSE_DECL (u
) = v
;
34889 OMP_CLAUSE_CHAIN (u
) = list
;
34904 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
34907 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
34909 tree c
= build_omp_clause (loc
, code
);
34910 OMP_CLAUSE_CHAIN (c
) = list
;
34916 num_gangs ( expression )
34917 num_workers ( expression )
34918 vector_length ( expression ) */
34921 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
34922 const char *str
, tree list
)
34924 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34926 matching_parens parens
;
34927 if (!parens
.require_open (parser
))
34930 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
34932 if (t
== error_mark_node
34933 || !parens
.require_close (parser
))
34935 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34936 /*or_comma=*/false,
34937 /*consume_paren=*/true);
34941 check_no_duplicate_clause (list
, code
, str
, loc
);
34943 tree c
= build_omp_clause (loc
, code
);
34944 OMP_CLAUSE_OPERAND (c
, 0) = t
;
34945 OMP_CLAUSE_CHAIN (c
) = list
;
34951 gang [( gang-arg-list )]
34952 worker [( [num:] int-expr )]
34953 vector [( [length:] int-expr )]
34955 where gang-arg is one of:
34960 and size-expr may be:
34967 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
34968 omp_clause_code kind
,
34969 const char *str
, tree list
)
34971 const char *id
= "num";
34972 cp_lexer
*lexer
= parser
->lexer
;
34973 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
34975 if (kind
== OMP_CLAUSE_VECTOR
)
34978 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
34980 matching_parens parens
;
34981 parens
.consume_open (parser
);
34985 cp_token
*next
= cp_lexer_peek_token (lexer
);
34988 /* Gang static argument. */
34989 if (kind
== OMP_CLAUSE_GANG
34990 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
34992 cp_lexer_consume_token (lexer
);
34994 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34995 goto cleanup_error
;
34998 if (ops
[idx
] != NULL
)
35000 cp_parser_error (parser
, "too many %<static%> arguments");
35001 goto cleanup_error
;
35004 /* Check for the '*' argument. */
35005 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
35006 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
35007 || cp_lexer_nth_token_is (parser
->lexer
, 2,
35010 cp_lexer_consume_token (lexer
);
35011 ops
[idx
] = integer_minus_one_node
;
35013 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
35015 cp_lexer_consume_token (lexer
);
35021 /* Worker num: argument and vector length: arguments. */
35022 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
35023 && id_equal (next
->u
.value
, id
)
35024 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
35026 cp_lexer_consume_token (lexer
); /* id */
35027 cp_lexer_consume_token (lexer
); /* ':' */
35030 /* Now collect the actual argument. */
35031 if (ops
[idx
] != NULL_TREE
)
35033 cp_parser_error (parser
, "unexpected argument");
35034 goto cleanup_error
;
35037 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
35039 if (expr
== error_mark_node
)
35040 goto cleanup_error
;
35042 mark_exp_read (expr
);
35045 if (kind
== OMP_CLAUSE_GANG
35046 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
35048 cp_lexer_consume_token (lexer
);
35055 if (!parens
.require_close (parser
))
35056 goto cleanup_error
;
35059 check_no_duplicate_clause (list
, kind
, str
, loc
);
35061 c
= build_omp_clause (loc
, kind
);
35064 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
35066 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
35067 OMP_CLAUSE_CHAIN (c
) = list
;
35072 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
35077 tile ( size-expr-list ) */
35080 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
35082 tree c
, expr
= error_mark_node
;
35083 tree tile
= NULL_TREE
;
35085 /* Collapse and tile are mutually exclusive. (The spec doesn't say
35086 so, but the spec authors never considered such a case and have
35087 differing opinions on what it might mean, including 'not
35089 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
35090 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
35093 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35098 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
35101 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
35102 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
35103 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
35105 cp_lexer_consume_token (parser
->lexer
);
35106 expr
= integer_zero_node
;
35109 expr
= cp_parser_constant_expression (parser
);
35111 tile
= tree_cons (NULL_TREE
, expr
, tile
);
35113 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
35115 /* Consume the trailing ')'. */
35116 cp_lexer_consume_token (parser
->lexer
);
35118 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
35119 tile
= nreverse (tile
);
35120 OMP_CLAUSE_TILE_LIST (c
) = tile
;
35121 OMP_CLAUSE_CHAIN (c
) = list
;
35126 Parse wait clause or directive parameters. */
35129 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
35131 vec
<tree
, va_gc
> *args
;
35134 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
35136 /*allow_expansion_p=*/true,
35137 /*non_constant_p=*/NULL
);
35139 if (args
== NULL
|| args
->length () == 0)
35143 cp_parser_error (parser
, "expected integer expression list");
35144 release_tree_vector (args
);
35149 args_tree
= build_tree_list_vec (args
);
35151 release_tree_vector (args
);
35153 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
35155 tree targ
= TREE_VALUE (t
);
35157 if (targ
!= error_mark_node
)
35159 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
35160 error ("%<wait%> expression must be integral");
35163 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
35165 targ
= mark_rvalue_use (targ
);
35166 OMP_CLAUSE_DECL (c
) = targ
;
35167 OMP_CLAUSE_CHAIN (c
) = list
;
35177 wait [( int-expr-list )] */
35180 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
35182 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
35184 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
35185 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
35188 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
35190 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
35191 OMP_CLAUSE_CHAIN (c
) = list
;
35199 collapse ( constant-expression ) */
35202 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
35208 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35209 matching_parens parens
;
35210 if (!parens
.require_open (parser
))
35213 num
= cp_parser_constant_expression (parser
);
35215 if (!parens
.require_close (parser
))
35216 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35217 /*or_comma=*/false,
35218 /*consume_paren=*/true);
35220 if (num
== error_mark_node
)
35222 num
= fold_non_dependent_expr (num
);
35223 if (!tree_fits_shwi_p (num
)
35224 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
35225 || (n
= tree_to_shwi (num
)) <= 0
35228 error_at (loc
, "collapse argument needs positive constant integer expression");
35232 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
35233 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
35234 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
35235 OMP_CLAUSE_CHAIN (c
) = list
;
35236 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
35242 default ( none | shared )
35245 default ( none | present ) */
35248 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
35249 location_t location
, bool is_oacc
)
35251 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
35254 matching_parens parens
;
35255 if (!parens
.require_open (parser
))
35257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35259 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35260 const char *p
= IDENTIFIER_POINTER (id
);
35265 if (strcmp ("none", p
) != 0)
35267 kind
= OMP_CLAUSE_DEFAULT_NONE
;
35271 if (strcmp ("present", p
) != 0 || !is_oacc
)
35273 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
35277 if (strcmp ("shared", p
) != 0 || is_oacc
)
35279 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
35286 cp_lexer_consume_token (parser
->lexer
);
35292 cp_parser_error (parser
, "expected %<none%> or %<present%>");
35294 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
35297 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
35298 || !parens
.require_close (parser
))
35299 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35300 /*or_comma=*/false,
35301 /*consume_paren=*/true);
35303 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
35306 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
35307 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
35308 OMP_CLAUSE_CHAIN (c
) = list
;
35309 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
35315 final ( expression ) */
35318 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
35322 matching_parens parens
;
35323 if (!parens
.require_open (parser
))
35326 t
= cp_parser_assignment_expression (parser
);
35328 if (t
== error_mark_node
35329 || !parens
.require_close (parser
))
35330 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35331 /*or_comma=*/false,
35332 /*consume_paren=*/true);
35334 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
35336 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
35337 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
35338 OMP_CLAUSE_CHAIN (c
) = list
;
35347 if ( directive-name-modifier : expression )
35349 directive-name-modifier:
35350 parallel | task | taskloop | target data | target | target update
35351 | target enter data | target exit data
35354 directive-name-modifier:
35355 ... | simd | cancel */
35358 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
35362 enum tree_code if_modifier
= ERROR_MARK
;
35364 matching_parens parens
;
35365 if (!parens
.require_open (parser
))
35368 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35370 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35371 const char *p
= IDENTIFIER_POINTER (id
);
35374 if (strcmp ("cancel", p
) == 0)
35375 if_modifier
= VOID_CST
;
35376 else if (strcmp ("parallel", p
) == 0)
35377 if_modifier
= OMP_PARALLEL
;
35378 else if (strcmp ("simd", p
) == 0)
35379 if_modifier
= OMP_SIMD
;
35380 else if (strcmp ("task", p
) == 0)
35381 if_modifier
= OMP_TASK
;
35382 else if (strcmp ("taskloop", p
) == 0)
35383 if_modifier
= OMP_TASKLOOP
;
35384 else if (strcmp ("target", p
) == 0)
35386 if_modifier
= OMP_TARGET
;
35387 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35389 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
35390 p
= IDENTIFIER_POINTER (id
);
35391 if (strcmp ("data", p
) == 0)
35392 if_modifier
= OMP_TARGET_DATA
;
35393 else if (strcmp ("update", p
) == 0)
35394 if_modifier
= OMP_TARGET_UPDATE
;
35395 else if (strcmp ("enter", p
) == 0)
35396 if_modifier
= OMP_TARGET_ENTER_DATA
;
35397 else if (strcmp ("exit", p
) == 0)
35398 if_modifier
= OMP_TARGET_EXIT_DATA
;
35399 if (if_modifier
!= OMP_TARGET
)
35404 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
35405 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
35407 if_modifier
= ERROR_MARK
;
35409 if (if_modifier
== OMP_TARGET_ENTER_DATA
35410 || if_modifier
== OMP_TARGET_EXIT_DATA
)
35412 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
35414 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
35415 p
= IDENTIFIER_POINTER (id
);
35416 if (strcmp ("data", p
) == 0)
35422 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
35423 error_at (loc
, "expected %<data%>");
35424 if_modifier
= ERROR_MARK
;
35429 if (if_modifier
!= ERROR_MARK
)
35431 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
35434 cp_lexer_consume_token (parser
->lexer
);
35441 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
35442 error_at (loc
, "expected %<:%>");
35444 if_modifier
= ERROR_MARK
;
35449 t
= cp_parser_assignment_expression (parser
);
35451 if (t
== error_mark_node
35452 || !parens
.require_close (parser
))
35453 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35454 /*or_comma=*/false,
35455 /*consume_paren=*/true);
35457 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35458 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
35460 if (if_modifier
!= ERROR_MARK
35461 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
35463 const char *p
= NULL
;
35464 switch (if_modifier
)
35466 case VOID_CST
: p
= "cancel"; break;
35467 case OMP_PARALLEL
: p
= "parallel"; break;
35468 case OMP_SIMD
: p
= "simd"; break;
35469 case OMP_TASK
: p
= "task"; break;
35470 case OMP_TASKLOOP
: p
= "taskloop"; break;
35471 case OMP_TARGET_DATA
: p
= "target data"; break;
35472 case OMP_TARGET
: p
= "target"; break;
35473 case OMP_TARGET_UPDATE
: p
= "target update"; break;
35474 case OMP_TARGET_ENTER_DATA
: p
= "target enter data"; break;
35475 case OMP_TARGET_EXIT_DATA
: p
= "target exit data"; break;
35476 default: gcc_unreachable ();
35478 error_at (location
, "too many %<if%> clauses with %qs modifier",
35482 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
35485 error_at (location
, "too many %<if%> clauses");
35487 error_at (location
, "too many %<if%> clauses without modifier");
35490 else if (if_modifier
== ERROR_MARK
35491 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
35493 error_at (location
, "if any %<if%> clause has modifier, then all "
35494 "%<if%> clauses have to use modifier");
35499 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
35500 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
35501 OMP_CLAUSE_IF_EXPR (c
) = t
;
35502 OMP_CLAUSE_CHAIN (c
) = list
;
35511 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
35512 tree list
, location_t location
)
35516 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
35519 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
35520 OMP_CLAUSE_CHAIN (c
) = list
;
35528 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
35529 tree list
, location_t location
)
35533 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
35535 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
35536 OMP_CLAUSE_CHAIN (c
) = list
;
35541 num_threads ( expression ) */
35544 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
35545 location_t location
)
35549 matching_parens parens
;
35550 if (!parens
.require_open (parser
))
35553 t
= cp_parser_assignment_expression (parser
);
35555 if (t
== error_mark_node
35556 || !parens
.require_close (parser
))
35557 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35558 /*or_comma=*/false,
35559 /*consume_paren=*/true);
35561 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
35562 "num_threads", location
);
35564 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
35565 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
35566 OMP_CLAUSE_CHAIN (c
) = list
;
35572 num_tasks ( expression ) */
35575 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
35576 location_t location
)
35580 matching_parens parens
;
35581 if (!parens
.require_open (parser
))
35584 t
= cp_parser_assignment_expression (parser
);
35586 if (t
== error_mark_node
35587 || !parens
.require_close (parser
))
35588 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35589 /*or_comma=*/false,
35590 /*consume_paren=*/true);
35592 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
35593 "num_tasks", location
);
35595 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
35596 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
35597 OMP_CLAUSE_CHAIN (c
) = list
;
35603 grainsize ( expression ) */
35606 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
35607 location_t location
)
35611 matching_parens parens
;
35612 if (!parens
.require_open (parser
))
35615 t
= cp_parser_assignment_expression (parser
);
35617 if (t
== error_mark_node
35618 || !parens
.require_close (parser
))
35619 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35620 /*or_comma=*/false,
35621 /*consume_paren=*/true);
35623 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
35624 "grainsize", location
);
35626 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
35627 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
35628 OMP_CLAUSE_CHAIN (c
) = list
;
35634 priority ( expression ) */
35637 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
35638 location_t location
)
35642 matching_parens parens
;
35643 if (!parens
.require_open (parser
))
35646 t
= cp_parser_assignment_expression (parser
);
35648 if (t
== error_mark_node
35649 || !parens
.require_close (parser
))
35650 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35651 /*or_comma=*/false,
35652 /*consume_paren=*/true);
35654 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
35655 "priority", location
);
35657 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
35658 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
35659 OMP_CLAUSE_CHAIN (c
) = list
;
35665 hint ( expression ) */
35668 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
35672 matching_parens parens
;
35673 if (!parens
.require_open (parser
))
35676 t
= cp_parser_assignment_expression (parser
);
35678 if (t
!= error_mark_node
)
35680 t
= fold_non_dependent_expr (t
);
35681 if (!value_dependent_expression_p (t
)
35682 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
35683 || !tree_fits_shwi_p (t
)
35684 || tree_int_cst_sgn (t
) == -1))
35685 error_at (location
, "expected constant integer expression with "
35686 "valid sync-hint value");
35688 if (t
== error_mark_node
35689 || !parens
.require_close (parser
))
35690 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35691 /*or_comma=*/false,
35692 /*consume_paren=*/true);
35693 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
35695 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
35696 OMP_CLAUSE_HINT_EXPR (c
) = t
;
35697 OMP_CLAUSE_CHAIN (c
) = list
;
35703 defaultmap ( tofrom : scalar )
35706 defaultmap ( implicit-behavior [ : variable-category ] ) */
35709 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
35710 location_t location
)
35714 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
35715 enum omp_clause_defaultmap_kind category
35716 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
35718 matching_parens parens
;
35719 if (!parens
.require_open (parser
))
35722 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
35724 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35727 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
35728 "%<tofrom%>, %<firstprivate%>, %<none%> "
35734 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35735 p
= IDENTIFIER_POINTER (id
);
35741 if (strcmp ("alloc", p
) == 0)
35742 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
35744 goto invalid_behavior
;
35748 if (strcmp ("default", p
) == 0)
35749 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
35751 goto invalid_behavior
;
35755 if (strcmp ("firstprivate", p
) == 0)
35756 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
35757 else if (strcmp ("from", p
) == 0)
35758 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
35760 goto invalid_behavior
;
35764 if (strcmp ("none", p
) == 0)
35765 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
35767 goto invalid_behavior
;
35771 if (strcmp ("tofrom", p
) == 0)
35772 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
35773 else if (strcmp ("to", p
) == 0)
35774 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
35776 goto invalid_behavior
;
35780 goto invalid_behavior
;
35782 cp_lexer_consume_token (parser
->lexer
);
35784 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
35786 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35789 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35792 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
35796 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35797 p
= IDENTIFIER_POINTER (id
);
35802 if (strcmp ("aggregate", p
) == 0)
35803 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
35805 goto invalid_category
;
35809 if (strcmp ("pointer", p
) == 0)
35810 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
35812 goto invalid_category
;
35816 if (strcmp ("scalar", p
) == 0)
35817 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
35819 goto invalid_category
;
35823 goto invalid_category
;
35826 cp_lexer_consume_token (parser
->lexer
);
35828 if (!parens
.require_close (parser
))
35831 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35832 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
35833 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
35834 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
35835 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
35836 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
35838 enum omp_clause_defaultmap_kind cat
= category
;
35839 location_t loc
= OMP_CLAUSE_LOCATION (c
);
35840 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
35841 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
35845 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
35848 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
35851 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
35854 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
35858 gcc_unreachable ();
35861 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
35864 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
35869 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
35870 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
35871 OMP_CLAUSE_CHAIN (c
) = list
;
35875 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35876 /*or_comma=*/false,
35877 /*consume_paren=*/true);
35882 order ( concurrent ) */
35885 cp_parser_omp_clause_order (cp_parser
*parser
, tree list
, location_t location
)
35890 matching_parens parens
;
35891 if (!parens
.require_open (parser
))
35894 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35896 cp_parser_error (parser
, "expected %<concurrent%>");
35901 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35902 p
= IDENTIFIER_POINTER (id
);
35904 if (strcmp (p
, "concurrent") != 0)
35906 cp_parser_error (parser
, "expected %<concurrent%>");
35909 cp_lexer_consume_token (parser
->lexer
);
35910 if (!parens
.require_close (parser
))
35913 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
35914 c
= build_omp_clause (location
, OMP_CLAUSE_ORDER
);
35915 OMP_CLAUSE_CHAIN (c
) = list
;
35919 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35920 /*or_comma=*/false,
35921 /*consume_paren=*/true);
35926 bind ( teams | parallel | thread ) */
35929 cp_parser_omp_clause_bind (cp_parser
*parser
, tree list
,
35930 location_t location
)
35934 enum omp_clause_bind_kind kind
= OMP_CLAUSE_BIND_THREAD
;
35936 matching_parens parens
;
35937 if (!parens
.require_open (parser
))
35940 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35943 cp_parser_error (parser
,
35944 "expected %<teams%>, %<parallel%> or %<thread%>");
35949 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35950 p
= IDENTIFIER_POINTER (id
);
35952 if (strcmp (p
, "teams") == 0)
35953 kind
= OMP_CLAUSE_BIND_TEAMS
;
35954 else if (strcmp (p
, "parallel") == 0)
35955 kind
= OMP_CLAUSE_BIND_PARALLEL
;
35956 else if (strcmp (p
, "thread") != 0)
35958 cp_lexer_consume_token (parser
->lexer
);
35959 if (!parens
.require_close (parser
))
35962 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
35963 c
= build_omp_clause (location
, OMP_CLAUSE_BIND
);
35964 OMP_CLAUSE_BIND_KIND (c
) = kind
;
35965 OMP_CLAUSE_CHAIN (c
) = list
;
35969 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35970 /*or_comma=*/false,
35971 /*consume_paren=*/true);
35979 ordered ( constant-expression ) */
35982 cp_parser_omp_clause_ordered (cp_parser
*parser
,
35983 tree list
, location_t location
)
35985 tree c
, num
= NULL_TREE
;
35988 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
35989 "ordered", location
);
35991 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35993 matching_parens parens
;
35994 parens
.consume_open (parser
);
35996 num
= cp_parser_constant_expression (parser
);
35998 if (!parens
.require_close (parser
))
35999 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36000 /*or_comma=*/false,
36001 /*consume_paren=*/true);
36003 if (num
== error_mark_node
)
36005 num
= fold_non_dependent_expr (num
);
36006 if (!tree_fits_shwi_p (num
)
36007 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
36008 || (n
= tree_to_shwi (num
)) <= 0
36011 error_at (location
,
36012 "ordered argument needs positive constant integer "
36018 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
36019 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
36020 OMP_CLAUSE_CHAIN (c
) = list
;
36025 reduction ( reduction-operator : variable-list )
36027 reduction-operator:
36028 One of: + * - & ^ | && ||
36032 reduction-operator:
36033 One of: + * - & ^ | && || min max
36037 reduction-operator:
36038 One of: + * - & ^ | && ||
36042 reduction ( reduction-modifier, reduction-operator : variable-list )
36043 in_reduction ( reduction-operator : variable-list )
36044 task_reduction ( reduction-operator : variable-list ) */
36047 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
36048 bool is_omp
, tree list
)
36050 enum tree_code code
= ERROR_MARK
;
36051 tree nlist
, c
, id
= NULL_TREE
;
36053 bool inscan
= false;
36055 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36058 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
36060 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
36061 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
36063 cp_lexer_consume_token (parser
->lexer
);
36064 cp_lexer_consume_token (parser
->lexer
);
36066 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36067 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
36069 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36070 const char *p
= IDENTIFIER_POINTER (id
);
36071 if (strcmp (p
, "task") == 0)
36073 else if (strcmp (p
, "inscan") == 0)
36075 if (task
|| inscan
)
36077 cp_lexer_consume_token (parser
->lexer
);
36078 cp_lexer_consume_token (parser
->lexer
);
36083 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
36085 case CPP_PLUS
: code
= PLUS_EXPR
; break;
36086 case CPP_MULT
: code
= MULT_EXPR
; break;
36087 case CPP_MINUS
: code
= MINUS_EXPR
; break;
36088 case CPP_AND
: code
= BIT_AND_EXPR
; break;
36089 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
36090 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
36091 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
36092 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
36096 if (code
!= ERROR_MARK
)
36097 cp_lexer_consume_token (parser
->lexer
);
36100 bool saved_colon_corrects_to_scope_p
;
36101 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36102 parser
->colon_corrects_to_scope_p
= false;
36103 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
36104 /*check_dependency_p=*/true,
36105 /*template_p=*/NULL
,
36106 /*declarator_p=*/false,
36107 /*optional_p=*/false);
36108 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36109 if (identifier_p (id
))
36111 const char *p
= IDENTIFIER_POINTER (id
);
36113 if (strcmp (p
, "min") == 0)
36115 else if (strcmp (p
, "max") == 0)
36117 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
36119 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
36121 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
36123 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
36124 code
= BIT_AND_EXPR
;
36125 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
36126 code
= BIT_IOR_EXPR
;
36127 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
36128 code
= BIT_XOR_EXPR
;
36129 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
36130 code
= TRUTH_ANDIF_EXPR
;
36131 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
36132 code
= TRUTH_ORIF_EXPR
;
36133 id
= omp_reduction_id (code
, id
, NULL_TREE
);
36134 tree scope
= parser
->scope
;
36136 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
36137 parser
->scope
= NULL_TREE
;
36138 parser
->qualifying_scope
= NULL_TREE
;
36139 parser
->object_scope
= NULL_TREE
;
36143 error ("invalid reduction-identifier");
36145 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36146 /*or_comma=*/false,
36147 /*consume_paren=*/true);
36152 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36155 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
36157 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36159 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
36161 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
36163 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
36164 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
36171 schedule ( schedule-kind )
36172 schedule ( schedule-kind , expression )
36175 static | dynamic | guided | runtime | auto
36178 schedule ( schedule-modifier : schedule-kind )
36179 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
36187 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
36190 int modifiers
= 0, nmodifiers
= 0;
36192 matching_parens parens
;
36193 if (!parens
.require_open (parser
))
36196 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
36198 location_t comma
= UNKNOWN_LOCATION
;
36199 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36201 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36202 const char *p
= IDENTIFIER_POINTER (id
);
36203 if (strcmp ("simd", p
) == 0)
36204 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
36205 else if (strcmp ("monotonic", p
) == 0)
36206 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
36207 else if (strcmp ("nonmonotonic", p
) == 0)
36208 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
36211 comma
= UNKNOWN_LOCATION
;
36212 cp_lexer_consume_token (parser
->lexer
);
36213 if (nmodifiers
++ == 0
36214 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36216 comma
= cp_lexer_peek_token (parser
->lexer
)->location
;
36217 cp_lexer_consume_token (parser
->lexer
);
36221 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36225 if (comma
!= UNKNOWN_LOCATION
)
36226 error_at (comma
, "expected %<:%>");
36228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36230 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36231 const char *p
= IDENTIFIER_POINTER (id
);
36236 if (strcmp ("dynamic", p
) != 0)
36238 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
36242 if (strcmp ("guided", p
) != 0)
36244 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
36248 if (strcmp ("runtime", p
) != 0)
36250 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
36257 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
36258 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
36259 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
36260 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
36263 cp_lexer_consume_token (parser
->lexer
);
36265 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
36266 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
36267 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36268 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
36270 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36275 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36278 cp_lexer_consume_token (parser
->lexer
);
36280 token
= cp_lexer_peek_token (parser
->lexer
);
36281 t
= cp_parser_assignment_expression (parser
);
36283 if (t
== error_mark_node
)
36285 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
36286 error_at (token
->location
, "schedule %<runtime%> does not take "
36287 "a %<chunk_size%> parameter");
36288 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
36289 error_at (token
->location
, "schedule %<auto%> does not take "
36290 "a %<chunk_size%> parameter");
36292 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
36294 if (!parens
.require_close (parser
))
36297 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
36300 OMP_CLAUSE_SCHEDULE_KIND (c
)
36301 = (enum omp_clause_schedule_kind
)
36302 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
36304 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
36305 OMP_CLAUSE_CHAIN (c
) = list
;
36309 cp_parser_error (parser
, "invalid schedule kind");
36311 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36312 /*or_comma=*/false,
36313 /*consume_paren=*/true);
36321 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
36322 tree list
, location_t location
)
36326 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
36328 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
36329 OMP_CLAUSE_CHAIN (c
) = list
;
36338 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
36339 tree list
, location_t location
)
36341 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
36342 tree c
= build_omp_clause (location
, code
);
36343 OMP_CLAUSE_CHAIN (c
) = list
;
36354 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
36355 enum omp_clause_code code
,
36356 tree list
, location_t location
)
36358 tree c
= build_omp_clause (location
, code
);
36359 OMP_CLAUSE_CHAIN (c
) = list
;
36367 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
36368 tree list
, location_t location
)
36370 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
36371 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
36372 OMP_CLAUSE_CHAIN (c
) = list
;
36381 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
36382 enum omp_clause_code code
,
36383 tree list
, location_t location
)
36385 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
36386 tree c
= build_omp_clause (location
, code
);
36387 OMP_CLAUSE_CHAIN (c
) = list
;
36392 num_teams ( expression ) */
36395 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
36396 location_t location
)
36400 matching_parens parens
;
36401 if (!parens
.require_open (parser
))
36404 t
= cp_parser_assignment_expression (parser
);
36406 if (t
== error_mark_node
36407 || !parens
.require_close (parser
))
36408 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36409 /*or_comma=*/false,
36410 /*consume_paren=*/true);
36412 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
36413 "num_teams", location
);
36415 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
36416 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
36417 OMP_CLAUSE_CHAIN (c
) = list
;
36423 thread_limit ( expression ) */
36426 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
36427 location_t location
)
36431 matching_parens parens
;
36432 if (!parens
.require_open (parser
))
36435 t
= cp_parser_assignment_expression (parser
);
36437 if (t
== error_mark_node
36438 || !parens
.require_close (parser
))
36439 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36440 /*or_comma=*/false,
36441 /*consume_paren=*/true);
36443 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
36444 "thread_limit", location
);
36446 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
36447 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
36448 OMP_CLAUSE_CHAIN (c
) = list
;
36454 aligned ( variable-list )
36455 aligned ( variable-list : constant-expression ) */
36458 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
36460 tree nlist
, c
, alignment
= NULL_TREE
;
36463 matching_parens parens
;
36464 if (!parens
.require_open (parser
))
36467 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
36472 alignment
= cp_parser_constant_expression (parser
);
36474 if (!parens
.require_close (parser
))
36475 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36476 /*or_comma=*/false,
36477 /*consume_paren=*/true);
36479 if (alignment
== error_mark_node
)
36480 alignment
= NULL_TREE
;
36483 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36484 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
36490 allocate ( variable-list )
36491 allocate ( expression : variable-list ) */
36494 cp_parser_omp_clause_allocate (cp_parser
*parser
, tree list
)
36496 tree nlist
, c
, allocator
= NULL_TREE
;
36499 matching_parens parens
;
36500 if (!parens
.require_open (parser
))
36503 cp_parser_parse_tentatively (parser
);
36504 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36505 parser
->colon_corrects_to_scope_p
= false;
36506 allocator
= cp_parser_assignment_expression (parser
);
36507 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
36510 cp_parser_parse_definitely (parser
);
36511 cp_lexer_consume_token (parser
->lexer
);
36512 if (allocator
== error_mark_node
)
36513 allocator
= NULL_TREE
;
36517 cp_parser_abort_tentative_parse (parser
);
36518 allocator
= NULL_TREE
;
36521 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALLOCATE
, list
,
36524 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36525 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
36531 lastprivate ( variable-list )
36534 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
36537 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
36539 bool conditional
= false;
36541 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36545 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
36547 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36548 const char *p
= IDENTIFIER_POINTER (id
);
36550 if (strcmp ("conditional", p
) == 0)
36552 conditional
= true;
36553 cp_lexer_consume_token (parser
->lexer
);
36554 cp_lexer_consume_token (parser
->lexer
);
36558 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
36562 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36563 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
36568 linear ( variable-list )
36569 linear ( variable-list : expression )
36572 linear ( modifier ( variable-list ) )
36573 linear ( modifier ( variable-list ) : expression ) */
36576 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
36579 tree nlist
, c
, step
= integer_one_node
;
36581 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
36583 matching_parens parens
;
36584 if (!parens
.require_open (parser
))
36587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36589 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36590 const char *p
= IDENTIFIER_POINTER (id
);
36592 if (strcmp ("ref", p
) == 0)
36593 kind
= OMP_CLAUSE_LINEAR_REF
;
36594 else if (strcmp ("val", p
) == 0)
36595 kind
= OMP_CLAUSE_LINEAR_VAL
;
36596 else if (strcmp ("uval", p
) == 0)
36597 kind
= OMP_CLAUSE_LINEAR_UVAL
;
36598 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
36599 cp_lexer_consume_token (parser
->lexer
);
36601 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
36604 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
36605 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
36609 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
36610 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
36612 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36613 else if (!parens
.require_close (parser
))
36614 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36615 /*or_comma=*/false,
36616 /*consume_paren=*/true);
36623 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36624 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
36626 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36627 cp_parser_parse_tentatively (parser
);
36628 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
36629 /*check_dependency_p=*/true,
36630 /*template_p=*/NULL
,
36631 /*declarator_p=*/false,
36632 /*optional_p=*/false);
36633 if (step
!= error_mark_node
)
36634 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
36635 if (step
== error_mark_node
)
36638 cp_parser_abort_tentative_parse (parser
);
36640 else if (!cp_parser_parse_definitely (parser
))
36644 step
= cp_parser_assignment_expression (parser
);
36646 if (!parens
.require_close (parser
))
36647 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36648 /*or_comma=*/false,
36649 /*consume_paren=*/true);
36651 if (step
== error_mark_node
)
36655 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36657 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
36658 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
36665 safelen ( constant-expression ) */
36668 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
36669 location_t location
)
36673 matching_parens parens
;
36674 if (!parens
.require_open (parser
))
36677 t
= cp_parser_constant_expression (parser
);
36679 if (t
== error_mark_node
36680 || !parens
.require_close (parser
))
36681 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36682 /*or_comma=*/false,
36683 /*consume_paren=*/true);
36685 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
36687 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
36688 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
36689 OMP_CLAUSE_CHAIN (c
) = list
;
36695 simdlen ( constant-expression ) */
36698 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
36699 location_t location
)
36703 matching_parens parens
;
36704 if (!parens
.require_open (parser
))
36707 t
= cp_parser_constant_expression (parser
);
36709 if (t
== error_mark_node
36710 || !parens
.require_close (parser
))
36711 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36712 /*or_comma=*/false,
36713 /*consume_paren=*/true);
36715 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
36717 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
36718 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
36719 OMP_CLAUSE_CHAIN (c
) = list
;
36726 identifier [+/- integer]
36727 vec , identifier [+/- integer]
36731 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
36736 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36738 cp_parser_error (parser
, "expected identifier");
36742 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36744 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36745 tree t
, identifier
= cp_parser_identifier (parser
);
36746 tree addend
= NULL
;
36748 if (identifier
== error_mark_node
)
36749 t
= error_mark_node
;
36752 t
= cp_parser_lookup_name_simple
36753 (parser
, identifier
,
36754 cp_lexer_peek_token (parser
->lexer
)->location
);
36755 if (t
== error_mark_node
)
36756 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
36761 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
36763 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
36765 addend
= integer_zero_node
;
36766 goto add_to_vector
;
36768 cp_lexer_consume_token (parser
->lexer
);
36770 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
36772 cp_parser_error (parser
, "expected integer");
36776 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36777 if (TREE_CODE (addend
) != INTEGER_CST
)
36779 cp_parser_error (parser
, "expected integer");
36782 cp_lexer_consume_token (parser
->lexer
);
36785 if (t
!= error_mark_node
)
36787 vec
= tree_cons (addend
, t
, vec
);
36789 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
36792 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
36795 cp_lexer_consume_token (parser
->lexer
);
36798 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
36800 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
36801 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
36802 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
36803 OMP_CLAUSE_CHAIN (u
) = list
;
36810 iterators ( iterators-definition )
36812 iterators-definition:
36814 iterator-specifier , iterators-definition
36816 iterator-specifier:
36817 identifier = range-specification
36818 iterator-type identifier = range-specification
36820 range-specification:
36822 begin : end : step */
36825 cp_parser_omp_iterators (cp_parser
*parser
)
36827 tree ret
= NULL_TREE
, *last
= &ret
;
36828 cp_lexer_consume_token (parser
->lexer
);
36830 matching_parens parens
;
36831 if (!parens
.require_open (parser
))
36832 return error_mark_node
;
36834 bool saved_colon_corrects_to_scope_p
36835 = parser
->colon_corrects_to_scope_p
;
36836 bool saved_colon_doesnt_start_class_def_p
36837 = parser
->colon_doesnt_start_class_def_p
;
36842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36843 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
36844 iter_type
= integer_type_node
;
36847 const char *saved_message
36848 = parser
->type_definition_forbidden_message
;
36849 parser
->type_definition_forbidden_message
36850 = G_("types may not be defined in iterator type");
36852 iter_type
= cp_parser_type_id (parser
);
36854 parser
->type_definition_forbidden_message
= saved_message
;
36857 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36858 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36860 cp_parser_error (parser
, "expected identifier");
36864 tree id
= cp_parser_identifier (parser
);
36865 if (id
== error_mark_node
)
36868 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36871 parser
->colon_corrects_to_scope_p
= false;
36872 parser
->colon_doesnt_start_class_def_p
= true;
36873 tree begin
= cp_parser_assignment_expression (parser
);
36875 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36878 tree end
= cp_parser_assignment_expression (parser
);
36880 tree step
= integer_one_node
;
36881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
36883 cp_lexer_consume_token (parser
->lexer
);
36884 step
= cp_parser_assignment_expression (parser
);
36887 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
36888 DECL_ARTIFICIAL (iter_var
) = 1;
36889 DECL_CONTEXT (iter_var
) = current_function_decl
;
36890 pushdecl (iter_var
);
36892 *last
= make_tree_vec (6);
36893 TREE_VEC_ELT (*last
, 0) = iter_var
;
36894 TREE_VEC_ELT (*last
, 1) = begin
;
36895 TREE_VEC_ELT (*last
, 2) = end
;
36896 TREE_VEC_ELT (*last
, 3) = step
;
36897 last
= &TREE_CHAIN (*last
);
36899 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36901 cp_lexer_consume_token (parser
->lexer
);
36908 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36909 parser
->colon_doesnt_start_class_def_p
36910 = saved_colon_doesnt_start_class_def_p
;
36912 if (!parens
.require_close (parser
))
36913 cp_parser_skip_to_closing_parenthesis (parser
,
36914 /*recovering=*/true,
36915 /*or_comma=*/false,
36916 /*consume_paren=*/true);
36918 return ret
? ret
: error_mark_node
;
36922 depend ( depend-kind : variable-list )
36930 depend ( sink : vec )
36933 depend ( depend-modifier , depend-kind: variable-list )
36936 in | out | inout | mutexinoutset | depobj
36939 iterator ( iterators-definition ) */
36942 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
36944 tree nlist
, c
, iterators
= NULL_TREE
;
36945 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
36947 matching_parens parens
;
36948 if (!parens
.require_open (parser
))
36953 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36956 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36957 const char *p
= IDENTIFIER_POINTER (id
);
36959 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
36961 begin_scope (sk_omp
, NULL
);
36962 iterators
= cp_parser_omp_iterators (parser
);
36963 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
36966 if (strcmp ("in", p
) == 0)
36967 kind
= OMP_CLAUSE_DEPEND_IN
;
36968 else if (strcmp ("inout", p
) == 0)
36969 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36970 else if (strcmp ("mutexinoutset", p
) == 0)
36971 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36972 else if (strcmp ("out", p
) == 0)
36973 kind
= OMP_CLAUSE_DEPEND_OUT
;
36974 else if (strcmp ("depobj", p
) == 0)
36975 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
36976 else if (strcmp ("sink", p
) == 0)
36977 kind
= OMP_CLAUSE_DEPEND_SINK
;
36978 else if (strcmp ("source", p
) == 0)
36979 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36986 cp_lexer_consume_token (parser
->lexer
);
36989 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
36991 poplevel (0, 1, 0);
36992 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
36993 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
36994 iterators
= NULL_TREE
;
36997 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36999 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
37000 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
37001 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
37002 OMP_CLAUSE_CHAIN (c
) = list
;
37003 if (!parens
.require_close (parser
))
37004 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37005 /*or_comma=*/false,
37006 /*consume_paren=*/true);
37010 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37013 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
37014 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
37017 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
37022 tree block
= poplevel (1, 1, 0);
37023 if (iterators
== error_mark_node
)
37024 iterators
= NULL_TREE
;
37026 TREE_VEC_ELT (iterators
, 5) = block
;
37029 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37031 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
37033 OMP_CLAUSE_DECL (c
)
37034 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
37040 cp_parser_error (parser
, "invalid depend kind");
37043 poplevel (0, 1, 0);
37044 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37045 /*or_comma=*/false,
37046 /*consume_paren=*/true);
37051 map ( map-kind : variable-list )
37052 map ( variable-list )
37055 alloc | to | from | tofrom
37059 alloc | to | from | tofrom | release | delete
37061 map ( always [,] map-kind: variable-list ) */
37064 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
37067 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
37068 bool always
= false;
37070 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37073 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37075 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37076 const char *p
= IDENTIFIER_POINTER (id
);
37078 if (strcmp ("always", p
) == 0)
37081 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
37083 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
37084 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
37086 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
37090 cp_lexer_consume_token (parser
->lexer
);
37092 cp_lexer_consume_token (parser
->lexer
);
37097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37098 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
37100 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37101 const char *p
= IDENTIFIER_POINTER (id
);
37103 if (strcmp ("alloc", p
) == 0)
37104 kind
= GOMP_MAP_ALLOC
;
37105 else if (strcmp ("to", p
) == 0)
37106 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
37107 else if (strcmp ("from", p
) == 0)
37108 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
37109 else if (strcmp ("tofrom", p
) == 0)
37110 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
37111 else if (strcmp ("release", p
) == 0)
37112 kind
= GOMP_MAP_RELEASE
;
37115 cp_parser_error (parser
, "invalid map kind");
37116 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37117 /*or_comma=*/false,
37118 /*consume_paren=*/true);
37121 cp_lexer_consume_token (parser
->lexer
);
37122 cp_lexer_consume_token (parser
->lexer
);
37124 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
37125 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
37127 kind
= GOMP_MAP_DELETE
;
37128 cp_lexer_consume_token (parser
->lexer
);
37129 cp_lexer_consume_token (parser
->lexer
);
37132 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
37135 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37136 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
37142 device ( expression ) */
37145 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
37146 location_t location
)
37150 matching_parens parens
;
37151 if (!parens
.require_open (parser
))
37154 t
= cp_parser_assignment_expression (parser
);
37156 if (t
== error_mark_node
37157 || !parens
.require_close (parser
))
37158 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37159 /*or_comma=*/false,
37160 /*consume_paren=*/true);
37162 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
37163 "device", location
);
37165 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
37166 OMP_CLAUSE_DEVICE_ID (c
) = t
;
37167 OMP_CLAUSE_CHAIN (c
) = list
;
37173 dist_schedule ( static )
37174 dist_schedule ( static , expression ) */
37177 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
37178 location_t location
)
37182 matching_parens parens
;
37183 if (!parens
.require_open (parser
))
37186 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
37188 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
37190 cp_lexer_consume_token (parser
->lexer
);
37192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37194 cp_lexer_consume_token (parser
->lexer
);
37196 t
= cp_parser_assignment_expression (parser
);
37198 if (t
== error_mark_node
)
37200 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
37202 if (!parens
.require_close (parser
))
37205 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
37208 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
37209 "dist_schedule", location); */
37210 if (omp_find_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
))
37211 warning_at (location
, 0, "too many %qs clauses", "dist_schedule");
37212 OMP_CLAUSE_CHAIN (c
) = list
;
37216 cp_parser_error (parser
, "invalid dist_schedule kind");
37218 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37219 /*or_comma=*/false,
37220 /*consume_paren=*/true);
37225 proc_bind ( proc-bind-kind )
37228 master | close | spread */
37231 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
37232 location_t location
)
37235 enum omp_clause_proc_bind_kind kind
;
37237 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37242 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37243 const char *p
= IDENTIFIER_POINTER (id
);
37245 if (strcmp ("master", p
) == 0)
37246 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
37247 else if (strcmp ("close", p
) == 0)
37248 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
37249 else if (strcmp ("spread", p
) == 0)
37250 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
37257 cp_lexer_consume_token (parser
->lexer
);
37258 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
37261 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
37262 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
37264 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
37265 OMP_CLAUSE_CHAIN (c
) = list
;
37269 cp_parser_error (parser
, "invalid depend kind");
37271 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37272 /*or_comma=*/false,
37273 /*consume_paren=*/true);
37278 device_type ( host | nohost | any ) */
37281 cp_parser_omp_clause_device_type (cp_parser
*parser
, tree list
,
37282 location_t location
)
37285 enum omp_clause_device_type_kind kind
;
37287 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37292 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37293 const char *p
= IDENTIFIER_POINTER (id
);
37295 if (strcmp ("host", p
) == 0)
37296 kind
= OMP_CLAUSE_DEVICE_TYPE_HOST
;
37297 else if (strcmp ("nohost", p
) == 0)
37298 kind
= OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
37299 else if (strcmp ("any", p
) == 0)
37300 kind
= OMP_CLAUSE_DEVICE_TYPE_ANY
;
37307 cp_lexer_consume_token (parser
->lexer
);
37308 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
37311 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE_TYPE
);
37312 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37314 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = kind
;
37315 OMP_CLAUSE_CHAIN (c
) = list
;
37319 cp_parser_error (parser
, "invalid depend kind");
37321 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37322 /*or_comma=*/false,
37323 /*consume_paren=*/true);
37328 async [( int-expr )] */
37331 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
37334 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37336 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
37338 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37340 matching_parens parens
;
37341 parens
.consume_open (parser
);
37343 t
= cp_parser_expression (parser
);
37344 if (t
== error_mark_node
37345 || !parens
.require_close (parser
))
37346 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37347 /*or_comma=*/false,
37348 /*consume_paren=*/true);
37351 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
37353 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
37354 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
37355 OMP_CLAUSE_CHAIN (c
) = list
;
37361 /* Parse all OpenACC clauses. The set clauses allowed by the directive
37362 is a bitmask in MASK. Return the list of clauses found. */
37365 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
37366 const char *where
, cp_token
*pragma_tok
,
37367 bool finish_p
= true)
37369 tree clauses
= NULL
;
37372 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37375 pragma_omp_clause c_kind
;
37376 omp_clause_code code
;
37377 const char *c_name
;
37378 tree prev
= clauses
;
37380 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37381 cp_lexer_consume_token (parser
->lexer
);
37383 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
37384 c_kind
= cp_parser_omp_clause_name (parser
);
37388 case PRAGMA_OACC_CLAUSE_ASYNC
:
37389 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
37392 case PRAGMA_OACC_CLAUSE_AUTO
:
37393 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
37397 case PRAGMA_OACC_CLAUSE_ATTACH
:
37398 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37401 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
37402 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
37403 c_name
= "collapse";
37405 case PRAGMA_OACC_CLAUSE_COPY
:
37406 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37409 case PRAGMA_OACC_CLAUSE_COPYIN
:
37410 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37413 case PRAGMA_OACC_CLAUSE_COPYOUT
:
37414 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37415 c_name
= "copyout";
37417 case PRAGMA_OACC_CLAUSE_CREATE
:
37418 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37421 case PRAGMA_OACC_CLAUSE_DELETE
:
37422 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37425 case PRAGMA_OMP_CLAUSE_DEFAULT
:
37426 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
37427 c_name
= "default";
37429 case PRAGMA_OACC_CLAUSE_DETACH
:
37430 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37433 case PRAGMA_OACC_CLAUSE_DEVICE
:
37434 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37437 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
37438 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
37439 c_name
= "deviceptr";
37441 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
37442 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37443 c_name
= "device_resident";
37445 case PRAGMA_OACC_CLAUSE_FINALIZE
:
37446 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
37448 c_name
= "finalize";
37450 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
37451 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
37453 c_name
= "firstprivate";
37455 case PRAGMA_OACC_CLAUSE_GANG
:
37457 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
37460 case PRAGMA_OACC_CLAUSE_HOST
:
37461 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37464 case PRAGMA_OACC_CLAUSE_IF
:
37465 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
37468 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
37469 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
37471 c_name
= "if_present";
37473 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
37474 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
37476 c_name
= "independent";
37478 case PRAGMA_OACC_CLAUSE_LINK
:
37479 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37482 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
37483 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37484 c_name
= "no_create";
37486 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
37487 code
= OMP_CLAUSE_NUM_GANGS
;
37488 c_name
= "num_gangs";
37489 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
37492 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
37493 c_name
= "num_workers";
37494 code
= OMP_CLAUSE_NUM_WORKERS
;
37495 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
37498 case PRAGMA_OACC_CLAUSE_PRESENT
:
37499 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37500 c_name
= "present";
37502 case PRAGMA_OACC_CLAUSE_PRIVATE
:
37503 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
37505 c_name
= "private";
37507 case PRAGMA_OACC_CLAUSE_REDUCTION
:
37509 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
37511 c_name
= "reduction";
37513 case PRAGMA_OACC_CLAUSE_SEQ
:
37514 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
37518 case PRAGMA_OACC_CLAUSE_TILE
:
37519 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
37522 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
37523 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
37525 c_name
= "use_device";
37527 case PRAGMA_OACC_CLAUSE_VECTOR
:
37529 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
37533 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
37534 c_name
= "vector_length";
37535 code
= OMP_CLAUSE_VECTOR_LENGTH
;
37536 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
37539 case PRAGMA_OACC_CLAUSE_WAIT
:
37540 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
37543 case PRAGMA_OACC_CLAUSE_WORKER
:
37545 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
37550 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
37556 if (((mask
>> c_kind
) & 1) == 0)
37558 /* Remove the invalid clause(s) from the list to avoid
37559 confusing the rest of the compiler. */
37561 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
37566 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37569 return finish_omp_clauses (clauses
, C_ORT_ACC
);
37574 /* Parse all OpenMP clauses. The set clauses allowed by the directive
37575 is a bitmask in MASK. Return the list of clauses found.
37576 FINISH_P set if finish_omp_clauses should be called.
37577 NESTED non-zero if clauses should be terminated by closing paren instead
37578 of end of pragma. If it is 2, additionally commas are required in between
37582 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
37583 const char *where
, cp_token
*pragma_tok
,
37584 bool finish_p
= true, int nested
= 0)
37586 tree clauses
= NULL
;
37588 cp_token
*token
= NULL
;
37590 /* Don't create location wrapper nodes within OpenMP clauses. */
37591 auto_suppress_location_wrappers sentinel
;
37593 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37595 pragma_omp_clause c_kind
;
37596 const char *c_name
;
37597 tree prev
= clauses
;
37599 if (nested
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
37604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37605 cp_lexer_consume_token (parser
->lexer
);
37606 else if (nested
== 2)
37607 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37608 "clauses in %<simd%> trait should be separated "
37612 token
= cp_lexer_peek_token (parser
->lexer
);
37613 c_kind
= cp_parser_omp_clause_name (parser
);
37617 case PRAGMA_OMP_CLAUSE_BIND
:
37618 clauses
= cp_parser_omp_clause_bind (parser
, clauses
,
37622 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
37623 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
37625 c_name
= "collapse";
37627 case PRAGMA_OMP_CLAUSE_COPYIN
:
37628 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
37631 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
37632 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
37634 c_name
= "copyprivate";
37636 case PRAGMA_OMP_CLAUSE_DEFAULT
:
37637 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
37638 token
->location
, false);
37639 c_name
= "default";
37641 case PRAGMA_OMP_CLAUSE_FINAL
:
37642 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
37645 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
37646 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
37648 c_name
= "firstprivate";
37650 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
37651 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
37653 c_name
= "grainsize";
37655 case PRAGMA_OMP_CLAUSE_HINT
:
37656 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
37660 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
37661 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
37663 c_name
= "defaultmap";
37665 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
37666 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
37668 c_name
= "use_device_ptr";
37670 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
:
37671 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_ADDR
,
37673 c_name
= "use_device_addr";
37675 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
37676 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
37678 c_name
= "is_device_ptr";
37680 case PRAGMA_OMP_CLAUSE_IF
:
37681 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
37685 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
37687 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
37689 c_name
= "in_reduction";
37691 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
37692 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
37693 c_name
= "lastprivate";
37695 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
37696 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
37698 c_name
= "mergeable";
37700 case PRAGMA_OMP_CLAUSE_NOWAIT
:
37701 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
,
37705 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
37706 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
37708 c_name
= "num_tasks";
37710 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
37711 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
37713 c_name
= "num_threads";
37715 case PRAGMA_OMP_CLAUSE_ORDER
:
37716 clauses
= cp_parser_omp_clause_order (parser
, clauses
,
37720 case PRAGMA_OMP_CLAUSE_ORDERED
:
37721 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
37723 c_name
= "ordered";
37725 case PRAGMA_OMP_CLAUSE_PRIORITY
:
37726 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
37728 c_name
= "priority";
37730 case PRAGMA_OMP_CLAUSE_PRIVATE
:
37731 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
37733 c_name
= "private";
37735 case PRAGMA_OMP_CLAUSE_REDUCTION
:
37737 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
37739 c_name
= "reduction";
37741 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
37742 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
37744 c_name
= "schedule";
37746 case PRAGMA_OMP_CLAUSE_SHARED
:
37747 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
37751 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
37753 = cp_parser_omp_clause_reduction (parser
,
37754 OMP_CLAUSE_TASK_REDUCTION
,
37756 c_name
= "task_reduction";
37758 case PRAGMA_OMP_CLAUSE_UNTIED
:
37759 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
37763 case PRAGMA_OMP_CLAUSE_INBRANCH
:
37764 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
37765 clauses
, token
->location
);
37766 c_name
= "inbranch";
37768 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
37769 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
37771 c_name
= "nontemporal";
37773 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
37774 clauses
= cp_parser_omp_clause_branch (parser
,
37775 OMP_CLAUSE_NOTINBRANCH
,
37776 clauses
, token
->location
);
37777 c_name
= "notinbranch";
37779 case PRAGMA_OMP_CLAUSE_PARALLEL
:
37780 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
37781 clauses
, token
->location
);
37782 c_name
= "parallel";
37786 error_at (token
->location
, "%qs must be the first clause of %qs",
37791 case PRAGMA_OMP_CLAUSE_FOR
:
37792 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
37793 clauses
, token
->location
);
37796 goto clause_not_first
;
37798 case PRAGMA_OMP_CLAUSE_SECTIONS
:
37799 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
37800 clauses
, token
->location
);
37801 c_name
= "sections";
37803 goto clause_not_first
;
37805 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
37806 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
37807 clauses
, token
->location
);
37808 c_name
= "taskgroup";
37810 goto clause_not_first
;
37812 case PRAGMA_OMP_CLAUSE_LINK
:
37813 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
37816 case PRAGMA_OMP_CLAUSE_TO
:
37817 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
37818 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37821 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
37824 case PRAGMA_OMP_CLAUSE_FROM
:
37825 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
37828 case PRAGMA_OMP_CLAUSE_UNIFORM
:
37829 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
37831 c_name
= "uniform";
37833 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
37834 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
37836 c_name
= "num_teams";
37838 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
37839 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
37841 c_name
= "thread_limit";
37843 case PRAGMA_OMP_CLAUSE_ALIGNED
:
37844 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
37845 c_name
= "aligned";
37847 case PRAGMA_OMP_CLAUSE_ALLOCATE
:
37848 clauses
= cp_parser_omp_clause_allocate (parser
, clauses
);
37849 c_name
= "allocate";
37851 case PRAGMA_OMP_CLAUSE_LINEAR
:
37853 bool declare_simd
= false;
37854 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
37855 declare_simd
= true;
37856 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
37860 case PRAGMA_OMP_CLAUSE_DEPEND
:
37861 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
37865 case PRAGMA_OMP_CLAUSE_MAP
:
37866 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
37869 case PRAGMA_OMP_CLAUSE_DEVICE
:
37870 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
37874 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
37875 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
37877 c_name
= "dist_schedule";
37879 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
37880 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
37882 c_name
= "proc_bind";
37884 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE
:
37885 clauses
= cp_parser_omp_clause_device_type (parser
, clauses
,
37887 c_name
= "device_type";
37889 case PRAGMA_OMP_CLAUSE_SAFELEN
:
37890 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
37892 c_name
= "safelen";
37894 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
37895 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
37897 c_name
= "simdlen";
37899 case PRAGMA_OMP_CLAUSE_NOGROUP
:
37900 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
37902 c_name
= "nogroup";
37904 case PRAGMA_OMP_CLAUSE_THREADS
:
37906 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
37907 clauses
, token
->location
);
37908 c_name
= "threads";
37910 case PRAGMA_OMP_CLAUSE_SIMD
:
37912 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
37913 clauses
, token
->location
);
37917 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
37923 if (((mask
>> c_kind
) & 1) == 0)
37925 /* Remove the invalid clause(s) from the list to avoid
37926 confusing the rest of the compiler. */
37928 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
37933 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37936 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
37937 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
37939 return finish_omp_clauses (clauses
, C_ORT_OMP
);
37948 In practice, we're also interested in adding the statement to an
37949 outer node. So it is convenient if we work around the fact that
37950 cp_parser_statement calls add_stmt. */
37953 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
37955 unsigned save
= parser
->in_statement
;
37957 /* Only move the values to IN_OMP_BLOCK if they weren't false.
37958 This preserves the "not within loop or switch" style error messages
37959 for nonsense cases like
37965 if (parser
->in_statement
)
37966 parser
->in_statement
= IN_OMP_BLOCK
;
37972 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
37974 parser
->in_statement
= save
;
37978 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
37980 tree stmt
= begin_omp_structured_block ();
37981 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37983 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37985 cp_parser_end_omp_structured_block (parser
, save
);
37986 return finish_omp_structured_block (stmt
);
37990 # pragma omp atomic new-line
37994 x binop= expr | x++ | ++x | x-- | --x
37996 +, *, -, /, &, ^, |, <<, >>
37998 where x is an lvalue expression with scalar type.
38001 # pragma omp atomic new-line
38004 # pragma omp atomic read new-line
38007 # pragma omp atomic write new-line
38010 # pragma omp atomic update new-line
38013 # pragma omp atomic capture new-line
38016 # pragma omp atomic capture new-line
38024 expression-stmt | x = x binop expr
38026 v = expression-stmt
38028 { v = x; update-stmt; } | { update-stmt; v = x; }
38032 expression-stmt | x = x binop expr | x = expr binop x
38036 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
38038 where x and v are lvalue expressions with scalar type. */
38041 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
, bool openacc
)
38043 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
38044 tree rhs1
= NULL_TREE
, orig_lhs
;
38045 location_t loc
= pragma_tok
->location
;
38046 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
38047 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
38048 bool structured_block
= false;
38050 tree clauses
= NULL_TREE
;
38052 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38054 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38055 cp_lexer_consume_token (parser
->lexer
);
38059 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38061 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38062 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38063 const char *p
= IDENTIFIER_POINTER (id
);
38064 enum tree_code new_code
= ERROR_MARK
;
38065 enum omp_memory_order new_memory_order
38066 = OMP_MEMORY_ORDER_UNSPECIFIED
;
38068 if (!strcmp (p
, "read"))
38069 new_code
= OMP_ATOMIC_READ
;
38070 else if (!strcmp (p
, "write"))
38071 new_code
= NOP_EXPR
;
38072 else if (!strcmp (p
, "update"))
38073 new_code
= OMP_ATOMIC
;
38074 else if (!strcmp (p
, "capture"))
38075 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
38079 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
38080 "or %<capture%> clause");
38082 else if (!strcmp (p
, "seq_cst"))
38083 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38084 else if (!strcmp (p
, "acq_rel"))
38085 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
38086 else if (!strcmp (p
, "release"))
38087 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
38088 else if (!strcmp (p
, "acquire"))
38089 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
38090 else if (!strcmp (p
, "relaxed"))
38091 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
38092 else if (!strcmp (p
, "hint"))
38094 cp_lexer_consume_token (parser
->lexer
);
38095 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
38101 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
38102 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
38103 "%<release%>, %<relaxed%> or %<hint%> clause");
38107 if (new_code
!= ERROR_MARK
)
38109 /* OpenACC permits 'update capture'. */
38111 && code
== OMP_ATOMIC
38112 && new_code
== OMP_ATOMIC_CAPTURE_NEW
)
38114 else if (code
!= ERROR_MARK
)
38115 error_at (cloc
, "too many atomic clauses");
38119 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
38121 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
38122 error_at (cloc
, "too many memory order clauses");
38124 memory_order
= new_memory_order
;
38126 cp_lexer_consume_token (parser
->lexer
);
38132 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38134 if (code
== ERROR_MARK
)
38137 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
38138 else if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
38141 = (enum omp_requires
) (omp_requires_mask
38142 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
38143 switch ((enum omp_memory_order
)
38144 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
38146 case OMP_MEMORY_ORDER_UNSPECIFIED
:
38147 case OMP_MEMORY_ORDER_RELAXED
:
38148 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
38150 case OMP_MEMORY_ORDER_SEQ_CST
:
38151 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38153 case OMP_MEMORY_ORDER_ACQ_REL
:
38156 case OMP_ATOMIC_READ
:
38157 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
38159 case NOP_EXPR
: /* atomic write */
38161 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
38164 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
38169 gcc_unreachable ();
38175 case OMP_ATOMIC_READ
:
38176 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
38177 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
38179 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
38180 "%<acq_rel%> or %<release%> clauses");
38181 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38184 case NOP_EXPR
: /* atomic write */
38185 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
38186 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
38188 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
38189 "%<acq_rel%> or %<acquire%> clauses");
38190 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38194 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
38195 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
38197 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
38198 "%<acq_rel%> or %<acquire%> clauses");
38199 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38208 case OMP_ATOMIC_READ
:
38209 case NOP_EXPR
: /* atomic write */
38210 v
= cp_parser_unary_expression (parser
);
38211 if (v
== error_mark_node
)
38213 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38215 if (code
== NOP_EXPR
)
38216 lhs
= cp_parser_expression (parser
);
38218 lhs
= cp_parser_unary_expression (parser
);
38219 if (lhs
== error_mark_node
)
38221 if (code
== NOP_EXPR
)
38223 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
38231 case OMP_ATOMIC_CAPTURE_NEW
:
38232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
38234 cp_lexer_consume_token (parser
->lexer
);
38235 structured_block
= true;
38239 v
= cp_parser_unary_expression (parser
);
38240 if (v
== error_mark_node
)
38242 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38250 lhs
= cp_parser_unary_expression (parser
);
38252 switch (TREE_CODE (lhs
))
38257 case POSTINCREMENT_EXPR
:
38258 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
38259 code
= OMP_ATOMIC_CAPTURE_OLD
;
38261 case PREINCREMENT_EXPR
:
38262 lhs
= TREE_OPERAND (lhs
, 0);
38263 opcode
= PLUS_EXPR
;
38264 rhs
= integer_one_node
;
38267 case POSTDECREMENT_EXPR
:
38268 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
38269 code
= OMP_ATOMIC_CAPTURE_OLD
;
38271 case PREDECREMENT_EXPR
:
38272 lhs
= TREE_OPERAND (lhs
, 0);
38273 opcode
= MINUS_EXPR
;
38274 rhs
= integer_one_node
;
38277 case COMPOUND_EXPR
:
38278 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
38279 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
38280 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
38281 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
38282 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
38283 (TREE_OPERAND (lhs
, 1), 0), 0)))
38285 /* Undo effects of boolean_increment for post {in,de}crement. */
38286 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
38289 if (TREE_CODE (lhs
) == MODIFY_EXPR
38290 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
38292 /* Undo effects of boolean_increment. */
38293 if (integer_onep (TREE_OPERAND (lhs
, 1)))
38295 /* This is pre or post increment. */
38296 rhs
= TREE_OPERAND (lhs
, 1);
38297 lhs
= TREE_OPERAND (lhs
, 0);
38299 if (code
== OMP_ATOMIC_CAPTURE_NEW
38300 && !structured_block
38301 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
38302 code
= OMP_ATOMIC_CAPTURE_OLD
;
38308 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
38311 opcode
= MULT_EXPR
;
38314 opcode
= TRUNC_DIV_EXPR
;
38317 opcode
= PLUS_EXPR
;
38320 opcode
= MINUS_EXPR
;
38322 case CPP_LSHIFT_EQ
:
38323 opcode
= LSHIFT_EXPR
;
38325 case CPP_RSHIFT_EQ
:
38326 opcode
= RSHIFT_EXPR
;
38329 opcode
= BIT_AND_EXPR
;
38332 opcode
= BIT_IOR_EXPR
;
38335 opcode
= BIT_XOR_EXPR
;
38338 enum cp_parser_prec oprec
;
38340 cp_lexer_consume_token (parser
->lexer
);
38341 cp_parser_parse_tentatively (parser
);
38342 rhs1
= cp_parser_simple_cast_expression (parser
);
38343 if (rhs1
== error_mark_node
)
38345 cp_parser_abort_tentative_parse (parser
);
38346 cp_parser_simple_cast_expression (parser
);
38349 token
= cp_lexer_peek_token (parser
->lexer
);
38350 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
38352 cp_parser_abort_tentative_parse (parser
);
38353 cp_parser_parse_tentatively (parser
);
38354 rhs
= cp_parser_binary_expression (parser
, false, true,
38355 PREC_NOT_OPERATOR
, NULL
);
38356 if (rhs
== error_mark_node
)
38358 cp_parser_abort_tentative_parse (parser
);
38359 cp_parser_binary_expression (parser
, false, true,
38360 PREC_NOT_OPERATOR
, NULL
);
38363 switch (TREE_CODE (rhs
))
38366 case TRUNC_DIV_EXPR
:
38375 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
38377 if (cp_parser_parse_definitely (parser
))
38379 opcode
= TREE_CODE (rhs
);
38380 rhs1
= TREE_OPERAND (rhs
, 0);
38381 rhs
= TREE_OPERAND (rhs
, 1);
38391 cp_parser_abort_tentative_parse (parser
);
38392 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
38394 rhs
= cp_parser_expression (parser
);
38395 if (rhs
== error_mark_node
)
38401 cp_parser_error (parser
,
38402 "invalid form of %<#pragma omp atomic%>");
38405 if (!cp_parser_parse_definitely (parser
))
38407 switch (token
->type
)
38409 case CPP_SEMICOLON
:
38410 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
38412 code
= OMP_ATOMIC_CAPTURE_OLD
;
38417 cp_lexer_consume_token (parser
->lexer
);
38420 else if (structured_block
)
38427 cp_parser_error (parser
,
38428 "invalid form of %<#pragma omp atomic%>");
38431 opcode
= MULT_EXPR
;
38434 opcode
= TRUNC_DIV_EXPR
;
38437 opcode
= PLUS_EXPR
;
38440 opcode
= MINUS_EXPR
;
38443 opcode
= LSHIFT_EXPR
;
38446 opcode
= RSHIFT_EXPR
;
38449 opcode
= BIT_AND_EXPR
;
38452 opcode
= BIT_IOR_EXPR
;
38455 opcode
= BIT_XOR_EXPR
;
38458 cp_parser_error (parser
,
38459 "invalid operator for %<#pragma omp atomic%>");
38462 oprec
= TOKEN_PRECEDENCE (token
);
38463 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
38464 if (commutative_tree_code (opcode
))
38465 oprec
= (enum cp_parser_prec
) (oprec
- 1);
38466 cp_lexer_consume_token (parser
->lexer
);
38467 rhs
= cp_parser_binary_expression (parser
, false, false,
38469 if (rhs
== error_mark_node
)
38474 cp_parser_error (parser
,
38475 "invalid operator for %<#pragma omp atomic%>");
38478 cp_lexer_consume_token (parser
->lexer
);
38480 rhs
= cp_parser_expression (parser
);
38481 if (rhs
== error_mark_node
)
38486 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
38488 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
38490 v
= cp_parser_unary_expression (parser
);
38491 if (v
== error_mark_node
)
38493 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38495 lhs1
= cp_parser_unary_expression (parser
);
38496 if (lhs1
== error_mark_node
)
38499 if (structured_block
)
38501 cp_parser_consume_semicolon_at_end_of_statement (parser
);
38502 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
38505 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38506 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
38507 rhs1
, clauses
, memory_order
);
38508 if (!structured_block
)
38509 cp_parser_consume_semicolon_at_end_of_statement (parser
);
38513 cp_parser_skip_to_end_of_block_or_statement (parser
);
38514 if (structured_block
)
38516 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
38517 cp_lexer_consume_token (parser
->lexer
);
38518 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
38520 cp_parser_skip_to_end_of_block_or_statement (parser
);
38521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
38522 cp_lexer_consume_token (parser
->lexer
);
38529 # pragma omp barrier new-line */
38532 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
38534 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38535 finish_omp_barrier ();
38539 # pragma omp critical [(name)] new-line
38543 # pragma omp critical [(name) [hint(expression)]] new-line
38544 structured-block */
38546 #define OMP_CRITICAL_CLAUSE_MASK \
38547 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
38550 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38552 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
38554 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38556 matching_parens parens
;
38557 parens
.consume_open (parser
);
38559 name
= cp_parser_identifier (parser
);
38561 if (name
== error_mark_node
38562 || !parens
.require_close (parser
))
38563 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38564 /*or_comma=*/false,
38565 /*consume_paren=*/true);
38566 if (name
== error_mark_node
)
38569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
38570 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
38571 cp_lexer_consume_token (parser
->lexer
);
38574 clauses
= cp_parser_omp_all_clauses (parser
, OMP_CRITICAL_CLAUSE_MASK
,
38575 "#pragma omp critical", pragma_tok
);
38577 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
38578 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
38582 # pragma omp depobj ( depobj ) depobj-clause new-line
38585 depend (dependence-type : locator)
38587 update (dependence-type)
38596 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
38598 location_t loc
= pragma_tok
->location
;
38599 matching_parens parens
;
38600 if (!parens
.require_open (parser
))
38602 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38606 tree depobj
= cp_parser_assignment_expression (parser
);
38608 if (!parens
.require_close (parser
))
38609 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38610 /*or_comma=*/false,
38611 /*consume_paren=*/true);
38613 tree clause
= NULL_TREE
;
38614 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
38615 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38616 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38618 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38619 const char *p
= IDENTIFIER_POINTER (id
);
38621 cp_lexer_consume_token (parser
->lexer
);
38622 if (!strcmp ("depend", p
))
38624 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
38626 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
38628 clause
= error_mark_node
;
38630 else if (!strcmp ("destroy", p
))
38631 kind
= OMP_CLAUSE_DEPEND_LAST
;
38632 else if (!strcmp ("update", p
))
38634 matching_parens c_parens
;
38635 if (c_parens
.require_open (parser
))
38638 = cp_lexer_peek_token (parser
->lexer
)->location
;
38639 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38641 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38642 const char *p2
= IDENTIFIER_POINTER (id2
);
38644 cp_lexer_consume_token (parser
->lexer
);
38645 if (!strcmp ("in", p2
))
38646 kind
= OMP_CLAUSE_DEPEND_IN
;
38647 else if (!strcmp ("out", p2
))
38648 kind
= OMP_CLAUSE_DEPEND_OUT
;
38649 else if (!strcmp ("inout", p2
))
38650 kind
= OMP_CLAUSE_DEPEND_INOUT
;
38651 else if (!strcmp ("mutexinoutset", p2
))
38652 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
38654 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
38656 clause
= error_mark_node
;
38657 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
38658 "%<mutexinoutset%>");
38660 if (!c_parens
.require_close (parser
))
38661 cp_parser_skip_to_closing_parenthesis (parser
,
38662 /*recovering=*/true,
38663 /*or_comma=*/false,
38664 /*consume_paren=*/true);
38667 clause
= error_mark_node
;
38670 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
38672 clause
= error_mark_node
;
38673 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
38675 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38677 finish_omp_depobj (loc
, depobj
, kind
, clause
);
38682 # pragma omp flush flush-vars[opt] new-line
38688 # pragma omp flush memory-order-clause new-line */
38691 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
38693 enum memmodel mo
= MEMMODEL_LAST
;
38694 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38696 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38697 const char *p
= IDENTIFIER_POINTER (id
);
38698 if (!strcmp (p
, "acq_rel"))
38699 mo
= MEMMODEL_ACQ_REL
;
38700 else if (!strcmp (p
, "release"))
38701 mo
= MEMMODEL_RELEASE
;
38702 else if (!strcmp (p
, "acquire"))
38703 mo
= MEMMODEL_ACQUIRE
;
38705 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38706 "expected %<acq_rel%>, %<release%> or %<acquire%>");
38707 cp_lexer_consume_token (parser
->lexer
);
38709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38711 if (mo
!= MEMMODEL_LAST
)
38712 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38713 "%<flush%> list specified together with memory order "
38715 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
38717 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38719 finish_omp_flush (mo
);
38722 /* Helper function, to parse omp for increment expression. */
38725 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
38727 tree cond
= cp_parser_binary_expression (parser
, false, true,
38728 PREC_NOT_OPERATOR
, NULL
);
38729 if (cond
== error_mark_node
38730 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
38732 cp_parser_skip_to_end_of_statement (parser
);
38733 return error_mark_node
;
38736 switch (TREE_CODE (cond
))
38744 if (code
!= OACC_LOOP
)
38746 gcc_fallthrough ();
38748 return error_mark_node
;
38751 /* If decl is an iterator, preserve LHS and RHS of the relational
38752 expr until finish_omp_for. */
38754 && (type_dependent_expression_p (decl
)
38755 || CLASS_TYPE_P (TREE_TYPE (decl
))))
38758 return build_x_binary_op (cp_expr_loc_or_input_loc (cond
),
38760 TREE_OPERAND (cond
, 0), ERROR_MARK
,
38761 TREE_OPERAND (cond
, 1), ERROR_MARK
,
38762 /*overload=*/NULL
, tf_warning_or_error
);
38765 /* Helper function, to parse omp for increment expression. */
38768 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
38770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38776 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
38778 op
= (token
->type
== CPP_PLUS_PLUS
38779 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
38780 cp_lexer_consume_token (parser
->lexer
);
38781 lhs
= cp_parser_simple_cast_expression (parser
);
38783 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
38784 return error_mark_node
;
38785 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
38788 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
38790 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
38791 return error_mark_node
;
38793 token
= cp_lexer_peek_token (parser
->lexer
);
38794 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
38796 op
= (token
->type
== CPP_PLUS_PLUS
38797 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
38798 cp_lexer_consume_token (parser
->lexer
);
38799 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
38802 op
= cp_parser_assignment_operator_opt (parser
);
38803 if (op
== ERROR_MARK
)
38804 return error_mark_node
;
38806 if (op
!= NOP_EXPR
)
38808 rhs
= cp_parser_assignment_expression (parser
);
38809 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
38810 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
38813 lhs
= cp_parser_binary_expression (parser
, false, false,
38814 PREC_ADDITIVE_EXPRESSION
, NULL
);
38815 token
= cp_lexer_peek_token (parser
->lexer
);
38816 decl_first
= (lhs
== decl
38817 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
38820 if (token
->type
!= CPP_PLUS
38821 && token
->type
!= CPP_MINUS
)
38822 return error_mark_node
;
38826 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
38827 cp_lexer_consume_token (parser
->lexer
);
38828 rhs
= cp_parser_binary_expression (parser
, false, false,
38829 PREC_ADDITIVE_EXPRESSION
, NULL
);
38830 token
= cp_lexer_peek_token (parser
->lexer
);
38831 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
38833 if (lhs
== NULL_TREE
)
38835 if (op
== PLUS_EXPR
)
38838 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
38839 tf_warning_or_error
);
38842 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
38843 ERROR_MARK
, NULL
, tf_warning_or_error
);
38846 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
38851 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
38852 || op
== MINUS_EXPR
)
38853 return error_mark_node
;
38854 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
38857 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
38859 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
38862 /* Parse the initialization statement of an OpenMP for loop.
38864 Return true if the resulting construct should have an
38865 OMP_CLAUSE_PRIVATE added to it. */
38868 cp_parser_omp_for_loop_init (cp_parser
*parser
,
38869 tree
&this_pre_body
,
38870 releasing_vec
&for_block
,
38876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
38879 tree add_private_clause
= NULL_TREE
;
38881 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
38885 integer-type var = lb
38886 random-access-iterator-type var = lb
38887 pointer-type var = lb
38889 cp_decl_specifier_seq type_specifiers
;
38891 /* First, try to parse as an initialized declaration. See
38892 cp_parser_condition, from whence the bulk of this is copied. */
38894 cp_parser_parse_tentatively (parser
);
38895 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
38896 /*is_declaration=*/true,
38897 /*is_trailing_return=*/false,
38899 if (cp_parser_parse_definitely (parser
))
38901 /* If parsing a type specifier seq succeeded, then this
38902 MUST be a initialized declaration. */
38903 tree asm_specification
, attributes
;
38904 cp_declarator
*declarator
;
38906 declarator
= cp_parser_declarator (parser
,
38907 CP_PARSER_DECLARATOR_NAMED
,
38908 CP_PARSER_FLAGS_NONE
,
38909 /*ctor_dtor_or_conv_p=*/NULL
,
38910 /*parenthesized_p=*/NULL
,
38911 /*member_p=*/false,
38912 /*friend_p=*/false,
38913 /*static_p=*/false);
38914 attributes
= cp_parser_attributes_opt (parser
);
38915 asm_specification
= cp_parser_asm_specification_opt (parser
);
38917 if (declarator
== cp_error_declarator
)
38918 cp_parser_skip_to_end_of_statement (parser
);
38922 tree pushed_scope
, auto_node
;
38924 decl
= start_decl (declarator
, &type_specifiers
,
38925 SD_INITIALIZED
, attributes
,
38926 /*prefix_attributes=*/NULL_TREE
,
38929 auto_node
= type_uses_auto (TREE_TYPE (decl
));
38930 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
38932 if (cp_lexer_next_token_is (parser
->lexer
,
38934 error ("parenthesized initialization is not allowed in "
38935 "OpenMP %<for%> loop");
38937 /* Trigger an error. */
38938 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
38940 init
= error_mark_node
;
38941 cp_parser_skip_to_end_of_statement (parser
);
38943 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
38944 || type_dependent_expression_p (decl
)
38947 bool is_direct_init
, is_non_constant_init
;
38949 init
= cp_parser_initializer (parser
,
38951 &is_non_constant_init
);
38956 = do_auto_deduction (TREE_TYPE (decl
), init
,
38959 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
38960 && !type_dependent_expression_p (decl
))
38964 cp_finish_decl (decl
, init
, !is_non_constant_init
,
38966 LOOKUP_ONLYCONVERTING
);
38968 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
38970 vec_safe_push (for_block
, this_pre_body
);
38975 init
= pop_stmt_list (this_pre_body
);
38976 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
38978 tree_stmt_iterator i
= tsi_start (init
);
38979 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
38980 while (!tsi_end_p (i
))
38982 tree t
= tsi_stmt (i
);
38983 if (TREE_CODE (t
) == DECL_EXPR
38984 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
38987 vec_safe_push (for_block
, t
);
38992 if (tsi_one_before_end_p (i
))
38994 tree t
= tsi_stmt (i
);
38996 free_stmt_list (init
);
39001 this_pre_body
= NULL_TREE
;
39006 cp_lexer_consume_token (parser
->lexer
);
39007 init
= cp_parser_assignment_expression (parser
);
39010 if (TYPE_REF_P (TREE_TYPE (decl
)))
39011 init
= error_mark_node
;
39013 cp_finish_decl (decl
, NULL_TREE
,
39014 /*init_const_expr_p=*/false,
39016 LOOKUP_ONLYCONVERTING
);
39020 pop_scope (pushed_scope
);
39026 /* If parsing a type specifier sequence failed, then
39027 this MUST be a simple expression. */
39028 cp_parser_parse_tentatively (parser
);
39029 decl
= cp_parser_primary_expression (parser
, false, false,
39031 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
39032 if (!cp_parser_error_occurred (parser
)
39034 && (TREE_CODE (decl
) == COMPONENT_REF
39035 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
39037 cp_parser_abort_tentative_parse (parser
);
39038 cp_parser_parse_tentatively (parser
);
39039 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
39040 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39041 /*check_dependency_p=*/true,
39042 /*template_p=*/NULL
,
39043 /*declarator_p=*/false,
39044 /*optional_p=*/false);
39045 if (name
!= error_mark_node
39046 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
39048 decl
= cp_parser_lookup_name_simple (parser
, name
,
39050 if (TREE_CODE (decl
) == FIELD_DECL
)
39051 add_private_clause
= omp_privatize_field (decl
, false);
39053 cp_parser_abort_tentative_parse (parser
);
39054 cp_parser_parse_tentatively (parser
);
39055 decl
= cp_parser_primary_expression (parser
, false, false,
39058 if (!cp_parser_error_occurred (parser
)
39061 && CLASS_TYPE_P (TREE_TYPE (decl
)))
39065 cp_parser_parse_definitely (parser
);
39066 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
39067 rhs
= cp_parser_assignment_expression (parser
);
39069 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
39072 tf_warning_or_error
));
39073 if (!add_private_clause
)
39074 add_private_clause
= decl
;
39079 cp_parser_abort_tentative_parse (parser
);
39080 init
= cp_parser_expression (parser
);
39083 if (TREE_CODE (init
) == MODIFY_EXPR
39084 || TREE_CODE (init
) == MODOP_EXPR
)
39085 real_decl
= TREE_OPERAND (init
, 0);
39089 return add_private_clause
;
39092 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
39095 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
39096 tree
&decl
, tree
&orig_decl
, tree
&init
,
39097 tree
&orig_init
, tree
&cond
, tree
&incr
)
39099 tree begin
, end
, range_temp_decl
= NULL_TREE
;
39100 tree iter_type
, begin_expr
, end_expr
;
39102 if (processing_template_decl
)
39104 if (check_for_bare_parameter_packs (init
))
39105 init
= error_mark_node
;
39106 if (!type_dependent_expression_p (init
)
39107 /* do_auto_deduction doesn't mess with template init-lists. */
39108 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
39111 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
39113 tree v
= DECL_VALUE_EXPR (decl
);
39114 if (TREE_CODE (v
) == ARRAY_REF
39115 && VAR_P (TREE_OPERAND (v
, 0))
39116 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
39117 d
= TREE_OPERAND (v
, 0);
39119 do_range_for_auto_deduction (d
, init
);
39121 cond
= global_namespace
;
39125 this_pre_body
= pop_stmt_list (this_pre_body
);
39129 init
= mark_lvalue_use (init
);
39131 if (decl
== error_mark_node
|| init
== error_mark_node
)
39132 /* If an error happened previously do nothing or else a lot of
39133 unhelpful errors would be issued. */
39134 begin_expr
= end_expr
= iter_type
= error_mark_node
;
39140 && array_of_runtime_bound_p (TREE_TYPE (init
)))
39141 /* Can't bind a reference to an array of runtime bound. */
39145 range_temp
= build_range_temp (init
);
39146 DECL_NAME (range_temp
) = NULL_TREE
;
39147 pushdecl (range_temp
);
39148 cp_finish_decl (range_temp
, init
,
39149 /*is_constant_init*/false, NULL_TREE
,
39150 LOOKUP_ONLYCONVERTING
);
39151 range_temp_decl
= range_temp
;
39152 range_temp
= convert_from_reference (range_temp
);
39154 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
39155 &begin_expr
, &end_expr
);
39158 tree end_iter_type
= iter_type
;
39159 if (cxx_dialect
>= cxx17
)
39160 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
39161 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
39162 TREE_USED (end
) = 1;
39163 DECL_ARTIFICIAL (end
) = 1;
39165 cp_finish_decl (end
, end_expr
,
39166 /*is_constant_init*/false, NULL_TREE
,
39167 LOOKUP_ONLYCONVERTING
);
39169 /* The new for initialization statement. */
39170 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
39171 TREE_USED (begin
) = 1;
39172 DECL_ARTIFICIAL (begin
) = 1;
39175 if (CLASS_TYPE_P (iter_type
))
39180 begin_expr
= NULL_TREE
;
39182 cp_finish_decl (begin
, begin_expr
,
39183 /*is_constant_init*/false, NULL_TREE
,
39184 LOOKUP_ONLYCONVERTING
);
39186 /* The new for condition. */
39187 if (CLASS_TYPE_P (iter_type
))
39188 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
39190 cond
= build_x_binary_op (input_location
, NE_EXPR
,
39193 NULL
, tf_warning_or_error
);
39195 /* The new increment expression. */
39196 if (CLASS_TYPE_P (iter_type
))
39197 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
39199 incr
= finish_unary_op_expr (input_location
,
39200 PREINCREMENT_EXPR
, begin
,
39201 tf_warning_or_error
);
39207 vec_safe_push (for_block
, this_pre_body
);
39208 this_pre_body
= NULL_TREE
;
39211 tree decomp_first_name
= NULL_TREE
;
39212 unsigned decomp_cnt
= 0;
39213 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
39215 tree v
= DECL_VALUE_EXPR (orig_decl
);
39216 if (TREE_CODE (v
) == ARRAY_REF
39217 && VAR_P (TREE_OPERAND (v
, 0))
39218 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
39220 tree d
= orig_decl
;
39221 orig_decl
= TREE_OPERAND (v
, 0);
39222 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
39223 decomp_first_name
= d
;
39227 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
39230 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
39232 if (!error_operand_p (t
))
39233 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
39237 tree v
= make_tree_vec (decomp_cnt
+ 3);
39238 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
39239 TREE_VEC_ELT (v
, 1) = end
;
39240 TREE_VEC_ELT (v
, 2) = orig_decl
;
39241 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
39243 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
39244 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
39246 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
39249 /* Helper for cp_parser_omp_for_loop, finalize part of range for
39250 inside of the collapsed body. */
39253 cp_finish_omp_range_for (tree orig
, tree begin
)
39255 gcc_assert (TREE_CODE (orig
) == TREE_LIST
39256 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
39257 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
39258 tree decomp_first_name
= NULL_TREE
;
39259 unsigned int decomp_cnt
= 0;
39261 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
39263 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
39264 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
39265 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
39268 /* The declaration is initialized with *__begin inside the loop body. */
39269 cp_finish_decl (decl
,
39270 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
39271 tf_warning_or_error
),
39272 /*is_constant_init*/false, NULL_TREE
,
39273 LOOKUP_ONLYCONVERTING
);
39274 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
39275 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
39281 { structured-block scan-directive structured-block } */
39284 cp_parser_omp_scan_loop_body (cp_parser
*parser
)
39286 tree substmt
, clauses
= NULL_TREE
;
39288 matching_braces braces
;
39289 if (!braces
.require_open (parser
))
39292 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
39293 substmt
= build2 (OMP_SCAN
, void_type_node
, substmt
, NULL_TREE
);
39294 add_stmt (substmt
);
39296 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
39297 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SCAN
)
39299 enum omp_clause_code clause
= OMP_CLAUSE_ERROR
;
39301 cp_lexer_consume_token (parser
->lexer
);
39303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39305 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39306 const char *p
= IDENTIFIER_POINTER (id
);
39307 if (strcmp (p
, "inclusive") == 0)
39308 clause
= OMP_CLAUSE_INCLUSIVE
;
39309 else if (strcmp (p
, "exclusive") == 0)
39310 clause
= OMP_CLAUSE_EXCLUSIVE
;
39312 if (clause
!= OMP_CLAUSE_ERROR
)
39314 cp_lexer_consume_token (parser
->lexer
);
39315 clauses
= cp_parser_omp_var_list (parser
, clause
, NULL_TREE
);
39318 cp_parser_error (parser
, "expected %<inclusive%> or "
39319 "%<exclusive%> clause");
39321 cp_parser_require_pragma_eol (parser
, tok
);
39324 error ("expected %<#pragma omp scan%>");
39326 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39327 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
39328 substmt
= build2_loc (tok
->location
, OMP_SCAN
, void_type_node
, substmt
,
39330 add_stmt (substmt
);
39332 braces
.require_close (parser
);
39335 /* Parse the restricted form of the for statement allowed by OpenMP. */
39338 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
39339 tree
*cclauses
, bool *if_p
)
39341 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
39343 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
39344 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
39345 location_t loc_first
;
39346 bool collapse_err
= false;
39347 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
39348 releasing_vec for_block
;
39349 auto_vec
<tree
, 4> orig_inits
;
39350 bool tiling
= false;
39351 bool inscan
= false;
39353 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
39354 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
39355 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
39356 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
39359 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
39361 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
39362 && OMP_CLAUSE_ORDERED_EXPR (cl
))
39365 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
39367 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_REDUCTION
39368 && OMP_CLAUSE_REDUCTION_INSCAN (cl
)
39369 && (code
== OMP_SIMD
|| code
== OMP_FOR
))
39372 if (ordered
&& ordered
< collapse
)
39374 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
39375 "%<ordered%> clause parameter is less than %<collapse%>");
39376 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
39377 = build_int_cst (NULL_TREE
, collapse
);
39378 ordered
= collapse
;
39382 for (tree
*pc
= &clauses
; *pc
; )
39383 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
39385 error_at (OMP_CLAUSE_LOCATION (*pc
),
39386 "%<linear%> clause may not be specified together "
39387 "with %<ordered%> clause with a parameter");
39388 *pc
= OMP_CLAUSE_CHAIN (*pc
);
39391 pc
= &OMP_CLAUSE_CHAIN (*pc
);
39394 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
39395 count
= ordered
? ordered
: collapse
;
39397 declv
= make_tree_vec (count
);
39398 initv
= make_tree_vec (count
);
39399 condv
= make_tree_vec (count
);
39400 incrv
= make_tree_vec (count
);
39401 orig_declv
= NULL_TREE
;
39403 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
39405 for (i
= 0; i
< count
; i
++)
39407 int bracecount
= 0;
39408 tree add_private_clause
= NULL_TREE
;
39411 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
39414 cp_parser_error (parser
, "for statement expected");
39417 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
39419 /* Don't create location wrapper nodes within an OpenMP "for"
39421 auto_suppress_location_wrappers sentinel
;
39423 matching_parens parens
;
39424 if (!parens
.require_open (parser
))
39427 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
39428 this_pre_body
= push_stmt_list ();
39430 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
39432 /* Save tokens so that we can put them back. */
39433 cp_lexer_save_tokens (parser
->lexer
);
39435 /* Look for ':' that is not nested in () or {}. */
39437 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
39438 /*recovering=*/false,
39443 /* Roll back the tokens we skipped. */
39444 cp_lexer_rollback_tokens (parser
->lexer
);
39448 bool saved_colon_corrects_to_scope_p
39449 = parser
->colon_corrects_to_scope_p
;
39451 /* A colon is used in range-based for. */
39452 parser
->colon_corrects_to_scope_p
= false;
39454 /* Parse the declaration. */
39455 cp_parser_simple_declaration (parser
,
39456 /*function_definition_allowed_p=*/
39458 parser
->colon_corrects_to_scope_p
39459 = saved_colon_corrects_to_scope_p
;
39461 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
39463 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
39466 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
39467 orig_decl
, init
, orig_init
,
39474 pre_body
= push_stmt_list ();
39476 add_stmt (this_pre_body
);
39477 pre_body
= pop_stmt_list (pre_body
);
39480 pre_body
= this_pre_body
;
39484 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
39485 "%<ordered%> clause with parameter on "
39486 "range-based %<for%> loop");
39488 goto parse_close_paren
;
39493 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
39494 init
, orig_init
, decl
, real_decl
);
39496 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
39499 this_pre_body
= pop_stmt_list (this_pre_body
);
39503 pre_body
= push_stmt_list ();
39505 add_stmt (this_pre_body
);
39506 pre_body
= pop_stmt_list (pre_body
);
39509 pre_body
= this_pre_body
;
39514 if (cclauses
!= NULL
39515 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
39516 && real_decl
!= NULL_TREE
39517 && code
!= OMP_LOOP
)
39520 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
39521 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
39522 && OMP_CLAUSE_DECL (*c
) == real_decl
)
39524 error_at (loc
, "iteration variable %qD"
39525 " should not be firstprivate", real_decl
);
39526 *c
= OMP_CLAUSE_CHAIN (*c
);
39528 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
39529 && OMP_CLAUSE_DECL (*c
) == real_decl
)
39531 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
39533 *c
= OMP_CLAUSE_CHAIN (*c
);
39534 if (code
== OMP_SIMD
)
39536 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
39537 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
39541 OMP_CLAUSE_CHAIN (l
) = clauses
;
39544 add_private_clause
= NULL_TREE
;
39548 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
39549 && OMP_CLAUSE_DECL (*c
) == real_decl
)
39550 add_private_clause
= NULL_TREE
;
39551 c
= &OMP_CLAUSE_CHAIN (*c
);
39555 if (add_private_clause
)
39558 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39560 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
39561 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
39562 && OMP_CLAUSE_DECL (c
) == decl
)
39564 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
39565 && OMP_CLAUSE_DECL (c
) == decl
)
39566 error_at (loc
, "iteration variable %qD "
39567 "should not be firstprivate",
39569 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
39570 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
39571 && OMP_CLAUSE_DECL (c
) == decl
)
39572 error_at (loc
, "iteration variable %qD should not be reduction",
39577 if ((code
== OMP_SIMD
&& collapse
!= 1) || code
== OMP_LOOP
)
39578 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
39579 else if (code
!= OMP_SIMD
)
39580 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
39582 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
39583 OMP_CLAUSE_DECL (c
) = add_private_clause
;
39584 c
= finish_omp_clauses (c
, C_ORT_OMP
);
39587 OMP_CLAUSE_CHAIN (c
) = clauses
;
39589 /* For linear, signal that we need to fill up
39590 the so far unknown linear step. */
39591 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
39592 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
39598 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
39599 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
39600 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
39603 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
39605 /* If decl is an iterator, preserve the operator on decl
39606 until finish_omp_for. */
39608 && ((processing_template_decl
39609 && (TREE_TYPE (real_decl
) == NULL_TREE
39610 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
39611 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
39612 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
39614 incr
= cp_parser_expression (parser
);
39615 protected_set_expr_location_if_unset (incr
, input_location
);
39619 if (!parens
.require_close (parser
))
39620 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39621 /*or_comma=*/false,
39622 /*consume_paren=*/true);
39624 TREE_VEC_ELT (declv
, i
) = decl
;
39625 TREE_VEC_ELT (initv
, i
) = init
;
39626 TREE_VEC_ELT (condv
, i
) = cond
;
39627 TREE_VEC_ELT (incrv
, i
) = incr
;
39630 orig_inits
.safe_grow_cleared (i
+ 1, true);
39631 orig_inits
[i
] = orig_init
;
39636 orig_declv
= copy_node (declv
);
39637 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
39639 else if (orig_declv
)
39640 TREE_VEC_ELT (orig_declv
, i
) = decl
;
39642 if (i
== count
- 1)
39645 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
39646 in between the collapsed for loops to be still considered perfectly
39647 nested. Hopefully the final version clarifies this.
39648 For now handle (multiple) {'s and empty statements. */
39649 cp_parser_parse_tentatively (parser
);
39652 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
39654 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
39656 cp_lexer_consume_token (parser
->lexer
);
39659 else if (bracecount
39660 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
39661 cp_lexer_consume_token (parser
->lexer
);
39664 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39665 error_at (loc
, "not enough for loops to collapse");
39666 collapse_err
= true;
39667 cp_parser_abort_tentative_parse (parser
);
39675 cp_parser_parse_definitely (parser
);
39676 nbraces
+= bracecount
;
39683 /* Note that we saved the original contents of this flag when we entered
39684 the structured block, and so we don't need to re-save it here. */
39685 parser
->in_statement
= IN_OMP_FOR
;
39687 /* Note that the grammar doesn't call for a structured block here,
39688 though the loop as a whole is a structured block. */
39691 body
= begin_omp_structured_block ();
39692 for (i
= 0; i
< count
; i
++)
39693 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
39694 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
39695 TREE_VEC_ELT (declv
, i
));
39698 body
= push_stmt_list ();
39700 cp_parser_omp_scan_loop_body (parser
);
39702 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39704 body
= finish_omp_structured_block (body
);
39706 body
= pop_stmt_list (body
);
39708 if (declv
== NULL_TREE
)
39711 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
39712 incrv
, body
, pre_body
, &orig_inits
, clauses
);
39716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
39718 cp_lexer_consume_token (parser
->lexer
);
39721 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
39722 cp_lexer_consume_token (parser
->lexer
);
39727 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39728 "collapsed loops not perfectly nested");
39730 collapse_err
= true;
39731 cp_parser_statement_seq_opt (parser
, NULL
);
39732 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
39737 while (!for_block
->is_empty ())
39739 tree t
= for_block
->pop ();
39740 if (TREE_CODE (t
) == STATEMENT_LIST
)
39741 add_stmt (pop_stmt_list (t
));
39749 /* Helper function for OpenMP parsing, split clauses and call
39750 finish_omp_clauses on each of the set of clauses afterwards. */
39753 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
39754 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
39757 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
39758 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
39760 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
39764 #pragma omp loop loop-clause[optseq] new-line
39767 #define OMP_LOOP_CLAUSE_MASK \
39768 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39769 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39770 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39771 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39772 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
39773 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39776 cp_parser_omp_loop (cp_parser
*parser
, cp_token
*pragma_tok
,
39777 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39780 tree clauses
, sb
, ret
;
39782 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39784 strcat (p_name
, " loop");
39785 mask
|= OMP_LOOP_CLAUSE_MASK
;
39787 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39791 cp_omp_split_clauses (loc
, OMP_LOOP
, mask
, clauses
, cclauses
);
39792 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_LOOP
];
39795 keep_next_level (true);
39796 sb
= begin_omp_structured_block ();
39797 save
= cp_parser_begin_omp_structured_block (parser
);
39799 ret
= cp_parser_omp_for_loop (parser
, OMP_LOOP
, clauses
, cclauses
, if_p
);
39801 cp_parser_end_omp_structured_block (parser
, save
);
39802 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39808 #pragma omp simd simd-clause[optseq] new-line
39811 #define OMP_SIMD_CLAUSE_MASK \
39812 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
39813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39814 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39815 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39817 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39818 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39819 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
39822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39825 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39826 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39829 tree clauses
, sb
, ret
;
39831 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39833 strcat (p_name
, " simd");
39834 mask
|= OMP_SIMD_CLAUSE_MASK
;
39836 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39840 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
39841 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
39842 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
39843 OMP_CLAUSE_ORDERED
);
39844 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
39846 error_at (OMP_CLAUSE_LOCATION (c
),
39847 "%<ordered%> clause with parameter may not be specified "
39848 "on %qs construct", p_name
);
39849 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
39853 keep_next_level (true);
39854 sb
= begin_omp_structured_block ();
39855 save
= cp_parser_begin_omp_structured_block (parser
);
39857 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
39859 cp_parser_end_omp_structured_block (parser
, save
);
39860 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39866 #pragma omp for for-clause[optseq] new-line
39870 #pragma omp for simd for-simd-clause[optseq] new-line
39873 #define OMP_FOR_CLAUSE_MASK \
39874 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
39880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
39881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
39884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39887 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
39888 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39891 tree clauses
, sb
, ret
;
39893 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39895 strcat (p_name
, " for");
39896 mask
|= OMP_FOR_CLAUSE_MASK
;
39897 /* parallel for{, simd} disallows nowait clause, but for
39898 target {teams distribute ,}parallel for{, simd} it should be accepted. */
39899 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
39900 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
39901 /* Composite distribute parallel for{, simd} disallows ordered clause. */
39902 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
39903 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
39905 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39907 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39908 const char *p
= IDENTIFIER_POINTER (id
);
39910 if (strcmp (p
, "simd") == 0)
39912 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39913 if (cclauses
== NULL
)
39914 cclauses
= cclauses_buf
;
39916 cp_lexer_consume_token (parser
->lexer
);
39917 if (!flag_openmp
) /* flag_openmp_simd */
39918 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39920 sb
= begin_omp_structured_block ();
39921 save
= cp_parser_begin_omp_structured_block (parser
);
39922 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39924 cp_parser_end_omp_structured_block (parser
, save
);
39925 tree body
= finish_omp_structured_block (sb
);
39928 ret
= make_node (OMP_FOR
);
39929 TREE_TYPE (ret
) = void_type_node
;
39930 OMP_FOR_BODY (ret
) = body
;
39931 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
39932 SET_EXPR_LOCATION (ret
, loc
);
39937 if (!flag_openmp
) /* flag_openmp_simd */
39939 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39943 /* Composite distribute parallel for disallows linear clause. */
39944 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
39945 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
39947 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39951 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
39952 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
39955 keep_next_level (true);
39956 sb
= begin_omp_structured_block ();
39957 save
= cp_parser_begin_omp_structured_block (parser
);
39959 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
39961 cp_parser_end_omp_structured_block (parser
, save
);
39962 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39967 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
39968 omp_clause_mask
, tree
*, bool *);
39971 # pragma omp master new-line
39972 structured-block */
39975 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
39976 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39979 tree clauses
, sb
, ret
;
39981 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39983 strcat (p_name
, " master");
39985 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39987 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39988 const char *p
= IDENTIFIER_POINTER (id
);
39990 if (strcmp (p
, "taskloop") == 0)
39992 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39993 if (cclauses
== NULL
)
39994 cclauses
= cclauses_buf
;
39996 cp_lexer_consume_token (parser
->lexer
);
39997 if (!flag_openmp
) /* flag_openmp_simd */
39998 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
40000 sb
= begin_omp_structured_block ();
40001 save
= cp_parser_begin_omp_structured_block (parser
);
40002 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
40004 cp_parser_end_omp_structured_block (parser
, save
);
40005 tree body
= finish_omp_structured_block (sb
);
40008 return c_finish_omp_master (loc
, body
);
40011 if (!flag_openmp
) /* flag_openmp_simd */
40013 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40019 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40021 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
40024 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40026 return c_finish_omp_master (loc
,
40027 cp_parser_omp_structured_block (parser
, if_p
));
40031 # pragma omp ordered new-line
40035 # pragma omp ordered ordered-clauses new-line
40036 structured-block */
40038 #define OMP_ORDERED_CLAUSE_MASK \
40039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
40040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
40042 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
40043 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40046 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
40047 enum pragma_context context
, bool *if_p
)
40049 location_t loc
= pragma_tok
->location
;
40051 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40053 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40054 const char *p
= IDENTIFIER_POINTER (id
);
40056 if (strcmp (p
, "depend") == 0)
40058 if (!flag_openmp
) /* flag_openmp_simd */
40060 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40063 if (context
== pragma_stmt
)
40065 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
40066 "%<depend%> clause may only be used in compound "
40068 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40072 = cp_parser_omp_all_clauses (parser
,
40073 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
40074 "#pragma omp ordered", pragma_tok
);
40075 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
40081 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
40082 "#pragma omp ordered", pragma_tok
);
40084 if (!flag_openmp
/* flag_openmp_simd */
40085 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
40088 c_finish_omp_ordered (loc
, clauses
,
40089 cp_parser_omp_structured_block (parser
, if_p
));
40096 { section-sequence }
40099 section-directive[opt] structured-block
40100 section-sequence section-directive structured-block */
40103 cp_parser_omp_sections_scope (cp_parser
*parser
)
40105 tree stmt
, substmt
;
40106 bool error_suppress
= false;
40109 matching_braces braces
;
40110 if (!braces
.require_open (parser
))
40113 stmt
= push_stmt_list ();
40115 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
40116 != PRAGMA_OMP_SECTION
)
40118 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
40119 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
40120 add_stmt (substmt
);
40125 tok
= cp_lexer_peek_token (parser
->lexer
);
40126 if (tok
->type
== CPP_CLOSE_BRACE
)
40128 if (tok
->type
== CPP_EOF
)
40131 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
40133 cp_lexer_consume_token (parser
->lexer
);
40134 cp_parser_require_pragma_eol (parser
, tok
);
40135 error_suppress
= false;
40137 else if (!error_suppress
)
40139 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
40140 error_suppress
= true;
40143 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
40144 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
40145 add_stmt (substmt
);
40147 braces
.require_close (parser
);
40149 substmt
= pop_stmt_list (stmt
);
40151 stmt
= make_node (OMP_SECTIONS
);
40152 TREE_TYPE (stmt
) = void_type_node
;
40153 OMP_SECTIONS_BODY (stmt
) = substmt
;
40160 # pragma omp sections sections-clause[optseq] newline
40163 #define OMP_SECTIONS_CLAUSE_MASK \
40164 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40165 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40166 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40172 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
40173 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
40176 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40178 strcat (p_name
, " sections");
40179 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
40181 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
40183 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40187 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
40188 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
40191 ret
= cp_parser_omp_sections_scope (parser
);
40193 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
40199 # pragma omp parallel parallel-clause[optseq] new-line
40201 # pragma omp parallel for parallel-for-clause[optseq] new-line
40203 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
40207 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
40208 structured-block */
40210 #define OMP_PARALLEL_CLAUSE_MASK \
40211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
40217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
40219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
40223 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
40224 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40227 tree stmt
, clauses
, block
;
40229 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40231 strcat (p_name
, " parallel");
40232 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
40233 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
40234 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
40235 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
40236 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
40238 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
40240 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40241 if (cclauses
== NULL
)
40242 cclauses
= cclauses_buf
;
40244 cp_lexer_consume_token (parser
->lexer
);
40245 if (!flag_openmp
) /* flag_openmp_simd */
40246 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
40248 block
= begin_omp_parallel ();
40249 save
= cp_parser_begin_omp_structured_block (parser
);
40250 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
40252 cp_parser_end_omp_structured_block (parser
, save
);
40253 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
40255 if (ret
== NULL_TREE
)
40257 OMP_PARALLEL_COMBINED (stmt
) = 1;
40260 /* When combined with distribute, parallel has to be followed by for.
40261 #pragma omp target parallel is allowed though. */
40263 && (mask
& (OMP_CLAUSE_MASK_1
40264 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
40266 error_at (loc
, "expected %<for%> after %qs", p_name
);
40267 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40270 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40272 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40273 const char *p
= IDENTIFIER_POINTER (id
);
40274 if (cclauses
== NULL
&& strcmp (p
, "master") == 0)
40276 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40277 cclauses
= cclauses_buf
;
40279 cp_lexer_consume_token (parser
->lexer
);
40280 block
= begin_omp_parallel ();
40281 save
= cp_parser_begin_omp_structured_block (parser
);
40282 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
40284 cp_parser_end_omp_structured_block (parser
, save
);
40285 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
40287 if (ret
== NULL_TREE
)
40289 OMP_PARALLEL_COMBINED (stmt
) = 1;
40292 else if (strcmp (p
, "loop") == 0)
40294 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40295 if (cclauses
== NULL
)
40296 cclauses
= cclauses_buf
;
40298 cp_lexer_consume_token (parser
->lexer
);
40299 if (!flag_openmp
) /* flag_openmp_simd */
40300 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40302 block
= begin_omp_parallel ();
40303 save
= cp_parser_begin_omp_structured_block (parser
);
40304 tree ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40306 cp_parser_end_omp_structured_block (parser
, save
);
40307 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
40309 if (ret
== NULL_TREE
)
40311 OMP_PARALLEL_COMBINED (stmt
) = 1;
40314 else if (!flag_openmp
) /* flag_openmp_simd */
40316 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40319 else if (cclauses
== NULL
&& strcmp (p
, "sections") == 0)
40321 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40322 cclauses
= cclauses_buf
;
40324 cp_lexer_consume_token (parser
->lexer
);
40325 block
= begin_omp_parallel ();
40326 save
= cp_parser_begin_omp_structured_block (parser
);
40327 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
40328 cp_parser_end_omp_structured_block (parser
, save
);
40329 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
40331 OMP_PARALLEL_COMBINED (stmt
) = 1;
40335 else if (!flag_openmp
) /* flag_openmp_simd */
40337 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40341 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40345 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
40346 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
40349 block
= begin_omp_parallel ();
40350 save
= cp_parser_begin_omp_structured_block (parser
);
40351 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40352 cp_parser_end_omp_structured_block (parser
, save
);
40353 stmt
= finish_omp_parallel (clauses
, block
);
40358 # pragma omp single single-clause[optseq] new-line
40359 structured-block */
40361 #define OMP_SINGLE_CLAUSE_MASK \
40362 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
40365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40369 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40371 tree stmt
= make_node (OMP_SINGLE
);
40372 TREE_TYPE (stmt
) = void_type_node
;
40373 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40375 OMP_SINGLE_CLAUSES (stmt
)
40376 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
40377 "#pragma omp single", pragma_tok
);
40378 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40380 return add_stmt (stmt
);
40384 # pragma omp task task-clause[optseq] new-line
40385 structured-block */
40387 #define OMP_TASK_CLAUSE_MASK \
40388 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40402 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40404 tree clauses
, block
;
40407 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
40408 "#pragma omp task", pragma_tok
);
40409 block
= begin_omp_task ();
40410 save
= cp_parser_begin_omp_structured_block (parser
);
40411 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40412 cp_parser_end_omp_structured_block (parser
, save
);
40413 return finish_omp_task (clauses
, block
);
40417 # pragma omp taskwait new-line
40420 # pragma omp taskwait taskwait-clause[opt] new-line */
40422 #define OMP_TASKWAIT_CLAUSE_MASK \
40423 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40426 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
40429 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
40430 "#pragma omp taskwait", pragma_tok
);
40434 tree stmt
= make_node (OMP_TASK
);
40435 TREE_TYPE (stmt
) = void_node
;
40436 OMP_TASK_CLAUSES (stmt
) = clauses
;
40437 OMP_TASK_BODY (stmt
) = NULL_TREE
;
40438 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40442 finish_omp_taskwait ();
40446 # pragma omp taskyield new-line */
40449 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
40451 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40452 finish_omp_taskyield ();
40456 # pragma omp taskgroup new-line
40460 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
40462 #define OMP_TASKGROUP_CLAUSE_MASK \
40463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
40467 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40470 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
40471 "#pragma omp taskgroup", pragma_tok
);
40472 return c_finish_omp_taskgroup (input_location
,
40473 cp_parser_omp_structured_block (parser
,
40480 # pragma omp threadprivate (variable-list) */
40483 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
40487 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
40488 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40490 finish_omp_threadprivate (vars
);
40494 # pragma omp cancel cancel-clause[optseq] new-line */
40496 #define OMP_CANCEL_CLAUSE_MASK \
40497 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
40501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
40504 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
40506 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
40507 "#pragma omp cancel", pragma_tok
);
40508 finish_omp_cancel (clauses
);
40512 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
40514 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
40515 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
40521 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
40522 enum pragma_context context
)
40525 bool point_seen
= false;
40527 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40529 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40530 const char *p
= IDENTIFIER_POINTER (id
);
40532 if (strcmp (p
, "point") == 0)
40534 cp_lexer_consume_token (parser
->lexer
);
40540 cp_parser_error (parser
, "expected %<point%>");
40541 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40545 if (context
!= pragma_compound
)
40547 if (context
== pragma_stmt
)
40548 error_at (pragma_tok
->location
,
40549 "%<#pragma %s%> may only be used in compound statements",
40550 "omp cancellation point");
40552 cp_parser_error (parser
, "expected declaration specifiers");
40553 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40557 clauses
= cp_parser_omp_all_clauses (parser
,
40558 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
40559 "#pragma omp cancellation point",
40561 finish_omp_cancellation_point (clauses
);
40565 #pragma omp distribute distribute-clause[optseq] new-line
40568 #define OMP_DISTRIBUTE_CLAUSE_MASK \
40569 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
40573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
40577 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
40578 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40581 tree clauses
, sb
, ret
;
40583 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40585 strcat (p_name
, " distribute");
40586 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
40588 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40590 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40591 const char *p
= IDENTIFIER_POINTER (id
);
40593 bool parallel
= false;
40595 if (strcmp (p
, "simd") == 0)
40598 parallel
= strcmp (p
, "parallel") == 0;
40599 if (parallel
|| simd
)
40601 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40602 if (cclauses
== NULL
)
40603 cclauses
= cclauses_buf
;
40604 cp_lexer_consume_token (parser
->lexer
);
40605 if (!flag_openmp
) /* flag_openmp_simd */
40608 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40611 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
40614 sb
= begin_omp_structured_block ();
40615 save
= cp_parser_begin_omp_structured_block (parser
);
40617 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40620 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
40622 cp_parser_end_omp_structured_block (parser
, save
);
40623 tree body
= finish_omp_structured_block (sb
);
40626 ret
= make_node (OMP_DISTRIBUTE
);
40627 TREE_TYPE (ret
) = void_type_node
;
40628 OMP_FOR_BODY (ret
) = body
;
40629 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
40630 SET_EXPR_LOCATION (ret
, loc
);
40635 if (!flag_openmp
) /* flag_openmp_simd */
40637 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40641 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40645 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
40646 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
40649 keep_next_level (true);
40650 sb
= begin_omp_structured_block ();
40651 save
= cp_parser_begin_omp_structured_block (parser
);
40653 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
40655 cp_parser_end_omp_structured_block (parser
, save
);
40656 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40662 # pragma omp teams teams-clause[optseq] new-line
40663 structured-block */
40665 #define OMP_TEAMS_CLAUSE_MASK \
40666 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40667 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40668 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40669 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40670 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
40671 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
40672 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40673 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
40676 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
40677 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40680 tree clauses
, sb
, ret
;
40682 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40684 strcat (p_name
, " teams");
40685 mask
|= OMP_TEAMS_CLAUSE_MASK
;
40687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40689 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40690 const char *p
= IDENTIFIER_POINTER (id
);
40691 if (strcmp (p
, "distribute") == 0)
40693 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40694 if (cclauses
== NULL
)
40695 cclauses
= cclauses_buf
;
40697 cp_lexer_consume_token (parser
->lexer
);
40698 if (!flag_openmp
) /* flag_openmp_simd */
40699 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
40701 keep_next_level (true);
40702 sb
= begin_omp_structured_block ();
40703 save
= cp_parser_begin_omp_structured_block (parser
);
40704 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
40706 cp_parser_end_omp_structured_block (parser
, save
);
40707 tree body
= finish_omp_structured_block (sb
);
40710 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40711 ret
= make_node (OMP_TEAMS
);
40712 TREE_TYPE (ret
) = void_type_node
;
40713 OMP_TEAMS_CLAUSES (ret
) = clauses
;
40714 OMP_TEAMS_BODY (ret
) = body
;
40715 OMP_TEAMS_COMBINED (ret
) = 1;
40716 SET_EXPR_LOCATION (ret
, loc
);
40717 return add_stmt (ret
);
40719 else if (strcmp (p
, "loop") == 0)
40721 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40722 if (cclauses
== NULL
)
40723 cclauses
= cclauses_buf
;
40725 cp_lexer_consume_token (parser
->lexer
);
40726 if (!flag_openmp
) /* flag_openmp_simd */
40727 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40729 keep_next_level (true);
40730 sb
= begin_omp_structured_block ();
40731 save
= cp_parser_begin_omp_structured_block (parser
);
40732 ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40734 cp_parser_end_omp_structured_block (parser
, save
);
40735 tree body
= finish_omp_structured_block (sb
);
40738 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40739 ret
= make_node (OMP_TEAMS
);
40740 TREE_TYPE (ret
) = void_type_node
;
40741 OMP_TEAMS_CLAUSES (ret
) = clauses
;
40742 OMP_TEAMS_BODY (ret
) = body
;
40743 OMP_TEAMS_COMBINED (ret
) = 1;
40744 SET_EXPR_LOCATION (ret
, loc
);
40745 return add_stmt (ret
);
40748 if (!flag_openmp
) /* flag_openmp_simd */
40750 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40754 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40758 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
40759 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40762 tree stmt
= make_node (OMP_TEAMS
);
40763 TREE_TYPE (stmt
) = void_type_node
;
40764 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
40765 keep_next_level (true);
40766 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40767 SET_EXPR_LOCATION (stmt
, loc
);
40769 return add_stmt (stmt
);
40773 # pragma omp target data target-data-clause[optseq] new-line
40774 structured-block */
40776 #define OMP_TARGET_DATA_CLAUSE_MASK \
40777 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
40781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
40784 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40787 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
40788 "#pragma omp target data", pragma_tok
);
40789 c_omp_adjust_map_clauses (clauses
, false);
40791 for (tree
*pc
= &clauses
; *pc
;)
40793 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40794 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40797 case GOMP_MAP_ALWAYS_TO
:
40798 case GOMP_MAP_FROM
:
40799 case GOMP_MAP_ALWAYS_FROM
:
40800 case GOMP_MAP_TOFROM
:
40801 case GOMP_MAP_ALWAYS_TOFROM
:
40802 case GOMP_MAP_ALLOC
:
40805 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40806 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40807 case GOMP_MAP_ALWAYS_POINTER
:
40808 case GOMP_MAP_ATTACH_DETACH
:
40812 error_at (OMP_CLAUSE_LOCATION (*pc
),
40813 "%<#pragma omp target data%> with map-type other "
40814 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40815 "on %<map%> clause");
40816 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40819 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
40820 || OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_ADDR
)
40822 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40828 error_at (pragma_tok
->location
,
40829 "%<#pragma omp target data%> must contain at least "
40830 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
40835 tree stmt
= make_node (OMP_TARGET_DATA
);
40836 TREE_TYPE (stmt
) = void_type_node
;
40837 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
40839 keep_next_level (true);
40840 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40842 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40843 return add_stmt (stmt
);
40847 # pragma omp target enter data target-enter-data-clause[optseq] new-line
40848 structured-block */
40850 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
40851 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40858 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
40859 enum pragma_context context
)
40861 bool data_seen
= false;
40862 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40864 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40865 const char *p
= IDENTIFIER_POINTER (id
);
40867 if (strcmp (p
, "data") == 0)
40869 cp_lexer_consume_token (parser
->lexer
);
40875 cp_parser_error (parser
, "expected %<data%>");
40876 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40880 if (context
== pragma_stmt
)
40882 error_at (pragma_tok
->location
,
40883 "%<#pragma %s%> may only be used in compound statements",
40884 "omp target enter data");
40885 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40890 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
40891 "#pragma omp target enter data", pragma_tok
);
40892 c_omp_adjust_map_clauses (clauses
, false);
40894 for (tree
*pc
= &clauses
; *pc
;)
40896 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40897 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40900 case GOMP_MAP_ALWAYS_TO
:
40901 case GOMP_MAP_ALLOC
:
40904 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40905 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40906 case GOMP_MAP_ALWAYS_POINTER
:
40907 case GOMP_MAP_ATTACH_DETACH
:
40911 error_at (OMP_CLAUSE_LOCATION (*pc
),
40912 "%<#pragma omp target enter data%> with map-type other "
40913 "than %<to%> or %<alloc%> on %<map%> clause");
40914 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40917 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40923 error_at (pragma_tok
->location
,
40924 "%<#pragma omp target enter data%> must contain at least "
40925 "one %<map%> clause");
40929 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
40930 TREE_TYPE (stmt
) = void_type_node
;
40931 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
40932 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40933 return add_stmt (stmt
);
40937 # pragma omp target exit data target-enter-data-clause[optseq] new-line
40938 structured-block */
40940 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
40941 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40948 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
40949 enum pragma_context context
)
40951 bool data_seen
= false;
40952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40954 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40955 const char *p
= IDENTIFIER_POINTER (id
);
40957 if (strcmp (p
, "data") == 0)
40959 cp_lexer_consume_token (parser
->lexer
);
40965 cp_parser_error (parser
, "expected %<data%>");
40966 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40970 if (context
== pragma_stmt
)
40972 error_at (pragma_tok
->location
,
40973 "%<#pragma %s%> may only be used in compound statements",
40974 "omp target exit data");
40975 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40980 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
40981 "#pragma omp target exit data", pragma_tok
);
40982 c_omp_adjust_map_clauses (clauses
, false);
40984 for (tree
*pc
= &clauses
; *pc
;)
40986 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40987 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40989 case GOMP_MAP_FROM
:
40990 case GOMP_MAP_ALWAYS_FROM
:
40991 case GOMP_MAP_RELEASE
:
40992 case GOMP_MAP_DELETE
:
40995 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40996 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40997 case GOMP_MAP_ALWAYS_POINTER
:
40998 case GOMP_MAP_ATTACH_DETACH
:
41002 error_at (OMP_CLAUSE_LOCATION (*pc
),
41003 "%<#pragma omp target exit data%> with map-type other "
41004 "than %<from%>, %<release%> or %<delete%> on %<map%>"
41006 *pc
= OMP_CLAUSE_CHAIN (*pc
);
41009 pc
= &OMP_CLAUSE_CHAIN (*pc
);
41015 error_at (pragma_tok
->location
,
41016 "%<#pragma omp target exit data%> must contain at least "
41017 "one %<map%> clause");
41021 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
41022 TREE_TYPE (stmt
) = void_type_node
;
41023 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
41024 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41025 return add_stmt (stmt
);
41029 # pragma omp target update target-update-clause[optseq] new-line */
41031 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
41032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
41033 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41037 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41040 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
41041 enum pragma_context context
)
41043 if (context
== pragma_stmt
)
41045 error_at (pragma_tok
->location
,
41046 "%<#pragma %s%> may only be used in compound statements",
41047 "omp target update");
41048 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41053 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
41054 "#pragma omp target update", pragma_tok
);
41055 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
41056 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
41058 error_at (pragma_tok
->location
,
41059 "%<#pragma omp target update%> must contain at least one "
41060 "%<from%> or %<to%> clauses");
41064 tree stmt
= make_node (OMP_TARGET_UPDATE
);
41065 TREE_TYPE (stmt
) = void_type_node
;
41066 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
41067 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41073 # pragma omp target target-clause[optseq] new-line
41074 structured-block */
41076 #define OMP_TARGET_CLAUSE_MASK \
41077 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
41082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41084 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
41085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
41089 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
41090 enum pragma_context context
, bool *if_p
)
41092 tree
*pc
= NULL
, stmt
;
41096 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
41098 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41100 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41101 const char *p
= IDENTIFIER_POINTER (id
);
41102 enum tree_code ccode
= ERROR_MARK
;
41104 if (strcmp (p
, "teams") == 0)
41106 else if (strcmp (p
, "parallel") == 0)
41107 ccode
= OMP_PARALLEL
;
41108 else if (strcmp (p
, "simd") == 0)
41110 if (ccode
!= ERROR_MARK
)
41112 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
41113 char p_name
[sizeof ("#pragma omp target teams distribute "
41114 "parallel for simd")];
41116 cp_lexer_consume_token (parser
->lexer
);
41117 strcpy (p_name
, "#pragma omp target");
41118 if (!flag_openmp
) /* flag_openmp_simd */
41124 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
41125 OMP_TARGET_CLAUSE_MASK
,
41129 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
41130 OMP_TARGET_CLAUSE_MASK
,
41134 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
41135 OMP_TARGET_CLAUSE_MASK
,
41139 gcc_unreachable ();
41141 return stmt
!= NULL_TREE
;
41143 keep_next_level (true);
41144 tree sb
= begin_omp_structured_block (), ret
;
41145 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
41149 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
41150 OMP_TARGET_CLAUSE_MASK
, cclauses
,
41154 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
41155 OMP_TARGET_CLAUSE_MASK
, cclauses
,
41159 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
41160 OMP_TARGET_CLAUSE_MASK
, cclauses
,
41164 gcc_unreachable ();
41166 cp_parser_end_omp_structured_block (parser
, save
);
41167 tree body
= finish_omp_structured_block (sb
);
41168 if (ret
== NULL_TREE
)
41170 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
41172 /* For combined target teams, ensure the num_teams and
41173 thread_limit clause expressions are evaluated on the host,
41174 before entering the target construct. */
41176 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
41177 c
; c
= OMP_CLAUSE_CHAIN (c
))
41178 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
41179 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
41180 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
41182 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
41183 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
41184 if (expr
== error_mark_node
)
41186 tree tmp
= TARGET_EXPR_SLOT (expr
);
41188 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
41189 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
41190 OMP_CLAUSE_FIRSTPRIVATE
);
41191 OMP_CLAUSE_DECL (tc
) = tmp
;
41192 OMP_CLAUSE_CHAIN (tc
)
41193 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
41194 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
41197 tree stmt
= make_node (OMP_TARGET
);
41198 TREE_TYPE (stmt
) = void_type_node
;
41199 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
41200 OMP_TARGET_BODY (stmt
) = body
;
41201 OMP_TARGET_COMBINED (stmt
) = 1;
41202 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41204 pc
= &OMP_TARGET_CLAUSES (stmt
);
41205 goto check_clauses
;
41207 else if (!flag_openmp
) /* flag_openmp_simd */
41209 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41212 else if (strcmp (p
, "data") == 0)
41214 cp_lexer_consume_token (parser
->lexer
);
41215 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
41218 else if (strcmp (p
, "enter") == 0)
41220 cp_lexer_consume_token (parser
->lexer
);
41221 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
41224 else if (strcmp (p
, "exit") == 0)
41226 cp_lexer_consume_token (parser
->lexer
);
41227 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
41230 else if (strcmp (p
, "update") == 0)
41232 cp_lexer_consume_token (parser
->lexer
);
41233 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
41236 if (!flag_openmp
) /* flag_openmp_simd */
41238 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41242 stmt
= make_node (OMP_TARGET
);
41243 TREE_TYPE (stmt
) = void_type_node
;
41245 OMP_TARGET_CLAUSES (stmt
)
41246 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
41247 "#pragma omp target", pragma_tok
);
41248 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt
), true);
41250 pc
= &OMP_TARGET_CLAUSES (stmt
);
41251 keep_next_level (true);
41252 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
41254 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41260 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
41261 switch (OMP_CLAUSE_MAP_KIND (*pc
))
41264 case GOMP_MAP_ALWAYS_TO
:
41265 case GOMP_MAP_FROM
:
41266 case GOMP_MAP_ALWAYS_FROM
:
41267 case GOMP_MAP_TOFROM
:
41268 case GOMP_MAP_ALWAYS_TOFROM
:
41269 case GOMP_MAP_ALLOC
:
41270 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41271 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
41272 case GOMP_MAP_ALWAYS_POINTER
:
41273 case GOMP_MAP_ATTACH_DETACH
:
41276 error_at (OMP_CLAUSE_LOCATION (*pc
),
41277 "%<#pragma omp target%> with map-type other "
41278 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41279 "on %<map%> clause");
41280 *pc
= OMP_CLAUSE_CHAIN (*pc
);
41283 pc
= &OMP_CLAUSE_CHAIN (*pc
);
41289 # pragma acc cache (variable-list) new-line
41293 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
41295 tree stmt
, clauses
;
41297 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
41298 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
41300 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
41302 stmt
= make_node (OACC_CACHE
);
41303 TREE_TYPE (stmt
) = void_type_node
;
41304 OACC_CACHE_CLAUSES (stmt
) = clauses
;
41305 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41312 # pragma acc data oacc-data-clause[optseq] new-line
41313 structured-block */
41315 #define OACC_DATA_CLAUSE_MASK \
41316 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
41328 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41330 tree stmt
, clauses
, block
;
41333 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
41334 "#pragma acc data", pragma_tok
);
41336 block
= begin_omp_parallel ();
41337 save
= cp_parser_begin_omp_structured_block (parser
);
41338 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
41339 cp_parser_end_omp_structured_block (parser
, save
);
41340 stmt
= finish_oacc_data (clauses
, block
);
41345 # pragma acc host_data <clauses> new-line
41346 structured-block */
41348 #define OACC_HOST_DATA_CLAUSE_MASK \
41349 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
41350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
41354 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41356 tree stmt
, clauses
, block
;
41359 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
41360 "#pragma acc host_data", pragma_tok
);
41362 block
= begin_omp_parallel ();
41363 save
= cp_parser_begin_omp_structured_block (parser
);
41364 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
41365 cp_parser_end_omp_structured_block (parser
, save
);
41366 stmt
= finish_oacc_host_data (clauses
, block
);
41371 # pragma acc declare oacc-data-clause[optseq] new-line
41374 #define OACC_DECLARE_CLAUSE_MASK \
41375 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
41381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
41382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
41385 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
41387 tree clauses
, stmt
;
41388 bool error
= false;
41389 bool found_in_scope
= global_bindings_p ();
41391 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
41392 "#pragma acc declare", pragma_tok
, true);
41395 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
41397 error_at (pragma_tok
->location
,
41398 "no valid clauses specified in %<#pragma acc declare%>");
41402 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
41404 location_t loc
= OMP_CLAUSE_LOCATION (t
);
41405 tree decl
= OMP_CLAUSE_DECL (t
);
41406 if (!DECL_P (decl
))
41408 error_at (loc
, "array section in %<#pragma acc declare%>");
41412 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
41413 switch (OMP_CLAUSE_MAP_KIND (t
))
41415 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41416 case GOMP_MAP_ALLOC
:
41418 case GOMP_MAP_FORCE_DEVICEPTR
:
41419 case GOMP_MAP_DEVICE_RESIDENT
:
41422 case GOMP_MAP_LINK
:
41423 if (!global_bindings_p ()
41424 && (TREE_STATIC (decl
)
41425 || !DECL_EXTERNAL (decl
)))
41428 "%qD must be a global variable in "
41429 "%<#pragma acc declare link%>",
41437 if (global_bindings_p ())
41439 error_at (loc
, "invalid OpenACC clause at file scope");
41443 if (DECL_EXTERNAL (decl
))
41446 "invalid use of %<extern%> variable %qD "
41447 "in %<#pragma acc declare%>", decl
);
41451 else if (TREE_PUBLIC (decl
))
41454 "invalid use of %<global%> variable %qD "
41455 "in %<#pragma acc declare%>", decl
);
41462 if (!found_in_scope
)
41463 /* This seems to ignore the existence of cleanup scopes?
41464 What is the meaning for local extern decls? The local
41465 extern is in this scope, but it is referring to a decl that
41466 is namespace scope. */
41467 for (tree d
= current_binding_level
->names
; d
; d
= TREE_CHAIN (d
))
41470 found_in_scope
= true;
41473 if (!found_in_scope
)
41476 "%qD must be a variable declared in the same scope as "
41477 "%<#pragma acc declare%>", decl
);
41482 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
41483 || lookup_attribute ("omp declare target link",
41484 DECL_ATTRIBUTES (decl
)))
41486 error_at (loc
, "variable %qD used more than once with "
41487 "%<#pragma acc declare%>", decl
);
41496 if (DECL_LOCAL_DECL_P (decl
))
41497 /* We need to mark the aliased decl, as that is the entity
41498 that is being referred to. This won't work for
41499 dependent variables, but it didn't work for them before
41500 DECL_LOCAL_DECL_P was a thing either. But then
41501 dependent local extern variable decls are as rare as
41503 if (auto alias
= DECL_LOCAL_DECL_ALIAS (decl
))
41506 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
41507 id
= get_identifier ("omp declare target link");
41509 id
= get_identifier ("omp declare target");
41511 DECL_ATTRIBUTES (decl
)
41512 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
41513 if (current_binding_level
->kind
== sk_namespace
)
41515 symtab_node
*node
= symtab_node::get (decl
);
41518 node
->offloadable
= 1;
41519 if (ENABLE_OFFLOADING
)
41521 g
->have_offload
= true;
41522 if (is_a
<varpool_node
*> (node
))
41523 vec_safe_push (offload_vars
, decl
);
41530 if (error
|| current_binding_level
->kind
== sk_namespace
)
41533 stmt
= make_node (OACC_DECLARE
);
41534 TREE_TYPE (stmt
) = void_type_node
;
41535 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
41536 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41544 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
41548 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
41550 LOC is the location of the #pragma token.
41553 #define OACC_ENTER_DATA_CLAUSE_MASK \
41554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41561 #define OACC_EXIT_DATA_CLAUSE_MASK \
41562 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
41566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
41568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41571 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
41574 location_t loc
= pragma_tok
->location
;
41575 tree stmt
, clauses
;
41576 const char *p
= "";
41578 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41579 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
41581 if (strcmp (p
, "data") != 0)
41583 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
41584 enter
? "enter" : "exit");
41585 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41589 cp_lexer_consume_token (parser
->lexer
);
41592 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
41593 "#pragma acc enter data", pragma_tok
);
41595 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
41596 "#pragma acc exit data", pragma_tok
);
41598 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
41600 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
41601 enter
? "enter" : "exit");
41605 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
41606 TREE_TYPE (stmt
) = void_type_node
;
41607 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
41608 SET_EXPR_LOCATION (stmt
, loc
);
41614 # pragma acc loop oacc-loop-clause[optseq] new-line
41615 structured-block */
41617 #define OACC_LOOP_CLAUSE_MASK \
41618 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
41619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
41625 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
41626 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
41627 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
41630 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
41631 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
41633 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
41635 strcat (p_name
, " loop");
41636 mask
|= OACC_LOOP_CLAUSE_MASK
;
41638 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
41642 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
41644 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
41646 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
41649 tree block
= begin_omp_structured_block ();
41650 int save
= cp_parser_begin_omp_structured_block (parser
);
41651 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
41652 cp_parser_end_omp_structured_block (parser
, save
);
41653 add_stmt (finish_omp_structured_block (block
));
41659 # pragma acc kernels oacc-kernels-clause[optseq] new-line
41664 # pragma acc parallel oacc-parallel-clause[optseq] new-line
41669 # pragma acc serial oacc-serial-clause[optseq] new-line
41672 #define OACC_KERNELS_CLAUSE_MASK \
41673 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41674 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41675 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41676 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41677 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41689 #define OACC_PARALLEL_CLAUSE_MASK \
41690 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41705 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41706 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41707 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41709 #define OACC_SERIAL_CLAUSE_MASK \
41710 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41711 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41720 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41721 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41722 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41727 cp_parser_oacc_compute (cp_parser
*parser
, cp_token
*pragma_tok
,
41728 char *p_name
, bool *if_p
)
41730 omp_clause_mask mask
;
41731 enum tree_code code
;
41732 switch (cp_parser_pragma_kind (pragma_tok
))
41734 case PRAGMA_OACC_KERNELS
:
41735 strcat (p_name
, " kernels");
41736 mask
= OACC_KERNELS_CLAUSE_MASK
;
41737 code
= OACC_KERNELS
;
41739 case PRAGMA_OACC_PARALLEL
:
41740 strcat (p_name
, " parallel");
41741 mask
= OACC_PARALLEL_CLAUSE_MASK
;
41742 code
= OACC_PARALLEL
;
41744 case PRAGMA_OACC_SERIAL
:
41745 strcat (p_name
, " serial");
41746 mask
= OACC_SERIAL_CLAUSE_MASK
;
41747 code
= OACC_SERIAL
;
41750 gcc_unreachable ();
41753 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41756 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
41757 if (strcmp (p
, "loop") == 0)
41759 cp_lexer_consume_token (parser
->lexer
);
41760 tree block
= begin_omp_parallel ();
41762 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
41764 protected_set_expr_location (stmt
, pragma_tok
->location
);
41765 return finish_omp_construct (code
, block
, clauses
);
41769 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
41771 tree block
= begin_omp_parallel ();
41772 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
41773 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
41774 cp_parser_end_omp_structured_block (parser
, save
);
41775 return finish_omp_construct (code
, block
, clauses
);
41779 # pragma acc update oacc-update-clause[optseq] new-line
41782 #define OACC_UPDATE_CLAUSE_MASK \
41783 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
41785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
41786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
41788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
41791 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
41793 tree stmt
, clauses
;
41795 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
41796 "#pragma acc update", pragma_tok
);
41798 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
41800 error_at (pragma_tok
->location
,
41801 "%<#pragma acc update%> must contain at least one "
41802 "%<device%> or %<host%> or %<self%> clause");
41806 stmt
= make_node (OACC_UPDATE
);
41807 TREE_TYPE (stmt
) = void_type_node
;
41808 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
41809 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41815 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
41817 LOC is the location of the #pragma token.
41820 #define OACC_WAIT_CLAUSE_MASK \
41821 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
41824 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
41826 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
41827 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41829 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
41830 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
41832 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
41833 "#pragma acc wait", pragma_tok
);
41835 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
41836 stmt
= finish_expr_stmt (stmt
);
41842 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
41844 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
41845 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
41846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
41847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
41848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
41849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
41850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
41853 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
41854 enum pragma_context context
,
41857 bool first_p
= parser
->omp_declare_simd
== NULL
;
41858 cp_omp_declare_simd_data data
;
41861 data
.error_seen
= false;
41862 data
.fndecl_seen
= false;
41863 data
.variant_p
= variant_p
;
41864 data
.tokens
= vNULL
;
41865 data
.clauses
= NULL_TREE
;
41866 /* It is safe to take the address of a local variable; it will only be
41867 used while this scope is live. */
41868 parser
->omp_declare_simd
= &data
;
41870 else if (parser
->omp_declare_simd
->variant_p
!= variant_p
)
41872 error_at (pragma_tok
->location
,
41873 "%<#pragma omp declare %s%> followed by "
41874 "%<#pragma omp declare %s%>",
41875 parser
->omp_declare_simd
->variant_p
? "variant" : "simd",
41876 parser
->omp_declare_simd
->variant_p
? "simd" : "variant");
41877 parser
->omp_declare_simd
->error_seen
= true;
41880 /* Store away all pragma tokens. */
41881 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
41882 cp_lexer_consume_token (parser
->lexer
);
41883 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41884 struct cp_token_cache
*cp
41885 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
41886 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
41890 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
41891 cp_parser_pragma (parser
, context
, NULL
);
41894 case pragma_external
:
41895 cp_parser_declaration (parser
);
41897 case pragma_member
:
41898 cp_parser_member_declaration (parser
);
41900 case pragma_objc_icode
:
41901 cp_parser_block_declaration (parser
, /*statement_p=*/false);
41904 cp_parser_declaration_statement (parser
);
41907 if (parser
->omp_declare_simd
41908 && !parser
->omp_declare_simd
->error_seen
41909 && !parser
->omp_declare_simd
->fndecl_seen
)
41910 error_at (pragma_tok
->location
,
41911 "%<#pragma omp declare %s%> not immediately followed by "
41912 "function declaration or definition",
41913 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
41914 data
.tokens
.release ();
41915 parser
->omp_declare_simd
= NULL
;
41919 static const char *const omp_construct_selectors
[] = {
41920 "simd", "target", "teams", "parallel", "for", NULL
};
41921 static const char *const omp_device_selectors
[] = {
41922 "kind", "isa", "arch", NULL
};
41923 static const char *const omp_implementation_selectors
[] = {
41924 "vendor", "extension", "atomic_default_mem_order", "unified_address",
41925 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL
};
41926 static const char *const omp_user_selectors
[] = {
41927 "condition", NULL
};
41932 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
41935 score(score-expression) */
41938 cp_parser_omp_context_selector (cp_parser
*parser
, tree set
, bool has_parms_p
)
41940 tree ret
= NULL_TREE
;
41944 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
41945 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41946 selector
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41949 cp_parser_error (parser
, "expected trait selector name");
41950 return error_mark_node
;
41953 tree properties
= NULL_TREE
;
41954 const char *const *selectors
= NULL
;
41955 bool allow_score
= true;
41956 bool allow_user
= false;
41957 int property_limit
= 0;
41958 enum { CTX_PROPERTY_NONE
, CTX_PROPERTY_USER
, CTX_PROPERTY_NAME_LIST
,
41959 CTX_PROPERTY_ID
, CTX_PROPERTY_EXPR
,
41960 CTX_PROPERTY_SIMD
} property_kind
= CTX_PROPERTY_NONE
;
41961 switch (IDENTIFIER_POINTER (set
)[0])
41963 case 'c': /* construct */
41964 selectors
= omp_construct_selectors
;
41965 allow_score
= false;
41966 property_limit
= 1;
41967 property_kind
= CTX_PROPERTY_SIMD
;
41969 case 'd': /* device */
41970 selectors
= omp_device_selectors
;
41971 allow_score
= false;
41973 property_limit
= 3;
41974 property_kind
= CTX_PROPERTY_NAME_LIST
;
41976 case 'i': /* implementation */
41977 selectors
= omp_implementation_selectors
;
41979 property_limit
= 3;
41980 property_kind
= CTX_PROPERTY_NAME_LIST
;
41982 case 'u': /* user */
41983 selectors
= omp_user_selectors
;
41984 property_limit
= 1;
41985 property_kind
= CTX_PROPERTY_EXPR
;
41988 gcc_unreachable ();
41990 for (int i
= 0; ; i
++)
41992 if (selectors
[i
] == NULL
)
41996 property_kind
= CTX_PROPERTY_USER
;
42001 error ("selector %qs not allowed for context selector "
42002 "set %qs", IDENTIFIER_POINTER (selector
),
42003 IDENTIFIER_POINTER (set
));
42004 cp_lexer_consume_token (parser
->lexer
);
42005 return error_mark_node
;
42008 if (i
== property_limit
)
42009 property_kind
= CTX_PROPERTY_NONE
;
42010 if (strcmp (selectors
[i
], IDENTIFIER_POINTER (selector
)) == 0)
42013 if (property_kind
== CTX_PROPERTY_NAME_LIST
42014 && IDENTIFIER_POINTER (set
)[0] == 'i'
42015 && strcmp (IDENTIFIER_POINTER (selector
),
42016 "atomic_default_mem_order") == 0)
42017 property_kind
= CTX_PROPERTY_ID
;
42019 cp_lexer_consume_token (parser
->lexer
);
42021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42023 if (property_kind
== CTX_PROPERTY_NONE
)
42025 error ("selector %qs does not accept any properties",
42026 IDENTIFIER_POINTER (selector
));
42027 return error_mark_node
;
42030 matching_parens parens
;
42031 parens
.consume_open (parser
);
42033 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
42035 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
42036 && strcmp (IDENTIFIER_POINTER (token
->u
.value
), "score") == 0
42037 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
42039 cp_lexer_save_tokens (parser
->lexer
);
42040 cp_lexer_consume_token (parser
->lexer
);
42041 cp_lexer_consume_token (parser
->lexer
);
42042 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
42044 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
42046 cp_lexer_rollback_tokens (parser
->lexer
);
42047 cp_lexer_consume_token (parser
->lexer
);
42049 matching_parens parens2
;
42050 parens2
.require_open (parser
);
42051 tree score
= cp_parser_constant_expression (parser
);
42052 if (!parens2
.require_close (parser
))
42053 cp_parser_skip_to_closing_parenthesis (parser
, true,
42055 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
42056 if (score
!= error_mark_node
)
42058 score
= fold_non_dependent_expr (score
);
42059 if (value_dependent_expression_p (score
))
42060 properties
= tree_cons (get_identifier (" score"),
42061 score
, properties
);
42062 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score
))
42063 || TREE_CODE (score
) != INTEGER_CST
)
42064 error_at (token
->location
, "score argument must be "
42065 "constant integer expression");
42066 else if (tree_int_cst_sgn (score
) < 0)
42067 error_at (token
->location
, "score argument must be "
42070 properties
= tree_cons (get_identifier (" score"),
42071 score
, properties
);
42075 cp_lexer_rollback_tokens (parser
->lexer
);
42077 token
= cp_lexer_peek_token (parser
->lexer
);
42080 switch (property_kind
)
42083 case CTX_PROPERTY_USER
:
42086 t
= cp_parser_constant_expression (parser
);
42087 if (t
!= error_mark_node
)
42089 t
= fold_non_dependent_expr (t
);
42090 if (TREE_CODE (t
) == STRING_CST
)
42091 properties
= tree_cons (NULL_TREE
, t
, properties
);
42092 else if (!value_dependent_expression_p (t
)
42093 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
42094 || !tree_fits_shwi_p (t
)))
42095 error_at (token
->location
, "property must be "
42096 "constant integer expression or string "
42099 properties
= tree_cons (NULL_TREE
, t
, properties
);
42102 return error_mark_node
;
42104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42105 cp_lexer_consume_token (parser
->lexer
);
42111 case CTX_PROPERTY_ID
:
42112 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
42113 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42115 tree prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42116 cp_lexer_consume_token (parser
->lexer
);
42117 properties
= tree_cons (prop
, NULL_TREE
, properties
);
42121 cp_parser_error (parser
, "expected identifier");
42122 return error_mark_node
;
42125 case CTX_PROPERTY_NAME_LIST
:
42128 tree prop
= NULL_TREE
, value
= NULL_TREE
;
42129 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
42130 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42132 prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42133 cp_lexer_consume_token (parser
->lexer
);
42135 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_STRING
))
42136 value
= cp_parser_string_literal (parser
, false, false);
42139 cp_parser_error (parser
, "expected identifier or "
42141 return error_mark_node
;
42144 properties
= tree_cons (prop
, value
, properties
);
42146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42147 cp_lexer_consume_token (parser
->lexer
);
42153 case CTX_PROPERTY_EXPR
:
42154 t
= cp_parser_constant_expression (parser
);
42155 if (t
!= error_mark_node
)
42157 t
= fold_non_dependent_expr (t
);
42158 if (!value_dependent_expression_p (t
)
42159 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
42160 || !tree_fits_shwi_p (t
)))
42161 error_at (token
->location
, "property must be "
42162 "constant integer expression");
42164 properties
= tree_cons (NULL_TREE
, t
, properties
);
42167 return error_mark_node
;
42169 case CTX_PROPERTY_SIMD
:
42172 error_at (token
->location
, "properties for %<simd%> "
42173 "selector may not be specified in "
42174 "%<metadirective%>");
42175 return error_mark_node
;
42178 = cp_parser_omp_all_clauses (parser
,
42179 OMP_DECLARE_SIMD_CLAUSE_MASK
,
42180 "simd", NULL
, true, 2);
42183 gcc_unreachable ();
42186 if (!parens
.require_close (parser
))
42187 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
42189 properties
= nreverse (properties
);
42191 else if (property_kind
== CTX_PROPERTY_NAME_LIST
42192 || property_kind
== CTX_PROPERTY_ID
42193 || property_kind
== CTX_PROPERTY_EXPR
)
42195 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
42196 return error_mark_node
;
42199 ret
= tree_cons (selector
, properties
, ret
);
42201 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42202 cp_lexer_consume_token (parser
->lexer
);
42208 return nreverse (ret
);
42213 trait-set-selector[,trait-set-selector[,...]]
42215 trait-set-selector:
42216 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
42218 trait-set-selector-name:
42225 cp_parser_omp_context_selector_specification (cp_parser
*parser
,
42228 tree ret
= NULL_TREE
;
42231 const char *setp
= "";
42232 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42234 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
42238 if (strcmp (setp
, "construct") == 0)
42242 if (strcmp (setp
, "device") == 0)
42246 if (strcmp (setp
, "implementation") == 0)
42250 if (strcmp (setp
, "user") == 0)
42258 cp_parser_error (parser
, "expected %<construct%>, %<device%>, "
42259 "%<implementation%> or %<user%>");
42260 return error_mark_node
;
42263 tree set
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42264 cp_lexer_consume_token (parser
->lexer
);
42266 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
42267 return error_mark_node
;
42269 matching_braces braces
;
42270 if (!braces
.require_open (parser
))
42271 return error_mark_node
;
42274 = cp_parser_omp_context_selector (parser
, set
, has_parms_p
);
42275 if (selectors
== error_mark_node
)
42277 cp_parser_skip_to_closing_brace (parser
);
42278 ret
= error_mark_node
;
42280 else if (ret
!= error_mark_node
)
42281 ret
= tree_cons (set
, selectors
, ret
);
42283 braces
.require_close (parser
);
42285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42286 cp_lexer_consume_token (parser
->lexer
);
42292 if (ret
== error_mark_node
)
42294 return nreverse (ret
);
42297 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
42298 that into "omp declare variant base" attribute. */
42301 cp_finish_omp_declare_variant (cp_parser
*parser
, cp_token
*pragma_tok
,
42304 matching_parens parens
;
42305 if (!parens
.require_open (parser
))
42308 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42313 cp_id_kind idk
= CP_ID_KIND_NONE
;
42314 cp_token
*varid_token
= cp_lexer_peek_token (parser
->lexer
);
42316 = cp_parser_id_expression (parser
, /*template_keyword_p=*/false,
42317 /*check_dependency_p=*/true,
42318 /*template_p=*/&template_p
,
42319 /*declarator_p=*/false,
42320 /*optional_p=*/false);
42321 parens
.require_close (parser
);
42324 if (TREE_CODE (varid
) == TEMPLATE_ID_EXPR
42325 || TREE_CODE (varid
) == TYPE_DECL
42326 || varid
== error_mark_node
)
42328 else if (varid_token
->type
== CPP_NAME
&& varid_token
->error_reported
)
42329 variant
= NULL_TREE
;
42332 tree ambiguous_decls
;
42333 variant
= cp_parser_lookup_name (parser
, varid
, none_type
,
42334 template_p
, /*is_namespace=*/false,
42335 /*check_dependency=*/true,
42337 varid
.get_location ());
42338 if (ambiguous_decls
)
42339 variant
= NULL_TREE
;
42341 if (variant
== NULL_TREE
)
42342 variant
= error_mark_node
;
42343 else if (TREE_CODE (variant
) != SCOPE_REF
)
42345 const char *error_msg
;
42347 = finish_id_expression (varid
, variant
, parser
->scope
,
42349 &parser
->non_integral_constant_expression_p
,
42350 template_p
, true, false, false, &error_msg
,
42351 varid
.get_location ());
42353 cp_parser_error (parser
, error_msg
);
42355 location_t caret_loc
= get_pure_location (varid
.get_location ());
42356 location_t start_loc
= get_start (varid_token
->location
);
42357 location_t finish_loc
= get_finish (varid
.get_location ());
42358 location_t varid_loc
= make_location (caret_loc
, start_loc
, finish_loc
);
42360 const char *clause
= "";
42361 location_t match_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42362 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42363 clause
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
42364 if (strcmp (clause
, "match"))
42366 cp_parser_error (parser
, "expected %<match%>");
42370 cp_lexer_consume_token (parser
->lexer
);
42372 if (!parens
.require_open (parser
))
42375 tree ctx
= cp_parser_omp_context_selector_specification (parser
, true);
42376 if (ctx
== error_mark_node
)
42378 ctx
= c_omp_check_context_selector (match_loc
, ctx
);
42379 if (ctx
!= error_mark_node
&& variant
!= error_mark_node
)
42381 tree match_loc_node
= maybe_wrap_with_location (integer_zero_node
,
42383 tree loc_node
= maybe_wrap_with_location (integer_zero_node
, varid_loc
);
42384 loc_node
= tree_cons (match_loc_node
,
42385 build_int_cst (integer_type_node
, idk
),
42386 build_tree_list (loc_node
, integer_zero_node
));
42387 attrs
= tree_cons (get_identifier ("omp declare variant base"),
42388 tree_cons (variant
, ctx
, loc_node
), attrs
);
42389 if (processing_template_decl
)
42390 ATTR_IS_DEPENDENT (attrs
) = 1;
42393 parens
.require_close (parser
);
42394 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42399 /* Finalize #pragma omp declare simd clauses after direct declarator has
42400 been parsed, and put that into "omp declare simd" attribute. */
42403 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
42405 struct cp_token_cache
*ce
;
42406 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
42409 if (!data
->error_seen
&& data
->fndecl_seen
)
42411 error ("%<#pragma omp declare %s%> not immediately followed by "
42412 "a single function declaration or definition",
42413 data
->variant_p
? "variant" : "simd");
42414 data
->error_seen
= true;
42416 if (data
->error_seen
)
42419 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
42423 cp_parser_push_lexer_for_tokens (parser
, ce
);
42424 parser
->lexer
->in_pragma
= true;
42425 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
42426 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
42427 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42428 const char *kind
= IDENTIFIER_POINTER (id
);
42429 cp_lexer_consume_token (parser
->lexer
);
42430 if (strcmp (kind
, "simd") == 0)
42432 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
42433 "#pragma omp declare simd",
42436 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
42437 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
42438 TREE_CHAIN (c
) = attrs
;
42439 if (processing_template_decl
)
42440 ATTR_IS_DEPENDENT (c
) = 1;
42445 gcc_assert (strcmp (kind
, "variant") == 0);
42446 attrs
= cp_finish_omp_declare_variant (parser
, pragma_tok
, attrs
);
42448 cp_parser_pop_lexer (parser
);
42451 data
->fndecl_seen
= true;
42457 # pragma omp declare target new-line
42458 declarations and definitions
42459 # pragma omp end declare target new-line
42462 # pragma omp declare target ( extended-list ) new-line
42464 # pragma omp declare target declare-target-clauses[seq] new-line */
42466 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
42467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
42468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
42469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
42472 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
42474 tree clauses
= NULL_TREE
;
42475 int device_type
= 0;
42476 bool only_device_type
= true;
42477 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42479 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
42480 "#pragma omp declare target", pragma_tok
);
42481 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42483 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
42485 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
42486 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42490 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42491 scope_chain
->omp_declare_target_attribute
++;
42494 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
42495 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
42496 device_type
|= OMP_CLAUSE_DEVICE_TYPE_KIND (c
);
42497 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
42499 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
42501 tree t
= OMP_CLAUSE_DECL (c
), id
;
42502 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
42503 tree at2
= lookup_attribute ("omp declare target link",
42504 DECL_ATTRIBUTES (t
));
42505 only_device_type
= false;
42506 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
42508 id
= get_identifier ("omp declare target link");
42509 std::swap (at1
, at2
);
42512 id
= get_identifier ("omp declare target");
42515 error_at (OMP_CLAUSE_LOCATION (c
),
42516 "%qD specified both in declare target %<link%> and %<to%>"
42522 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
42523 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
42526 symtab_node
*node
= symtab_node::get (t
);
42529 node
->offloadable
= 1;
42530 if (ENABLE_OFFLOADING
)
42532 g
->have_offload
= true;
42533 if (is_a
<varpool_node
*> (node
))
42534 vec_safe_push (offload_vars
, t
);
42538 if (TREE_CODE (t
) != FUNCTION_DECL
)
42540 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0)
42542 tree at3
= lookup_attribute ("omp declare target host",
42543 DECL_ATTRIBUTES (t
));
42544 if (at3
== NULL_TREE
)
42546 id
= get_identifier ("omp declare target host");
42547 DECL_ATTRIBUTES (t
)
42548 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
42551 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0)
42553 tree at3
= lookup_attribute ("omp declare target nohost",
42554 DECL_ATTRIBUTES (t
));
42555 if (at3
== NULL_TREE
)
42557 id
= get_identifier ("omp declare target nohost");
42558 DECL_ATTRIBUTES (t
)
42559 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
42563 if (device_type
&& only_device_type
)
42564 warning_at (OMP_CLAUSE_LOCATION (clauses
), 0,
42565 "directive with only %<device_type%> clauses ignored");
42569 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
42571 const char *p
= "";
42572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42574 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42575 p
= IDENTIFIER_POINTER (id
);
42577 if (strcmp (p
, "declare") == 0)
42579 cp_lexer_consume_token (parser
->lexer
);
42581 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42583 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42584 p
= IDENTIFIER_POINTER (id
);
42586 if (strcmp (p
, "target") == 0)
42587 cp_lexer_consume_token (parser
->lexer
);
42590 cp_parser_error (parser
, "expected %<target%>");
42591 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42597 cp_parser_error (parser
, "expected %<declare%>");
42598 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42601 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42602 if (!scope_chain
->omp_declare_target_attribute
)
42603 error_at (pragma_tok
->location
,
42604 "%<#pragma omp end declare target%> without corresponding "
42605 "%<#pragma omp declare target%>");
42607 scope_chain
->omp_declare_target_attribute
--;
42610 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
42611 expression and optional initializer clause of
42612 #pragma omp declare reduction. We store the expression(s) as
42613 either 3, 6 or 7 special statements inside of the artificial function's
42614 body. The first two statements are DECL_EXPRs for the artificial
42615 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
42616 expression that uses those variables.
42617 If there was any INITIALIZER clause, this is followed by further statements,
42618 the fourth and fifth statements are DECL_EXPRs for the artificial
42619 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
42620 constructor variant (first token after open paren is not omp_priv),
42621 then the sixth statement is a statement with the function call expression
42622 that uses the OMP_PRIV and optionally OMP_ORIG variable.
42623 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
42624 to initialize the OMP_PRIV artificial variable and there is seventh
42625 statement, a DECL_EXPR of the OMP_PRIV statement again. */
42628 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
42630 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
42631 gcc_assert (TYPE_REF_P (type
));
42632 type
= TREE_TYPE (type
);
42633 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
42634 DECL_ARTIFICIAL (omp_out
) = 1;
42635 pushdecl (omp_out
);
42636 add_decl_expr (omp_out
);
42637 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
42638 DECL_ARTIFICIAL (omp_in
) = 1;
42640 add_decl_expr (omp_in
);
42642 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
42644 keep_next_level (true);
42645 tree block
= begin_omp_structured_block ();
42646 combiner
= cp_parser_expression (parser
);
42647 finish_expr_stmt (combiner
);
42648 block
= finish_omp_structured_block (block
);
42649 if (processing_template_decl
)
42650 block
= build_stmt (input_location
, EXPR_STMT
, block
);
42653 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
42656 const char *p
= "";
42657 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42659 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42660 p
= IDENTIFIER_POINTER (id
);
42663 if (strcmp (p
, "initializer") == 0)
42665 cp_lexer_consume_token (parser
->lexer
);
42666 matching_parens parens
;
42667 if (!parens
.require_open (parser
))
42671 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42673 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42674 p
= IDENTIFIER_POINTER (id
);
42677 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
42678 DECL_ARTIFICIAL (omp_priv
) = 1;
42679 pushdecl (omp_priv
);
42680 add_decl_expr (omp_priv
);
42681 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
42682 DECL_ARTIFICIAL (omp_orig
) = 1;
42683 pushdecl (omp_orig
);
42684 add_decl_expr (omp_orig
);
42686 keep_next_level (true);
42687 block
= begin_omp_structured_block ();
42690 if (strcmp (p
, "omp_priv") == 0)
42692 bool is_direct_init
, is_non_constant_init
;
42694 cp_lexer_consume_token (parser
->lexer
);
42695 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
42696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
42697 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
42698 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
42700 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
42701 == CPP_CLOSE_PAREN
))
42703 finish_omp_structured_block (block
);
42704 error ("invalid initializer clause");
42707 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
42708 &is_non_constant_init
);
42709 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
42710 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
42714 cp_parser_parse_tentatively (parser
);
42715 /* Don't create location wrapper nodes here. */
42716 auto_suppress_location_wrappers sentinel
;
42717 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
42718 /*check_dependency_p=*/true,
42719 /*template_p=*/NULL
,
42720 /*declarator_p=*/false,
42721 /*optional_p=*/false);
42722 vec
<tree
, va_gc
> *args
;
42723 if (fn_name
== error_mark_node
42724 || cp_parser_error_occurred (parser
)
42725 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
42726 || ((args
= cp_parser_parenthesized_expression_list
42727 (parser
, non_attr
, /*cast_p=*/false,
42728 /*allow_expansion_p=*/true,
42729 /*non_constant_p=*/NULL
)),
42730 cp_parser_error_occurred (parser
)))
42732 finish_omp_structured_block (block
);
42733 cp_parser_abort_tentative_parse (parser
);
42734 cp_parser_error (parser
, "expected id-expression (arguments)");
42739 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
42740 if (arg
== omp_priv
42741 || (TREE_CODE (arg
) == ADDR_EXPR
42742 && TREE_OPERAND (arg
, 0) == omp_priv
))
42744 cp_parser_abort_tentative_parse (parser
);
42745 if (arg
== NULL_TREE
)
42746 error ("one of the initializer call arguments should be %<omp_priv%>"
42747 " or %<&omp_priv%>");
42748 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
42750 finish_expr_stmt (initializer
);
42753 block
= finish_omp_structured_block (block
);
42754 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
42755 if (processing_template_decl
)
42756 block
= build_stmt (input_location
, EXPR_STMT
, block
);
42760 add_decl_expr (omp_orig
);
42762 if (!parens
.require_close (parser
))
42766 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
42767 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
42774 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42775 initializer-clause[opt] new-line
42777 initializer-clause:
42778 initializer (omp_priv initializer)
42779 initializer (function-name (argument-list)) */
42782 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
42783 enum pragma_context
)
42785 auto_vec
<tree
> types
;
42786 enum tree_code reduc_code
= ERROR_MARK
;
42787 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
42789 cp_token
*first_token
;
42790 cp_token_cache
*cp
;
42794 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
42795 p
= obstack_alloc (&declarator_obstack
, 0);
42797 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
42800 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
42803 reduc_code
= PLUS_EXPR
;
42806 reduc_code
= MULT_EXPR
;
42809 reduc_code
= MINUS_EXPR
;
42812 reduc_code
= BIT_AND_EXPR
;
42815 reduc_code
= BIT_XOR_EXPR
;
42818 reduc_code
= BIT_IOR_EXPR
;
42821 reduc_code
= TRUTH_ANDIF_EXPR
;
42824 reduc_code
= TRUTH_ORIF_EXPR
;
42827 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
42830 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
42831 "%<|%>, %<&&%>, %<||%> or identifier");
42835 if (reduc_code
!= ERROR_MARK
)
42836 cp_lexer_consume_token (parser
->lexer
);
42838 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
42839 if (reduc_id
== error_mark_node
)
42842 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
42845 /* Types may not be defined in declare reduction type list. */
42846 const char *saved_message
;
42847 saved_message
= parser
->type_definition_forbidden_message
;
42848 parser
->type_definition_forbidden_message
42849 = G_("types may not be defined in declare reduction type list");
42850 bool saved_colon_corrects_to_scope_p
;
42851 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
42852 parser
->colon_corrects_to_scope_p
= false;
42853 bool saved_colon_doesnt_start_class_def_p
;
42854 saved_colon_doesnt_start_class_def_p
42855 = parser
->colon_doesnt_start_class_def_p
;
42856 parser
->colon_doesnt_start_class_def_p
= true;
42860 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42861 type
= cp_parser_type_id (parser
);
42862 if (type
== error_mark_node
)
42864 else if (ARITHMETIC_TYPE_P (type
)
42865 && (orig_reduc_id
== NULL_TREE
42866 || (TREE_CODE (type
) != COMPLEX_TYPE
42867 && (id_equal (orig_reduc_id
, "min")
42868 || id_equal (orig_reduc_id
, "max")))))
42869 error_at (loc
, "predeclared arithmetic type %qT in "
42870 "%<#pragma omp declare reduction%>", type
);
42871 else if (FUNC_OR_METHOD_TYPE_P (type
)
42872 || TREE_CODE (type
) == ARRAY_TYPE
)
42873 error_at (loc
, "function or array type %qT in "
42874 "%<#pragma omp declare reduction%>", type
);
42875 else if (TYPE_REF_P (type
))
42876 error_at (loc
, "reference type %qT in "
42877 "%<#pragma omp declare reduction%>", type
);
42878 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
42879 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
42880 "type %qT in %<#pragma omp declare reduction%>", type
);
42882 types
.safe_push (type
);
42884 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42885 cp_lexer_consume_token (parser
->lexer
);
42890 /* Restore the saved message. */
42891 parser
->type_definition_forbidden_message
= saved_message
;
42892 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
42893 parser
->colon_doesnt_start_class_def_p
42894 = saved_colon_doesnt_start_class_def_p
;
42896 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
42897 || types
.is_empty ())
42900 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42904 first_token
= cp_lexer_peek_token (parser
->lexer
);
42907 FOR_EACH_VEC_ELT (types
, i
, type
)
42910 = build_function_type_list (void_type_node
,
42911 cp_build_reference_type (type
, false),
42913 tree this_reduc_id
= reduc_id
;
42914 if (!dependent_type_p (type
))
42915 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
42916 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
42917 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
42918 DECL_ARTIFICIAL (fndecl
) = 1;
42919 DECL_EXTERNAL (fndecl
) = 1;
42920 DECL_DECLARED_INLINE_P (fndecl
) = 1;
42921 DECL_IGNORED_P (fndecl
) = 1;
42922 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
42923 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
42924 DECL_ATTRIBUTES (fndecl
)
42925 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
42926 DECL_ATTRIBUTES (fndecl
));
42927 bool block_scope
= false;
42928 if (current_function_decl
)
42930 block_scope
= true;
42931 DECL_CONTEXT (fndecl
) = current_function_decl
;
42932 DECL_LOCAL_DECL_P (fndecl
) = true;
42935 if (processing_template_decl
)
42936 fndecl
= push_template_decl (fndecl
);
42940 if (!processing_template_decl
)
42943 else if (current_class_type
)
42947 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
42948 cp_lexer_consume_token (parser
->lexer
);
42949 cp
= cp_token_cache_new (first_token
,
42950 cp_lexer_peek_nth_token (parser
->lexer
,
42953 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
42954 finish_member_declaration (fndecl
);
42955 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
42956 DECL_PENDING_INLINE_P (fndecl
) = 1;
42957 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
42962 DECL_CONTEXT (fndecl
) = current_namespace
;
42963 tree d
= pushdecl (fndecl
);
42964 /* We should never meet a matched duplicate decl. */
42965 gcc_checking_assert (d
== error_mark_node
|| d
== fndecl
);
42968 tree block
= NULL_TREE
;
42970 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
42972 block
= begin_omp_structured_block ();
42975 cp_parser_push_lexer_for_tokens (parser
, cp
);
42976 parser
->lexer
->in_pragma
= true;
42979 bool ok
= cp_parser_omp_declare_reduction_exprs (fndecl
, parser
);
42982 cp_parser_pop_lexer (parser
);
42984 finish_function (/*inline_p=*/false);
42987 DECL_CONTEXT (fndecl
) = current_function_decl
;
42988 if (DECL_TEMPLATE_INFO (fndecl
))
42989 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl
)) = current_function_decl
;
42996 block
= finish_omp_structured_block (block
);
42997 if (TREE_CODE (block
) == BIND_EXPR
)
42998 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
42999 else if (TREE_CODE (block
) == STATEMENT_LIST
)
43000 DECL_SAVED_TREE (fndecl
) = block
;
43001 if (processing_template_decl
)
43002 add_decl_expr (fndecl
);
43005 cp_check_omp_declare_reduction (fndecl
);
43006 if (cp
== NULL
&& types
.length () > 1)
43007 cp
= cp_token_cache_new (first_token
,
43008 cp_lexer_peek_nth_token (parser
->lexer
, 2));
43009 if (errs
!= errorcount
)
43013 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43016 /* Free any declarators allocated. */
43017 obstack_free (&declarator_obstack
, p
);
43021 #pragma omp declare simd declare-simd-clauses[optseq] new-line
43022 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43023 initializer-clause[opt] new-line
43024 #pragma omp declare target new-line
43027 #pragma omp declare variant (identifier) match (context-selector) */
43030 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
43031 enum pragma_context context
)
43033 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43035 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43036 const char *p
= IDENTIFIER_POINTER (id
);
43038 if (strcmp (p
, "simd") == 0)
43040 cp_lexer_consume_token (parser
->lexer
);
43041 cp_parser_omp_declare_simd (parser
, pragma_tok
,
43045 if (flag_openmp
&& strcmp (p
, "variant") == 0)
43047 cp_lexer_consume_token (parser
->lexer
);
43048 cp_parser_omp_declare_simd (parser
, pragma_tok
,
43052 cp_ensure_no_omp_declare_simd (parser
);
43053 if (strcmp (p
, "reduction") == 0)
43055 cp_lexer_consume_token (parser
->lexer
);
43056 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
43060 if (!flag_openmp
) /* flag_openmp_simd */
43062 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43065 if (strcmp (p
, "target") == 0)
43067 cp_lexer_consume_token (parser
->lexer
);
43068 cp_parser_omp_declare_target (parser
, pragma_tok
);
43072 cp_parser_error (parser
, "expected %<simd%>, %<reduction%>, "
43073 "%<target%> or %<variant%>");
43074 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43079 #pragma omp requires clauses[optseq] new-line */
43082 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
43085 enum omp_requires new_req
= (enum omp_requires
) 0;
43087 location_t loc
= pragma_tok
->location
;
43088 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
43090 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
43091 cp_lexer_consume_token (parser
->lexer
);
43095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43097 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43098 const char *p
= IDENTIFIER_POINTER (id
);
43099 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43100 enum omp_requires this_req
= (enum omp_requires
) 0;
43102 if (!strcmp (p
, "unified_address"))
43103 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
43104 else if (!strcmp (p
, "unified_shared_memory"))
43105 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
43106 else if (!strcmp (p
, "dynamic_allocators"))
43107 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
43108 else if (!strcmp (p
, "reverse_offload"))
43109 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
43110 else if (!strcmp (p
, "atomic_default_mem_order"))
43112 cp_lexer_consume_token (parser
->lexer
);
43114 matching_parens parens
;
43115 if (parens
.require_open (parser
))
43117 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43119 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43120 p
= IDENTIFIER_POINTER (id
);
43122 if (!strcmp (p
, "seq_cst"))
43124 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
43125 else if (!strcmp (p
, "relaxed"))
43127 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
43128 else if (!strcmp (p
, "acq_rel"))
43130 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
43134 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
43135 "expected %<seq_cst%>, %<relaxed%> or "
43137 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
43139 cp_lexer_consume_token (parser
->lexer
);
43142 cp_lexer_consume_token (parser
->lexer
);
43144 if (!parens
.require_close (parser
))
43145 cp_parser_skip_to_closing_parenthesis (parser
,
43146 /*recovering=*/true,
43147 /*or_comma=*/false,
43153 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43161 error_at (cloc
, "expected %<unified_address%>, "
43162 "%<unified_shared_memory%>, "
43163 "%<dynamic_allocators%>, "
43164 "%<reverse_offload%> "
43165 "or %<atomic_default_mem_order%> clause");
43166 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43170 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
43171 "supported yet", p
);
43173 cp_lexer_consume_token (parser
->lexer
);
43176 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
43178 if ((this_req
& new_req
) != 0)
43179 error_at (cloc
, "too many %qs clauses", p
);
43180 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
43181 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
43182 error_at (cloc
, "%qs clause used lexically after first "
43183 "target construct or offloading API", p
);
43185 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
43187 error_at (cloc
, "too many %qs clauses",
43188 "atomic_default_mem_order");
43189 this_req
= (enum omp_requires
) 0;
43191 else if ((omp_requires_mask
43192 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
43194 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
43195 " clause in a single compilation unit");
43197 = (enum omp_requires
)
43199 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
43201 else if ((omp_requires_mask
43202 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
43203 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
43204 "lexically after first %<atomic%> construct "
43205 "without memory order clause");
43206 new_req
= (enum omp_requires
) (new_req
| this_req
);
43208 = (enum omp_requires
) (omp_requires_mask
| this_req
);
43214 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43217 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
43223 #pragma omp taskloop taskloop-clause[optseq] new-line
43226 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
43229 #define OMP_TASKLOOP_CLAUSE_MASK \
43230 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43231 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43232 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43233 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43234 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
43236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
43237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
43243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43246 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
43249 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
43250 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43253 tree clauses
, sb
, ret
;
43255 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43257 strcat (p_name
, " taskloop");
43258 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
43259 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
43261 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
43262 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
43264 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43266 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43267 const char *p
= IDENTIFIER_POINTER (id
);
43269 if (strcmp (p
, "simd") == 0)
43271 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43272 if (cclauses
== NULL
)
43273 cclauses
= cclauses_buf
;
43275 cp_lexer_consume_token (parser
->lexer
);
43276 if (!flag_openmp
) /* flag_openmp_simd */
43277 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43279 sb
= begin_omp_structured_block ();
43280 save
= cp_parser_begin_omp_structured_block (parser
);
43281 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43283 cp_parser_end_omp_structured_block (parser
, save
);
43284 tree body
= finish_omp_structured_block (sb
);
43287 ret
= make_node (OMP_TASKLOOP
);
43288 TREE_TYPE (ret
) = void_type_node
;
43289 OMP_FOR_BODY (ret
) = body
;
43290 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
43291 SET_EXPR_LOCATION (ret
, loc
);
43296 if (!flag_openmp
) /* flag_openmp_simd */
43298 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43302 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
43306 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
43307 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
43310 keep_next_level (true);
43311 sb
= begin_omp_structured_block ();
43312 save
= cp_parser_begin_omp_structured_block (parser
);
43314 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
43317 cp_parser_end_omp_structured_block (parser
, save
);
43318 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
43325 # pragma acc routine oacc-routine-clause[optseq] new-line
43326 function-definition
43328 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
43331 #define OACC_ROUTINE_CLAUSE_MASK \
43332 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
43333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
43334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
43335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
43338 /* Parse the OpenACC routine pragma. This has an optional '( name )'
43339 component, which must resolve to a declared namespace-scope
43340 function. The clauses are either processed directly (for a named
43341 function), or defered until the immediatley following declaration
43345 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
43346 enum pragma_context context
)
43348 gcc_checking_assert (context
== pragma_external
);
43349 /* The checking for "another pragma following this one" in the "no optional
43350 '( name )'" case makes sure that we dont re-enter. */
43351 gcc_checking_assert (parser
->oacc_routine
== NULL
);
43353 cp_oacc_routine_data data
;
43354 data
.error_seen
= false;
43355 data
.fndecl_seen
= false;
43356 data
.tokens
= vNULL
;
43357 data
.clauses
= NULL_TREE
;
43358 data
.loc
= pragma_tok
->location
;
43359 /* It is safe to take the address of a local variable; it will only be
43360 used while this scope is live. */
43361 parser
->oacc_routine
= &data
;
43363 /* Look for optional '( name )'. */
43364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
43366 matching_parens parens
;
43367 parens
.consume_open (parser
); /* '(' */
43369 /* We parse the name as an id-expression. If it resolves to
43370 anything other than a non-overloaded function at namespace
43371 scope, it's an error. */
43372 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43373 tree name
= cp_parser_id_expression (parser
,
43374 /*template_keyword_p=*/false,
43375 /*check_dependency_p=*/false,
43376 /*template_p=*/NULL
,
43377 /*declarator_p=*/false,
43378 /*optional_p=*/false);
43379 tree decl
= (identifier_p (name
)
43380 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
43382 if (name
!= error_mark_node
&& decl
== error_mark_node
)
43383 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
43385 if (decl
== error_mark_node
43386 || !parens
.require_close (parser
))
43388 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43389 parser
->oacc_routine
= NULL
;
43394 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
43395 "#pragma acc routine",
43396 cp_lexer_peek_token (parser
->lexer
));
43397 /* The clauses are in reverse order; fix that to make later diagnostic
43398 emission easier. */
43399 data
.clauses
= nreverse (data
.clauses
);
43401 if (decl
&& is_overloaded_fn (decl
)
43402 && (TREE_CODE (decl
) != FUNCTION_DECL
43403 || DECL_FUNCTION_TEMPLATE_P (decl
)))
43405 error_at (name_loc
,
43406 "%<#pragma acc routine%> names a set of overloads");
43407 parser
->oacc_routine
= NULL
;
43411 /* Perhaps we should use the same rule as declarations in different
43413 if (!DECL_NAMESPACE_SCOPE_P (decl
))
43415 error_at (name_loc
,
43416 "%qD does not refer to a namespace scope function", decl
);
43417 parser
->oacc_routine
= NULL
;
43421 if (TREE_CODE (decl
) != FUNCTION_DECL
)
43423 error_at (name_loc
, "%qD does not refer to a function", decl
);
43424 parser
->oacc_routine
= NULL
;
43428 cp_finalize_oacc_routine (parser
, decl
, false);
43429 parser
->oacc_routine
= NULL
;
43431 else /* No optional '( name )'. */
43433 /* Store away all pragma tokens. */
43434 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
43435 cp_lexer_consume_token (parser
->lexer
);
43436 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43437 struct cp_token_cache
*cp
43438 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
43439 parser
->oacc_routine
->tokens
.safe_push (cp
);
43441 /* Emit a helpful diagnostic if there's another pragma following this
43443 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
43445 cp_ensure_no_oacc_routine (parser
);
43446 data
.tokens
.release ();
43447 /* ..., and then just keep going. */
43451 /* We only have to consider the pragma_external case here. */
43452 cp_parser_declaration (parser
);
43453 if (parser
->oacc_routine
43454 && !parser
->oacc_routine
->fndecl_seen
)
43455 cp_ensure_no_oacc_routine (parser
);
43457 parser
->oacc_routine
= NULL
;
43458 data
.tokens
.release ();
43462 /* Finalize #pragma acc routine clauses after direct declarator has
43466 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
43468 struct cp_token_cache
*ce
;
43469 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
43471 if (!data
->error_seen
&& data
->fndecl_seen
)
43473 error_at (data
->loc
,
43474 "%<#pragma acc routine%> not immediately followed by "
43475 "a single function declaration or definition");
43476 data
->error_seen
= true;
43478 if (data
->error_seen
)
43481 gcc_checking_assert (data
->tokens
.length () == 1);
43482 ce
= data
->tokens
[0];
43484 cp_parser_push_lexer_for_tokens (parser
, ce
);
43485 parser
->lexer
->in_pragma
= true;
43486 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
43488 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
43489 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
43490 parser
->oacc_routine
->clauses
43491 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
43492 "#pragma acc routine", pragma_tok
);
43493 /* The clauses are in reverse order; fix that to make later diagnostic
43494 emission easier. */
43495 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
43496 cp_parser_pop_lexer (parser
);
43497 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
43503 /* Apply any saved OpenACC routine clauses to a just-parsed
43507 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
43509 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
43511 /* Keep going if we're in error reporting mode. */
43512 if (parser
->oacc_routine
->error_seen
43513 || fndecl
== error_mark_node
)
43516 if (parser
->oacc_routine
->fndecl_seen
)
43518 error_at (parser
->oacc_routine
->loc
,
43519 "%<#pragma acc routine%> not immediately followed by"
43520 " a single function declaration or definition");
43521 parser
->oacc_routine
= NULL
;
43524 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
43526 cp_ensure_no_oacc_routine (parser
);
43531 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
43532 parser
->oacc_routine
->loc
,
43533 "#pragma acc routine");
43534 if (compatible
< 0)
43536 parser
->oacc_routine
= NULL
;
43539 if (compatible
> 0)
43544 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
43546 error_at (parser
->oacc_routine
->loc
,
43548 ? G_("%<#pragma acc routine%> must be applied before"
43550 : G_("%<#pragma acc routine%> must be applied before"
43552 parser
->oacc_routine
= NULL
;
43556 /* Set the routine's level of parallelism. */
43557 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
43558 oacc_replace_fn_attrib (fndecl
, dims
);
43560 /* Add an "omp declare target" attribute. */
43561 DECL_ATTRIBUTES (fndecl
)
43562 = tree_cons (get_identifier ("omp declare target"),
43563 parser
->oacc_routine
->clauses
,
43564 DECL_ATTRIBUTES (fndecl
));
43567 /* Don't unset parser->oacc_routine here: we may still need it to
43568 diagnose wrong usage. But, remember that we've used this "#pragma acc
43570 parser
->oacc_routine
->fndecl_seen
= true;
43574 /* Main entry point to OpenMP statement pragmas. */
43577 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43580 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
43581 omp_clause_mask
mask (0);
43583 switch (cp_parser_pragma_kind (pragma_tok
))
43585 case PRAGMA_OACC_ATOMIC
:
43586 cp_parser_omp_atomic (parser
, pragma_tok
, true);
43588 case PRAGMA_OACC_CACHE
:
43589 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
43591 case PRAGMA_OACC_DATA
:
43592 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
43594 case PRAGMA_OACC_ENTER_DATA
:
43595 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
43597 case PRAGMA_OACC_EXIT_DATA
:
43598 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
43600 case PRAGMA_OACC_HOST_DATA
:
43601 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
43603 case PRAGMA_OACC_KERNELS
:
43604 case PRAGMA_OACC_PARALLEL
:
43605 case PRAGMA_OACC_SERIAL
:
43606 strcpy (p_name
, "#pragma acc");
43607 stmt
= cp_parser_oacc_compute (parser
, pragma_tok
, p_name
, if_p
);
43609 case PRAGMA_OACC_LOOP
:
43610 strcpy (p_name
, "#pragma acc");
43611 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
43614 case PRAGMA_OACC_UPDATE
:
43615 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
43617 case PRAGMA_OACC_WAIT
:
43618 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
43620 case PRAGMA_OMP_ATOMIC
:
43621 cp_parser_omp_atomic (parser
, pragma_tok
, false);
43623 case PRAGMA_OMP_CRITICAL
:
43624 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
43626 case PRAGMA_OMP_DISTRIBUTE
:
43627 strcpy (p_name
, "#pragma omp");
43628 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
43631 case PRAGMA_OMP_FOR
:
43632 strcpy (p_name
, "#pragma omp");
43633 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
43636 case PRAGMA_OMP_LOOP
:
43637 strcpy (p_name
, "#pragma omp");
43638 stmt
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
43641 case PRAGMA_OMP_MASTER
:
43642 strcpy (p_name
, "#pragma omp");
43643 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
43646 case PRAGMA_OMP_PARALLEL
:
43647 strcpy (p_name
, "#pragma omp");
43648 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
43651 case PRAGMA_OMP_SECTIONS
:
43652 strcpy (p_name
, "#pragma omp");
43653 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
43655 case PRAGMA_OMP_SIMD
:
43656 strcpy (p_name
, "#pragma omp");
43657 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
43660 case PRAGMA_OMP_SINGLE
:
43661 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
43663 case PRAGMA_OMP_TASK
:
43664 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
43666 case PRAGMA_OMP_TASKGROUP
:
43667 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
43669 case PRAGMA_OMP_TASKLOOP
:
43670 strcpy (p_name
, "#pragma omp");
43671 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
43674 case PRAGMA_OMP_TEAMS
:
43675 strcpy (p_name
, "#pragma omp");
43676 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
43680 gcc_unreachable ();
43683 protected_set_expr_location (stmt
, pragma_tok
->location
);
43686 /* Transactional Memory parsing routines. */
43688 /* Parse a transaction attribute.
43694 We use this instead of cp_parser_attributes_opt for transactions to avoid
43695 the pedwarn in C++98 mode. */
43698 cp_parser_txn_attribute_opt (cp_parser
*parser
)
43701 tree attr_name
, attr
= NULL
;
43703 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
43704 return cp_parser_attributes_opt (parser
);
43706 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
43708 cp_lexer_consume_token (parser
->lexer
);
43709 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
43712 token
= cp_lexer_peek_token (parser
->lexer
);
43713 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
43715 token
= cp_lexer_consume_token (parser
->lexer
);
43717 attr_name
= (token
->type
== CPP_KEYWORD
43718 /* For keywords, use the canonical spelling,
43719 not the parsed identifier. */
43720 ? ridpointers
[(int) token
->keyword
]
43722 attr
= build_tree_list (attr_name
, NULL_TREE
);
43725 cp_parser_error (parser
, "expected identifier");
43727 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
43729 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
43733 /* Parse a __transaction_atomic or __transaction_relaxed statement.
43735 transaction-statement:
43736 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
43738 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
43742 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
43744 unsigned char old_in
= parser
->in_transaction
;
43745 unsigned char this_in
= 1, new_in
;
43746 enum rid keyword
= token
->keyword
;
43747 tree stmt
, attrs
, noex
;
43749 cp_lexer_consume_token (parser
->lexer
);
43751 if (keyword
== RID_TRANSACTION_RELAXED
43752 || keyword
== RID_SYNCHRONIZED
)
43753 this_in
|= TM_STMT_ATTR_RELAXED
;
43756 attrs
= cp_parser_txn_attribute_opt (parser
);
43758 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
43761 /* Parse a noexcept specification. */
43762 if (keyword
== RID_ATOMIC_NOEXCEPT
)
43763 noex
= boolean_true_node
;
43764 else if (keyword
== RID_ATOMIC_CANCEL
)
43766 /* cancel-and-throw is unimplemented. */
43767 sorry ("%<atomic_cancel%>");
43771 noex
= cp_parser_noexcept_specification_opt (parser
,
43772 CP_PARSER_FLAGS_NONE
,
43773 /*require_constexpr=*/true,
43774 /*consumed_expr=*/NULL
,
43775 /*return_cond=*/true);
43777 /* Keep track if we're in the lexical scope of an outer transaction. */
43778 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
43780 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
43782 parser
->in_transaction
= new_in
;
43783 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
43784 parser
->in_transaction
= old_in
;
43786 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
43791 /* Parse a __transaction_atomic or __transaction_relaxed expression.
43793 transaction-expression:
43794 __transaction_atomic txn-noexcept-spec[opt] ( expression )
43795 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
43799 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
43801 unsigned char old_in
= parser
->in_transaction
;
43802 unsigned char this_in
= 1;
43806 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43808 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
43809 || keyword
== RID_TRANSACTION_RELAXED
);
43813 keyword
== RID_TRANSACTION_RELAXED
43814 ? G_("%<__transaction_relaxed%> without transactional memory "
43816 : G_("%<__transaction_atomic%> without transactional memory "
43817 "support enabled"));
43819 token
= cp_parser_require_keyword (parser
, keyword
,
43820 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
43821 : RT_TRANSACTION_RELAXED
));
43822 gcc_assert (token
!= NULL
);
43824 if (keyword
== RID_TRANSACTION_RELAXED
)
43825 this_in
|= TM_STMT_ATTR_RELAXED
;
43827 /* Set this early. This might mean that we allow transaction_cancel in
43828 an expression that we find out later actually has to be a constexpr.
43829 However, we expect that cxx_constant_value will be able to deal with
43830 this; also, if the noexcept has no constexpr, then what we parse next
43831 really is a transaction's body. */
43832 parser
->in_transaction
= this_in
;
43834 /* Parse a noexcept specification. */
43835 noex
= cp_parser_noexcept_specification_opt (parser
,
43836 CP_PARSER_FLAGS_NONE
,
43837 /*require_constexpr=*/false,
43839 /*return_cond=*/true);
43841 if (!noex
|| !noex_expr
43842 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
43844 matching_parens parens
;
43845 parens
.require_open (parser
);
43847 expr
= cp_parser_expression (parser
);
43848 expr
= finish_parenthesized_expr (expr
);
43850 parens
.require_close (parser
);
43854 /* The only expression that is available got parsed for the noexcept
43855 already. noexcept is true then. */
43857 noex
= boolean_true_node
;
43860 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
43861 parser
->in_transaction
= old_in
;
43863 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
43864 return error_mark_node
;
43866 return (flag_tm
? expr
: error_mark_node
);
43869 /* Parse a function-transaction-block.
43871 function-transaction-block:
43872 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
43874 __transaction_atomic txn-attribute[opt] function-try-block
43875 __transaction_relaxed ctor-initializer[opt] function-body
43876 __transaction_relaxed function-try-block
43880 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
43882 unsigned char old_in
= parser
->in_transaction
;
43883 unsigned char new_in
= 1;
43884 tree compound_stmt
, stmt
, attrs
;
43887 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
43888 || keyword
== RID_TRANSACTION_RELAXED
);
43889 token
= cp_parser_require_keyword (parser
, keyword
,
43890 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
43891 : RT_TRANSACTION_RELAXED
));
43892 gcc_assert (token
!= NULL
);
43894 if (keyword
== RID_TRANSACTION_RELAXED
)
43895 new_in
|= TM_STMT_ATTR_RELAXED
;
43898 attrs
= cp_parser_txn_attribute_opt (parser
);
43900 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
43903 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
43905 parser
->in_transaction
= new_in
;
43907 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
43908 cp_parser_function_try_block (parser
);
43910 cp_parser_ctor_initializer_opt_and_function_body
43911 (parser
, /*in_function_try_block=*/false);
43913 parser
->in_transaction
= old_in
;
43915 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
43918 /* Parse a __transaction_cancel statement.
43921 __transaction_cancel txn-attribute[opt] ;
43922 __transaction_cancel txn-attribute[opt] throw-expression ;
43924 ??? Cancel and throw is not yet implemented. */
43927 cp_parser_transaction_cancel (cp_parser
*parser
)
43930 bool is_outer
= false;
43933 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
43934 RT_TRANSACTION_CANCEL
);
43935 gcc_assert (token
!= NULL
);
43937 attrs
= cp_parser_txn_attribute_opt (parser
);
43939 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
43941 /* ??? Parse cancel-and-throw here. */
43943 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
43947 error_at (token
->location
, "%<__transaction_cancel%> without "
43948 "transactional memory support enabled");
43949 return error_mark_node
;
43951 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
43953 error_at (token
->location
, "%<__transaction_cancel%> within a "
43954 "%<__transaction_relaxed%>");
43955 return error_mark_node
;
43959 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
43960 && !is_tm_may_cancel_outer (current_function_decl
))
43962 error_at (token
->location
, "outer %<__transaction_cancel%> not "
43963 "within outer %<__transaction_atomic%>");
43964 error_at (token
->location
,
43965 " or a %<transaction_may_cancel_outer%> function");
43966 return error_mark_node
;
43969 else if (parser
->in_transaction
== 0)
43971 error_at (token
->location
, "%<__transaction_cancel%> not within "
43972 "%<__transaction_atomic%>");
43973 return error_mark_node
;
43976 stmt
= build_tm_abort_call (token
->location
, is_outer
);
43984 static GTY (()) cp_parser
*the_parser
;
43987 /* Special handling for the first token or line in the file. The first
43988 thing in the file might be #pragma GCC pch_preprocess, which loads a
43989 PCH file, which is a GC collection point. So we need to handle this
43990 first pragma without benefit of an existing lexer structure.
43992 Always returns one token to the caller in *FIRST_TOKEN. This is
43993 either the true first token of the file, or the first token after
43994 the initial pragma. */
43997 cp_parser_initial_pragma (cp_token
*first_token
)
43999 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
44002 cp_lexer_get_preprocessor_token (0, first_token
);
44005 if (first_token
->type
== CPP_STRING
)
44007 name
= first_token
->u
.value
;
44009 cp_lexer_get_preprocessor_token (0, first_token
);
44012 /* Skip to the end of the pragma. */
44013 if (first_token
->type
!= CPP_PRAGMA_EOL
)
44015 error_at (first_token
->location
,
44016 "malformed %<#pragma GCC pch_preprocess%>");
44018 cp_lexer_get_preprocessor_token (0, first_token
);
44019 while (first_token
->type
!= CPP_PRAGMA_EOL
);
44022 /* Now actually load the PCH file. */
44024 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
44026 /* Read one more token to return to our caller. We have to do this
44027 after reading the PCH file in, since its pointers have to be
44029 cp_lexer_get_preprocessor_token (0, first_token
);
44032 /* Parse a pragma GCC ivdep. */
44035 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
44037 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44041 /* Parse a pragma GCC unroll. */
44043 static unsigned short
44044 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
44046 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
44047 tree expr
= cp_parser_constant_expression (parser
);
44048 unsigned short unroll
;
44049 expr
= maybe_constant_value (expr
);
44050 HOST_WIDE_INT lunroll
= 0;
44051 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
44052 || TREE_CODE (expr
) != INTEGER_CST
44053 || (lunroll
= tree_to_shwi (expr
)) < 0
44054 || lunroll
>= USHRT_MAX
)
44056 error_at (location
, "%<#pragma GCC unroll%> requires an"
44057 " assignment-expression that evaluates to a non-negative"
44058 " integral constant less than %u", USHRT_MAX
);
44063 unroll
= (unsigned short)lunroll
;
44067 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44071 /* Normal parsing of a pragma token. Here we can (and must) use the
44075 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
44077 cp_token
*pragma_tok
;
44082 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
44083 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
44084 parser
->lexer
->in_pragma
= true;
44086 id
= cp_parser_pragma_kind (pragma_tok
);
44087 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
44088 cp_ensure_no_omp_declare_simd (parser
);
44091 case PRAGMA_GCC_PCH_PREPROCESS
:
44092 error_at (pragma_tok
->location
,
44093 "%<#pragma GCC pch_preprocess%> must be first");
44096 case PRAGMA_OMP_BARRIER
:
44099 case pragma_compound
:
44100 cp_parser_omp_barrier (parser
, pragma_tok
);
44103 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
44104 "used in compound statements", "omp barrier");
44111 case PRAGMA_OMP_DEPOBJ
:
44114 case pragma_compound
:
44115 cp_parser_omp_depobj (parser
, pragma_tok
);
44118 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
44119 "used in compound statements", "omp depobj");
44126 case PRAGMA_OMP_FLUSH
:
44129 case pragma_compound
:
44130 cp_parser_omp_flush (parser
, pragma_tok
);
44133 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
44134 "used in compound statements", "omp flush");
44141 case PRAGMA_OMP_TASKWAIT
:
44144 case pragma_compound
:
44145 cp_parser_omp_taskwait (parser
, pragma_tok
);
44148 error_at (pragma_tok
->location
,
44149 "%<#pragma %s%> may only be used in compound statements",
44157 case PRAGMA_OMP_TASKYIELD
:
44160 case pragma_compound
:
44161 cp_parser_omp_taskyield (parser
, pragma_tok
);
44164 error_at (pragma_tok
->location
,
44165 "%<#pragma %s%> may only be used in compound statements",
44173 case PRAGMA_OMP_CANCEL
:
44176 case pragma_compound
:
44177 cp_parser_omp_cancel (parser
, pragma_tok
);
44180 error_at (pragma_tok
->location
,
44181 "%<#pragma %s%> may only be used in compound statements",
44189 case PRAGMA_OMP_CANCELLATION_POINT
:
44190 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
44193 case PRAGMA_OMP_THREADPRIVATE
:
44194 cp_parser_omp_threadprivate (parser
, pragma_tok
);
44197 case PRAGMA_OMP_DECLARE
:
44198 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
44200 case PRAGMA_OACC_DECLARE
:
44201 cp_parser_oacc_declare (parser
, pragma_tok
);
44204 case PRAGMA_OACC_ENTER_DATA
:
44205 if (context
== pragma_stmt
)
44207 error_at (pragma_tok
->location
,
44208 "%<#pragma %s%> may only be used in compound statements",
44212 else if (context
!= pragma_compound
)
44214 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44217 case PRAGMA_OACC_EXIT_DATA
:
44218 if (context
== pragma_stmt
)
44220 error_at (pragma_tok
->location
,
44221 "%<#pragma %s%> may only be used in compound statements",
44225 else if (context
!= pragma_compound
)
44227 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44230 case PRAGMA_OACC_ROUTINE
:
44231 if (context
!= pragma_external
)
44233 error_at (pragma_tok
->location
,
44234 "%<#pragma acc routine%> must be at file scope");
44237 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
44240 case PRAGMA_OACC_UPDATE
:
44241 if (context
== pragma_stmt
)
44243 error_at (pragma_tok
->location
,
44244 "%<#pragma %s%> may only be used in compound statements",
44248 else if (context
!= pragma_compound
)
44250 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44253 case PRAGMA_OACC_WAIT
:
44254 if (context
== pragma_stmt
)
44256 error_at (pragma_tok
->location
,
44257 "%<#pragma %s%> may only be used in compound statements",
44261 else if (context
!= pragma_compound
)
44263 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44266 case PRAGMA_OACC_ATOMIC
:
44267 case PRAGMA_OACC_CACHE
:
44268 case PRAGMA_OACC_DATA
:
44269 case PRAGMA_OACC_HOST_DATA
:
44270 case PRAGMA_OACC_KERNELS
:
44271 case PRAGMA_OACC_LOOP
:
44272 case PRAGMA_OACC_PARALLEL
:
44273 case PRAGMA_OACC_SERIAL
:
44274 case PRAGMA_OMP_ATOMIC
:
44275 case PRAGMA_OMP_CRITICAL
:
44276 case PRAGMA_OMP_DISTRIBUTE
:
44277 case PRAGMA_OMP_FOR
:
44278 case PRAGMA_OMP_LOOP
:
44279 case PRAGMA_OMP_MASTER
:
44280 case PRAGMA_OMP_PARALLEL
:
44281 case PRAGMA_OMP_SECTIONS
:
44282 case PRAGMA_OMP_SIMD
:
44283 case PRAGMA_OMP_SINGLE
:
44284 case PRAGMA_OMP_TASK
:
44285 case PRAGMA_OMP_TASKGROUP
:
44286 case PRAGMA_OMP_TASKLOOP
:
44287 case PRAGMA_OMP_TEAMS
:
44288 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
44290 stmt
= push_omp_privatization_clauses (false);
44291 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44292 pop_omp_privatization_clauses (stmt
);
44295 case PRAGMA_OMP_REQUIRES
:
44296 if (context
!= pragma_external
)
44298 error_at (pragma_tok
->location
,
44299 "%<#pragma omp requires%> may only be used at file or "
44300 "namespace scope");
44303 return cp_parser_omp_requires (parser
, pragma_tok
);
44305 case PRAGMA_OMP_ORDERED
:
44306 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
44308 stmt
= push_omp_privatization_clauses (false);
44309 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
44310 pop_omp_privatization_clauses (stmt
);
44313 case PRAGMA_OMP_TARGET
:
44314 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
44316 stmt
= push_omp_privatization_clauses (false);
44317 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
44318 pop_omp_privatization_clauses (stmt
);
44321 case PRAGMA_OMP_END_DECLARE_TARGET
:
44322 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
44325 case PRAGMA_OMP_SCAN
:
44326 error_at (pragma_tok
->location
,
44327 "%<#pragma omp scan%> may only be used in "
44328 "a loop construct with %<inscan%> %<reduction%> clause");
44331 case PRAGMA_OMP_SECTION
:
44332 error_at (pragma_tok
->location
,
44333 "%<#pragma omp section%> may only be used in "
44334 "%<#pragma omp sections%> construct");
44339 if (context
== pragma_external
)
44341 error_at (pragma_tok
->location
,
44342 "%<#pragma GCC ivdep%> must be inside a function");
44345 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
44346 unsigned short unroll
;
44347 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
44348 if (tok
->type
== CPP_PRAGMA
44349 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
44351 tok
= cp_lexer_consume_token (parser
->lexer
);
44352 unroll
= cp_parser_pragma_unroll (parser
, tok
);
44353 tok
= cp_lexer_peek_token (the_parser
->lexer
);
44357 if (tok
->type
!= CPP_KEYWORD
44358 || (tok
->keyword
!= RID_FOR
44359 && tok
->keyword
!= RID_WHILE
44360 && tok
->keyword
!= RID_DO
))
44362 cp_parser_error (parser
, "for, while or do statement expected");
44365 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
44369 case PRAGMA_UNROLL
:
44371 if (context
== pragma_external
)
44373 error_at (pragma_tok
->location
,
44374 "%<#pragma GCC unroll%> must be inside a function");
44377 const unsigned short unroll
44378 = cp_parser_pragma_unroll (parser
, pragma_tok
);
44380 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
44381 if (tok
->type
== CPP_PRAGMA
44382 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
44384 tok
= cp_lexer_consume_token (parser
->lexer
);
44385 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
44386 tok
= cp_lexer_peek_token (the_parser
->lexer
);
44390 if (tok
->type
!= CPP_KEYWORD
44391 || (tok
->keyword
!= RID_FOR
44392 && tok
->keyword
!= RID_WHILE
44393 && tok
->keyword
!= RID_DO
))
44395 cp_parser_error (parser
, "for, while or do statement expected");
44398 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
44403 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
44404 c_invoke_pragma_handler (id
);
44408 cp_parser_error (parser
, "expected declaration specifiers");
44412 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44416 /* The interface the pragma parsers have to the lexer. */
44419 pragma_lex (tree
*value
, location_t
*loc
)
44421 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
44422 enum cpp_ttype ret
= tok
->type
;
44424 *value
= tok
->u
.value
;
44426 *loc
= tok
->location
;
44428 if (ret
== CPP_PRAGMA_EOL
)
44430 else if (ret
== CPP_STRING
)
44431 *value
= cp_parser_string_literal (the_parser
, false, false);
44434 if (ret
== CPP_KEYWORD
)
44436 cp_lexer_consume_token (the_parser
->lexer
);
44443 /* External interface. */
44445 /* Parse one entire translation unit. */
44448 c_parse_file (void)
44450 static bool already_called
= false;
44452 if (already_called
)
44453 fatal_error (input_location
,
44454 "multi-source compilation not implemented for C++");
44455 already_called
= true;
44457 /* cp_lexer_new_main is called before doing any GC allocation
44458 because tokenization might load a PCH file. */
44459 cp_lexer
*lexer
= cp_lexer_new_main ();
44461 the_parser
= cp_parser_new (lexer
);
44463 cp_parser_translation_unit (the_parser
);
44464 class_decl_loc_t::diag_mismatched_tags ();
44468 finish_translation_unit ();
44471 /* Create an identifier for a generic parameter type (a synthesized
44472 template parameter implied by `auto' or a concept identifier). */
44474 static GTY(()) int generic_parm_count
;
44476 make_generic_type_name ()
44479 sprintf (buf
, "auto:%d", ++generic_parm_count
);
44480 return get_identifier (buf
);
44483 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
44484 (creating a new template parameter list if necessary). Returns the newly
44485 created template type parm. */
44488 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
44490 /* A requires-clause is not a function and cannot have placeholders. */
44491 if (current_binding_level
->kind
== sk_block
)
44493 error ("placeholder type not allowed in this context");
44494 return error_mark_node
;
44497 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
44499 /* We are either continuing a function template that already contains implicit
44500 template parameters, creating a new fully-implicit function template, or
44501 extending an existing explicit function template with implicit template
44504 cp_binding_level
*const entry_scope
= current_binding_level
;
44506 bool become_template
= false;
44507 cp_binding_level
*parent_scope
= 0;
44509 if (parser
->implicit_template_scope
)
44511 gcc_assert (parser
->implicit_template_parms
);
44513 current_binding_level
= parser
->implicit_template_scope
;
44517 /* Roll back to the existing template parameter scope (in the case of
44518 extending an explicit function template) or introduce a new template
44519 parameter scope ahead of the function parameter scope (or class scope
44520 in the case of out-of-line member definitions). The function scope is
44521 added back after template parameter synthesis below. */
44523 cp_binding_level
*scope
= entry_scope
;
44525 while (scope
->kind
== sk_function_parms
)
44527 parent_scope
= scope
;
44528 scope
= scope
->level_chain
;
44530 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
44532 /* If not defining a class, then any class scope is a scope level in
44533 an out-of-line member definition. In this case simply wind back
44534 beyond the first such scope to inject the template parameter list.
44535 Otherwise wind back to the class being defined. The latter can
44536 occur in class member friend declarations such as:
44542 friend void A::foo (auto);
44545 The template parameter list synthesized for the friend declaration
44546 must be injected in the scope of 'B'. This can also occur in
44547 erroneous cases such as:
44553 void B::foo (auto) {}
44556 Here the attempted definition of 'B::foo' within 'A' is ill-formed
44557 but, nevertheless, the template parameter list synthesized for the
44558 declarator should be injected into the scope of 'A' as if the
44559 ill-formed template was specified explicitly. */
44561 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
44563 parent_scope
= scope
;
44564 scope
= scope
->level_chain
;
44568 current_binding_level
= scope
;
44570 if (scope
->kind
!= sk_template_parms
44571 || !function_being_declared_is_template_p (parser
))
44573 /* Introduce a new template parameter list for implicit template
44576 become_template
= true;
44578 parser
->implicit_template_scope
44579 = begin_scope (sk_template_parms
, NULL
);
44581 ++processing_template_decl
;
44583 parser
->fully_implicit_function_template_p
= true;
44584 ++parser
->num_template_parameter_lists
;
44588 /* Synthesize implicit template parameters at the end of the explicit
44589 template parameter list. */
44591 gcc_assert (current_template_parms
);
44593 parser
->implicit_template_scope
= scope
;
44595 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
44596 parser
->implicit_template_parms
44597 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
44601 /* Synthesize a new template parameter and track the current template
44602 parameter chain with implicit_template_parms. */
44604 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
44605 tree synth_id
= make_generic_type_name ();
44606 tree synth_tmpl_parm
;
44607 bool non_type
= false;
44609 /* Synthesize the type template parameter. */
44610 gcc_assert(!proto
|| TREE_CODE (proto
) == TYPE_DECL
);
44611 synth_tmpl_parm
= finish_template_type_parm (class_type_node
, synth_id
);
44613 /* Attach the constraint to the parm before processing. */
44614 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
44615 TREE_TYPE (node
) = constr
;
44617 = process_template_parm (parser
->implicit_template_parms
,
44620 /*non_type=*/non_type
,
44621 /*param_pack=*/false);
44623 /* Mark the synthetic declaration "virtual". This is used when
44624 comparing template-heads to determine if whether an abbreviated
44625 function template is equivalent to an explicit template.
44627 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
44628 DECL_VIRTUAL_P (TREE_VALUE (new_parm
)) = true;
44630 // Chain the new parameter to the list of implicit parameters.
44631 if (parser
->implicit_template_parms
)
44632 parser
->implicit_template_parms
44633 = TREE_CHAIN (parser
->implicit_template_parms
);
44635 parser
->implicit_template_parms
= new_parm
;
44637 tree new_decl
= get_local_decls ();
44639 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
44640 new_decl
= DECL_INITIAL (new_decl
);
44642 /* If creating a fully implicit function template, start the new implicit
44643 template parameter list with this synthesized type, otherwise grow the
44644 current template parameter list. */
44646 if (become_template
)
44648 parent_scope
->level_chain
= current_binding_level
;
44650 tree new_parms
= make_tree_vec (1);
44651 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
44652 current_template_parms
= tree_cons (size_int (processing_template_decl
),
44653 new_parms
, current_template_parms
);
44657 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
44658 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
44659 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
44660 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
44663 /* If the new parameter was constrained, we need to add that to the
44664 constraints in the template parameter list. */
44665 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
44667 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
44668 reqs
= combine_constraint_expressions (reqs
, req
);
44669 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
44672 current_binding_level
= entry_scope
;
44677 /* Finish the declaration of a fully implicit function template. Such a
44678 template has no explicit template parameter list so has not been through the
44679 normal template head and tail processing. synthesize_implicit_template_parm
44680 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
44681 provided if the declaration is a class member such that its template
44682 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
44683 form is returned. Otherwise NULL_TREE is returned. */
44686 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
44688 gcc_assert (parser
->fully_implicit_function_template_p
);
44690 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
44691 && DECL_VIRTUAL_P (member_decl_opt
))
44693 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
44694 "implicit templates may not be %<virtual%>");
44695 DECL_VIRTUAL_P (member_decl_opt
) = false;
44698 if (member_decl_opt
)
44699 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
44700 end_template_decl ();
44702 parser
->fully_implicit_function_template_p
= false;
44703 parser
->implicit_template_parms
= 0;
44704 parser
->implicit_template_scope
= 0;
44705 --parser
->num_template_parameter_lists
;
44707 return member_decl_opt
;
44710 /* Like finish_fully_implicit_template, but to be used in error
44711 recovery, rearranging scopes so that we restore the state we had
44712 before synthesize_implicit_template_parm inserted the implement
44713 template parms scope. */
44716 abort_fully_implicit_template (cp_parser
*parser
)
44718 cp_binding_level
*return_to_scope
= current_binding_level
;
44720 if (parser
->implicit_template_scope
44721 && return_to_scope
!= parser
->implicit_template_scope
)
44723 cp_binding_level
*child
= return_to_scope
;
44724 for (cp_binding_level
*scope
= child
->level_chain
;
44725 scope
!= parser
->implicit_template_scope
;
44726 scope
= child
->level_chain
)
44728 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
44729 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
44730 current_binding_level
= parser
->implicit_template_scope
;
44733 return_to_scope
= return_to_scope
->level_chain
;
44735 finish_fully_implicit_template (parser
, NULL
);
44737 gcc_assert (current_binding_level
== return_to_scope
);
44740 /* Helper function for diagnostics that have complained about things
44741 being used with 'extern "C"' linkage.
44743 Attempt to issue a note showing where the 'extern "C"' linkage began. */
44746 maybe_show_extern_c_location (void)
44748 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
44749 inform (the_parser
->innermost_linkage_specification_location
,
44750 "%<extern \"C\"%> linkage started here");
44753 #include "gt-cp-parser.h"