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 (cp_lexer
*, 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)
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
623 /* Initially we are not debugging. */
624 lexer
->debugging_p
= false;
626 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
628 /* Create the buffer. */
629 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
639 cp_lexer_new_main (void)
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token
);
649 lexer
= cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer
->buffer
->quick_push (token
);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token
.type
!= CPP_EOF
)
657 cp_lexer_get_preprocessor_token (lexer
, &token
);
658 vec_safe_push (lexer
->buffer
, token
);
661 lexer
->next_token
= lexer
->buffer
->address ();
662 lexer
->last_token
= lexer
->next_token
663 + lexer
->buffer
->length ()
666 /* Subsequent preprocessor diagnostics should use compiler
667 diagnostic functions to get the compiler source location. */
670 gcc_assert (!lexer
->next_token
->purged_p
);
674 /* Create a new lexer whose token stream is primed with the tokens in
675 CACHE. When these tokens are exhausted, no new tokens will be read. */
678 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
680 cp_token
*first
= cache
->first
;
681 cp_token
*last
= cache
->last
;
682 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
684 /* We do not own the buffer. */
685 lexer
->buffer
= NULL
;
687 /* Insert an EOF token. */
688 lexer
->saved_type
= last
->type
;
689 lexer
->saved_keyword
= last
->keyword
;
690 last
->type
= CPP_EOF
;
691 last
->keyword
= RID_MAX
;
693 lexer
->next_token
= first
;
694 lexer
->last_token
= last
;
696 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
698 /* Initially we are not debugging. */
699 lexer
->debugging_p
= false;
701 gcc_assert (!lexer
->next_token
->purged_p
);
705 /* Frees all resources associated with LEXER. */
708 cp_lexer_destroy (cp_lexer
*lexer
)
711 vec_free (lexer
->buffer
);
714 /* Restore the token we overwrite with EOF. */
715 lexer
->last_token
->type
= lexer
->saved_type
;
716 lexer
->last_token
->keyword
= lexer
->saved_keyword
;
718 lexer
->saved_tokens
.release ();
722 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
723 be used. The point of this flag is to help the compiler to fold away calls
724 to cp_lexer_debugging_p within this source file at compile time, when the
725 lexer is not being debugged. */
727 #define LEXER_DEBUGGING_ENABLED_P false
729 /* Returns nonzero if debugging information should be output. */
732 cp_lexer_debugging_p (cp_lexer
*lexer
)
734 if (!LEXER_DEBUGGING_ENABLED_P
)
737 return lexer
->debugging_p
;
741 static inline cp_token_position
742 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
744 return lexer
->next_token
- previous_p
;
747 static inline cp_token
*
748 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
754 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
756 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
759 static inline cp_token_position
760 cp_lexer_previous_token_position (cp_lexer
*lexer
)
762 return cp_lexer_token_position (lexer
, true);
765 static inline cp_token
*
766 cp_lexer_previous_token (cp_lexer
*lexer
)
768 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
770 /* Skip past purged tokens. */
773 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
777 return cp_lexer_token_at (lexer
, tp
);
780 /* Same as above, but return NULL when the lexer doesn't own the token
781 buffer or if the next_token is at the start of the token
785 cp_lexer_safe_previous_token (cp_lexer
*lexer
)
788 if (lexer
->next_token
!= lexer
->buffer
->address ())
789 return cp_lexer_previous_token (lexer
);
794 /* Overload for make_location, taking the lexer to mean the location of the
797 static inline location_t
798 make_location (location_t caret
, location_t start
, cp_lexer
*lexer
)
800 cp_token
*t
= cp_lexer_previous_token (lexer
);
801 return make_location (caret
, start
, t
->location
);
804 /* nonzero if we are presently saving tokens. */
807 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
809 return lexer
->saved_tokens
.length () != 0;
812 /* Store the next token from the preprocessor in *TOKEN. Return true
813 if we reach EOF. If LEXER is NULL, assume we are handling an
814 initial #pragma pch_preprocess, and thus want the lexer to return
815 processed strings. */
818 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
820 static int is_extern_c
= 0;
822 /* Get a new token from the preprocessor. */
824 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
825 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
826 token
->keyword
= RID_MAX
;
827 token
->purged_p
= false;
828 token
->error_reported
= false;
829 token
->tree_check_p
= false;
831 /* On some systems, some header files are surrounded by an
832 implicit extern "C" block. Set a flag in the token if it
833 comes from such a header. */
834 is_extern_c
+= pending_lang_change
;
835 pending_lang_change
= 0;
836 token
->implicit_extern_c
= is_extern_c
> 0;
838 /* Check to see if this token is a keyword. */
839 if (token
->type
== CPP_NAME
)
841 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
843 /* Mark this token as a keyword. */
844 token
->type
= CPP_KEYWORD
;
845 /* Record which keyword. */
846 token
->keyword
= C_RID_CODE (token
->u
.value
);
850 if (warn_cxx11_compat
851 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
852 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
854 /* Warn about the C++0x keyword (but still treat it as
856 warning_at (token
->location
, OPT_Wc__11_compat
,
857 "identifier %qE is a keyword in C++11",
860 /* Clear out the C_RID_CODE so we don't warn about this
861 particular identifier-turned-keyword again. */
862 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
864 if (warn_cxx20_compat
865 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX20
866 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX20
)
868 /* Warn about the C++20 keyword (but still treat it as
870 warning_at (token
->location
, OPT_Wc__20_compat
,
871 "identifier %qE is a keyword in C++20",
874 /* Clear out the C_RID_CODE so we don't warn about this
875 particular identifier-turned-keyword again. */
876 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
879 token
->keyword
= RID_MAX
;
882 else if (token
->type
== CPP_AT_NAME
)
884 /* This only happens in Objective-C++; it must be a keyword. */
885 token
->type
= CPP_KEYWORD
;
886 switch (C_RID_CODE (token
->u
.value
))
888 /* Replace 'class' with '@class', 'private' with '@private',
889 etc. This prevents confusion with the C++ keyword
890 'class', and makes the tokens consistent with other
891 Objective-C 'AT' keywords. For example '@class' is
892 reported as RID_AT_CLASS which is consistent with
893 '@synchronized', which is reported as
896 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
897 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
898 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
899 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
900 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
901 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
902 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
903 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
904 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
909 /* Update the globals input_location and the input file stack from TOKEN. */
911 cp_lexer_set_source_position_from_token (cp_token
*token
)
913 input_location
= token
->location
;
916 /* Update the globals input_location and the input file stack from LEXER. */
918 cp_lexer_set_source_position (cp_lexer
*lexer
)
920 cp_token
*token
= cp_lexer_peek_token (lexer
);
921 cp_lexer_set_source_position_from_token (token
);
924 /* Return a pointer to the next token in the token stream, but do not
927 static inline cp_token
*
928 cp_lexer_peek_token (cp_lexer
*lexer
)
930 if (cp_lexer_debugging_p (lexer
))
932 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
933 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
934 putc ('\n', cp_lexer_debug_stream
);
936 return lexer
->next_token
;
939 /* Return true if the next token has the indicated TYPE. */
942 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
944 return cp_lexer_peek_token (lexer
)->type
== type
;
947 /* Return true if the next token does not have the indicated TYPE. */
950 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
952 return !cp_lexer_next_token_is (lexer
, type
);
955 /* Return true if the next token is the indicated KEYWORD. */
958 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
960 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
964 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
966 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
970 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
972 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
975 /* Return true if KEYWORD can start a decl-specifier. */
978 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
982 /* auto specifier: storage-class-specifier in C++,
983 simple-type-specifier in C++0x. */
985 /* Storage classes. */
991 /* Elaborated type specifiers. */
997 /* Simple type specifiers. */
1012 /* GNU extensions. */
1015 /* C++11 extensions. */
1017 case RID_UNDERLYING_TYPE
:
1019 /* C++20 extensions. */
1025 if (keyword
>= RID_FIRST_INT_N
1026 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
1027 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
1033 /* Return true if the next token is a keyword for a decl-specifier. */
1036 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1040 token
= cp_lexer_peek_token (lexer
);
1041 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1044 /* Returns TRUE iff the token T begins a decltype type. */
1047 token_is_decltype (cp_token
*t
)
1049 return (t
->keyword
== RID_DECLTYPE
1050 || t
->type
== CPP_DECLTYPE
);
1053 /* Returns TRUE iff the next token begins a decltype type. */
1056 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1058 cp_token
*t
= cp_lexer_peek_token (lexer
);
1059 return token_is_decltype (t
);
1062 /* Called when processing a token with tree_check_value; perform or defer the
1063 associated checks and return the value. */
1066 saved_checks_value (struct tree_check
*check_value
)
1068 /* Perform any access checks that were deferred. */
1069 vec
<deferred_access_check
, va_gc
> *checks
;
1070 deferred_access_check
*chk
;
1071 checks
= check_value
->checks
;
1075 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1076 perform_or_defer_access_check (chk
->binfo
,
1078 chk
->diag_decl
, tf_warning_or_error
);
1080 /* Return the stored value. */
1081 return check_value
->value
;
1084 /* Return a pointer to the Nth token in the token stream. If N is 1,
1085 then this is precisely equivalent to cp_lexer_peek_token (except
1086 that it is not inline). One would like to disallow that case, but
1087 there is one case (cp_parser_nth_token_starts_template_id) where
1088 the caller passes a variable for N and it might be 1. */
1091 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1095 /* N is 1-based, not zero-based. */
1098 if (cp_lexer_debugging_p (lexer
))
1099 fprintf (cp_lexer_debug_stream
,
1100 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1103 token
= lexer
->next_token
;
1104 while (n
&& token
->type
!= CPP_EOF
)
1107 if (!token
->purged_p
)
1111 if (cp_lexer_debugging_p (lexer
))
1113 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1114 putc ('\n', cp_lexer_debug_stream
);
1120 /* Return the next token, and advance the lexer's next_token pointer
1121 to point to the next non-purged token. */
1124 cp_lexer_consume_token (cp_lexer
* lexer
)
1126 cp_token
*token
= lexer
->next_token
;
1128 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1132 gcc_assert (token
->type
!= CPP_EOF
);
1133 lexer
->next_token
++;
1135 while (lexer
->next_token
->purged_p
);
1137 cp_lexer_set_source_position_from_token (token
);
1139 /* Provide debugging output. */
1140 if (cp_lexer_debugging_p (lexer
))
1142 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1143 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1144 putc ('\n', cp_lexer_debug_stream
);
1150 /* Permanently remove the next token from the token stream, and
1151 advance the next_token pointer to refer to the next non-purged
1155 cp_lexer_purge_token (cp_lexer
*lexer
)
1157 cp_token
*tok
= lexer
->next_token
;
1159 gcc_assert (tok
->type
!= CPP_EOF
);
1160 tok
->purged_p
= true;
1161 tok
->location
= UNKNOWN_LOCATION
;
1162 tok
->u
.value
= NULL_TREE
;
1163 tok
->keyword
= RID_MAX
;
1167 while (tok
->purged_p
);
1168 lexer
->next_token
= tok
;
1171 /* Permanently remove all tokens after TOK, up to, but not
1172 including, the token that will be returned next by
1173 cp_lexer_peek_token. */
1176 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1178 cp_token
*peek
= lexer
->next_token
;
1180 gcc_assert (tok
< peek
);
1182 for (tok
++; tok
!= peek
; tok
++)
1184 tok
->purged_p
= true;
1185 tok
->location
= UNKNOWN_LOCATION
;
1186 tok
->u
.value
= NULL_TREE
;
1187 tok
->keyword
= RID_MAX
;
1191 /* Begin saving tokens. All tokens consumed after this point will be
1195 cp_lexer_save_tokens (cp_lexer
* lexer
)
1197 /* Provide debugging output. */
1198 if (cp_lexer_debugging_p (lexer
))
1199 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1201 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1204 /* Commit to the portion of the token stream most recently saved. */
1207 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1209 /* Provide debugging output. */
1210 if (cp_lexer_debugging_p (lexer
))
1211 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1213 lexer
->saved_tokens
.pop ();
1216 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1217 to the token stream. Stop saving tokens. */
1220 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1222 /* Provide debugging output. */
1223 if (cp_lexer_debugging_p (lexer
))
1224 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1226 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1229 /* RAII wrapper around the above functions, with sanity checking. Creating
1230 a variable saves tokens, which are committed when the variable is
1231 destroyed unless they are explicitly rolled back by calling the rollback
1234 struct saved_token_sentinel
1239 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1241 len
= lexer
->saved_tokens
.length ();
1242 cp_lexer_save_tokens (lexer
);
1246 cp_lexer_rollback_tokens (lexer
);
1249 ~saved_token_sentinel()
1252 cp_lexer_commit_tokens (lexer
);
1253 gcc_assert (lexer
->saved_tokens
.length () == len
);
1257 /* Print a representation of the TOKEN on the STREAM. */
1260 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1262 /* We don't use cpp_type2name here because the parser defines
1263 a few tokens of its own. */
1264 static const char *const token_names
[] = {
1265 /* cpplib-defined token types */
1266 #define OP(e, s) #e,
1267 #define TK(e, s) #e,
1271 /* C++ parser token types - see "Manifest constants", above. */
1274 "NESTED_NAME_SPECIFIER",
1277 /* For some tokens, print the associated data. */
1278 switch (token
->type
)
1281 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1282 For example, `struct' is mapped to an INTEGER_CST. */
1283 if (!identifier_p (token
->u
.value
))
1287 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1294 case CPP_UTF8STRING
:
1295 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1299 print_generic_expr (stream
, token
->u
.value
);
1303 /* If we have a name for the token, print it out. Otherwise, we
1304 simply give the numeric code. */
1305 if (token
->type
< ARRAY_SIZE(token_names
))
1306 fputs (token_names
[token
->type
], stream
);
1308 fprintf (stream
, "[%d]", token
->type
);
1314 debug (cp_token
&ref
)
1316 cp_lexer_print_token (stderr
, &ref
);
1317 fprintf (stderr
, "\n");
1321 debug (cp_token
*ptr
)
1326 fprintf (stderr
, "<nil>\n");
1330 /* Start emitting debugging information. */
1333 cp_lexer_start_debugging (cp_lexer
* lexer
)
1335 if (!LEXER_DEBUGGING_ENABLED_P
)
1336 fatal_error (input_location
,
1337 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1339 lexer
->debugging_p
= true;
1340 cp_lexer_debug_stream
= stderr
;
1343 /* Stop emitting debugging information. */
1346 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1348 if (!LEXER_DEBUGGING_ENABLED_P
)
1349 fatal_error (input_location
,
1350 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1352 lexer
->debugging_p
= false;
1353 cp_lexer_debug_stream
= NULL
;
1356 /* Create a new cp_token_cache, representing a range of tokens. */
1358 static cp_token_cache
*
1359 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1361 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1362 cache
->first
= first
;
1367 /* Diagnose if #pragma omp declare simd isn't followed immediately
1368 by function declaration or definition. */
1371 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1373 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1375 error ("%<#pragma omp declare %s%> not immediately followed by "
1376 "function declaration or definition",
1377 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1378 parser
->omp_declare_simd
= NULL
;
1382 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1383 and put that into "omp declare simd" attribute. */
1386 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1388 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1390 if (fndecl
== error_mark_node
)
1392 parser
->omp_declare_simd
= NULL
;
1395 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1397 cp_ensure_no_omp_declare_simd (parser
);
1403 /* Diagnose if #pragma acc routine isn't followed immediately by function
1404 declaration or definition. */
1407 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1409 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1411 error_at (parser
->oacc_routine
->loc
,
1412 "%<#pragma acc routine%> not immediately followed by "
1413 "function declaration or definition");
1414 parser
->oacc_routine
= NULL
;
1418 /* Decl-specifiers. */
1420 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1423 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1425 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1430 /* Nothing other than the parser should be creating declarators;
1431 declarators are a semi-syntactic representation of C++ entities.
1432 Other parts of the front end that need to create entities (like
1433 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1435 static cp_declarator
*make_call_declarator
1436 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1437 static cp_declarator
*make_array_declarator
1438 (cp_declarator
*, tree
);
1439 static cp_declarator
*make_pointer_declarator
1440 (cp_cv_quals
, cp_declarator
*, tree
);
1441 static cp_declarator
*make_reference_declarator
1442 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1443 static cp_declarator
*make_ptrmem_declarator
1444 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1446 /* An erroneous declarator. */
1447 static cp_declarator
*cp_error_declarator
;
1449 /* The obstack on which declarators and related data structures are
1451 static struct obstack declarator_obstack
;
1453 /* Alloc BYTES from the declarator memory pool. */
1455 static inline void *
1456 alloc_declarator (size_t bytes
)
1458 return obstack_alloc (&declarator_obstack
, bytes
);
1461 /* Allocate a declarator of the indicated KIND. Clear fields that are
1462 common to all declarators. */
1464 static cp_declarator
*
1465 make_declarator (cp_declarator_kind kind
)
1467 cp_declarator
*declarator
;
1469 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1470 declarator
->kind
= kind
;
1471 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1472 declarator
->attributes
= NULL_TREE
;
1473 declarator
->std_attributes
= NULL_TREE
;
1474 declarator
->declarator
= NULL
;
1475 declarator
->parameter_pack_p
= false;
1476 declarator
->id_loc
= UNKNOWN_LOCATION
;
1481 /* Make a declarator for a generalized identifier. If
1482 QUALIFYING_SCOPE is non-NULL, the identifier is
1483 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1484 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1487 static cp_declarator
*
1488 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1489 special_function_kind sfk
, location_t id_location
)
1491 cp_declarator
*declarator
;
1493 /* It is valid to write:
1495 class C { void f(); };
1499 The standard is not clear about whether `typedef const C D' is
1500 legal; as of 2002-09-15 the committee is considering that
1501 question. EDG 3.0 allows that syntax. Therefore, we do as
1503 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1504 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1506 gcc_assert (identifier_p (unqualified_name
)
1507 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1508 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1510 declarator
= make_declarator (cdk_id
);
1511 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1512 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1513 declarator
->u
.id
.sfk
= sfk
;
1514 declarator
->id_loc
= id_location
;
1519 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1520 of modifiers such as const or volatile to apply to the pointer
1521 type, represented as identifiers. ATTRIBUTES represent the attributes that
1522 appertain to the pointer or reference. */
1525 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1528 cp_declarator
*declarator
;
1530 declarator
= make_declarator (cdk_pointer
);
1531 declarator
->declarator
= target
;
1532 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1533 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1536 declarator
->id_loc
= target
->id_loc
;
1537 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1538 target
->parameter_pack_p
= false;
1541 declarator
->parameter_pack_p
= false;
1543 declarator
->std_attributes
= attributes
;
1548 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1549 represent the attributes that appertain to the pointer or
1553 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1554 bool rvalue_ref
, tree attributes
)
1556 cp_declarator
*declarator
;
1558 declarator
= make_declarator (cdk_reference
);
1559 declarator
->declarator
= target
;
1560 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1561 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1564 declarator
->id_loc
= target
->id_loc
;
1565 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1566 target
->parameter_pack_p
= false;
1569 declarator
->parameter_pack_p
= false;
1571 declarator
->std_attributes
= attributes
;
1576 /* Like make_pointer_declarator -- but for a pointer to a non-static
1577 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1578 appertain to the pointer or reference. */
1581 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1582 cp_declarator
*pointee
,
1585 cp_declarator
*declarator
;
1587 declarator
= make_declarator (cdk_ptrmem
);
1588 declarator
->declarator
= pointee
;
1589 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1590 declarator
->u
.pointer
.class_type
= class_type
;
1594 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1595 pointee
->parameter_pack_p
= false;
1598 declarator
->parameter_pack_p
= false;
1600 declarator
->std_attributes
= attributes
;
1605 /* Make a declarator for the function given by TARGET, with the
1606 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1607 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1608 indicates what exceptions can be thrown. */
1611 make_call_declarator (cp_declarator
*target
,
1613 cp_cv_quals cv_qualifiers
,
1614 cp_virt_specifiers virt_specifiers
,
1615 cp_ref_qualifier ref_qualifier
,
1617 tree exception_specification
,
1618 tree late_return_type
,
1619 tree requires_clause
)
1621 cp_declarator
*declarator
;
1623 declarator
= make_declarator (cdk_function
);
1624 declarator
->declarator
= target
;
1625 declarator
->u
.function
.parameters
= parms
;
1626 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1627 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1628 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1629 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1630 declarator
->u
.function
.exception_specification
= exception_specification
;
1631 declarator
->u
.function
.late_return_type
= late_return_type
;
1632 declarator
->u
.function
.requires_clause
= requires_clause
;
1635 declarator
->id_loc
= target
->id_loc
;
1636 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1637 target
->parameter_pack_p
= false;
1640 declarator
->parameter_pack_p
= false;
1645 /* Make a declarator for an array of BOUNDS elements, each of which is
1646 defined by ELEMENT. */
1649 make_array_declarator (cp_declarator
*element
, tree bounds
)
1651 cp_declarator
*declarator
;
1653 declarator
= make_declarator (cdk_array
);
1654 declarator
->declarator
= element
;
1655 declarator
->u
.array
.bounds
= bounds
;
1658 declarator
->id_loc
= element
->id_loc
;
1659 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1660 element
->parameter_pack_p
= false;
1663 declarator
->parameter_pack_p
= false;
1668 /* Determine whether the declarator we've seen so far can be a
1669 parameter pack, when followed by an ellipsis. */
1671 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1673 if (declarator
&& declarator
->parameter_pack_p
)
1674 /* We already saw an ellipsis. */
1677 /* Search for a declarator name, or any other declarator that goes
1678 after the point where the ellipsis could appear in a parameter
1679 pack. If we find any of these, then this declarator cannot be
1680 made into a parameter pack. */
1682 while (declarator
&& !found
)
1684 switch ((int)declarator
->kind
)
1696 declarator
= declarator
->declarator
;
1704 cp_parameter_declarator
*no_parameters
;
1706 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1707 DECLARATOR and DEFAULT_ARGUMENT. */
1709 cp_parameter_declarator
*
1710 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1711 cp_declarator
*declarator
,
1712 tree default_argument
,
1714 bool template_parameter_pack_p
= false)
1716 cp_parameter_declarator
*parameter
;
1718 parameter
= ((cp_parameter_declarator
*)
1719 alloc_declarator (sizeof (cp_parameter_declarator
)));
1720 parameter
->next
= NULL
;
1721 if (decl_specifiers
)
1722 parameter
->decl_specifiers
= *decl_specifiers
;
1724 clear_decl_specs (¶meter
->decl_specifiers
);
1725 parameter
->declarator
= declarator
;
1726 parameter
->default_argument
= default_argument
;
1727 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1728 parameter
->loc
= loc
;
1733 /* Returns true iff DECLARATOR is a declaration for a function. */
1736 function_declarator_p (const cp_declarator
*declarator
)
1740 if (declarator
->kind
== cdk_function
1741 && declarator
->declarator
->kind
== cdk_id
)
1743 if (declarator
->kind
== cdk_id
1744 || declarator
->kind
== cdk_decomp
1745 || declarator
->kind
== cdk_error
)
1747 declarator
= declarator
->declarator
;
1757 A cp_parser parses the token stream as specified by the C++
1758 grammar. Its job is purely parsing, not semantic analysis. For
1759 example, the parser breaks the token stream into declarators,
1760 expressions, statements, and other similar syntactic constructs.
1761 It does not check that the types of the expressions on either side
1762 of an assignment-statement are compatible, or that a function is
1763 not declared with a parameter of type `void'.
1765 The parser invokes routines elsewhere in the compiler to perform
1766 semantic analysis and to build up the abstract syntax tree for the
1769 The parser (and the template instantiation code, which is, in a
1770 way, a close relative of parsing) are the only parts of the
1771 compiler that should be calling push_scope and pop_scope, or
1772 related functions. The parser (and template instantiation code)
1773 keeps track of what scope is presently active; everything else
1774 should simply honor that. (The code that generates static
1775 initializers may also need to set the scope, in order to check
1776 access control correctly when emitting the initializers.)
1781 The parser is of the standard recursive-descent variety. Upcoming
1782 tokens in the token stream are examined in order to determine which
1783 production to use when parsing a non-terminal. Some C++ constructs
1784 require arbitrary look ahead to disambiguate. For example, it is
1785 impossible, in the general case, to tell whether a statement is an
1786 expression or declaration without scanning the entire statement.
1787 Therefore, the parser is capable of "parsing tentatively." When the
1788 parser is not sure what construct comes next, it enters this mode.
1789 Then, while we attempt to parse the construct, the parser queues up
1790 error messages, rather than issuing them immediately, and saves the
1791 tokens it consumes. If the construct is parsed successfully, the
1792 parser "commits", i.e., it issues any queued error messages and
1793 the tokens that were being preserved are permanently discarded.
1794 If, however, the construct is not parsed successfully, the parser
1795 rolls back its state completely so that it can resume parsing using
1796 a different alternative.
1801 The performance of the parser could probably be improved substantially.
1802 We could often eliminate the need to parse tentatively by looking ahead
1803 a little bit. In some places, this approach might not entirely eliminate
1804 the need to parse tentatively, but it might still speed up the average
1807 /* Flags that are passed to some parsing functions. These values can
1808 be bitwise-ored together. */
1813 CP_PARSER_FLAGS_NONE
= 0x0,
1814 /* The construct is optional. If it is not present, then no error
1815 should be issued. */
1816 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1817 /* When parsing a type-specifier, treat user-defined type-names
1818 as non-type identifiers. */
1819 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1820 /* When parsing a type-specifier, do not try to parse a class-specifier
1821 or enum-specifier. */
1822 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1823 /* When parsing a decl-specifier-seq, only allow type-specifier or
1825 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1826 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1827 for C++20 consteval. */
1828 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1829 /* When parsing a decl-specifier-seq, allow missing typename. */
1830 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20,
1831 /* When parsing of the noexcept-specifier should be delayed. */
1832 CP_PARSER_FLAGS_DELAY_NOEXCEPT
= 0x40,
1833 /* When parsing a consteval declarator. */
1834 CP_PARSER_FLAGS_CONSTEVAL
= 0x80
1837 /* This type is used for parameters and variables which hold
1838 combinations of the above flags. */
1839 typedef int cp_parser_flags
;
1841 /* The different kinds of declarators we want to parse. */
1843 enum cp_parser_declarator_kind
1845 /* We want an abstract declarator. */
1846 CP_PARSER_DECLARATOR_ABSTRACT
,
1847 /* We want a named declarator. */
1848 CP_PARSER_DECLARATOR_NAMED
,
1849 /* We don't mind, but the name must be an unqualified-id. */
1850 CP_PARSER_DECLARATOR_EITHER
1853 /* The precedence values used to parse binary expressions. The minimum value
1854 of PREC must be 1, because zero is reserved to quickly discriminate
1855 binary operators from other tokens. */
1860 PREC_LOGICAL_OR_EXPRESSION
,
1861 PREC_LOGICAL_AND_EXPRESSION
,
1862 PREC_INCLUSIVE_OR_EXPRESSION
,
1863 PREC_EXCLUSIVE_OR_EXPRESSION
,
1864 PREC_AND_EXPRESSION
,
1865 PREC_EQUALITY_EXPRESSION
,
1866 PREC_RELATIONAL_EXPRESSION
,
1867 PREC_SPACESHIP_EXPRESSION
,
1868 PREC_SHIFT_EXPRESSION
,
1869 PREC_ADDITIVE_EXPRESSION
,
1870 PREC_MULTIPLICATIVE_EXPRESSION
,
1872 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1875 /* A mapping from a token type to a corresponding tree node type, with a
1876 precedence value. */
1878 struct cp_parser_binary_operations_map_node
1880 /* The token type. */
1881 enum cpp_ttype token_type
;
1882 /* The corresponding tree code. */
1883 enum tree_code tree_type
;
1884 /* The precedence of this operator. */
1885 enum cp_parser_prec prec
;
1888 struct cp_parser_expression_stack_entry
1890 /* Left hand side of the binary operation we are currently
1893 /* Original tree code for left hand side, if it was a binary
1894 expression itself (used for -Wparentheses). */
1895 enum tree_code lhs_type
;
1896 /* Tree code for the binary operation we are parsing. */
1897 enum tree_code tree_type
;
1898 /* Precedence of the binary operation we are parsing. */
1899 enum cp_parser_prec prec
;
1900 /* Location of the binary operation we are parsing. */
1904 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1905 entries because precedence levels on the stack are monotonically
1907 typedef struct cp_parser_expression_stack_entry
1908 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1912 /* Constructors and destructors. */
1914 static cp_parser_context
*cp_parser_context_new
1915 (cp_parser_context
*);
1917 /* Class variables. */
1919 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1921 /* The operator-precedence table used by cp_parser_binary_expression.
1922 Transformed into an associative array (binops_by_token) by
1925 static const cp_parser_binary_operations_map_node binops
[] = {
1926 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1927 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1929 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1930 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1931 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1933 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1934 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1936 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1937 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1939 { CPP_SPACESHIP
, SPACESHIP_EXPR
, PREC_SPACESHIP_EXPRESSION
},
1941 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1942 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1943 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1944 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1946 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1947 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1949 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1951 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1953 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1955 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1957 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1960 /* The same as binops, but initialized by cp_parser_new so that
1961 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1963 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1965 /* Constructors and destructors. */
1967 /* Construct a new context. The context below this one on the stack
1968 is given by NEXT. */
1970 static cp_parser_context
*
1971 cp_parser_context_new (cp_parser_context
* next
)
1973 cp_parser_context
*context
;
1975 /* Allocate the storage. */
1976 if (cp_parser_context_free_list
!= NULL
)
1978 /* Pull the first entry from the free list. */
1979 context
= cp_parser_context_free_list
;
1980 cp_parser_context_free_list
= context
->next
;
1981 memset (context
, 0, sizeof (*context
));
1984 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1986 /* No errors have occurred yet in this context. */
1987 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1988 /* If this is not the bottommost context, copy information that we
1989 need from the previous context. */
1992 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1993 expression, then we are parsing one in this context, too. */
1994 context
->object_type
= next
->object_type
;
1995 /* Thread the stack. */
1996 context
->next
= next
;
2002 /* Managing the unparsed function queues. */
2004 #define unparsed_funs_with_default_args \
2005 parser->unparsed_queues->last ().funs_with_default_args
2006 #define unparsed_funs_with_definitions \
2007 parser->unparsed_queues->last ().funs_with_definitions
2008 #define unparsed_nsdmis \
2009 parser->unparsed_queues->last ().nsdmis
2010 #define unparsed_noexcepts \
2011 parser->unparsed_queues->last ().noexcepts
2014 push_unparsed_function_queues (cp_parser
*parser
)
2016 cp_unparsed_functions_entry e
= { NULL
, make_tree_vector (), NULL
, NULL
};
2017 vec_safe_push (parser
->unparsed_queues
, e
);
2021 pop_unparsed_function_queues (cp_parser
*parser
)
2023 release_tree_vector (unparsed_funs_with_definitions
);
2024 parser
->unparsed_queues
->pop ();
2029 /* Constructors and destructors. */
2031 static cp_parser
*cp_parser_new
2034 /* Routines to parse various constructs.
2036 Those that return `tree' will return the error_mark_node (rather
2037 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2038 Sometimes, they will return an ordinary node if error-recovery was
2039 attempted, even though a parse error occurred. So, to check
2040 whether or not a parse error occurred, you should always use
2041 cp_parser_error_occurred. If the construct is optional (indicated
2042 either by an `_opt' in the name of the function that does the
2043 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2044 the construct is not present. */
2046 /* Lexical conventions [gram.lex] */
2048 static cp_expr cp_parser_identifier
2050 static cp_expr cp_parser_string_literal
2051 (cp_parser
*, bool, bool, bool);
2052 static cp_expr cp_parser_userdef_char_literal
2054 static tree cp_parser_userdef_string_literal
2056 static cp_expr cp_parser_userdef_numeric_literal
2059 /* Basic concepts [gram.basic] */
2061 static void cp_parser_translation_unit (cp_parser
*);
2063 /* Expressions [gram.expr] */
2065 static cp_expr cp_parser_primary_expression
2066 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2067 static cp_expr cp_parser_id_expression
2068 (cp_parser
*, bool, bool, bool *, bool, bool);
2069 static cp_expr cp_parser_unqualified_id
2070 (cp_parser
*, bool, bool, bool, bool);
2071 static tree cp_parser_nested_name_specifier_opt
2072 (cp_parser
*, bool, bool, bool, bool, bool = false);
2073 static tree cp_parser_nested_name_specifier
2074 (cp_parser
*, bool, bool, bool, bool);
2075 static tree cp_parser_qualifying_entity
2076 (cp_parser
*, bool, bool, bool, bool, bool);
2077 static cp_expr cp_parser_postfix_expression
2078 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2079 static tree cp_parser_postfix_open_square_expression
2080 (cp_parser
*, tree
, bool, bool);
2081 static tree cp_parser_postfix_dot_deref_expression
2082 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2083 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2084 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2086 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2087 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2088 static void cp_parser_pseudo_destructor_name
2089 (cp_parser
*, tree
, tree
*, tree
*);
2090 static cp_expr cp_parser_unary_expression
2091 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2092 static enum tree_code cp_parser_unary_operator
2094 static tree cp_parser_has_attribute_expression
2096 static tree cp_parser_new_expression
2098 static vec
<tree
, va_gc
> *cp_parser_new_placement
2100 static tree cp_parser_new_type_id
2101 (cp_parser
*, tree
*);
2102 static cp_declarator
*cp_parser_new_declarator_opt
2104 static cp_declarator
*cp_parser_direct_new_declarator
2106 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2108 static tree cp_parser_delete_expression
2110 static cp_expr cp_parser_cast_expression
2111 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2112 static cp_expr cp_parser_binary_expression
2113 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2114 static tree cp_parser_question_colon_clause
2115 (cp_parser
*, cp_expr
);
2116 static cp_expr cp_parser_assignment_expression
2117 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2118 static enum tree_code cp_parser_assignment_operator_opt
2120 static cp_expr cp_parser_expression
2121 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2122 static cp_expr cp_parser_constant_expression
2123 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2124 static cp_expr cp_parser_builtin_offsetof
2126 static cp_expr cp_parser_lambda_expression
2128 static void cp_parser_lambda_introducer
2129 (cp_parser
*, tree
);
2130 static bool cp_parser_lambda_declarator_opt
2131 (cp_parser
*, tree
);
2132 static void cp_parser_lambda_body
2133 (cp_parser
*, tree
);
2135 /* Statements [gram.stmt.stmt] */
2137 static void cp_parser_statement
2138 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2139 static void cp_parser_label_for_labeled_statement
2140 (cp_parser
*, tree
);
2141 static tree cp_parser_expression_statement
2142 (cp_parser
*, tree
);
2143 static tree cp_parser_compound_statement
2144 (cp_parser
*, tree
, int, bool);
2145 static void cp_parser_statement_seq_opt
2146 (cp_parser
*, tree
);
2147 static tree cp_parser_selection_statement
2148 (cp_parser
*, bool *, vec
<tree
> *);
2149 static tree cp_parser_condition
2151 static tree cp_parser_iteration_statement
2152 (cp_parser
*, bool *, bool, unsigned short);
2153 static bool cp_parser_init_statement
2154 (cp_parser
*, tree
*decl
);
2155 static tree cp_parser_for
2156 (cp_parser
*, bool, unsigned short);
2157 static tree cp_parser_c_for
2158 (cp_parser
*, tree
, tree
, bool, unsigned short);
2159 static tree cp_parser_range_for
2160 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2161 static void do_range_for_auto_deduction
2163 static tree cp_parser_perform_range_for_lookup
2164 (tree
, tree
*, tree
*);
2165 static tree cp_parser_range_for_member_function
2167 static tree cp_parser_jump_statement
2169 static void cp_parser_declaration_statement
2172 static tree cp_parser_implicitly_scoped_statement
2173 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2174 static void cp_parser_already_scoped_statement
2175 (cp_parser
*, bool *, const token_indent_info
&);
2177 /* Declarations [gram.dcl.dcl] */
2179 static void cp_parser_declaration_seq_opt
2181 static void cp_parser_declaration
2183 static void cp_parser_toplevel_declaration
2185 static void cp_parser_block_declaration
2186 (cp_parser
*, bool);
2187 static void cp_parser_simple_declaration
2188 (cp_parser
*, bool, tree
*);
2189 static void cp_parser_decl_specifier_seq
2190 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2191 static tree cp_parser_storage_class_specifier_opt
2193 static tree cp_parser_function_specifier_opt
2194 (cp_parser
*, cp_decl_specifier_seq
*);
2195 static tree cp_parser_type_specifier
2196 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2198 static tree cp_parser_simple_type_specifier
2199 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2200 static tree cp_parser_placeholder_type_specifier
2201 (cp_parser
*, location_t
, tree
, bool);
2202 static tree cp_parser_type_name
2203 (cp_parser
*, bool);
2204 static tree cp_parser_nonclass_name
2205 (cp_parser
* parser
);
2206 static tree cp_parser_elaborated_type_specifier
2207 (cp_parser
*, bool, bool);
2208 static tree cp_parser_enum_specifier
2210 static void cp_parser_enumerator_list
2211 (cp_parser
*, tree
);
2212 static void cp_parser_enumerator_definition
2213 (cp_parser
*, tree
);
2214 static tree cp_parser_namespace_name
2216 static void cp_parser_namespace_definition
2218 static void cp_parser_namespace_body
2220 static tree cp_parser_qualified_namespace_specifier
2222 static void cp_parser_namespace_alias_definition
2224 static bool cp_parser_using_declaration
2225 (cp_parser
*, bool);
2226 static void cp_parser_using_directive
2228 static tree cp_parser_alias_declaration
2230 static void cp_parser_asm_definition
2232 static void cp_parser_linkage_specification
2234 static void cp_parser_static_assert
2235 (cp_parser
*, bool);
2236 static tree cp_parser_decltype
2238 static tree cp_parser_decomposition_declaration
2239 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2241 /* Declarators [gram.dcl.decl] */
2243 static tree cp_parser_init_declarator
2244 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2245 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2246 location_t
*, tree
*);
2247 static cp_declarator
*cp_parser_declarator
2248 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2250 static cp_declarator
*cp_parser_direct_declarator
2251 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2253 static enum tree_code cp_parser_ptr_operator
2254 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2255 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2257 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2259 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2261 static tree cp_parser_tx_qualifier_opt
2263 static tree cp_parser_late_return_type_opt
2264 (cp_parser
*, cp_declarator
*, tree
&);
2265 static tree cp_parser_declarator_id
2266 (cp_parser
*, bool);
2267 static tree cp_parser_type_id
2268 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2269 static tree cp_parser_template_type_arg
2271 static tree
cp_parser_trailing_type_id (cp_parser
*);
2272 static tree cp_parser_type_id_1
2273 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2274 static void cp_parser_type_specifier_seq
2275 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2276 static tree cp_parser_parameter_declaration_clause
2277 (cp_parser
*, cp_parser_flags
);
2278 static tree cp_parser_parameter_declaration_list
2279 (cp_parser
*, cp_parser_flags
);
2280 static cp_parameter_declarator
*cp_parser_parameter_declaration
2281 (cp_parser
*, cp_parser_flags
, bool, bool *);
2282 static tree cp_parser_default_argument
2283 (cp_parser
*, bool);
2284 static void cp_parser_function_body
2285 (cp_parser
*, bool);
2286 static tree cp_parser_initializer
2287 (cp_parser
*, bool *, bool *, bool = false);
2288 static cp_expr cp_parser_initializer_clause
2289 (cp_parser
*, bool *);
2290 static cp_expr cp_parser_braced_list
2291 (cp_parser
*, bool*);
2292 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2293 (cp_parser
*, bool *, bool *);
2295 static void cp_parser_ctor_initializer_opt_and_function_body
2296 (cp_parser
*, bool);
2298 static tree cp_parser_late_parsing_omp_declare_simd
2299 (cp_parser
*, tree
);
2301 static tree cp_parser_late_parsing_oacc_routine
2302 (cp_parser
*, tree
);
2304 static tree synthesize_implicit_template_parm
2305 (cp_parser
*, tree
);
2306 static tree finish_fully_implicit_template
2307 (cp_parser
*, tree
);
2308 static void abort_fully_implicit_template
2311 /* Classes [gram.class] */
2313 static tree cp_parser_class_name
2314 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2315 static tree cp_parser_class_specifier
2317 static tree cp_parser_class_head
2318 (cp_parser
*, bool *);
2319 static enum tag_types cp_parser_class_key
2321 static void cp_parser_type_parameter_key
2322 (cp_parser
* parser
);
2323 static void cp_parser_member_specification_opt
2325 static void cp_parser_member_declaration
2327 static tree cp_parser_pure_specifier
2329 static tree cp_parser_constant_initializer
2332 /* Derived classes [gram.class.derived] */
2334 static tree cp_parser_base_clause
2336 static tree cp_parser_base_specifier
2339 /* Special member functions [gram.special] */
2341 static tree cp_parser_conversion_function_id
2343 static tree cp_parser_conversion_type_id
2345 static cp_declarator
*cp_parser_conversion_declarator_opt
2347 static void cp_parser_ctor_initializer_opt
2349 static void cp_parser_mem_initializer_list
2351 static tree cp_parser_mem_initializer
2353 static tree cp_parser_mem_initializer_id
2356 /* Overloading [gram.over] */
2358 static cp_expr cp_parser_operator_function_id
2360 static cp_expr cp_parser_operator
2361 (cp_parser
*, location_t
);
2363 /* Templates [gram.temp] */
2365 static void cp_parser_template_declaration
2366 (cp_parser
*, bool);
2367 static tree cp_parser_template_parameter_list
2369 static tree cp_parser_template_parameter
2370 (cp_parser
*, bool *, bool *);
2371 static tree cp_parser_type_parameter
2372 (cp_parser
*, bool *);
2373 static tree cp_parser_template_id
2374 (cp_parser
*, bool, bool, enum tag_types
, bool);
2375 static tree cp_parser_template_id_expr
2376 (cp_parser
*, bool, bool, bool);
2377 static tree cp_parser_template_name
2378 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2379 static tree cp_parser_template_argument_list
2381 static tree cp_parser_template_argument
2383 static void cp_parser_explicit_instantiation
2385 static void cp_parser_explicit_specialization
2388 /* Exception handling [gram.except] */
2390 static tree cp_parser_try_block
2392 static void cp_parser_function_try_block
2394 static void cp_parser_handler_seq
2396 static void cp_parser_handler
2398 static tree cp_parser_exception_declaration
2400 static tree cp_parser_throw_expression
2402 static tree cp_parser_exception_specification_opt
2403 (cp_parser
*, cp_parser_flags
);
2404 static tree cp_parser_type_id_list
2406 static tree cp_parser_noexcept_specification_opt
2407 (cp_parser
*, cp_parser_flags
, bool, bool *, bool);
2409 /* GNU Extensions */
2411 static tree cp_parser_asm_specification_opt
2413 static tree cp_parser_asm_operand_list
2415 static tree cp_parser_asm_clobber_list
2417 static tree cp_parser_asm_label_list
2419 static bool cp_next_tokens_can_be_attribute_p
2421 static bool cp_next_tokens_can_be_gnu_attribute_p
2423 static bool cp_next_tokens_can_be_std_attribute_p
2425 static bool cp_nth_tokens_can_be_std_attribute_p
2426 (cp_parser
*, size_t);
2427 static bool cp_nth_tokens_can_be_gnu_attribute_p
2428 (cp_parser
*, size_t);
2429 static bool cp_nth_tokens_can_be_attribute_p
2430 (cp_parser
*, size_t);
2431 static tree cp_parser_attributes_opt
2433 static tree cp_parser_gnu_attributes_opt
2435 static tree cp_parser_gnu_attribute_list
2436 (cp_parser
*, bool = false);
2437 static tree cp_parser_std_attribute
2438 (cp_parser
*, tree
);
2439 static tree cp_parser_std_attribute_spec
2441 static tree cp_parser_std_attribute_spec_seq
2443 static size_t cp_parser_skip_attributes_opt
2444 (cp_parser
*, size_t);
2445 static bool cp_parser_extension_opt
2446 (cp_parser
*, int *);
2447 static void cp_parser_label_declaration
2450 /* Concept Extensions */
2452 static tree cp_parser_concept_definition
2454 static tree cp_parser_constraint_expression
2456 static tree cp_parser_requires_clause_opt
2457 (cp_parser
*, bool);
2458 static tree cp_parser_requires_expression
2460 static tree cp_parser_requirement_parameter_list
2462 static tree cp_parser_requirement_body
2464 static tree cp_parser_requirement_seq
2466 static tree cp_parser_requirement
2468 static tree cp_parser_simple_requirement
2470 static tree cp_parser_compound_requirement
2472 static tree cp_parser_type_requirement
2474 static tree cp_parser_nested_requirement
2477 /* Transactional Memory Extensions */
2479 static tree cp_parser_transaction
2480 (cp_parser
*, cp_token
*);
2481 static tree cp_parser_transaction_expression
2482 (cp_parser
*, enum rid
);
2483 static void cp_parser_function_transaction
2484 (cp_parser
*, enum rid
);
2485 static tree cp_parser_transaction_cancel
2488 /* Coroutine extensions. */
2490 static tree cp_parser_yield_expression
2494 enum pragma_context
{
2501 static bool cp_parser_pragma
2502 (cp_parser
*, enum pragma_context
, bool *);
2504 /* Objective-C++ Productions */
2506 static tree cp_parser_objc_message_receiver
2508 static tree cp_parser_objc_message_args
2510 static tree cp_parser_objc_message_expression
2512 static cp_expr cp_parser_objc_encode_expression
2514 static tree cp_parser_objc_defs_expression
2516 static tree cp_parser_objc_protocol_expression
2518 static tree cp_parser_objc_selector_expression
2520 static cp_expr cp_parser_objc_expression
2522 static bool cp_parser_objc_selector_p
2524 static tree cp_parser_objc_selector
2526 static tree cp_parser_objc_protocol_refs_opt
2528 static void cp_parser_objc_declaration
2529 (cp_parser
*, tree
);
2530 static tree cp_parser_objc_statement
2532 static bool cp_parser_objc_valid_prefix_attributes
2533 (cp_parser
*, tree
*);
2534 static void cp_parser_objc_at_property_declaration
2536 static void cp_parser_objc_at_synthesize_declaration
2538 static void cp_parser_objc_at_dynamic_declaration
2540 static tree cp_parser_objc_struct_declaration
2543 /* Utility Routines */
2545 static cp_expr cp_parser_lookup_name
2546 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2547 static tree cp_parser_lookup_name_simple
2548 (cp_parser
*, tree
, location_t
);
2549 static tree cp_parser_maybe_treat_template_as_class
2551 static bool cp_parser_check_declarator_template_parameters
2552 (cp_parser
*, cp_declarator
*, location_t
);
2553 static bool cp_parser_check_template_parameters
2554 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2555 static cp_expr cp_parser_simple_cast_expression
2557 static tree cp_parser_global_scope_opt
2558 (cp_parser
*, bool);
2559 static bool cp_parser_constructor_declarator_p
2560 (cp_parser
*, cp_parser_flags
, bool);
2561 static tree cp_parser_function_definition_from_specifiers_and_declarator
2562 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2563 static tree cp_parser_function_definition_after_declarator
2564 (cp_parser
*, bool);
2565 static bool cp_parser_template_declaration_after_export
2566 (cp_parser
*, bool);
2567 static void cp_parser_perform_template_parameter_access_checks
2568 (vec
<deferred_access_check
, va_gc
> *);
2569 static tree cp_parser_single_declaration
2570 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2571 static cp_expr cp_parser_functional_cast
2572 (cp_parser
*, tree
);
2573 static tree cp_parser_save_member_function_body
2574 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2575 static tree cp_parser_save_nsdmi
2577 static tree cp_parser_enclosed_template_argument_list
2579 static void cp_parser_save_default_args
2580 (cp_parser
*, tree
);
2581 static void cp_parser_late_parsing_for_member
2582 (cp_parser
*, tree
);
2583 static tree cp_parser_late_parse_one_default_arg
2584 (cp_parser
*, tree
, tree
, tree
);
2585 static void cp_parser_late_parsing_nsdmi
2586 (cp_parser
*, tree
);
2587 static void cp_parser_late_parsing_default_args
2588 (cp_parser
*, tree
);
2589 static tree cp_parser_sizeof_operand
2590 (cp_parser
*, enum rid
);
2591 static cp_expr cp_parser_trait_expr
2592 (cp_parser
*, enum rid
);
2593 static bool cp_parser_declares_only_class_p
2595 static void cp_parser_set_storage_class
2596 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2597 static void cp_parser_set_decl_spec_type
2598 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2599 static void set_and_check_decl_spec_loc
2600 (cp_decl_specifier_seq
*decl_specs
,
2601 cp_decl_spec ds
, cp_token
*);
2602 static bool cp_parser_friend_p
2603 (const cp_decl_specifier_seq
*);
2604 static void cp_parser_required_error
2605 (cp_parser
*, required_token
, bool, location_t
);
2606 static cp_token
*cp_parser_require
2607 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2608 static cp_token
*cp_parser_require_keyword
2609 (cp_parser
*, enum rid
, required_token
);
2610 static bool cp_parser_token_starts_function_definition_p
2612 static bool cp_parser_next_token_starts_class_definition_p
2614 static bool cp_parser_next_token_ends_template_argument_p
2616 static bool cp_parser_nth_token_starts_template_argument_list_p
2617 (cp_parser
*, size_t);
2618 static enum tag_types cp_parser_token_is_class_key
2620 static enum tag_types cp_parser_token_is_type_parameter_key
2622 static void cp_parser_maybe_warn_enum_key (cp_parser
*, location_t
, tree
, rid
);
2623 static void cp_parser_check_class_key
2624 (cp_parser
*, location_t
, enum tag_types
, tree type
, bool, bool);
2625 static void cp_parser_check_access_in_redeclaration
2626 (tree type
, location_t location
);
2627 static bool cp_parser_optional_template_keyword
2629 static void cp_parser_pre_parsed_nested_name_specifier
2631 static bool cp_parser_cache_group
2632 (cp_parser
*, enum cpp_ttype
, unsigned);
2633 static tree cp_parser_cache_defarg
2634 (cp_parser
*parser
, bool nsdmi
);
2635 static void cp_parser_parse_tentatively
2637 static void cp_parser_commit_to_tentative_parse
2639 static void cp_parser_commit_to_topmost_tentative_parse
2641 static void cp_parser_abort_tentative_parse
2643 static bool cp_parser_parse_definitely
2645 static inline bool cp_parser_parsing_tentatively
2647 static bool cp_parser_uncommitted_to_tentative_parse_p
2649 static void cp_parser_error
2650 (cp_parser
*, const char *);
2651 static void cp_parser_name_lookup_error
2652 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2653 static bool cp_parser_simulate_error
2655 static bool cp_parser_check_type_definition
2657 static void cp_parser_check_for_definition_in_return_type
2658 (cp_declarator
*, tree
, location_t type_location
);
2659 static void cp_parser_check_for_invalid_template_id
2660 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2661 static bool cp_parser_non_integral_constant_expression
2662 (cp_parser
*, non_integral_constant
);
2663 static void cp_parser_diagnose_invalid_type_name
2664 (cp_parser
*, tree
, location_t
);
2665 static bool cp_parser_parse_and_diagnose_invalid_type_name
2667 static int cp_parser_skip_to_closing_parenthesis
2668 (cp_parser
*, bool, bool, bool);
2669 static void cp_parser_skip_to_end_of_statement
2671 static void cp_parser_consume_semicolon_at_end_of_statement
2673 static void cp_parser_skip_to_end_of_block_or_statement
2675 static bool cp_parser_skip_to_closing_brace
2677 static void cp_parser_skip_to_end_of_template_parameter_list
2679 static void cp_parser_skip_to_pragma_eol
2680 (cp_parser
*, cp_token
*);
2681 static bool cp_parser_error_occurred
2683 static bool cp_parser_allow_gnu_extensions_p
2685 static bool cp_parser_is_pure_string_literal
2687 static bool cp_parser_is_string_literal
2689 static bool cp_parser_is_keyword
2690 (cp_token
*, enum rid
);
2691 static tree cp_parser_make_typename_type
2692 (cp_parser
*, tree
, location_t location
);
2693 static cp_declarator
* cp_parser_make_indirect_declarator
2694 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2695 static bool cp_parser_compound_literal_p
2697 static bool cp_parser_array_designator_p
2699 static bool cp_parser_init_statement_p
2701 static bool cp_parser_skip_to_closing_square_bracket
2703 static size_t cp_parser_skip_balanced_tokens (cp_parser
*, size_t);
2705 // -------------------------------------------------------------------------- //
2706 // Unevaluated Operand Guard
2708 // Implementation of an RAII helper for unevaluated operand parsing.
2709 cp_unevaluated::cp_unevaluated ()
2711 ++cp_unevaluated_operand
;
2712 ++c_inhibit_evaluation_warnings
;
2715 cp_unevaluated::~cp_unevaluated ()
2717 --c_inhibit_evaluation_warnings
;
2718 --cp_unevaluated_operand
;
2721 // -------------------------------------------------------------------------- //
2722 // Tentative Parsing
2724 /* Returns nonzero if we are parsing tentatively. */
2727 cp_parser_parsing_tentatively (cp_parser
* parser
)
2729 return parser
->context
->next
!= NULL
;
2732 /* Returns nonzero if TOKEN is a string literal. */
2735 cp_parser_is_pure_string_literal (cp_token
* token
)
2737 return (token
->type
== CPP_STRING
||
2738 token
->type
== CPP_STRING16
||
2739 token
->type
== CPP_STRING32
||
2740 token
->type
== CPP_WSTRING
||
2741 token
->type
== CPP_UTF8STRING
);
2744 /* Returns nonzero if TOKEN is a string literal
2745 of a user-defined string literal. */
2748 cp_parser_is_string_literal (cp_token
* token
)
2750 return (cp_parser_is_pure_string_literal (token
) ||
2751 token
->type
== CPP_STRING_USERDEF
||
2752 token
->type
== CPP_STRING16_USERDEF
||
2753 token
->type
== CPP_STRING32_USERDEF
||
2754 token
->type
== CPP_WSTRING_USERDEF
||
2755 token
->type
== CPP_UTF8STRING_USERDEF
);
2758 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2761 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2763 return token
->keyword
== keyword
;
2766 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2769 static enum pragma_kind
2770 cp_parser_pragma_kind (cp_token
*token
)
2772 if (token
->type
!= CPP_PRAGMA
)
2774 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2775 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2778 /* Helper function for cp_parser_error.
2779 Having peeked a token of kind TOK1_KIND that might signify
2780 a conflict marker, peek successor tokens to determine
2781 if we actually do have a conflict marker.
2782 Specifically, we consider a run of 7 '<', '=' or '>' characters
2783 at the start of a line as a conflict marker.
2784 These come through the lexer as three pairs and a single,
2785 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2786 If it returns true, *OUT_LOC is written to with the location/range
2790 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2791 location_t
*out_loc
)
2793 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2794 if (token2
->type
!= tok1_kind
)
2796 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2797 if (token3
->type
!= tok1_kind
)
2799 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2800 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2803 /* It must be at the start of the line. */
2804 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2805 if (LOCATION_COLUMN (start_loc
) != 1)
2808 /* We have a conflict marker. Construct a location of the form:
2811 with start == caret, finishing at the end of the marker. */
2812 location_t finish_loc
= get_finish (token4
->location
);
2813 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2818 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2822 get_matching_symbol (required_token token_desc
)
2829 case RT_CLOSE_BRACE
:
2831 case RT_CLOSE_PAREN
:
2836 /* Attempt to convert TOKEN_DESC from a required_token to an
2837 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2839 static enum cpp_ttype
2840 get_required_cpp_ttype (required_token token_desc
)
2845 return CPP_SEMICOLON
;
2847 return CPP_OPEN_PAREN
;
2848 case RT_CLOSE_BRACE
:
2849 return CPP_CLOSE_BRACE
;
2851 return CPP_OPEN_BRACE
;
2852 case RT_CLOSE_SQUARE
:
2853 return CPP_CLOSE_SQUARE
;
2854 case RT_OPEN_SQUARE
:
2855 return CPP_OPEN_SQUARE
;
2860 case RT_CLOSE_PAREN
:
2861 return CPP_CLOSE_PAREN
;
2864 /* Use CPP_EOF as a "no completions possible" code. */
2870 /* Subroutine of cp_parser_error and cp_parser_required_error.
2872 Issue a diagnostic of the form
2873 FILE:LINE: MESSAGE before TOKEN
2874 where TOKEN is the next token in the input stream. MESSAGE
2875 (specified by the caller) is usually of the form "expected
2878 This bypasses the check for tentative passing, and potentially
2879 adds material needed by cp_parser_required_error.
2881 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2882 suggesting insertion of the missing token.
2884 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2885 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2889 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2890 required_token missing_token_desc
,
2891 location_t matching_location
)
2893 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2894 /* This diagnostic makes more sense if it is tagged to the line
2895 of the token we just peeked at. */
2896 cp_lexer_set_source_position_from_token (token
);
2898 if (token
->type
== CPP_PRAGMA
)
2900 error_at (token
->location
,
2901 "%<#pragma%> is not allowed here");
2902 cp_parser_skip_to_pragma_eol (parser
, token
);
2906 /* If this is actually a conflict marker, report it as such. */
2907 if (token
->type
== CPP_LSHIFT
2908 || token
->type
== CPP_RSHIFT
2909 || token
->type
== CPP_EQ_EQ
)
2912 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2914 error_at (loc
, "version control conflict marker in file");
2915 expanded_location token_exploc
= expand_location (token
->location
);
2916 /* Consume tokens until the end of the source line. */
2919 cp_lexer_consume_token (parser
->lexer
);
2920 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2921 if (next
->type
== CPP_EOF
)
2923 if (next
->location
== UNKNOWN_LOCATION
2924 || loc
== UNKNOWN_LOCATION
)
2927 expanded_location next_exploc
= expand_location (next
->location
);
2928 if (next_exploc
.file
!= token_exploc
.file
)
2930 if (next_exploc
.line
!= token_exploc
.line
)
2937 auto_diagnostic_group d
;
2938 gcc_rich_location
richloc (input_location
);
2940 bool added_matching_location
= false;
2942 if (missing_token_desc
!= RT_NONE
)
2944 /* Potentially supply a fix-it hint, suggesting to add the
2945 missing token immediately after the *previous* token.
2946 This may move the primary location within richloc. */
2947 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2948 location_t prev_token_loc
2949 = cp_lexer_previous_token (parser
->lexer
)->location
;
2950 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2952 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2953 Attempt to consolidate diagnostics by printing it as a
2954 secondary range within the main diagnostic. */
2955 if (matching_location
!= UNKNOWN_LOCATION
)
2956 added_matching_location
2957 = richloc
.add_location_if_nearby (matching_location
);
2960 /* If we were parsing a string-literal and there is an unknown name
2961 token right after, then check to see if that could also have been
2962 a literal string by checking the name against a list of known
2963 standard string literal constants defined in header files. If
2964 there is one, then add that as an hint to the error message. */
2966 cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
);
2967 if (prev_token
&& cp_parser_is_string_literal (prev_token
)
2968 && token
->type
== CPP_NAME
)
2970 tree name
= token
->u
.value
;
2971 const char *token_name
= IDENTIFIER_POINTER (name
);
2972 const char *header_hint
2973 = get_cp_stdlib_header_for_string_macro_name (token_name
);
2974 if (header_hint
!= NULL
)
2975 h
= name_hint (NULL
, new suggest_missing_header (token
->location
,
2980 /* Actually emit the error. */
2981 c_parse_error (gmsgid
,
2982 /* Because c_parser_error does not understand
2983 CPP_KEYWORD, keywords are treated like
2985 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2986 token
->u
.value
, token
->flags
, &richloc
);
2988 if (missing_token_desc
!= RT_NONE
)
2990 /* If we weren't able to consolidate matching_location, then
2991 print it as a secondary diagnostic. */
2992 if (matching_location
!= UNKNOWN_LOCATION
2993 && !added_matching_location
)
2994 inform (matching_location
, "to match this %qs",
2995 get_matching_symbol (missing_token_desc
));
2999 /* If not parsing tentatively, issue a diagnostic of the form
3000 FILE:LINE: MESSAGE before TOKEN
3001 where TOKEN is the next token in the input stream. MESSAGE
3002 (specified by the caller) is usually of the form "expected
3006 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
3008 if (!cp_parser_simulate_error (parser
))
3009 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
3012 /* Issue an error about name-lookup failing. NAME is the
3013 IDENTIFIER_NODE DECL is the result of
3014 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3015 the thing that we hoped to find. */
3018 cp_parser_name_lookup_error (cp_parser
* parser
,
3021 name_lookup_error desired
,
3022 location_t location
)
3024 /* If name lookup completely failed, tell the user that NAME was not
3026 if (decl
== error_mark_node
)
3028 if (parser
->scope
&& parser
->scope
!= global_namespace
)
3029 error_at (location
, "%<%E::%E%> has not been declared",
3030 parser
->scope
, name
);
3031 else if (parser
->scope
== global_namespace
)
3032 error_at (location
, "%<::%E%> has not been declared", name
);
3033 else if (parser
->object_scope
3034 && !CLASS_TYPE_P (parser
->object_scope
))
3035 error_at (location
, "request for member %qE in non-class type %qT",
3036 name
, parser
->object_scope
);
3037 else if (parser
->object_scope
)
3038 error_at (location
, "%<%T::%E%> has not been declared",
3039 parser
->object_scope
, name
);
3041 error_at (location
, "%qE has not been declared", name
);
3043 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
3048 error_at (location
, "%<%E::%E%> is not a type",
3049 parser
->scope
, name
);
3052 error_at (location
, "%<%E::%E%> is not a class or namespace",
3053 parser
->scope
, name
);
3057 "%<%E::%E%> is not a class, namespace, or enumeration",
3058 parser
->scope
, name
);
3065 else if (parser
->scope
== global_namespace
)
3070 error_at (location
, "%<::%E%> is not a type", name
);
3073 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3077 "%<::%E%> is not a class, namespace, or enumeration",
3089 error_at (location
, "%qE is not a type", name
);
3092 error_at (location
, "%qE is not a class or namespace", name
);
3096 "%qE is not a class, namespace, or enumeration", name
);
3104 /* If we are parsing tentatively, remember that an error has occurred
3105 during this tentative parse. Returns true if the error was
3106 simulated; false if a message should be issued by the caller. */
3109 cp_parser_simulate_error (cp_parser
* parser
)
3111 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3113 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3119 /* This function is called when a type is defined. If type
3120 definitions are forbidden at this point, an error message is
3124 cp_parser_check_type_definition (cp_parser
* parser
)
3126 /* If types are forbidden here, issue a message. */
3127 if (parser
->type_definition_forbidden_message
)
3129 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3130 or %qs in the message need to be interpreted. */
3131 error (parser
->type_definition_forbidden_message
,
3132 parser
->type_definition_forbidden_message_arg
);
3138 /* This function is called when the DECLARATOR is processed. The TYPE
3139 was a type defined in the decl-specifiers. If it is invalid to
3140 define a type in the decl-specifiers for DECLARATOR, an error is
3141 issued. TYPE_LOCATION is the location of TYPE and is used
3142 for error reporting. */
3145 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3146 tree type
, location_t type_location
)
3148 /* [dcl.fct] forbids type definitions in return types.
3149 Unfortunately, it's not easy to know whether or not we are
3150 processing a return type until after the fact. */
3152 && (declarator
->kind
== cdk_pointer
3153 || declarator
->kind
== cdk_reference
3154 || declarator
->kind
== cdk_ptrmem
))
3155 declarator
= declarator
->declarator
;
3157 && declarator
->kind
== cdk_function
)
3159 error_at (type_location
,
3160 "new types may not be defined in a return type");
3161 inform (type_location
,
3162 "(perhaps a semicolon is missing after the definition of %qT)",
3167 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3168 "<" in any valid C++ program. If the next token is indeed "<",
3169 issue a message warning the user about what appears to be an
3170 invalid attempt to form a template-id. LOCATION is the location
3171 of the type-specifier (TYPE) */
3174 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3176 enum tag_types tag_type
,
3177 location_t location
)
3179 cp_token_position start
= 0;
3181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3183 if (TREE_CODE (type
) == TYPE_DECL
)
3184 type
= TREE_TYPE (type
);
3185 if (TYPE_P (type
) && !template_placeholder_p (type
))
3186 error_at (location
, "%qT is not a template", type
);
3187 else if (identifier_p (type
))
3189 if (tag_type
!= none_type
)
3190 error_at (location
, "%qE is not a class template", type
);
3192 error_at (location
, "%qE is not a template", type
);
3195 error_at (location
, "invalid template-id");
3196 /* Remember the location of the invalid "<". */
3197 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3198 start
= cp_lexer_token_position (parser
->lexer
, true);
3199 /* Consume the "<". */
3200 cp_lexer_consume_token (parser
->lexer
);
3201 /* Parse the template arguments. */
3202 cp_parser_enclosed_template_argument_list (parser
);
3203 /* Permanently remove the invalid template arguments so that
3204 this error message is not issued again. */
3206 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3210 /* If parsing an integral constant-expression, issue an error message
3211 about the fact that THING appeared and return true. Otherwise,
3212 return false. In either case, set
3213 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3216 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3217 non_integral_constant thing
)
3219 parser
->non_integral_constant_expression_p
= true;
3220 if (parser
->integral_constant_expression_p
)
3222 if (!parser
->allow_non_integral_constant_expression_p
)
3224 const char *msg
= NULL
;
3228 pedwarn (input_location
, OPT_Wpedantic
,
3229 "ISO C++ forbids using a floating-point literal "
3230 "in a constant-expression");
3233 error ("a cast to a type other than an integral or "
3234 "enumeration type cannot appear in a "
3235 "constant-expression");
3238 error ("%<typeid%> operator "
3239 "cannot appear in a constant-expression");
3242 error ("non-constant compound literals "
3243 "cannot appear in a constant-expression");
3246 error ("a function call "
3247 "cannot appear in a constant-expression");
3250 error ("an increment "
3251 "cannot appear in a constant-expression");
3254 error ("an decrement "
3255 "cannot appear in a constant-expression");
3258 error ("an array reference "
3259 "cannot appear in a constant-expression");
3261 case NIC_ADDR_LABEL
:
3262 error ("the address of a label "
3263 "cannot appear in a constant-expression");
3265 case NIC_OVERLOADED
:
3266 error ("calls to overloaded operators "
3267 "cannot appear in a constant-expression");
3269 case NIC_ASSIGNMENT
:
3270 error ("an assignment cannot appear in a constant-expression");
3273 error ("a comma operator "
3274 "cannot appear in a constant-expression");
3276 case NIC_CONSTRUCTOR
:
3277 error ("a call to a constructor "
3278 "cannot appear in a constant-expression");
3280 case NIC_TRANSACTION
:
3281 error ("a transaction expression "
3282 "cannot appear in a constant-expression");
3288 msg
= "__FUNCTION__";
3290 case NIC_PRETTY_FUNC
:
3291 msg
= "__PRETTY_FUNCTION__";
3311 case NIC_PREINCREMENT
:
3314 case NIC_PREDECREMENT
:
3327 error ("%qs cannot appear in a constant-expression", msg
);
3334 /* Emit a diagnostic for an invalid type name. This function commits
3335 to the current active tentative parse, if any. (Otherwise, the
3336 problematic construct might be encountered again later, resulting
3337 in duplicate error messages.) LOCATION is the location of ID. */
3340 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3341 location_t location
)
3343 tree decl
, ambiguous_decls
;
3344 cp_parser_commit_to_tentative_parse (parser
);
3345 /* Try to lookup the identifier. */
3346 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3347 /*is_template=*/false,
3348 /*is_namespace=*/false,
3349 /*check_dependency=*/true,
3350 &ambiguous_decls
, location
);
3351 if (ambiguous_decls
)
3352 /* If the lookup was ambiguous, an error will already have
3355 /* If the lookup found a template-name, it means that the user forgot
3356 to specify an argument list. Emit a useful error message. */
3357 if (DECL_TYPE_TEMPLATE_P (decl
))
3359 auto_diagnostic_group d
;
3361 "invalid use of template-name %qE without an argument list",
3363 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3364 inform (location
, "class template argument deduction is only available "
3365 "with %<-std=c++17%> or %<-std=gnu++17%>");
3366 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3368 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3369 error_at (location
, "invalid use of destructor %qD as a type", id
);
3370 else if (TREE_CODE (decl
) == TYPE_DECL
)
3371 /* Something like 'unsigned A a;' */
3372 error_at (location
, "invalid combination of multiple type-specifiers");
3373 else if (!parser
->scope
)
3375 /* Issue an error message. */
3376 auto_diagnostic_group d
;
3378 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3379 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3380 if (const char *suggestion
= hint
.suggestion ())
3382 gcc_rich_location
richloc (location
);
3383 richloc
.add_fixit_replace (suggestion
);
3385 "%qE does not name a type; did you mean %qs?",
3389 error_at (location
, "%qE does not name a type", id
);
3390 /* If we're in a template class, it's possible that the user was
3391 referring to a type from a base class. For example:
3393 template <typename T> struct A { typedef T X; };
3394 template <typename T> struct B : public A<T> { X x; };
3396 The user should have said "typename A<T>::X". */
3397 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3398 inform (location
, "C++11 %<constexpr%> only available with "
3399 "%<-std=c++11%> or %<-std=gnu++11%>");
3400 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3401 inform (location
, "C++11 %<noexcept%> only available with "
3402 "%<-std=c++11%> or %<-std=gnu++11%>");
3403 else if (cxx_dialect
< cxx11
3404 && TREE_CODE (id
) == IDENTIFIER_NODE
3405 && id_equal (id
, "thread_local"))
3406 inform (location
, "C++11 %<thread_local%> only available with "
3407 "%<-std=c++11%> or %<-std=gnu++11%>");
3408 else if (cxx_dialect
< cxx20
&& id
== ridpointers
[(int)RID_CONSTINIT
])
3409 inform (location
, "C++20 %<constinit%> only available with "
3410 "%<-std=c++20%> or %<-std=gnu++20%>");
3411 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3412 inform (location
, "%<concept%> only available with %<-std=c++20%> or "
3414 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_REQUIRES
])
3415 inform (location
, "%<requires%> only available with %<-std=c++20%> or "
3417 else if (processing_template_decl
&& current_class_type
3418 && TYPE_BINFO (current_class_type
))
3420 for (tree b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3421 b
; b
= TREE_CHAIN (b
))
3423 tree base_type
= BINFO_TYPE (b
);
3424 if (CLASS_TYPE_P (base_type
)
3425 && dependent_type_p (base_type
))
3427 /* Go from a particular instantiation of the
3428 template (which will have an empty TYPE_FIELDs),
3429 to the main version. */
3430 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3431 for (tree field
= TYPE_FIELDS (base_type
);
3432 field
; field
= DECL_CHAIN (field
))
3433 if (TREE_CODE (field
) == TYPE_DECL
3434 && DECL_NAME (field
) == id
)
3437 "(perhaps %<typename %T::%E%> was intended)",
3438 BINFO_TYPE (b
), id
);
3446 /* Here we diagnose qualified-ids where the scope is actually correct,
3447 but the identifier does not resolve to a valid type name. */
3448 else if (parser
->scope
!= error_mark_node
)
3450 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3452 auto_diagnostic_group d
;
3454 if (decl
== error_mark_node
)
3455 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3457 const char *suggestion
= hint
.suggestion ();
3458 gcc_rich_location
richloc (location_of (id
));
3460 richloc
.add_fixit_replace (suggestion
);
3461 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3465 "%qE in namespace %qE does not name a template"
3466 " type; did you mean %qs?",
3467 id
, parser
->scope
, suggestion
);
3470 "%qE in namespace %qE does not name a template type",
3473 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3477 "%qE in namespace %qE does not name a template"
3478 " type; did you mean %qs?",
3479 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3482 "%qE in namespace %qE does not name a template"
3484 TREE_OPERAND (id
, 0), parser
->scope
);
3490 "%qE in namespace %qE does not name a type"
3491 "; did you mean %qs?",
3492 id
, parser
->scope
, suggestion
);
3495 "%qE in namespace %qE does not name a type",
3499 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3501 else if (CLASS_TYPE_P (parser
->scope
)
3502 && constructor_name_p (id
, parser
->scope
))
3505 auto_diagnostic_group d
;
3506 error_at (location
, "%<%T::%E%> names the constructor, not"
3507 " the type", parser
->scope
, id
);
3508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3509 error_at (location
, "and %qT has no template constructors",
3512 else if (TYPE_P (parser
->scope
)
3513 && dependent_scope_p (parser
->scope
))
3515 gcc_rich_location
richloc (location
);
3516 richloc
.add_fixit_insert_before ("typename ");
3517 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3519 "need %<typename%> before %<%T::%D::%E%> because "
3520 "%<%T::%D%> is a dependent scope",
3521 TYPE_CONTEXT (parser
->scope
),
3522 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3524 TYPE_CONTEXT (parser
->scope
),
3525 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3527 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3528 "%qT is a dependent scope",
3529 parser
->scope
, id
, parser
->scope
);
3531 else if (TYPE_P (parser
->scope
))
3533 auto_diagnostic_group d
;
3534 if (!COMPLETE_TYPE_P (parser
->scope
))
3535 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3537 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3538 error_at (location_of (id
),
3539 "%qE in %q#T does not name a template type",
3541 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3542 error_at (location_of (id
),
3543 "%qE in %q#T does not name a template type",
3544 TREE_OPERAND (id
, 0), parser
->scope
);
3546 error_at (location_of (id
),
3547 "%qE in %q#T does not name a type",
3550 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3557 /* Check for a common situation where a type-name should be present,
3558 but is not, and issue a sensible error message. Returns true if an
3559 invalid type-name was detected.
3561 The situation handled by this function are variable declarations of the
3562 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3563 Usually, `ID' should name a type, but if we got here it means that it
3564 does not. We try to emit the best possible error message depending on
3565 how exactly the id-expression looks like. */
3568 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3571 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3573 /* Avoid duplicate error about ambiguous lookup. */
3574 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3576 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3577 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3581 cp_parser_parse_tentatively (parser
);
3582 id
= cp_parser_id_expression (parser
,
3583 /*template_keyword_p=*/false,
3584 /*check_dependency_p=*/true,
3585 /*template_p=*/NULL
,
3586 /*declarator_p=*/false,
3587 /*optional_p=*/false);
3588 /* If the next token is a (, this is a function with no explicit return
3589 type, i.e. constructor, destructor or conversion op. */
3590 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3591 || TREE_CODE (id
) == TYPE_DECL
)
3593 cp_parser_abort_tentative_parse (parser
);
3596 if (!cp_parser_parse_definitely (parser
))
3599 /* Emit a diagnostic for the invalid type. */
3600 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3602 /* If we aren't in the middle of a declarator (i.e. in a
3603 parameter-declaration-clause), skip to the end of the declaration;
3604 there's no point in trying to process it. */
3605 if (!parser
->in_declarator_p
)
3606 cp_parser_skip_to_end_of_block_or_statement (parser
);
3610 /* Consume tokens up to, and including, the next non-nested closing `)'.
3611 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3612 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3613 found an unnested token of that type. */
3616 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3621 unsigned paren_depth
= 0;
3622 unsigned brace_depth
= 0;
3623 unsigned square_depth
= 0;
3624 unsigned condop_depth
= 0;
3626 if (recovering
&& or_ttype
== CPP_EOF
3627 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3632 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3634 /* Have we found what we're looking for before the closing paren? */
3635 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3636 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3639 switch (token
->type
)
3641 case CPP_PRAGMA_EOL
:
3642 if (!parser
->lexer
->in_pragma
)
3646 /* If we've run out of tokens, then there is no closing `)'. */
3649 /* This is good for lambda expression capture-lists. */
3650 case CPP_OPEN_SQUARE
:
3653 case CPP_CLOSE_SQUARE
:
3654 if (!square_depth
--)
3659 /* This matches the processing in skip_to_end_of_statement. */
3664 case CPP_OPEN_BRACE
:
3667 case CPP_CLOSE_BRACE
:
3672 case CPP_OPEN_PAREN
:
3677 case CPP_CLOSE_PAREN
:
3678 if (!brace_depth
&& !paren_depth
--)
3681 cp_lexer_consume_token (parser
->lexer
);
3687 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3692 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3700 /* Consume the token. */
3701 cp_lexer_consume_token (parser
->lexer
);
3705 /* Consume tokens up to, and including, the next non-nested closing `)'.
3706 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3707 are doing error recovery. Returns -1 if OR_COMMA is true and we
3708 found an unnested token of that type. */
3711 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3716 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3717 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3718 ttype
, consume_paren
);
3721 /* Consume tokens until we reach the end of the current statement.
3722 Normally, that will be just before consuming a `;'. However, if a
3723 non-nested `}' comes first, then we stop before consuming that. */
3726 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3728 unsigned nesting_depth
= 0;
3730 /* Unwind generic function template scope if necessary. */
3731 if (parser
->fully_implicit_function_template_p
)
3732 abort_fully_implicit_template (parser
);
3736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3738 switch (token
->type
)
3740 case CPP_PRAGMA_EOL
:
3741 if (!parser
->lexer
->in_pragma
)
3745 /* If we've run out of tokens, stop. */
3749 /* If the next token is a `;', we have reached the end of the
3755 case CPP_CLOSE_BRACE
:
3756 /* If this is a non-nested '}', stop before consuming it.
3757 That way, when confronted with something like:
3761 we stop before consuming the closing '}', even though we
3762 have not yet reached a `;'. */
3763 if (nesting_depth
== 0)
3766 /* If it is the closing '}' for a block that we have
3767 scanned, stop -- but only after consuming the token.
3773 we will stop after the body of the erroneously declared
3774 function, but before consuming the following `typedef'
3776 if (--nesting_depth
== 0)
3778 cp_lexer_consume_token (parser
->lexer
);
3783 case CPP_OPEN_BRACE
:
3791 /* Consume the token. */
3792 cp_lexer_consume_token (parser
->lexer
);
3796 /* This function is called at the end of a statement or declaration.
3797 If the next token is a semicolon, it is consumed; otherwise, error
3798 recovery is attempted. */
3801 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3803 /* Look for the trailing `;'. */
3804 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3806 /* If there is additional (erroneous) input, skip to the end of
3808 cp_parser_skip_to_end_of_statement (parser
);
3809 /* If the next token is now a `;', consume it. */
3810 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3811 cp_lexer_consume_token (parser
->lexer
);
3815 /* Skip tokens until we have consumed an entire block, or until we
3816 have consumed a non-nested `;'. */
3819 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3821 int nesting_depth
= 0;
3823 /* Unwind generic function template scope if necessary. */
3824 if (parser
->fully_implicit_function_template_p
)
3825 abort_fully_implicit_template (parser
);
3827 while (nesting_depth
>= 0)
3829 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3831 switch (token
->type
)
3833 case CPP_PRAGMA_EOL
:
3834 if (!parser
->lexer
->in_pragma
)
3838 /* If we've run out of tokens, stop. */
3842 /* Stop if this is an unnested ';'. */
3847 case CPP_CLOSE_BRACE
:
3848 /* Stop if this is an unnested '}', or closes the outermost
3851 if (nesting_depth
< 0)
3857 case CPP_OPEN_BRACE
:
3866 /* Consume the token. */
3867 cp_lexer_consume_token (parser
->lexer
);
3871 /* Skip tokens until a non-nested closing curly brace is the next
3872 token, or there are no more tokens. Return true in the first case,
3876 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3878 unsigned nesting_depth
= 0;
3882 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3884 switch (token
->type
)
3886 case CPP_PRAGMA_EOL
:
3887 if (!parser
->lexer
->in_pragma
)
3891 /* If we've run out of tokens, stop. */
3894 case CPP_CLOSE_BRACE
:
3895 /* If the next token is a non-nested `}', then we have reached
3896 the end of the current block. */
3897 if (nesting_depth
-- == 0)
3901 case CPP_OPEN_BRACE
:
3902 /* If it the next token is a `{', then we are entering a new
3903 block. Consume the entire block. */
3911 /* Consume the token. */
3912 cp_lexer_consume_token (parser
->lexer
);
3916 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3917 parameter is the PRAGMA token, allowing us to purge the entire pragma
3921 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3925 parser
->lexer
->in_pragma
= false;
3928 token
= cp_lexer_consume_token (parser
->lexer
);
3929 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3931 /* Ensure that the pragma is not parsed again. */
3932 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3935 /* Require pragma end of line, resyncing with it as necessary. The
3936 arguments are as for cp_parser_skip_to_pragma_eol. */
3939 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3941 parser
->lexer
->in_pragma
= false;
3942 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3943 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3946 /* This is a simple wrapper around make_typename_type. When the id is
3947 an unresolved identifier node, we can provide a superior diagnostic
3948 using cp_parser_diagnose_invalid_type_name. */
3951 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3952 location_t id_location
)
3955 if (identifier_p (id
))
3957 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3958 /*complain=*/tf_none
);
3959 if (result
== error_mark_node
)
3960 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3963 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3966 /* This is a wrapper around the
3967 make_{pointer,ptrmem,reference}_declarator functions that decides
3968 which one to call based on the CODE and CLASS_TYPE arguments. The
3969 CODE argument should be one of the values returned by
3970 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3971 appertain to the pointer or reference. */
3973 static cp_declarator
*
3974 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3975 cp_cv_quals cv_qualifiers
,
3976 cp_declarator
*target
,
3979 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3980 return cp_error_declarator
;
3982 if (code
== INDIRECT_REF
)
3983 if (class_type
== NULL_TREE
)
3984 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3986 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3987 target
, attributes
);
3988 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3989 return make_reference_declarator (cv_qualifiers
, target
,
3991 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3992 return make_reference_declarator (cv_qualifiers
, target
,
3997 /* Create a new C++ parser. */
4000 cp_parser_new (void)
4006 /* cp_lexer_new_main is called before doing GC allocation because
4007 cp_lexer_new_main might load a PCH file. */
4008 lexer
= cp_lexer_new_main ();
4010 /* Initialize the binops_by_token so that we can get the tree
4011 directly from the token. */
4012 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
4013 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
4015 parser
= ggc_cleared_alloc
<cp_parser
> ();
4016 parser
->lexer
= lexer
;
4017 parser
->context
= cp_parser_context_new (NULL
);
4019 /* For now, we always accept GNU extensions. */
4020 parser
->allow_gnu_extensions_p
= 1;
4022 /* The `>' token is a greater-than operator, not the end of a
4024 parser
->greater_than_is_operator_p
= true;
4026 parser
->default_arg_ok_p
= true;
4028 /* We are not parsing a constant-expression. */
4029 parser
->integral_constant_expression_p
= false;
4030 parser
->allow_non_integral_constant_expression_p
= false;
4031 parser
->non_integral_constant_expression_p
= false;
4033 /* Local variable names are not forbidden. */
4034 parser
->local_variables_forbidden_p
= 0;
4036 /* We are not processing an `extern "C"' declaration. */
4037 parser
->in_unbraced_linkage_specification_p
= false;
4039 /* We are not processing a declarator. */
4040 parser
->in_declarator_p
= false;
4042 /* We are not processing a template-argument-list. */
4043 parser
->in_template_argument_list_p
= false;
4045 /* We are not in an iteration statement. */
4046 parser
->in_statement
= 0;
4048 /* We are not in a switch statement. */
4049 parser
->in_switch_statement_p
= false;
4051 /* We are not parsing a type-id inside an expression. */
4052 parser
->in_type_id_in_expr_p
= false;
4054 /* String literals should be translated to the execution character set. */
4055 parser
->translate_strings_p
= true;
4057 /* We are not parsing a function body. */
4058 parser
->in_function_body
= false;
4060 /* We can correct until told otherwise. */
4061 parser
->colon_corrects_to_scope_p
= true;
4063 /* The unparsed function queue is empty. */
4064 push_unparsed_function_queues (parser
);
4066 /* There are no classes being defined. */
4067 parser
->num_classes_being_defined
= 0;
4069 /* No template parameters apply. */
4070 parser
->num_template_parameter_lists
= 0;
4072 /* Special parsing data structures. */
4073 parser
->omp_declare_simd
= NULL
;
4074 parser
->oacc_routine
= NULL
;
4076 /* Not declaring an implicit function template. */
4077 parser
->auto_is_implicit_function_template_parm_p
= false;
4078 parser
->fully_implicit_function_template_p
= false;
4079 parser
->implicit_template_parms
= 0;
4080 parser
->implicit_template_scope
= 0;
4082 /* Allow constrained-type-specifiers. */
4083 parser
->prevent_constrained_type_specifiers
= 0;
4085 /* We haven't yet seen an 'extern "C"'. */
4086 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4091 /* Create a cp_lexer structure which will emit the tokens in CACHE
4092 and push it onto the parser's lexer stack. This is used for delayed
4093 parsing of in-class method bodies and default arguments, and should
4094 not be confused with tentative parsing. */
4096 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4098 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4099 lexer
->next
= parser
->lexer
;
4100 parser
->lexer
= lexer
;
4102 /* Move the current source position to that of the first token in the
4104 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4107 /* Pop the top lexer off the parser stack. This is never used for the
4108 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4110 cp_parser_pop_lexer (cp_parser
*parser
)
4112 cp_lexer
*lexer
= parser
->lexer
;
4113 parser
->lexer
= lexer
->next
;
4114 cp_lexer_destroy (lexer
);
4116 /* Put the current source position back where it was before this
4117 lexer was pushed. */
4118 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4121 /* Lexical conventions [gram.lex] */
4123 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4127 cp_parser_identifier (cp_parser
* parser
)
4131 /* Look for the identifier. */
4132 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4133 /* Return the value. */
4135 return cp_expr (token
->u
.value
, token
->location
);
4137 return error_mark_node
;
4140 /* Parse a sequence of adjacent string constants. Returns a
4141 TREE_STRING representing the combined, nul-terminated string
4142 constant. If TRANSLATE is true, translate the string to the
4143 execution character set. If WIDE_OK is true, a wide string is
4146 C++98 [lex.string] says that if a narrow string literal token is
4147 adjacent to a wide string literal token, the behavior is undefined.
4148 However, C99 6.4.5p4 says that this results in a wide string literal.
4149 We follow C99 here, for consistency with the C front end.
4151 This code is largely lifted from lex_string() in c-lex.c.
4153 FUTURE: ObjC++ will need to handle @-strings here. */
4155 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4156 bool lookup_udlit
= true)
4160 struct obstack str_ob
;
4161 struct obstack loc_ob
;
4162 cpp_string str
, istr
, *strs
;
4164 enum cpp_ttype type
, curr_type
;
4165 int have_suffix_p
= 0;
4167 tree suffix_id
= NULL_TREE
;
4168 bool curr_tok_is_userdef_p
= false;
4170 tok
= cp_lexer_peek_token (parser
->lexer
);
4171 if (!cp_parser_is_string_literal (tok
))
4173 cp_parser_error (parser
, "expected string-literal");
4174 return error_mark_node
;
4177 location_t loc
= tok
->location
;
4179 if (cpp_userdef_string_p (tok
->type
))
4181 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4182 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4183 curr_tok_is_userdef_p
= true;
4187 string_tree
= tok
->u
.value
;
4188 curr_type
= tok
->type
;
4192 /* Try to avoid the overhead of creating and destroying an obstack
4193 for the common case of just one string. */
4194 if (!cp_parser_is_string_literal
4195 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4197 cp_lexer_consume_token (parser
->lexer
);
4199 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4200 str
.len
= TREE_STRING_LENGTH (string_tree
);
4203 if (curr_tok_is_userdef_p
)
4205 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4207 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4210 curr_type
= tok
->type
;
4216 location_t last_tok_loc
= tok
->location
;
4217 gcc_obstack_init (&str_ob
);
4218 gcc_obstack_init (&loc_ob
);
4223 cp_lexer_consume_token (parser
->lexer
);
4225 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4226 str
.len
= TREE_STRING_LENGTH (string_tree
);
4228 if (curr_tok_is_userdef_p
)
4230 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4231 if (have_suffix_p
== 0)
4233 suffix_id
= curr_suffix_id
;
4236 else if (have_suffix_p
== 1
4237 && curr_suffix_id
!= suffix_id
)
4239 error ("inconsistent user-defined literal suffixes"
4240 " %qD and %qD in string literal",
4241 suffix_id
, curr_suffix_id
);
4244 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4247 curr_type
= tok
->type
;
4249 if (type
!= curr_type
)
4251 if (type
== CPP_STRING
)
4253 else if (curr_type
!= CPP_STRING
)
4255 rich_location
rich_loc (line_table
, tok
->location
);
4256 rich_loc
.add_range (last_tok_loc
);
4257 error_at (&rich_loc
,
4258 "unsupported non-standard concatenation "
4259 "of string literals");
4263 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4264 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4266 last_tok_loc
= tok
->location
;
4268 tok
= cp_lexer_peek_token (parser
->lexer
);
4269 if (cpp_userdef_string_p (tok
->type
))
4271 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4272 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4273 curr_tok_is_userdef_p
= true;
4277 string_tree
= tok
->u
.value
;
4278 curr_type
= tok
->type
;
4279 curr_tok_is_userdef_p
= false;
4282 while (cp_parser_is_string_literal (tok
));
4284 /* A string literal built by concatenation has its caret=start at
4285 the start of the initial string, and its finish at the finish of
4286 the final string literal. */
4287 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4289 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4292 if (type
!= CPP_STRING
&& !wide_ok
)
4294 cp_parser_error (parser
, "a wide string is invalid in this context");
4298 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4299 (parse_in
, strs
, count
, &istr
, type
))
4301 value
= build_string (istr
.len
, (const char *)istr
.text
);
4302 free (CONST_CAST (unsigned char *, istr
.text
));
4305 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4306 gcc_assert (g_string_concat_db
);
4307 g_string_concat_db
->record_string_concatenation (count
, locs
);
4314 TREE_TYPE (value
) = char_array_type_node
;
4316 case CPP_UTF8STRING
:
4318 TREE_TYPE (value
) = char8_array_type_node
;
4320 TREE_TYPE (value
) = char_array_type_node
;
4323 TREE_TYPE (value
) = char16_array_type_node
;
4326 TREE_TYPE (value
) = char32_array_type_node
;
4329 TREE_TYPE (value
) = wchar_array_type_node
;
4333 value
= fix_string_type (value
);
4337 tree literal
= build_userdef_literal (suffix_id
, value
,
4338 OT_NONE
, NULL_TREE
);
4340 value
= cp_parser_userdef_string_literal (literal
);
4346 /* cpp_interpret_string has issued an error. */
4347 value
= error_mark_node
;
4351 obstack_free (&str_ob
, 0);
4352 obstack_free (&loc_ob
, 0);
4355 return cp_expr (value
, loc
);
4358 /* Look up a literal operator with the name and the exact arguments. */
4361 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4363 tree decl
= lookup_name (name
);
4364 if (!decl
|| !is_overloaded_fn (decl
))
4365 return error_mark_node
;
4367 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4371 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4377 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4378 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4380 tree tparm
= TREE_VALUE (parmtypes
);
4381 tree targ
= TREE_TYPE ((*args
)[ix
]);
4382 bool ptr
= TYPE_PTR_P (tparm
);
4383 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4384 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4386 || !same_type_p (TREE_TYPE (tparm
),
4392 && ix
== vec_safe_length (args
)
4393 /* May be this should be sufficient_parms_p instead,
4394 depending on how exactly should user-defined literals
4395 work in presence of default arguments on the literal
4396 operator parameters. */
4397 && parmtypes
== void_list_node
)
4402 return error_mark_node
;
4405 /* Parse a user-defined char constant. Returns a call to a user-defined
4406 literal operator taking the character as an argument. */
4409 cp_parser_userdef_char_literal (cp_parser
*parser
)
4411 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4412 tree literal
= token
->u
.value
;
4413 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4414 tree value
= USERDEF_LITERAL_VALUE (literal
);
4415 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4418 /* Build up a call to the user-defined operator */
4419 /* Lookup the name we got back from the id-expression. */
4421 vec_safe_push (args
, value
);
4422 decl
= lookup_literal_operator (name
, args
);
4423 if (!decl
|| decl
== error_mark_node
)
4425 error ("unable to find character literal operator %qD with %qT argument",
4426 name
, TREE_TYPE (value
));
4427 return error_mark_node
;
4429 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4433 /* A subroutine of cp_parser_userdef_numeric_literal to
4434 create a char... template parameter pack from a string node. */
4437 make_char_string_pack (tree value
)
4440 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4441 const char *str
= TREE_STRING_POINTER (value
);
4442 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4443 tree argvec
= make_tree_vec (1);
4445 /* Fill in CHARVEC with all of the parameters. */
4446 charvec
= make_tree_vec (len
);
4447 for (i
= 0; i
< len
; ++i
)
4449 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4450 cpp_string in
= { 3, s
};
4451 cpp_string out
= { 0, 0 };
4452 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4454 gcc_assert (out
.len
== 2);
4455 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4459 /* Build the argument packs. */
4460 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4462 TREE_VEC_ELT (argvec
, 0) = argpack
;
4467 /* A subroutine of cp_parser_userdef_numeric_literal to
4468 create a char... template parameter pack from a string node. */
4471 make_string_pack (tree value
)
4474 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4475 const unsigned char *str
4476 = (const unsigned char *) TREE_STRING_POINTER (value
);
4477 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4478 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4479 tree argvec
= make_tree_vec (2);
4481 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4482 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4484 /* First template parm is character type. */
4485 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4487 /* Fill in CHARVEC with all of the parameters. */
4488 charvec
= make_tree_vec (len
);
4489 for (int i
= 0; i
< len
; ++i
)
4490 TREE_VEC_ELT (charvec
, i
)
4491 = double_int_to_tree (str_char_type_node
,
4492 double_int::from_buffer (str
+ i
* sz
, sz
));
4494 /* Build the argument packs. */
4495 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4497 TREE_VEC_ELT (argvec
, 1) = argpack
;
4502 /* Parse a user-defined numeric constant. returns a call to a user-defined
4503 literal operator. */
4506 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4508 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4509 tree literal
= token
->u
.value
;
4510 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4511 tree value
= USERDEF_LITERAL_VALUE (literal
);
4512 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4513 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4514 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4517 /* Look for a literal operator taking the exact type of numeric argument
4518 as the literal value. */
4520 vec_safe_push (args
, value
);
4521 decl
= lookup_literal_operator (name
, args
);
4522 if (decl
&& decl
!= error_mark_node
)
4524 result
= finish_call_expr (decl
, &args
, false, true,
4525 tf_warning_or_error
);
4527 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4529 warning_at (token
->location
, OPT_Woverflow
,
4530 "integer literal exceeds range of %qT type",
4531 long_long_unsigned_type_node
);
4536 warning_at (token
->location
, OPT_Woverflow
,
4537 "floating literal exceeds range of %qT type",
4538 long_double_type_node
);
4539 else if (overflow
< 0)
4540 warning_at (token
->location
, OPT_Woverflow
,
4541 "floating literal truncated to zero");
4547 /* If the numeric argument didn't work, look for a raw literal
4548 operator taking a const char* argument consisting of the number
4549 in string format. */
4551 vec_safe_push (args
, num_string
);
4552 decl
= lookup_literal_operator (name
, args
);
4553 if (decl
&& decl
!= error_mark_node
)
4555 result
= finish_call_expr (decl
, &args
, false, true,
4556 tf_warning_or_error
);
4560 /* If the raw literal didn't work, look for a non-type template
4561 function with parameter pack char.... Call the function with
4562 template parameter characters representing the number. */
4564 decl
= lookup_literal_operator (name
, args
);
4565 if (decl
&& decl
!= error_mark_node
)
4567 tree tmpl_args
= make_char_string_pack (num_string
);
4568 if (tmpl_args
== NULL_TREE
)
4570 error ("failed to translate literal to execution character set %qT",
4572 return error_mark_node
;
4574 decl
= lookup_template_function (decl
, tmpl_args
);
4575 result
= finish_call_expr (decl
, &args
, false, true,
4576 tf_warning_or_error
);
4580 /* In C++14 the standard library defines complex number suffixes that
4581 conflict with GNU extensions. Prefer them if <complex> is #included. */
4582 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4583 bool i14
= (cxx_dialect
> cxx11
4584 && (id_equal (suffix_id
, "i")
4585 || id_equal (suffix_id
, "if")
4586 || id_equal (suffix_id
, "il")));
4587 diagnostic_t kind
= DK_ERROR
;
4592 tree cxlit
= lookup_qualified_name (std_node
, "complex_literals",
4594 if (cxlit
== error_mark_node
)
4596 /* No <complex>, so pedwarn and use GNU semantics. */
4598 opt
= OPT_Wpedantic
;
4603 = emit_diagnostic (kind
, input_location
, opt
,
4604 "unable to find numeric literal operator %qD", name
);
4607 /* Don't inform either. */;
4610 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4611 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4614 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4615 "GNU built-in suffix");
4618 inform (token
->location
, "use %<-fext-numeric-literals%> "
4619 "to enable more built-in suffixes");
4621 if (kind
== DK_ERROR
)
4622 value
= error_mark_node
;
4625 /* Use the built-in semantics. */
4627 if (id_equal (suffix_id
, "i"))
4629 if (TREE_CODE (value
) == INTEGER_CST
)
4630 type
= integer_type_node
;
4632 type
= double_type_node
;
4634 else if (id_equal (suffix_id
, "if"))
4635 type
= float_type_node
;
4636 else /* if (id_equal (suffix_id, "il")) */
4637 type
= long_double_type_node
;
4639 value
= build_complex (build_complex_type (type
),
4640 fold_convert (type
, integer_zero_node
),
4641 fold_convert (type
, value
));
4644 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4645 /* Avoid repeated diagnostics. */
4646 token
->u
.value
= value
;
4650 /* Parse a user-defined string constant. Returns a call to a user-defined
4651 literal operator taking a character pointer and the length of the string
4655 cp_parser_userdef_string_literal (tree literal
)
4657 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4658 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4659 tree value
= USERDEF_LITERAL_VALUE (literal
);
4660 int len
= TREE_STRING_LENGTH (value
)
4661 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4664 /* Build up a call to the user-defined operator. */
4665 /* Lookup the name we got back from the id-expression. */
4667 vec_safe_push (args
, value
);
4668 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4669 decl
= lookup_literal_operator (name
, args
);
4671 if (decl
&& decl
!= error_mark_node
)
4672 return finish_call_expr (decl
, &args
, false, true,
4673 tf_warning_or_error
);
4675 /* Look for a suitable template function, either (C++20) with a single
4676 parameter of class type, or (N3599) with typename parameter CharT and
4677 parameter pack CharT... */
4679 decl
= lookup_literal_operator (name
, args
);
4680 if (decl
&& decl
!= error_mark_node
)
4682 /* Use resolve_nondeduced_context to try to choose one form of template
4684 tree tmpl_args
= make_tree_vec (1);
4685 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4686 decl
= lookup_template_function (decl
, tmpl_args
);
4687 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4692 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4693 res
= resolve_nondeduced_context (decl
, tf_none
);
4697 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4698 TREE_OPERAND (decl
, 1) = tmpl_args
;
4699 return finish_call_expr (decl
, &args
, false, true,
4700 tf_warning_or_error
);
4703 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4704 name
, TREE_TYPE (value
), size_type_node
);
4705 return error_mark_node
;
4709 /* Basic concepts [gram.basic] */
4711 /* Parse a translation-unit.
4714 declaration-seq [opt] */
4717 cp_parser_translation_unit (cp_parser
* parser
)
4719 gcc_checking_assert (!cp_error_declarator
);
4721 /* Create the declarator obstack. */
4722 gcc_obstack_init (&declarator_obstack
);
4723 /* Create the error declarator. */
4724 cp_error_declarator
= make_declarator (cdk_error
);
4725 /* Create the empty parameter list. */
4726 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4728 /* Remember where the base of the declarator obstack lies. */
4729 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4731 bool implicit_extern_c
= false;
4735 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4737 /* If we're entering or exiting a region that's implicitly
4738 extern "C", modify the lang context appropriately. */
4739 if (implicit_extern_c
4740 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4742 implicit_extern_c
= !implicit_extern_c
;
4743 if (implicit_extern_c
)
4744 push_lang_context (lang_name_c
);
4746 pop_lang_context ();
4749 if (token
->type
== CPP_EOF
)
4752 if (token
->type
== CPP_CLOSE_BRACE
)
4754 cp_parser_error (parser
, "expected declaration");
4755 cp_lexer_consume_token (parser
->lexer
);
4756 /* If the next token is now a `;', consume it. */
4757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4758 cp_lexer_consume_token (parser
->lexer
);
4761 cp_parser_toplevel_declaration (parser
);
4764 /* Get rid of the token array; we don't need it any more. */
4765 cp_lexer_destroy (parser
->lexer
);
4766 parser
->lexer
= NULL
;
4768 /* The EOF should have reset this. */
4769 gcc_checking_assert (!implicit_extern_c
);
4771 /* Make sure the declarator obstack was fully cleaned up. */
4772 gcc_assert (obstack_next_free (&declarator_obstack
)
4773 == declarator_obstack_base
);
4776 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4777 decltype context. */
4779 static inline tsubst_flags_t
4780 complain_flags (bool decltype_p
)
4782 tsubst_flags_t complain
= tf_warning_or_error
;
4784 complain
|= tf_decltype
;
4788 /* We're about to parse a collection of statements. If we're currently
4789 parsing tentatively, set up a firewall so that any nested
4790 cp_parser_commit_to_tentative_parse won't affect the current context. */
4792 static cp_token_position
4793 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4795 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4798 cp_parser_parse_tentatively (parser
);
4799 cp_parser_commit_to_topmost_tentative_parse (parser
);
4800 return cp_lexer_token_position (parser
->lexer
, false);
4803 /* We've finished parsing the collection of statements. Wrap up the
4804 firewall and replace the relevant tokens with the parsed form. */
4807 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4813 /* Finish the firewall level. */
4814 cp_parser_parse_definitely (parser
);
4815 /* And remember the result of the parse for when we try again. */
4816 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4817 token
->type
= CPP_PREPARSED_EXPR
;
4818 token
->u
.value
= expr
;
4819 token
->keyword
= RID_MAX
;
4820 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4823 /* Like the above functions, but let the user modify the tokens. Used by
4824 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4825 later parses, so it makes sense to localize the effects of
4826 cp_parser_commit_to_tentative_parse. */
4828 struct tentative_firewall
4833 tentative_firewall (cp_parser
*p
): parser(p
)
4835 /* If we're currently parsing tentatively, start a committed level as a
4836 firewall and then an inner tentative parse. */
4837 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4839 cp_parser_parse_tentatively (parser
);
4840 cp_parser_commit_to_topmost_tentative_parse (parser
);
4841 cp_parser_parse_tentatively (parser
);
4845 ~tentative_firewall()
4849 /* Finish the inner tentative parse and the firewall, propagating any
4850 uncommitted error state to the outer tentative parse. */
4851 bool err
= cp_parser_error_occurred (parser
);
4852 cp_parser_parse_definitely (parser
);
4853 cp_parser_parse_definitely (parser
);
4855 cp_parser_simulate_error (parser
);
4860 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4861 This class is for tracking such a matching pair of symbols.
4862 In particular, it tracks the location of the first token,
4863 so that if the second token is missing, we can highlight the
4864 location of the first token when notifying the user about the
4867 template <typename traits_t
>
4871 /* token_pair's ctor. */
4872 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4874 /* If the next token is the opening symbol for this pair, consume it and
4876 Otherwise, issue an error and return false.
4877 In either case, record the location of the opening token. */
4879 bool require_open (cp_parser
*parser
)
4881 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4882 return cp_parser_require (parser
, traits_t::open_token_type
,
4883 traits_t::required_token_open
);
4886 /* Consume the next token from PARSER, recording its location as
4887 that of the opening token within the pair. */
4889 cp_token
* consume_open (cp_parser
*parser
)
4891 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4892 gcc_assert (tok
->type
== traits_t::open_token_type
);
4893 m_open_loc
= tok
->location
;
4897 /* If the next token is the closing symbol for this pair, consume it
4899 Otherwise, issue an error, highlighting the location of the
4900 corresponding opening token, and return NULL. */
4902 cp_token
*require_close (cp_parser
*parser
) const
4904 return cp_parser_require (parser
, traits_t::close_token_type
,
4905 traits_t::required_token_close
,
4909 location_t
open_location () const { return m_open_loc
; }
4912 location_t m_open_loc
;
4915 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4917 struct matching_paren_traits
4919 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4920 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4921 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4922 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4925 /* "matching_parens" is a token_pair<T> class for tracking matching
4926 pairs of parentheses. */
4928 typedef token_pair
<matching_paren_traits
> matching_parens
;
4930 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4932 struct matching_brace_traits
4934 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4935 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4936 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4937 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4940 /* "matching_braces" is a token_pair<T> class for tracking matching
4943 typedef token_pair
<matching_brace_traits
> matching_braces
;
4946 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4947 enclosing parentheses. */
4950 cp_parser_statement_expr (cp_parser
*parser
)
4952 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4954 /* Consume the '('. */
4955 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4956 matching_parens parens
;
4957 parens
.consume_open (parser
);
4958 /* Start the statement-expression. */
4959 tree expr
= begin_stmt_expr ();
4960 /* Parse the compound-statement. */
4961 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4963 expr
= finish_stmt_expr (expr
, false);
4964 /* Consume the ')'. */
4965 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4966 if (!parens
.require_close (parser
))
4967 cp_parser_skip_to_end_of_statement (parser
);
4969 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4970 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4971 return cp_expr (expr
, combined_loc
);
4974 /* Expressions [gram.expr] */
4976 /* Parse a fold-operator.
4979 - * / % ^ & | = < > << >>
4980 = -= *= /= %= ^= &= |= <<= >>=
4981 == != <= >= && || , .* ->*
4983 This returns the tree code corresponding to the matched operator
4984 as an int. When the current token matches a compound assignment
4985 operator, the resulting tree code is the negative value of the
4986 non-assignment operator. */
4989 cp_parser_fold_operator (cp_token
*token
)
4991 switch (token
->type
)
4993 case CPP_PLUS
: return PLUS_EXPR
;
4994 case CPP_MINUS
: return MINUS_EXPR
;
4995 case CPP_MULT
: return MULT_EXPR
;
4996 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4997 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4998 case CPP_XOR
: return BIT_XOR_EXPR
;
4999 case CPP_AND
: return BIT_AND_EXPR
;
5000 case CPP_OR
: return BIT_IOR_EXPR
;
5001 case CPP_LSHIFT
: return LSHIFT_EXPR
;
5002 case CPP_RSHIFT
: return RSHIFT_EXPR
;
5004 case CPP_EQ
: return -NOP_EXPR
;
5005 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
5006 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
5007 case CPP_MULT_EQ
: return -MULT_EXPR
;
5008 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
5009 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
5010 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
5011 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
5012 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
5013 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
5014 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
5016 case CPP_EQ_EQ
: return EQ_EXPR
;
5017 case CPP_NOT_EQ
: return NE_EXPR
;
5018 case CPP_LESS
: return LT_EXPR
;
5019 case CPP_GREATER
: return GT_EXPR
;
5020 case CPP_LESS_EQ
: return LE_EXPR
;
5021 case CPP_GREATER_EQ
: return GE_EXPR
;
5023 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
5024 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
5026 case CPP_COMMA
: return COMPOUND_EXPR
;
5028 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
5029 case CPP_DEREF_STAR
: return MEMBER_REF
;
5031 default: return ERROR_MARK
;
5035 /* Returns true if CODE indicates a binary expression, which is not allowed in
5036 the LHS of a fold-expression. More codes will need to be added to use this
5037 function in other contexts. */
5040 is_binary_op (tree_code code
)
5045 case POINTER_PLUS_EXPR
:
5048 case TRUNC_DIV_EXPR
:
5049 case TRUNC_MOD_EXPR
:
5065 case TRUTH_ANDIF_EXPR
:
5066 case TRUTH_ORIF_EXPR
:
5079 /* If the next token is a suitable fold operator, consume it and return as
5080 the function above. */
5083 cp_parser_fold_operator (cp_parser
*parser
)
5085 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5086 int code
= cp_parser_fold_operator (token
);
5087 if (code
!= ERROR_MARK
)
5088 cp_lexer_consume_token (parser
->lexer
);
5092 /* Parse a fold-expression.
5095 ( ... folding-operator cast-expression)
5096 ( cast-expression folding-operator ... )
5097 ( cast-expression folding operator ... folding-operator cast-expression)
5099 Note that the '(' and ')' are matched in primary expression. */
5102 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5107 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5109 cp_lexer_consume_token (parser
->lexer
);
5110 int op
= cp_parser_fold_operator (parser
);
5111 if (op
== ERROR_MARK
)
5113 cp_parser_error (parser
, "expected binary operator");
5114 return error_mark_node
;
5117 tree expr
= cp_parser_cast_expression (parser
, false, false,
5119 if (expr
== error_mark_node
)
5120 return error_mark_node
;
5121 return finish_left_unary_fold_expr (expr
, op
);
5124 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5125 int op
= cp_parser_fold_operator (parser
);
5126 if (op
== ERROR_MARK
)
5128 cp_parser_error (parser
, "expected binary operator");
5129 return error_mark_node
;
5132 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5134 cp_parser_error (parser
, "expected ...");
5135 return error_mark_node
;
5137 cp_lexer_consume_token (parser
->lexer
);
5139 /* The operands of a fold-expression are cast-expressions, so binary or
5140 conditional expressions are not allowed. We check this here to avoid
5141 tentative parsing. */
5142 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5143 /* OK, the expression was parenthesized. */;
5144 else if (is_binary_op (TREE_CODE (expr1
)))
5145 error_at (location_of (expr1
),
5146 "binary expression in operand of fold-expression");
5147 else if (TREE_CODE (expr1
) == COND_EXPR
5148 || (REFERENCE_REF_P (expr1
)
5149 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5150 error_at (location_of (expr1
),
5151 "conditional expression in operand of fold-expression");
5154 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5155 return finish_right_unary_fold_expr (expr1
, op
);
5157 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5159 cp_parser_error (parser
, "mismatched operator in fold-expression");
5160 return error_mark_node
;
5162 cp_lexer_consume_token (parser
->lexer
);
5164 // Binary left or right fold.
5165 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5166 if (expr2
== error_mark_node
)
5167 return error_mark_node
;
5168 return finish_binary_fold_expr (expr1
, expr2
, op
);
5171 /* Parse a primary-expression.
5178 lambda-expression (C++11)
5183 ( compound-statement )
5184 __builtin_va_arg ( assignment-expression , type-id )
5185 __builtin_offsetof ( type-id , offsetof-expression )
5188 __has_nothrow_assign ( type-id )
5189 __has_nothrow_constructor ( type-id )
5190 __has_nothrow_copy ( type-id )
5191 __has_trivial_assign ( type-id )
5192 __has_trivial_constructor ( type-id )
5193 __has_trivial_copy ( type-id )
5194 __has_trivial_destructor ( type-id )
5195 __has_virtual_destructor ( type-id )
5196 __is_abstract ( type-id )
5197 __is_base_of ( type-id , type-id )
5198 __is_class ( type-id )
5199 __is_empty ( type-id )
5200 __is_enum ( type-id )
5201 __is_final ( type-id )
5202 __is_literal_type ( type-id )
5203 __is_pod ( type-id )
5204 __is_polymorphic ( type-id )
5205 __is_std_layout ( type-id )
5206 __is_trivial ( type-id )
5207 __is_union ( type-id )
5209 Objective-C++ Extension:
5217 ADDRESS_P is true iff this expression was immediately preceded by
5218 "&" and therefore might denote a pointer-to-member. CAST_P is true
5219 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5220 true iff this expression is a template argument.
5222 Returns a representation of the expression. Upon return, *IDK
5223 indicates what kind of id-expression (if any) was present. */
5226 cp_parser_primary_expression (cp_parser
*parser
,
5229 bool template_arg_p
,
5233 cp_token
*token
= NULL
;
5235 /* Assume the primary expression is not an id-expression. */
5236 *idk
= CP_ID_KIND_NONE
;
5238 /* Peek at the next token. */
5239 token
= cp_lexer_peek_token (parser
->lexer
);
5240 switch ((int) token
->type
)
5249 user-defined-literal */
5256 case CPP_PREPARSED_EXPR
:
5257 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5258 return cp_parser_userdef_numeric_literal (parser
);
5259 token
= cp_lexer_consume_token (parser
->lexer
);
5260 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5262 error_at (token
->location
,
5263 "fixed-point types not supported in C++");
5264 return error_mark_node
;
5266 /* Floating-point literals are only allowed in an integral
5267 constant expression if they are cast to an integral or
5268 enumeration type. */
5269 if (TREE_CODE (token
->u
.value
) == REAL_CST
5270 && parser
->integral_constant_expression_p
5273 /* CAST_P will be set even in invalid code like "int(2.7 +
5274 ...)". Therefore, we have to check that the next token
5275 is sure to end the cast. */
5278 cp_token
*next_token
;
5280 next_token
= cp_lexer_peek_token (parser
->lexer
);
5281 if (/* The comma at the end of an
5282 enumerator-definition. */
5283 next_token
->type
!= CPP_COMMA
5284 /* The curly brace at the end of an enum-specifier. */
5285 && next_token
->type
!= CPP_CLOSE_BRACE
5286 /* The end of a statement. */
5287 && next_token
->type
!= CPP_SEMICOLON
5288 /* The end of the cast-expression. */
5289 && next_token
->type
!= CPP_CLOSE_PAREN
5290 /* The end of an array bound. */
5291 && next_token
->type
!= CPP_CLOSE_SQUARE
5292 /* The closing ">" in a template-argument-list. */
5293 && (next_token
->type
!= CPP_GREATER
5294 || parser
->greater_than_is_operator_p
)
5295 /* C++0x only: A ">>" treated like two ">" tokens,
5296 in a template-argument-list. */
5297 && (next_token
->type
!= CPP_RSHIFT
5298 || (cxx_dialect
== cxx98
)
5299 || parser
->greater_than_is_operator_p
))
5303 /* If we are within a cast, then the constraint that the
5304 cast is to an integral or enumeration type will be
5305 checked at that point. If we are not within a cast, then
5306 this code is invalid. */
5308 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5310 return (cp_expr (token
->u
.value
, token
->location
)
5311 .maybe_add_location_wrapper ());
5313 case CPP_CHAR_USERDEF
:
5314 case CPP_CHAR16_USERDEF
:
5315 case CPP_CHAR32_USERDEF
:
5316 case CPP_WCHAR_USERDEF
:
5317 case CPP_UTF8CHAR_USERDEF
:
5318 return cp_parser_userdef_char_literal (parser
);
5324 case CPP_UTF8STRING
:
5325 case CPP_STRING_USERDEF
:
5326 case CPP_STRING16_USERDEF
:
5327 case CPP_STRING32_USERDEF
:
5328 case CPP_WSTRING_USERDEF
:
5329 case CPP_UTF8STRING_USERDEF
:
5330 /* ??? Should wide strings be allowed when parser->translate_strings_p
5331 is false (i.e. in attributes)? If not, we can kill the third
5332 argument to cp_parser_string_literal. */
5333 return (cp_parser_string_literal (parser
,
5334 parser
->translate_strings_p
,
5336 .maybe_add_location_wrapper ());
5338 case CPP_OPEN_PAREN
:
5339 /* If we see `( { ' then we are looking at the beginning of
5340 a GNU statement-expression. */
5341 if (cp_parser_allow_gnu_extensions_p (parser
)
5342 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5344 /* Statement-expressions are not allowed by the standard. */
5345 pedwarn (token
->location
, OPT_Wpedantic
,
5346 "ISO C++ forbids braced-groups within expressions");
5348 /* And they're not allowed outside of a function-body; you
5349 cannot, for example, write:
5351 int i = ({ int j = 3; j + 1; });
5353 at class or namespace scope. */
5354 if (!parser
->in_function_body
5355 || parser
->in_template_argument_list_p
)
5357 error_at (token
->location
,
5358 "statement-expressions are not allowed outside "
5359 "functions nor in template-argument lists");
5360 cp_parser_skip_to_end_of_block_or_statement (parser
);
5361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5362 cp_lexer_consume_token (parser
->lexer
);
5363 return error_mark_node
;
5366 return cp_parser_statement_expr (parser
);
5368 /* Otherwise it's a normal parenthesized expression. */
5371 bool saved_greater_than_is_operator_p
;
5373 location_t open_paren_loc
= token
->location
;
5375 /* Consume the `('. */
5376 matching_parens parens
;
5377 parens
.consume_open (parser
);
5378 /* Within a parenthesized expression, a `>' token is always
5379 the greater-than operator. */
5380 saved_greater_than_is_operator_p
5381 = parser
->greater_than_is_operator_p
;
5382 parser
->greater_than_is_operator_p
= true;
5384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5385 /* Left fold expression. */
5388 /* Parse the parenthesized expression. */
5389 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5391 token
= cp_lexer_peek_token (parser
->lexer
);
5392 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5394 expr
= cp_parser_fold_expression (parser
, expr
);
5395 if (expr
!= error_mark_node
5396 && cxx_dialect
< cxx17
)
5397 pedwarn (input_location
, 0, "fold-expressions only available "
5398 "with %<-std=c++17%> or %<-std=gnu++17%>");
5401 /* Let the front end know that this expression was
5402 enclosed in parentheses. This matters in case, for
5403 example, the expression is of the form `A::B', since
5404 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5406 expr
= finish_parenthesized_expr (expr
);
5408 /* DR 705: Wrapping an unqualified name in parentheses
5409 suppresses arg-dependent lookup. We want to pass back
5410 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5411 (c++/37862), but none of the others. */
5412 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5413 *idk
= CP_ID_KIND_NONE
;
5415 /* The `>' token might be the end of a template-id or
5416 template-parameter-list now. */
5417 parser
->greater_than_is_operator_p
5418 = saved_greater_than_is_operator_p
;
5420 /* Consume the `)'. */
5421 token
= cp_lexer_peek_token (parser
->lexer
);
5422 location_t close_paren_loc
= token
->location
;
5423 expr
.set_range (open_paren_loc
, close_paren_loc
);
5424 if (!parens
.require_close (parser
)
5425 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5426 cp_parser_skip_to_end_of_statement (parser
);
5431 case CPP_OPEN_SQUARE
:
5433 if (c_dialect_objc ())
5435 /* We might have an Objective-C++ message. */
5436 cp_parser_parse_tentatively (parser
);
5437 tree msg
= cp_parser_objc_message_expression (parser
);
5438 /* If that works out, we're done ... */
5439 if (cp_parser_parse_definitely (parser
))
5441 /* ... else, fall though to see if it's a lambda. */
5443 cp_expr lam
= cp_parser_lambda_expression (parser
);
5444 /* Don't warn about a failed tentative parse. */
5445 if (cp_parser_error_occurred (parser
))
5446 return error_mark_node
;
5447 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5451 case CPP_OBJC_STRING
:
5452 if (c_dialect_objc ())
5453 /* We have an Objective-C++ string literal. */
5454 return cp_parser_objc_expression (parser
);
5455 cp_parser_error (parser
, "expected primary-expression");
5456 return error_mark_node
;
5459 switch (token
->keyword
)
5461 /* These two are the boolean literals. */
5463 cp_lexer_consume_token (parser
->lexer
);
5464 return cp_expr (boolean_true_node
, token
->location
);
5466 cp_lexer_consume_token (parser
->lexer
);
5467 return cp_expr (boolean_false_node
, token
->location
);
5469 /* The `__null' literal. */
5471 cp_lexer_consume_token (parser
->lexer
);
5472 return cp_expr (null_node
, token
->location
);
5474 /* The `nullptr' literal. */
5476 cp_lexer_consume_token (parser
->lexer
);
5477 return cp_expr (nullptr_node
, token
->location
);
5479 /* Recognize the `this' keyword. */
5481 cp_lexer_consume_token (parser
->lexer
);
5482 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5484 error_at (token
->location
,
5485 "%<this%> may not be used in this context");
5486 return error_mark_node
;
5488 /* Pointers cannot appear in constant-expressions. */
5489 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5490 return error_mark_node
;
5491 return cp_expr (finish_this_expr (), token
->location
);
5493 /* The `operator' keyword can be the beginning of an
5498 case RID_FUNCTION_NAME
:
5499 case RID_PRETTY_FUNCTION_NAME
:
5500 case RID_C99_FUNCTION_NAME
:
5502 non_integral_constant name
;
5504 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5505 __func__ are the names of variables -- but they are
5506 treated specially. Therefore, they are handled here,
5507 rather than relying on the generic id-expression logic
5508 below. Grammatically, these names are id-expressions.
5510 Consume the token. */
5511 token
= cp_lexer_consume_token (parser
->lexer
);
5513 switch (token
->keyword
)
5515 case RID_FUNCTION_NAME
:
5516 name
= NIC_FUNC_NAME
;
5518 case RID_PRETTY_FUNCTION_NAME
:
5519 name
= NIC_PRETTY_FUNC
;
5521 case RID_C99_FUNCTION_NAME
:
5522 name
= NIC_C99_FUNC
;
5528 if (cp_parser_non_integral_constant_expression (parser
, name
))
5529 return error_mark_node
;
5531 /* Look up the name. */
5532 return finish_fname (token
->u
.value
);
5539 location_t type_location
;
5540 location_t start_loc
5541 = cp_lexer_peek_token (parser
->lexer
)->location
;
5542 /* The `__builtin_va_arg' construct is used to handle
5543 `va_arg'. Consume the `__builtin_va_arg' token. */
5544 cp_lexer_consume_token (parser
->lexer
);
5545 /* Look for the opening `('. */
5546 matching_parens parens
;
5547 parens
.require_open (parser
);
5548 /* Now, parse the assignment-expression. */
5549 expression
= cp_parser_assignment_expression (parser
);
5550 /* Look for the `,'. */
5551 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5552 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5553 /* Parse the type-id. */
5555 type_id_in_expr_sentinel
s (parser
);
5556 type
= cp_parser_type_id (parser
);
5558 /* Look for the closing `)'. */
5559 location_t finish_loc
5560 = cp_lexer_peek_token (parser
->lexer
)->location
;
5561 parens
.require_close (parser
);
5562 /* Using `va_arg' in a constant-expression is not
5564 if (cp_parser_non_integral_constant_expression (parser
,
5566 return error_mark_node
;
5567 /* Construct a location of the form:
5568 __builtin_va_arg (v, int)
5569 ~~~~~~~~~~~~~~~~~~~~~^~~~
5570 with the caret at the type, ranging from the start of the
5571 "__builtin_va_arg" token to the close paren. */
5572 location_t combined_loc
5573 = make_location (type_location
, start_loc
, finish_loc
);
5574 return build_x_va_arg (combined_loc
, expression
, type
);
5578 return cp_parser_builtin_offsetof (parser
);
5580 case RID_HAS_NOTHROW_ASSIGN
:
5581 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5582 case RID_HAS_NOTHROW_COPY
:
5583 case RID_HAS_TRIVIAL_ASSIGN
:
5584 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5585 case RID_HAS_TRIVIAL_COPY
:
5586 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5587 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5588 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5589 case RID_IS_ABSTRACT
:
5590 case RID_IS_AGGREGATE
:
5591 case RID_IS_BASE_OF
:
5596 case RID_IS_LITERAL_TYPE
:
5598 case RID_IS_POLYMORPHIC
:
5599 case RID_IS_SAME_AS
:
5600 case RID_IS_STD_LAYOUT
:
5601 case RID_IS_TRIVIAL
:
5602 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5603 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5604 case RID_IS_TRIVIALLY_COPYABLE
:
5606 case RID_IS_ASSIGNABLE
:
5607 case RID_IS_CONSTRUCTIBLE
:
5608 return cp_parser_trait_expr (parser
, token
->keyword
);
5612 return cp_parser_requires_expression (parser
);
5614 /* Objective-C++ expressions. */
5616 case RID_AT_PROTOCOL
:
5617 case RID_AT_SELECTOR
:
5618 return cp_parser_objc_expression (parser
);
5621 if (parser
->in_function_body
5622 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5625 error_at (token
->location
,
5626 "a template declaration cannot appear at block scope");
5627 cp_parser_skip_to_end_of_block_or_statement (parser
);
5628 return error_mark_node
;
5632 cp_parser_error (parser
, "expected primary-expression");
5633 return error_mark_node
;
5636 /* An id-expression can start with either an identifier, a
5637 `::' as the beginning of a qualified-id, or the "operator"
5641 case CPP_TEMPLATE_ID
:
5642 case CPP_NESTED_NAME_SPECIFIER
:
5645 cp_expr id_expression
;
5647 const char *error_msg
;
5650 cp_token
*id_expr_token
;
5652 /* Parse the id-expression. */
5654 = cp_parser_id_expression (parser
,
5655 /*template_keyword_p=*/false,
5656 /*check_dependency_p=*/true,
5658 /*declarator_p=*/false,
5659 /*optional_p=*/false);
5660 if (id_expression
== error_mark_node
)
5661 return error_mark_node
;
5662 id_expr_token
= token
;
5663 token
= cp_lexer_peek_token (parser
->lexer
);
5664 done
= (token
->type
!= CPP_OPEN_SQUARE
5665 && token
->type
!= CPP_OPEN_PAREN
5666 && token
->type
!= CPP_DOT
5667 && token
->type
!= CPP_DEREF
5668 && token
->type
!= CPP_PLUS_PLUS
5669 && token
->type
!= CPP_MINUS_MINUS
);
5670 /* If we have a template-id, then no further lookup is
5671 required. If the template-id was for a template-class, we
5672 will sometimes have a TYPE_DECL at this point. */
5673 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5674 || TREE_CODE (id_expression
) == TYPE_DECL
)
5675 decl
= id_expression
;
5676 /* Look up the name. */
5679 tree ambiguous_decls
;
5681 /* If we already know that this lookup is ambiguous, then
5682 we've already issued an error message; there's no reason
5684 if (id_expr_token
->type
== CPP_NAME
5685 && id_expr_token
->error_reported
)
5687 cp_parser_simulate_error (parser
);
5688 return error_mark_node
;
5691 decl
= cp_parser_lookup_name (parser
, id_expression
,
5694 /*is_namespace=*/false,
5695 /*check_dependency=*/true,
5697 id_expression
.get_location ());
5698 /* If the lookup was ambiguous, an error will already have
5700 if (ambiguous_decls
)
5701 return error_mark_node
;
5703 /* In Objective-C++, we may have an Objective-C 2.0
5704 dot-syntax for classes here. */
5705 if (c_dialect_objc ()
5706 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5707 && TREE_CODE (decl
) == TYPE_DECL
5708 && objc_is_class_name (decl
))
5711 cp_lexer_consume_token (parser
->lexer
);
5712 component
= cp_parser_identifier (parser
);
5713 if (component
== error_mark_node
)
5714 return error_mark_node
;
5716 tree result
= objc_build_class_component_ref (id_expression
,
5718 /* Build a location of the form:
5721 with caret at the start of the component name (at
5722 input_location), ranging from the start of the id_expression
5723 to the end of the component name. */
5724 location_t combined_loc
5725 = make_location (input_location
, id_expression
.get_start (),
5726 get_finish (input_location
));
5727 protected_set_expr_location (result
, combined_loc
);
5731 /* In Objective-C++, an instance variable (ivar) may be preferred
5732 to whatever cp_parser_lookup_name() found.
5733 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5734 rest of c-family, we have to do a little extra work to preserve
5735 any location information in cp_expr "decl". Given that
5736 objc_lookup_ivar is implemented in "c-family" and "objc", we
5737 have a trip through the pure "tree" type, rather than cp_expr.
5738 Naively copying it back to "decl" would implicitly give the
5739 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5740 store an EXPR_LOCATION. Hence we only update "decl" (and
5741 hence its location_t) if we get back a different tree node. */
5742 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5744 if (decl_tree
!= decl
.get_value ())
5745 decl
= cp_expr (decl_tree
);
5747 /* If name lookup gives us a SCOPE_REF, then the
5748 qualifying scope was dependent. */
5749 if (TREE_CODE (decl
) == SCOPE_REF
)
5751 /* At this point, we do not know if DECL is a valid
5752 integral constant expression. We assume that it is
5753 in fact such an expression, so that code like:
5755 template <int N> struct A {
5759 is accepted. At template-instantiation time, we
5760 will check that B<N>::i is actually a constant. */
5763 /* Check to see if DECL is a local variable in a context
5764 where that is forbidden. */
5765 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5766 && local_variable_p (decl
))
5768 error_at (id_expression
.get_location (),
5769 "local variable %qD may not appear in this context",
5771 return error_mark_node
;
5775 decl
= (finish_id_expression
5776 (id_expression
, decl
, parser
->scope
,
5778 parser
->integral_constant_expression_p
,
5779 parser
->allow_non_integral_constant_expression_p
,
5780 &parser
->non_integral_constant_expression_p
,
5781 template_p
, done
, address_p
,
5784 id_expression
.get_location ()));
5786 cp_parser_error (parser
, error_msg
);
5787 /* Build a location for an id-expression of the form:
5793 i.e. from the start of the first token to the end of the final
5794 token, with the caret at the start of the unqualified-id. */
5795 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5796 location_t start_loc
= get_start (id_expr_token
->location
);
5797 location_t finish_loc
= get_finish (id_expression
.get_location ());
5798 location_t combined_loc
5799 = make_location (caret_loc
, start_loc
, finish_loc
);
5801 decl
.set_location (combined_loc
);
5805 /* Anything else is an error. */
5807 cp_parser_error (parser
, "expected primary-expression");
5808 return error_mark_node
;
5812 static inline cp_expr
5813 cp_parser_primary_expression (cp_parser
*parser
,
5816 bool template_arg_p
,
5819 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5820 /*decltype*/false, idk
);
5823 /* Parse an id-expression.
5830 :: [opt] nested-name-specifier template [opt] unqualified-id
5832 :: operator-function-id
5835 Return a representation of the unqualified portion of the
5836 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5837 a `::' or nested-name-specifier.
5839 Often, if the id-expression was a qualified-id, the caller will
5840 want to make a SCOPE_REF to represent the qualified-id. This
5841 function does not do this in order to avoid wastefully creating
5842 SCOPE_REFs when they are not required.
5844 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5847 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5848 uninstantiated templates.
5850 If *TEMPLATE_P is non-NULL, it is set to true iff the
5851 `template' keyword is used to explicitly indicate that the entity
5852 named is a template.
5854 If DECLARATOR_P is true, the id-expression is appearing as part of
5855 a declarator, rather than as part of an expression. */
5858 cp_parser_id_expression (cp_parser
*parser
,
5859 bool template_keyword_p
,
5860 bool check_dependency_p
,
5865 bool global_scope_p
;
5866 bool nested_name_specifier_p
;
5868 /* Assume the `template' keyword was not used. */
5870 *template_p
= template_keyword_p
;
5872 /* Look for the optional `::' operator. */
5874 = (!template_keyword_p
5875 && (cp_parser_global_scope_opt (parser
,
5876 /*current_scope_valid_p=*/false)
5879 /* Look for the optional nested-name-specifier. */
5880 nested_name_specifier_p
5881 = (cp_parser_nested_name_specifier_opt (parser
,
5882 /*typename_keyword_p=*/false,
5889 /* If there is a nested-name-specifier, then we are looking at
5890 the first qualified-id production. */
5891 if (nested_name_specifier_p
)
5894 tree saved_object_scope
;
5895 tree saved_qualifying_scope
;
5896 cp_expr unqualified_id
;
5899 /* See if the next token is the `template' keyword. */
5901 template_p
= &is_template
;
5902 *template_p
= cp_parser_optional_template_keyword (parser
);
5903 /* Name lookup we do during the processing of the
5904 unqualified-id might obliterate SCOPE. */
5905 saved_scope
= parser
->scope
;
5906 saved_object_scope
= parser
->object_scope
;
5907 saved_qualifying_scope
= parser
->qualifying_scope
;
5908 /* Process the final unqualified-id. */
5909 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5912 /*optional_p=*/false);
5913 /* Restore the SAVED_SCOPE for our caller. */
5914 parser
->scope
= saved_scope
;
5915 parser
->object_scope
= saved_object_scope
;
5916 parser
->qualifying_scope
= saved_qualifying_scope
;
5918 return unqualified_id
;
5920 /* Otherwise, if we are in global scope, then we are looking at one
5921 of the other qualified-id productions. */
5922 else if (global_scope_p
)
5927 /* Peek at the next token. */
5928 token
= cp_lexer_peek_token (parser
->lexer
);
5930 /* If it's an identifier, and the next token is not a "<", then
5931 we can avoid the template-id case. This is an optimization
5932 for this common case. */
5933 if (token
->type
== CPP_NAME
5934 && !cp_parser_nth_token_starts_template_argument_list_p
5936 return cp_parser_identifier (parser
);
5938 cp_parser_parse_tentatively (parser
);
5939 /* Try a template-id. */
5940 id
= cp_parser_template_id_expr (parser
,
5941 /*template_keyword_p=*/false,
5942 /*check_dependency_p=*/true,
5944 /* If that worked, we're done. */
5945 if (cp_parser_parse_definitely (parser
))
5948 /* Peek at the next token. (Changes in the token buffer may
5949 have invalidated the pointer obtained above.) */
5950 token
= cp_lexer_peek_token (parser
->lexer
);
5952 switch (token
->type
)
5955 return cp_parser_identifier (parser
);
5958 if (token
->keyword
== RID_OPERATOR
)
5959 return cp_parser_operator_function_id (parser
);
5963 cp_parser_error (parser
, "expected id-expression");
5964 return error_mark_node
;
5968 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5969 /*check_dependency_p=*/true,
5974 /* Parse an unqualified-id.
5978 operator-function-id
5979 conversion-function-id
5983 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5984 keyword, in a construct like `A::template ...'.
5986 Returns a representation of unqualified-id. For the `identifier'
5987 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5988 production a BIT_NOT_EXPR is returned; the operand of the
5989 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5990 other productions, see the documentation accompanying the
5991 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5992 names are looked up in uninstantiated templates. If DECLARATOR_P
5993 is true, the unqualified-id is appearing as part of a declarator,
5994 rather than as part of an expression. */
5997 cp_parser_unqualified_id (cp_parser
* parser
,
5998 bool template_keyword_p
,
5999 bool check_dependency_p
,
6005 /* Peek at the next token. */
6006 token
= cp_lexer_peek_token (parser
->lexer
);
6008 switch ((int) token
->type
)
6014 /* We don't know yet whether or not this will be a
6016 cp_parser_parse_tentatively (parser
);
6017 /* Try a template-id. */
6018 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6021 /* If it worked, we're done. */
6022 if (cp_parser_parse_definitely (parser
))
6024 /* Otherwise, it's an ordinary identifier. */
6025 return cp_parser_identifier (parser
);
6028 case CPP_TEMPLATE_ID
:
6029 return cp_parser_template_id_expr (parser
, template_keyword_p
,
6036 tree qualifying_scope
;
6040 location_t tilde_loc
= token
->location
;
6042 /* Consume the `~' token. */
6043 cp_lexer_consume_token (parser
->lexer
);
6044 /* Parse the class-name. The standard, as written, seems to
6047 template <typename T> struct S { ~S (); };
6048 template <typename T> S<T>::~S() {}
6050 is invalid, since `~' must be followed by a class-name, but
6051 `S<T>' is dependent, and so not known to be a class.
6052 That's not right; we need to look in uninstantiated
6053 templates. A further complication arises from:
6055 template <typename T> void f(T t) {
6059 Here, it is not possible to look up `T' in the scope of `T'
6060 itself. We must look in both the current scope, and the
6061 scope of the containing complete expression.
6063 Yet another issue is:
6072 The standard does not seem to say that the `S' in `~S'
6073 should refer to the type `S' and not the data member
6076 /* DR 244 says that we look up the name after the "~" in the
6077 same scope as we looked up the qualifying name. That idea
6078 isn't fully worked out; it's more complicated than that. */
6079 scope
= parser
->scope
;
6080 object_scope
= parser
->object_scope
;
6081 qualifying_scope
= parser
->qualifying_scope
;
6083 /* Check for invalid scopes. */
6084 if (scope
== error_mark_node
)
6086 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6087 cp_lexer_consume_token (parser
->lexer
);
6088 return error_mark_node
;
6090 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6092 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6093 error_at (token
->location
,
6094 "scope %qT before %<~%> is not a class-name",
6096 cp_parser_simulate_error (parser
);
6097 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6098 cp_lexer_consume_token (parser
->lexer
);
6099 return error_mark_node
;
6101 if (template_keyword_p
)
6103 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6104 error_at (tilde_loc
, "%<template%> keyword not permitted in "
6106 cp_parser_simulate_error (parser
);
6107 return error_mark_node
;
6110 gcc_assert (!scope
|| TYPE_P (scope
));
6112 token
= cp_lexer_peek_token (parser
->lexer
);
6114 /* Create a location with caret == start at the tilde,
6115 finishing at the end of the peeked token, e.g:
6119 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6121 /* If the name is of the form "X::~X" it's OK even if X is a
6125 && token
->type
== CPP_NAME
6126 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6128 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6129 || (CLASS_TYPE_P (scope
)
6130 && constructor_name_p (token
->u
.value
, scope
))))
6132 cp_lexer_consume_token (parser
->lexer
);
6133 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6136 /* ~auto means the destructor of whatever the object is. */
6137 if (cp_parser_is_keyword (token
, RID_AUTO
))
6139 if (cxx_dialect
< cxx14
)
6141 "%<~auto%> only available with "
6142 "%<-std=c++14%> or %<-std=gnu++14%>");
6143 cp_lexer_consume_token (parser
->lexer
);
6144 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, make_auto ());
6147 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6148 declarator-id of a constructor or destructor. */
6149 if (token
->type
== CPP_TEMPLATE_ID
&& cxx_dialect
>= cxx20
)
6151 if (!cp_parser_simulate_error (parser
))
6152 error_at (tilde_loc
, "template-id not allowed for destructor");
6153 return error_mark_node
;
6156 /* If there was an explicit qualification (S::~T), first look
6157 in the scope given by the qualification (i.e., S).
6159 Note: in the calls to cp_parser_class_name below we pass
6160 typename_type so that lookup finds the injected-class-name
6161 rather than the constructor. */
6163 type_decl
= NULL_TREE
;
6166 cp_parser_parse_tentatively (parser
);
6167 type_decl
= cp_parser_class_name (parser
,
6168 /*typename_keyword_p=*/false,
6169 /*template_keyword_p=*/false,
6171 /*check_dependency=*/false,
6172 /*class_head_p=*/false,
6174 if (cp_parser_parse_definitely (parser
))
6177 /* In "N::S::~S", look in "N" as well. */
6178 if (!done
&& scope
&& qualifying_scope
)
6180 cp_parser_parse_tentatively (parser
);
6181 parser
->scope
= qualifying_scope
;
6182 parser
->object_scope
= NULL_TREE
;
6183 parser
->qualifying_scope
= NULL_TREE
;
6185 = cp_parser_class_name (parser
,
6186 /*typename_keyword_p=*/false,
6187 /*template_keyword_p=*/false,
6189 /*check_dependency=*/false,
6190 /*class_head_p=*/false,
6192 if (cp_parser_parse_definitely (parser
))
6195 /* In "p->S::~T", look in the scope given by "*p" as well. */
6196 else if (!done
&& object_scope
)
6198 cp_parser_parse_tentatively (parser
);
6199 parser
->scope
= object_scope
;
6200 parser
->object_scope
= NULL_TREE
;
6201 parser
->qualifying_scope
= NULL_TREE
;
6203 = cp_parser_class_name (parser
,
6204 /*typename_keyword_p=*/false,
6205 /*template_keyword_p=*/false,
6207 /*check_dependency=*/false,
6208 /*class_head_p=*/false,
6210 if (cp_parser_parse_definitely (parser
))
6213 /* Look in the surrounding context. */
6216 parser
->scope
= NULL_TREE
;
6217 parser
->object_scope
= NULL_TREE
;
6218 parser
->qualifying_scope
= NULL_TREE
;
6219 if (processing_template_decl
)
6220 cp_parser_parse_tentatively (parser
);
6222 = cp_parser_class_name (parser
,
6223 /*typename_keyword_p=*/false,
6224 /*template_keyword_p=*/false,
6226 /*check_dependency=*/false,
6227 /*class_head_p=*/false,
6229 if (processing_template_decl
6230 && ! cp_parser_parse_definitely (parser
))
6232 /* We couldn't find a type with this name. If we're parsing
6233 tentatively, fail and try something else. */
6234 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6236 cp_parser_simulate_error (parser
);
6237 return error_mark_node
;
6239 /* Otherwise, accept it and check for a match at instantiation
6241 type_decl
= cp_parser_identifier (parser
);
6242 if (type_decl
!= error_mark_node
)
6243 type_decl
= build_min_nt_loc (loc
, BIT_NOT_EXPR
, type_decl
);
6247 /* If an error occurred, assume that the name of the
6248 destructor is the same as the name of the qualifying
6249 class. That allows us to keep parsing after running
6250 into ill-formed destructor names. */
6251 if (type_decl
== error_mark_node
&& scope
)
6252 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6253 else if (type_decl
== error_mark_node
)
6254 return error_mark_node
;
6256 /* Check that destructor name and scope match. */
6257 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6259 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6261 "declaration of %<~%T%> as member of %qT",
6263 cp_parser_simulate_error (parser
);
6264 return error_mark_node
;
6269 A typedef-name that names a class shall not be used as the
6270 identifier in the declarator for a destructor declaration. */
6272 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6273 && !DECL_SELF_REFERENCE_P (type_decl
)
6274 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6276 "typedef-name %qD used as destructor declarator",
6279 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6283 if (token
->keyword
== RID_OPERATOR
)
6287 /* This could be a template-id, so we try that first. */
6288 cp_parser_parse_tentatively (parser
);
6289 /* Try a template-id. */
6290 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6291 /*check_dependency_p=*/true,
6293 /* If that worked, we're done. */
6294 if (cp_parser_parse_definitely (parser
))
6296 /* We still don't know whether we're looking at an
6297 operator-function-id or a conversion-function-id. */
6298 cp_parser_parse_tentatively (parser
);
6299 /* Try an operator-function-id. */
6300 id
= cp_parser_operator_function_id (parser
);
6301 /* If that didn't work, try a conversion-function-id. */
6302 if (!cp_parser_parse_definitely (parser
))
6303 id
= cp_parser_conversion_function_id (parser
);
6312 cp_parser_error (parser
, "expected unqualified-id");
6313 return error_mark_node
;
6317 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6318 be a template-id or the name shall refer to a class template or an
6322 check_template_keyword_in_nested_name_spec (tree name
)
6324 if (CLASS_TYPE_P (name
)
6325 && ((CLASSTYPE_USE_TEMPLATE (name
)
6326 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name
)))
6327 || CLASSTYPE_IS_TEMPLATE (name
)))
6330 if (TREE_CODE (name
) == TYPENAME_TYPE
6331 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name
)) == TEMPLATE_ID_EXPR
)
6333 /* Alias templates are also OK. */
6334 else if (alias_template_specialization_p (name
, nt_opaque
))
6337 permerror (input_location
, TYPE_P (name
)
6338 ? G_("%qT is not a template")
6339 : G_("%qD is not a template"),
6343 /* Parse an (optional) nested-name-specifier.
6345 nested-name-specifier: [C++98]
6346 class-or-namespace-name :: nested-name-specifier [opt]
6347 class-or-namespace-name :: template nested-name-specifier [opt]
6349 nested-name-specifier: [C++0x]
6352 nested-name-specifier identifier ::
6353 nested-name-specifier template [opt] simple-template-id ::
6355 PARSER->SCOPE should be set appropriately before this function is
6356 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6357 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6360 Sets PARSER->SCOPE to the class (TYPE) or namespace
6361 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6362 it unchanged if there is no nested-name-specifier. Returns the new
6363 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6365 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6366 part of a declaration and/or decl-specifier. */
6369 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6370 bool typename_keyword_p
,
6371 bool check_dependency_p
,
6373 bool is_declaration
,
6374 bool template_keyword_p
/* = false */)
6376 bool success
= false;
6377 cp_token_position start
= 0;
6380 /* Remember where the nested-name-specifier starts. */
6381 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
6382 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
6384 start
= cp_lexer_token_position (parser
->lexer
, false);
6385 push_deferring_access_checks (dk_deferred
);
6392 tree saved_qualifying_scope
;
6394 /* Spot cases that cannot be the beginning of a
6395 nested-name-specifier. */
6396 token
= cp_lexer_peek_token (parser
->lexer
);
6398 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6399 the already parsed nested-name-specifier. */
6400 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6402 /* Grab the nested-name-specifier and continue the loop. */
6403 cp_parser_pre_parsed_nested_name_specifier (parser
);
6404 /* If we originally encountered this nested-name-specifier
6405 with IS_DECLARATION set to false, we will not have
6406 resolved TYPENAME_TYPEs, so we must do so here. */
6408 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6410 new_scope
= resolve_typename_type (parser
->scope
,
6411 /*only_current_p=*/false);
6412 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6413 parser
->scope
= new_scope
;
6419 /* Spot cases that cannot be the beginning of a
6420 nested-name-specifier. On the second and subsequent times
6421 through the loop, we look for the `template' keyword. */
6422 if (success
&& token
->keyword
== RID_TEMPLATE
)
6424 /* A template-id can start a nested-name-specifier. */
6425 else if (token
->type
== CPP_TEMPLATE_ID
)
6427 /* DR 743: decltype can be used in a nested-name-specifier. */
6428 else if (token_is_decltype (token
))
6432 /* If the next token is not an identifier, then it is
6433 definitely not a type-name or namespace-name. */
6434 if (token
->type
!= CPP_NAME
)
6436 /* If the following token is neither a `<' (to begin a
6437 template-id), nor a `::', then we are not looking at a
6438 nested-name-specifier. */
6439 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6441 if (token
->type
== CPP_COLON
6442 && parser
->colon_corrects_to_scope_p
6443 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6445 gcc_rich_location
richloc (token
->location
);
6446 richloc
.add_fixit_replace ("::");
6448 "found %<:%> in nested-name-specifier, "
6450 token
->type
= CPP_SCOPE
;
6453 if (token
->type
!= CPP_SCOPE
6454 && !cp_parser_nth_token_starts_template_argument_list_p
6459 /* The nested-name-specifier is optional, so we parse
6461 cp_parser_parse_tentatively (parser
);
6463 /* Look for the optional `template' keyword, if this isn't the
6464 first time through the loop. */
6467 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6468 /* DR1710: "In a qualified-id used as the name in
6469 a typename-specifier, elaborated-type-specifier, using-declaration,
6470 or class-or-decltype, an optional keyword template appearing at
6471 the top level is ignored." */
6472 if (!template_keyword_p
6473 && typename_keyword_p
6474 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
6475 template_keyword_p
= true;
6478 /* Save the old scope since the name lookup we are about to do
6479 might destroy it. */
6480 old_scope
= parser
->scope
;
6481 saved_qualifying_scope
= parser
->qualifying_scope
;
6482 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6483 look up names in "X<T>::I" in order to determine that "Y" is
6484 a template. So, if we have a typename at this point, we make
6485 an effort to look through it. */
6487 && !typename_keyword_p
6489 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6490 parser
->scope
= resolve_typename_type (parser
->scope
,
6491 /*only_current_p=*/false);
6492 /* Parse the qualifying entity. */
6494 = cp_parser_qualifying_entity (parser
,
6500 /* Look for the `::' token. */
6501 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6503 /* If we found what we wanted, we keep going; otherwise, we're
6505 if (!cp_parser_parse_definitely (parser
))
6507 bool error_p
= false;
6509 /* Restore the OLD_SCOPE since it was valid before the
6510 failed attempt at finding the last
6511 class-or-namespace-name. */
6512 parser
->scope
= old_scope
;
6513 parser
->qualifying_scope
= saved_qualifying_scope
;
6515 /* If the next token is a decltype, and the one after that is a
6516 `::', then the decltype has failed to resolve to a class or
6517 enumeration type. Give this error even when parsing
6518 tentatively since it can't possibly be valid--and we're going
6519 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6520 won't get another chance.*/
6521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6522 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6525 token
= cp_lexer_consume_token (parser
->lexer
);
6526 tree dtype
= token
->u
.tree_check_value
->value
;
6527 if (dtype
!= error_mark_node
)
6528 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6529 "which is not a class or enumeration type",
6531 parser
->scope
= error_mark_node
;
6535 cp_lexer_consume_token (parser
->lexer
);
6538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6539 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6541 /* If we have a non-type template-id followed by ::, it can't
6542 possibly be valid. */
6543 token
= cp_lexer_peek_token (parser
->lexer
);
6544 tree tid
= token
->u
.tree_check_value
->value
;
6545 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6546 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6548 tree tmpl
= NULL_TREE
;
6549 if (is_overloaded_fn (tid
))
6551 tree fns
= get_fns (tid
);
6552 if (OVL_SINGLE_P (fns
))
6553 tmpl
= OVL_FIRST (fns
);
6554 if (function_concept_p (fns
))
6555 error_at (token
->location
, "concept-id %qD "
6556 "in nested-name-specifier", tid
);
6558 error_at (token
->location
, "function template-id "
6559 "%qD in nested-name-specifier", tid
);
6563 tmpl
= TREE_OPERAND (tid
, 0);
6564 if (variable_concept_p (tmpl
)
6565 || standard_concept_p (tmpl
))
6566 error_at (token
->location
, "concept-id %qD "
6567 "in nested-name-specifier", tid
);
6570 /* Variable template. */
6571 gcc_assert (variable_template_p (tmpl
));
6572 error_at (token
->location
, "variable template-id "
6573 "%qD in nested-name-specifier", tid
);
6577 inform (DECL_SOURCE_LOCATION (tmpl
),
6578 "%qD declared here", tmpl
);
6580 parser
->scope
= error_mark_node
;
6584 cp_lexer_consume_token (parser
->lexer
);
6585 cp_lexer_consume_token (parser
->lexer
);
6589 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6591 /* If the next token is an identifier, and the one after
6592 that is a `::', then any valid interpretation would have
6593 found a class-or-namespace-name. */
6594 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6595 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6597 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6600 token
= cp_lexer_consume_token (parser
->lexer
);
6603 if (!token
->error_reported
)
6606 tree ambiguous_decls
;
6608 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6610 /*is_template=*/false,
6611 /*is_namespace=*/false,
6612 /*check_dependency=*/true,
6615 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6616 error_at (token
->location
,
6617 "%qD used without template arguments",
6619 else if (ambiguous_decls
)
6621 // cp_parser_lookup_name has the same diagnostic,
6622 // thus make sure to emit it at most once.
6623 if (cp_parser_uncommitted_to_tentative_parse_p
6626 error_at (token
->location
,
6627 "reference to %qD is ambiguous",
6629 print_candidates (ambiguous_decls
);
6631 decl
= error_mark_node
;
6635 if (cxx_dialect
!= cxx98
)
6636 cp_parser_name_lookup_error
6637 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6640 cp_parser_name_lookup_error
6641 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6645 parser
->scope
= error_mark_node
;
6647 /* Treat this as a successful nested-name-specifier
6652 If the name found is not a class-name (clause
6653 _class_) or namespace-name (_namespace.def_), the
6654 program is ill-formed. */
6657 cp_lexer_consume_token (parser
->lexer
);
6661 /* We've found one valid nested-name-specifier. */
6663 /* Name lookup always gives us a DECL. */
6664 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6665 new_scope
= TREE_TYPE (new_scope
);
6666 /* Uses of "template" must be followed by actual templates. */
6667 if (template_keyword_p
)
6668 check_template_keyword_in_nested_name_spec (new_scope
);
6669 /* If it is a class scope, try to complete it; we are about to
6670 be looking up names inside the class. */
6671 if (TYPE_P (new_scope
)
6672 /* Since checking types for dependency can be expensive,
6673 avoid doing it if the type is already complete. */
6674 && !COMPLETE_TYPE_P (new_scope
)
6675 /* Do not try to complete dependent types. */
6676 && !dependent_type_p (new_scope
))
6678 new_scope
= complete_type (new_scope
);
6679 /* If it is a typedef to current class, use the current
6680 class instead, as the typedef won't have any names inside
6682 if (!COMPLETE_TYPE_P (new_scope
)
6683 && currently_open_class (new_scope
))
6684 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6686 /* Make sure we look in the right scope the next time through
6688 parser
->scope
= new_scope
;
6691 /* If parsing tentatively, replace the sequence of tokens that makes
6692 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6693 token. That way, should we re-parse the token stream, we will
6694 not have to repeat the effort required to do the parse, nor will
6695 we issue duplicate error messages. */
6696 if (success
&& start
)
6700 token
= cp_lexer_token_at (parser
->lexer
, start
);
6701 /* Reset the contents of the START token. */
6702 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6703 /* Retrieve any deferred checks. Do not pop this access checks yet
6704 so the memory will not be reclaimed during token replacing below. */
6705 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6706 token
->tree_check_p
= true;
6707 token
->u
.tree_check_value
->value
= parser
->scope
;
6708 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6709 token
->u
.tree_check_value
->qualifying_scope
=
6710 parser
->qualifying_scope
;
6711 token
->keyword
= RID_MAX
;
6713 /* Purge all subsequent tokens. */
6714 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6718 pop_to_parent_deferring_access_checks ();
6720 return success
? parser
->scope
: NULL_TREE
;
6723 /* Parse a nested-name-specifier. See
6724 cp_parser_nested_name_specifier_opt for details. This function
6725 behaves identically, except that it will an issue an error if no
6726 nested-name-specifier is present. */
6729 cp_parser_nested_name_specifier (cp_parser
*parser
,
6730 bool typename_keyword_p
,
6731 bool check_dependency_p
,
6733 bool is_declaration
)
6737 /* Look for the nested-name-specifier. */
6738 scope
= cp_parser_nested_name_specifier_opt (parser
,
6743 /* If it was not present, issue an error message. */
6746 cp_parser_error (parser
, "expected nested-name-specifier");
6747 parser
->scope
= NULL_TREE
;
6753 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6754 this is either a class-name or a namespace-name (which corresponds
6755 to the class-or-namespace-name production in the grammar). For
6756 C++0x, it can also be a type-name that refers to an enumeration
6757 type or a simple-template-id.
6759 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6760 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6761 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6762 TYPE_P is TRUE iff the next name should be taken as a class-name,
6763 even the same name is declared to be another entity in the same
6766 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6767 specified by the class-or-namespace-name. If neither is found the
6768 ERROR_MARK_NODE is returned. */
6771 cp_parser_qualifying_entity (cp_parser
*parser
,
6772 bool typename_keyword_p
,
6773 bool template_keyword_p
,
6774 bool check_dependency_p
,
6776 bool is_declaration
)
6779 tree saved_qualifying_scope
;
6780 tree saved_object_scope
;
6783 bool successful_parse_p
;
6785 /* DR 743: decltype can appear in a nested-name-specifier. */
6786 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6788 scope
= cp_parser_decltype (parser
);
6789 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6790 && !MAYBE_CLASS_TYPE_P (scope
))
6792 cp_parser_simulate_error (parser
);
6793 return error_mark_node
;
6795 if (TYPE_NAME (scope
))
6796 scope
= TYPE_NAME (scope
);
6800 /* Before we try to parse the class-name, we must save away the
6801 current PARSER->SCOPE since cp_parser_class_name will destroy
6803 saved_scope
= parser
->scope
;
6804 saved_qualifying_scope
= parser
->qualifying_scope
;
6805 saved_object_scope
= parser
->object_scope
;
6806 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6807 there is no need to look for a namespace-name. */
6808 only_class_p
= template_keyword_p
6809 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6811 cp_parser_parse_tentatively (parser
);
6812 scope
= cp_parser_class_name (parser
,
6815 type_p
? class_type
: none_type
,
6817 /*class_head_p=*/false,
6819 /*enum_ok=*/cxx_dialect
> cxx98
);
6820 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6821 /* If that didn't work, try for a namespace-name. */
6822 if (!only_class_p
&& !successful_parse_p
)
6824 /* Restore the saved scope. */
6825 parser
->scope
= saved_scope
;
6826 parser
->qualifying_scope
= saved_qualifying_scope
;
6827 parser
->object_scope
= saved_object_scope
;
6828 /* If we are not looking at an identifier followed by the scope
6829 resolution operator, then this is not part of a
6830 nested-name-specifier. (Note that this function is only used
6831 to parse the components of a nested-name-specifier.) */
6832 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6833 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6834 return error_mark_node
;
6835 scope
= cp_parser_namespace_name (parser
);
6841 /* Return true if we are looking at a compound-literal, false otherwise. */
6844 cp_parser_compound_literal_p (cp_parser
*parser
)
6846 cp_lexer_save_tokens (parser
->lexer
);
6848 /* Skip tokens until the next token is a closing parenthesis.
6849 If we find the closing `)', and the next token is a `{', then
6850 we are looking at a compound-literal. */
6851 bool compound_literal_p
6852 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6853 /*consume_paren=*/true)
6854 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6856 /* Roll back the tokens we skipped. */
6857 cp_lexer_rollback_tokens (parser
->lexer
);
6859 return compound_literal_p
;
6862 /* Return true if EXPR is the integer constant zero or a complex constant
6863 of zero, without any folding, but ignoring location wrappers. */
6866 literal_integer_zerop (const_tree expr
)
6868 return (location_wrapper_p (expr
)
6869 && integer_zerop (TREE_OPERAND (expr
, 0)));
6872 /* Parse a postfix-expression.
6876 postfix-expression [ expression ]
6877 postfix-expression ( expression-list [opt] )
6878 simple-type-specifier ( expression-list [opt] )
6879 typename :: [opt] nested-name-specifier identifier
6880 ( expression-list [opt] )
6881 typename :: [opt] nested-name-specifier template [opt] template-id
6882 ( expression-list [opt] )
6883 postfix-expression . template [opt] id-expression
6884 postfix-expression -> template [opt] id-expression
6885 postfix-expression . pseudo-destructor-name
6886 postfix-expression -> pseudo-destructor-name
6887 postfix-expression ++
6888 postfix-expression --
6889 dynamic_cast < type-id > ( expression )
6890 static_cast < type-id > ( expression )
6891 reinterpret_cast < type-id > ( expression )
6892 const_cast < type-id > ( expression )
6893 typeid ( expression )
6899 ( type-id ) { initializer-list , [opt] }
6901 This extension is a GNU version of the C99 compound-literal
6902 construct. (The C99 grammar uses `type-name' instead of `type-id',
6903 but they are essentially the same concept.)
6905 If ADDRESS_P is true, the postfix expression is the operand of the
6906 `&' operator. CAST_P is true if this expression is the target of a
6909 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6910 class member access expressions [expr.ref].
6912 Returns a representation of the expression. */
6915 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6916 bool member_access_only_p
, bool decltype_p
,
6917 cp_id_kind
* pidk_return
)
6922 cp_id_kind idk
= CP_ID_KIND_NONE
;
6923 cp_expr postfix_expression
= NULL_TREE
;
6924 bool is_member_access
= false;
6926 /* Peek at the next token. */
6927 token
= cp_lexer_peek_token (parser
->lexer
);
6928 loc
= token
->location
;
6929 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6931 /* Some of the productions are determined by keywords. */
6932 keyword
= token
->keyword
;
6942 const char *saved_message
;
6943 bool saved_in_type_id_in_expr_p
;
6945 /* All of these can be handled in the same way from the point
6946 of view of parsing. Begin by consuming the token
6947 identifying the cast. */
6948 cp_lexer_consume_token (parser
->lexer
);
6950 /* New types cannot be defined in the cast. */
6951 saved_message
= parser
->type_definition_forbidden_message
;
6952 parser
->type_definition_forbidden_message
6953 = G_("types may not be defined in casts");
6955 /* Look for the opening `<'. */
6956 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6957 /* Parse the type to which we are casting. */
6958 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6959 parser
->in_type_id_in_expr_p
= true;
6960 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6962 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6963 /* Look for the closing `>'. */
6964 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6965 /* Restore the old message. */
6966 parser
->type_definition_forbidden_message
= saved_message
;
6968 bool saved_greater_than_is_operator_p
6969 = parser
->greater_than_is_operator_p
;
6970 parser
->greater_than_is_operator_p
= true;
6972 /* And the expression which is being cast. */
6973 matching_parens parens
;
6974 parens
.require_open (parser
);
6975 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6976 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6978 location_t end_loc
= close_paren
?
6979 close_paren
->location
: UNKNOWN_LOCATION
;
6981 parser
->greater_than_is_operator_p
6982 = saved_greater_than_is_operator_p
;
6984 /* Only type conversions to integral or enumeration types
6985 can be used in constant-expressions. */
6986 if (!cast_valid_in_integral_constant_expression_p (type
)
6987 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6989 postfix_expression
= error_mark_node
;
6993 /* Construct a location e.g. :
6994 reinterpret_cast <int *> (expr)
6995 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6996 ranging from the start of the "*_cast" token to the final closing
6997 paren, with the caret at the start. */
6998 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
7004 = build_dynamic_cast (cp_cast_loc
, type
, expression
,
7005 tf_warning_or_error
);
7009 = build_static_cast (cp_cast_loc
, type
, expression
,
7010 tf_warning_or_error
);
7014 = build_reinterpret_cast (cp_cast_loc
, type
, expression
,
7015 tf_warning_or_error
);
7019 = build_const_cast (cp_cast_loc
, type
, expression
,
7020 tf_warning_or_error
);
7031 const char *saved_message
;
7032 bool saved_in_type_id_in_expr_p
;
7034 /* Consume the `typeid' token. */
7035 cp_lexer_consume_token (parser
->lexer
);
7036 /* Look for the `(' token. */
7037 matching_parens parens
;
7038 parens
.require_open (parser
);
7039 /* Types cannot be defined in a `typeid' expression. */
7040 saved_message
= parser
->type_definition_forbidden_message
;
7041 parser
->type_definition_forbidden_message
7042 = G_("types may not be defined in a %<typeid%> expression");
7043 /* We can't be sure yet whether we're looking at a type-id or an
7045 cp_parser_parse_tentatively (parser
);
7046 /* Try a type-id first. */
7047 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7048 parser
->in_type_id_in_expr_p
= true;
7049 type
= cp_parser_type_id (parser
);
7050 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7051 /* Look for the `)' token. Otherwise, we can't be sure that
7052 we're not looking at an expression: consider `typeid (int
7053 (3))', for example. */
7054 cp_token
*close_paren
= parens
.require_close (parser
);
7055 /* If all went well, simply lookup the type-id. */
7056 if (cp_parser_parse_definitely (parser
))
7057 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
7058 /* Otherwise, fall back to the expression variant. */
7063 /* Look for an expression. */
7064 expression
= cp_parser_expression (parser
, & idk
);
7065 /* Compute its typeid. */
7066 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
7067 /* Look for the `)' token. */
7068 close_paren
= parens
.require_close (parser
);
7070 /* Restore the saved message. */
7071 parser
->type_definition_forbidden_message
= saved_message
;
7072 /* `typeid' may not appear in an integral constant expression. */
7073 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
7074 postfix_expression
= error_mark_node
;
7076 /* Construct a location e.g. :
7079 ranging from the start of the "typeid" token to the final closing
7080 paren, with the caret at the start. */
7083 location_t typeid_loc
7084 = make_location (start_loc
, start_loc
, close_paren
->location
);
7085 postfix_expression
.set_location (typeid_loc
);
7086 postfix_expression
.maybe_add_location_wrapper ();
7094 /* The syntax permitted here is the same permitted for an
7095 elaborated-type-specifier. */
7096 ++parser
->prevent_constrained_type_specifiers
;
7097 type
= cp_parser_elaborated_type_specifier (parser
,
7098 /*is_friend=*/false,
7099 /*is_declaration=*/false);
7100 --parser
->prevent_constrained_type_specifiers
;
7101 postfix_expression
= cp_parser_functional_cast (parser
, type
);
7106 case RID_BUILTIN_SHUFFLE
:
7107 case RID_BUILTIN_LAUNDER
:
7109 vec
<tree
, va_gc
> *vec
;
7113 cp_lexer_consume_token (parser
->lexer
);
7114 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
7115 /*cast_p=*/false, /*allow_expansion_p=*/true,
7116 /*non_constant_p=*/NULL
);
7119 postfix_expression
= error_mark_node
;
7123 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7129 if (vec
->length () == 1)
7131 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7134 error_at (loc
, "wrong number of arguments to "
7135 "%<__builtin_addressof%>");
7136 postfix_expression
= error_mark_node
;
7140 case RID_BUILTIN_LAUNDER
:
7141 if (vec
->length () == 1)
7142 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7143 tf_warning_or_error
);
7146 error_at (loc
, "wrong number of arguments to "
7147 "%<__builtin_launder%>");
7148 postfix_expression
= error_mark_node
;
7152 case RID_BUILTIN_SHUFFLE
:
7153 if (vec
->length () == 2)
7155 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7156 (*vec
)[1], tf_warning_or_error
);
7157 else if (vec
->length () == 3)
7159 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7160 (*vec
)[2], tf_warning_or_error
);
7163 error_at (loc
, "wrong number of arguments to "
7164 "%<__builtin_shuffle%>");
7165 postfix_expression
= error_mark_node
;
7175 case RID_BUILTIN_CONVERTVECTOR
:
7179 /* Consume the `__builtin_convertvector' token. */
7180 cp_lexer_consume_token (parser
->lexer
);
7181 /* Look for the opening `('. */
7182 matching_parens parens
;
7183 parens
.require_open (parser
);
7184 /* Now, parse the assignment-expression. */
7185 expression
= cp_parser_assignment_expression (parser
);
7186 /* Look for the `,'. */
7187 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7188 location_t type_location
7189 = cp_lexer_peek_token (parser
->lexer
)->location
;
7190 /* Parse the type-id. */
7192 type_id_in_expr_sentinel
s (parser
);
7193 type
= cp_parser_type_id (parser
);
7195 /* Look for the closing `)'. */
7196 parens
.require_close (parser
);
7197 return cp_build_vec_convert (expression
, type_location
, type
,
7198 tf_warning_or_error
);
7205 /* If the next thing is a simple-type-specifier, we may be
7206 looking at a functional cast. We could also be looking at
7207 an id-expression. So, we try the functional cast, and if
7208 that doesn't work we fall back to the primary-expression. */
7209 cp_parser_parse_tentatively (parser
);
7210 /* Look for the simple-type-specifier. */
7211 ++parser
->prevent_constrained_type_specifiers
;
7212 type
= cp_parser_simple_type_specifier (parser
,
7213 /*decl_specs=*/NULL
,
7214 CP_PARSER_FLAGS_NONE
);
7215 --parser
->prevent_constrained_type_specifiers
;
7216 /* Parse the cast itself. */
7217 if (!cp_parser_error_occurred (parser
))
7219 = cp_parser_functional_cast (parser
, type
);
7220 /* If that worked, we're done. */
7221 if (cp_parser_parse_definitely (parser
))
7224 /* If the functional-cast didn't work out, try a
7225 compound-literal. */
7226 if (cp_parser_allow_gnu_extensions_p (parser
)
7227 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7229 cp_expr initializer
= NULL_TREE
;
7231 cp_parser_parse_tentatively (parser
);
7233 matching_parens parens
;
7234 parens
.consume_open (parser
);
7236 /* Avoid calling cp_parser_type_id pointlessly, see comment
7237 in cp_parser_cast_expression about c++/29234. */
7238 if (!cp_parser_compound_literal_p (parser
))
7239 cp_parser_simulate_error (parser
);
7242 /* Parse the type. */
7243 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7244 parser
->in_type_id_in_expr_p
= true;
7245 type
= cp_parser_type_id (parser
);
7246 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7247 parens
.require_close (parser
);
7250 /* If things aren't going well, there's no need to
7252 if (!cp_parser_error_occurred (parser
))
7254 bool non_constant_p
;
7255 /* Parse the brace-enclosed initializer list. */
7256 initializer
= cp_parser_braced_list (parser
,
7259 /* If that worked, we're definitely looking at a
7260 compound-literal expression. */
7261 if (cp_parser_parse_definitely (parser
))
7263 /* Warn the user that a compound literal is not
7264 allowed in standard C++. */
7265 pedwarn (input_location
, OPT_Wpedantic
,
7266 "ISO C++ forbids compound-literals");
7267 /* For simplicity, we disallow compound literals in
7268 constant-expressions. We could
7269 allow compound literals of integer type, whose
7270 initializer was a constant, in constant
7271 expressions. Permitting that usage, as a further
7272 extension, would not change the meaning of any
7273 currently accepted programs. (Of course, as
7274 compound literals are not part of ISO C++, the
7275 standard has nothing to say.) */
7276 if (cp_parser_non_integral_constant_expression (parser
,
7279 postfix_expression
= error_mark_node
;
7282 /* Form the representation of the compound-literal. */
7284 = finish_compound_literal (type
, initializer
,
7285 tf_warning_or_error
, fcl_c99
);
7286 postfix_expression
.set_location (initializer
.get_location ());
7291 /* It must be a primary-expression. */
7293 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7294 /*template_arg_p=*/false,
7301 /* Note that we don't need to worry about calling build_cplus_new on a
7302 class-valued CALL_EXPR in decltype when it isn't the end of the
7303 postfix-expression; unary_complex_lvalue will take care of that for
7306 /* Keep looping until the postfix-expression is complete. */
7309 if (idk
== CP_ID_KIND_UNQUALIFIED
7310 && identifier_p (postfix_expression
)
7311 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7312 /* It is not a Koenig lookup function call. */
7314 = unqualified_name_lookup_error (postfix_expression
);
7316 /* Peek at the next token. */
7317 token
= cp_lexer_peek_token (parser
->lexer
);
7319 switch (token
->type
)
7321 case CPP_OPEN_SQUARE
:
7322 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7324 cp_parser_error (parser
,
7325 "two consecutive %<[%> shall "
7326 "only introduce an attribute");
7327 return error_mark_node
;
7330 = cp_parser_postfix_open_square_expression (parser
,
7334 postfix_expression
.set_range (start_loc
,
7335 postfix_expression
.get_location ());
7337 idk
= CP_ID_KIND_NONE
;
7338 is_member_access
= false;
7341 case CPP_OPEN_PAREN
:
7342 /* postfix-expression ( expression-list [opt] ) */
7345 bool is_builtin_constant_p
;
7346 bool saved_integral_constant_expression_p
= false;
7347 bool saved_non_integral_constant_expression_p
= false;
7348 tsubst_flags_t complain
= complain_flags (decltype_p
);
7349 vec
<tree
, va_gc
> *args
;
7350 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7352 is_member_access
= false;
7354 tree stripped_expression
7355 = tree_strip_any_location_wrapper (postfix_expression
);
7356 is_builtin_constant_p
7357 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7358 if (is_builtin_constant_p
)
7360 /* The whole point of __builtin_constant_p is to allow
7361 non-constant expressions to appear as arguments. */
7362 saved_integral_constant_expression_p
7363 = parser
->integral_constant_expression_p
;
7364 saved_non_integral_constant_expression_p
7365 = parser
->non_integral_constant_expression_p
;
7366 parser
->integral_constant_expression_p
= false;
7368 args
= (cp_parser_parenthesized_expression_list
7370 /*cast_p=*/false, /*allow_expansion_p=*/true,
7371 /*non_constant_p=*/NULL
,
7372 /*close_paren_loc=*/&close_paren_loc
,
7373 /*wrap_locations_p=*/true));
7374 if (is_builtin_constant_p
)
7376 parser
->integral_constant_expression_p
7377 = saved_integral_constant_expression_p
;
7378 parser
->non_integral_constant_expression_p
7379 = saved_non_integral_constant_expression_p
;
7384 postfix_expression
= error_mark_node
;
7388 /* Function calls are not permitted in
7389 constant-expressions. */
7390 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7391 && cp_parser_non_integral_constant_expression (parser
,
7394 postfix_expression
= error_mark_node
;
7395 release_tree_vector (args
);
7400 if (idk
== CP_ID_KIND_UNQUALIFIED
7401 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7403 if (identifier_p (postfix_expression
)
7404 /* In C++20, we may need to perform ADL for a template
7406 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7407 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7409 if (!args
->is_empty ())
7412 if (!any_type_dependent_arguments_p (args
))
7414 = perform_koenig_lookup (postfix_expression
, args
,
7419 = unqualified_fn_lookup_error (postfix_expression
);
7421 /* We do not perform argument-dependent lookup if
7422 normal lookup finds a non-function, in accordance
7423 with the expected resolution of DR 218. */
7424 else if (!args
->is_empty ()
7425 && is_overloaded_fn (postfix_expression
))
7427 /* Do not do argument dependent lookup if regular
7428 lookup finds a member function or a block-scope
7429 function declaration. [basic.lookup.argdep]/3 */
7430 bool do_adl_p
= true;
7431 tree fns
= get_fns (postfix_expression
);
7432 for (lkp_iterator
iter (fns
); iter
; ++iter
)
7434 tree fn
= STRIP_TEMPLATE (*iter
);
7435 if ((TREE_CODE (fn
) == USING_DECL
7436 && DECL_DEPENDENT_P (fn
))
7437 || DECL_FUNCTION_MEMBER_P (fn
)
7438 || DECL_LOCAL_FUNCTION_P (fn
))
7448 if (!any_type_dependent_arguments_p (args
))
7450 = perform_koenig_lookup (postfix_expression
, args
,
7456 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7458 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7459 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7461 if (processing_template_decl
7462 && (type_dependent_object_expression_p (instance
)
7463 || (!BASELINK_P (fn
)
7464 && TREE_CODE (fn
) != FIELD_DECL
)
7465 || type_dependent_expression_p (fn
)
7466 || any_type_dependent_arguments_p (args
)))
7468 maybe_generic_this_capture (instance
, fn
);
7470 = build_min_nt_call_vec (postfix_expression
, args
);
7472 else if (BASELINK_P (fn
))
7475 = (build_new_method_call
7476 (instance
, fn
, &args
, NULL_TREE
,
7477 (idk
== CP_ID_KIND_QUALIFIED
7478 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7485 = finish_call_expr (postfix_expression
, &args
,
7486 /*disallow_virtual=*/false,
7490 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7491 || TREE_CODE (postfix_expression
) == MEMBER_REF
7492 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7493 postfix_expression
= (build_offset_ref_call_from_tree
7494 (postfix_expression
, &args
,
7496 else if (idk
== CP_ID_KIND_QUALIFIED
)
7497 /* A call to a static class member, or a namespace-scope
7500 = finish_call_expr (postfix_expression
, &args
,
7501 /*disallow_virtual=*/true,
7505 /* All other function calls. */
7507 = finish_call_expr (postfix_expression
, &args
,
7508 /*disallow_virtual=*/false,
7512 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7514 location_t combined_loc
= make_location (token
->location
,
7517 postfix_expression
.set_location (combined_loc
);
7520 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7521 idk
= CP_ID_KIND_NONE
;
7523 release_tree_vector (args
);
7529 /* postfix-expression . template [opt] id-expression
7530 postfix-expression . pseudo-destructor-name
7531 postfix-expression -> template [opt] id-expression
7532 postfix-expression -> pseudo-destructor-name */
7534 /* Consume the `.' or `->' operator. */
7535 cp_lexer_consume_token (parser
->lexer
);
7538 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7542 is_member_access
= true;
7546 /* postfix-expression ++ */
7547 /* Consume the `++' token. */
7548 cp_lexer_consume_token (parser
->lexer
);
7549 /* Generate a representation for the complete expression. */
7551 = finish_increment_expr (postfix_expression
,
7552 POSTINCREMENT_EXPR
);
7553 /* Increments may not appear in constant-expressions. */
7554 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7555 postfix_expression
= error_mark_node
;
7556 idk
= CP_ID_KIND_NONE
;
7557 is_member_access
= false;
7560 case CPP_MINUS_MINUS
:
7561 /* postfix-expression -- */
7562 /* Consume the `--' token. */
7563 cp_lexer_consume_token (parser
->lexer
);
7564 /* Generate a representation for the complete expression. */
7566 = finish_increment_expr (postfix_expression
,
7567 POSTDECREMENT_EXPR
);
7568 /* Decrements may not appear in constant-expressions. */
7569 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7570 postfix_expression
= error_mark_node
;
7571 idk
= CP_ID_KIND_NONE
;
7572 is_member_access
= false;
7576 if (pidk_return
!= NULL
)
7577 * pidk_return
= idk
;
7578 if (member_access_only_p
)
7579 return is_member_access
7580 ? postfix_expression
7581 : cp_expr (error_mark_node
);
7583 return postfix_expression
;
7587 /* We should never get here. */
7589 return error_mark_node
;
7592 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7593 by cp_parser_builtin_offsetof. We're looking for
7595 postfix-expression [ expression ]
7596 postfix-expression [ braced-init-list ] (C++11)
7598 FOR_OFFSETOF is set if we're being called in that context, which
7599 changes how we deal with integer constant expressions. */
7602 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7603 tree postfix_expression
,
7607 tree index
= NULL_TREE
;
7608 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7609 bool saved_greater_than_is_operator_p
;
7611 /* Consume the `[' token. */
7612 cp_lexer_consume_token (parser
->lexer
);
7614 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7615 parser
->greater_than_is_operator_p
= true;
7617 /* Parse the index expression. */
7618 /* ??? For offsetof, there is a question of what to allow here. If
7619 offsetof is not being used in an integral constant expression context,
7620 then we *could* get the right answer by computing the value at runtime.
7621 If we are in an integral constant expression context, then we might
7622 could accept any constant expression; hard to say without analysis.
7623 Rather than open the barn door too wide right away, allow only integer
7624 constant expressions here. */
7626 index
= cp_parser_constant_expression (parser
);
7629 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7631 bool expr_nonconst_p
;
7632 cp_lexer_set_source_position (parser
->lexer
);
7633 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7634 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7637 index
= cp_parser_expression (parser
, NULL
, /*cast_p=*/false,
7638 /*decltype_p=*/false,
7639 /*warn_comma_p=*/warn_comma_subscript
);
7642 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7644 /* Look for the closing `]'. */
7645 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7647 /* Build the ARRAY_REF. */
7648 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7651 /* When not doing offsetof, array references are not permitted in
7652 constant-expressions. */
7654 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7655 postfix_expression
= error_mark_node
;
7657 return postfix_expression
;
7660 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7661 dereference of incomplete type, returns true if error_mark_node should
7662 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7663 and *DEPENDENT_P. */
7666 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7669 /* In a template, be permissive by treating an object expression
7670 of incomplete type as dependent (after a pedwarn). */
7671 diagnostic_t kind
= (processing_template_decl
7672 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7674 switch (TREE_CODE (*postfix_expression
))
7677 case REINTERPRET_CAST_EXPR
:
7678 case CONST_CAST_EXPR
:
7679 case STATIC_CAST_EXPR
:
7680 case DYNAMIC_CAST_EXPR
:
7681 case IMPLICIT_CONV_EXPR
:
7682 case VIEW_CONVERT_EXPR
:
7683 case NON_LVALUE_EXPR
:
7687 /* Don't emit any diagnostic for OVERLOADs. */
7691 /* Avoid clobbering e.g. DECLs. */
7692 if (!EXPR_P (*postfix_expression
))
7697 if (kind
== DK_IGNORED
)
7700 location_t exploc
= location_of (*postfix_expression
);
7701 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7702 if (!MAYBE_CLASS_TYPE_P (*scope
))
7704 if (kind
== DK_ERROR
)
7705 *scope
= *postfix_expression
= error_mark_node
;
7706 else if (processing_template_decl
)
7708 *dependent_p
= true;
7709 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7714 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7715 by cp_parser_builtin_offsetof. We're looking for
7717 postfix-expression . template [opt] id-expression
7718 postfix-expression . pseudo-destructor-name
7719 postfix-expression -> template [opt] id-expression
7720 postfix-expression -> pseudo-destructor-name
7722 FOR_OFFSETOF is set if we're being called in that context. That sorta
7723 limits what of the above we'll actually accept, but nevermind.
7724 TOKEN_TYPE is the "." or "->" token, which will already have been
7725 removed from the stream. */
7728 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7729 enum cpp_ttype token_type
,
7730 cp_expr postfix_expression
,
7731 bool for_offsetof
, cp_id_kind
*idk
,
7732 location_t location
)
7736 bool pseudo_destructor_p
;
7737 tree scope
= NULL_TREE
;
7738 location_t start_loc
= postfix_expression
.get_start ();
7740 /* If this is a `->' operator, dereference the pointer. */
7741 if (token_type
== CPP_DEREF
)
7742 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7743 tf_warning_or_error
);
7744 /* Check to see whether or not the expression is type-dependent and
7745 not the current instantiation. */
7746 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7747 /* The identifier following the `->' or `.' is not qualified. */
7748 parser
->scope
= NULL_TREE
;
7749 parser
->qualifying_scope
= NULL_TREE
;
7750 parser
->object_scope
= NULL_TREE
;
7751 *idk
= CP_ID_KIND_NONE
;
7753 /* Enter the scope corresponding to the type of the object
7754 given by the POSTFIX_EXPRESSION. */
7757 scope
= TREE_TYPE (postfix_expression
);
7758 /* According to the standard, no expression should ever have
7759 reference type. Unfortunately, we do not currently match
7760 the standard in this respect in that our internal representation
7761 of an expression may have reference type even when the standard
7762 says it does not. Therefore, we have to manually obtain the
7763 underlying type here. */
7764 scope
= non_reference (scope
);
7765 /* The type of the POSTFIX_EXPRESSION must be complete. */
7766 /* Unlike the object expression in other contexts, *this is not
7767 required to be of complete type for purposes of class member
7768 access (5.2.5) outside the member function body. */
7769 if (postfix_expression
!= current_class_ref
7770 && scope
!= error_mark_node
7771 && !currently_open_class (scope
))
7773 scope
= complete_type (scope
);
7774 if (!COMPLETE_TYPE_P (scope
)
7775 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7777 return error_mark_node
;
7782 /* Let the name lookup machinery know that we are processing a
7783 class member access expression. */
7784 parser
->context
->object_type
= scope
;
7785 /* If something went wrong, we want to be able to discern that case,
7786 as opposed to the case where there was no SCOPE due to the type
7787 of expression being dependent. */
7789 scope
= error_mark_node
;
7790 /* If the SCOPE was erroneous, make the various semantic analysis
7791 functions exit quickly -- and without issuing additional error
7793 if (scope
== error_mark_node
)
7794 postfix_expression
= error_mark_node
;
7800 tree type
= TREE_TYPE (postfix_expression
);
7801 /* If we don't have a (type-dependent) object of class type, use
7802 typeof to figure out the type of the object. */
7803 if (type
== NULL_TREE
)
7804 type
= finish_typeof (postfix_expression
);
7805 parser
->context
->object_type
= type
;
7808 /* Assume this expression is not a pseudo-destructor access. */
7809 pseudo_destructor_p
= false;
7811 /* If the SCOPE is a scalar type, then, if this is a valid program,
7812 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7813 is type dependent, it can be pseudo-destructor-name or something else.
7814 Try to parse it as pseudo-destructor-name first. */
7815 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7820 cp_parser_parse_tentatively (parser
);
7821 /* Parse the pseudo-destructor-name. */
7823 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7826 && (cp_parser_error_occurred (parser
)
7827 || !SCALAR_TYPE_P (type
)))
7828 cp_parser_abort_tentative_parse (parser
);
7829 else if (cp_parser_parse_definitely (parser
))
7831 pseudo_destructor_p
= true;
7833 = finish_pseudo_destructor_expr (postfix_expression
,
7838 if (!pseudo_destructor_p
)
7840 /* If the SCOPE is not a scalar type, we are looking at an
7841 ordinary class member access expression, rather than a
7842 pseudo-destructor-name. */
7844 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7845 /* Parse the id-expression. */
7846 name
= (cp_parser_id_expression
7848 cp_parser_optional_template_keyword (parser
),
7849 /*check_dependency_p=*/true,
7851 /*declarator_p=*/false,
7852 /*optional_p=*/false));
7853 /* In general, build a SCOPE_REF if the member name is qualified.
7854 However, if the name was not dependent and has already been
7855 resolved; there is no need to build the SCOPE_REF. For example;
7857 struct X { void f(); };
7858 template <typename T> void f(T* t) { t->X::f(); }
7860 Even though "t" is dependent, "X::f" is not and has been resolved
7861 to a BASELINK; there is no need to include scope information. */
7863 /* But we do need to remember that there was an explicit scope for
7864 virtual function calls. */
7866 *idk
= CP_ID_KIND_QUALIFIED
;
7868 /* If the name is a template-id that names a type, we will get a
7869 TYPE_DECL here. That is invalid code. */
7870 if (TREE_CODE (name
) == TYPE_DECL
)
7872 error_at (token
->location
, "invalid use of %qD", name
);
7873 postfix_expression
= error_mark_node
;
7877 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7879 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7881 error_at (token
->location
, "%<%D::%D%> is not a class member",
7882 parser
->scope
, name
);
7883 postfix_expression
= error_mark_node
;
7886 name
= build_qualified_name (/*type=*/NULL_TREE
,
7890 parser
->scope
= NULL_TREE
;
7891 parser
->qualifying_scope
= NULL_TREE
;
7892 parser
->object_scope
= NULL_TREE
;
7894 if (parser
->scope
&& name
&& BASELINK_P (name
))
7895 adjust_result_of_qualified_name_lookup
7896 (name
, parser
->scope
, scope
);
7898 = finish_class_member_access_expr (postfix_expression
, name
,
7900 tf_warning_or_error
);
7901 /* Build a location e.g.:
7904 where the caret is at the deref token, ranging from
7905 the start of postfix_expression to the end of the access expr. */
7906 location_t combined_loc
7907 = make_location (input_location
, start_loc
, parser
->lexer
);
7908 protected_set_expr_location (postfix_expression
, combined_loc
);
7912 /* We no longer need to look up names in the scope of the object on
7913 the left-hand side of the `.' or `->' operator. */
7914 parser
->context
->object_type
= NULL_TREE
;
7916 /* Outside of offsetof, these operators may not appear in
7917 constant-expressions. */
7919 && (cp_parser_non_integral_constant_expression
7920 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7921 postfix_expression
= error_mark_node
;
7923 return postfix_expression
;
7926 /* Parse a parenthesized expression-list.
7929 assignment-expression
7930 expression-list, assignment-expression
7935 identifier, expression-list
7937 CAST_P is true if this expression is the target of a cast.
7939 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7942 WRAP_LOCATIONS_P is true if expressions within this list for which
7943 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7944 their source locations.
7946 Returns a vector of trees. Each element is a representation of an
7947 assignment-expression. NULL is returned if the ( and or ) are
7948 missing. An empty, but allocated, vector is returned on no
7949 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7950 if we are parsing an attribute list for an attribute that wants a
7951 plain identifier argument, normal_attr for an attribute that wants
7952 an expression, or non_attr if we aren't parsing an attribute list. If
7953 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7954 not all of the expressions in the list were constant.
7955 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7956 will be written to with the location of the closing parenthesis. If
7957 an error occurs, it may or may not be written to. */
7959 static vec
<tree
, va_gc
> *
7960 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7961 int is_attribute_list
,
7963 bool allow_expansion_p
,
7964 bool *non_constant_p
,
7965 location_t
*close_paren_loc
,
7966 bool wrap_locations_p
)
7968 vec
<tree
, va_gc
> *expression_list
;
7969 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7970 tree identifier
= NULL_TREE
;
7971 bool saved_greater_than_is_operator_p
;
7973 /* Assume all the expressions will be constant. */
7975 *non_constant_p
= false;
7977 matching_parens parens
;
7978 if (!parens
.require_open (parser
))
7981 expression_list
= make_tree_vector ();
7983 /* Within a parenthesized expression, a `>' token is always
7984 the greater-than operator. */
7985 saved_greater_than_is_operator_p
7986 = parser
->greater_than_is_operator_p
;
7987 parser
->greater_than_is_operator_p
= true;
7989 cp_expr
expr (NULL_TREE
);
7991 /* Consume expressions until there are no more. */
7992 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7995 /* At the beginning of attribute lists, check to see if the
7996 next token is an identifier. */
7997 if (is_attribute_list
== id_attr
7998 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
8002 /* Consume the identifier. */
8003 token
= cp_lexer_consume_token (parser
->lexer
);
8004 /* Save the identifier. */
8005 identifier
= token
->u
.value
;
8009 bool expr_non_constant_p
;
8011 /* Parse the next assignment-expression. */
8012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8014 /* A braced-init-list. */
8015 cp_lexer_set_source_position (parser
->lexer
);
8016 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8017 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8018 if (non_constant_p
&& expr_non_constant_p
)
8019 *non_constant_p
= true;
8021 else if (non_constant_p
)
8023 expr
= (cp_parser_constant_expression
8024 (parser
, /*allow_non_constant_p=*/true,
8025 &expr_non_constant_p
));
8026 if (expr_non_constant_p
)
8027 *non_constant_p
= true;
8030 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
8034 expr
= instantiate_non_dependent_expr (expr
);
8036 /* If we have an ellipsis, then this is an expression
8038 if (allow_expansion_p
8039 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8041 /* Consume the `...'. */
8042 cp_lexer_consume_token (parser
->lexer
);
8044 /* Build the argument pack. */
8045 expr
= make_pack_expansion (expr
);
8048 if (wrap_locations_p
)
8049 expr
.maybe_add_location_wrapper ();
8051 /* Add it to the list. We add error_mark_node
8052 expressions to the list, so that we can still tell if
8053 the correct form for a parenthesized expression-list
8054 is found. That gives better errors. */
8055 vec_safe_push (expression_list
, expr
.get_value ());
8057 if (expr
== error_mark_node
)
8061 /* After the first item, attribute lists look the same as
8062 expression lists. */
8063 is_attribute_list
= non_attr
;
8066 /* If the next token isn't a `,', then we are done. */
8067 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8070 /* Otherwise, consume the `,' and keep going. */
8071 cp_lexer_consume_token (parser
->lexer
);
8074 if (close_paren_loc
)
8075 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8077 if (!parens
.require_close (parser
))
8082 /* We try and resync to an unnested comma, as that will give the
8083 user better diagnostics. */
8084 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
8085 /*recovering=*/true,
8087 /*consume_paren=*/true);
8092 parser
->greater_than_is_operator_p
8093 = saved_greater_than_is_operator_p
;
8098 parser
->greater_than_is_operator_p
8099 = saved_greater_than_is_operator_p
;
8102 vec_safe_insert (expression_list
, 0, identifier
);
8104 return expression_list
;
8107 /* Parse a pseudo-destructor-name.
8109 pseudo-destructor-name:
8110 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8111 :: [opt] nested-name-specifier template template-id :: ~ type-name
8112 :: [opt] nested-name-specifier [opt] ~ type-name
8114 If either of the first two productions is used, sets *SCOPE to the
8115 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8116 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8117 or ERROR_MARK_NODE if the parse fails. */
8120 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
8125 bool nested_name_specifier_p
;
8128 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8129 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8130 && !type_dependent_expression_p (object
))
8132 if (cxx_dialect
< cxx14
)
8133 pedwarn (input_location
, 0,
8134 "%<~auto%> only available with "
8135 "%<-std=c++14%> or %<-std=gnu++14%>");
8136 cp_lexer_consume_token (parser
->lexer
);
8137 cp_lexer_consume_token (parser
->lexer
);
8139 *type
= TREE_TYPE (object
);
8143 /* Assume that things will not work out. */
8144 *type
= error_mark_node
;
8146 /* Look for the optional `::' operator. */
8147 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8148 /* Look for the optional nested-name-specifier. */
8149 nested_name_specifier_p
8150 = (cp_parser_nested_name_specifier_opt (parser
,
8151 /*typename_keyword_p=*/false,
8152 /*check_dependency_p=*/true,
8154 /*is_declaration=*/false)
8156 /* Now, if we saw a nested-name-specifier, we might be doing the
8157 second production. */
8158 if (nested_name_specifier_p
8159 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8161 /* Consume the `template' keyword. */
8162 cp_lexer_consume_token (parser
->lexer
);
8163 /* Parse the template-id. */
8164 cp_parser_template_id (parser
,
8165 /*template_keyword_p=*/true,
8166 /*check_dependency_p=*/false,
8168 /*is_declaration=*/true);
8169 /* Look for the `::' token. */
8170 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8172 /* If the next token is not a `~', then there might be some
8173 additional qualification. */
8174 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8176 /* At this point, we're looking for "type-name :: ~". The type-name
8177 must not be a class-name, since this is a pseudo-destructor. So,
8178 it must be either an enum-name, or a typedef-name -- both of which
8179 are just identifiers. So, we peek ahead to check that the "::"
8180 and "~" tokens are present; if they are not, then we can avoid
8181 calling type_name. */
8182 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8183 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8184 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8186 cp_parser_error (parser
, "non-scalar type");
8190 /* Look for the type-name. */
8191 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8192 if (*scope
== error_mark_node
)
8195 /* Look for the `::' token. */
8196 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8201 /* Look for the `~'. */
8202 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8204 /* Once we see the ~, this has to be a pseudo-destructor. */
8205 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8206 cp_parser_commit_to_topmost_tentative_parse (parser
);
8208 /* Look for the type-name again. We are not responsible for
8209 checking that it matches the first type-name. */
8210 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8213 /* Parse a unary-expression.
8220 unary-operator cast-expression
8221 sizeof unary-expression
8223 alignof ( type-id ) [C++0x]
8230 __extension__ cast-expression
8231 __alignof__ unary-expression
8232 __alignof__ ( type-id )
8233 alignof unary-expression [C++0x]
8234 __real__ cast-expression
8235 __imag__ cast-expression
8237 sizeof ( type-id ) { initializer-list , [opt] }
8238 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8239 __alignof__ ( type-id ) { initializer-list , [opt] }
8241 ADDRESS_P is true iff the unary-expression is appearing as the
8242 operand of the `&' operator. CAST_P is true if this expression is
8243 the target of a cast.
8245 Returns a representation of the expression. */
8248 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8249 bool address_p
, bool cast_p
, bool decltype_p
)
8252 enum tree_code unary_operator
;
8254 /* Peek at the next token. */
8255 token
= cp_lexer_peek_token (parser
->lexer
);
8256 /* Some keywords give away the kind of expression. */
8257 if (token
->type
== CPP_KEYWORD
)
8259 enum rid keyword
= token
->keyword
;
8268 location_t start_loc
= token
->location
;
8270 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8271 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8273 /* Consume the token. */
8274 cp_lexer_consume_token (parser
->lexer
);
8275 /* Parse the operand. */
8276 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8278 /* Construct a location e.g. :
8281 with start == caret at the start of the "alignof"/"sizeof"
8282 token, with the endpoint at the final closing paren. */
8283 location_t compound_loc
8284 = make_location (start_loc
, start_loc
, parser
->lexer
);
8286 if (TYPE_P (operand
))
8287 ret
= cxx_sizeof_or_alignof_type (compound_loc
, operand
, op
,
8291 /* ISO C++ defines alignof only with types, not with
8292 expressions. So pedwarn if alignof is used with a non-
8293 type expression. However, __alignof__ is ok. */
8295 pedwarn (token
->location
, OPT_Wpedantic
,
8296 "ISO C++ does not allow %<alignof%> "
8299 ret
= cxx_sizeof_or_alignof_expr (compound_loc
,
8302 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8303 SIZEOF_EXPR with the original operand. */
8304 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8306 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8308 if (!processing_template_decl
&& TYPE_P (operand
))
8310 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8311 build1 (NOP_EXPR
, operand
,
8313 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8316 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8317 TREE_SIDE_EFFECTS (ret
) = 0;
8318 TREE_READONLY (ret
) = 1;
8319 SET_EXPR_LOCATION (ret
, compound_loc
);
8323 cp_expr
ret_expr (ret
, compound_loc
);
8324 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8328 case RID_BUILTIN_HAS_ATTRIBUTE
:
8329 return cp_parser_has_attribute_expression (parser
);
8332 return cp_parser_new_expression (parser
);
8335 return cp_parser_delete_expression (parser
);
8339 /* The saved value of the PEDANTIC flag. */
8343 /* Save away the PEDANTIC flag. */
8344 cp_parser_extension_opt (parser
, &saved_pedantic
);
8345 /* Parse the cast-expression. */
8346 expr
= cp_parser_simple_cast_expression (parser
);
8347 /* Restore the PEDANTIC flag. */
8348 pedantic
= saved_pedantic
;
8358 /* Consume the `__real__' or `__imag__' token. */
8359 cp_lexer_consume_token (parser
->lexer
);
8360 /* Parse the cast-expression. */
8361 expression
= cp_parser_simple_cast_expression (parser
);
8362 /* Create the complete representation. */
8363 return build_x_unary_op (token
->location
,
8364 (keyword
== RID_REALPART
8365 ? REALPART_EXPR
: IMAGPART_EXPR
),
8367 tf_warning_or_error
);
8371 case RID_TRANSACTION_ATOMIC
:
8372 case RID_TRANSACTION_RELAXED
:
8373 return cp_parser_transaction_expression (parser
, keyword
);
8378 const char *saved_message
;
8379 bool saved_integral_constant_expression_p
;
8380 bool saved_non_integral_constant_expression_p
;
8381 bool saved_greater_than_is_operator_p
;
8383 location_t start_loc
= token
->location
;
8385 cp_lexer_consume_token (parser
->lexer
);
8386 matching_parens parens
;
8387 parens
.require_open (parser
);
8389 saved_message
= parser
->type_definition_forbidden_message
;
8390 parser
->type_definition_forbidden_message
8391 = G_("types may not be defined in %<noexcept%> expressions");
8393 saved_integral_constant_expression_p
8394 = parser
->integral_constant_expression_p
;
8395 saved_non_integral_constant_expression_p
8396 = parser
->non_integral_constant_expression_p
;
8397 parser
->integral_constant_expression_p
= false;
8399 saved_greater_than_is_operator_p
8400 = parser
->greater_than_is_operator_p
;
8401 parser
->greater_than_is_operator_p
= true;
8403 ++cp_unevaluated_operand
;
8404 ++c_inhibit_evaluation_warnings
;
8405 ++cp_noexcept_operand
;
8406 expr
= cp_parser_expression (parser
);
8407 --cp_noexcept_operand
;
8408 --c_inhibit_evaluation_warnings
;
8409 --cp_unevaluated_operand
;
8411 parser
->greater_than_is_operator_p
8412 = saved_greater_than_is_operator_p
;
8414 parser
->integral_constant_expression_p
8415 = saved_integral_constant_expression_p
;
8416 parser
->non_integral_constant_expression_p
8417 = saved_non_integral_constant_expression_p
;
8419 parser
->type_definition_forbidden_message
= saved_message
;
8421 parens
.require_close (parser
);
8423 /* Construct a location of the form:
8426 with start == caret, finishing at the close-paren. */
8427 location_t noexcept_loc
8428 = make_location (start_loc
, start_loc
, parser
->lexer
);
8430 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8437 location_t kw_loc
= token
->location
;
8439 /* Consume the `co_await' token. */
8440 cp_lexer_consume_token (parser
->lexer
);
8441 /* Parse its cast-expression. */
8442 expr
= cp_parser_simple_cast_expression (parser
);
8443 if (expr
== error_mark_node
)
8444 return error_mark_node
;
8446 /* Handle [expr.await]. */
8447 return cp_expr (finish_co_await_expr (kw_loc
, expr
));
8455 /* Look for the `:: new' and `:: delete', which also signal the
8456 beginning of a new-expression, or delete-expression,
8457 respectively. If the next token is `::', then it might be one of
8459 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8463 /* See if the token after the `::' is one of the keywords in
8464 which we're interested. */
8465 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8466 /* If it's `new', we have a new-expression. */
8467 if (keyword
== RID_NEW
)
8468 return cp_parser_new_expression (parser
);
8469 /* Similarly, for `delete'. */
8470 else if (keyword
== RID_DELETE
)
8471 return cp_parser_delete_expression (parser
);
8474 /* Look for a unary operator. */
8475 unary_operator
= cp_parser_unary_operator (token
);
8476 /* The `++' and `--' operators can be handled similarly, even though
8477 they are not technically unary-operators in the grammar. */
8478 if (unary_operator
== ERROR_MARK
)
8480 if (token
->type
== CPP_PLUS_PLUS
)
8481 unary_operator
= PREINCREMENT_EXPR
;
8482 else if (token
->type
== CPP_MINUS_MINUS
)
8483 unary_operator
= PREDECREMENT_EXPR
;
8484 /* Handle the GNU address-of-label extension. */
8485 else if (cp_parser_allow_gnu_extensions_p (parser
)
8486 && token
->type
== CPP_AND_AND
)
8490 location_t start_loc
= token
->location
;
8492 /* Consume the '&&' token. */
8493 cp_lexer_consume_token (parser
->lexer
);
8494 /* Look for the identifier. */
8495 identifier
= cp_parser_identifier (parser
);
8496 /* Construct a location of the form:
8499 with caret==start at the "&&", finish at the end of the label. */
8500 location_t combined_loc
8501 = make_location (start_loc
, start_loc
, parser
->lexer
);
8502 /* Create an expression representing the address. */
8503 expression
= finish_label_address_expr (identifier
, combined_loc
);
8504 if (cp_parser_non_integral_constant_expression (parser
,
8506 expression
= error_mark_node
;
8510 if (unary_operator
!= ERROR_MARK
)
8512 cp_expr cast_expression
;
8513 cp_expr expression
= error_mark_node
;
8514 non_integral_constant non_constant_p
= NIC_NONE
;
8515 location_t loc
= token
->location
;
8516 tsubst_flags_t complain
= complain_flags (decltype_p
);
8518 /* Consume the operator token. */
8519 token
= cp_lexer_consume_token (parser
->lexer
);
8520 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8522 /* Parse the cast-expression. */
8524 = cp_parser_cast_expression (parser
,
8525 unary_operator
== ADDR_EXPR
,
8531 OP_TOKEN CAST_EXPRESSION
8532 ^~~~~~~~~~~~~~~~~~~~~~~~~
8533 with start==caret at the operator token, and
8534 extending to the end of the cast_expression. */
8535 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8537 /* Now, build an appropriate representation. */
8538 switch (unary_operator
)
8541 non_constant_p
= NIC_STAR
;
8542 expression
= build_x_indirect_ref (loc
, cast_expression
,
8545 /* TODO: build_x_indirect_ref does not always honor the
8546 location, so ensure it is set. */
8547 expression
.set_location (loc
);
8551 non_constant_p
= NIC_ADDR
;
8554 expression
= build_x_unary_op (loc
, unary_operator
,
8557 /* TODO: build_x_unary_op does not always honor the location,
8558 so ensure it is set. */
8559 expression
.set_location (loc
);
8562 case PREINCREMENT_EXPR
:
8563 case PREDECREMENT_EXPR
:
8564 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8565 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8568 /* Immediately fold negation of a constant, unless the constant is 0
8569 (since -0 == 0) or it would overflow. */
8570 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8573 = tree_strip_any_location_wrapper (cast_expression
);
8574 if (CONSTANT_CLASS_P (stripped_expr
)
8575 && !integer_zerop (stripped_expr
)
8576 && !TREE_OVERFLOW (stripped_expr
))
8578 tree folded
= fold_build1 (unary_operator
,
8579 TREE_TYPE (stripped_expr
),
8581 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8583 expression
= maybe_wrap_with_location (folded
, loc
);
8589 case UNARY_PLUS_EXPR
:
8590 case TRUTH_NOT_EXPR
:
8591 expression
= finish_unary_op_expr (loc
, unary_operator
,
8592 cast_expression
, complain
);
8599 if (non_constant_p
!= NIC_NONE
8600 && cp_parser_non_integral_constant_expression (parser
,
8602 expression
= error_mark_node
;
8607 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8608 /*member_access_only_p=*/false,
8613 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8614 unary-operator, the corresponding tree code is returned. */
8616 static enum tree_code
8617 cp_parser_unary_operator (cp_token
* token
)
8619 switch (token
->type
)
8622 return INDIRECT_REF
;
8628 return UNARY_PLUS_EXPR
;
8634 return TRUTH_NOT_EXPR
;
8637 return BIT_NOT_EXPR
;
8644 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8645 Returns a representation of the expression. */
8648 cp_parser_has_attribute_expression (cp_parser
*parser
)
8650 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8652 /* Consume the __builtin_has_attribute token. */
8653 cp_lexer_consume_token (parser
->lexer
);
8655 matching_parens parens
;
8656 if (!parens
.require_open (parser
))
8657 return error_mark_node
;
8659 /* Types cannot be defined in a `sizeof' expression. Save away the
8661 const char *saved_message
= parser
->type_definition_forbidden_message
;
8662 const char *saved_message_arg
8663 = parser
->type_definition_forbidden_message_arg
;
8664 parser
->type_definition_forbidden_message
8665 = G_("types may not be defined in %qs expressions");
8666 parser
->type_definition_forbidden_message_arg
8667 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
8669 /* The restrictions on constant-expressions do not apply inside
8670 sizeof expressions. */
8671 bool saved_integral_constant_expression_p
8672 = parser
->integral_constant_expression_p
;
8673 bool saved_non_integral_constant_expression_p
8674 = parser
->non_integral_constant_expression_p
;
8675 parser
->integral_constant_expression_p
= false;
8677 /* Do not actually evaluate the expression. */
8678 ++cp_unevaluated_operand
;
8679 ++c_inhibit_evaluation_warnings
;
8681 tree oper
= NULL_TREE
;
8683 /* We can't be sure yet whether we're looking at a type-id or an
8685 cp_parser_parse_tentatively (parser
);
8687 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8688 parser
->in_type_id_in_expr_p
= true;
8689 /* Look for the type-id. */
8690 oper
= cp_parser_type_id (parser
);
8691 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8693 cp_parser_parse_definitely (parser
);
8695 /* If the type-id production did not work out, then we must be
8696 looking at an expression. */
8697 if (!oper
|| oper
== error_mark_node
)
8698 oper
= cp_parser_assignment_expression (parser
);
8700 STRIP_ANY_LOCATION_WRAPPER (oper
);
8702 /* Go back to evaluating expressions. */
8703 --cp_unevaluated_operand
;
8704 --c_inhibit_evaluation_warnings
;
8706 /* And restore the old one. */
8707 parser
->type_definition_forbidden_message
= saved_message
;
8708 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
8709 parser
->integral_constant_expression_p
8710 = saved_integral_constant_expression_p
;
8711 parser
->non_integral_constant_expression_p
8712 = saved_non_integral_constant_expression_p
;
8714 /* Consume the comma if it's there. */
8715 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8717 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8718 /*consume_paren=*/true);
8719 return error_mark_node
;
8722 /* Parse the attribute specification. */
8724 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8725 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8727 if (oper
== error_mark_node
)
8729 else if (type_dependent_expression_p (oper
))
8730 sorry_at (atloc
, "%<__builtin_has_attribute%> with dependent argument "
8731 "not supported yet");
8734 /* Fold constant expressions used in attributes first. */
8735 cp_check_const_attributes (attr
);
8737 /* Finally, see if OPER has been declared with ATTR. */
8738 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8741 parens
.require_close (parser
);
8745 error_at (atloc
, "expected identifier");
8746 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8749 /* Construct a location e.g. :
8750 __builtin_has_attribute (oper, attr)
8751 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8752 with start == caret at the start of the built-in token,
8753 and with the endpoint at the final closing paren. */
8754 location_t compound_loc
8755 = make_location (start_loc
, start_loc
, parser
->lexer
);
8757 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8758 ret_expr
.set_location (compound_loc
);
8759 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8763 /* Parse a new-expression.
8766 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8767 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8769 Returns a representation of the expression. */
8772 cp_parser_new_expression (cp_parser
* parser
)
8774 bool global_scope_p
;
8775 vec
<tree
, va_gc
> *placement
;
8777 vec
<tree
, va_gc
> *initializer
;
8778 tree nelts
= NULL_TREE
;
8781 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8783 /* Look for the optional `::' operator. */
8785 = (cp_parser_global_scope_opt (parser
,
8786 /*current_scope_valid_p=*/false)
8788 /* Look for the `new' operator. */
8789 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8790 /* There's no easy way to tell a new-placement from the
8791 `( type-id )' construct. */
8792 cp_parser_parse_tentatively (parser
);
8793 /* Look for a new-placement. */
8794 placement
= cp_parser_new_placement (parser
);
8795 /* If that didn't work out, there's no new-placement. */
8796 if (!cp_parser_parse_definitely (parser
))
8798 if (placement
!= NULL
)
8799 release_tree_vector (placement
);
8803 /* If the next token is a `(', then we have a parenthesized
8805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8808 const char *saved_message
= parser
->type_definition_forbidden_message
;
8810 /* Consume the `('. */
8811 matching_parens parens
;
8812 parens
.consume_open (parser
);
8814 /* Parse the type-id. */
8815 parser
->type_definition_forbidden_message
8816 = G_("types may not be defined in a new-expression");
8818 type_id_in_expr_sentinel
s (parser
);
8819 type
= cp_parser_type_id (parser
);
8821 parser
->type_definition_forbidden_message
= saved_message
;
8823 /* Look for the closing `)'. */
8824 parens
.require_close (parser
);
8825 token
= cp_lexer_peek_token (parser
->lexer
);
8826 /* There should not be a direct-new-declarator in this production,
8827 but GCC used to allowed this, so we check and emit a sensible error
8828 message for this case. */
8829 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8831 error_at (token
->location
,
8832 "array bound forbidden after parenthesized type-id");
8833 inform (token
->location
,
8834 "try removing the parentheses around the type-id");
8835 cp_parser_direct_new_declarator (parser
);
8838 /* Otherwise, there must be a new-type-id. */
8840 type
= cp_parser_new_type_id (parser
, &nelts
);
8842 /* If the next token is a `(' or '{', then we have a new-initializer. */
8843 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8844 if (token
->type
== CPP_OPEN_PAREN
8845 || token
->type
== CPP_OPEN_BRACE
)
8846 initializer
= cp_parser_new_initializer (parser
);
8850 /* A new-expression may not appear in an integral constant
8852 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8853 ret
= error_mark_node
;
8854 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8855 of a new-type-id or type-id of a new-expression, the new-expression shall
8856 contain a new-initializer of the form ( assignment-expression )".
8857 Additionally, consistently with the spirit of DR 1467, we want to accept
8858 'new auto { 2 }' too. */
8859 else if ((ret
= type_uses_auto (type
))
8860 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8861 && (vec_safe_length (initializer
) != 1
8862 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8863 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8865 error_at (token
->location
,
8866 "initialization of new-expression for type %<auto%> "
8867 "requires exactly one element");
8868 ret
= error_mark_node
;
8872 /* Construct a location e.g.:
8875 with caret == start at the start of the "new" token, and the end
8876 at the end of the final token we consumed. */
8877 location_t combined_loc
= make_location (start_loc
, start_loc
,
8879 /* Create a representation of the new-expression. */
8880 ret
= build_new (combined_loc
, &placement
, type
, nelts
, &initializer
,
8881 global_scope_p
, tf_warning_or_error
);
8884 if (placement
!= NULL
)
8885 release_tree_vector (placement
);
8886 if (initializer
!= NULL
)
8887 release_tree_vector (initializer
);
8892 /* Parse a new-placement.
8897 Returns the same representation as for an expression-list. */
8899 static vec
<tree
, va_gc
> *
8900 cp_parser_new_placement (cp_parser
* parser
)
8902 vec
<tree
, va_gc
> *expression_list
;
8904 /* Parse the expression-list. */
8905 expression_list
= (cp_parser_parenthesized_expression_list
8906 (parser
, non_attr
, /*cast_p=*/false,
8907 /*allow_expansion_p=*/true,
8908 /*non_constant_p=*/NULL
));
8910 if (expression_list
&& expression_list
->is_empty ())
8911 error ("expected expression-list or type-id");
8913 return expression_list
;
8916 /* Parse a new-type-id.
8919 type-specifier-seq new-declarator [opt]
8921 Returns the TYPE allocated. If the new-type-id indicates an array
8922 type, *NELTS is set to the number of elements in the last array
8923 bound; the TYPE will not include the last array bound. */
8926 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8928 cp_decl_specifier_seq type_specifier_seq
;
8929 cp_declarator
*new_declarator
;
8930 cp_declarator
*declarator
;
8931 cp_declarator
*outer_declarator
;
8932 const char *saved_message
;
8934 /* The type-specifier sequence must not contain type definitions.
8935 (It cannot contain declarations of new types either, but if they
8936 are not definitions we will catch that because they are not
8938 saved_message
= parser
->type_definition_forbidden_message
;
8939 parser
->type_definition_forbidden_message
8940 = G_("types may not be defined in a new-type-id");
8941 /* Parse the type-specifier-seq. */
8942 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8943 /*is_declaration=*/false,
8944 /*is_trailing_return=*/false,
8945 &type_specifier_seq
);
8946 /* Restore the old message. */
8947 parser
->type_definition_forbidden_message
= saved_message
;
8949 if (type_specifier_seq
.type
== error_mark_node
)
8950 return error_mark_node
;
8952 /* Parse the new-declarator. */
8953 new_declarator
= cp_parser_new_declarator_opt (parser
);
8955 /* Determine the number of elements in the last array dimension, if
8958 /* Skip down to the last array dimension. */
8959 declarator
= new_declarator
;
8960 outer_declarator
= NULL
;
8961 while (declarator
&& (declarator
->kind
== cdk_pointer
8962 || declarator
->kind
== cdk_ptrmem
))
8964 outer_declarator
= declarator
;
8965 declarator
= declarator
->declarator
;
8968 && declarator
->kind
== cdk_array
8969 && declarator
->declarator
8970 && declarator
->declarator
->kind
== cdk_array
)
8972 outer_declarator
= declarator
;
8973 declarator
= declarator
->declarator
;
8976 if (declarator
&& declarator
->kind
== cdk_array
)
8978 *nelts
= declarator
->u
.array
.bounds
;
8979 if (*nelts
== error_mark_node
)
8980 *nelts
= integer_one_node
;
8982 if (outer_declarator
)
8983 outer_declarator
->declarator
= declarator
->declarator
;
8985 new_declarator
= NULL
;
8988 return groktypename (&type_specifier_seq
, new_declarator
, false);
8991 /* Parse an (optional) new-declarator.
8994 ptr-operator new-declarator [opt]
8995 direct-new-declarator
8997 Returns the declarator. */
8999 static cp_declarator
*
9000 cp_parser_new_declarator_opt (cp_parser
* parser
)
9002 enum tree_code code
;
9003 tree type
, std_attributes
= NULL_TREE
;
9004 cp_cv_quals cv_quals
;
9006 /* We don't know if there's a ptr-operator next, or not. */
9007 cp_parser_parse_tentatively (parser
);
9008 /* Look for a ptr-operator. */
9009 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
9010 /* If that worked, look for more new-declarators. */
9011 if (cp_parser_parse_definitely (parser
))
9013 cp_declarator
*declarator
;
9015 /* Parse another optional declarator. */
9016 declarator
= cp_parser_new_declarator_opt (parser
);
9018 declarator
= cp_parser_make_indirect_declarator
9019 (code
, type
, cv_quals
, declarator
, std_attributes
);
9024 /* If the next token is a `[', there is a direct-new-declarator. */
9025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9026 return cp_parser_direct_new_declarator (parser
);
9031 /* Parse a direct-new-declarator.
9033 direct-new-declarator:
9035 direct-new-declarator [constant-expression]
9039 static cp_declarator
*
9040 cp_parser_direct_new_declarator (cp_parser
* parser
)
9042 cp_declarator
*declarator
= NULL
;
9049 /* Look for the opening `['. */
9050 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9052 token
= cp_lexer_peek_token (parser
->lexer
);
9053 expression
= cp_parser_expression (parser
);
9054 /* The standard requires that the expression have integral
9055 type. DR 74 adds enumeration types. We believe that the
9056 real intent is that these expressions be handled like the
9057 expression in a `switch' condition, which also allows
9058 classes with a single conversion to integral or
9059 enumeration type. */
9060 if (!processing_template_decl
)
9063 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
9068 error_at (token
->location
,
9069 "expression in new-declarator must have integral "
9070 "or enumeration type");
9071 expression
= error_mark_node
;
9075 /* Look for the closing `]'. */
9076 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9078 /* Add this bound to the declarator. */
9079 declarator
= make_array_declarator (declarator
, expression
);
9081 /* If the next token is not a `[', then there are no more
9083 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
9090 /* Parse a new-initializer.
9093 ( expression-list [opt] )
9096 Returns a representation of the expression-list. */
9098 static vec
<tree
, va_gc
> *
9099 cp_parser_new_initializer (cp_parser
* parser
)
9101 vec
<tree
, va_gc
> *expression_list
;
9103 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9106 bool expr_non_constant_p
;
9107 cp_lexer_set_source_position (parser
->lexer
);
9108 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9109 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9110 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
9111 expression_list
= make_tree_vector_single (t
);
9114 expression_list
= (cp_parser_parenthesized_expression_list
9115 (parser
, non_attr
, /*cast_p=*/false,
9116 /*allow_expansion_p=*/true,
9117 /*non_constant_p=*/NULL
));
9119 return expression_list
;
9122 /* Parse a delete-expression.
9125 :: [opt] delete cast-expression
9126 :: [opt] delete [ ] cast-expression
9128 Returns a representation of the expression. */
9131 cp_parser_delete_expression (cp_parser
* parser
)
9133 bool global_scope_p
;
9136 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9138 /* Look for the optional `::' operator. */
9140 = (cp_parser_global_scope_opt (parser
,
9141 /*current_scope_valid_p=*/false)
9143 /* Look for the `delete' keyword. */
9144 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9145 /* See if the array syntax is in use. */
9146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9148 /* Consume the `[' token. */
9149 cp_lexer_consume_token (parser
->lexer
);
9150 /* Look for the `]' token. */
9151 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9152 /* Remember that this is the `[]' construct. */
9158 /* Parse the cast-expression. */
9159 expression
= cp_parser_simple_cast_expression (parser
);
9161 /* A delete-expression may not appear in an integral constant
9163 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9164 return error_mark_node
;
9166 /* Construct a location e.g.:
9169 with caret == start at the start of the "delete" token, and
9170 the end at the end of the final token we consumed. */
9171 location_t combined_loc
= make_location (start_loc
, start_loc
,
9173 expression
= delete_sanity (combined_loc
, expression
, NULL_TREE
, array_p
,
9174 global_scope_p
, tf_warning_or_error
);
9179 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9180 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9184 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9186 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9187 switch (token
->type
)
9193 case CPP_CLOSE_SQUARE
:
9194 case CPP_CLOSE_PAREN
:
9195 case CPP_CLOSE_BRACE
:
9196 case CPP_OPEN_BRACE
:
9200 case CPP_DEREF_STAR
:
9208 case CPP_GREATER_EQ
:
9229 case CPP_OPEN_PAREN
:
9230 /* In ((type ()) () the last () isn't a valid cast-expression,
9231 so the whole must be parsed as postfix-expression. */
9232 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9235 case CPP_OPEN_SQUARE
:
9236 /* '[' may start a primary-expression in obj-c++ and in C++11,
9237 as a lambda-expression, eg, '(void)[]{}'. */
9238 if (cxx_dialect
>= cxx11
)
9240 return c_dialect_objc ();
9243 case CPP_MINUS_MINUS
:
9244 /* '++' and '--' may or may not start a cast-expression:
9246 struct T { void operator++(int); };
9247 void f() { (T())++; }
9260 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9261 in the order: const_cast, static_cast, reinterpret_cast.
9263 Don't suggest dynamic_cast.
9265 Return the first legal cast kind found, or NULL otherwise. */
9268 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9272 /* Reuse the parser logic by attempting to build the various kinds of
9273 cast, with "complain" disabled.
9274 Identify the first such cast that is valid. */
9276 /* Don't attempt to run such logic within template processing. */
9277 if (processing_template_decl
)
9280 /* First try const_cast. */
9281 trial
= build_const_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9282 if (trial
!= error_mark_node
)
9283 return "const_cast";
9285 /* If that fails, try static_cast. */
9286 trial
= build_static_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9287 if (trial
!= error_mark_node
)
9288 return "static_cast";
9290 /* Finally, try reinterpret_cast. */
9291 trial
= build_reinterpret_cast (input_location
, dst_type
, orig_expr
,
9293 if (trial
!= error_mark_node
)
9294 return "reinterpret_cast";
9296 /* No such cast possible. */
9300 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9301 suggesting how to convert a C-style cast of the form:
9305 to a C++-style cast.
9307 The primary range of RICHLOC is asssumed to be that of the original
9308 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9309 of the parens in the C-style cast. */
9312 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9313 location_t close_paren_loc
, tree orig_expr
,
9316 /* This function is non-trivial, so bail out now if the warning isn't
9317 going to be emitted. */
9318 if (!warn_old_style_cast
)
9321 /* Try to find a legal C++ cast, trying them in order:
9322 const_cast, static_cast, reinterpret_cast. */
9323 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9324 if (!cast_suggestion
)
9327 /* Replace the open paren with "CAST_SUGGESTION<". */
9329 pp_printf (&pp
, "%s<", cast_suggestion
);
9330 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9332 /* Replace the close paren with "> (". */
9333 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9335 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9336 rich_loc
->add_fixit_insert_after (")");
9340 /* Parse a cast-expression.
9344 ( type-id ) cast-expression
9346 ADDRESS_P is true iff the unary-expression is appearing as the
9347 operand of the `&' operator. CAST_P is true if this expression is
9348 the target of a cast.
9350 Returns a representation of the expression. */
9353 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9354 bool decltype_p
, cp_id_kind
* pidk
)
9356 /* If it's a `(', then we might be looking at a cast. */
9357 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9359 tree type
= NULL_TREE
;
9360 cp_expr
expr (NULL_TREE
);
9361 int cast_expression
= 0;
9362 const char *saved_message
;
9364 /* There's no way to know yet whether or not this is a cast.
9365 For example, `(int (3))' is a unary-expression, while `(int)
9366 3' is a cast. So, we resort to parsing tentatively. */
9367 cp_parser_parse_tentatively (parser
);
9368 /* Types may not be defined in a cast. */
9369 saved_message
= parser
->type_definition_forbidden_message
;
9370 parser
->type_definition_forbidden_message
9371 = G_("types may not be defined in casts");
9372 /* Consume the `('. */
9373 matching_parens parens
;
9374 cp_token
*open_paren
= parens
.consume_open (parser
);
9375 location_t open_paren_loc
= open_paren
->location
;
9376 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9378 /* A very tricky bit is that `(struct S) { 3 }' is a
9379 compound-literal (which we permit in C++ as an extension).
9380 But, that construct is not a cast-expression -- it is a
9381 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9382 is legal; if the compound-literal were a cast-expression,
9383 you'd need an extra set of parentheses.) But, if we parse
9384 the type-id, and it happens to be a class-specifier, then we
9385 will commit to the parse at that point, because we cannot
9386 undo the action that is done when creating a new class. So,
9387 then we cannot back up and do a postfix-expression.
9389 Another tricky case is the following (c++/29234):
9391 struct S { void operator () (); };
9398 As a type-id we parse the parenthesized S()() as a function
9399 returning a function, groktypename complains and we cannot
9400 back up in this case either.
9402 Therefore, we scan ahead to the closing `)', and check to see
9403 if the tokens after the `)' can start a cast-expression. Otherwise
9404 we are dealing with an unary-expression, a postfix-expression
9407 Yet another tricky case, in C++11, is the following (c++/54891):
9411 The issue is that usually, besides the case of lambda-expressions,
9412 the parenthesized type-id cannot be followed by '[', and, eg, we
9413 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9414 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9415 we don't commit, we try a cast-expression, then an unary-expression.
9417 Save tokens so that we can put them back. */
9418 cp_lexer_save_tokens (parser
->lexer
);
9420 /* We may be looking at a cast-expression. */
9421 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9422 /*consume_paren=*/true))
9424 = cp_parser_tokens_start_cast_expression (parser
);
9426 /* Roll back the tokens we skipped. */
9427 cp_lexer_rollback_tokens (parser
->lexer
);
9428 /* If we aren't looking at a cast-expression, simulate an error so
9429 that the call to cp_parser_error_occurred below returns true. */
9430 if (!cast_expression
)
9431 cp_parser_simulate_error (parser
);
9434 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9435 parser
->in_type_id_in_expr_p
= true;
9436 /* Look for the type-id. */
9437 type
= cp_parser_type_id (parser
);
9438 /* Look for the closing `)'. */
9439 cp_token
*close_paren
= parens
.require_close (parser
);
9441 close_paren_loc
= close_paren
->location
;
9442 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9445 /* Restore the saved message. */
9446 parser
->type_definition_forbidden_message
= saved_message
;
9448 /* At this point this can only be either a cast or a
9449 parenthesized ctor such as `(T ())' that looks like a cast to
9450 function returning T. */
9451 if (!cp_parser_error_occurred (parser
))
9453 /* Only commit if the cast-expression doesn't start with
9454 '++', '--', or '[' in C++11. */
9455 if (cast_expression
> 0)
9456 cp_parser_commit_to_topmost_tentative_parse (parser
);
9458 expr
= cp_parser_cast_expression (parser
,
9459 /*address_p=*/false,
9461 /*decltype_p=*/false,
9464 if (cp_parser_parse_definitely (parser
))
9466 /* Warn about old-style casts, if so requested. */
9467 if (warn_old_style_cast
9468 && !in_system_header_at (input_location
)
9469 && !VOID_TYPE_P (type
)
9470 && current_lang_name
!= lang_name_c
)
9472 gcc_rich_location
rich_loc (input_location
);
9473 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9475 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9476 "use of old-style cast to %q#T", type
);
9479 /* Only type conversions to integral or enumeration types
9480 can be used in constant-expressions. */
9481 if (!cast_valid_in_integral_constant_expression_p (type
)
9482 && cp_parser_non_integral_constant_expression (parser
,
9484 return error_mark_node
;
9486 /* Perform the cast. */
9490 with start==caret at the open paren, extending to the
9492 location_t cast_loc
= make_location (open_paren_loc
,
9494 expr
.get_finish ());
9495 expr
= build_c_cast (cast_loc
, type
, expr
);
9500 cp_parser_abort_tentative_parse (parser
);
9503 /* If we get here, then it's not a cast, so it must be a
9504 unary-expression. */
9505 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9506 cast_p
, decltype_p
);
9509 /* Parse a binary expression of the general form:
9513 pm-expression .* cast-expression
9514 pm-expression ->* cast-expression
9516 multiplicative-expression:
9518 multiplicative-expression * pm-expression
9519 multiplicative-expression / pm-expression
9520 multiplicative-expression % pm-expression
9522 additive-expression:
9523 multiplicative-expression
9524 additive-expression + multiplicative-expression
9525 additive-expression - multiplicative-expression
9529 shift-expression << additive-expression
9530 shift-expression >> additive-expression
9532 relational-expression:
9534 relational-expression < shift-expression
9535 relational-expression > shift-expression
9536 relational-expression <= shift-expression
9537 relational-expression >= shift-expression
9541 relational-expression:
9542 relational-expression <? shift-expression
9543 relational-expression >? shift-expression
9545 equality-expression:
9546 relational-expression
9547 equality-expression == relational-expression
9548 equality-expression != relational-expression
9552 and-expression & equality-expression
9554 exclusive-or-expression:
9556 exclusive-or-expression ^ and-expression
9558 inclusive-or-expression:
9559 exclusive-or-expression
9560 inclusive-or-expression | exclusive-or-expression
9562 logical-and-expression:
9563 inclusive-or-expression
9564 logical-and-expression && inclusive-or-expression
9566 logical-or-expression:
9567 logical-and-expression
9568 logical-or-expression || logical-and-expression
9570 All these are implemented with a single function like:
9573 simple-cast-expression
9574 binary-expression <token> binary-expression
9576 CAST_P is true if this expression is the target of a cast.
9578 The binops_by_token map is used to get the tree codes for each <token> type.
9579 binary-expressions are associated according to a precedence table. */
9581 #define TOKEN_PRECEDENCE(token) \
9582 (((token->type == CPP_GREATER \
9583 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9584 && !parser->greater_than_is_operator_p) \
9585 ? PREC_NOT_OPERATOR \
9586 : binops_by_token[token->type].prec)
9589 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9590 bool no_toplevel_fold_p
,
9592 enum cp_parser_prec prec
,
9595 cp_parser_expression_stack stack
;
9596 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9597 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9598 cp_parser_expression_stack_entry current
;
9601 enum tree_code rhs_type
;
9602 enum cp_parser_prec new_prec
, lookahead_prec
;
9605 /* Parse the first expression. */
9606 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9607 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9608 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9609 cast_p
, decltype_p
, pidk
);
9610 current
.prec
= prec
;
9612 if (cp_parser_error_occurred (parser
))
9613 return error_mark_node
;
9617 /* Get an operator token. */
9618 token
= cp_lexer_peek_token (parser
->lexer
);
9620 if (warn_cxx11_compat
9621 && token
->type
== CPP_RSHIFT
9622 && !parser
->greater_than_is_operator_p
)
9624 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9625 "%<>>%> operator is treated"
9626 " as two right angle brackets in C++11"))
9627 inform (token
->location
,
9628 "suggest parentheses around %<>>%> expression");
9631 new_prec
= TOKEN_PRECEDENCE (token
);
9632 if (new_prec
!= PREC_NOT_OPERATOR
9633 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9634 /* This is a fold-expression; handle it later. */
9635 new_prec
= PREC_NOT_OPERATOR
;
9637 /* Popping an entry off the stack means we completed a subexpression:
9638 - either we found a token which is not an operator (`>' where it is not
9639 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9640 will happen repeatedly;
9641 - or, we found an operator which has lower priority. This is the case
9642 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9644 if (new_prec
<= current
.prec
)
9653 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9654 current
.loc
= token
->location
;
9656 /* We used the operator token. */
9657 cp_lexer_consume_token (parser
->lexer
);
9659 /* For "false && x" or "true || x", x will never be executed;
9660 disable warnings while evaluating it. */
9661 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
9662 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
9663 || (current
.tree_type
== TRUTH_ORIF_EXPR
9664 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
9666 disable_warnings_sp
= sp
;
9667 ++c_inhibit_evaluation_warnings
;
9670 /* Extract another operand. It may be the RHS of this expression
9671 or the LHS of a new, higher priority expression. */
9672 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9673 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9674 rhs
= cp_parser_simple_cast_expression (parser
);
9676 /* Get another operator token. Look up its precedence to avoid
9677 building a useless (immediately popped) stack entry for common
9678 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9679 token
= cp_lexer_peek_token (parser
->lexer
);
9680 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9681 if (lookahead_prec
!= PREC_NOT_OPERATOR
9682 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9683 lookahead_prec
= PREC_NOT_OPERATOR
;
9684 if (lookahead_prec
> new_prec
)
9686 /* ... and prepare to parse the RHS of the new, higher priority
9687 expression. Since precedence levels on the stack are
9688 monotonically increasing, we do not have to care about
9693 current
.lhs_type
= rhs_type
;
9694 current
.prec
= new_prec
;
9695 new_prec
= lookahead_prec
;
9699 lookahead_prec
= new_prec
;
9700 /* If the stack is not empty, we have parsed into LHS the right side
9701 (`4' in the example above) of an expression we had suspended.
9702 We can use the information on the stack to recover the LHS (`3')
9703 from the stack together with the tree code (`MULT_EXPR'), and
9704 the precedence of the higher level subexpression
9705 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9706 which will be used to actually build the additive expression. */
9708 rhs_type
= current
.lhs_type
;
9713 /* Undo the disabling of warnings done above. */
9714 if (sp
== disable_warnings_sp
)
9716 disable_warnings_sp
= NULL
;
9717 --c_inhibit_evaluation_warnings
;
9720 if (warn_logical_not_paren
9721 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9722 && current
.lhs_type
== TRUTH_NOT_EXPR
9723 /* Avoid warning for !!x == y. */
9724 && (TREE_CODE (current
.lhs
) != NE_EXPR
9725 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9726 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9727 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9728 /* Avoid warning for !b == y where b is boolean. */
9729 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9730 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9732 /* Avoid warning for !!b == y where b is boolean. */
9733 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9734 || TREE_TYPE (current
.lhs
) == NULL_TREE
9735 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9736 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9737 current
.lhs
, maybe_constant_value (rhs
));
9741 location_t combined_loc
= make_location (current
.loc
,
9742 current
.lhs
.get_start (),
9745 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9746 ERROR_MARK for everything that is not a binary expression.
9747 This makes warn_about_parentheses miss some warnings that
9748 involve unary operators. For unary expressions we should
9749 pass the correct tree_code unless the unary expression was
9750 surrounded by parentheses.
9752 if (no_toplevel_fold_p
9753 && lookahead_prec
<= current
.prec
9756 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9757 current
.lhs
= error_mark_node
;
9760 current
.lhs
.maybe_add_location_wrapper ();
9761 rhs
.maybe_add_location_wrapper ();
9763 = build_min (current
.tree_type
,
9764 TREE_CODE_CLASS (current
.tree_type
)
9766 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9767 current
.lhs
.get_value (), rhs
.get_value ());
9768 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9773 op_location_t
op_loc (current
.loc
, combined_loc
);
9774 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9775 current
.lhs
, current
.lhs_type
,
9776 rhs
, rhs_type
, &overload
,
9777 complain_flags (decltype_p
));
9778 /* TODO: build_x_binary_op doesn't always honor the location. */
9779 current
.lhs
.set_location (combined_loc
);
9781 current
.lhs_type
= current
.tree_type
;
9783 /* If the binary operator required the use of an overloaded operator,
9784 then this expression cannot be an integral constant-expression.
9785 An overloaded operator can be used even if both operands are
9786 otherwise permissible in an integral constant-expression if at
9787 least one of the operands is of enumeration type. */
9790 && cp_parser_non_integral_constant_expression (parser
,
9792 return error_mark_node
;
9799 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9800 bool no_toplevel_fold_p
,
9801 enum cp_parser_prec prec
,
9804 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9805 /*decltype*/false, prec
, pidk
);
9808 /* Parse the `? expression : assignment-expression' part of a
9809 conditional-expression. The LOGICAL_OR_EXPR is the
9810 logical-or-expression that started the conditional-expression.
9811 Returns a representation of the entire conditional-expression.
9813 This routine is used by cp_parser_assignment_expression.
9815 ? expression : assignment-expression
9819 ? : assignment-expression */
9822 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9824 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9825 cp_expr assignment_expr
;
9826 struct cp_token
*token
;
9827 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9829 /* Consume the `?' token. */
9830 cp_lexer_consume_token (parser
->lexer
);
9831 token
= cp_lexer_peek_token (parser
->lexer
);
9832 if (cp_parser_allow_gnu_extensions_p (parser
)
9833 && token
->type
== CPP_COLON
)
9835 pedwarn (token
->location
, OPT_Wpedantic
,
9836 "ISO C++ does not allow %<?:%> with omitted middle operand");
9837 /* Implicit true clause. */
9839 c_inhibit_evaluation_warnings
+=
9840 folded_logical_or_expr
== truthvalue_true_node
;
9841 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9845 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9846 parser
->colon_corrects_to_scope_p
= false;
9847 /* Parse the expression. */
9848 c_inhibit_evaluation_warnings
+=
9849 folded_logical_or_expr
== truthvalue_false_node
;
9850 expr
= cp_parser_expression (parser
);
9851 c_inhibit_evaluation_warnings
+=
9852 ((folded_logical_or_expr
== truthvalue_true_node
)
9853 - (folded_logical_or_expr
== truthvalue_false_node
));
9854 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9857 /* The next token should be a `:'. */
9858 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9859 /* Parse the assignment-expression. */
9860 assignment_expr
= cp_parser_assignment_expression (parser
);
9861 c_inhibit_evaluation_warnings
-=
9862 folded_logical_or_expr
== truthvalue_true_node
;
9865 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9866 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9867 with the caret at the "?", ranging from the start of
9868 the logical_or_expr to the end of the assignment_expr. */
9869 loc
= make_location (loc
,
9870 logical_or_expr
.get_start (),
9871 assignment_expr
.get_finish ());
9873 /* Build the conditional-expression. */
9874 return build_x_conditional_expr (loc
, logical_or_expr
,
9877 tf_warning_or_error
);
9880 /* Parse an assignment-expression.
9882 assignment-expression:
9883 conditional-expression
9884 logical-or-expression assignment-operator assignment_expression
9888 CAST_P is true if this expression is the target of a cast.
9889 DECLTYPE_P is true if this expression is the operand of decltype.
9891 Returns a representation for the expression. */
9894 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9895 bool cast_p
, bool decltype_p
)
9899 /* If the next token is the `throw' keyword, then we're looking at
9900 a throw-expression. */
9901 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9902 expr
= cp_parser_throw_expression (parser
);
9903 /* If the next token is the `co_yield' keyword, then we're looking at
9904 a yield-expression. */
9905 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CO_YIELD
))
9906 expr
= cp_parser_yield_expression (parser
);
9907 /* Otherwise, it must be that we are looking at a
9908 logical-or-expression. */
9911 /* Parse the binary expressions (logical-or-expression). */
9912 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9914 PREC_NOT_OPERATOR
, pidk
);
9915 /* If the next token is a `?' then we're actually looking at a
9916 conditional-expression. */
9917 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9918 return cp_parser_question_colon_clause (parser
, expr
);
9921 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9923 /* If it's an assignment-operator, we're using the second
9925 enum tree_code assignment_operator
9926 = cp_parser_assignment_operator_opt (parser
);
9927 if (assignment_operator
!= ERROR_MARK
)
9929 bool non_constant_p
;
9931 /* Parse the right-hand side of the assignment. */
9932 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9935 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9936 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9938 /* An assignment may not appear in a
9939 constant-expression. */
9940 if (cp_parser_non_integral_constant_expression (parser
,
9942 return error_mark_node
;
9943 /* Build the assignment expression. Its default
9947 is the location of the '=' token as the
9948 caret, ranging from the start of the lhs to the
9950 loc
= make_location (loc
,
9953 expr
= build_x_modify_expr (loc
, expr
,
9954 assignment_operator
,
9956 complain_flags (decltype_p
));
9957 /* TODO: build_x_modify_expr doesn't honor the location,
9958 so we must set it here. */
9959 expr
.set_location (loc
);
9967 /* Parse an (optional) assignment-operator.
9969 assignment-operator: one of
9970 = *= /= %= += -= >>= <<= &= ^= |=
9974 assignment-operator: one of
9977 If the next token is an assignment operator, the corresponding tree
9978 code is returned, and the token is consumed. For example, for
9979 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9980 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9981 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9982 operator, ERROR_MARK is returned. */
9984 static enum tree_code
9985 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9990 /* Peek at the next token. */
9991 token
= cp_lexer_peek_token (parser
->lexer
);
9993 switch (token
->type
)
10004 op
= TRUNC_DIV_EXPR
;
10008 op
= TRUNC_MOD_EXPR
;
10019 case CPP_RSHIFT_EQ
:
10023 case CPP_LSHIFT_EQ
:
10040 /* Nothing else is an assignment operator. */
10044 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10045 if (op
!= ERROR_MARK
10046 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10049 /* If it was an assignment operator, consume it. */
10050 if (op
!= ERROR_MARK
)
10051 cp_lexer_consume_token (parser
->lexer
);
10056 /* Parse an expression.
10059 assignment-expression
10060 expression , assignment-expression
10062 CAST_P is true if this expression is the target of a cast.
10063 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10064 except possibly parenthesized or on the RHS of a comma (N3276).
10065 WARN_COMMA_P is true if a comma should be diagnosed.
10067 Returns a representation of the expression. */
10070 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10071 bool cast_p
, bool decltype_p
, bool warn_comma_p
)
10073 cp_expr expression
= NULL_TREE
;
10074 location_t loc
= UNKNOWN_LOCATION
;
10078 cp_expr assignment_expression
;
10080 /* Parse the next assignment-expression. */
10081 assignment_expression
10082 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
10084 /* We don't create a temporary for a call that is the immediate operand
10085 of decltype or on the RHS of a comma. But when we see a comma, we
10086 need to create a temporary for a call on the LHS. */
10087 if (decltype_p
&& !processing_template_decl
10088 && TREE_CODE (assignment_expression
) == CALL_EXPR
10089 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
10090 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10091 assignment_expression
10092 = build_cplus_new (TREE_TYPE (assignment_expression
),
10093 assignment_expression
, tf_warning_or_error
);
10095 /* If this is the first assignment-expression, we can just
10098 expression
= assignment_expression
;
10101 /* Create a location with caret at the comma, ranging
10102 from the start of the LHS to the end of the RHS. */
10103 loc
= make_location (loc
,
10104 expression
.get_start (),
10105 assignment_expression
.get_finish ());
10106 expression
= build_x_compound_expr (loc
, expression
,
10107 assignment_expression
,
10108 complain_flags (decltype_p
));
10109 expression
.set_location (loc
);
10111 /* If the next token is not a comma, or we're in a fold-expression, then
10112 we are done with the expression. */
10113 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
10114 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10116 /* Consume the `,'. */
10117 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10120 /* [depr.comma.subscript]: A comma expression appearing as
10121 the expr-or-braced-init-list of a subscripting expression
10122 is deprecated. A parenthesized comma expression is not
10124 warning_at (loc
, OPT_Wcomma_subscript
,
10125 "top-level comma expression in array subscript "
10127 warn_comma_p
= false;
10129 cp_lexer_consume_token (parser
->lexer
);
10130 /* A comma operator cannot appear in a constant-expression. */
10131 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
10132 expression
= error_mark_node
;
10138 /* Parse a constant-expression.
10140 constant-expression:
10141 conditional-expression
10143 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10144 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10145 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10146 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10147 only parse a conditional-expression, otherwise parse an
10148 assignment-expression. See below for rationale. */
10151 cp_parser_constant_expression (cp_parser
* parser
,
10152 bool allow_non_constant_p
,
10153 bool *non_constant_p
,
10156 bool saved_integral_constant_expression_p
;
10157 bool saved_allow_non_integral_constant_expression_p
;
10158 bool saved_non_integral_constant_expression_p
;
10159 cp_expr expression
;
10161 /* It might seem that we could simply parse the
10162 conditional-expression, and then check to see if it were
10163 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10164 one that the compiler can figure out is constant, possibly after
10165 doing some simplifications or optimizations. The standard has a
10166 precise definition of constant-expression, and we must honor
10167 that, even though it is somewhat more restrictive.
10173 is not a legal declaration, because `(2, 3)' is not a
10174 constant-expression. The `,' operator is forbidden in a
10175 constant-expression. However, GCC's constant-folding machinery
10176 will fold this operation to an INTEGER_CST for `3'. */
10178 /* Save the old settings. */
10179 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10180 saved_allow_non_integral_constant_expression_p
10181 = parser
->allow_non_integral_constant_expression_p
;
10182 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10183 /* We are now parsing a constant-expression. */
10184 parser
->integral_constant_expression_p
= true;
10185 parser
->allow_non_integral_constant_expression_p
10186 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10187 parser
->non_integral_constant_expression_p
= false;
10188 /* Although the grammar says "conditional-expression", when not STRICT_P,
10189 we parse an "assignment-expression", which also permits
10190 "throw-expression" and the use of assignment operators. In the case
10191 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10192 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10193 actually essential that we look for an assignment-expression.
10194 For example, cp_parser_initializer_clauses uses this function to
10195 determine whether a particular assignment-expression is in fact
10199 /* Parse the binary expressions (logical-or-expression). */
10200 expression
= cp_parser_binary_expression (parser
, false, false, false,
10201 PREC_NOT_OPERATOR
, NULL
);
10202 /* If the next token is a `?' then we're actually looking at
10203 a conditional-expression; otherwise we're done. */
10204 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10205 expression
= cp_parser_question_colon_clause (parser
, expression
);
10208 expression
= cp_parser_assignment_expression (parser
);
10209 /* Restore the old settings. */
10210 parser
->integral_constant_expression_p
10211 = saved_integral_constant_expression_p
;
10212 parser
->allow_non_integral_constant_expression_p
10213 = saved_allow_non_integral_constant_expression_p
;
10214 if (cxx_dialect
>= cxx11
)
10216 /* Require an rvalue constant expression here; that's what our
10217 callers expect. Reference constant expressions are handled
10218 separately in e.g. cp_parser_template_argument. */
10219 tree decay
= expression
;
10220 if (TREE_TYPE (expression
)
10221 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10222 decay
= build_address (expression
);
10223 bool is_const
= is_rvalue_constant_expression (decay
);
10224 parser
->non_integral_constant_expression_p
= !is_const
;
10225 if (!is_const
&& !allow_non_constant_p
)
10226 require_rvalue_constant_expression (decay
);
10228 if (allow_non_constant_p
)
10229 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10230 parser
->non_integral_constant_expression_p
10231 = saved_non_integral_constant_expression_p
;
10236 /* Parse __builtin_offsetof.
10238 offsetof-expression:
10239 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10241 offsetof-member-designator:
10243 | offsetof-member-designator "." id-expression
10244 | offsetof-member-designator "[" expression "]"
10245 | offsetof-member-designator "->" id-expression */
10248 cp_parser_builtin_offsetof (cp_parser
*parser
)
10250 int save_ice_p
, save_non_ice_p
;
10255 location_t finish_loc
;
10257 /* We're about to accept non-integral-constant things, but will
10258 definitely yield an integral constant expression. Save and
10259 restore these values around our local parsing. */
10260 save_ice_p
= parser
->integral_constant_expression_p
;
10261 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10263 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10265 /* Consume the "__builtin_offsetof" token. */
10266 cp_lexer_consume_token (parser
->lexer
);
10267 /* Consume the opening `('. */
10268 matching_parens parens
;
10269 parens
.require_open (parser
);
10270 /* Parse the type-id. */
10271 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10273 const char *saved_message
= parser
->type_definition_forbidden_message
;
10274 parser
->type_definition_forbidden_message
10275 = G_("types may not be defined within %<__builtin_offsetof%>");
10276 type
= cp_parser_type_id (parser
);
10277 parser
->type_definition_forbidden_message
= saved_message
;
10279 /* Look for the `,'. */
10280 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10281 token
= cp_lexer_peek_token (parser
->lexer
);
10283 /* Build the (type *)null that begins the traditional offsetof macro. */
10285 = build_static_cast (input_location
, build_pointer_type (type
),
10286 null_pointer_node
, tf_warning_or_error
);
10288 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10289 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10290 true, &dummy
, token
->location
);
10293 token
= cp_lexer_peek_token (parser
->lexer
);
10294 switch (token
->type
)
10296 case CPP_OPEN_SQUARE
:
10297 /* offsetof-member-designator "[" expression "]" */
10298 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10303 /* offsetof-member-designator "->" identifier */
10304 expr
= grok_array_decl (token
->location
, expr
,
10305 integer_zero_node
, false);
10309 /* offsetof-member-designator "." identifier */
10310 cp_lexer_consume_token (parser
->lexer
);
10311 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10312 expr
, true, &dummy
,
10316 case CPP_CLOSE_PAREN
:
10317 /* Consume the ")" token. */
10318 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10319 cp_lexer_consume_token (parser
->lexer
);
10323 /* Error. We know the following require will fail, but
10324 that gives the proper error message. */
10325 parens
.require_close (parser
);
10326 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10327 expr
= error_mark_node
;
10333 /* Make a location of the form:
10334 __builtin_offsetof (struct s, f)
10335 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10336 with caret at the type-id, ranging from the start of the
10337 "_builtin_offsetof" token to the close paren. */
10338 loc
= make_location (loc
, start_loc
, finish_loc
);
10339 /* The result will be an INTEGER_CST, so we need to explicitly
10340 preserve the location. */
10341 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10344 parser
->integral_constant_expression_p
= save_ice_p
;
10345 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10347 expr
= expr
.maybe_add_location_wrapper ();
10351 /* Parse a trait expression.
10353 Returns a representation of the expression, the underlying type
10354 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10357 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10359 cp_trait_kind kind
;
10360 tree type1
, type2
= NULL_TREE
;
10361 bool binary
= false;
10362 bool variadic
= false;
10366 case RID_HAS_NOTHROW_ASSIGN
:
10367 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10369 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10370 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10372 case RID_HAS_NOTHROW_COPY
:
10373 kind
= CPTK_HAS_NOTHROW_COPY
;
10375 case RID_HAS_TRIVIAL_ASSIGN
:
10376 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10378 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10379 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10381 case RID_HAS_TRIVIAL_COPY
:
10382 kind
= CPTK_HAS_TRIVIAL_COPY
;
10384 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10385 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10387 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10388 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10390 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10391 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10393 case RID_IS_ABSTRACT
:
10394 kind
= CPTK_IS_ABSTRACT
;
10396 case RID_IS_AGGREGATE
:
10397 kind
= CPTK_IS_AGGREGATE
;
10399 case RID_IS_BASE_OF
:
10400 kind
= CPTK_IS_BASE_OF
;
10404 kind
= CPTK_IS_CLASS
;
10407 kind
= CPTK_IS_EMPTY
;
10410 kind
= CPTK_IS_ENUM
;
10413 kind
= CPTK_IS_FINAL
;
10415 case RID_IS_LITERAL_TYPE
:
10416 kind
= CPTK_IS_LITERAL_TYPE
;
10419 kind
= CPTK_IS_POD
;
10421 case RID_IS_POLYMORPHIC
:
10422 kind
= CPTK_IS_POLYMORPHIC
;
10424 case RID_IS_SAME_AS
:
10425 kind
= CPTK_IS_SAME_AS
;
10428 case RID_IS_STD_LAYOUT
:
10429 kind
= CPTK_IS_STD_LAYOUT
;
10431 case RID_IS_TRIVIAL
:
10432 kind
= CPTK_IS_TRIVIAL
;
10434 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10435 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10438 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10439 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10442 case RID_IS_TRIVIALLY_COPYABLE
:
10443 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10446 kind
= CPTK_IS_UNION
;
10448 case RID_UNDERLYING_TYPE
:
10449 kind
= CPTK_UNDERLYING_TYPE
;
10454 case RID_DIRECT_BASES
:
10455 kind
= CPTK_DIRECT_BASES
;
10457 case RID_IS_ASSIGNABLE
:
10458 kind
= CPTK_IS_ASSIGNABLE
;
10461 case RID_IS_CONSTRUCTIBLE
:
10462 kind
= CPTK_IS_CONSTRUCTIBLE
;
10466 gcc_unreachable ();
10469 /* Get location of initial token. */
10470 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10472 /* Consume the token. */
10473 cp_lexer_consume_token (parser
->lexer
);
10475 matching_parens parens
;
10476 parens
.require_open (parser
);
10479 type_id_in_expr_sentinel
s (parser
);
10480 type1
= cp_parser_type_id (parser
);
10483 if (type1
== error_mark_node
)
10484 return error_mark_node
;
10488 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10491 type_id_in_expr_sentinel
s (parser
);
10492 type2
= cp_parser_type_id (parser
);
10495 if (type2
== error_mark_node
)
10496 return error_mark_node
;
10500 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10502 cp_lexer_consume_token (parser
->lexer
);
10503 tree elt
= cp_parser_type_id (parser
);
10504 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10506 cp_lexer_consume_token (parser
->lexer
);
10507 elt
= make_pack_expansion (elt
);
10509 if (elt
== error_mark_node
)
10510 return error_mark_node
;
10511 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10515 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10516 parens
.require_close (parser
);
10518 /* Construct a location of the form:
10519 __is_trivially_copyable(_Tp)
10520 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10521 with start == caret, finishing at the close-paren. */
10522 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10524 /* Complete the trait expression, which may mean either processing
10525 the trait expr now or saving it for template instantiation. */
10528 case CPTK_UNDERLYING_TYPE
:
10529 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10531 return cp_expr (finish_bases (type1
, false), trait_loc
);
10532 case CPTK_DIRECT_BASES
:
10533 return cp_expr (finish_bases (type1
, true), trait_loc
);
10535 return finish_trait_expr (trait_loc
, kind
, type1
, type2
);
10539 /* Parse a lambda expression.
10542 lambda-introducer lambda-declarator [opt] compound-statement
10544 Returns a representation of the expression. */
10547 cp_parser_lambda_expression (cp_parser
* parser
)
10549 tree lambda_expr
= build_lambda_expr ();
10552 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10553 cp_token_position start
= 0;
10555 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10557 if (cxx_dialect
>= cxx20
)
10558 /* C++20 allows lambdas in unevaluated context. */;
10559 else if (cp_unevaluated_operand
)
10561 if (!token
->error_reported
)
10563 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10564 "lambda-expression in unevaluated context"
10565 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10566 token
->error_reported
= true;
10570 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10572 if (!token
->error_reported
)
10574 error_at (token
->location
, "lambda-expression in template-argument"
10575 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10576 token
->error_reported
= true;
10581 /* We may be in the middle of deferred access check. Disable
10583 push_deferring_access_checks (dk_no_deferred
);
10585 cp_parser_lambda_introducer (parser
, lambda_expr
);
10586 if (cp_parser_error_occurred (parser
))
10587 return error_mark_node
;
10589 type
= begin_lambda_type (lambda_expr
);
10590 if (type
== error_mark_node
)
10591 return error_mark_node
;
10593 record_lambda_scope (lambda_expr
);
10595 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10596 determine_visibility (TYPE_NAME (type
));
10598 /* Now that we've started the type, add the capture fields for any
10599 explicit captures. */
10600 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10603 /* Inside the class, surrounding template-parameter-lists do not apply. */
10604 unsigned int saved_num_template_parameter_lists
10605 = parser
->num_template_parameter_lists
;
10606 unsigned char in_statement
= parser
->in_statement
;
10607 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10608 bool fully_implicit_function_template_p
10609 = parser
->fully_implicit_function_template_p
;
10610 tree implicit_template_parms
= parser
->implicit_template_parms
;
10611 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10612 bool auto_is_implicit_function_template_parm_p
10613 = parser
->auto_is_implicit_function_template_parm_p
;
10615 parser
->num_template_parameter_lists
= 0;
10616 parser
->in_statement
= 0;
10617 parser
->in_switch_statement_p
= false;
10618 parser
->fully_implicit_function_template_p
= false;
10619 parser
->implicit_template_parms
= 0;
10620 parser
->implicit_template_scope
= 0;
10621 parser
->auto_is_implicit_function_template_parm_p
= false;
10623 /* The body of a lambda in a discarded statement is not discarded. */
10624 bool discarded
= in_discarded_stmt
;
10625 in_discarded_stmt
= 0;
10627 /* By virtue of defining a local class, a lambda expression has access to
10628 the private variables of enclosing classes. */
10630 if (cp_parser_start_tentative_firewall (parser
))
10633 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10635 if (ok
&& cp_parser_error_occurred (parser
))
10640 cp_parser_lambda_body (parser
, lambda_expr
);
10642 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10644 if (cp_parser_skip_to_closing_brace (parser
))
10645 cp_lexer_consume_token (parser
->lexer
);
10648 /* The capture list was built up in reverse order; fix that now. */
10649 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10650 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10653 maybe_add_lambda_conv_op (type
);
10655 finish_struct (type
, /*attributes=*/NULL_TREE
);
10657 in_discarded_stmt
= discarded
;
10659 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10660 parser
->in_statement
= in_statement
;
10661 parser
->in_switch_statement_p
= in_switch_statement_p
;
10662 parser
->fully_implicit_function_template_p
10663 = fully_implicit_function_template_p
;
10664 parser
->implicit_template_parms
= implicit_template_parms
;
10665 parser
->implicit_template_scope
= implicit_template_scope
;
10666 parser
->auto_is_implicit_function_template_parm_p
10667 = auto_is_implicit_function_template_parm_p
;
10670 /* This field is only used during parsing of the lambda. */
10671 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10673 /* This lambda shouldn't have any proxies left at this point. */
10674 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10675 /* And now that we're done, push proxies for an enclosing lambda. */
10676 insert_pending_capture_proxies ();
10678 /* Update the lambda expression to a range. */
10679 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10684 lambda_expr
= build_lambda_object (lambda_expr
);
10686 lambda_expr
= error_mark_node
;
10688 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10690 pop_deferring_access_checks ();
10692 return lambda_expr
;
10695 /* Parse the beginning of a lambda expression.
10698 [ lambda-capture [opt] ]
10700 LAMBDA_EXPR is the current representation of the lambda expression. */
10703 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10705 /* Need commas after the first capture. */
10708 /* Eat the leading `['. */
10709 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10711 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10713 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
)
10714 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)
10715 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10716 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10717 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10718 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10720 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10722 cp_lexer_consume_token (parser
->lexer
);
10725 if (!(at_function_scope_p () || parsing_nsdmi ()))
10726 error ("non-local lambda expression cannot have a capture-default");
10729 hash_set
<tree
, true> ids
;
10730 tree first_capture_id
= NULL_TREE
;
10731 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10733 cp_token
* capture_token
;
10735 tree capture_init_expr
;
10736 cp_id_kind idk
= CP_ID_KIND_NONE
;
10737 bool explicit_init_p
= false;
10739 enum capture_kind_type
10744 enum capture_kind_type capture_kind
= BY_COPY
;
10746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10748 error ("expected end of capture-list");
10755 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10757 /* Possibly capture `this'. */
10758 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10760 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10761 if (cxx_dialect
< cxx20
10762 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10763 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10764 "with by-copy capture default");
10765 cp_lexer_consume_token (parser
->lexer
);
10766 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10767 pedwarn (input_location
, 0,
10768 "already captured %qD in lambda expression",
10771 add_capture (lambda_expr
, /*id=*/this_identifier
,
10772 /*initializer=*/finish_this_expr (),
10773 /*by_reference_p=*/true, explicit_init_p
);
10777 /* Possibly capture `*this'. */
10778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10779 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10781 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10782 if (cxx_dialect
< cxx17
)
10783 pedwarn (loc
, 0, "%<*this%> capture only available with "
10784 "%<-std=c++17%> or %<-std=gnu++17%>");
10785 cp_lexer_consume_token (parser
->lexer
);
10786 cp_lexer_consume_token (parser
->lexer
);
10787 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10788 pedwarn (input_location
, 0,
10789 "already captured %qD in lambda expression",
10792 add_capture (lambda_expr
, /*id=*/this_identifier
,
10793 /*initializer=*/finish_this_expr (),
10794 /*by_reference_p=*/false, explicit_init_p
);
10798 /* But reject `&this'. */
10799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10800 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10802 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10803 "%<this%> cannot be captured by reference");
10804 cp_lexer_consume_token (parser
->lexer
);
10805 cp_lexer_consume_token (parser
->lexer
);
10809 /* Remember whether we want to capture as a reference or not. */
10810 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10812 capture_kind
= BY_REFERENCE
;
10813 cp_lexer_consume_token (parser
->lexer
);
10816 bool init_pack_expansion
= false;
10817 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
10818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10820 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10821 if (cxx_dialect
< cxx20
)
10822 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
10823 "%<-std=c++20%> or %<-std=gnu++20%>");
10824 cp_lexer_consume_token (parser
->lexer
);
10825 init_pack_expansion
= true;
10828 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
10829 if (init_pack_expansion
&& capture_kind
!= BY_REFERENCE
10830 && cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10832 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
10833 0, "%<&%> should come before %<...%>");
10834 capture_kind
= BY_REFERENCE
;
10835 cp_lexer_consume_token (parser
->lexer
);
10838 /* Get the identifier. */
10839 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10840 capture_id
= cp_parser_identifier (parser
);
10842 if (capture_id
== error_mark_node
)
10843 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10844 delimiters, but I modified this to stop on unnested ']' as well. It
10845 was already changed to stop on unnested '}', so the
10846 "closing_parenthesis" name is no more misleading with my change. */
10848 cp_parser_skip_to_closing_parenthesis (parser
,
10849 /*recovering=*/true,
10851 /*consume_paren=*/true);
10855 /* Find the initializer for this capture. */
10856 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10857 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10858 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10860 bool direct
, non_constant
;
10861 /* An explicit initializer exists. */
10862 if (cxx_dialect
< cxx14
)
10863 pedwarn (input_location
, 0,
10864 "lambda capture initializers "
10865 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10866 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10867 &non_constant
, true);
10868 explicit_init_p
= true;
10869 if (capture_init_expr
== NULL_TREE
)
10871 error ("empty initializer for lambda init-capture");
10872 capture_init_expr
= error_mark_node
;
10874 if (init_pack_expansion
)
10875 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10879 const char* error_msg
;
10881 /* Turn the identifier into an id-expression. */
10883 = cp_parser_lookup_name_simple (parser
, capture_id
,
10884 capture_token
->location
);
10886 if (capture_init_expr
== error_mark_node
)
10888 unqualified_name_lookup_error (capture_id
);
10891 else if (!VAR_P (capture_init_expr
)
10892 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10894 error_at (capture_token
->location
,
10895 "capture of non-variable %qE",
10896 capture_init_expr
);
10897 if (DECL_P (capture_init_expr
))
10898 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10899 "%q#D declared here", capture_init_expr
);
10902 if (VAR_P (capture_init_expr
)
10903 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10905 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10906 "%qD with non-automatic storage duration",
10907 capture_init_expr
))
10908 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10909 "%q#D declared here", capture_init_expr
);
10914 = finish_id_expression
10919 /*integral_constant_expression_p=*/false,
10920 /*allow_non_integral_constant_expression_p=*/false,
10921 /*non_integral_constant_expression_p=*/NULL
,
10922 /*template_p=*/false,
10924 /*address_p=*/false,
10925 /*template_arg_p=*/false,
10927 capture_token
->location
);
10929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10931 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10932 cp_lexer_consume_token (parser
->lexer
);
10933 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10934 if (init_pack_expansion
)
10936 /* If what follows is an initializer, the second '...' is
10937 invalid. But for cases like [...xs...], the first one
10939 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10940 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10941 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10942 ellipsis_loc
= loc
;
10943 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
10949 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10950 && !explicit_init_p
)
10952 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10953 && capture_kind
== BY_COPY
)
10954 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10955 "of %qD redundant with by-copy capture default",
10957 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10958 && capture_kind
== BY_REFERENCE
)
10959 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10960 "capture of %qD redundant with by-reference capture "
10961 "default", capture_id
);
10964 /* Check for duplicates.
10965 Optimize for the zero or one explicit captures cases and only create
10966 the hash_set after adding second capture. */
10967 bool found
= false;
10968 if (!ids
.is_empty ())
10969 found
= ids
.add (capture_id
);
10970 else if (first_capture_id
== NULL_TREE
)
10971 first_capture_id
= capture_id
;
10972 else if (capture_id
== first_capture_id
)
10976 ids
.add (first_capture_id
);
10977 ids
.add (capture_id
);
10980 pedwarn (input_location
, 0,
10981 "already captured %qD in lambda expression", capture_id
);
10983 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
10984 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10987 /* If there is any qualification still in effect, clear it
10988 now; we will be starting fresh with the next capture. */
10989 parser
->scope
= NULL_TREE
;
10990 parser
->qualifying_scope
= NULL_TREE
;
10991 parser
->object_scope
= NULL_TREE
;
10994 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10997 /* Parse the (optional) middle of a lambda expression.
11000 < template-parameter-list [opt] >
11001 requires-clause [opt]
11002 ( parameter-declaration-clause [opt] )
11003 attribute-specifier [opt]
11004 decl-specifier-seq [opt]
11005 exception-specification [opt]
11006 lambda-return-type-clause [opt]
11007 requires-clause [opt]
11009 LAMBDA_EXPR is the current representation of the lambda expression. */
11012 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
11014 /* 5.1.1.4 of the standard says:
11015 If a lambda-expression does not include a lambda-declarator, it is as if
11016 the lambda-declarator were ().
11017 This means an empty parameter list, no attributes, and no exception
11019 tree param_list
= void_list_node
;
11020 tree std_attrs
= NULL_TREE
;
11021 tree gnu_attrs
= NULL_TREE
;
11022 tree exception_spec
= NULL_TREE
;
11023 tree template_param_list
= NULL_TREE
;
11024 tree tx_qual
= NULL_TREE
;
11025 tree return_type
= NULL_TREE
;
11026 tree trailing_requires_clause
= NULL_TREE
;
11027 cp_decl_specifier_seq lambda_specs
;
11028 clear_decl_specs (&lambda_specs
);
11029 /* A lambda op() is const unless explicitly 'mutable'. */
11030 cp_cv_quals quals
= TYPE_QUAL_CONST
;
11032 /* The template-parameter-list is optional, but must begin with
11033 an opening angle if present. */
11034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
11036 if (cxx_dialect
< cxx14
)
11037 pedwarn (parser
->lexer
->next_token
->location
, 0,
11038 "lambda templates are only available with "
11039 "%<-std=c++14%> or %<-std=gnu++14%>");
11040 else if (cxx_dialect
< cxx20
)
11041 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
11042 "lambda templates are only available with "
11043 "%<-std=c++20%> or %<-std=gnu++20%>");
11045 cp_lexer_consume_token (parser
->lexer
);
11047 template_param_list
= cp_parser_template_parameter_list (parser
);
11048 cp_parser_skip_to_end_of_template_parameter_list (parser
);
11050 /* We may have a constrained generic lambda; parse the requires-clause
11051 immediately after the template-parameter-list and combine with any
11052 shorthand constraints present. */
11053 tree dreqs
= cp_parser_requires_clause_opt (parser
, true);
11056 tree reqs
= get_shorthand_constraints (current_template_parms
);
11058 reqs
= combine_constraint_expressions (reqs
, dreqs
);
11059 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
11062 /* We just processed one more parameter list. */
11063 ++parser
->num_template_parameter_lists
;
11066 /* Committee discussion supports allowing attributes here. */
11067 lambda_specs
.attributes
= cp_parser_attributes_opt (parser
);
11069 /* The parameter-declaration-clause is optional (unless
11070 template-parameter-list was given), but must begin with an
11071 opening parenthesis if present. */
11072 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11074 bool is_consteval
= false;
11075 /* For C++20, before parsing the parameter list check if there is
11076 a consteval specifier in the corresponding decl-specifier-seq. */
11077 if (cxx_dialect
>= cxx20
)
11079 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
11080 cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_KEYWORD
); n
++)
11082 if (cp_lexer_peek_nth_token (parser
->lexer
, n
)->keyword
11085 is_consteval
= true;
11091 matching_parens parens
;
11092 parens
.consume_open (parser
);
11094 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
11097 current_binding_level
->immediate_fn_ctx_p
= true;
11099 /* Parse parameters. */
11101 = cp_parser_parameter_declaration_clause
11102 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
11104 /* Default arguments shall not be specified in the
11105 parameter-declaration-clause of a lambda-declarator. */
11106 if (cxx_dialect
< cxx14
)
11107 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
11108 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
11109 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
11110 "default argument specified for lambda parameter");
11112 parens
.require_close (parser
);
11114 /* In the decl-specifier-seq of the lambda-declarator, each
11115 decl-specifier shall either be mutable or constexpr. */
11116 int declares_class_or_enum
;
11117 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
11118 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
11119 cp_parser_decl_specifier_seq (parser
,
11120 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
11121 &lambda_specs
, &declares_class_or_enum
);
11122 if (lambda_specs
.storage_class
== sc_mutable
)
11124 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
11125 quals
= TYPE_UNQUALIFIED
;
11126 if (lambda_specs
.conflicting_specifiers_p
)
11127 error_at (lambda_specs
.locations
[ds_storage_class
],
11128 "duplicate %<mutable%>");
11131 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
11133 /* Parse optional exception specification. */
11135 = cp_parser_exception_specification_opt (parser
, CP_PARSER_FLAGS_NONE
);
11137 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11139 /* Parse optional trailing return type. */
11140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
11142 cp_lexer_consume_token (parser
->lexer
);
11143 return_type
= cp_parser_trailing_type_id (parser
);
11146 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11147 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
11149 /* Parse optional trailing requires clause. */
11150 trailing_requires_clause
= cp_parser_requires_clause_opt (parser
, false);
11152 /* The function parameters must be in scope all the way until after the
11153 trailing-return-type in case of decltype. */
11154 pop_bindings_and_leave_scope ();
11156 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
11157 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11159 /* Create the function call operator.
11161 Messing with declarators like this is no uglier than building up the
11162 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11165 cp_decl_specifier_seq return_type_specs
;
11166 cp_declarator
* declarator
;
11170 clear_decl_specs (&return_type_specs
);
11171 return_type_specs
.type
= make_auto ();
11173 if (lambda_specs
.locations
[ds_constexpr
])
11175 if (cxx_dialect
>= cxx17
)
11176 return_type_specs
.locations
[ds_constexpr
]
11177 = lambda_specs
.locations
[ds_constexpr
];
11179 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
11180 "lambda only available with %<-std=c++17%> or "
11181 "%<-std=gnu++17%>");
11183 if (lambda_specs
.locations
[ds_consteval
])
11184 return_type_specs
.locations
[ds_consteval
]
11185 = lambda_specs
.locations
[ds_consteval
];
11187 p
= obstack_alloc (&declarator_obstack
, 0);
11189 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
11190 LAMBDA_EXPR_LOCATION (lambda_expr
));
11192 declarator
= make_call_declarator (declarator
, param_list
, quals
,
11193 VIRT_SPEC_UNSPECIFIED
,
11198 trailing_requires_clause
);
11199 declarator
->std_attributes
= std_attrs
;
11201 fco
= grokmethod (&return_type_specs
,
11203 chainon (gnu_attrs
, lambda_specs
.attributes
));
11204 if (fco
!= error_mark_node
)
11206 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
11207 DECL_ARTIFICIAL (fco
) = 1;
11208 /* Give the object parameter a different name. */
11209 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
11210 DECL_SET_LAMBDA_FUNCTION (fco
, true);
11212 if (template_param_list
)
11214 fco
= finish_member_template_decl (fco
);
11215 finish_template_decl (template_param_list
);
11216 --parser
->num_template_parameter_lists
;
11218 else if (parser
->fully_implicit_function_template_p
)
11219 fco
= finish_fully_implicit_template (parser
, fco
);
11221 finish_member_declaration (fco
);
11223 obstack_free (&declarator_obstack
, p
);
11225 return (fco
!= error_mark_node
);
11229 /* Parse the body of a lambda expression, which is simply
11233 but which requires special handling.
11234 LAMBDA_EXPR is the current representation of the lambda expression. */
11237 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
11239 bool nested
= (current_function_decl
!= NULL_TREE
);
11240 unsigned char local_variables_forbidden_p
11241 = parser
->local_variables_forbidden_p
;
11242 bool in_function_body
= parser
->in_function_body
;
11244 /* The body of a lambda-expression is not a subexpression of the enclosing
11249 push_function_context ();
11251 /* Still increment function_depth so that we don't GC in the
11252 middle of an expression. */
11255 vec
<tree
> omp_privatization_save
;
11256 save_omp_privatization_clauses (omp_privatization_save
);
11257 /* Clear this in case we're in the middle of a default argument. */
11258 parser
->local_variables_forbidden_p
= 0;
11259 parser
->in_function_body
= true;
11262 local_specialization_stack
s (lss_copy
);
11263 tree fco
= lambda_function (lambda_expr
);
11264 tree body
= start_lambda_function (fco
, lambda_expr
);
11266 /* Originally C++11 required us to peek for 'return expr'; and
11267 process it specially here to deduce the return type. N3638
11268 removed the need for that. */
11269 cp_parser_function_body (parser
, false);
11271 finish_lambda_function (body
);
11274 restore_omp_privatization_clauses (omp_privatization_save
);
11275 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11276 parser
->in_function_body
= in_function_body
;
11278 pop_function_context();
11283 /* Statements [gram.stmt.stmt] */
11285 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11288 add_debug_begin_stmt (location_t loc
)
11290 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11292 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11293 /* A concept is never expanded normally. */
11296 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11297 SET_EXPR_LOCATION (stmt
, loc
);
11301 /* Parse a statement.
11305 expression-statement
11307 selection-statement
11308 iteration-statement
11310 declaration-statement
11317 attribute-specifier-seq (opt) expression-statement
11318 attribute-specifier-seq (opt) compound-statement
11319 attribute-specifier-seq (opt) selection-statement
11320 attribute-specifier-seq (opt) iteration-statement
11321 attribute-specifier-seq (opt) jump-statement
11322 declaration-statement
11323 attribute-specifier-seq (opt) try-block
11326 expression-statement
11334 IN_COMPOUND is true when the statement is nested inside a
11335 cp_parser_compound_statement; this matters for certain pragmas.
11337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11338 is a (possibly labeled) if statement which is not enclosed in braces
11339 and has an else clause. This is used to implement -Wparentheses.
11341 CHAIN is a vector of if-else-if conditions. */
11344 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11345 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11346 location_t
*loc_after_labels
)
11348 tree statement
, std_attrs
= NULL_TREE
;
11350 location_t statement_location
, attrs_loc
;
11355 /* There is no statement yet. */
11356 statement
= NULL_TREE
;
11358 saved_token_sentinel
saved_tokens (parser
->lexer
);
11359 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11360 if (c_dialect_objc ())
11361 /* In obj-c++, seeing '[[' might be the either the beginning of
11362 c++11 attributes, or a nested objc-message-expression. So
11363 let's parse the c++11 attributes tentatively. */
11364 cp_parser_parse_tentatively (parser
);
11365 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11367 attrs_loc
= make_location (attrs_loc
, attrs_loc
, parser
->lexer
);
11368 if (c_dialect_objc ())
11370 if (!cp_parser_parse_definitely (parser
))
11371 std_attrs
= NULL_TREE
;
11374 /* Peek at the next token. */
11375 token
= cp_lexer_peek_token (parser
->lexer
);
11376 /* Remember the location of the first token in the statement. */
11377 cp_token
*statement_token
= token
;
11378 statement_location
= token
->location
;
11379 add_debug_begin_stmt (statement_location
);
11380 /* If this is a keyword, then that will often determine what kind of
11381 statement we have. */
11382 if (token
->type
== CPP_KEYWORD
)
11384 enum rid keyword
= token
->keyword
;
11390 /* Looks like a labeled-statement with a case label.
11391 Parse the label, and then use tail recursion to parse
11393 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11394 in_compound
= false;
11399 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11400 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11406 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11407 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11413 case RID_CO_RETURN
:
11415 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11416 statement
= cp_parser_jump_statement (parser
);
11419 /* Objective-C++ exception-handling constructs. */
11422 case RID_AT_FINALLY
:
11423 case RID_AT_SYNCHRONIZED
:
11425 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11426 statement
= cp_parser_objc_statement (parser
);
11430 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11431 statement
= cp_parser_try_block (parser
);
11434 case RID_NAMESPACE
:
11435 /* This must be a namespace alias definition. */
11436 if (std_attrs
!= NULL_TREE
)
11438 /* Attributes should be parsed as part of the
11439 declaration, so let's un-parse them. */
11440 saved_tokens
.rollback();
11441 std_attrs
= NULL_TREE
;
11443 cp_parser_declaration_statement (parser
);
11446 case RID_TRANSACTION_ATOMIC
:
11447 case RID_TRANSACTION_RELAXED
:
11448 case RID_SYNCHRONIZED
:
11449 case RID_ATOMIC_NOEXCEPT
:
11450 case RID_ATOMIC_CANCEL
:
11451 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11452 statement
= cp_parser_transaction (parser
, token
);
11454 case RID_TRANSACTION_CANCEL
:
11455 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11456 statement
= cp_parser_transaction_cancel (parser
);
11460 /* It might be a keyword like `int' that can start a
11461 declaration-statement. */
11465 else if (token
->type
== CPP_NAME
)
11467 /* If the next token is a `:', then we are looking at a
11468 labeled-statement. */
11469 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11470 if (token
->type
== CPP_COLON
)
11472 /* Looks like a labeled-statement with an ordinary label.
11473 Parse the label, and then use tail recursion to parse
11476 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11477 in_compound
= false;
11481 /* Anything that starts with a `{' must be a compound-statement. */
11482 else if (token
->type
== CPP_OPEN_BRACE
)
11484 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11485 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11487 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11488 a statement all its own. */
11489 else if (token
->type
== CPP_PRAGMA
)
11491 /* Only certain OpenMP pragmas are attached to statements, and thus
11492 are considered statements themselves. All others are not. In
11493 the context of a compound, accept the pragma as a "statement" and
11494 return so that we can check for a close brace. Otherwise we
11495 require a real statement and must go back and read one. */
11497 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11498 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11502 else if (token
->type
== CPP_EOF
)
11504 cp_parser_error (parser
, "expected statement");
11508 /* Everything else must be a declaration-statement or an
11509 expression-statement. Try for the declaration-statement
11510 first, unless we are looking at a `;', in which case we know that
11511 we have an expression-statement. */
11514 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11516 if (std_attrs
!= NULL_TREE
)
11517 /* Attributes should be parsed as part of the declaration,
11518 so let's un-parse them. */
11519 saved_tokens
.rollback();
11521 cp_parser_parse_tentatively (parser
);
11522 /* Try to parse the declaration-statement. */
11523 cp_parser_declaration_statement (parser
);
11524 /* If that worked, we're done. */
11525 if (cp_parser_parse_definitely (parser
))
11527 /* It didn't work, restore the post-attribute position. */
11529 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11531 /* All preceding labels have been parsed at this point. */
11532 if (loc_after_labels
!= NULL
)
11533 *loc_after_labels
= statement_location
;
11535 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11537 /* Look for an expression-statement instead. */
11538 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11540 /* Handle [[fallthrough]];. */
11541 if (attribute_fallthrough_p (std_attrs
))
11543 /* The next token after the fallthrough attribute is ';'. */
11544 if (statement
== NULL_TREE
)
11546 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11547 statement
= build_call_expr_internal_loc (statement_location
,
11549 void_type_node
, 0);
11550 finish_expr_stmt (statement
);
11553 warning_at (statement_location
, OPT_Wattributes
,
11554 "%<fallthrough%> attribute not followed by %<;%>");
11555 std_attrs
= NULL_TREE
;
11559 /* Set the line number for the statement. */
11560 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11561 SET_EXPR_LOCATION (statement
, statement_location
);
11563 /* Allow "[[fallthrough]];", but warn otherwise. */
11564 if (std_attrs
!= NULL_TREE
)
11565 warning_at (attrs_loc
,
11567 "attributes at the beginning of statement are ignored");
11570 /* Append ATTR to attribute list ATTRS. */
11573 attr_chainon (tree attrs
, tree attr
)
11575 if (attrs
== error_mark_node
)
11576 return error_mark_node
;
11577 if (attr
== error_mark_node
)
11578 return error_mark_node
;
11579 return chainon (attrs
, attr
);
11582 /* Parse the label for a labeled-statement, i.e.
11585 case constant-expression :
11589 case constant-expression ... constant-expression : statement
11591 When a label is parsed without errors, the label is added to the
11592 parse tree by the finish_* functions, so this function doesn't
11593 have to return the label. */
11596 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11599 tree label
= NULL_TREE
;
11600 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11602 /* The next token should be an identifier. */
11603 token
= cp_lexer_peek_token (parser
->lexer
);
11604 if (token
->type
!= CPP_NAME
11605 && token
->type
!= CPP_KEYWORD
)
11607 cp_parser_error (parser
, "expected labeled-statement");
11611 /* Remember whether this case or a user-defined label is allowed to fall
11613 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11615 parser
->colon_corrects_to_scope_p
= false;
11616 switch (token
->keyword
)
11620 tree expr
, expr_hi
;
11621 cp_token
*ellipsis
;
11623 /* Consume the `case' token. */
11624 cp_lexer_consume_token (parser
->lexer
);
11625 /* Parse the constant-expression. */
11626 expr
= cp_parser_constant_expression (parser
);
11627 if (check_for_bare_parameter_packs (expr
))
11628 expr
= error_mark_node
;
11630 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11631 if (ellipsis
->type
== CPP_ELLIPSIS
)
11633 /* Consume the `...' token. */
11634 cp_lexer_consume_token (parser
->lexer
);
11635 expr_hi
= cp_parser_constant_expression (parser
);
11636 if (check_for_bare_parameter_packs (expr_hi
))
11637 expr_hi
= error_mark_node
;
11639 /* We don't need to emit warnings here, as the common code
11640 will do this for us. */
11643 expr_hi
= NULL_TREE
;
11645 if (parser
->in_switch_statement_p
)
11647 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11648 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11650 label
= CASE_LABEL (l
);
11651 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11655 error_at (token
->location
,
11656 "case label %qE not within a switch statement",
11662 /* Consume the `default' token. */
11663 cp_lexer_consume_token (parser
->lexer
);
11665 if (parser
->in_switch_statement_p
)
11667 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11668 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11670 label
= CASE_LABEL (l
);
11671 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11675 error_at (token
->location
, "case label not within a switch statement");
11679 /* Anything else must be an ordinary label. */
11680 label
= finish_label_stmt (cp_parser_identifier (parser
));
11681 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11682 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11686 /* Require the `:' token. */
11687 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11689 /* An ordinary label may optionally be followed by attributes.
11690 However, this is only permitted if the attributes are then
11691 followed by a semicolon. This is because, for backward
11692 compatibility, when parsing
11693 lab: __attribute__ ((unused)) int i;
11694 we want the attribute to attach to "i", not "lab". */
11695 if (label
!= NULL_TREE
11696 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11699 cp_parser_parse_tentatively (parser
);
11700 attrs
= cp_parser_gnu_attributes_opt (parser
);
11701 if (attrs
== NULL_TREE
11702 /* And fallthrough always binds to the expression-statement. */
11703 || attribute_fallthrough_p (attrs
)
11704 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11705 cp_parser_abort_tentative_parse (parser
);
11706 else if (!cp_parser_parse_definitely (parser
))
11709 attributes
= attr_chainon (attributes
, attrs
);
11712 if (attributes
!= NULL_TREE
)
11713 cplus_decl_attributes (&label
, attributes
, 0);
11715 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11718 /* Parse an expression-statement.
11720 expression-statement:
11723 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11724 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11725 indicates whether this expression-statement is part of an
11726 expression statement. */
11729 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11731 tree statement
= NULL_TREE
;
11732 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11733 location_t loc
= token
->location
;
11735 /* There might be attribute fallthrough. */
11736 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11738 /* If the next token is a ';', then there is no expression
11740 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11742 statement
= cp_parser_expression (parser
);
11743 if (statement
== error_mark_node
11744 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11746 cp_parser_skip_to_end_of_block_or_statement (parser
);
11747 return error_mark_node
;
11751 /* Handle [[fallthrough]];. */
11752 if (attribute_fallthrough_p (attr
))
11754 /* The next token after the fallthrough attribute is ';'. */
11755 if (statement
== NULL_TREE
)
11756 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11757 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11758 void_type_node
, 0);
11760 warning_at (loc
, OPT_Wattributes
,
11761 "%<fallthrough%> attribute not followed by %<;%>");
11765 /* Allow "[[fallthrough]];", but warn otherwise. */
11766 if (attr
!= NULL_TREE
)
11767 warning_at (loc
, OPT_Wattributes
,
11768 "attributes at the beginning of statement are ignored");
11770 /* Give a helpful message for "A<T>::type t;" and the like. */
11771 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11772 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11774 if (TREE_CODE (statement
) == SCOPE_REF
)
11775 error_at (token
->location
, "need %<typename%> before %qE because "
11776 "%qT is a dependent scope",
11777 statement
, TREE_OPERAND (statement
, 0));
11778 else if (is_overloaded_fn (statement
)
11779 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11782 tree fn
= get_first_fn (statement
);
11783 error_at (token
->location
,
11784 "%<%T::%D%> names the constructor, not the type",
11785 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11789 /* Consume the final `;'. */
11790 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11792 if (in_statement_expr
11793 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11794 /* This is the final expression statement of a statement
11796 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11797 else if (statement
)
11798 statement
= finish_expr_stmt (statement
);
11803 /* Parse a compound-statement.
11805 compound-statement:
11806 { statement-seq [opt] }
11810 compound-statement:
11811 { label-declaration-seq [opt] statement-seq [opt] }
11813 label-declaration-seq:
11815 label-declaration-seq label-declaration
11817 Returns a tree representing the statement. */
11820 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11821 int bcs_flags
, bool function_body
)
11823 tree compound_stmt
;
11824 matching_braces braces
;
11826 /* Consume the `{'. */
11827 if (!braces
.require_open (parser
))
11828 return error_mark_node
;
11829 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11830 && !function_body
&& cxx_dialect
< cxx14
)
11831 pedwarn (input_location
, OPT_Wpedantic
,
11832 "compound-statement in %<constexpr%> function");
11833 /* Begin the compound-statement. */
11834 compound_stmt
= begin_compound_stmt (bcs_flags
);
11835 /* If the next keyword is `__label__' we have a label declaration. */
11836 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11837 cp_parser_label_declaration (parser
);
11838 /* Parse an (optional) statement-seq. */
11839 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11842 maybe_splice_retval_cleanup (compound_stmt
);
11844 /* Finish the compound-statement. */
11845 finish_compound_stmt (compound_stmt
);
11846 /* Consume the `}'. */
11847 braces
.require_close (parser
);
11849 return compound_stmt
;
11852 /* Parse an (optional) statement-seq.
11856 statement-seq [opt] statement */
11859 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11861 /* Scan statements until there aren't any more. */
11864 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11866 /* If we are looking at a `}', then we have run out of
11867 statements; the same is true if we have reached the end
11868 of file, or have stumbled upon a stray '@end'. */
11869 if (token
->type
== CPP_CLOSE_BRACE
11870 || token
->type
== CPP_EOF
11871 || token
->type
== CPP_PRAGMA_EOL
11872 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11875 /* If we are in a compound statement and find 'else' then
11876 something went wrong. */
11877 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11879 if (parser
->in_statement
& IN_IF_STMT
)
11883 token
= cp_lexer_consume_token (parser
->lexer
);
11884 error_at (token
->location
, "%<else%> without a previous %<if%>");
11888 /* Parse the statement. */
11889 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11893 /* Return true if this is the C++20 version of range-based-for with
11897 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11901 /* Save tokens so that we can put them back. */
11902 cp_lexer_save_tokens (parser
->lexer
);
11904 /* There has to be an unnested ; followed by an unnested :. */
11905 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11906 /*recovering=*/false,
11908 /*consume_paren=*/false) != -1)
11911 /* We found the semicolon, eat it now. */
11912 cp_lexer_consume_token (parser
->lexer
);
11914 /* Now look for ':' that is not nested in () or {}. */
11915 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11916 /*recovering=*/false,
11918 /*consume_paren=*/false) == -1);
11921 /* Roll back the tokens we skipped. */
11922 cp_lexer_rollback_tokens (parser
->lexer
);
11927 /* Return true if we're looking at (init; cond), false otherwise. */
11930 cp_parser_init_statement_p (cp_parser
*parser
)
11932 /* Save tokens so that we can put them back. */
11933 cp_lexer_save_tokens (parser
->lexer
);
11935 /* Look for ';' that is not nested in () or {}. */
11936 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11937 /*recovering=*/false,
11939 /*consume_paren=*/false);
11941 /* Roll back the tokens we skipped. */
11942 cp_lexer_rollback_tokens (parser
->lexer
);
11947 /* Parse a selection-statement.
11949 selection-statement:
11950 if ( init-statement [opt] condition ) statement
11951 if ( init-statement [opt] condition ) statement else statement
11952 switch ( init-statement [opt] condition ) statement
11954 Returns the new IF_STMT or SWITCH_STMT.
11956 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11957 is a (possibly labeled) if statement which is not enclosed in
11958 braces and has an else clause. This is used to implement
11961 CHAIN is a vector of if-else-if conditions. This is used to implement
11962 -Wduplicated-cond. */
11965 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11970 token_indent_info guard_tinfo
;
11975 /* Peek at the next token. */
11976 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11977 guard_tinfo
= get_token_indent_info (token
);
11979 /* See what kind of keyword it is. */
11980 keyword
= token
->keyword
;
11990 if (keyword
== RID_IF
11991 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11995 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11996 if (cxx_dialect
< cxx17
)
11997 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11998 "with %<-std=c++17%> or %<-std=gnu++17%>");
12001 /* Look for the `('. */
12002 matching_parens parens
;
12003 if (!parens
.require_open (parser
))
12005 cp_parser_skip_to_end_of_statement (parser
);
12006 return error_mark_node
;
12009 /* Begin the selection-statement. */
12010 if (keyword
== RID_IF
)
12012 statement
= begin_if_stmt ();
12013 IF_STMT_CONSTEXPR_P (statement
) = cx
;
12016 statement
= begin_switch_stmt ();
12018 /* Parse the optional init-statement. */
12019 if (cp_parser_init_statement_p (parser
))
12022 if (cxx_dialect
< cxx17
)
12023 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12024 "init-statement in selection statements only available "
12025 "with %<-std=c++17%> or %<-std=gnu++17%>");
12026 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12028 /* A non-empty init-statement can have arbitrary side
12033 cp_parser_init_statement (parser
, &decl
);
12036 /* Parse the condition. */
12037 condition
= cp_parser_condition (parser
);
12038 /* Look for the `)'. */
12039 if (!parens
.require_close (parser
))
12040 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12041 /*consume_paren=*/true);
12043 if (keyword
== RID_IF
)
12046 unsigned char in_statement
;
12048 /* Add the condition. */
12049 condition
= finish_if_stmt_cond (condition
, statement
);
12051 if (warn_duplicated_cond
)
12052 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
12055 /* Parse the then-clause. */
12056 in_statement
= parser
->in_statement
;
12057 parser
->in_statement
|= IN_IF_STMT
;
12059 /* Outside a template, the non-selected branch of a constexpr
12060 if is a 'discarded statement', i.e. unevaluated. */
12061 bool was_discarded
= in_discarded_stmt
;
12062 bool discard_then
= (cx
&& !processing_template_decl
12063 && integer_zerop (condition
));
12066 in_discarded_stmt
= true;
12067 ++c_inhibit_evaluation_warnings
;
12070 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
12073 parser
->in_statement
= in_statement
;
12075 finish_then_clause (statement
);
12079 THEN_CLAUSE (statement
) = NULL_TREE
;
12080 in_discarded_stmt
= was_discarded
;
12081 --c_inhibit_evaluation_warnings
;
12084 /* If the next token is `else', parse the else-clause. */
12085 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12088 bool discard_else
= (cx
&& !processing_template_decl
12089 && integer_nonzerop (condition
));
12092 in_discarded_stmt
= true;
12093 ++c_inhibit_evaluation_warnings
;
12097 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12098 /* Consume the `else' keyword. */
12099 cp_lexer_consume_token (parser
->lexer
);
12100 if (warn_duplicated_cond
)
12102 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12106 /* We've got "if (COND) else if (COND2)". Start
12107 the condition chain and add COND as the first
12109 chain
= new vec
<tree
> ();
12110 if (!CONSTANT_CLASS_P (condition
)
12111 && !TREE_SIDE_EFFECTS (condition
))
12113 /* Wrap it in a NOP_EXPR so that we can set the
12114 location of the condition. */
12115 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
12117 SET_EXPR_LOCATION (e
, token
->location
);
12118 chain
->safe_push (e
);
12121 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
12124 /* This is if-else without subsequent if. Zap the
12125 condition chain; we would have already warned at
12131 begin_else_clause (statement
);
12132 /* Parse the else-clause. */
12133 cp_parser_implicitly_scoped_statement (parser
, NULL
,
12134 guard_tinfo
, chain
);
12136 finish_else_clause (statement
);
12138 /* If we are currently parsing a then-clause, then
12139 IF_P will not be NULL. We set it to true to
12140 indicate that this if statement has an else clause.
12141 This may trigger the Wparentheses warning below
12142 when we get back up to the parent if statement. */
12148 ELSE_CLAUSE (statement
) = NULL_TREE
;
12149 in_discarded_stmt
= was_discarded
;
12150 --c_inhibit_evaluation_warnings
;
12155 /* This if statement does not have an else clause. If
12156 NESTED_IF is true, then the then-clause has an if
12157 statement which does have an else clause. We warn
12158 about the potential ambiguity. */
12160 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
12161 "suggest explicit braces to avoid ambiguous"
12163 if (warn_duplicated_cond
)
12165 /* We don't need the condition chain anymore. */
12171 /* Now we're all done with the if-statement. */
12172 finish_if_stmt (statement
);
12176 bool in_switch_statement_p
;
12177 unsigned char in_statement
;
12179 /* Add the condition. */
12180 finish_switch_cond (condition
, statement
);
12182 /* Parse the body of the switch-statement. */
12183 in_switch_statement_p
= parser
->in_switch_statement_p
;
12184 in_statement
= parser
->in_statement
;
12185 parser
->in_switch_statement_p
= true;
12186 parser
->in_statement
|= IN_SWITCH_STMT
;
12187 cp_parser_implicitly_scoped_statement (parser
, if_p
,
12189 parser
->in_switch_statement_p
= in_switch_statement_p
;
12190 parser
->in_statement
= in_statement
;
12192 /* Now we're all done with the switch-statement. */
12193 finish_switch_stmt (statement
);
12201 cp_parser_error (parser
, "expected selection-statement");
12202 return error_mark_node
;
12206 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12207 If we have seen at least one decl-specifier, and the next token is not
12208 a parenthesis (after "int (" we might be looking at a functional cast)
12209 neither we are dealing with a concept-check expression then we must be
12210 looking at a declaration. */
12213 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
12214 cp_decl_specifier_seq
*decl_specs
)
12216 if (decl_specs
->any_specifiers_p
12217 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12218 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12219 && !cp_parser_error_occurred (parser
)
12220 && !(decl_specs
->type
12221 && TREE_CODE (decl_specs
->type
) == TYPE_DECL
12222 && is_constrained_auto (TREE_TYPE (decl_specs
->type
))))
12223 cp_parser_commit_to_tentative_parse (parser
);
12226 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12227 The declarator shall not specify a function or an array. Returns
12228 TRUE if the declarator is valid, FALSE otherwise. */
12231 cp_parser_check_condition_declarator (cp_parser
* parser
,
12232 cp_declarator
*declarator
,
12235 if (declarator
== cp_error_declarator
12236 || function_declarator_p (declarator
)
12237 || declarator
->kind
== cdk_array
)
12239 if (declarator
== cp_error_declarator
)
12240 /* Already complained. */;
12241 else if (declarator
->kind
== cdk_array
)
12242 error_at (loc
, "condition declares an array");
12244 error_at (loc
, "condition declares a function");
12245 if (parser
->fully_implicit_function_template_p
)
12246 abort_fully_implicit_template (parser
);
12247 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
12248 /*or_comma=*/false,
12249 /*consume_paren=*/false);
12256 /* Parse a condition.
12260 type-specifier-seq declarator = initializer-clause
12261 type-specifier-seq declarator braced-init-list
12266 type-specifier-seq declarator asm-specification [opt]
12267 attributes [opt] = assignment-expression
12269 Returns the expression that should be tested. */
12272 cp_parser_condition (cp_parser
* parser
)
12274 cp_decl_specifier_seq type_specifiers
;
12275 const char *saved_message
;
12276 int declares_class_or_enum
;
12278 /* Try the declaration first. */
12279 cp_parser_parse_tentatively (parser
);
12280 /* New types are not allowed in the type-specifier-seq for a
12282 saved_message
= parser
->type_definition_forbidden_message
;
12283 parser
->type_definition_forbidden_message
12284 = G_("types may not be defined in conditions");
12285 /* Parse the type-specifier-seq. */
12286 cp_parser_decl_specifier_seq (parser
,
12287 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12289 &declares_class_or_enum
);
12290 /* Restore the saved message. */
12291 parser
->type_definition_forbidden_message
= saved_message
;
12293 /* Gather the attributes that were provided with the
12294 decl-specifiers. */
12295 tree prefix_attributes
= type_specifiers
.attributes
;
12297 cp_parser_maybe_commit_to_declaration (parser
, &type_specifiers
);
12299 /* If all is well, we might be looking at a declaration. */
12300 if (!cp_parser_error_occurred (parser
))
12303 tree asm_specification
;
12305 cp_declarator
*declarator
;
12306 tree initializer
= NULL_TREE
;
12307 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12309 /* Parse the declarator. */
12310 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12311 CP_PARSER_FLAGS_NONE
,
12312 /*ctor_dtor_or_conv_p=*/NULL
,
12313 /*parenthesized_p=*/NULL
,
12314 /*member_p=*/false,
12315 /*friend_p=*/false,
12316 /*static_p=*/false);
12317 /* Parse the attributes. */
12318 attributes
= cp_parser_attributes_opt (parser
);
12319 /* Parse the asm-specification. */
12320 asm_specification
= cp_parser_asm_specification_opt (parser
);
12321 /* If the next token is not an `=' or '{', then we might still be
12322 looking at an expression. For example:
12326 looks like a decl-specifier-seq and a declarator -- but then
12327 there is no `=', so this is an expression. */
12328 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12329 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12330 cp_parser_simulate_error (parser
);
12332 /* If we did see an `=' or '{', then we are looking at a declaration
12334 if (cp_parser_parse_definitely (parser
))
12337 bool non_constant_p
= false;
12338 int flags
= LOOKUP_ONLYCONVERTING
;
12340 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12341 return error_mark_node
;
12343 /* Create the declaration. */
12344 decl
= start_decl (declarator
, &type_specifiers
,
12345 /*initialized_p=*/true,
12346 attributes
, prefix_attributes
,
12349 /* Parse the initializer. */
12350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12352 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12353 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12356 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12358 /* Consume the `='. */
12359 cp_lexer_consume_token (parser
->lexer
);
12360 initializer
= cp_parser_initializer_clause (parser
,
12365 cp_parser_error (parser
, "expected initializer");
12366 initializer
= error_mark_node
;
12368 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12369 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12371 /* Process the initializer. */
12372 cp_finish_decl (decl
,
12373 initializer
, !non_constant_p
,
12378 pop_scope (pushed_scope
);
12380 return convert_from_reference (decl
);
12383 /* If we didn't even get past the declarator successfully, we are
12384 definitely not looking at a declaration. */
12386 cp_parser_abort_tentative_parse (parser
);
12388 /* Otherwise, we are looking at an expression. */
12389 return cp_parser_expression (parser
);
12392 /* Parses a for-statement or range-for-statement until the closing ')',
12396 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12398 tree init
, scope
, decl
;
12401 /* Begin the for-statement. */
12402 scope
= begin_for_scope (&init
);
12404 /* Parse the initialization. */
12405 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12408 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12411 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12415 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12416 unsigned short unroll
)
12418 /* Normal for loop */
12419 tree condition
= NULL_TREE
;
12420 tree expression
= NULL_TREE
;
12423 stmt
= begin_for_stmt (scope
, init
);
12424 /* The init-statement has already been parsed in
12425 cp_parser_init_statement, so no work is needed here. */
12426 finish_init_stmt (stmt
);
12428 /* If there's a condition, process it. */
12429 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12430 condition
= cp_parser_condition (parser
);
12433 cp_parser_error (parser
, "missing loop condition in loop with "
12434 "%<GCC ivdep%> pragma");
12435 condition
= error_mark_node
;
12439 cp_parser_error (parser
, "missing loop condition in loop with "
12440 "%<GCC unroll%> pragma");
12441 condition
= error_mark_node
;
12443 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12444 /* Look for the `;'. */
12445 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12447 /* If there's an expression, process it. */
12448 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12449 expression
= cp_parser_expression (parser
);
12450 finish_for_expr (expression
, stmt
);
12455 /* Tries to parse a range-based for-statement:
12458 decl-specifier-seq declarator : expression
12460 The decl-specifier-seq declarator and the `:' are already parsed by
12461 cp_parser_init_statement. If processing_template_decl it returns a
12462 newly created RANGE_FOR_STMT; if not, it is converted to a
12463 regular FOR_STMT. */
12466 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12467 bool ivdep
, unsigned short unroll
, bool is_omp
)
12469 tree stmt
, range_expr
;
12470 auto_vec
<cxx_binding
*, 16> bindings
;
12471 auto_vec
<tree
, 16> names
;
12472 tree decomp_first_name
= NULL_TREE
;
12473 unsigned int decomp_cnt
= 0;
12475 /* Get the range declaration momentarily out of the way so that
12476 the range expression doesn't clash with it. */
12477 if (range_decl
!= error_mark_node
)
12479 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12481 tree v
= DECL_VALUE_EXPR (range_decl
);
12482 /* For decomposition declaration get all of the corresponding
12483 declarations out of the way. */
12484 if (TREE_CODE (v
) == ARRAY_REF
12485 && VAR_P (TREE_OPERAND (v
, 0))
12486 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12488 tree d
= range_decl
;
12489 range_decl
= TREE_OPERAND (v
, 0);
12490 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12491 decomp_first_name
= d
;
12492 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12494 tree name
= DECL_NAME (d
);
12495 names
.safe_push (name
);
12496 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12497 IDENTIFIER_BINDING (name
)
12498 = IDENTIFIER_BINDING (name
)->previous
;
12502 if (names
.is_empty ())
12504 tree name
= DECL_NAME (range_decl
);
12505 names
.safe_push (name
);
12506 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12507 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12513 bool expr_non_constant_p
;
12514 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12517 range_expr
= cp_parser_expression (parser
);
12519 /* Put the range declaration(s) back into scope. */
12520 for (unsigned int i
= 0; i
< names
.length (); i
++)
12522 cxx_binding
*binding
= bindings
[i
];
12523 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12524 IDENTIFIER_BINDING (names
[i
]) = binding
;
12527 /* finish_omp_for has its own code for the following, so just
12528 return the range_expr instead. */
12532 /* If in template, STMT is converted to a normal for-statement
12533 at instantiation. If not, it is done just ahead. */
12534 if (processing_template_decl
)
12536 if (check_for_bare_parameter_packs (range_expr
))
12537 range_expr
= error_mark_node
;
12538 stmt
= begin_range_for_stmt (scope
, init
);
12540 RANGE_FOR_IVDEP (stmt
) = 1;
12542 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12543 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12544 if (!type_dependent_expression_p (range_expr
)
12545 /* do_auto_deduction doesn't mess with template init-lists. */
12546 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12547 do_range_for_auto_deduction (range_decl
, range_expr
);
12551 stmt
= begin_for_stmt (scope
, init
);
12552 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12553 decomp_first_name
, decomp_cnt
, ivdep
,
12559 /* Subroutine of cp_convert_range_for: given the initializer expression,
12560 builds up the range temporary. */
12563 build_range_temp (tree range_expr
)
12565 tree range_type
, range_temp
;
12567 /* Find out the type deduced by the declaration
12568 `auto &&__range = range_expr'. */
12569 range_type
= cp_build_reference_type (make_auto (), true);
12570 range_type
= do_auto_deduction (range_type
, range_expr
,
12571 type_uses_auto (range_type
));
12573 /* Create the __range variable. */
12574 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12576 TREE_USED (range_temp
) = 1;
12577 DECL_ARTIFICIAL (range_temp
) = 1;
12582 /* Used by cp_parser_range_for in template context: we aren't going to
12583 do a full conversion yet, but we still need to resolve auto in the
12584 type of the for-range-declaration if present. This is basically
12585 a shortcut version of cp_convert_range_for. */
12588 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12590 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12593 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12594 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12595 iter_type
= (cp_parser_perform_range_for_lookup
12596 (range_temp
, &begin_dummy
, &end_dummy
));
12599 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12601 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12603 tf_warning_or_error
);
12604 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12605 iter_decl
, auto_node
);
12610 /* Converts a range-based for-statement into a normal
12611 for-statement, as per the definition.
12613 for (RANGE_DECL : RANGE_EXPR)
12616 should be equivalent to:
12619 auto &&__range = RANGE_EXPR;
12620 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12624 RANGE_DECL = *__begin;
12629 If RANGE_EXPR is an array:
12630 BEGIN_EXPR = __range
12631 END_EXPR = __range + ARRAY_SIZE(__range)
12632 Else if RANGE_EXPR has a member 'begin' or 'end':
12633 BEGIN_EXPR = __range.begin()
12634 END_EXPR = __range.end()
12636 BEGIN_EXPR = begin(__range)
12637 END_EXPR = end(__range);
12639 If __range has a member 'begin' but not 'end', or vice versa, we must
12640 still use the second alternative (it will surely fail, however).
12641 When calling begin()/end() in the third alternative we must use
12642 argument dependent lookup, but always considering 'std' as an associated
12646 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12647 tree decomp_first_name
, unsigned int decomp_cnt
,
12648 bool ivdep
, unsigned short unroll
)
12651 tree iter_type
, begin_expr
, end_expr
;
12652 tree condition
, expression
;
12654 range_expr
= mark_lvalue_use (range_expr
);
12656 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12657 /* If an error happened previously do nothing or else a lot of
12658 unhelpful errors would be issued. */
12659 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12664 if (VAR_P (range_expr
)
12665 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12666 /* Can't bind a reference to an array of runtime bound. */
12667 range_temp
= range_expr
;
12670 range_temp
= build_range_temp (range_expr
);
12671 pushdecl (range_temp
);
12672 cp_finish_decl (range_temp
, range_expr
,
12673 /*is_constant_init*/false, NULL_TREE
,
12674 LOOKUP_ONLYCONVERTING
);
12675 range_temp
= convert_from_reference (range_temp
);
12677 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12678 &begin_expr
, &end_expr
);
12681 /* The new for initialization statement. */
12682 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12684 TREE_USED (begin
) = 1;
12685 DECL_ARTIFICIAL (begin
) = 1;
12687 cp_finish_decl (begin
, begin_expr
,
12688 /*is_constant_init*/false, NULL_TREE
,
12689 LOOKUP_ONLYCONVERTING
);
12691 if (cxx_dialect
>= cxx17
)
12692 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12693 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12694 TREE_USED (end
) = 1;
12695 DECL_ARTIFICIAL (end
) = 1;
12697 cp_finish_decl (end
, end_expr
,
12698 /*is_constant_init*/false, NULL_TREE
,
12699 LOOKUP_ONLYCONVERTING
);
12701 finish_init_stmt (statement
);
12703 /* The new for condition. */
12704 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12707 NULL
, tf_warning_or_error
);
12708 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12710 /* The new increment expression. */
12711 expression
= finish_unary_op_expr (input_location
,
12712 PREINCREMENT_EXPR
, begin
,
12713 tf_warning_or_error
);
12714 finish_for_expr (expression
, statement
);
12716 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12717 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12719 /* The declaration is initialized with *__begin inside the loop body. */
12720 cp_finish_decl (range_decl
,
12721 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12722 tf_warning_or_error
),
12723 /*is_constant_init*/false, NULL_TREE
,
12724 LOOKUP_ONLYCONVERTING
);
12725 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12726 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12731 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12732 We need to solve both at the same time because the method used
12733 depends on the existence of members begin or end.
12734 Returns the type deduced for the iterator expression. */
12737 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12739 if (error_operand_p (range
))
12741 *begin
= *end
= error_mark_node
;
12742 return error_mark_node
;
12745 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12747 error ("range-based %<for%> expression of type %qT "
12748 "has incomplete type", TREE_TYPE (range
));
12749 *begin
= *end
= error_mark_node
;
12750 return error_mark_node
;
12752 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12754 /* If RANGE is an array, we will use pointer arithmetic. */
12755 *begin
= decay_conversion (range
, tf_warning_or_error
);
12756 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12758 array_type_nelts_top (TREE_TYPE (range
)),
12760 return TREE_TYPE (*begin
);
12764 /* If it is not an array, we must do a bit of magic. */
12765 tree id_begin
, id_end
;
12766 tree member_begin
, member_end
;
12768 *begin
= *end
= error_mark_node
;
12770 id_begin
= get_identifier ("begin");
12771 id_end
= get_identifier ("end");
12772 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12773 /*protect=*/2, /*want_type=*/false,
12774 tf_warning_or_error
);
12775 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12776 /*protect=*/2, /*want_type=*/false,
12777 tf_warning_or_error
);
12779 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12781 /* Use the member functions. */
12782 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12783 *end
= cp_parser_range_for_member_function (range
, id_end
);
12787 /* Use global functions with ADL. */
12790 vec_safe_push (vec
, range
);
12792 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12793 tf_warning_or_error
);
12794 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12795 tf_warning_or_error
);
12796 member_end
= perform_koenig_lookup (id_end
, vec
,
12797 tf_warning_or_error
);
12798 *end
= finish_call_expr (member_end
, &vec
, false, true,
12799 tf_warning_or_error
);
12802 /* Last common checks. */
12803 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12805 /* If one of the expressions is an error do no more checks. */
12806 *begin
= *end
= error_mark_node
;
12807 return error_mark_node
;
12809 else if (type_dependent_expression_p (*begin
)
12810 || type_dependent_expression_p (*end
))
12811 /* Can happen, when, eg, in a template context, Koenig lookup
12812 can't resolve begin/end (c++/58503). */
12816 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12817 /* The unqualified type of the __begin and __end temporaries should
12818 be the same, as required by the multiple auto declaration. */
12819 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12821 if (cxx_dialect
>= cxx17
12822 && (build_x_binary_op (input_location
, NE_EXPR
,
12823 *begin
, ERROR_MARK
,
12826 != error_mark_node
))
12827 /* P0184R0 allows __begin and __end to have different types,
12828 but make sure they are comparable so we can give a better
12831 error ("inconsistent begin/end types in range-based %<for%> "
12832 "statement: %qT and %qT",
12833 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12840 /* Helper function for cp_parser_perform_range_for_lookup.
12841 Builds a tree for RANGE.IDENTIFIER(). */
12844 cp_parser_range_for_member_function (tree range
, tree identifier
)
12848 member
= finish_class_member_access_expr (range
, identifier
,
12849 false, tf_warning_or_error
);
12850 if (member
== error_mark_node
)
12851 return error_mark_node
;
12854 res
= finish_call_expr (member
, &vec
,
12855 /*disallow_virtual=*/false,
12856 /*koenig_p=*/false,
12857 tf_warning_or_error
);
12861 /* Parse an iteration-statement.
12863 iteration-statement:
12864 while ( condition ) statement
12865 do statement while ( expression ) ;
12866 for ( init-statement condition [opt] ; expression [opt] )
12869 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12872 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12873 unsigned short unroll
)
12878 unsigned char in_statement
;
12879 token_indent_info guard_tinfo
;
12881 /* Peek at the next token. */
12882 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12884 return error_mark_node
;
12886 guard_tinfo
= get_token_indent_info (token
);
12888 /* Remember whether or not we are already within an iteration
12890 in_statement
= parser
->in_statement
;
12892 /* See what kind of keyword it is. */
12893 keyword
= token
->keyword
;
12900 /* Begin the while-statement. */
12901 statement
= begin_while_stmt ();
12902 /* Look for the `('. */
12903 matching_parens parens
;
12904 parens
.require_open (parser
);
12905 /* Parse the condition. */
12906 condition
= cp_parser_condition (parser
);
12907 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12908 /* Look for the `)'. */
12909 parens
.require_close (parser
);
12910 /* Parse the dependent statement. */
12911 parser
->in_statement
= IN_ITERATION_STMT
;
12912 bool prev
= note_iteration_stmt_body_start ();
12913 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12914 note_iteration_stmt_body_end (prev
);
12915 parser
->in_statement
= in_statement
;
12916 /* We're done with the while-statement. */
12917 finish_while_stmt (statement
);
12925 /* Begin the do-statement. */
12926 statement
= begin_do_stmt ();
12927 /* Parse the body of the do-statement. */
12928 parser
->in_statement
= IN_ITERATION_STMT
;
12929 bool prev
= note_iteration_stmt_body_start ();
12930 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12931 note_iteration_stmt_body_end (prev
);
12932 parser
->in_statement
= in_statement
;
12933 finish_do_body (statement
);
12934 /* Look for the `while' keyword. */
12935 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12936 /* Look for the `('. */
12937 matching_parens parens
;
12938 parens
.require_open (parser
);
12939 /* Parse the expression. */
12940 expression
= cp_parser_expression (parser
);
12941 /* We're done with the do-statement. */
12942 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12943 /* Look for the `)'. */
12944 parens
.require_close (parser
);
12945 /* Look for the `;'. */
12946 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12952 /* Look for the `('. */
12953 matching_parens parens
;
12954 parens
.require_open (parser
);
12956 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12958 /* Look for the `)'. */
12959 parens
.require_close (parser
);
12961 /* Parse the body of the for-statement. */
12962 parser
->in_statement
= IN_ITERATION_STMT
;
12963 bool prev
= note_iteration_stmt_body_start ();
12964 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12965 note_iteration_stmt_body_end (prev
);
12966 parser
->in_statement
= in_statement
;
12968 /* We're done with the for-statement. */
12969 finish_for_stmt (statement
);
12974 cp_parser_error (parser
, "expected iteration-statement");
12975 statement
= error_mark_node
;
12982 /* Parse a init-statement or the declarator of a range-based-for.
12983 Returns true if a range-based-for declaration is seen.
12986 expression-statement
12987 simple-declaration */
12990 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12992 /* If the next token is a `;', then we have an empty
12993 expression-statement. Grammatically, this is also a
12994 simple-declaration, but an invalid one, because it does not
12995 declare anything. Therefore, if we did not handle this case
12996 specially, we would issue an error message about an invalid
12998 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13000 bool is_range_for
= false;
13001 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
13003 /* Try to parse the init-statement. */
13004 if (cp_parser_range_based_for_with_init_p (parser
))
13007 cp_parser_parse_tentatively (parser
);
13008 /* Parse the declaration. */
13009 cp_parser_simple_declaration (parser
,
13010 /*function_definition_allowed_p=*/false,
13012 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13013 if (!cp_parser_parse_definitely (parser
))
13014 /* That didn't work, try to parse it as an expression-statement. */
13015 cp_parser_expression_statement (parser
, NULL_TREE
);
13017 if (cxx_dialect
< cxx20
)
13019 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
13020 "range-based %<for%> loops with initializer only "
13021 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13022 *decl
= error_mark_node
;
13026 /* A colon is used in range-based for. */
13027 parser
->colon_corrects_to_scope_p
= false;
13029 /* We're going to speculatively look for a declaration, falling back
13030 to an expression, if necessary. */
13031 cp_parser_parse_tentatively (parser
);
13032 /* Parse the declaration. */
13033 cp_parser_simple_declaration (parser
,
13034 /*function_definition_allowed_p=*/false,
13036 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
13037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13039 /* It is a range-for, consume the ':'. */
13040 cp_lexer_consume_token (parser
->lexer
);
13041 is_range_for
= true;
13042 if (cxx_dialect
< cxx11
)
13043 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
13044 "range-based %<for%> loops only available with "
13045 "%<-std=c++11%> or %<-std=gnu++11%>");
13048 /* The ';' is not consumed yet because we told
13049 cp_parser_simple_declaration not to. */
13050 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13052 if (cp_parser_parse_definitely (parser
))
13053 return is_range_for
;
13054 /* If the tentative parse failed, then we shall need to look for an
13055 expression-statement. */
13057 /* If we are here, it is an expression-statement. */
13058 cp_parser_expression_statement (parser
, NULL_TREE
);
13062 /* Parse a jump-statement.
13067 return expression [opt] ;
13068 return braced-init-list ;
13069 coroutine-return-statement;
13075 goto * expression ;
13077 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13080 cp_parser_jump_statement (cp_parser
* parser
)
13082 tree statement
= error_mark_node
;
13085 unsigned char in_statement
;
13087 /* Peek at the next token. */
13088 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
13090 return error_mark_node
;
13092 /* See what kind of keyword it is. */
13093 keyword
= token
->keyword
;
13097 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
13098 switch (in_statement
)
13101 error_at (token
->location
, "break statement not within loop or switch");
13104 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
13105 || in_statement
== IN_ITERATION_STMT
);
13106 statement
= finish_break_stmt ();
13107 if (in_statement
== IN_ITERATION_STMT
)
13108 break_maybe_infinite_loop ();
13111 error_at (token
->location
, "invalid exit from OpenMP structured block");
13114 error_at (token
->location
, "break statement used with OpenMP for loop");
13117 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13121 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
13124 error_at (token
->location
, "continue statement not within a loop");
13126 /* Fall through. */
13127 case IN_ITERATION_STMT
:
13129 statement
= finish_continue_stmt ();
13132 error_at (token
->location
, "invalid exit from OpenMP structured block");
13135 gcc_unreachable ();
13137 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13140 case RID_CO_RETURN
:
13144 bool expr_non_constant_p
;
13146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13148 cp_lexer_set_source_position (parser
->lexer
);
13149 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13150 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13152 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13153 expr
= cp_parser_expression (parser
);
13155 /* If the next token is a `;', then there is no
13158 /* Build the return-statement, check co-return first, since type
13159 deduction is not valid there. */
13160 if (keyword
== RID_CO_RETURN
)
13161 statement
= finish_co_return_stmt (token
->location
, expr
);
13162 else if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
13163 /* Don't deduce from a discarded return statement. */;
13165 statement
= finish_return_stmt (expr
);
13166 /* Look for the final `;'. */
13167 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13172 if (parser
->in_function_body
13173 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
13175 error ("%<goto%> in %<constexpr%> function");
13176 cp_function_chain
->invalid_constexpr
= true;
13179 /* Create the goto-statement. */
13180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
13182 /* Issue a warning about this use of a GNU extension. */
13183 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
13184 /* Consume the '*' token. */
13185 cp_lexer_consume_token (parser
->lexer
);
13186 /* Parse the dependent expression. */
13187 finish_goto_stmt (cp_parser_expression (parser
));
13190 finish_goto_stmt (cp_parser_identifier (parser
));
13191 /* Look for the final `;'. */
13192 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13196 cp_parser_error (parser
, "expected jump-statement");
13203 /* Parse a declaration-statement.
13205 declaration-statement:
13206 block-declaration */
13209 cp_parser_declaration_statement (cp_parser
* parser
)
13213 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13214 p
= obstack_alloc (&declarator_obstack
, 0);
13216 /* Parse the block-declaration. */
13217 cp_parser_block_declaration (parser
, /*statement_p=*/true);
13219 /* Free any declarators allocated. */
13220 obstack_free (&declarator_obstack
, p
);
13223 /* Some dependent statements (like `if (cond) statement'), are
13224 implicitly in their own scope. In other words, if the statement is
13225 a single statement (as opposed to a compound-statement), it is
13226 none-the-less treated as if it were enclosed in braces. Any
13227 declarations appearing in the dependent statement are out of scope
13228 after control passes that point. This function parses a statement,
13229 but ensures that is in its own scope, even if it is not a
13230 compound-statement.
13232 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13233 is a (possibly labeled) if statement which is not enclosed in
13234 braces and has an else clause. This is used to implement
13237 CHAIN is a vector of if-else-if conditions. This is used to implement
13240 Returns the new statement. */
13243 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
13244 const token_indent_info
&guard_tinfo
,
13248 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13249 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
13250 token_indent_info body_tinfo
13251 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13256 /* Mark if () ; with a special NOP_EXPR. */
13257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13259 cp_lexer_consume_token (parser
->lexer
);
13260 statement
= add_stmt (build_empty_stmt (body_loc
));
13262 if (guard_tinfo
.keyword
== RID_IF
13263 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13264 warning_at (body_loc
, OPT_Wempty_body
,
13265 "suggest braces around empty body in an %<if%> statement");
13266 else if (guard_tinfo
.keyword
== RID_ELSE
)
13267 warning_at (body_loc
, OPT_Wempty_body
,
13268 "suggest braces around empty body in an %<else%> statement");
13270 /* if a compound is opened, we simply parse the statement directly. */
13271 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13272 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13273 /* If the token is not a `{', then we must take special action. */
13276 /* Create a compound-statement. */
13277 statement
= begin_compound_stmt (0);
13278 /* Parse the dependent-statement. */
13279 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13280 &body_loc_after_labels
);
13281 /* Finish the dummy compound-statement. */
13282 finish_compound_stmt (statement
);
13285 token_indent_info next_tinfo
13286 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13287 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13289 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13290 && next_tinfo
.type
!= CPP_SEMICOLON
)
13291 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13292 guard_tinfo
.location
, guard_tinfo
.keyword
);
13294 /* Return the statement. */
13298 /* For some dependent statements (like `while (cond) statement'), we
13299 have already created a scope. Therefore, even if the dependent
13300 statement is a compound-statement, we do not want to create another
13304 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13305 const token_indent_info
&guard_tinfo
)
13307 /* If the token is a `{', then we must take special action. */
13308 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13310 token_indent_info body_tinfo
13311 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13312 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13314 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13315 &loc_after_labels
);
13316 token_indent_info next_tinfo
13317 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13318 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13320 if (loc_after_labels
!= UNKNOWN_LOCATION
13321 && next_tinfo
.type
!= CPP_SEMICOLON
)
13322 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13323 guard_tinfo
.location
,
13324 guard_tinfo
.keyword
);
13328 /* Avoid calling cp_parser_compound_statement, so that we
13329 don't create a new scope. Do everything else by hand. */
13330 matching_braces braces
;
13331 braces
.require_open (parser
);
13332 /* If the next keyword is `__label__' we have a label declaration. */
13333 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13334 cp_parser_label_declaration (parser
);
13335 /* Parse an (optional) statement-seq. */
13336 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13337 braces
.require_close (parser
);
13341 /* Declarations [gram.dcl.dcl] */
13343 /* Parse an optional declaration-sequence.
13347 declaration-seq declaration */
13350 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13354 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13356 if (token
->type
== CPP_CLOSE_BRACE
13357 || token
->type
== CPP_EOF
)
13360 cp_parser_toplevel_declaration (parser
);
13364 /* Parse a declaration.
13368 function-definition
13369 template-declaration
13370 explicit-instantiation
13371 explicit-specialization
13372 linkage-specification
13373 namespace-definition
13381 __extension__ declaration */
13384 cp_parser_declaration (cp_parser
* parser
)
13388 int saved_pedantic
;
13390 tree attributes
= NULL_TREE
;
13392 /* Check for the `__extension__' keyword. */
13393 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13395 /* Parse the qualified declaration. */
13396 cp_parser_declaration (parser
);
13397 /* Restore the PEDANTIC flag. */
13398 pedantic
= saved_pedantic
;
13403 /* Try to figure out what kind of declaration is present. */
13404 token1
= *cp_lexer_peek_token (parser
->lexer
);
13406 if (token1
.type
!= CPP_EOF
)
13407 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13410 token2
.type
= CPP_EOF
;
13411 token2
.keyword
= RID_MAX
;
13414 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13415 p
= obstack_alloc (&declarator_obstack
, 0);
13417 /* If the next token is `extern' and the following token is a string
13418 literal, then we have a linkage specification. */
13419 if (token1
.keyword
== RID_EXTERN
13420 && cp_parser_is_pure_string_literal (&token2
))
13421 cp_parser_linkage_specification (parser
);
13422 /* If the next token is `template', then we have either a template
13423 declaration, an explicit instantiation, or an explicit
13425 else if (token1
.keyword
== RID_TEMPLATE
)
13427 /* `template <>' indicates a template specialization. */
13428 if (token2
.type
== CPP_LESS
13429 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13430 cp_parser_explicit_specialization (parser
);
13431 /* `template <' indicates a template declaration. */
13432 else if (token2
.type
== CPP_LESS
)
13433 cp_parser_template_declaration (parser
, /*member_p=*/false);
13434 /* Anything else must be an explicit instantiation. */
13436 cp_parser_explicit_instantiation (parser
);
13438 /* If the next token is `export', then we have a template
13440 else if (token1
.keyword
== RID_EXPORT
)
13441 cp_parser_template_declaration (parser
, /*member_p=*/false);
13442 /* If the next token is `extern', 'static' or 'inline' and the one
13443 after that is `template', we have a GNU extended explicit
13444 instantiation directive. */
13445 else if (cp_parser_allow_gnu_extensions_p (parser
)
13446 && (token1
.keyword
== RID_EXTERN
13447 || token1
.keyword
== RID_STATIC
13448 || token1
.keyword
== RID_INLINE
)
13449 && token2
.keyword
== RID_TEMPLATE
)
13450 cp_parser_explicit_instantiation (parser
);
13451 /* If the next token is `namespace', check for a named or unnamed
13452 namespace definition. */
13453 else if (token1
.keyword
== RID_NAMESPACE
13454 && (/* A named namespace definition. */
13455 (token2
.type
== CPP_NAME
13456 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13458 || (token2
.type
== CPP_OPEN_SQUARE
13459 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13460 == CPP_OPEN_SQUARE
)
13461 /* An unnamed namespace definition. */
13462 || token2
.type
== CPP_OPEN_BRACE
13463 || token2
.keyword
== RID_ATTRIBUTE
))
13464 cp_parser_namespace_definition (parser
);
13465 /* An inline (associated) namespace definition. */
13466 else if (token1
.keyword
== RID_INLINE
13467 && token2
.keyword
== RID_NAMESPACE
)
13468 cp_parser_namespace_definition (parser
);
13469 /* Objective-C++ declaration/definition. */
13470 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13471 cp_parser_objc_declaration (parser
, NULL_TREE
);
13472 else if (c_dialect_objc ()
13473 && token1
.keyword
== RID_ATTRIBUTE
13474 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13475 cp_parser_objc_declaration (parser
, attributes
);
13476 /* At this point we may have a template declared by a concept
13478 else if (flag_concepts
13479 && cp_parser_template_declaration_after_export (parser
,
13480 /*member_p=*/false))
13483 /* Try to parse a block-declaration, or a function-definition. */
13484 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13486 /* Free any declarators allocated. */
13487 obstack_free (&declarator_obstack
, p
);
13490 /* Parse a namespace-scope declaration. */
13493 cp_parser_toplevel_declaration (cp_parser
* parser
)
13495 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13497 if (token
->type
== CPP_PRAGMA
)
13498 /* A top-level declaration can consist solely of a #pragma. A
13499 nested declaration cannot, so this is done here and not in
13500 cp_parser_declaration. (A #pragma at block scope is
13501 handled in cp_parser_statement.) */
13502 cp_parser_pragma (parser
, pragma_external
, NULL
);
13503 else if (token
->type
== CPP_SEMICOLON
)
13505 cp_lexer_consume_token (parser
->lexer
);
13506 /* A declaration consisting of a single semicolon is invalid
13507 * before C++11. Allow it unless we're being pedantic. */
13508 if (cxx_dialect
< cxx11
)
13509 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13512 /* Parse the declaration itself. */
13513 cp_parser_declaration (parser
);
13516 /* Parse a block-declaration.
13521 namespace-alias-definition
13528 __extension__ block-declaration
13533 static_assert-declaration
13535 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13536 part of a declaration-statement. */
13539 cp_parser_block_declaration (cp_parser
*parser
,
13543 int saved_pedantic
;
13545 /* Check for the `__extension__' keyword. */
13546 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13548 /* Parse the qualified declaration. */
13549 cp_parser_block_declaration (parser
, statement_p
);
13550 /* Restore the PEDANTIC flag. */
13551 pedantic
= saved_pedantic
;
13556 /* Peek at the next token to figure out which kind of declaration is
13558 token1
= cp_lexer_peek_token (parser
->lexer
);
13560 /* If the next keyword is `asm', we have an asm-definition. */
13561 if (token1
->keyword
== RID_ASM
)
13564 cp_parser_commit_to_tentative_parse (parser
);
13565 cp_parser_asm_definition (parser
);
13567 /* If the next keyword is `namespace', we have a
13568 namespace-alias-definition. */
13569 else if (token1
->keyword
== RID_NAMESPACE
)
13570 cp_parser_namespace_alias_definition (parser
);
13571 /* If the next keyword is `using', we have a
13572 using-declaration, a using-directive, or an alias-declaration. */
13573 else if (token1
->keyword
== RID_USING
)
13578 cp_parser_commit_to_tentative_parse (parser
);
13579 /* If the token after `using' is `namespace', then we have a
13580 using-directive. */
13581 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13582 if (token2
->keyword
== RID_NAMESPACE
)
13583 cp_parser_using_directive (parser
);
13584 /* If the second token after 'using' is '=', then we have an
13585 alias-declaration. */
13586 else if (cxx_dialect
>= cxx11
13587 && token2
->type
== CPP_NAME
13588 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13589 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13590 cp_parser_alias_declaration (parser
);
13591 /* Otherwise, it's a using-declaration. */
13593 cp_parser_using_declaration (parser
,
13594 /*access_declaration_p=*/false);
13596 /* If the next keyword is `__label__' we have a misplaced label
13598 else if (token1
->keyword
== RID_LABEL
)
13600 cp_lexer_consume_token (parser
->lexer
);
13601 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13602 cp_parser_skip_to_end_of_statement (parser
);
13603 /* If the next token is now a `;', consume it. */
13604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13605 cp_lexer_consume_token (parser
->lexer
);
13607 /* If the next token is `static_assert' we have a static assertion. */
13608 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13609 cp_parser_static_assert (parser
, /*member_p=*/false);
13610 /* Anything else must be a simple-declaration. */
13612 cp_parser_simple_declaration (parser
, !statement_p
,
13613 /*maybe_range_for_decl*/NULL
);
13616 /* Parse a simple-declaration.
13618 simple-declaration:
13619 decl-specifier-seq [opt] init-declarator-list [opt] ;
13620 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13621 brace-or-equal-initializer ;
13623 init-declarator-list:
13625 init-declarator-list , init-declarator
13627 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13628 function-definition as a simple-declaration.
13630 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13631 parsed declaration if it is an uninitialized single declarator not followed
13632 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13633 if present, will not be consumed. */
13636 cp_parser_simple_declaration (cp_parser
* parser
,
13637 bool function_definition_allowed_p
,
13638 tree
*maybe_range_for_decl
)
13640 cp_decl_specifier_seq decl_specifiers
;
13641 int declares_class_or_enum
;
13642 bool saw_declarator
;
13643 location_t comma_loc
= UNKNOWN_LOCATION
;
13644 location_t init_loc
= UNKNOWN_LOCATION
;
13646 if (maybe_range_for_decl
)
13647 *maybe_range_for_decl
= NULL_TREE
;
13649 /* Defer access checks until we know what is being declared; the
13650 checks for names appearing in the decl-specifier-seq should be
13651 done as if we were in the scope of the thing being declared. */
13652 push_deferring_access_checks (dk_deferred
);
13654 /* Parse the decl-specifier-seq. We have to keep track of whether
13655 or not the decl-specifier-seq declares a named class or
13656 enumeration type, since that is the only case in which the
13657 init-declarator-list is allowed to be empty.
13661 In a simple-declaration, the optional init-declarator-list can be
13662 omitted only when declaring a class or enumeration, that is when
13663 the decl-specifier-seq contains either a class-specifier, an
13664 elaborated-type-specifier, or an enum-specifier. */
13665 cp_parser_decl_specifier_seq (parser
,
13666 CP_PARSER_FLAGS_OPTIONAL
,
13668 &declares_class_or_enum
);
13669 /* We no longer need to defer access checks. */
13670 stop_deferring_access_checks ();
13672 /* In a block scope, a valid declaration must always have a
13673 decl-specifier-seq. By not trying to parse declarators, we can
13674 resolve the declaration/expression ambiguity more quickly. */
13675 if (!function_definition_allowed_p
13676 && !decl_specifiers
.any_specifiers_p
)
13678 cp_parser_error (parser
, "expected declaration");
13682 /* If the next two tokens are both identifiers, the code is
13683 erroneous. The usual cause of this situation is code like:
13687 where "T" should name a type -- but does not. */
13688 if (!decl_specifiers
.any_type_specifiers_p
13689 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13691 /* If parsing tentatively, we should commit; we really are
13692 looking at a declaration. */
13693 cp_parser_commit_to_tentative_parse (parser
);
13698 cp_parser_maybe_commit_to_declaration (parser
, &decl_specifiers
);
13700 /* Look for C++17 decomposition declaration. */
13701 for (size_t n
= 1; ; n
++)
13702 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13703 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13705 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13706 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13707 && decl_specifiers
.any_specifiers_p
)
13710 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13711 maybe_range_for_decl
,
13714 /* The next token should be either a `,' or a `;'. */
13715 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13716 /* If it's a `;', we are done. */
13717 if (token
->type
== CPP_SEMICOLON
)
13719 else if (maybe_range_for_decl
)
13721 if (*maybe_range_for_decl
== NULL_TREE
)
13722 *maybe_range_for_decl
= error_mark_node
;
13725 /* Anything else is an error. */
13728 /* If we have already issued an error message we don't need
13729 to issue another one. */
13730 if ((decl
!= error_mark_node
13731 && DECL_INITIAL (decl
) != error_mark_node
)
13732 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13733 cp_parser_error (parser
, "expected %<;%>");
13734 /* Skip tokens until we reach the end of the statement. */
13735 cp_parser_skip_to_end_of_statement (parser
);
13736 /* If the next token is now a `;', consume it. */
13737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13738 cp_lexer_consume_token (parser
->lexer
);
13746 bool auto_specifier_p
;
13747 /* NULL_TREE if both variable and function declaration are allowed,
13748 error_mark_node if function declaration are not allowed and
13749 a FUNCTION_DECL that should be diagnosed if it is followed by
13750 variable declarations. */
13751 tree auto_function_declaration
;
13753 last_type
= NULL_TREE
;
13755 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13756 auto_function_declaration
= NULL_TREE
;
13758 /* Keep going until we hit the `;' at the end of the simple
13760 saw_declarator
= false;
13761 while (cp_lexer_next_token_is_not (parser
->lexer
,
13765 bool function_definition_p
;
13767 tree auto_result
= NULL_TREE
;
13769 if (saw_declarator
)
13771 /* If we are processing next declarator, comma is expected */
13772 token
= cp_lexer_peek_token (parser
->lexer
);
13773 gcc_assert (token
->type
== CPP_COMMA
);
13774 cp_lexer_consume_token (parser
->lexer
);
13775 if (maybe_range_for_decl
)
13777 *maybe_range_for_decl
= error_mark_node
;
13778 if (comma_loc
== UNKNOWN_LOCATION
)
13779 comma_loc
= token
->location
;
13783 saw_declarator
= true;
13785 /* Parse the init-declarator. */
13786 decl
= cp_parser_init_declarator (parser
,
13787 CP_PARSER_FLAGS_NONE
,
13790 function_definition_allowed_p
,
13791 /*member_p=*/false,
13792 declares_class_or_enum
,
13793 &function_definition_p
,
13794 maybe_range_for_decl
,
13797 /* If an error occurred while parsing tentatively, exit quickly.
13798 (That usually happens when in the body of a function; each
13799 statement is treated as a declaration-statement until proven
13801 if (cp_parser_error_occurred (parser
))
13804 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13806 /* If the init-declarator-list contains more than one
13807 init-declarator, they shall all form declarations of
13809 if (auto_function_declaration
== NULL_TREE
)
13810 auto_function_declaration
13811 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13812 else if (TREE_CODE (decl
) == FUNCTION_DECL
13813 || auto_function_declaration
!= error_mark_node
)
13815 error_at (decl_specifiers
.locations
[ds_type_spec
],
13816 "non-variable %qD in declaration with more than one "
13817 "declarator with placeholder type",
13818 TREE_CODE (decl
) == FUNCTION_DECL
13819 ? decl
: auto_function_declaration
);
13820 auto_function_declaration
= error_mark_node
;
13825 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13828 && last_type
!= error_mark_node
13829 && !same_type_p (auto_result
, last_type
))
13831 /* If the list of declarators contains more than one declarator,
13832 the type of each declared variable is determined as described
13833 above. If the type deduced for the template parameter U is not
13834 the same in each deduction, the program is ill-formed. */
13835 error_at (decl_specifiers
.locations
[ds_type_spec
],
13836 "inconsistent deduction for %qT: %qT and then %qT",
13837 decl_specifiers
.type
, last_type
, auto_result
);
13838 last_type
= error_mark_node
;
13841 last_type
= auto_result
;
13844 /* Handle function definitions specially. */
13845 if (function_definition_p
)
13847 /* If the next token is a `,', then we are probably
13848 processing something like:
13852 which is erroneous. */
13853 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13855 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13856 error_at (token
->location
,
13858 " declarations and function-definitions is forbidden");
13860 /* Otherwise, we're done with the list of declarators. */
13863 pop_deferring_access_checks ();
13867 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13868 *maybe_range_for_decl
= decl
;
13869 /* The next token should be either a `,' or a `;'. */
13870 token
= cp_lexer_peek_token (parser
->lexer
);
13871 /* If it's a `,', there are more declarators to come. */
13872 if (token
->type
== CPP_COMMA
)
13873 /* will be consumed next time around */;
13874 /* If it's a `;', we are done. */
13875 else if (token
->type
== CPP_SEMICOLON
)
13877 else if (maybe_range_for_decl
)
13879 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13880 permerror (decl_specifiers
.locations
[ds_type_spec
],
13881 "types may not be defined in a for-range-declaration");
13884 /* Anything else is an error. */
13887 /* If we have already issued an error message we don't need
13888 to issue another one. */
13889 if ((decl
!= error_mark_node
13890 && DECL_INITIAL (decl
) != error_mark_node
)
13891 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13892 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13893 /* Skip tokens until we reach the end of the statement. */
13894 cp_parser_skip_to_end_of_statement (parser
);
13895 /* If the next token is now a `;', consume it. */
13896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13897 cp_lexer_consume_token (parser
->lexer
);
13900 /* After the first time around, a function-definition is not
13901 allowed -- even if it was OK at first. For example:
13906 function_definition_allowed_p
= false;
13909 /* Issue an error message if no declarators are present, and the
13910 decl-specifier-seq does not itself declare a class or
13911 enumeration: [dcl.dcl]/3. */
13912 if (!saw_declarator
)
13914 if (cp_parser_declares_only_class_p (parser
))
13916 if (!declares_class_or_enum
13917 && decl_specifiers
.type
13918 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13919 /* Ensure an error is issued anyway when finish_decltype_type,
13920 called via cp_parser_decl_specifier_seq, returns a class or
13921 an enumeration (c++/51786). */
13922 decl_specifiers
.type
= NULL_TREE
;
13923 shadow_tag (&decl_specifiers
);
13925 /* Perform any deferred access checks. */
13926 perform_deferred_access_checks (tf_warning_or_error
);
13929 /* Consume the `;'. */
13931 if (!maybe_range_for_decl
)
13932 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13933 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13935 if (init_loc
!= UNKNOWN_LOCATION
)
13936 error_at (init_loc
, "initializer in range-based %<for%> loop");
13937 if (comma_loc
!= UNKNOWN_LOCATION
)
13938 error_at (comma_loc
,
13939 "multiple declarations in range-based %<for%> loop");
13943 pop_deferring_access_checks ();
13946 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13947 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13951 cp_parser_decomposition_declaration (cp_parser
*parser
,
13952 cp_decl_specifier_seq
*decl_specifiers
,
13953 tree
*maybe_range_for_decl
,
13954 location_t
*init_loc
)
13956 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13957 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13958 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13960 /* Parse the identifier-list. */
13961 auto_vec
<cp_expr
, 10> v
;
13962 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13965 cp_expr e
= cp_parser_identifier (parser
);
13966 if (e
.get_value () == error_mark_node
)
13969 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13971 cp_lexer_consume_token (parser
->lexer
);
13974 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13975 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13977 end_loc
= UNKNOWN_LOCATION
;
13978 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13981 cp_lexer_consume_token (parser
->lexer
);
13984 cp_parser_skip_to_end_of_statement (parser
);
13985 return error_mark_node
;
13989 if (cxx_dialect
< cxx17
)
13990 pedwarn (loc
, 0, "structured bindings only available with "
13991 "%<-std=c++17%> or %<-std=gnu++17%>");
13994 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13995 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13996 declarator
->id_loc
= loc
;
13997 if (ref_qual
!= REF_QUAL_NONE
)
13998 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13999 ref_qual
== REF_QUAL_RVALUE
,
14001 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
14002 NULL_TREE
, decl_specifiers
->attributes
,
14004 tree orig_decl
= decl
;
14008 cp_decl_specifier_seq decl_specs
;
14009 clear_decl_specs (&decl_specs
);
14010 decl_specs
.type
= make_auto ();
14012 FOR_EACH_VEC_ELT (v
, i
, e
)
14015 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
14016 sfk_none
, e
.get_location ());
14019 declarator
->u
.id
.unqualified_name
= e
.get_value ();
14020 declarator
->id_loc
= e
.get_location ();
14022 tree elt_pushed_scope
;
14023 tree decl2
= start_decl (declarator
, &decl_specs
, SD_DECOMPOSITION
,
14024 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
14025 if (decl2
== error_mark_node
)
14026 decl
= error_mark_node
;
14027 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
14029 /* Ensure we've diagnosed redeclaration if we aren't creating
14031 gcc_assert (errorcount
);
14032 decl
= error_mark_node
;
14036 if (elt_pushed_scope
)
14037 pop_scope (elt_pushed_scope
);
14042 error_at (loc
, "empty structured binding declaration");
14043 decl
= error_mark_node
;
14046 if (maybe_range_for_decl
== NULL
14047 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14049 bool non_constant_p
= false, is_direct_init
= false;
14050 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14051 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
14053 if (initializer
== NULL_TREE
14054 || (TREE_CODE (initializer
) == TREE_LIST
14055 && TREE_CHAIN (initializer
))
14057 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
14058 && CONSTRUCTOR_NELTS (initializer
) != 1))
14060 error_at (loc
, "invalid initializer for structured binding "
14062 initializer
= error_mark_node
;
14065 if (decl
!= error_mark_node
)
14067 int flags
= (decl_spec_seq_has_spec_p (decl_specifiers
, ds_constinit
)
14068 ? LOOKUP_CONSTINIT
: 0);
14069 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
14070 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
14071 (is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
)
14073 cp_finish_decomp (decl
, prev
, v
.length ());
14076 else if (decl
!= error_mark_node
)
14078 *maybe_range_for_decl
= prev
;
14079 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14080 the underlying DECL. */
14081 cp_finish_decomp (decl
, prev
, v
.length ());
14085 pop_scope (pushed_scope
);
14087 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
14089 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
14090 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
14096 /* Parse a decl-specifier-seq.
14098 decl-specifier-seq:
14099 decl-specifier-seq [opt] decl-specifier
14100 decl-specifier attribute-specifier-seq [opt] (C++11)
14103 storage-class-specifier
14114 Concepts Extension:
14119 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14121 The parser flags FLAGS is used to control type-specifier parsing.
14123 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14126 1: one of the decl-specifiers is an elaborated-type-specifier
14127 (i.e., a type declaration)
14128 2: one of the decl-specifiers is an enum-specifier or a
14129 class-specifier (i.e., a type definition)
14134 cp_parser_decl_specifier_seq (cp_parser
* parser
,
14135 cp_parser_flags flags
,
14136 cp_decl_specifier_seq
*decl_specs
,
14137 int* declares_class_or_enum
)
14139 bool constructor_possible_p
= !parser
->in_declarator_p
;
14140 bool found_decl_spec
= false;
14141 cp_token
*start_token
= NULL
;
14144 /* Clear DECL_SPECS. */
14145 clear_decl_specs (decl_specs
);
14147 /* Assume no class or enumeration type is declared. */
14148 *declares_class_or_enum
= 0;
14150 /* Keep reading specifiers until there are no more to read. */
14153 bool constructor_p
;
14157 /* Peek at the next token. */
14158 token
= cp_lexer_peek_token (parser
->lexer
);
14160 /* Save the first token of the decl spec list for error
14163 start_token
= token
;
14164 /* Handle attributes. */
14165 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) == 0
14166 && cp_next_tokens_can_be_attribute_p (parser
))
14168 /* Parse the attributes. */
14169 tree attrs
= cp_parser_attributes_opt (parser
);
14171 /* In a sequence of declaration specifiers, c++11 attributes
14172 appertain to the type that precede them. In that case
14175 The attribute-specifier-seq affects the type only for
14176 the declaration it appears in, not other declarations
14177 involving the same type.
14179 But for now let's force the user to position the
14180 attribute either at the beginning of the declaration or
14181 after the declarator-id, which would clearly mean that it
14182 applies to the declarator. */
14183 if (cxx11_attribute_p (attrs
))
14185 if (!found_decl_spec
)
14186 /* The c++11 attribute is at the beginning of the
14187 declaration. It appertains to the entity being
14191 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
14193 /* This is an attribute following a
14194 class-specifier. */
14195 if (decl_specs
->type_definition_p
)
14196 warn_misplaced_attr_for_class_type (token
->location
,
14202 decl_specs
->std_attributes
14203 = attr_chainon (decl_specs
->std_attributes
, attrs
);
14204 if (decl_specs
->locations
[ds_std_attribute
] == 0)
14205 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
14211 decl_specs
->attributes
14212 = attr_chainon (decl_specs
->attributes
, attrs
);
14213 if (decl_specs
->locations
[ds_attribute
] == 0)
14214 decl_specs
->locations
[ds_attribute
] = token
->location
;
14217 /* Assume we will find a decl-specifier keyword. */
14218 found_decl_spec
= true;
14219 /* If the next token is an appropriate keyword, we can simply
14220 add it to the list. */
14221 switch (token
->keyword
)
14228 if (!at_class_scope_p ())
14230 gcc_rich_location
richloc (token
->location
);
14231 richloc
.add_fixit_remove ();
14232 error_at (&richloc
, "%<friend%> used outside of class");
14233 cp_lexer_purge_token (parser
->lexer
);
14238 /* Consume the token. */
14239 cp_lexer_consume_token (parser
->lexer
);
14243 case RID_CONSTEXPR
:
14245 cp_lexer_consume_token (parser
->lexer
);
14248 case RID_CONSTINIT
:
14250 cp_lexer_consume_token (parser
->lexer
);
14253 case RID_CONSTEVAL
:
14255 cp_lexer_consume_token (parser
->lexer
);
14260 cp_lexer_consume_token (parser
->lexer
);
14262 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14265 /* Warn for concept as a decl-specifier. We'll rewrite these as
14266 concept declarations later. */
14267 if (!flag_concepts_ts
)
14269 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
14270 if (next
->keyword
== RID_BOOL
)
14271 pedwarn (next
->location
, 0, "the %<bool%> keyword is not "
14272 "allowed in a C++20 concept definition");
14274 pedwarn (token
->location
, 0, "C++20 concept definition syntax "
14275 "is %<concept <name> = <expr>%>");
14278 /* In C++20 a concept definition is just 'concept name = expr;'
14279 Support that syntax as a TS extension by pretending we've seen
14280 the 'bool' specifier. */
14281 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14282 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
)
14283 && !decl_specs
->any_type_specifiers_p
)
14285 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
14286 token
, /*type_definition*/false);
14287 decl_specs
->any_type_specifiers_p
= true;
14291 /* function-specifier:
14298 cp_parser_function_specifier_opt (parser
, decl_specs
);
14305 /* Consume the token. */
14306 cp_lexer_consume_token (parser
->lexer
);
14308 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14311 /* A constructor declarator cannot appear in a typedef. */
14312 constructor_possible_p
= false;
14313 /* The "typedef" keyword can only occur in a declaration; we
14314 may as well commit at this point. */
14315 cp_parser_commit_to_tentative_parse (parser
);
14317 if (decl_specs
->storage_class
!= sc_none
)
14318 decl_specs
->conflicting_specifiers_p
= true;
14321 /* storage-class-specifier:
14331 if (cxx_dialect
== cxx98
)
14333 /* Consume the token. */
14334 cp_lexer_consume_token (parser
->lexer
);
14336 /* Complain about `auto' as a storage specifier, if
14337 we're complaining about C++0x compatibility. */
14338 gcc_rich_location
richloc (token
->location
);
14339 richloc
.add_fixit_remove ();
14340 warning_at (&richloc
, OPT_Wc__11_compat
,
14341 "%<auto%> changes meaning in C++11; "
14342 "please remove it");
14344 /* Set the storage class anyway. */
14345 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14349 /* C++0x auto type-specifier. */
14350 found_decl_spec
= false;
14357 /* Consume the token. */
14358 cp_lexer_consume_token (parser
->lexer
);
14359 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14363 /* Consume the token. */
14365 cp_lexer_consume_token (parser
->lexer
);
14369 /* We did not yet find a decl-specifier yet. */
14370 found_decl_spec
= false;
14374 if (found_decl_spec
14375 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14376 && token
->keyword
!= RID_CONSTEXPR
)
14377 error ("%<decl-specifier%> invalid in condition");
14379 if (found_decl_spec
14380 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14381 && token
->keyword
!= RID_MUTABLE
14382 && token
->keyword
!= RID_CONSTEXPR
14383 && token
->keyword
!= RID_CONSTEVAL
)
14384 error_at (token
->location
, "%qD invalid in lambda",
14385 ridpointers
[token
->keyword
]);
14388 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14390 /* Constructors are a special case. The `S' in `S()' is not a
14391 decl-specifier; it is the beginning of the declarator. */
14393 = (!found_decl_spec
14394 && constructor_possible_p
14395 && (cp_parser_constructor_declarator_p
14396 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14399 /* If we don't have a DECL_SPEC yet, then we must be looking at
14400 a type-specifier. */
14401 if (!found_decl_spec
&& !constructor_p
)
14403 int decl_spec_declares_class_or_enum
;
14404 bool is_cv_qualifier
;
14407 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14408 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
14411 = cp_parser_type_specifier (parser
, flags
,
14413 /*is_declaration=*/true,
14414 &decl_spec_declares_class_or_enum
,
14416 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14418 /* If this type-specifier referenced a user-defined type
14419 (a typedef, class-name, etc.), then we can't allow any
14420 more such type-specifiers henceforth.
14424 The longest sequence of decl-specifiers that could
14425 possibly be a type name is taken as the
14426 decl-specifier-seq of a declaration. The sequence shall
14427 be self-consistent as described below.
14431 As a general rule, at most one type-specifier is allowed
14432 in the complete decl-specifier-seq of a declaration. The
14433 only exceptions are the following:
14435 -- const or volatile can be combined with any other
14438 -- signed or unsigned can be combined with char, long,
14446 void g (const int Pc);
14448 Here, Pc is *not* part of the decl-specifier seq; it's
14449 the declarator. Therefore, once we see a type-specifier
14450 (other than a cv-qualifier), we forbid any additional
14451 user-defined types. We *do* still allow things like `int
14452 int' to be considered a decl-specifier-seq, and issue the
14453 error message later. */
14454 if (type_spec
&& !is_cv_qualifier
)
14455 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14456 /* A constructor declarator cannot follow a type-specifier. */
14459 constructor_possible_p
= false;
14460 found_decl_spec
= true;
14461 if (!is_cv_qualifier
)
14462 decl_specs
->any_type_specifiers_p
= true;
14464 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14465 error_at (token
->location
, "type-specifier invalid in lambda");
14469 /* If we still do not have a DECL_SPEC, then there are no more
14470 decl-specifiers. */
14471 if (!found_decl_spec
)
14474 decl_specs
->any_specifiers_p
= true;
14475 /* After we see one decl-specifier, further decl-specifiers are
14476 always optional. */
14477 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14480 /* Don't allow a friend specifier with a class definition. */
14481 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14482 && (*declares_class_or_enum
& 2))
14483 error_at (decl_specs
->locations
[ds_friend
],
14484 "class definition may not be declared a friend");
14487 /* Parse an (optional) storage-class-specifier.
14489 storage-class-specifier:
14498 storage-class-specifier:
14501 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14504 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14506 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14509 if (cxx_dialect
!= cxx98
)
14511 /* Fall through for C++98. */
14512 gcc_fallthrough ();
14519 /* Consume the token. */
14520 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14527 /* Parse an (optional) function-specifier.
14529 function-specifier:
14535 explicit(constant-expression)
14537 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14538 Updates DECL_SPECS, if it is non-NULL. */
14541 cp_parser_function_specifier_opt (cp_parser
* parser
,
14542 cp_decl_specifier_seq
*decl_specs
)
14544 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14545 switch (token
->keyword
)
14548 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14552 /* 14.5.2.3 [temp.mem]
14554 A member function template shall not be virtual. */
14555 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14556 && current_class_type
)
14557 error_at (token
->location
, "templates may not be %<virtual%>");
14559 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14564 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14565 /* If we see '(', it's C++20 explicit(bool). */
14567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14569 matching_parens parens
;
14570 parens
.consume_open (parser
);
14572 /* New types are not allowed in an explicit-specifier. */
14573 const char *saved_message
14574 = parser
->type_definition_forbidden_message
;
14575 parser
->type_definition_forbidden_message
14576 = G_("types may not be defined in explicit-specifier");
14578 if (cxx_dialect
< cxx20
)
14579 pedwarn (token
->location
, 0,
14580 "%<explicit(bool)%> only available with %<-std=c++20%> "
14581 "or %<-std=gnu++20%>");
14583 /* Parse the constant-expression. */
14584 expr
= cp_parser_constant_expression (parser
);
14586 /* Restore the saved message. */
14587 parser
->type_definition_forbidden_message
= saved_message
;
14588 parens
.require_close (parser
);
14591 /* The explicit-specifier explicit without a constant-expression is
14592 equivalent to the explicit-specifier explicit(true). */
14593 expr
= boolean_true_node
;
14596 "the constant-expression, if supplied, shall be a contextually
14597 converted constant expression of type bool." */
14598 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14599 /* We could evaluate it -- mark the decl as appropriate. */
14600 if (expr
== boolean_true_node
)
14601 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14602 else if (expr
== boolean_false_node
)
14603 /* Don't mark the decl as explicit. */;
14604 else if (decl_specs
)
14605 /* The expression was value-dependent. Remember it so that we can
14606 substitute it later. */
14607 decl_specs
->explicit_specifier
= expr
;
14615 /* Consume the token. */
14616 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14619 /* Parse a linkage-specification.
14621 linkage-specification:
14622 extern string-literal { declaration-seq [opt] }
14623 extern string-literal declaration */
14626 cp_parser_linkage_specification (cp_parser
* parser
)
14630 /* Look for the `extern' keyword. */
14631 cp_token
*extern_token
14632 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14634 /* Look for the string-literal. */
14635 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14636 linkage
= cp_parser_string_literal (parser
, false, false);
14638 /* Transform the literal into an identifier. If the literal is a
14639 wide-character string, or contains embedded NULs, then we can't
14640 handle it as the user wants. */
14641 if (strlen (TREE_STRING_POINTER (linkage
))
14642 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14644 cp_parser_error (parser
, "invalid linkage-specification");
14645 /* Assume C++ linkage. */
14646 linkage
= lang_name_cplusplus
;
14649 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14651 /* We're now using the new linkage. */
14652 push_lang_context (linkage
);
14654 /* Preserve the location of the innermost linkage specification,
14655 tracking the locations of nested specifications via a local. */
14656 location_t saved_location
14657 = parser
->innermost_linkage_specification_location
;
14658 /* Construct a location ranging from the start of the "extern" to
14659 the end of the string-literal, with the caret at the start, e.g.:
14663 parser
->innermost_linkage_specification_location
14664 = make_location (extern_token
->location
,
14665 extern_token
->location
,
14666 get_finish (string_token
->location
));
14668 /* If the next token is a `{', then we're using the first
14670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14672 cp_ensure_no_omp_declare_simd (parser
);
14673 cp_ensure_no_oacc_routine (parser
);
14675 /* Consume the `{' token. */
14676 matching_braces braces
;
14677 braces
.consume_open (parser
);
14678 /* Parse the declarations. */
14679 cp_parser_declaration_seq_opt (parser
);
14680 /* Look for the closing `}'. */
14681 braces
.require_close (parser
);
14683 /* Otherwise, there's just one declaration. */
14686 bool saved_in_unbraced_linkage_specification_p
;
14688 saved_in_unbraced_linkage_specification_p
14689 = parser
->in_unbraced_linkage_specification_p
;
14690 parser
->in_unbraced_linkage_specification_p
= true;
14691 cp_parser_declaration (parser
);
14692 parser
->in_unbraced_linkage_specification_p
14693 = saved_in_unbraced_linkage_specification_p
;
14696 /* We're done with the linkage-specification. */
14697 pop_lang_context ();
14699 /* Restore location of parent linkage specification, if any. */
14700 parser
->innermost_linkage_specification_location
= saved_location
;
14703 /* Parse a static_assert-declaration.
14705 static_assert-declaration:
14706 static_assert ( constant-expression , string-literal ) ;
14707 static_assert ( constant-expression ) ; (C++17)
14709 If MEMBER_P, this static_assert is a class member. */
14712 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14715 location_t token_loc
;
14719 /* Peek at the `static_assert' token so we can keep track of exactly
14720 where the static assertion started. */
14721 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14723 /* Look for the `static_assert' keyword. */
14724 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14728 /* We know we are in a static assertion; commit to any tentative
14730 if (cp_parser_parsing_tentatively (parser
))
14731 cp_parser_commit_to_tentative_parse (parser
);
14733 /* Parse the `(' starting the static assertion condition. */
14734 matching_parens parens
;
14735 parens
.require_open (parser
);
14737 /* Parse the constant-expression. Allow a non-constant expression
14738 here in order to give better diagnostics in finish_static_assert. */
14740 cp_parser_constant_expression (parser
,
14741 /*allow_non_constant_p=*/true,
14742 /*non_constant_p=*/&dummy
);
14744 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14746 if (cxx_dialect
< cxx17
)
14747 pedwarn (input_location
, OPT_Wpedantic
,
14748 "%<static_assert%> without a message "
14749 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14751 cp_lexer_consume_token (parser
->lexer
);
14752 message
= build_string (1, "");
14753 TREE_TYPE (message
) = char_array_type_node
;
14754 fix_string_type (message
);
14758 /* Parse the separating `,'. */
14759 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14761 /* Parse the string-literal message. */
14762 message
= cp_parser_string_literal (parser
,
14763 /*translate=*/false,
14766 /* A `)' completes the static assertion. */
14767 if (!parens
.require_close (parser
))
14768 cp_parser_skip_to_closing_parenthesis (parser
,
14769 /*recovering=*/true,
14770 /*or_comma=*/false,
14771 /*consume_paren=*/true);
14774 /* A semicolon terminates the declaration. */
14775 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14777 /* Get the location for the static assertion. Use that of the
14778 condition if available, otherwise, use that of the "static_assert"
14780 location_t assert_loc
= condition
.get_location ();
14781 if (assert_loc
== UNKNOWN_LOCATION
)
14782 assert_loc
= token_loc
;
14784 /* Complete the static assertion, which may mean either processing
14785 the static assert now or saving it for template instantiation. */
14786 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14789 /* Parse the expression in decltype ( expression ). */
14792 cp_parser_decltype_expr (cp_parser
*parser
,
14793 bool &id_expression_or_member_access_p
)
14795 cp_token
*id_expr_start_token
;
14798 /* First, try parsing an id-expression. */
14799 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14800 cp_parser_parse_tentatively (parser
);
14801 expr
= cp_parser_id_expression (parser
,
14802 /*template_keyword_p=*/false,
14803 /*check_dependency_p=*/true,
14804 /*template_p=*/NULL
,
14805 /*declarator_p=*/false,
14806 /*optional_p=*/false);
14808 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14810 bool non_integral_constant_expression_p
= false;
14811 tree id_expression
= expr
;
14813 const char *error_msg
;
14815 if (identifier_p (expr
))
14816 /* Lookup the name we got back from the id-expression. */
14817 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14818 id_expr_start_token
->location
);
14820 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14821 /* A template without args is not a complete id-expression. */
14822 expr
= error_mark_node
;
14825 && expr
!= error_mark_node
14826 && TREE_CODE (expr
) != TYPE_DECL
14827 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14828 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14829 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14831 /* Complete lookup of the id-expression. */
14832 expr
= (finish_id_expression
14833 (id_expression
, expr
, parser
->scope
, &idk
,
14834 /*integral_constant_expression_p=*/false,
14835 /*allow_non_integral_constant_expression_p=*/true,
14836 &non_integral_constant_expression_p
,
14837 /*template_p=*/false,
14839 /*address_p=*/false,
14840 /*template_arg_p=*/false,
14842 id_expr_start_token
->location
));
14844 if (expr
== error_mark_node
)
14845 /* We found an id-expression, but it was something that we
14846 should not have found. This is an error, not something
14847 we can recover from, so note that we found an
14848 id-expression and we'll recover as gracefully as
14850 id_expression_or_member_access_p
= true;
14854 && expr
!= error_mark_node
14855 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14856 /* We have an id-expression. */
14857 id_expression_or_member_access_p
= true;
14860 if (!id_expression_or_member_access_p
)
14862 /* Abort the id-expression parse. */
14863 cp_parser_abort_tentative_parse (parser
);
14865 /* Parsing tentatively, again. */
14866 cp_parser_parse_tentatively (parser
);
14868 /* Parse a class member access. */
14869 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14870 /*cast_p=*/false, /*decltype*/true,
14871 /*member_access_only_p=*/true, NULL
);
14874 && expr
!= error_mark_node
14875 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14876 /* We have an id-expression. */
14877 id_expression_or_member_access_p
= true;
14880 if (id_expression_or_member_access_p
)
14881 /* We have parsed the complete id-expression or member access. */
14882 cp_parser_parse_definitely (parser
);
14885 /* Abort our attempt to parse an id-expression or member access
14887 cp_parser_abort_tentative_parse (parser
);
14889 /* Parse a full expression. */
14890 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14891 /*decltype_p=*/true);
14897 /* Parse a `decltype' type. Returns the type.
14899 decltype-specifier:
14900 decltype ( expression )
14902 decltype ( auto ) */
14905 cp_parser_decltype (cp_parser
*parser
)
14907 bool id_expression_or_member_access_p
= false;
14908 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14910 if (start_token
->type
== CPP_DECLTYPE
)
14912 /* Already parsed. */
14913 cp_lexer_consume_token (parser
->lexer
);
14914 return saved_checks_value (start_token
->u
.tree_check_value
);
14917 /* Look for the `decltype' token. */
14918 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14919 return error_mark_node
;
14921 /* Parse the opening `('. */
14922 matching_parens parens
;
14923 if (!parens
.require_open (parser
))
14924 return error_mark_node
;
14926 /* Since we're going to preserve any side-effects from this parse, set up a
14927 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14928 in the expression. */
14929 tentative_firewall
firewall (parser
);
14931 /* If in_declarator_p, a reparse as an expression might succeed (60361).
14932 Otherwise, commit now for better diagnostics. */
14933 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
14934 && !parser
->in_declarator_p
)
14935 cp_parser_commit_to_topmost_tentative_parse (parser
);
14937 push_deferring_access_checks (dk_deferred
);
14939 tree expr
= NULL_TREE
;
14941 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
)
14942 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
14944 /* decltype (auto) */
14945 cp_lexer_consume_token (parser
->lexer
);
14946 if (cxx_dialect
< cxx14
)
14948 error_at (start_token
->location
,
14949 "%<decltype(auto)%> type specifier only available with "
14950 "%<-std=c++14%> or %<-std=gnu++14%>");
14951 expr
= error_mark_node
;
14956 /* decltype (expression) */
14958 /* Types cannot be defined in a `decltype' expression. Save away the
14959 old message and set the new one. */
14960 const char *saved_message
= parser
->type_definition_forbidden_message
;
14961 parser
->type_definition_forbidden_message
14962 = G_("types may not be defined in %<decltype%> expressions");
14964 /* The restrictions on constant-expressions do not apply inside
14965 decltype expressions. */
14966 bool saved_integral_constant_expression_p
14967 = parser
->integral_constant_expression_p
;
14968 bool saved_non_integral_constant_expression_p
14969 = parser
->non_integral_constant_expression_p
;
14970 parser
->integral_constant_expression_p
= false;
14972 /* Within a parenthesized expression, a `>' token is always
14973 the greater-than operator. */
14974 bool saved_greater_than_is_operator_p
14975 = parser
->greater_than_is_operator_p
;
14976 parser
->greater_than_is_operator_p
= true;
14978 /* Do not actually evaluate the expression. */
14979 ++cp_unevaluated_operand
;
14981 /* Do not warn about problems with the expression. */
14982 ++c_inhibit_evaluation_warnings
;
14984 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14985 STRIP_ANY_LOCATION_WRAPPER (expr
);
14987 /* Go back to evaluating expressions. */
14988 --cp_unevaluated_operand
;
14989 --c_inhibit_evaluation_warnings
;
14991 /* The `>' token might be the end of a template-id or
14992 template-parameter-list now. */
14993 parser
->greater_than_is_operator_p
14994 = saved_greater_than_is_operator_p
;
14996 /* Restore the old message and the integral constant expression
14998 parser
->type_definition_forbidden_message
= saved_message
;
14999 parser
->integral_constant_expression_p
15000 = saved_integral_constant_expression_p
;
15001 parser
->non_integral_constant_expression_p
15002 = saved_non_integral_constant_expression_p
;
15005 /* Parse to the closing `)'. */
15006 if (expr
== error_mark_node
|| !parens
.require_close (parser
))
15008 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15009 /*consume_paren=*/true);
15010 expr
= error_mark_node
;
15013 /* If we got a parse error while tentative, bail out now. */
15014 if (cp_parser_error_occurred (parser
))
15016 pop_deferring_access_checks ();
15017 return error_mark_node
;
15022 expr
= make_decltype_auto ();
15024 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
15025 tf_warning_or_error
);
15027 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15029 start_token
->type
= CPP_DECLTYPE
;
15030 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15031 start_token
->tree_check_p
= true;
15032 start_token
->u
.tree_check_value
->value
= expr
;
15033 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15034 start_token
->keyword
= RID_MAX
;
15036 location_t loc
= start_token
->location
;
15037 loc
= make_location (loc
, loc
, parser
->lexer
);
15038 start_token
->location
= loc
;
15040 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
15042 pop_to_parent_deferring_access_checks ();
15047 /* Special member functions [gram.special] */
15049 /* Parse a conversion-function-id.
15051 conversion-function-id:
15052 operator conversion-type-id
15054 Returns an IDENTIFIER_NODE representing the operator. */
15057 cp_parser_conversion_function_id (cp_parser
* parser
)
15061 tree saved_qualifying_scope
;
15062 tree saved_object_scope
;
15063 tree pushed_scope
= NULL_TREE
;
15065 /* Look for the `operator' token. */
15066 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15067 return error_mark_node
;
15068 /* When we parse the conversion-type-id, the current scope will be
15069 reset. However, we need that information in able to look up the
15070 conversion function later, so we save it here. */
15071 saved_scope
= parser
->scope
;
15072 saved_qualifying_scope
= parser
->qualifying_scope
;
15073 saved_object_scope
= parser
->object_scope
;
15074 /* We must enter the scope of the class so that the names of
15075 entities declared within the class are available in the
15076 conversion-type-id. For example, consider:
15083 S::operator I() { ... }
15085 In order to see that `I' is a type-name in the definition, we
15086 must be in the scope of `S'. */
15088 pushed_scope
= push_scope (saved_scope
);
15089 /* Parse the conversion-type-id. */
15090 type
= cp_parser_conversion_type_id (parser
);
15091 /* Leave the scope of the class, if any. */
15093 pop_scope (pushed_scope
);
15094 /* Restore the saved scope. */
15095 parser
->scope
= saved_scope
;
15096 parser
->qualifying_scope
= saved_qualifying_scope
;
15097 parser
->object_scope
= saved_object_scope
;
15098 /* If the TYPE is invalid, indicate failure. */
15099 if (type
== error_mark_node
)
15100 return error_mark_node
;
15101 return make_conv_op_name (type
);
15104 /* Parse a conversion-type-id:
15106 conversion-type-id:
15107 type-specifier-seq conversion-declarator [opt]
15109 Returns the TYPE specified. */
15112 cp_parser_conversion_type_id (cp_parser
* parser
)
15115 cp_decl_specifier_seq type_specifiers
;
15116 cp_declarator
*declarator
;
15117 tree type_specified
;
15118 const char *saved_message
;
15120 /* Parse the attributes. */
15121 attributes
= cp_parser_attributes_opt (parser
);
15123 saved_message
= parser
->type_definition_forbidden_message
;
15124 parser
->type_definition_forbidden_message
15125 = G_("types may not be defined in a conversion-type-id");
15127 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15128 optional in conversion-type-id. */
15129 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15130 /*is_declaration=*/false,
15131 /*is_trailing_return=*/false,
15134 parser
->type_definition_forbidden_message
= saved_message
;
15136 /* If that didn't work, stop. */
15137 if (type_specifiers
.type
== error_mark_node
)
15138 return error_mark_node
;
15139 /* Parse the conversion-declarator. */
15140 declarator
= cp_parser_conversion_declarator_opt (parser
);
15142 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
15143 /*initialized=*/0, &attributes
);
15145 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
15147 /* Don't give this error when parsing tentatively. This happens to
15148 work because we always parse this definitively once. */
15149 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
15150 && type_uses_auto (type_specified
))
15152 if (cxx_dialect
< cxx14
)
15154 error ("invalid use of %<auto%> in conversion operator");
15155 return error_mark_node
;
15157 else if (template_parm_scope_p ())
15158 warning (0, "use of %<auto%> in member template "
15159 "conversion operator can never be deduced");
15162 return type_specified
;
15165 /* Parse an (optional) conversion-declarator.
15167 conversion-declarator:
15168 ptr-operator conversion-declarator [opt]
15172 static cp_declarator
*
15173 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
15175 enum tree_code code
;
15176 tree class_type
, std_attributes
= NULL_TREE
;
15177 cp_cv_quals cv_quals
;
15179 /* We don't know if there's a ptr-operator next, or not. */
15180 cp_parser_parse_tentatively (parser
);
15181 /* Try the ptr-operator. */
15182 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
15184 /* If it worked, look for more conversion-declarators. */
15185 if (cp_parser_parse_definitely (parser
))
15187 cp_declarator
*declarator
;
15189 /* Parse another optional declarator. */
15190 declarator
= cp_parser_conversion_declarator_opt (parser
);
15192 declarator
= cp_parser_make_indirect_declarator
15193 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
15201 /* Parse an (optional) ctor-initializer.
15204 : mem-initializer-list */
15207 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
15209 /* If the next token is not a `:', then there is no
15210 ctor-initializer. */
15211 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
15213 /* Do default initialization of any bases and members. */
15214 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15215 finish_mem_initializers (NULL_TREE
);
15219 /* Consume the `:' token. */
15220 cp_lexer_consume_token (parser
->lexer
);
15221 /* And the mem-initializer-list. */
15222 cp_parser_mem_initializer_list (parser
);
15225 /* Parse a mem-initializer-list.
15227 mem-initializer-list:
15228 mem-initializer ... [opt]
15229 mem-initializer ... [opt] , mem-initializer-list */
15232 cp_parser_mem_initializer_list (cp_parser
* parser
)
15234 tree mem_initializer_list
= NULL_TREE
;
15235 tree target_ctor
= error_mark_node
;
15236 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15238 /* Let the semantic analysis code know that we are starting the
15239 mem-initializer-list. */
15240 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
15241 error_at (token
->location
,
15242 "only constructors take member initializers");
15244 /* Loop through the list. */
15247 tree mem_initializer
;
15249 token
= cp_lexer_peek_token (parser
->lexer
);
15250 /* Parse the mem-initializer. */
15251 mem_initializer
= cp_parser_mem_initializer (parser
);
15252 /* If the next token is a `...', we're expanding member initializers. */
15253 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15255 || (mem_initializer
!= error_mark_node
15256 && check_for_bare_parameter_packs (TREE_PURPOSE
15257 (mem_initializer
))))
15259 /* Consume the `...'. */
15261 cp_lexer_consume_token (parser
->lexer
);
15263 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15264 can be expanded but members cannot. */
15265 if (mem_initializer
!= error_mark_node
15266 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
15268 error_at (token
->location
,
15269 "cannot expand initializer for member %qD",
15270 TREE_PURPOSE (mem_initializer
));
15271 mem_initializer
= error_mark_node
;
15274 /* Construct the pack expansion type. */
15275 if (mem_initializer
!= error_mark_node
)
15276 mem_initializer
= make_pack_expansion (mem_initializer
);
15278 if (target_ctor
!= error_mark_node
15279 && mem_initializer
!= error_mark_node
)
15281 error ("mem-initializer for %qD follows constructor delegation",
15282 TREE_PURPOSE (mem_initializer
));
15283 mem_initializer
= error_mark_node
;
15285 /* Look for a target constructor. */
15286 if (mem_initializer
!= error_mark_node
15287 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
15288 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
15290 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
15291 if (mem_initializer_list
)
15293 error ("constructor delegation follows mem-initializer for %qD",
15294 TREE_PURPOSE (mem_initializer_list
));
15295 mem_initializer
= error_mark_node
;
15297 target_ctor
= mem_initializer
;
15299 /* Add it to the list, unless it was erroneous. */
15300 if (mem_initializer
!= error_mark_node
)
15302 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
15303 mem_initializer_list
= mem_initializer
;
15305 /* If the next token is not a `,', we're done. */
15306 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15308 /* Consume the `,' token. */
15309 cp_lexer_consume_token (parser
->lexer
);
15312 /* Perform semantic analysis. */
15313 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15314 finish_mem_initializers (mem_initializer_list
);
15317 /* Parse a mem-initializer.
15320 mem-initializer-id ( expression-list [opt] )
15321 mem-initializer-id braced-init-list
15326 ( expression-list [opt] )
15328 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15329 class) or FIELD_DECL (for a non-static data member) to initialize;
15330 the TREE_VALUE is the expression-list. An empty initialization
15331 list is represented by void_list_node. */
15334 cp_parser_mem_initializer (cp_parser
* parser
)
15336 tree mem_initializer_id
;
15337 tree expression_list
;
15339 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15341 /* Find out what is being initialized. */
15342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15344 permerror (token
->location
,
15345 "anachronistic old-style base class initializer");
15346 mem_initializer_id
= NULL_TREE
;
15350 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15351 if (mem_initializer_id
== error_mark_node
)
15352 return mem_initializer_id
;
15354 member
= expand_member_init (mem_initializer_id
);
15355 if (member
&& !DECL_P (member
))
15356 in_base_initializer
= 1;
15358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15360 bool expr_non_constant_p
;
15361 cp_lexer_set_source_position (parser
->lexer
);
15362 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15363 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15364 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15365 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15369 vec
<tree
, va_gc
> *vec
;
15370 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15372 /*allow_expansion_p=*/true,
15373 /*non_constant_p=*/NULL
,
15374 /*close_paren_loc=*/NULL
,
15375 /*wrap_locations_p=*/true);
15377 return error_mark_node
;
15378 expression_list
= build_tree_list_vec (vec
);
15379 release_tree_vector (vec
);
15382 if (expression_list
== error_mark_node
)
15383 return error_mark_node
;
15384 if (!expression_list
)
15385 expression_list
= void_type_node
;
15387 in_base_initializer
= 0;
15389 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15392 /* Parse a mem-initializer-id.
15394 mem-initializer-id:
15395 :: [opt] nested-name-specifier [opt] class-name
15396 decltype-specifier (C++11)
15399 Returns a TYPE indicating the class to be initialized for the first
15400 production (and the second in C++11). Returns an IDENTIFIER_NODE
15401 indicating the data member to be initialized for the last production. */
15404 cp_parser_mem_initializer_id (cp_parser
* parser
)
15406 bool global_scope_p
;
15407 bool nested_name_specifier_p
;
15408 bool template_p
= false;
15411 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15413 /* `typename' is not allowed in this context ([temp.res]). */
15414 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15416 error_at (token
->location
,
15417 "keyword %<typename%> not allowed in this context (a qualified "
15418 "member initializer is implicitly a type)");
15419 cp_lexer_consume_token (parser
->lexer
);
15421 /* Look for the optional `::' operator. */
15423 = (cp_parser_global_scope_opt (parser
,
15424 /*current_scope_valid_p=*/false)
15426 /* Look for the optional nested-name-specifier. The simplest way to
15431 The keyword `typename' is not permitted in a base-specifier or
15432 mem-initializer; in these contexts a qualified name that
15433 depends on a template-parameter is implicitly assumed to be a
15436 is to assume that we have seen the `typename' keyword at this
15438 nested_name_specifier_p
15439 = (cp_parser_nested_name_specifier_opt (parser
,
15440 /*typename_keyword_p=*/true,
15441 /*check_dependency_p=*/true,
15443 /*is_declaration=*/true)
15445 if (nested_name_specifier_p
)
15446 template_p
= cp_parser_optional_template_keyword (parser
);
15447 /* If there is a `::' operator or a nested-name-specifier, then we
15448 are definitely looking for a class-name. */
15449 if (global_scope_p
|| nested_name_specifier_p
)
15450 return cp_parser_class_name (parser
,
15451 /*typename_keyword_p=*/true,
15452 /*template_keyword_p=*/template_p
,
15454 /*check_dependency_p=*/true,
15455 /*class_head_p=*/false,
15456 /*is_declaration=*/true);
15457 /* Otherwise, we could also be looking for an ordinary identifier. */
15458 cp_parser_parse_tentatively (parser
);
15459 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15460 /* Try a decltype-specifier. */
15461 id
= cp_parser_decltype (parser
);
15463 /* Otherwise, try a class-name. */
15464 id
= cp_parser_class_name (parser
,
15465 /*typename_keyword_p=*/true,
15466 /*template_keyword_p=*/false,
15468 /*check_dependency_p=*/true,
15469 /*class_head_p=*/false,
15470 /*is_declaration=*/true);
15471 /* If we found one, we're done. */
15472 if (cp_parser_parse_definitely (parser
))
15474 /* Otherwise, look for an ordinary identifier. */
15475 return cp_parser_identifier (parser
);
15478 /* Overloading [gram.over] */
15480 /* Parse an operator-function-id.
15482 operator-function-id:
15485 Returns an IDENTIFIER_NODE for the operator which is a
15486 human-readable spelling of the identifier, e.g., `operator +'. */
15489 cp_parser_operator_function_id (cp_parser
* parser
)
15491 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15492 /* Look for the `operator' keyword. */
15493 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15494 return error_mark_node
;
15495 /* And then the name of the operator itself. */
15496 return cp_parser_operator (parser
, start_loc
);
15499 /* Return an identifier node for a user-defined literal operator.
15500 The suffix identifier is chained to the operator name identifier. */
15503 cp_literal_operator_id (const char* name
)
15506 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15507 + strlen (name
) + 10);
15508 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15509 identifier
= get_identifier (buffer
);
15510 XDELETEVEC (buffer
);
15515 /* Parse an operator.
15518 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15519 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15520 || ++ -- , ->* -> () []
15527 Returns an IDENTIFIER_NODE for the operator which is a
15528 human-readable spelling of the identifier, e.g., `operator +'. */
15531 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15533 tree id
= NULL_TREE
;
15537 /* Peek at the next token. */
15538 token
= cp_lexer_peek_token (parser
->lexer
);
15540 location_t end_loc
= token
->location
;
15542 /* Figure out which operator we have. */
15543 enum tree_code op
= ERROR_MARK
;
15544 bool assop
= false;
15545 bool consumed
= false;
15546 switch (token
->type
)
15550 /* The keyword should be either `new', `delete' or `co_await'. */
15551 if (token
->keyword
== RID_NEW
)
15553 else if (token
->keyword
== RID_DELETE
)
15555 else if (token
->keyword
== RID_CO_AWAIT
)
15556 op
= CO_AWAIT_EXPR
;
15560 /* Consume the `new', `delete' or co_await token. */
15561 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15563 /* Peek at the next token. */
15564 token
= cp_lexer_peek_token (parser
->lexer
);
15565 /* If it's a `[' token then this is the array variant of the
15567 if (token
->type
== CPP_OPEN_SQUARE
15568 && op
!= CO_AWAIT_EXPR
)
15570 /* Consume the `[' token. */
15571 cp_lexer_consume_token (parser
->lexer
);
15572 /* Look for the `]' token. */
15573 if (cp_token
*close_token
15574 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15575 end_loc
= close_token
->location
;
15576 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15595 op
= TRUNC_DIV_EXPR
;
15599 op
= TRUNC_MOD_EXPR
;
15619 op
= TRUTH_NOT_EXPR
;
15652 op
= TRUNC_DIV_EXPR
;
15657 op
= TRUNC_MOD_EXPR
;
15683 case CPP_LSHIFT_EQ
:
15688 case CPP_RSHIFT_EQ
:
15705 case CPP_GREATER_EQ
:
15709 case CPP_SPACESHIP
:
15710 op
= SPACESHIP_EXPR
;
15714 op
= TRUTH_ANDIF_EXPR
;
15718 op
= TRUTH_ORIF_EXPR
;
15721 case CPP_PLUS_PLUS
:
15722 op
= POSTINCREMENT_EXPR
;
15725 case CPP_MINUS_MINUS
:
15726 op
= PREDECREMENT_EXPR
;
15730 op
= COMPOUND_EXPR
;
15733 case CPP_DEREF_STAR
:
15738 op
= COMPONENT_REF
;
15743 /* Consume the `?'. */
15744 cp_lexer_consume_token (parser
->lexer
);
15745 /* Look for the matching `:'. */
15746 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
15750 case CPP_OPEN_PAREN
:
15752 /* Consume the `('. */
15753 matching_parens parens
;
15754 parens
.consume_open (parser
);
15755 /* Look for the matching `)'. */
15756 token
= parens
.require_close (parser
);
15758 end_loc
= token
->location
;
15764 case CPP_OPEN_SQUARE
:
15765 /* Consume the `['. */
15766 cp_lexer_consume_token (parser
->lexer
);
15767 /* Look for the matching `]'. */
15768 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15770 end_loc
= token
->location
;
15775 case CPP_UTF8STRING
:
15776 case CPP_UTF8STRING_USERDEF
:
15783 case CPP_STRING_USERDEF
:
15784 case CPP_WSTRING_USERDEF
:
15785 case CPP_STRING16_USERDEF
:
15786 case CPP_STRING32_USERDEF
:
15792 if (cxx_dialect
== cxx98
)
15793 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15795 /* Consume the string. */
15796 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15797 /*wide_ok=*/true, /*lookup_udlit=*/false);
15798 if (str
== error_mark_node
)
15799 return error_mark_node
;
15800 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15802 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15803 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15804 end_loc
= str
.get_location ();
15809 /* Look for the suffix identifier. */
15810 token
= cp_lexer_peek_token (parser
->lexer
);
15811 if (token
->type
== CPP_NAME
)
15813 id
= cp_parser_identifier (parser
);
15814 end_loc
= token
->location
;
15816 else if (token
->type
== CPP_KEYWORD
)
15818 error ("unexpected keyword;"
15819 " remove space between quotes and suffix identifier");
15820 return error_mark_node
;
15824 error ("expected suffix identifier");
15825 return error_mark_node
;
15828 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15829 (TREE_TYPE (TREE_TYPE (string_tree
))));
15830 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15833 error ("expected empty string after %<operator%> keyword");
15834 return error_mark_node
;
15836 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15839 error ("invalid encoding prefix in literal operator");
15840 return error_mark_node
;
15842 if (id
!= error_mark_node
)
15844 const char *name
= IDENTIFIER_POINTER (id
);
15845 id
= cp_literal_operator_id (name
);
15847 /* Generate a location of the form:
15848 "" _suffix_identifier
15849 ^~~~~~~~~~~~~~~~~~~~~
15850 with caret == start at the start token, finish at the end of the
15851 suffix identifier. */
15852 location_t combined_loc
15853 = make_location (start_loc
, start_loc
, parser
->lexer
);
15854 return cp_expr (id
, combined_loc
);
15858 /* Anything else is an error. */
15862 /* If we have selected an identifier, we need to consume the
15864 if (op
!= ERROR_MARK
)
15866 id
= ovl_op_identifier (assop
, op
);
15868 cp_lexer_consume_token (parser
->lexer
);
15870 /* Otherwise, no valid operator name was present. */
15873 cp_parser_error (parser
, "expected operator");
15874 id
= error_mark_node
;
15877 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15878 return cp_expr (id
, start_loc
);
15881 /* Parse a template-declaration.
15883 template-declaration:
15884 export [opt] template < template-parameter-list > declaration
15886 If MEMBER_P is TRUE, this template-declaration occurs within a
15889 The grammar rule given by the standard isn't correct. What
15890 is really meant is:
15892 template-declaration:
15893 export [opt] template-parameter-list-seq
15894 decl-specifier-seq [opt] init-declarator [opt] ;
15895 export [opt] template-parameter-list-seq
15896 function-definition
15898 template-parameter-list-seq:
15899 template-parameter-list-seq [opt]
15900 template < template-parameter-list >
15902 Concept Extensions:
15904 template-parameter-list-seq:
15905 template < template-parameter-list > requires-clause [opt]
15908 requires logical-or-expression */
15911 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15913 /* Check for `export'. */
15914 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15916 /* Consume the `export' token. */
15917 cp_lexer_consume_token (parser
->lexer
);
15918 /* Warn that we do not support `export'. */
15919 warning (0, "keyword %<export%> not implemented, and will be ignored");
15922 cp_parser_template_declaration_after_export (parser
, member_p
);
15925 /* Parse a template-parameter-list.
15927 template-parameter-list:
15929 template-parameter-list , template-parameter
15931 Returns a TREE_LIST. Each node represents a template parameter.
15932 The nodes are connected via their TREE_CHAINs. */
15935 cp_parser_template_parameter_list (cp_parser
* parser
)
15937 tree parameter_list
= NULL_TREE
;
15939 /* Don't create wrapper nodes within a template-parameter-list,
15940 since we don't want to have different types based on the
15941 spelling location of constants and decls within them. */
15942 auto_suppress_location_wrappers sentinel
;
15944 begin_template_parm_list ();
15946 /* The loop below parses the template parms. We first need to know
15947 the total number of template parms to be able to compute proper
15948 canonical types of each dependent type. So after the loop, when
15949 we know the total number of template parms,
15950 end_template_parm_list computes the proper canonical types and
15951 fixes up the dependent types accordingly. */
15956 bool is_parameter_pack
;
15957 location_t parm_loc
;
15959 /* Parse the template-parameter. */
15960 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15961 parameter
= cp_parser_template_parameter (parser
,
15963 &is_parameter_pack
);
15964 /* Add it to the list. */
15965 if (parameter
!= error_mark_node
)
15966 parameter_list
= process_template_parm (parameter_list
,
15970 is_parameter_pack
);
15973 tree err_parm
= build_tree_list (parameter
, parameter
);
15974 parameter_list
= chainon (parameter_list
, err_parm
);
15977 /* If the next token is not a `,', we're done. */
15978 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15980 /* Otherwise, consume the `,' token. */
15981 cp_lexer_consume_token (parser
->lexer
);
15984 return end_template_parm_list (parameter_list
);
15987 /* Parse a introduction-list.
15990 introduced-parameter
15991 introduction-list , introduced-parameter
15993 introduced-parameter:
15994 ...[opt] identifier
15996 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15997 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15998 WILDCARD_DECL will also have DECL_NAME set and token location in
15999 DECL_SOURCE_LOCATION. */
16002 cp_parser_introduction_list (cp_parser
*parser
)
16004 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
16008 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
16010 cp_lexer_consume_token (parser
->lexer
);
16012 tree identifier
= cp_parser_identifier (parser
);
16013 if (identifier
== error_mark_node
)
16016 /* Build placeholder. */
16017 tree parm
= build_nt (WILDCARD_DECL
);
16018 DECL_SOURCE_LOCATION (parm
)
16019 = cp_lexer_peek_token (parser
->lexer
)->location
;
16020 DECL_NAME (parm
) = identifier
;
16021 WILDCARD_PACK_P (parm
) = is_pack
;
16022 vec_safe_push (introduction_vec
, parm
);
16024 /* If the next token is not a `,', we're done. */
16025 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16027 /* Otherwise, consume the `,' token. */
16028 cp_lexer_consume_token (parser
->lexer
);
16031 /* Convert the vec into a TREE_VEC. */
16032 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
16035 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
16036 TREE_VEC_ELT (introduction_list
, n
) = parm
;
16038 release_tree_vector (introduction_vec
);
16039 return introduction_list
;
16042 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16043 is an abstract declarator. */
16045 static inline cp_declarator
*
16046 get_id_declarator (cp_declarator
*declarator
)
16048 cp_declarator
*d
= declarator
;
16049 while (d
&& d
->kind
!= cdk_id
)
16054 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16055 is an abstract declarator. */
16058 get_unqualified_id (cp_declarator
*declarator
)
16060 declarator
= get_id_declarator (declarator
);
16062 return declarator
->u
.id
.unqualified_name
;
16067 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16070 is_constrained_parameter (tree type
)
16073 && TREE_CODE (type
) == TYPE_DECL
16074 && CONSTRAINED_PARM_CONCEPT (type
)
16075 && DECL_P (CONSTRAINED_PARM_CONCEPT (type
)));
16078 /* Returns true if PARM declares a constrained-parameter. */
16081 is_constrained_parameter (cp_parameter_declarator
*parm
)
16083 return is_constrained_parameter (parm
->decl_specifiers
.type
);
16086 /* Check that the type parameter is only a declarator-id, and that its
16087 type is not cv-qualified. */
16090 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
16091 cp_parameter_declarator
*parm
)
16093 if (!parm
->declarator
)
16096 if (parm
->declarator
->kind
!= cdk_id
)
16098 cp_parser_error (parser
, "invalid constrained type parameter");
16102 /* Don't allow cv-qualified type parameters. */
16103 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
16104 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
16106 cp_parser_error (parser
, "cv-qualified type parameter");
16113 /* Finish parsing/processing a template type parameter and checking
16114 various restrictions. */
16117 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
16119 cp_parameter_declarator
* parmdecl
)
16121 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
16122 return finish_template_type_parm (class_type_node
, id
);
16124 return error_mark_node
;
16128 finish_constrained_template_template_parm (tree proto
, tree id
)
16130 /* FIXME: This should probably be copied, and we may need to adjust
16131 the template parameter depths. */
16132 tree saved_parms
= current_template_parms
;
16133 begin_template_parm_list ();
16134 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
16135 end_template_parm_list ();
16137 tree parm
= finish_template_template_parm (class_type_node
, id
);
16138 current_template_parms
= saved_parms
;
16143 /* Finish parsing/processing a template template parameter by borrowing
16144 the template parameter list from the prototype parameter. */
16147 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
16150 cp_parameter_declarator
*parmdecl
)
16152 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
16153 return error_mark_node
;
16154 return finish_constrained_template_template_parm (proto
, id
);
16157 /* Create a new non-type template parameter from the given PARM
16161 cp_parser_constrained_non_type_template_parm (bool *is_non_type
,
16162 cp_parameter_declarator
*parm
)
16164 *is_non_type
= true;
16165 cp_declarator
*decl
= parm
->declarator
;
16166 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
16167 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
16168 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
16171 /* Build a constrained template parameter based on the PARMDECL
16172 declarator. The type of PARMDECL is the constrained type, which
16173 refers to the prototype template parameter that ultimately
16174 specifies the type of the declared parameter. */
16177 finish_constrained_parameter (cp_parser
*parser
,
16178 cp_parameter_declarator
*parmdecl
,
16181 tree decl
= parmdecl
->decl_specifiers
.type
;
16182 tree id
= get_unqualified_id (parmdecl
->declarator
);
16183 tree def
= parmdecl
->default_argument
;
16184 tree proto
= DECL_INITIAL (decl
);
16186 /* Build the parameter. Return an error if the declarator was invalid. */
16188 if (TREE_CODE (proto
) == TYPE_DECL
)
16189 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
16190 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
16191 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
16194 parm
= cp_parser_constrained_non_type_template_parm (is_non_type
, parmdecl
);
16195 if (parm
== error_mark_node
)
16196 return error_mark_node
;
16198 /* Finish the parameter decl and create a node attaching the
16199 default argument and constraint. */
16200 parm
= build_tree_list (def
, parm
);
16201 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
16206 /* Returns true if the parsed type actually represents the declaration
16207 of a type template-parameter. */
16210 declares_constrained_type_template_parameter (tree type
)
16212 return (is_constrained_parameter (type
)
16213 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
16216 /* Returns true if the parsed type actually represents the declaration of
16217 a template template-parameter. */
16220 declares_constrained_template_template_parameter (tree type
)
16222 return (is_constrained_parameter (type
)
16223 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
16226 /* Parse a default argument for a type template-parameter.
16227 Note that diagnostics are handled in cp_parser_template_parameter. */
16230 cp_parser_default_type_template_argument (cp_parser
*parser
)
16232 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16234 /* Consume the `=' token. */
16235 cp_lexer_consume_token (parser
->lexer
);
16237 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16239 /* Parse the default-argument. */
16240 push_deferring_access_checks (dk_no_deferred
);
16241 tree default_argument
= cp_parser_type_id (parser
,
16242 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16244 pop_deferring_access_checks ();
16246 if (flag_concepts
&& type_uses_auto (default_argument
))
16248 error_at (token
->location
,
16249 "invalid use of %<auto%> in default template argument");
16250 return error_mark_node
;
16253 return default_argument
;
16256 /* Parse a default argument for a template template-parameter. */
16259 cp_parser_default_template_template_argument (cp_parser
*parser
)
16261 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16265 /* Consume the `='. */
16266 cp_lexer_consume_token (parser
->lexer
);
16267 /* Parse the id-expression. */
16268 push_deferring_access_checks (dk_no_deferred
);
16269 /* save token before parsing the id-expression, for error
16271 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
16272 tree default_argument
16273 = cp_parser_id_expression (parser
,
16274 /*template_keyword_p=*/false,
16275 /*check_dependency_p=*/true,
16276 /*template_p=*/&is_template
,
16277 /*declarator_p=*/false,
16278 /*optional_p=*/false);
16279 if (TREE_CODE (default_argument
) == TYPE_DECL
)
16280 /* If the id-expression was a template-id that refers to
16281 a template-class, we already have the declaration here,
16282 so no further lookup is needed. */
16285 /* Look up the name. */
16287 = cp_parser_lookup_name (parser
, default_argument
,
16289 /*is_template=*/is_template
,
16290 /*is_namespace=*/false,
16291 /*check_dependency=*/true,
16292 /*ambiguous_decls=*/NULL
,
16294 /* See if the default argument is valid. */
16295 default_argument
= check_template_template_default_arg (default_argument
);
16296 pop_deferring_access_checks ();
16297 return default_argument
;
16300 /* Parse a template-parameter.
16302 template-parameter:
16304 parameter-declaration
16306 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16307 the parameter. The TREE_PURPOSE is the default value, if any.
16308 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16309 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16310 set to true iff this parameter is a parameter pack. */
16313 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
16314 bool *is_parameter_pack
)
16317 cp_parameter_declarator
*parameter_declarator
;
16320 /* Assume it is a type parameter or a template parameter. */
16321 *is_non_type
= false;
16322 /* Assume it not a parameter pack. */
16323 *is_parameter_pack
= false;
16324 /* Peek at the next token. */
16325 token
= cp_lexer_peek_token (parser
->lexer
);
16326 /* If it is `template', we have a type-parameter. */
16327 if (token
->keyword
== RID_TEMPLATE
)
16328 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16329 /* If it is `class' or `typename' we do not know yet whether it is a
16330 type parameter or a non-type parameter. Consider:
16332 template <typename T, typename T::X X> ...
16336 template <class C, class D*> ...
16338 Here, the first parameter is a type parameter, and the second is
16339 a non-type parameter. We can tell by looking at the token after
16340 the identifier -- if it is a `,', `=', or `>' then we have a type
16342 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16344 /* Peek at the token after `class' or `typename'. */
16345 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16346 /* If it's an ellipsis, we have a template type parameter
16348 if (token
->type
== CPP_ELLIPSIS
)
16349 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16350 /* If it's an identifier, skip it. */
16351 if (token
->type
== CPP_NAME
)
16352 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16353 /* Now, see if the token looks like the end of a template
16355 if (token
->type
== CPP_COMMA
16356 || token
->type
== CPP_EQ
16357 || token
->type
== CPP_GREATER
)
16358 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16361 /* Otherwise, it is a non-type parameter or a constrained parameter.
16365 When parsing a default template-argument for a non-type
16366 template-parameter, the first non-nested `>' is taken as the end
16367 of the template parameter-list rather than a greater-than
16369 parameter_declarator
16370 = cp_parser_parameter_declaration (parser
,
16371 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16372 /*template_parm_p=*/true,
16373 /*parenthesized_p=*/NULL
);
16375 if (!parameter_declarator
)
16376 return error_mark_node
;
16378 /* If the parameter declaration is marked as a parameter pack, set
16379 *IS_PARAMETER_PACK to notify the caller. */
16380 if (parameter_declarator
->template_parameter_pack_p
)
16381 *is_parameter_pack
= true;
16383 if (parameter_declarator
->default_argument
)
16385 /* Can happen in some cases of erroneous input (c++/34892). */
16386 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16387 /* Consume the `...' for better error recovery. */
16388 cp_lexer_consume_token (parser
->lexer
);
16391 /* The parameter may have been constrained type parameter. */
16392 if (is_constrained_parameter (parameter_declarator
))
16393 return finish_constrained_parameter (parser
,
16394 parameter_declarator
,
16397 // Now we're sure that the parameter is a non-type parameter.
16398 *is_non_type
= true;
16400 parm
= grokdeclarator (parameter_declarator
->declarator
,
16401 ¶meter_declarator
->decl_specifiers
,
16402 TPARM
, /*initialized=*/0,
16403 /*attrlist=*/NULL
);
16404 if (parm
== error_mark_node
)
16405 return error_mark_node
;
16407 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16410 /* Parse a type-parameter.
16413 class identifier [opt]
16414 class identifier [opt] = type-id
16415 typename identifier [opt]
16416 typename identifier [opt] = type-id
16417 template < template-parameter-list > class identifier [opt]
16418 template < template-parameter-list > class identifier [opt]
16421 GNU Extension (variadic templates):
16424 class ... identifier [opt]
16425 typename ... identifier [opt]
16427 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16428 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16429 the declaration of the parameter.
16431 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16434 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16439 /* Look for a keyword to tell us what kind of parameter this is. */
16440 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16442 return error_mark_node
;
16444 switch (token
->keyword
)
16450 tree default_argument
;
16452 /* If the next token is an ellipsis, we have a template
16454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16456 /* Consume the `...' token. */
16457 cp_lexer_consume_token (parser
->lexer
);
16458 maybe_warn_variadic_templates ();
16460 *is_parameter_pack
= true;
16463 /* If the next token is an identifier, then it names the
16465 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16466 identifier
= cp_parser_identifier (parser
);
16468 identifier
= NULL_TREE
;
16470 /* Create the parameter. */
16471 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16473 /* If the next token is an `=', we have a default argument. */
16474 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16477 = cp_parser_default_type_template_argument (parser
);
16479 /* Template parameter packs cannot have default
16481 if (*is_parameter_pack
)
16484 error_at (token
->location
,
16485 "template parameter pack %qD cannot have a "
16486 "default argument", identifier
);
16488 error_at (token
->location
,
16489 "template parameter packs cannot have "
16490 "default arguments");
16491 default_argument
= NULL_TREE
;
16493 else if (check_for_bare_parameter_packs (default_argument
))
16494 default_argument
= error_mark_node
;
16497 default_argument
= NULL_TREE
;
16499 /* Create the combined representation of the parameter and the
16500 default argument. */
16501 parameter
= build_tree_list (default_argument
, parameter
);
16508 tree default_argument
;
16510 /* Look for the `<'. */
16511 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16512 /* Parse the template-parameter-list. */
16513 cp_parser_template_parameter_list (parser
);
16514 /* Look for the `>'. */
16515 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16517 /* If template requirements are present, parse them. */
16520 tree reqs
= get_shorthand_constraints (current_template_parms
);
16521 if (tree dreqs
= cp_parser_requires_clause_opt (parser
, false))
16522 reqs
= combine_constraint_expressions (reqs
, dreqs
);
16523 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16526 /* Look for the `class' or 'typename' keywords. */
16527 cp_parser_type_parameter_key (parser
);
16528 /* If the next token is an ellipsis, we have a template
16530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16532 /* Consume the `...' token. */
16533 cp_lexer_consume_token (parser
->lexer
);
16534 maybe_warn_variadic_templates ();
16536 *is_parameter_pack
= true;
16538 /* If the next token is an `=', then there is a
16539 default-argument. If the next token is a `>', we are at
16540 the end of the parameter-list. If the next token is a `,',
16541 then we are at the end of this parameter. */
16542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16543 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16544 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16546 identifier
= cp_parser_identifier (parser
);
16547 /* Treat invalid names as if the parameter were nameless. */
16548 if (identifier
== error_mark_node
)
16549 identifier
= NULL_TREE
;
16552 identifier
= NULL_TREE
;
16554 /* Create the template parameter. */
16555 parameter
= finish_template_template_parm (class_type_node
,
16558 /* If the next token is an `=', then there is a
16559 default-argument. */
16560 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16563 = cp_parser_default_template_template_argument (parser
);
16565 /* Template parameter packs cannot have default
16567 if (*is_parameter_pack
)
16570 error_at (token
->location
,
16571 "template parameter pack %qD cannot "
16572 "have a default argument",
16575 error_at (token
->location
, "template parameter packs cannot "
16576 "have default arguments");
16577 default_argument
= NULL_TREE
;
16581 default_argument
= NULL_TREE
;
16583 /* Create the combined representation of the parameter and the
16584 default argument. */
16585 parameter
= build_tree_list (default_argument
, parameter
);
16590 gcc_unreachable ();
16597 /* Parse a template-id.
16600 template-name < template-argument-list [opt] >
16602 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16603 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16604 returned. Otherwise, if the template-name names a function, or set
16605 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16606 names a class, returns a TYPE_DECL for the specialization.
16608 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16609 uninstantiated templates. */
16612 cp_parser_template_id (cp_parser
*parser
,
16613 bool template_keyword_p
,
16614 bool check_dependency_p
,
16615 enum tag_types tag_type
,
16616 bool is_declaration
)
16621 cp_token_position start_of_id
= 0;
16622 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16623 bool is_identifier
;
16625 /* If the next token corresponds to a template-id, there is no need
16627 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16629 if (token
->type
== CPP_TEMPLATE_ID
)
16631 cp_lexer_consume_token (parser
->lexer
);
16632 return saved_checks_value (token
->u
.tree_check_value
);
16635 /* Avoid performing name lookup if there is no possibility of
16636 finding a template-id. */
16637 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16638 || (token
->type
== CPP_NAME
16639 && !cp_parser_nth_token_starts_template_argument_list_p
16642 cp_parser_error (parser
, "expected template-id");
16643 return error_mark_node
;
16646 /* Remember where the template-id starts. */
16647 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16648 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16650 push_deferring_access_checks (dk_deferred
);
16652 /* Parse the template-name. */
16653 is_identifier
= false;
16654 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16655 check_dependency_p
,
16660 /* Push any access checks inside the firewall we're about to create. */
16661 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16662 pop_deferring_access_checks ();
16663 if (templ
== error_mark_node
|| is_identifier
)
16666 /* Since we're going to preserve any side-effects from this parse, set up a
16667 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16668 in the template arguments. */
16669 tentative_firewall
firewall (parser
);
16670 reopen_deferring_access_checks (checks
);
16672 /* If we find the sequence `[:' after a template-name, it's probably
16673 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16674 parse correctly the argument list. */
16675 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16676 == CPP_OPEN_SQUARE
)
16677 && next_token
->flags
& DIGRAPH
16678 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16680 && !(next_token_2
->flags
& PREV_WHITE
))
16682 cp_parser_parse_tentatively (parser
);
16683 /* Change `:' into `::'. */
16684 next_token_2
->type
= CPP_SCOPE
;
16685 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16687 cp_lexer_consume_token (parser
->lexer
);
16689 /* Parse the arguments. */
16690 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16691 if (!cp_parser_parse_definitely (parser
))
16693 /* If we couldn't parse an argument list, then we revert our changes
16694 and return simply an error. Maybe this is not a template-id
16696 next_token_2
->type
= CPP_COLON
;
16697 cp_parser_error (parser
, "expected %<<%>");
16698 pop_deferring_access_checks ();
16699 return error_mark_node
;
16701 /* Otherwise, emit an error about the invalid digraph, but continue
16702 parsing because we got our argument list. */
16703 if (permerror (next_token
->location
,
16704 "%<<::%> cannot begin a template-argument list"))
16706 static bool hint
= false;
16707 inform (next_token
->location
,
16708 "%<<:%> is an alternate spelling for %<[%>."
16709 " Insert whitespace between %<<%> and %<::%>");
16710 if (!hint
&& !flag_permissive
)
16712 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16713 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16714 "accept your code)");
16721 /* Look for the `<' that starts the template-argument-list. */
16722 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16724 pop_deferring_access_checks ();
16725 return error_mark_node
;
16727 /* Parse the arguments. */
16728 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16730 if ((cxx_dialect
> cxx17
)
16731 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16732 && !template_keyword_p
16733 && (cp_parser_error_occurred (parser
)
16734 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16736 /* This didn't go well. */
16737 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16739 /* C++20 says that "function-name < a;" is now ill-formed. */
16740 if (cp_parser_error_occurred (parser
))
16742 error_at (token
->location
, "invalid template-argument-list");
16743 inform (token
->location
, "function name as the left hand "
16744 "operand of %<<%> is ill-formed in C++20; wrap the "
16745 "function name in %<()%>");
16748 /* We expect "f<targs>" to be followed by "(args)". */
16749 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16750 "expected %<(%> after template-argument-list");
16752 /* Purge all subsequent tokens. */
16753 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16756 cp_parser_simulate_error (parser
);
16757 pop_deferring_access_checks ();
16758 return error_mark_node
;
16762 /* Set the location to be of the form:
16763 template-name < template-argument-list [opt] >
16764 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16765 with caret == start at the start of the template-name,
16766 ranging until the closing '>'. */
16767 location_t combined_loc
16768 = make_location (token
->location
, token
->location
, parser
->lexer
);
16770 /* Check for concepts autos where they don't belong. We could
16771 identify types in some cases of identifier TEMPL, looking ahead
16772 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16773 types. We reject them in functions, but if what we have is an
16774 identifier, even with none_type we can't conclude it's NOT a
16775 type, we have to wait for template substitution. */
16776 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16777 template_id
= error_mark_node
;
16778 /* Build a representation of the specialization. */
16779 else if (identifier_p (templ
))
16780 template_id
= build_min_nt_loc (combined_loc
,
16783 else if (DECL_TYPE_TEMPLATE_P (templ
)
16784 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16786 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16787 template (rather than some instantiation thereof) only if
16788 is not nested within some other construct. For example, in
16789 "template <typename T> void f(T) { A<T>::", A<T> is just an
16790 instantiation of A. */
16791 bool entering_scope
16792 = (template_parm_scope_p ()
16793 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16795 = finish_template_type (templ
, arguments
, entering_scope
);
16797 else if (concept_definition_p (templ
))
16799 /* The caller will decide whether this is a concept check or type
16801 template_id
= build2_loc (combined_loc
, TEMPLATE_ID_EXPR
,
16802 boolean_type_node
, templ
, arguments
);
16804 else if (variable_template_p (templ
))
16806 template_id
= lookup_template_variable (templ
, arguments
);
16807 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16808 SET_EXPR_LOCATION (template_id
, combined_loc
);
16812 /* If it's not a class-template or a template-template, it should be
16813 a function-template. */
16814 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
16816 template_id
= lookup_template_function (templ
, arguments
);
16817 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16818 SET_EXPR_LOCATION (template_id
, combined_loc
);
16821 /* If parsing tentatively, replace the sequence of tokens that makes
16822 up the template-id with a CPP_TEMPLATE_ID token. That way,
16823 should we re-parse the token stream, we will not have to repeat
16824 the effort required to do the parse, nor will we issue duplicate
16825 error messages about problems during instantiation of the
16828 /* Don't do this if we had a parse error in a declarator; re-parsing
16829 might succeed if a name changes meaning (60361). */
16830 && !(cp_parser_error_occurred (parser
)
16831 && cp_parser_parsing_tentatively (parser
)
16832 && parser
->in_declarator_p
))
16834 /* Reset the contents of the START_OF_ID token. */
16835 token
->type
= CPP_TEMPLATE_ID
;
16836 token
->location
= combined_loc
;
16838 /* Retrieve any deferred checks. Do not pop this access checks yet
16839 so the memory will not be reclaimed during token replacing below. */
16840 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16841 token
->tree_check_p
= true;
16842 token
->u
.tree_check_value
->value
= template_id
;
16843 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16844 token
->keyword
= RID_MAX
;
16846 /* Purge all subsequent tokens. */
16847 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16849 /* ??? Can we actually assume that, if template_id ==
16850 error_mark_node, we will have issued a diagnostic to the
16851 user, as opposed to simply marking the tentative parse as
16853 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16854 error_at (token
->location
, "parse error in template argument list");
16857 pop_to_parent_deferring_access_checks ();
16858 return template_id
;
16861 /* Like cp_parser_template_id, called in non-type context. */
16864 cp_parser_template_id_expr (cp_parser
*parser
,
16865 bool template_keyword_p
,
16866 bool check_dependency_p
,
16867 bool is_declaration
)
16869 tree x
= cp_parser_template_id (parser
, template_keyword_p
, check_dependency_p
,
16870 none_type
, is_declaration
);
16871 if (TREE_CODE (x
) == TEMPLATE_ID_EXPR
16872 && concept_check_p (x
))
16873 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16874 return build_concept_id (x
);
16878 /* Parse a template-name.
16883 The standard should actually say:
16887 operator-function-id
16889 A defect report has been filed about this issue.
16891 A conversion-function-id cannot be a template name because they cannot
16892 be part of a template-id. In fact, looking at this code:
16894 a.operator K<int>()
16896 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16897 It is impossible to call a templated conversion-function-id with an
16898 explicit argument list, since the only allowed template parameter is
16899 the type to which it is converting.
16901 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16902 `template' keyword, in a construction like:
16906 In that case `f' is taken to be a template-name, even though there
16907 is no way of knowing for sure.
16909 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16910 name refers to a set of overloaded functions, at least one of which
16911 is a template, or an IDENTIFIER_NODE with the name of the template,
16912 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16913 names are looked up inside uninstantiated templates. */
16916 cp_parser_template_name (cp_parser
* parser
,
16917 bool template_keyword_p
,
16918 bool check_dependency_p
,
16919 bool is_declaration
,
16920 enum tag_types tag_type
,
16921 bool *is_identifier
)
16925 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16927 /* If the next token is `operator', then we have either an
16928 operator-function-id or a conversion-function-id. */
16929 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16931 /* We don't know whether we're looking at an
16932 operator-function-id or a conversion-function-id. */
16933 cp_parser_parse_tentatively (parser
);
16934 /* Try an operator-function-id. */
16935 identifier
= cp_parser_operator_function_id (parser
);
16936 /* If that didn't work, try a conversion-function-id. */
16937 if (!cp_parser_parse_definitely (parser
))
16939 cp_parser_error (parser
, "expected template-name");
16940 return error_mark_node
;
16943 /* Look for the identifier. */
16945 identifier
= cp_parser_identifier (parser
);
16947 /* If we didn't find an identifier, we don't have a template-id. */
16948 if (identifier
== error_mark_node
)
16949 return error_mark_node
;
16951 /* If the name immediately followed the `template' keyword, then it
16952 is a template-name. However, if the next token is not `<', then
16953 we do not treat it as a template-name, since it is not being used
16954 as part of a template-id. This enables us to handle constructs
16957 template <typename T> struct S { S(); };
16958 template <typename T> S<T>::S();
16960 correctly. We would treat `S' as a template -- if it were `S<T>'
16961 -- but we do not if there is no `<'. */
16963 if (processing_template_decl
16964 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16966 /* In a declaration, in a dependent context, we pretend that the
16967 "template" keyword was present in order to improve error
16968 recovery. For example, given:
16970 template <typename T> void f(T::X<int>);
16972 we want to treat "X<int>" as a template-id. */
16974 && !template_keyword_p
16975 && parser
->scope
&& TYPE_P (parser
->scope
)
16976 && check_dependency_p
16977 && dependent_scope_p (parser
->scope
)
16978 /* Do not do this for dtors (or ctors), since they never
16979 need the template keyword before their name. */
16980 && !constructor_name_p (identifier
, parser
->scope
))
16982 cp_token_position start
= 0;
16984 /* Explain what went wrong. */
16985 error_at (token
->location
, "non-template %qD used as template",
16987 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16988 parser
->scope
, identifier
);
16989 /* If parsing tentatively, find the location of the "<" token. */
16990 if (cp_parser_simulate_error (parser
))
16991 start
= cp_lexer_token_position (parser
->lexer
, true);
16992 /* Parse the template arguments so that we can issue error
16993 messages about them. */
16994 cp_lexer_consume_token (parser
->lexer
);
16995 cp_parser_enclosed_template_argument_list (parser
);
16996 /* Skip tokens until we find a good place from which to
16997 continue parsing. */
16998 cp_parser_skip_to_closing_parenthesis (parser
,
16999 /*recovering=*/true,
17001 /*consume_paren=*/false);
17002 /* If parsing tentatively, permanently remove the
17003 template argument list. That will prevent duplicate
17004 error messages from being issued about the missing
17005 "template" keyword. */
17007 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
17009 *is_identifier
= true;
17010 parser
->context
->object_type
= NULL_TREE
;
17014 /* If the "template" keyword is present, then there is generally
17015 no point in doing name-lookup, so we just return IDENTIFIER.
17016 But, if the qualifying scope is non-dependent then we can
17017 (and must) do name-lookup normally. */
17018 if (template_keyword_p
)
17020 tree scope
= (parser
->scope
? parser
->scope
17021 : parser
->context
->object_type
);
17022 if (scope
&& TYPE_P (scope
)
17023 && (!CLASS_TYPE_P (scope
)
17024 || (check_dependency_p
&& dependent_type_p (scope
))))
17026 /* We're optimizing away the call to cp_parser_lookup_name, but
17027 we still need to do this. */
17028 parser
->context
->object_type
= NULL_TREE
;
17034 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17035 const bool scoped_p
= ((parser
->scope
? parser
->scope
17036 : parser
->context
->object_type
) != NULL_TREE
);
17038 /* Look up the name. */
17039 decl
= cp_parser_lookup_name (parser
, identifier
,
17041 /*is_template=*/true,
17042 /*is_namespace=*/false,
17043 check_dependency_p
,
17044 /*ambiguous_decls=*/NULL
,
17047 decl
= strip_using_decl (decl
);
17049 /* If DECL is a template, then the name was a template-name. */
17050 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
17052 if (TREE_DEPRECATED (decl
)
17053 && deprecated_state
!= DEPRECATED_SUPPRESS
)
17055 tree d
= DECL_TEMPLATE_RESULT (decl
);
17057 if (TREE_CODE (d
) == TYPE_DECL
)
17058 attr
= lookup_attribute ("deprecated",
17059 TYPE_ATTRIBUTES (TREE_TYPE (d
)));
17061 attr
= lookup_attribute ("deprecated",
17062 DECL_ATTRIBUTES (d
));
17063 warn_deprecated_use (decl
, attr
);
17068 /* The standard does not explicitly indicate whether a name that
17069 names a set of overloaded declarations, some of which are
17070 templates, is a template-name. However, such a name should
17071 be a template-name; otherwise, there is no way to form a
17072 template-id for the overloaded templates. */
17073 bool found
= false;
17075 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
17076 !found
&& iter
; ++iter
)
17077 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
17081 && (cxx_dialect
> cxx17
)
17083 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
17084 && tag_type
== none_type
)
17086 /* [temp.names] says "A name is also considered to refer to a template
17087 if it is an unqualified-id followed by a < and name lookup finds
17088 either one or more functions or finds nothing." */
17090 /* The "more functions" case. Just use the OVERLOAD as normally.
17091 We don't use is_overloaded_fn here to avoid considering
17093 if (TREE_CODE (decl
) == OVERLOAD
17094 /* Name lookup found one function. */
17095 || TREE_CODE (decl
) == FUNCTION_DECL
)
17097 /* Name lookup found nothing. */
17098 else if (decl
== error_mark_node
)
17104 /* The name does not name a template. */
17105 cp_parser_error (parser
, "expected template-name");
17106 return error_mark_node
;
17113 /* Parse a template-argument-list.
17115 template-argument-list:
17116 template-argument ... [opt]
17117 template-argument-list , template-argument ... [opt]
17119 Returns a TREE_VEC containing the arguments. */
17122 cp_parser_template_argument_list (cp_parser
* parser
)
17124 tree fixed_args
[10];
17125 unsigned n_args
= 0;
17126 unsigned alloced
= 10;
17127 tree
*arg_ary
= fixed_args
;
17129 bool saved_in_template_argument_list_p
;
17131 bool saved_non_ice_p
;
17133 /* Don't create location wrapper nodes within a template-argument-list. */
17134 auto_suppress_location_wrappers sentinel
;
17136 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
17137 parser
->in_template_argument_list_p
= true;
17138 /* Even if the template-id appears in an integral
17139 constant-expression, the contents of the argument list do
17141 saved_ice_p
= parser
->integral_constant_expression_p
;
17142 parser
->integral_constant_expression_p
= false;
17143 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
17144 parser
->non_integral_constant_expression_p
= false;
17146 /* Parse the arguments. */
17152 /* Consume the comma. */
17153 cp_lexer_consume_token (parser
->lexer
);
17155 /* Parse the template-argument. */
17156 argument
= cp_parser_template_argument (parser
);
17158 /* If the next token is an ellipsis, we're expanding a template
17160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17162 if (argument
== error_mark_node
)
17164 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17165 error_at (token
->location
,
17166 "expected parameter pack before %<...%>");
17168 /* Consume the `...' token. */
17169 cp_lexer_consume_token (parser
->lexer
);
17171 /* Make the argument into a TYPE_PACK_EXPANSION or
17172 EXPR_PACK_EXPANSION. */
17173 argument
= make_pack_expansion (argument
);
17176 if (n_args
== alloced
)
17180 if (arg_ary
== fixed_args
)
17182 arg_ary
= XNEWVEC (tree
, alloced
);
17183 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
17186 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
17188 arg_ary
[n_args
++] = argument
;
17190 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
17192 vec
= make_tree_vec (n_args
);
17195 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
17197 if (arg_ary
!= fixed_args
)
17199 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
17200 parser
->integral_constant_expression_p
= saved_ice_p
;
17201 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
17203 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
17207 /* Parse a template-argument.
17210 assignment-expression
17214 The representation is that of an assignment-expression, type-id, or
17215 id-expression -- except that the qualified id-expression is
17216 evaluated, so that the value returned is either a DECL or an
17219 Although the standard says "assignment-expression", it forbids
17220 throw-expressions or assignments in the template argument.
17221 Therefore, we use "conditional-expression" instead. */
17224 cp_parser_template_argument (cp_parser
* parser
)
17229 bool maybe_type_id
= false;
17230 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
17231 location_t loc
= 0;
17234 /* There's really no way to know what we're looking at, so we just
17235 try each alternative in order.
17239 In a template-argument, an ambiguity between a type-id and an
17240 expression is resolved to a type-id, regardless of the form of
17241 the corresponding template-parameter.
17243 Therefore, we try a type-id first. */
17244 cp_parser_parse_tentatively (parser
);
17245 argument
= cp_parser_template_type_arg (parser
);
17246 /* If there was no error parsing the type-id but the next token is a
17247 '>>', our behavior depends on which dialect of C++ we're
17248 parsing. In C++98, we probably found a typo for '> >'. But there
17249 are type-id which are also valid expressions. For instance:
17251 struct X { int operator >> (int); };
17252 template <int V> struct Foo {};
17255 Here 'X()' is a valid type-id of a function type, but the user just
17256 wanted to write the expression "X() >> 5". Thus, we remember that we
17257 found a valid type-id, but we still try to parse the argument as an
17258 expression to see what happens.
17260 In C++0x, the '>>' will be considered two separate '>'
17262 if (!cp_parser_error_occurred (parser
)
17263 && cxx_dialect
== cxx98
17264 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17266 maybe_type_id
= true;
17267 cp_parser_abort_tentative_parse (parser
);
17271 /* If the next token isn't a `,' or a `>', then this argument wasn't
17272 really finished. This means that the argument is not a valid
17274 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17275 cp_parser_error (parser
, "expected template-argument");
17276 /* If that worked, we're done. */
17277 if (cp_parser_parse_definitely (parser
))
17280 /* We're still not sure what the argument will be. */
17281 cp_parser_parse_tentatively (parser
);
17282 /* Try a template. */
17283 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
17284 argument
= cp_parser_id_expression (parser
,
17285 /*template_keyword_p=*/false,
17286 /*check_dependency_p=*/true,
17288 /*declarator_p=*/false,
17289 /*optional_p=*/false);
17290 /* If the next token isn't a `,' or a `>', then this argument wasn't
17291 really finished. */
17292 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17293 cp_parser_error (parser
, "expected template-argument");
17294 if (!cp_parser_error_occurred (parser
))
17296 /* Figure out what is being referred to. If the id-expression
17297 was for a class template specialization, then we will have a
17298 TYPE_DECL at this point. There is no need to do name lookup
17299 at this point in that case. */
17300 if (TREE_CODE (argument
) != TYPE_DECL
)
17301 argument
= cp_parser_lookup_name (parser
, argument
,
17303 /*is_template=*/template_p
,
17304 /*is_namespace=*/false,
17305 /*check_dependency=*/true,
17306 /*ambiguous_decls=*/NULL
,
17307 argument_start_token
->location
);
17308 if (TREE_CODE (argument
) != TEMPLATE_DECL
17309 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
17310 cp_parser_error (parser
, "expected template-name");
17312 if (cp_parser_parse_definitely (parser
))
17314 if (TREE_DEPRECATED (argument
))
17315 warn_deprecated_use (argument
, NULL_TREE
);
17318 /* It must be a non-type argument. In C++17 any constant-expression is
17320 if (cxx_dialect
> cxx14
)
17323 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17325 -- an integral constant-expression of integral or enumeration
17328 -- the name of a non-type template-parameter; or
17330 -- the name of an object or function with external linkage...
17332 -- the address of an object or function with external linkage...
17334 -- a pointer to member... */
17335 /* Look for a non-type template parameter. */
17336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17338 cp_parser_parse_tentatively (parser
);
17339 argument
= cp_parser_primary_expression (parser
,
17340 /*address_p=*/false,
17342 /*template_arg_p=*/true,
17344 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
17345 || !cp_parser_next_token_ends_template_argument_p (parser
))
17346 cp_parser_simulate_error (parser
);
17347 if (cp_parser_parse_definitely (parser
))
17351 /* If the next token is "&", the argument must be the address of an
17352 object or function with external linkage. */
17353 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17356 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17357 cp_lexer_consume_token (parser
->lexer
);
17359 /* See if we might have an id-expression. */
17360 token
= cp_lexer_peek_token (parser
->lexer
);
17361 if (token
->type
== CPP_NAME
17362 || token
->keyword
== RID_OPERATOR
17363 || token
->type
== CPP_SCOPE
17364 || token
->type
== CPP_TEMPLATE_ID
17365 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17367 cp_parser_parse_tentatively (parser
);
17368 argument
= cp_parser_primary_expression (parser
,
17371 /*template_arg_p=*/true,
17373 if (cp_parser_error_occurred (parser
)
17374 || !cp_parser_next_token_ends_template_argument_p (parser
))
17375 cp_parser_abort_tentative_parse (parser
);
17380 if (INDIRECT_REF_P (argument
))
17382 /* Strip the dereference temporarily. */
17383 gcc_assert (REFERENCE_REF_P (argument
));
17384 argument
= TREE_OPERAND (argument
, 0);
17387 /* If we're in a template, we represent a qualified-id referring
17388 to a static data member as a SCOPE_REF even if the scope isn't
17389 dependent so that we can check access control later. */
17391 if (TREE_CODE (probe
) == SCOPE_REF
)
17392 probe
= TREE_OPERAND (probe
, 1);
17395 /* A variable without external linkage might still be a
17396 valid constant-expression, so no error is issued here
17397 if the external-linkage check fails. */
17398 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
17399 cp_parser_simulate_error (parser
);
17401 else if (is_overloaded_fn (argument
))
17402 /* All overloaded functions are allowed; if the external
17403 linkage test does not pass, an error will be issued
17407 && (TREE_CODE (argument
) == OFFSET_REF
17408 || TREE_CODE (argument
) == SCOPE_REF
))
17409 /* A pointer-to-member. */
17411 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17414 cp_parser_simulate_error (parser
);
17416 if (cp_parser_parse_definitely (parser
))
17419 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17420 tf_warning_or_error
);
17422 argument
= convert_from_reference (argument
);
17427 /* If the argument started with "&", there are no other valid
17428 alternatives at this point. */
17431 cp_parser_error (parser
, "invalid non-type template argument");
17432 return error_mark_node
;
17436 /* If the argument wasn't successfully parsed as a type-id followed
17437 by '>>', the argument can only be a constant expression now.
17438 Otherwise, we try parsing the constant-expression tentatively,
17439 because the argument could really be a type-id. */
17441 cp_parser_parse_tentatively (parser
);
17443 if (cxx_dialect
<= cxx14
)
17444 argument
= cp_parser_constant_expression (parser
);
17447 /* In C++20, we can encounter a braced-init-list. */
17448 if (cxx_dialect
>= cxx20
17449 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17451 bool expr_non_constant_p
;
17452 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17455 /* With C++17 generalized non-type template arguments we need to handle
17456 lvalue constant expressions, too. */
17457 argument
= cp_parser_assignment_expression (parser
);
17458 require_potential_constant_expression (argument
);
17461 if (!maybe_type_id
)
17463 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17464 cp_parser_error (parser
, "expected template-argument");
17465 if (cp_parser_parse_definitely (parser
))
17467 /* We did our best to parse the argument as a non type-id, but that
17468 was the only alternative that matched (albeit with a '>' after
17469 it). We can assume it's just a typo from the user, and a
17470 diagnostic will then be issued. */
17471 return cp_parser_template_type_arg (parser
);
17474 /* Parse an explicit-instantiation.
17476 explicit-instantiation:
17477 template declaration
17479 Although the standard says `declaration', what it really means is:
17481 explicit-instantiation:
17482 template decl-specifier-seq [opt] declarator [opt] ;
17484 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17485 supposed to be allowed. A defect report has been filed about this
17490 explicit-instantiation:
17491 storage-class-specifier template
17492 decl-specifier-seq [opt] declarator [opt] ;
17493 function-specifier template
17494 decl-specifier-seq [opt] declarator [opt] ; */
17497 cp_parser_explicit_instantiation (cp_parser
* parser
)
17499 int declares_class_or_enum
;
17500 cp_decl_specifier_seq decl_specifiers
;
17501 tree extension_specifier
= NULL_TREE
;
17503 timevar_push (TV_TEMPLATE_INST
);
17505 /* Look for an (optional) storage-class-specifier or
17506 function-specifier. */
17507 if (cp_parser_allow_gnu_extensions_p (parser
))
17509 extension_specifier
17510 = cp_parser_storage_class_specifier_opt (parser
);
17511 if (!extension_specifier
)
17512 extension_specifier
17513 = cp_parser_function_specifier_opt (parser
,
17514 /*decl_specs=*/NULL
);
17517 /* Look for the `template' keyword. */
17518 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17519 /* Let the front end know that we are processing an explicit
17521 begin_explicit_instantiation ();
17522 /* [temp.explicit] says that we are supposed to ignore access
17523 control while processing explicit instantiation directives. */
17524 push_deferring_access_checks (dk_no_check
);
17525 /* Parse a decl-specifier-seq. */
17526 cp_parser_decl_specifier_seq (parser
,
17527 CP_PARSER_FLAGS_OPTIONAL
,
17529 &declares_class_or_enum
);
17530 /* If there was exactly one decl-specifier, and it declared a class,
17531 and there's no declarator, then we have an explicit type
17533 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17537 type
= check_tag_decl (&decl_specifiers
,
17538 /*explicit_type_instantiation_p=*/true);
17539 /* Turn access control back on for names used during
17540 template instantiation. */
17541 pop_deferring_access_checks ();
17543 do_type_instantiation (type
, extension_specifier
,
17544 /*complain=*/tf_error
);
17548 cp_declarator
*declarator
;
17551 /* Parse the declarator. */
17553 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17554 CP_PARSER_FLAGS_NONE
,
17555 /*ctor_dtor_or_conv_p=*/NULL
,
17556 /*parenthesized_p=*/NULL
,
17557 /*member_p=*/false,
17558 /*friend_p=*/false,
17559 /*static_p=*/false);
17560 if (declares_class_or_enum
& 2)
17561 cp_parser_check_for_definition_in_return_type (declarator
,
17562 decl_specifiers
.type
,
17563 decl_specifiers
.locations
[ds_type_spec
]);
17564 if (declarator
!= cp_error_declarator
)
17566 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17567 permerror (decl_specifiers
.locations
[ds_inline
],
17568 "explicit instantiation shall not use"
17569 " %<inline%> specifier");
17570 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17571 permerror (decl_specifiers
.locations
[ds_constexpr
],
17572 "explicit instantiation shall not use"
17573 " %<constexpr%> specifier");
17574 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_consteval
))
17575 permerror (decl_specifiers
.locations
[ds_consteval
],
17576 "explicit instantiation shall not use"
17577 " %<consteval%> specifier");
17579 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17580 NORMAL
, 0, &decl_specifiers
.attributes
);
17581 /* Turn access control back on for names used during
17582 template instantiation. */
17583 pop_deferring_access_checks ();
17584 /* Do the explicit instantiation. */
17585 do_decl_instantiation (decl
, extension_specifier
);
17589 pop_deferring_access_checks ();
17590 /* Skip the body of the explicit instantiation. */
17591 cp_parser_skip_to_end_of_statement (parser
);
17594 /* We're done with the instantiation. */
17595 end_explicit_instantiation ();
17597 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17599 timevar_pop (TV_TEMPLATE_INST
);
17602 /* Parse an explicit-specialization.
17604 explicit-specialization:
17605 template < > declaration
17607 Although the standard says `declaration', what it really means is:
17609 explicit-specialization:
17610 template <> decl-specifier [opt] init-declarator [opt] ;
17611 template <> function-definition
17612 template <> explicit-specialization
17613 template <> template-declaration */
17616 cp_parser_explicit_specialization (cp_parser
* parser
)
17618 bool need_lang_pop
;
17619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17621 /* Look for the `template' keyword. */
17622 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17623 /* Look for the `<'. */
17624 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17625 /* Look for the `>'. */
17626 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17627 /* We have processed another parameter list. */
17628 ++parser
->num_template_parameter_lists
;
17631 A template ... explicit specialization ... shall not have C
17633 if (current_lang_name
== lang_name_c
)
17635 error_at (token
->location
, "template specialization with C linkage");
17636 maybe_show_extern_c_location ();
17637 /* Give it C++ linkage to avoid confusing other parts of the
17639 push_lang_context (lang_name_cplusplus
);
17640 need_lang_pop
= true;
17643 need_lang_pop
= false;
17644 /* Let the front end know that we are beginning a specialization. */
17645 if (!begin_specialization ())
17647 end_specialization ();
17651 /* If the next keyword is `template', we need to figure out whether
17652 or not we're looking a template-declaration. */
17653 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17655 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17656 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17657 cp_parser_template_declaration_after_export (parser
,
17658 /*member_p=*/false);
17660 cp_parser_explicit_specialization (parser
);
17663 /* Parse the dependent declaration. */
17664 cp_parser_single_declaration (parser
,
17666 /*member_p=*/false,
17667 /*explicit_specialization_p=*/true,
17668 /*friend_p=*/NULL
);
17669 /* We're done with the specialization. */
17670 end_specialization ();
17671 /* For the erroneous case of a template with C linkage, we pushed an
17672 implicit C++ linkage scope; exit that scope now. */
17674 pop_lang_context ();
17675 /* We're done with this parameter list. */
17676 --parser
->num_template_parameter_lists
;
17679 /* Parse a type-specifier.
17682 simple-type-specifier
17685 elaborated-type-specifier
17693 Returns a representation of the type-specifier. For a
17694 class-specifier, enum-specifier, or elaborated-type-specifier, a
17695 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17697 The parser flags FLAGS is used to control type-specifier parsing.
17699 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17700 in a decl-specifier-seq.
17702 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17703 class-specifier, enum-specifier, or elaborated-type-specifier, then
17704 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17705 if a type is declared; 2 if it is defined. Otherwise, it is set to
17708 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17709 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17710 is set to FALSE. */
17713 cp_parser_type_specifier (cp_parser
* parser
,
17714 cp_parser_flags flags
,
17715 cp_decl_specifier_seq
*decl_specs
,
17716 bool is_declaration
,
17717 int* declares_class_or_enum
,
17718 bool* is_cv_qualifier
)
17720 tree type_spec
= NULL_TREE
;
17723 cp_decl_spec ds
= ds_last
;
17725 /* Assume this type-specifier does not declare a new type. */
17726 if (declares_class_or_enum
)
17727 *declares_class_or_enum
= 0;
17728 /* And that it does not specify a cv-qualifier. */
17729 if (is_cv_qualifier
)
17730 *is_cv_qualifier
= false;
17731 /* Peek at the next token. */
17732 token
= cp_lexer_peek_token (parser
->lexer
);
17734 /* If we're looking at a keyword, we can use that to guide the
17735 production we choose. */
17736 keyword
= token
->keyword
;
17740 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17741 goto elaborated_type_specifier
;
17743 /* Look for the enum-specifier. */
17744 type_spec
= cp_parser_enum_specifier (parser
);
17745 /* If that worked, we're done. */
17748 if (declares_class_or_enum
)
17749 *declares_class_or_enum
= 2;
17751 cp_parser_set_decl_spec_type (decl_specs
,
17754 /*type_definition_p=*/true);
17758 goto elaborated_type_specifier
;
17760 /* Any of these indicate either a class-specifier, or an
17761 elaborated-type-specifier. */
17765 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17766 goto elaborated_type_specifier
;
17768 /* Parse tentatively so that we can back up if we don't find a
17769 class-specifier. */
17770 cp_parser_parse_tentatively (parser
);
17771 /* Look for the class-specifier. */
17772 type_spec
= cp_parser_class_specifier (parser
);
17773 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17774 /* If that worked, we're done. */
17775 if (cp_parser_parse_definitely (parser
))
17777 if (declares_class_or_enum
)
17778 *declares_class_or_enum
= 2;
17780 cp_parser_set_decl_spec_type (decl_specs
,
17783 /*type_definition_p=*/true);
17787 /* Fall through. */
17788 elaborated_type_specifier
:
17789 /* We're declaring (not defining) a class or enum. */
17790 if (declares_class_or_enum
)
17791 *declares_class_or_enum
= 1;
17793 /* Fall through. */
17795 /* Look for an elaborated-type-specifier. */
17797 = (cp_parser_elaborated_type_specifier
17799 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17802 cp_parser_set_decl_spec_type (decl_specs
,
17805 /*type_definition_p=*/false);
17810 if (is_cv_qualifier
)
17811 *is_cv_qualifier
= true;
17816 if (is_cv_qualifier
)
17817 *is_cv_qualifier
= true;
17822 if (is_cv_qualifier
)
17823 *is_cv_qualifier
= true;
17827 /* The `__complex__' keyword is a GNU extension. */
17835 /* Handle simple keywords. */
17840 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17841 decl_specs
->any_specifiers_p
= true;
17843 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17846 /* If we do not already have a type-specifier, assume we are looking
17847 at a simple-type-specifier. */
17848 type_spec
= cp_parser_simple_type_specifier (parser
,
17852 /* If we didn't find a type-specifier, and a type-specifier was not
17853 optional in this context, issue an error message. */
17854 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17856 cp_parser_error (parser
, "expected type specifier");
17857 return error_mark_node
;
17863 /* Parse a simple-type-specifier.
17865 simple-type-specifier:
17866 :: [opt] nested-name-specifier [opt] type-name
17867 :: [opt] nested-name-specifier template template-id
17882 simple-type-specifier:
17884 decltype ( expression )
17887 __underlying_type ( type-id )
17891 nested-name-specifier(opt) template-name
17895 simple-type-specifier:
17897 __typeof__ unary-expression
17898 __typeof__ ( type-id )
17899 __typeof__ ( type-id ) { initializer-list , [opt] }
17901 Concepts Extension:
17903 simple-type-specifier:
17904 constrained-type-specifier
17906 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17907 appropriately updated. */
17910 cp_parser_simple_type_specifier (cp_parser
* parser
,
17911 cp_decl_specifier_seq
*decl_specs
,
17912 cp_parser_flags flags
)
17914 tree type
= NULL_TREE
;
17918 /* Peek at the next token. */
17919 token
= cp_lexer_peek_token (parser
->lexer
);
17921 /* If we're looking at a keyword, things are easy. */
17922 switch (token
->keyword
)
17926 decl_specs
->explicit_char_p
= true;
17927 type
= char_type_node
;
17930 type
= char8_type_node
;
17933 type
= char16_type_node
;
17936 type
= char32_type_node
;
17939 type
= wchar_type_node
;
17942 type
= boolean_type_node
;
17945 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17946 type
= short_integer_type_node
;
17950 decl_specs
->explicit_int_p
= true;
17951 type
= integer_type_node
;
17957 idx
= token
->keyword
- RID_INT_N_0
;
17958 if (! int_n_enabled_p
[idx
])
17962 decl_specs
->explicit_intN_p
= true;
17963 decl_specs
->int_n_idx
= idx
;
17964 /* Check if the alternate "__intN__" form has been used instead of
17966 if (strncmp (IDENTIFIER_POINTER (token
->u
.value
)
17967 + (IDENTIFIER_LENGTH (token
->u
.value
) - 2),
17969 decl_specs
->int_n_alt
= true;
17971 type
= int_n_trees
[idx
].signed_type
;
17975 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17976 type
= long_integer_type_node
;
17979 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17980 type
= integer_type_node
;
17983 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17984 type
= unsigned_type_node
;
17987 type
= float_type_node
;
17990 type
= double_type_node
;
17993 type
= void_type_node
;
17997 maybe_warn_cpp0x (CPP0X_AUTO
);
17998 if (parser
->auto_is_implicit_function_template_parm_p
)
18000 /* The 'auto' might be the placeholder return type for a function decl
18001 with trailing return type. */
18002 bool have_trailing_return_fn_decl
= false;
18004 cp_parser_parse_tentatively (parser
);
18005 cp_lexer_consume_token (parser
->lexer
);
18006 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
18007 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
18008 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
18009 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
18011 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18013 cp_lexer_consume_token (parser
->lexer
);
18014 cp_parser_skip_to_closing_parenthesis (parser
,
18015 /*recovering*/false,
18017 /*consume_paren*/true);
18021 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
18023 have_trailing_return_fn_decl
= true;
18027 cp_lexer_consume_token (parser
->lexer
);
18029 cp_parser_abort_tentative_parse (parser
);
18031 if (have_trailing_return_fn_decl
)
18033 type
= make_auto ();
18037 if (cxx_dialect
>= cxx14
)
18039 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
18040 type
= TREE_TYPE (type
);
18043 type
= error_mark_node
;
18045 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
18047 if (cxx_dialect
< cxx14
)
18048 error_at (token
->location
,
18049 "use of %<auto%> in lambda parameter declaration "
18050 "only available with "
18051 "%<-std=c++14%> or %<-std=gnu++14%>");
18053 else if (cxx_dialect
< cxx14
)
18054 error_at (token
->location
,
18055 "use of %<auto%> in parameter declaration "
18056 "only available with "
18057 "%<-std=c++14%> or %<-std=gnu++14%>");
18058 else if (!flag_concepts
)
18059 pedwarn (token
->location
, 0,
18060 "use of %<auto%> in parameter declaration "
18061 "only available with %<-fconcepts-ts%>");
18064 type
= make_auto ();
18068 /* Since DR 743, decltype can either be a simple-type-specifier by
18069 itself or begin a nested-name-specifier. Parsing it will replace
18070 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18071 handling below decide what to do. */
18072 cp_parser_decltype (parser
);
18073 cp_lexer_set_token_position (parser
->lexer
, token
);
18077 /* Consume the `typeof' token. */
18078 cp_lexer_consume_token (parser
->lexer
);
18079 /* Parse the operand to `typeof'. */
18080 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
18081 /* If it is not already a TYPE, take its type. */
18082 if (!TYPE_P (type
))
18083 type
= finish_typeof (type
);
18086 cp_parser_set_decl_spec_type (decl_specs
, type
,
18088 /*type_definition_p=*/false);
18092 case RID_UNDERLYING_TYPE
:
18093 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
18095 cp_parser_set_decl_spec_type (decl_specs
, type
,
18097 /*type_definition_p=*/false);
18102 case RID_DIRECT_BASES
:
18103 type
= cp_parser_trait_expr (parser
, token
->keyword
);
18105 cp_parser_set_decl_spec_type (decl_specs
, type
,
18107 /*type_definition_p=*/false);
18113 /* If token is an already-parsed decltype not followed by ::,
18114 it's a simple-type-specifier. */
18115 if (token
->type
== CPP_DECLTYPE
18116 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
18118 type
= saved_checks_value (token
->u
.tree_check_value
);
18121 cp_parser_set_decl_spec_type (decl_specs
, type
,
18123 /*type_definition_p=*/false);
18124 /* Remember that we are handling a decltype in order to
18125 implement the resolution of DR 1510 when the argument
18126 isn't instantiation dependent. */
18127 decl_specs
->decltype_p
= true;
18129 cp_lexer_consume_token (parser
->lexer
);
18133 /* If the type-specifier was for a built-in type, we're done. */
18136 /* Record the type. */
18138 && (token
->keyword
!= RID_SIGNED
18139 && token
->keyword
!= RID_UNSIGNED
18140 && token
->keyword
!= RID_SHORT
18141 && token
->keyword
!= RID_LONG
))
18142 cp_parser_set_decl_spec_type (decl_specs
,
18145 /*type_definition_p=*/false);
18147 decl_specs
->any_specifiers_p
= true;
18149 /* Consume the token. */
18150 cp_lexer_consume_token (parser
->lexer
);
18152 if (type
== error_mark_node
)
18153 return error_mark_node
;
18155 /* There is no valid C++ program where a non-template type is
18156 followed by a "<". That usually indicates that the user thought
18157 that the type was a template. */
18158 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
18161 return TYPE_NAME (type
);
18164 /* The type-specifier must be a user-defined type. */
18165 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
18169 const bool typename_p
= (cxx_dialect
>= cxx20
18170 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
18172 /* Don't gobble tokens or issue error messages if this is an
18173 optional type-specifier. */
18174 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
18175 cp_parser_parse_tentatively (parser
);
18177 /* Remember current tentative parsing state -- if we know we need
18178 a type, we can give better diagnostics here. */
18179 bool tent
= cp_parser_parsing_tentatively (parser
);
18181 token
= cp_lexer_peek_token (parser
->lexer
);
18183 /* Look for the optional `::' operator. */
18185 = (cp_parser_global_scope_opt (parser
,
18186 /*current_scope_valid_p=*/false)
18188 /* Look for the nested-name specifier. */
18190 = (cp_parser_nested_name_specifier_opt (parser
,
18191 /*typename_keyword_p=*/false,
18192 /*check_dependency_p=*/true,
18194 /*is_declaration=*/false)
18196 /* If we have seen a nested-name-specifier, and the next token
18197 is `template', then we are using the template-id production. */
18199 && cp_parser_optional_template_keyword (parser
))
18201 /* Look for the template-id. */
18202 type
= cp_parser_template_id (parser
,
18203 /*template_keyword_p=*/true,
18204 /*check_dependency_p=*/true,
18206 /*is_declaration=*/false);
18207 /* If the template-id did not name a type, we are out of
18209 if (TREE_CODE (type
) != TYPE_DECL
)
18211 /* ...unless we pretend we have seen 'typename'. */
18213 type
= cp_parser_make_typename_type (parser
, type
,
18217 cp_parser_error (parser
, "expected template-id for type");
18218 type
= error_mark_node
;
18222 /* DR 1812: A < following a qualified-id in a typename-specifier
18223 could safely be assumed to begin a template argument list, so
18224 the template keyword should be optional. */
18225 else if (parser
->scope
18228 && cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
))
18230 cp_parser_parse_tentatively (parser
);
18232 type
= cp_parser_template_id (parser
,
18233 /*template_keyword_p=*/true,
18234 /*check_dependency_p=*/true,
18236 /*is_declaration=*/false);
18237 /* This is handled below, so back off. */
18238 if (type
&& concept_check_p (type
))
18239 cp_parser_simulate_error (parser
);
18241 if (!cp_parser_parse_definitely (parser
))
18243 else if (TREE_CODE (type
) == TEMPLATE_ID_EXPR
)
18244 type
= make_typename_type (parser
->scope
, type
, typename_type
,
18245 /*complain=*/tf_error
);
18246 else if (TREE_CODE (type
) != TYPE_DECL
)
18250 /* Otherwise, look for a type-name. */
18253 if (cxx_dialect
>= cxx17
)
18254 cp_parser_parse_tentatively (parser
);
18256 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
18258 if (cxx_dialect
>= cxx17
&& !cp_parser_parse_definitely (parser
))
18262 if (!type
&& flag_concepts
&& decl_specs
)
18264 /* Try for a type-constraint with template arguments. We check
18265 decl_specs here to avoid trying this for a functional cast. */
18267 cp_parser_parse_tentatively (parser
);
18269 type
= cp_parser_template_id (parser
,
18270 /*template_keyword_p=*/false,
18271 /*check_dependency_p=*/true,
18273 /*is_declaration=*/false);
18274 if (type
&& concept_check_p (type
))
18276 location_t loc
= EXPR_LOCATION (type
);
18277 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18279 if (tent
&& type
== error_mark_node
)
18280 /* Perhaps it's a concept-check expression. */
18281 cp_parser_simulate_error (parser
);
18284 cp_parser_simulate_error (parser
);
18286 if (!cp_parser_parse_definitely (parser
))
18290 if (!type
&& cxx_dialect
>= cxx17
)
18292 /* Try class template argument deduction or type-constraint without
18293 template arguments. */
18294 tree name
= cp_parser_identifier (parser
);
18295 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
18296 && parser
->scope
!= error_mark_node
)
18299 = cp_lexer_previous_token (parser
->lexer
)->location
;
18300 tree tmpl
= cp_parser_lookup_name (parser
, name
,
18302 /*is_template=*/false,
18303 /*is_namespace=*/false,
18304 /*check_dependency=*/true,
18305 /*ambiguous_decls=*/NULL
,
18307 if (tmpl
&& tmpl
!= error_mark_node
18308 && ctad_template_p (tmpl
))
18309 type
= make_template_placeholder (tmpl
);
18310 else if (flag_concepts
&& tmpl
&& concept_definition_p (tmpl
))
18311 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18315 type
= error_mark_node
;
18316 if (!cp_parser_simulate_error (parser
))
18317 cp_parser_name_lookup_error (parser
, name
, tmpl
,
18318 NLE_TYPE
, token
->location
);
18322 type
= error_mark_node
;
18325 /* If it didn't work out, we don't have a TYPE. */
18326 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
18327 && !cp_parser_parse_definitely (parser
))
18330 /* Keep track of all name-lookups performed in class scopes. */
18334 && TREE_CODE (type
) == TYPE_DECL
18335 && identifier_p (DECL_NAME (type
)))
18336 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
18338 if (type
&& decl_specs
)
18339 cp_parser_set_decl_spec_type (decl_specs
, type
,
18341 /*type_definition_p=*/false);
18344 /* If we didn't get a type-name, issue an error message. */
18345 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
18347 cp_parser_error (parser
, "expected type-name");
18348 return error_mark_node
;
18351 if (type
&& type
!= error_mark_node
)
18353 /* See if TYPE is an Objective-C type, and if so, parse and
18354 accept any protocol references following it. Do this before
18355 the cp_parser_check_for_invalid_template_id() call, because
18356 Objective-C types can be followed by '<...>' which would
18357 enclose protocol names rather than template arguments, and so
18358 everything is fine. */
18359 if (c_dialect_objc () && !parser
->scope
18360 && (objc_is_id (type
) || objc_is_class_name (type
)))
18362 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18363 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
18365 /* Clobber the "unqualified" type previously entered into
18366 DECL_SPECS with the new, improved protocol-qualified version. */
18368 decl_specs
->type
= qual_type
;
18373 /* There is no valid C++ program where a non-template type is
18374 followed by a "<". That usually indicates that the user
18375 thought that the type was a template. */
18376 cp_parser_check_for_invalid_template_id (parser
, type
,
18384 /* Parse the remainder of a placholder-type-specifier.
18386 placeholder-type-specifier:
18387 type-constraint_opt auto
18388 type-constraint_opt decltype(auto)
18390 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18391 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18392 parses the placeholder type, and performs some contextual syntactic analysis.
18394 LOC provides the location of the template name.
18396 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18397 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18398 might actually be a concept-check,
18400 Note that the Concepts TS allows the auto or decltype(auto) to be
18401 omitted in a constrained-type-specifier. */
18404 cp_parser_placeholder_type_specifier (cp_parser
*parser
, location_t loc
,
18405 tree tmpl
, bool tentative
)
18407 if (tmpl
== error_mark_node
)
18408 return error_mark_node
;
18410 tree orig_tmpl
= tmpl
;
18412 /* Get the arguments as written for subsequent analysis. */
18413 tree args
= NULL_TREE
;
18414 if (TREE_CODE (tmpl
) == TEMPLATE_ID_EXPR
)
18416 args
= TREE_OPERAND (tmpl
, 1);
18417 tmpl
= TREE_OPERAND (tmpl
, 0);
18419 if (args
== NULL_TREE
)
18420 /* A concept-name with no arguments can't be an expression. */
18423 tsubst_flags_t complain
= tentative
? tf_none
: tf_warning_or_error
;
18425 /* Get the concept and prototype parameter for the constraint. */
18426 tree_pair info
= finish_type_constraints (tmpl
, args
, complain
);
18427 tree con
= info
.first
;
18428 tree proto
= info
.second
;
18429 if (con
== error_mark_node
)
18430 return error_mark_node
;
18432 /* As per the standard, require auto or decltype(auto), except in some
18433 cases (template parameter lists, -fconcepts-ts enabled). */
18434 cp_token
*placeholder
= NULL
, *close_paren
= NULL
;
18435 if (cxx_dialect
>= cxx20
)
18437 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
18438 placeholder
= cp_lexer_consume_token (parser
->lexer
);
18439 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DECLTYPE
))
18441 placeholder
= cp_lexer_consume_token (parser
->lexer
);
18442 matching_parens parens
;
18443 parens
.require_open (parser
);
18444 cp_parser_require_keyword (parser
, RID_AUTO
, RT_AUTO
);
18445 close_paren
= parens
.require_close (parser
);
18449 /* A type constraint constrains a contextually determined type or type
18450 parameter pack. However, the Concepts TS does allow concepts
18451 to introduce non-type and template template parameters. */
18452 if (TREE_CODE (proto
) != TYPE_DECL
)
18454 if (!flag_concepts_ts
18455 || !processing_template_parmlist
)
18457 error_at (loc
, "%qE does not constrain a type", DECL_NAME (con
));
18458 inform (DECL_SOURCE_LOCATION (con
), "concept defined here");
18459 return error_mark_node
;
18463 /* In a template parameter list, a type-parameter can be introduced
18464 by type-constraints alone. */
18465 if (processing_template_parmlist
&& !placeholder
)
18466 return build_constrained_parameter (con
, proto
, args
);
18468 /* Diagnose issues placeholder issues. */
18469 if (!flag_concepts_ts
18470 && !parser
->in_result_type_constraint_p
18474 /* Perhaps it's a concept-check expression (c++/91073). */
18475 return error_mark_node
;
18477 tree id
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, args
);
18478 tree expr
= DECL_P (orig_tmpl
) ? DECL_NAME (con
) : id
;
18479 error_at (input_location
,
18480 "expected %<auto%> or %<decltype(auto)%> after %qE", expr
);
18481 /* Fall through. This is an error of omission. */
18483 else if (parser
->in_result_type_constraint_p
&& placeholder
)
18485 /* A trailing return type only allows type-constraints. */
18486 error_at (input_location
,
18487 "unexpected placeholder in constrained result type");
18490 /* In a parameter-declaration-clause, a placeholder-type-specifier
18491 results in an invented template parameter. */
18492 if (parser
->auto_is_implicit_function_template_parm_p
)
18496 location_t loc
= make_location (placeholder
->location
,
18497 placeholder
->location
,
18498 close_paren
->location
);
18499 error_at (loc
, "cannot declare a parameter with %<decltype(auto)%>");
18500 return error_mark_node
;
18502 tree parm
= build_constrained_parameter (con
, proto
, args
);
18503 return synthesize_implicit_template_parm (parser
, parm
);
18506 /* Determine if the type should be deduced using template argument
18507 deduction or decltype deduction. Note that the latter is always
18508 used for type-constraints in trailing return types. */
18509 bool decltype_p
= placeholder
18510 ? placeholder
->keyword
== RID_DECLTYPE
18511 : parser
->in_result_type_constraint_p
;
18513 /* Otherwise, this is the type of a variable or return type. */
18515 return make_constrained_decltype_auto (con
, args
);
18517 return make_constrained_auto (con
, args
);
18520 /* Parse a type-name.
18526 simple-template-id [in c++0x]
18543 Returns a TYPE_DECL for the type. */
18546 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
18550 /* We can't know yet whether it is a class-name or not. */
18551 cp_parser_parse_tentatively (parser
);
18552 /* Try a class-name. */
18553 type_decl
= cp_parser_class_name (parser
,
18554 typename_keyword_p
,
18555 /*template_keyword_p=*/false,
18557 /*check_dependency_p=*/true,
18558 /*class_head_p=*/false,
18559 /*is_declaration=*/false);
18560 /* If it's not a class-name, keep looking. */
18561 if (!cp_parser_parse_definitely (parser
))
18563 if (cxx_dialect
< cxx11
)
18564 /* It must be a typedef-name or an enum-name. */
18565 return cp_parser_nonclass_name (parser
);
18567 cp_parser_parse_tentatively (parser
);
18568 /* It is either a simple-template-id representing an
18569 instantiation of an alias template... */
18570 type_decl
= cp_parser_template_id (parser
,
18571 /*template_keyword_p=*/false,
18572 /*check_dependency_p=*/true,
18574 /*is_declaration=*/false);
18575 /* Note that this must be an instantiation of an alias template
18576 because [temp.names]/6 says:
18578 A template-id that names an alias template specialization
18581 Whereas [temp.names]/7 says:
18583 A simple-template-id that names a class template
18584 specialization is a class-name.
18586 With concepts, this could also be a partial-concept-id that
18587 declares a non-type template parameter. */
18588 if (type_decl
!= NULL_TREE
18589 && TREE_CODE (type_decl
) == TYPE_DECL
18590 && TYPE_DECL_ALIAS_P (type_decl
))
18591 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
18593 cp_parser_simulate_error (parser
);
18595 if (!cp_parser_parse_definitely (parser
))
18596 /* ... Or a typedef-name or an enum-name. */
18597 return cp_parser_nonclass_name (parser
);
18603 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18615 Returns a TYPE_DECL for the type. */
18618 cp_parser_nonclass_name (cp_parser
* parser
)
18623 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18624 identifier
= cp_parser_identifier (parser
);
18625 if (identifier
== error_mark_node
)
18626 return error_mark_node
;
18628 /* Look up the type-name. */
18629 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18631 type_decl
= strip_using_decl (type_decl
);
18633 if (TREE_CODE (type_decl
) != TYPE_DECL
18634 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18636 /* See if this is an Objective-C type. */
18637 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18638 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18640 type_decl
= TYPE_NAME (type
);
18643 /* Issue an error if we did not find a type-name. */
18644 if (TREE_CODE (type_decl
) != TYPE_DECL
18645 /* In Objective-C, we have the complication that class names are
18646 normally type names and start declarations (eg, the
18647 "NSObject" in "NSObject *object;"), but can be used in an
18648 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18649 is an expression. So, a classname followed by a dot is not a
18650 valid type-name. */
18651 || (objc_is_class_name (TREE_TYPE (type_decl
))
18652 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18654 if (!cp_parser_simulate_error (parser
))
18655 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18656 NLE_TYPE
, token
->location
);
18657 return error_mark_node
;
18659 /* Remember that the name was used in the definition of the
18660 current class so that we can check later to see if the
18661 meaning would have been different after the class was
18662 entirely defined. */
18663 else if (type_decl
!= error_mark_node
18665 maybe_note_name_used_in_class (identifier
, type_decl
);
18670 /* Parse an elaborated-type-specifier. Note that the grammar given
18671 here incorporates the resolution to DR68.
18673 elaborated-type-specifier:
18674 class-key :: [opt] nested-name-specifier [opt] identifier
18675 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18676 enum-key :: [opt] nested-name-specifier [opt] identifier
18677 typename :: [opt] nested-name-specifier identifier
18678 typename :: [opt] nested-name-specifier template [opt]
18683 elaborated-type-specifier:
18684 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18685 class-key attributes :: [opt] nested-name-specifier [opt]
18686 template [opt] template-id
18687 enum attributes :: [opt] nested-name-specifier [opt] identifier
18689 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18690 declared `friend'. If IS_DECLARATION is TRUE, then this
18691 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18692 something is being declared.
18694 Returns the TYPE specified. */
18697 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18699 bool is_declaration
)
18701 enum tag_types tag_type
;
18703 tree type
= NULL_TREE
;
18704 tree attributes
= NULL_TREE
;
18706 cp_token
*token
= NULL
;
18708 /* For class and enum types the location of the class-key or enum-key. */
18709 location_t key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18710 /* For a scoped enum, the 'class' or 'struct' keyword id. */
18711 rid scoped_key
= RID_MAX
;
18713 /* See if we're looking at the `enum' keyword. */
18714 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18716 /* Consume the `enum' token. */
18717 cp_lexer_consume_token (parser
->lexer
);
18718 /* Remember that it's an enumeration type. */
18719 tag_type
= enum_type
;
18720 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18721 enums) is used here. */
18722 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18723 if (cp_parser_is_keyword (token
, scoped_key
= RID_CLASS
)
18724 || cp_parser_is_keyword (token
, scoped_key
= RID_STRUCT
))
18726 location_t loc
= token
->location
;
18727 gcc_rich_location
richloc (loc
);
18728 richloc
.add_range (input_location
);
18729 richloc
.add_fixit_remove ();
18730 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18731 "a scoped enum must not use the %qD keyword",
18733 /* Consume the `struct' or `class' and parse it anyway. */
18734 cp_lexer_consume_token (parser
->lexer
);
18735 /* Create a combined location for the whole scoped-enum-key. */
18736 key_loc
= make_location (key_loc
, key_loc
, loc
);
18739 scoped_key
= RID_MAX
;
18741 /* Parse the attributes. */
18742 attributes
= cp_parser_attributes_opt (parser
);
18744 /* Or, it might be `typename'. */
18745 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18748 /* Consume the `typename' token. */
18749 cp_lexer_consume_token (parser
->lexer
);
18750 /* Remember that it's a `typename' type. */
18751 tag_type
= typename_type
;
18753 /* Otherwise it must be a class-key. */
18756 key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
18757 tag_type
= cp_parser_class_key (parser
);
18758 if (tag_type
== none_type
)
18759 return error_mark_node
;
18760 /* Parse the attributes. */
18761 attributes
= cp_parser_attributes_opt (parser
);
18764 /* Look for the `::' operator. */
18765 globalscope
= cp_parser_global_scope_opt (parser
,
18766 /*current_scope_valid_p=*/false);
18767 /* Look for the nested-name-specifier. */
18768 tree nested_name_specifier
;
18769 if (tag_type
== typename_type
&& !globalscope
)
18771 nested_name_specifier
18772 = cp_parser_nested_name_specifier (parser
,
18773 /*typename_keyword_p=*/true,
18774 /*check_dependency_p=*/true,
18777 if (!nested_name_specifier
)
18778 return error_mark_node
;
18781 /* Even though `typename' is not present, the proposed resolution
18782 to Core Issue 180 says that in `class A<T>::B', `B' should be
18783 considered a type-name, even if `A<T>' is dependent. */
18784 nested_name_specifier
18785 = cp_parser_nested_name_specifier_opt (parser
,
18786 /*typename_keyword_p=*/true,
18787 /*check_dependency_p=*/true,
18790 /* For everything but enumeration types, consider a template-id.
18791 For an enumeration type, consider only a plain identifier. */
18792 if (tag_type
!= enum_type
)
18794 bool template_p
= false;
18797 /* Allow the `template' keyword. */
18798 template_p
= cp_parser_optional_template_keyword (parser
);
18799 /* If we didn't see `template', we don't know if there's a
18800 template-id or not. */
18802 cp_parser_parse_tentatively (parser
);
18803 /* The `template' keyword must follow a nested-name-specifier. */
18804 else if (!nested_name_specifier
)
18806 cp_parser_error (parser
, "%<template%> must follow a nested-"
18808 return error_mark_node
;
18811 /* Parse the template-id. */
18812 token
= cp_lexer_peek_token (parser
->lexer
);
18813 decl
= cp_parser_template_id (parser
, template_p
,
18814 /*check_dependency_p=*/true,
18817 /* If we didn't find a template-id, look for an ordinary
18819 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18821 /* We can get here when cp_parser_template_id, called by
18822 cp_parser_class_name with tag_type == none_type, succeeds
18823 and caches a BASELINK. Then, when called again here,
18824 instead of failing and returning an error_mark_node
18825 returns it (see template/typename17.C in C++11).
18826 ??? Could we diagnose this earlier? */
18827 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18829 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18830 type
= error_mark_node
;
18832 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18833 in effect, then we must assume that, upon instantiation, the
18834 template will correspond to a class. */
18835 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18836 && tag_type
== typename_type
)
18837 type
= make_typename_type (parser
->scope
, decl
,
18839 /*complain=*/tf_error
);
18840 /* If the `typename' keyword is in effect and DECL is not a type
18841 decl, then type is non existent. */
18842 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18844 else if (TREE_CODE (decl
) == TYPE_DECL
)
18846 type
= check_elaborated_type_specifier (tag_type
, decl
,
18847 /*allow_template_p=*/true);
18849 /* If the next token is a semicolon, this must be a specialization,
18850 instantiation, or friend declaration. Check the scope while we
18851 still know whether or not we had a nested-name-specifier. */
18852 if (type
!= error_mark_node
18853 && !nested_name_specifier
&& !is_friend
18854 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18855 check_unqualified_spec_or_inst (type
, token
->location
);
18857 else if (decl
== error_mark_node
)
18858 type
= error_mark_node
;
18863 token
= cp_lexer_peek_token (parser
->lexer
);
18864 identifier
= cp_parser_identifier (parser
);
18866 if (identifier
== error_mark_node
)
18868 parser
->scope
= NULL_TREE
;
18869 return error_mark_node
;
18872 /* For a `typename', we needn't call xref_tag. */
18873 if (tag_type
== typename_type
18874 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18875 return cp_parser_make_typename_type (parser
, identifier
,
18878 /* Template parameter lists apply only if we are not within a
18879 function parameter list. */
18880 bool template_parm_lists_apply
18881 = parser
->num_template_parameter_lists
;
18882 if (template_parm_lists_apply
)
18883 for (cp_binding_level
*s
= current_binding_level
;
18884 s
&& s
->kind
!= sk_template_parms
;
18885 s
= s
->level_chain
)
18886 if (s
->kind
== sk_function_parms
)
18887 template_parm_lists_apply
= false;
18889 /* Look up a qualified name in the usual way. */
18893 tree ambiguous_decls
;
18895 decl
= cp_parser_lookup_name (parser
, identifier
,
18897 /*is_template=*/false,
18898 /*is_namespace=*/false,
18899 /*check_dependency=*/true,
18903 /* If the lookup was ambiguous, an error will already have been
18905 if (ambiguous_decls
)
18906 return error_mark_node
;
18908 /* If we are parsing friend declaration, DECL may be a
18909 TEMPLATE_DECL tree node here. However, we need to check
18910 whether this TEMPLATE_DECL results in valid code. Consider
18911 the following example:
18914 template <class T> class C {};
18917 template <class T> friend class N::C; // #1, valid code
18919 template <class T> class Y {
18920 friend class N::C; // #2, invalid code
18923 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18924 name lookup of `N::C'. We see that friend declaration must
18925 be template for the code to be valid. Note that
18926 processing_template_decl does not work here since it is
18927 always 1 for the above two cases. */
18929 decl
= (cp_parser_maybe_treat_template_as_class
18930 (decl
, /*tag_name_p=*/is_friend
18931 && template_parm_lists_apply
));
18933 if (TREE_CODE (decl
) != TYPE_DECL
)
18935 cp_parser_diagnose_invalid_type_name (parser
,
18938 return error_mark_node
;
18941 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18943 bool allow_template
= (template_parm_lists_apply
18944 || DECL_SELF_REFERENCE_P (decl
));
18945 type
= check_elaborated_type_specifier (tag_type
, decl
,
18948 if (type
== error_mark_node
)
18949 return error_mark_node
;
18952 /* Forward declarations of nested types, such as
18957 are invalid unless all components preceding the final '::'
18958 are complete. If all enclosing types are complete, these
18959 declarations become merely pointless.
18961 Invalid forward declarations of nested types are errors
18962 caught elsewhere in parsing. Those that are pointless arrive
18965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18966 && !is_friend
&& is_declaration
18967 && !processing_explicit_instantiation
)
18968 warning (0, "declaration %qD does not declare anything", decl
);
18970 type
= TREE_TYPE (decl
);
18974 /* An elaborated-type-specifier sometimes introduces a new type and
18975 sometimes names an existing type. Normally, the rule is that it
18976 introduces a new type only if there is not an existing type of
18977 the same name already in scope. For example, given:
18980 void f() { struct S s; }
18982 the `struct S' in the body of `f' is the same `struct S' as in
18983 the global scope; the existing definition is used. However, if
18984 there were no global declaration, this would introduce a new
18985 local class named `S'.
18987 An exception to this rule applies to the following code:
18989 namespace N { struct S; }
18991 Here, the elaborated-type-specifier names a new type
18992 unconditionally; even if there is already an `S' in the
18993 containing scope this declaration names a new type.
18994 This exception only applies if the elaborated-type-specifier
18995 forms the complete declaration:
18999 A declaration consisting solely of `class-key identifier ;' is
19000 either a redeclaration of the name in the current scope or a
19001 forward declaration of the identifier as a class name. It
19002 introduces the name into the current scope.
19004 We are in this situation precisely when the next token is a `;'.
19006 An exception to the exception is that a `friend' declaration does
19007 *not* name a new type; i.e., given:
19009 struct S { friend struct T; };
19011 `T' is not a new type in the scope of `S'.
19013 Also, `new struct S' or `sizeof (struct S)' never results in the
19014 definition of a new type; a new type can only be declared in a
19015 declaration context. */
19021 /* Friends have special name lookup rules. */
19022 ts
= ts_within_enclosing_non_class
;
19023 else if (is_declaration
19024 && cp_lexer_next_token_is (parser
->lexer
,
19026 /* This is a `class-key identifier ;' */
19032 (template_parm_lists_apply
19033 && (cp_parser_next_token_starts_class_definition_p (parser
)
19034 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
19035 /* An unqualified name was used to reference this type, so
19036 there were no qualifying templates. */
19037 if (template_parm_lists_apply
19038 && !cp_parser_check_template_parameters (parser
,
19039 /*num_templates=*/0,
19040 /*template_id*/false,
19042 /*declarator=*/NULL
))
19043 return error_mark_node
;
19044 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
19048 if (type
== error_mark_node
)
19049 return error_mark_node
;
19051 /* Allow attributes on forward declarations of classes. */
19054 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19055 warning (OPT_Wattributes
,
19056 "attributes ignored on uninstantiated type");
19057 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
19058 && ! processing_explicit_instantiation
)
19059 warning (OPT_Wattributes
,
19060 "attributes ignored on template instantiation");
19061 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
19062 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
19064 warning (OPT_Wattributes
,
19065 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19068 if (tag_type
== enum_type
)
19069 cp_parser_maybe_warn_enum_key (parser
, key_loc
, type
, scoped_key
);
19072 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19073 for alias definition. */
19074 bool decl_class
= (is_declaration
19075 && cp_parser_declares_only_class_p (parser
));
19076 cp_parser_check_class_key (parser
, key_loc
, tag_type
, type
, false,
19079 /* Indicate whether this class was declared as a `class' or as a
19081 if (CLASS_TYPE_P (type
) && !currently_open_class (type
))
19082 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
19085 /* A "<" cannot follow an elaborated type specifier. If that
19086 happens, the user was probably trying to form a template-id. */
19087 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
19093 /* Parse an enum-specifier.
19096 enum-head { enumerator-list [opt] }
19097 enum-head { enumerator-list , } [C++0x]
19100 enum-key identifier [opt] enum-base [opt]
19101 enum-key nested-name-specifier identifier enum-base [opt]
19106 enum struct [C++0x]
19109 : type-specifier-seq
19111 opaque-enum-specifier:
19112 enum-key identifier enum-base [opt] ;
19115 enum-key attributes[opt] identifier [opt] enum-base [opt]
19116 { enumerator-list [opt] }attributes[opt]
19117 enum-key attributes[opt] identifier [opt] enum-base [opt]
19118 { enumerator-list, }attributes[opt] [C++0x]
19120 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19121 if the token stream isn't an enum-specifier after all. */
19124 cp_parser_enum_specifier (cp_parser
* parser
)
19127 tree type
= NULL_TREE
;
19129 tree nested_name_specifier
= NULL_TREE
;
19131 bool scoped_enum_p
= false;
19132 bool has_underlying_type
= false;
19133 bool nested_being_defined
= false;
19134 bool new_value_list
= false;
19135 bool is_new_type
= false;
19136 bool is_unnamed
= false;
19137 tree underlying_type
= NULL_TREE
;
19138 cp_token
*type_start_token
= NULL
;
19139 temp_override
<bool> cleanup (parser
->colon_corrects_to_scope_p
, false);
19141 /* Parse tentatively so that we can back up if we don't find a
19143 cp_parser_parse_tentatively (parser
);
19145 /* Caller guarantees that the current token is 'enum', an identifier
19146 possibly follows, and the token after that is an opening brace.
19147 If we don't have an identifier, fabricate an anonymous name for
19148 the enumeration being defined. */
19149 cp_lexer_consume_token (parser
->lexer
);
19151 /* Parse the "class" or "struct", which indicates a scoped
19152 enumeration type in C++0x. */
19153 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
19154 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
19156 if (cxx_dialect
< cxx11
)
19157 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19159 /* Consume the `struct' or `class' token. */
19160 cp_lexer_consume_token (parser
->lexer
);
19162 scoped_enum_p
= true;
19165 attributes
= cp_parser_attributes_opt (parser
);
19167 /* Clear the qualification. */
19168 parser
->scope
= NULL_TREE
;
19169 parser
->qualifying_scope
= NULL_TREE
;
19170 parser
->object_scope
= NULL_TREE
;
19172 /* Figure out in what scope the declaration is being placed. */
19173 prev_scope
= current_scope ();
19175 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19177 push_deferring_access_checks (dk_no_check
);
19178 nested_name_specifier
19179 = cp_parser_nested_name_specifier_opt (parser
,
19180 /*typename_keyword_p=*/true,
19181 /*check_dependency_p=*/false,
19183 /*is_declaration=*/false);
19185 if (nested_name_specifier
)
19189 identifier
= cp_parser_identifier (parser
);
19190 name
= cp_parser_lookup_name (parser
, identifier
,
19192 /*is_template=*/false,
19193 /*is_namespace=*/false,
19194 /*check_dependency=*/true,
19195 /*ambiguous_decls=*/NULL
,
19197 if (name
&& name
!= error_mark_node
)
19199 type
= TREE_TYPE (name
);
19200 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19202 /* Are template enums allowed in ISO? */
19203 if (template_parm_scope_p ())
19204 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
19205 "%qD is an enumeration template", name
);
19206 /* ignore a typename reference, for it will be solved by name
19211 else if (nested_name_specifier
== error_mark_node
)
19212 /* We already issued an error. */;
19215 error_at (type_start_token
->location
,
19216 "%qD does not name an enumeration in %qT",
19217 identifier
, nested_name_specifier
);
19218 nested_name_specifier
= error_mark_node
;
19223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19224 identifier
= cp_parser_identifier (parser
);
19227 identifier
= make_anon_name ();
19230 error_at (type_start_token
->location
,
19231 "unnamed scoped enum is not allowed");
19234 pop_deferring_access_checks ();
19236 /* Check for the `:' that denotes a specified underlying type in C++0x.
19237 Note that a ':' could also indicate a bitfield width, however. */
19238 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19240 cp_decl_specifier_seq type_specifiers
;
19242 /* Consume the `:'. */
19243 cp_lexer_consume_token (parser
->lexer
);
19245 /* Parse the type-specifier-seq. */
19246 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
19247 /*is_declaration=*/false,
19248 /*is_trailing_return=*/false,
19251 /* At this point this is surely not elaborated type specifier. */
19252 if (!cp_parser_parse_definitely (parser
))
19255 if (cxx_dialect
< cxx11
)
19256 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19258 has_underlying_type
= true;
19260 /* If that didn't work, stop. */
19261 if (type_specifiers
.type
!= error_mark_node
)
19263 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
19264 /*initialized=*/0, NULL
);
19265 if (underlying_type
== error_mark_node
19266 || check_for_bare_parameter_packs (underlying_type
))
19267 underlying_type
= NULL_TREE
;
19271 /* Look for the `{' but don't consume it yet. */
19272 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19274 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
19276 if (has_underlying_type
)
19277 cp_parser_commit_to_tentative_parse (parser
);
19278 cp_parser_error (parser
, "expected %<{%>");
19279 if (has_underlying_type
)
19280 return error_mark_node
;
19282 /* An opaque-enum-specifier must have a ';' here. */
19283 if ((scoped_enum_p
|| underlying_type
)
19284 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19286 if (has_underlying_type
)
19287 cp_parser_commit_to_tentative_parse (parser
);
19288 cp_parser_error (parser
, "expected %<;%> or %<{%>");
19289 if (has_underlying_type
)
19290 return error_mark_node
;
19294 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
19297 if (nested_name_specifier
)
19299 if (CLASS_TYPE_P (nested_name_specifier
))
19301 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
19302 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
19303 push_scope (nested_name_specifier
);
19305 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19306 push_nested_namespace (nested_name_specifier
);
19309 /* Issue an error message if type-definitions are forbidden here. */
19310 if (!cp_parser_check_type_definition (parser
))
19311 type
= error_mark_node
;
19313 /* Create the new type. We do this before consuming the opening
19314 brace so the enum will be recorded as being on the line of its
19315 tag (or the 'enum' keyword, if there is no tag). */
19316 type
= start_enum (identifier
, type
, underlying_type
,
19317 attributes
, scoped_enum_p
, &is_new_type
);
19319 /* If the next token is not '{' it is an opaque-enum-specifier or an
19320 elaborated-type-specifier. */
19321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19323 timevar_push (TV_PARSE_ENUM
);
19324 if (nested_name_specifier
19325 && nested_name_specifier
!= error_mark_node
)
19327 /* The following catches invalid code such as:
19328 enum class S<int>::E { A, B, C }; */
19329 if (!processing_specialization
19330 && CLASS_TYPE_P (nested_name_specifier
)
19331 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
19332 error_at (type_start_token
->location
, "cannot add an enumerator "
19333 "list to a template instantiation");
19335 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
19337 error_at (type_start_token
->location
,
19338 "%<%T::%E%> has not been declared",
19339 TYPE_CONTEXT (nested_name_specifier
),
19340 nested_name_specifier
);
19341 type
= error_mark_node
;
19343 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
19344 && !CLASS_TYPE_P (nested_name_specifier
))
19346 error_at (type_start_token
->location
, "nested name specifier "
19347 "%qT for enum declaration does not name a class "
19348 "or namespace", nested_name_specifier
);
19349 type
= error_mark_node
;
19351 /* If that scope does not contain the scope in which the
19352 class was originally declared, the program is invalid. */
19353 else if (prev_scope
&& !is_ancestor (prev_scope
,
19354 nested_name_specifier
))
19356 if (at_namespace_scope_p ())
19357 error_at (type_start_token
->location
,
19358 "declaration of %qD in namespace %qD which does not "
19360 type
, prev_scope
, nested_name_specifier
);
19362 error_at (type_start_token
->location
,
19363 "declaration of %qD in %qD which does not "
19365 type
, prev_scope
, nested_name_specifier
);
19366 type
= error_mark_node
;
19368 /* If that scope is the scope where the declaration is being placed
19369 the program is invalid. */
19370 else if (CLASS_TYPE_P (nested_name_specifier
)
19371 && CLASS_TYPE_P (prev_scope
)
19372 && same_type_p (nested_name_specifier
, prev_scope
))
19374 permerror (type_start_token
->location
,
19375 "extra qualification not allowed");
19376 nested_name_specifier
= NULL_TREE
;
19381 begin_scope (sk_scoped_enum
, type
);
19383 /* Consume the opening brace. */
19384 matching_braces braces
;
19385 braces
.consume_open (parser
);
19387 if (type
== error_mark_node
)
19388 ; /* Nothing to add */
19389 else if (OPAQUE_ENUM_P (type
)
19390 || (cxx_dialect
> cxx98
&& processing_specialization
))
19392 new_value_list
= true;
19393 SET_OPAQUE_ENUM_P (type
, false);
19394 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
19398 error_at (type_start_token
->location
,
19399 "multiple definition of %q#T", type
);
19400 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
19401 "previous definition here");
19402 type
= error_mark_node
;
19405 if (type
== error_mark_node
)
19406 cp_parser_skip_to_end_of_block_or_statement (parser
);
19407 /* If the next token is not '}', then there are some enumerators. */
19408 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19410 if (is_unnamed
&& !scoped_enum_p
)
19411 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
19412 "ISO C++ forbids empty unnamed enum");
19415 cp_parser_enumerator_list (parser
, type
);
19417 /* Consume the final '}'. */
19418 braces
.require_close (parser
);
19422 timevar_pop (TV_PARSE_ENUM
);
19426 /* If a ';' follows, then it is an opaque-enum-specifier
19427 and additional restrictions apply. */
19428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19431 error_at (type_start_token
->location
,
19432 "opaque-enum-specifier without name");
19433 else if (nested_name_specifier
)
19434 error_at (type_start_token
->location
,
19435 "opaque-enum-specifier must use a simple identifier");
19439 /* Look for trailing attributes to apply to this enumeration, and
19440 apply them if appropriate. */
19441 if (cp_parser_allow_gnu_extensions_p (parser
))
19443 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
19444 cplus_decl_attributes (&type
,
19446 (int) ATTR_FLAG_TYPE_IN_PLACE
);
19449 /* Finish up the enumeration. */
19450 if (type
!= error_mark_node
)
19452 if (new_value_list
)
19453 finish_enum_value_list (type
);
19455 finish_enum (type
);
19458 if (nested_name_specifier
)
19460 if (CLASS_TYPE_P (nested_name_specifier
))
19462 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
19463 pop_scope (nested_name_specifier
);
19465 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19466 pop_nested_namespace (nested_name_specifier
);
19471 /* Parse an enumerator-list. The enumerators all have the indicated
19475 enumerator-definition
19476 enumerator-list , enumerator-definition */
19479 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
19483 /* Parse an enumerator-definition. */
19484 cp_parser_enumerator_definition (parser
, type
);
19486 /* If the next token is not a ',', we've reached the end of
19488 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19490 /* Otherwise, consume the `,' and keep going. */
19491 cp_lexer_consume_token (parser
->lexer
);
19492 /* If the next token is a `}', there is a trailing comma. */
19493 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19495 if (cxx_dialect
< cxx11
)
19496 pedwarn (input_location
, OPT_Wpedantic
,
19497 "comma at end of enumerator list");
19503 /* Parse an enumerator-definition. The enumerator has the indicated
19506 enumerator-definition:
19508 enumerator = constant-expression
19515 enumerator-definition:
19516 enumerator attributes [opt]
19517 enumerator attributes [opt] = constant-expression */
19520 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19526 /* Save the input location because we are interested in the location
19527 of the identifier and not the location of the explicit value. */
19528 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19530 /* Look for the identifier. */
19531 identifier
= cp_parser_identifier (parser
);
19532 if (identifier
== error_mark_node
)
19535 /* Parse any specified attributes. */
19536 tree attrs
= cp_parser_attributes_opt (parser
);
19538 /* If the next token is an '=', then there is an explicit value. */
19539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19541 /* Consume the `=' token. */
19542 cp_lexer_consume_token (parser
->lexer
);
19543 /* Parse the value. */
19544 value
= cp_parser_constant_expression (parser
);
19549 /* If we are processing a template, make sure the initializer of the
19550 enumerator doesn't contain any bare template parameter pack. */
19551 if (check_for_bare_parameter_packs (value
))
19552 value
= error_mark_node
;
19554 /* Create the enumerator. */
19555 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19558 /* Parse a namespace-name.
19561 original-namespace-name
19564 Returns the NAMESPACE_DECL for the namespace. */
19567 cp_parser_namespace_name (cp_parser
* parser
)
19570 tree namespace_decl
;
19572 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19574 /* Get the name of the namespace. */
19575 identifier
= cp_parser_identifier (parser
);
19576 if (identifier
== error_mark_node
)
19577 return error_mark_node
;
19579 /* Look up the identifier in the currently active scope. Look only
19580 for namespaces, due to:
19582 [basic.lookup.udir]
19584 When looking up a namespace-name in a using-directive or alias
19585 definition, only namespace names are considered.
19589 [basic.lookup.qual]
19591 During the lookup of a name preceding the :: scope resolution
19592 operator, object, function, and enumerator names are ignored.
19594 (Note that cp_parser_qualifying_entity only calls this
19595 function if the token after the name is the scope resolution
19597 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19599 /*is_template=*/false,
19600 /*is_namespace=*/true,
19601 /*check_dependency=*/true,
19602 /*ambiguous_decls=*/NULL
,
19604 /* If it's not a namespace, issue an error. */
19605 if (namespace_decl
== error_mark_node
19606 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19608 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19610 auto_diagnostic_group d
;
19612 if (namespace_decl
== error_mark_node
19613 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19614 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19617 if (const char *suggestion
= hint
.suggestion ())
19619 gcc_rich_location
richloc (token
->location
);
19620 richloc
.add_fixit_replace (suggestion
);
19621 error_at (&richloc
,
19622 "%qD is not a namespace-name; did you mean %qs?",
19623 identifier
, suggestion
);
19626 error_at (token
->location
, "%qD is not a namespace-name",
19630 cp_parser_error (parser
, "expected namespace-name");
19631 namespace_decl
= error_mark_node
;
19634 return namespace_decl
;
19637 /* Parse a namespace-definition.
19639 namespace-definition:
19640 named-namespace-definition
19641 unnamed-namespace-definition
19643 named-namespace-definition:
19644 original-namespace-definition
19645 extension-namespace-definition
19647 original-namespace-definition:
19648 namespace identifier { namespace-body }
19650 extension-namespace-definition:
19651 namespace original-namespace-name { namespace-body }
19653 unnamed-namespace-definition:
19654 namespace { namespace-body } */
19657 cp_parser_namespace_definition (cp_parser
* parser
)
19660 int nested_definition_count
= 0;
19662 cp_ensure_no_omp_declare_simd (parser
);
19663 cp_ensure_no_oacc_routine (parser
);
19665 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19666 const bool topmost_inline_p
= is_inline
;
19670 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19671 cp_lexer_consume_token (parser
->lexer
);
19674 /* Look for the `namespace' keyword. */
19676 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19678 /* Parse any specified attributes before the identifier. */
19679 tree attribs
= cp_parser_attributes_opt (parser
);
19683 identifier
= NULL_TREE
;
19685 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19687 if (nested_inline_p
&& nested_definition_count
!= 0)
19689 if (cxx_dialect
< cxx20
)
19690 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19691 OPT_Wpedantic
, "nested inline namespace definitions only "
19692 "available with %<-std=c++20%> or %<-std=gnu++20%>");
19693 cp_lexer_consume_token (parser
->lexer
);
19696 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19698 identifier
= cp_parser_identifier (parser
);
19700 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19701 pedwarn (input_location
, OPT_Wpedantic
,
19702 "standard attributes on namespaces must precede "
19703 "the namespace name");
19705 /* Parse any attributes specified after the identifier. */
19706 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19709 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19711 /* Don't forget that the innermost namespace might have been
19712 marked as inline. Use |= because we cannot overwrite
19713 IS_INLINE in case the outermost namespace is inline, but
19714 there are no nested inlines. */
19715 is_inline
|= nested_inline_p
;
19719 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19720 pedwarn (input_location
, OPT_Wpedantic
,
19721 "nested namespace definitions only available with "
19722 "%<-std=c++17%> or %<-std=gnu++17%>");
19724 /* Nested namespace names can create new namespaces (unlike
19725 other qualified-ids). */
19726 if (int count
= (identifier
19727 ? push_namespace (identifier
, nested_inline_p
)
19729 nested_definition_count
+= count
;
19731 cp_parser_error (parser
, "nested namespace name required");
19732 cp_lexer_consume_token (parser
->lexer
);
19735 if (nested_definition_count
&& !identifier
)
19736 cp_parser_error (parser
, "namespace name required");
19738 if (nested_definition_count
&& attribs
)
19739 error_at (token
->location
,
19740 "a nested namespace definition cannot have attributes");
19741 if (nested_definition_count
&& topmost_inline_p
)
19742 error_at (token
->location
,
19743 "a nested namespace definition cannot be inline");
19745 /* Start the namespace. */
19746 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19748 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19750 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19752 /* Look for the `{' to validate starting the namespace. */
19753 matching_braces braces
;
19754 if (braces
.require_open (parser
))
19756 /* Parse the body of the namespace. */
19757 cp_parser_namespace_body (parser
);
19759 /* Look for the final `}'. */
19760 braces
.require_close (parser
);
19763 if (has_visibility
)
19764 pop_visibility (1);
19766 /* Pop the nested namespace definitions. */
19767 while (nested_definition_count
--)
19771 /* Parse a namespace-body.
19774 declaration-seq [opt] */
19777 cp_parser_namespace_body (cp_parser
* parser
)
19779 cp_parser_declaration_seq_opt (parser
);
19782 /* Parse a namespace-alias-definition.
19784 namespace-alias-definition:
19785 namespace identifier = qualified-namespace-specifier ; */
19788 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19791 tree namespace_specifier
;
19793 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19795 /* Look for the `namespace' keyword. */
19796 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19797 /* Look for the identifier. */
19798 identifier
= cp_parser_identifier (parser
);
19799 if (identifier
== error_mark_node
)
19801 /* Look for the `=' token. */
19802 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19803 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19805 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19806 /* Skip the definition. */
19807 cp_lexer_consume_token (parser
->lexer
);
19808 if (cp_parser_skip_to_closing_brace (parser
))
19809 cp_lexer_consume_token (parser
->lexer
);
19812 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19813 /* Look for the qualified-namespace-specifier. */
19814 namespace_specifier
19815 = cp_parser_qualified_namespace_specifier (parser
);
19816 cp_warn_deprecated_use_scopes (namespace_specifier
);
19817 /* Look for the `;' token. */
19818 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19820 /* Register the alias in the symbol table. */
19821 do_namespace_alias (identifier
, namespace_specifier
);
19824 /* Parse a qualified-namespace-specifier.
19826 qualified-namespace-specifier:
19827 :: [opt] nested-name-specifier [opt] namespace-name
19829 Returns a NAMESPACE_DECL corresponding to the specified
19833 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19835 /* Look for the optional `::'. */
19836 cp_parser_global_scope_opt (parser
,
19837 /*current_scope_valid_p=*/false);
19839 /* Look for the optional nested-name-specifier. */
19840 cp_parser_nested_name_specifier_opt (parser
,
19841 /*typename_keyword_p=*/false,
19842 /*check_dependency_p=*/true,
19844 /*is_declaration=*/true);
19846 return cp_parser_namespace_name (parser
);
19849 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19850 access declaration.
19853 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19854 using :: unqualified-id ;
19856 access-declaration:
19862 cp_parser_using_declaration (cp_parser
* parser
,
19863 bool access_declaration_p
)
19866 bool typename_p
= false;
19867 bool global_scope_p
;
19871 int oldcount
= errorcount
;
19872 cp_token
*diag_token
= NULL
;
19874 if (access_declaration_p
)
19876 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19877 cp_parser_parse_tentatively (parser
);
19881 /* Look for the `using' keyword. */
19882 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19885 /* Peek at the next token. */
19886 token
= cp_lexer_peek_token (parser
->lexer
);
19887 /* See if it's `typename'. */
19888 if (token
->keyword
== RID_TYPENAME
)
19890 /* Remember that we've seen it. */
19892 /* Consume the `typename' token. */
19893 cp_lexer_consume_token (parser
->lexer
);
19897 /* Look for the optional global scope qualification. */
19899 = (cp_parser_global_scope_opt (parser
,
19900 /*current_scope_valid_p=*/false)
19903 /* If we saw `typename', or didn't see `::', then there must be a
19904 nested-name-specifier present. */
19905 if (typename_p
|| !global_scope_p
)
19907 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19908 /*check_dependency_p=*/true,
19910 /*is_declaration=*/true);
19911 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19913 cp_parser_skip_to_end_of_block_or_statement (parser
);
19917 /* Otherwise, we could be in either of the two productions. In that
19918 case, treat the nested-name-specifier as optional. */
19920 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19921 /*typename_keyword_p=*/false,
19922 /*check_dependency_p=*/true,
19924 /*is_declaration=*/true);
19926 qscope
= global_namespace
;
19927 else if (UNSCOPED_ENUM_P (qscope
)
19928 && !TYPE_FUNCTION_SCOPE_P (qscope
))
19929 qscope
= CP_TYPE_CONTEXT (qscope
);
19931 cp_warn_deprecated_use_scopes (qscope
);
19933 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19934 /* Something has already gone wrong; there's no need to parse
19935 further. Since an error has occurred, the return value of
19936 cp_parser_parse_definitely will be false, as required. */
19937 return cp_parser_parse_definitely (parser
);
19939 token
= cp_lexer_peek_token (parser
->lexer
);
19940 /* Parse the unqualified-id. */
19941 identifier
= cp_parser_unqualified_id (parser
,
19942 /*template_keyword_p=*/false,
19943 /*check_dependency_p=*/true,
19944 /*declarator_p=*/true,
19945 /*optional_p=*/false);
19947 if (access_declaration_p
)
19949 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19950 cp_parser_simulate_error (parser
);
19951 if (!cp_parser_parse_definitely (parser
))
19954 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19956 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19957 if (cxx_dialect
< cxx17
)
19958 pedwarn (ell
->location
, 0,
19959 "pack expansion in using-declaration only available "
19960 "with %<-std=c++17%> or %<-std=gnu++17%>");
19961 qscope
= make_pack_expansion (qscope
);
19964 /* The function we call to handle a using-declaration is different
19965 depending on what scope we are in. */
19966 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19968 else if (!identifier_p (identifier
)
19969 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19970 /* [namespace.udecl]
19972 A using declaration shall not name a template-id. */
19973 error_at (token
->location
,
19974 "a template-id may not appear in a using-declaration");
19977 if (at_class_scope_p ())
19979 /* Create the USING_DECL. */
19980 decl
= do_class_using_decl (qscope
, identifier
);
19982 if (decl
&& typename_p
)
19983 USING_DECL_TYPENAME_P (decl
) = 1;
19985 if (check_for_bare_parameter_packs (decl
))
19987 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19991 /* Add it to the list of members in this class. */
19992 finish_member_declaration (decl
);
19995 finish_nonmember_using_decl (qscope
, identifier
);
19998 if (!access_declaration_p
19999 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20001 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
20002 if (cxx_dialect
< cxx17
)
20003 pedwarn (comma
->location
, 0,
20004 "comma-separated list in using-declaration only available "
20005 "with %<-std=c++17%> or %<-std=gnu++17%>");
20009 /* Look for the final `;'. */
20010 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20012 if (access_declaration_p
&& errorcount
== oldcount
)
20013 warning_at (diag_token
->location
, OPT_Wdeprecated
,
20014 "access declarations are deprecated "
20015 "in favour of using-declarations; "
20016 "suggestion: add the %<using%> keyword");
20021 /* Parse an alias-declaration.
20024 using identifier attribute-specifier-seq [opt] = type-id */
20027 cp_parser_alias_declaration (cp_parser
* parser
)
20029 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
20030 location_t id_location
, type_location
;
20031 cp_declarator
*declarator
;
20032 cp_decl_specifier_seq decl_specs
;
20034 const char *saved_message
= NULL
;
20036 /* Look for the `using' keyword. */
20037 cp_token
*using_token
20038 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20039 if (using_token
== NULL
)
20040 return error_mark_node
;
20042 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
20043 id
= cp_parser_identifier (parser
);
20044 if (id
== error_mark_node
)
20045 return error_mark_node
;
20047 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
20048 attributes
= cp_parser_attributes_opt (parser
);
20049 if (attributes
== error_mark_node
)
20050 return error_mark_node
;
20052 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
20054 if (cp_parser_error_occurred (parser
))
20055 return error_mark_node
;
20057 cp_parser_commit_to_tentative_parse (parser
);
20059 /* Now we are going to parse the type-id of the declaration. */
20064 "A type-specifier-seq shall not define a class or enumeration
20065 unless it appears in the type-id of an alias-declaration (7.1.3) that
20066 is not the declaration of a template-declaration."
20068 In other words, if we currently are in an alias template, the
20069 type-id should not define a type.
20071 So let's set parser->type_definition_forbidden_message in that
20072 case; cp_parser_check_type_definition (called by
20073 cp_parser_class_specifier) will then emit an error if a type is
20074 defined in the type-id. */
20075 if (parser
->num_template_parameter_lists
)
20077 saved_message
= parser
->type_definition_forbidden_message
;
20078 parser
->type_definition_forbidden_message
=
20079 G_("types may not be defined in alias template declarations");
20082 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
20085 /* Restore the error message if need be. */
20086 if (parser
->num_template_parameter_lists
)
20087 parser
->type_definition_forbidden_message
= saved_message
;
20089 if (type
== error_mark_node
20090 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
20092 cp_parser_skip_to_end_of_block_or_statement (parser
);
20093 return error_mark_node
;
20096 /* A typedef-name can also be introduced by an alias-declaration. The
20097 identifier following the using keyword becomes a typedef-name. It has
20098 the same semantics as if it were introduced by the typedef
20099 specifier. In particular, it does not define a new type and it shall
20100 not appear in the type-id. */
20102 clear_decl_specs (&decl_specs
);
20103 decl_specs
.type
= type
;
20104 if (attributes
!= NULL_TREE
)
20106 decl_specs
.attributes
= attributes
;
20107 set_and_check_decl_spec_loc (&decl_specs
,
20111 set_and_check_decl_spec_loc (&decl_specs
,
20114 set_and_check_decl_spec_loc (&decl_specs
,
20117 decl_specs
.locations
[ds_type_spec
] = type_location
;
20119 if (parser
->num_template_parameter_lists
20120 && !cp_parser_check_template_parameters (parser
,
20121 /*num_templates=*/0,
20122 /*template_id*/false,
20124 /*declarator=*/NULL
))
20125 return error_mark_node
;
20127 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
20129 member_p
= at_class_scope_p ();
20131 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
20132 NULL_TREE
, attributes
);
20134 decl
= start_decl (declarator
, &decl_specs
, 0,
20135 attributes
, NULL_TREE
, &pushed_scope
);
20136 if (decl
== error_mark_node
)
20139 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
20142 pop_scope (pushed_scope
);
20144 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20145 added into the symbol table; otherwise, return the TYPE_DECL. */
20146 if (DECL_LANG_SPECIFIC (decl
)
20147 && DECL_TEMPLATE_INFO (decl
)
20148 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
20150 decl
= DECL_TI_TEMPLATE (decl
);
20152 check_member_template (decl
);
20158 /* Parse a using-directive.
20161 using namespace :: [opt] nested-name-specifier [opt]
20162 namespace-name ; */
20165 cp_parser_using_directive (cp_parser
* parser
)
20167 tree namespace_decl
;
20170 /* Look for the `using' keyword. */
20171 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20172 /* And the `namespace' keyword. */
20173 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
20174 /* Look for the optional `::' operator. */
20175 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20176 /* And the optional nested-name-specifier. */
20177 cp_parser_nested_name_specifier_opt (parser
,
20178 /*typename_keyword_p=*/false,
20179 /*check_dependency_p=*/true,
20181 /*is_declaration=*/true);
20182 /* Get the namespace being used. */
20183 namespace_decl
= cp_parser_namespace_name (parser
);
20184 cp_warn_deprecated_use_scopes (namespace_decl
);
20185 /* And any specified attributes. */
20186 attribs
= cp_parser_attributes_opt (parser
);
20188 /* Update the symbol table. */
20189 finish_using_directive (namespace_decl
, attribs
);
20191 /* Look for the final `;'. */
20192 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20195 /* Parse an asm-definition.
20202 asm-qualifier-list:
20204 asm-qualifier-list asm-qualifier
20207 asm ( string-literal ) ;
20212 asm asm-qualifier-list [opt] ( string-literal ) ;
20213 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20214 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20215 : asm-operand-list [opt] ) ;
20216 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20217 : asm-operand-list [opt]
20218 : asm-clobber-list [opt] ) ;
20219 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20220 : asm-clobber-list [opt]
20221 : asm-goto-list ) ;
20223 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20224 contains goto, and is the only allowed form in that case. No duplicates are
20225 allowed in an asm-qualifier-list. */
20228 cp_parser_asm_definition (cp_parser
* parser
)
20231 tree outputs
= NULL_TREE
;
20232 tree inputs
= NULL_TREE
;
20233 tree clobbers
= NULL_TREE
;
20234 tree labels
= NULL_TREE
;
20236 bool extended_p
= false;
20237 bool invalid_inputs_p
= false;
20238 bool invalid_outputs_p
= false;
20239 required_token missing
= RT_NONE
;
20240 location_t asm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20242 /* Look for the `asm' keyword. */
20243 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
20245 /* In C++20, unevaluated inline assembly is permitted in constexpr
20247 if (parser
->in_function_body
20248 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
20249 && (cxx_dialect
< cxx20
))
20250 pedwarn (asm_loc
, 0, "%<asm%> in %<constexpr%> function only available "
20251 "with %<-std=c++20%> or %<-std=gnu++20%>");
20253 /* Handle the asm-qualifier-list. */
20254 location_t volatile_loc
= UNKNOWN_LOCATION
;
20255 location_t inline_loc
= UNKNOWN_LOCATION
;
20256 location_t goto_loc
= UNKNOWN_LOCATION
;
20257 location_t first_loc
= UNKNOWN_LOCATION
;
20259 if (cp_parser_allow_gnu_extensions_p (parser
))
20262 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20263 location_t loc
= token
->location
;
20264 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
20269 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20271 inform (volatile_loc
, "first seen here");
20275 if (!parser
->in_function_body
)
20276 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
20277 "outside of function body", token
->u
.value
);
20278 volatile_loc
= loc
;
20280 cp_lexer_consume_token (parser
->lexer
);
20286 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20288 inform (inline_loc
, "first seen here");
20294 cp_lexer_consume_token (parser
->lexer
);
20300 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20302 inform (goto_loc
, "first seen here");
20308 cp_lexer_consume_token (parser
->lexer
);
20313 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
20314 cp_lexer_consume_token (parser
->lexer
);
20323 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
20324 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
20325 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
20327 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
20329 error_at (first_loc
, "%<asm%> qualifier outside of function body");
20330 inline_p
= goto_p
= false;
20333 /* Look for the opening `('. */
20334 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
20336 /* Look for the string. */
20337 string
= cp_parser_string_literal (parser
, false, false);
20338 if (string
== error_mark_node
)
20340 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20341 /*consume_paren=*/true);
20345 /* If we're allowing GNU extensions, check for the extended assembly
20346 syntax. Unfortunately, the `:' tokens need not be separated by
20347 a space in C, and so, for compatibility, we tolerate that here
20348 too. Doing that means that we have to treat the `::' operator as
20350 if (cp_parser_allow_gnu_extensions_p (parser
)
20351 && parser
->in_function_body
20352 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
20353 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
20355 bool inputs_p
= false;
20356 bool clobbers_p
= false;
20357 bool labels_p
= false;
20359 /* The extended syntax was used. */
20362 /* Look for outputs. */
20363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20365 /* Consume the `:'. */
20366 cp_lexer_consume_token (parser
->lexer
);
20367 /* Parse the output-operands. */
20368 if (cp_lexer_next_token_is_not (parser
->lexer
,
20370 && cp_lexer_next_token_is_not (parser
->lexer
,
20372 && cp_lexer_next_token_is_not (parser
->lexer
,
20376 outputs
= cp_parser_asm_operand_list (parser
);
20377 if (outputs
== error_mark_node
)
20378 invalid_outputs_p
= true;
20381 /* If the next token is `::', there are no outputs, and the
20382 next token is the beginning of the inputs. */
20383 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20384 /* The inputs are coming next. */
20387 /* Look for inputs. */
20389 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20391 /* Consume the `:' or `::'. */
20392 cp_lexer_consume_token (parser
->lexer
);
20393 /* Parse the output-operands. */
20394 if (cp_lexer_next_token_is_not (parser
->lexer
,
20396 && cp_lexer_next_token_is_not (parser
->lexer
,
20398 && cp_lexer_next_token_is_not (parser
->lexer
,
20401 inputs
= cp_parser_asm_operand_list (parser
);
20402 if (inputs
== error_mark_node
)
20403 invalid_inputs_p
= true;
20406 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20407 /* The clobbers are coming next. */
20410 /* Look for clobbers. */
20412 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20415 /* Consume the `:' or `::'. */
20416 cp_lexer_consume_token (parser
->lexer
);
20417 /* Parse the clobbers. */
20418 if (cp_lexer_next_token_is_not (parser
->lexer
,
20420 && cp_lexer_next_token_is_not (parser
->lexer
,
20422 clobbers
= cp_parser_asm_clobber_list (parser
);
20424 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20425 /* The labels are coming next. */
20428 /* Look for labels. */
20430 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
20433 /* Consume the `:' or `::'. */
20434 cp_lexer_consume_token (parser
->lexer
);
20435 /* Parse the labels. */
20436 labels
= cp_parser_asm_label_list (parser
);
20439 if (goto_p
&& !labels_p
)
20440 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
20443 missing
= RT_COLON_SCOPE
;
20445 /* Look for the closing `)'. */
20446 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
20447 missing
? missing
: RT_CLOSE_PAREN
))
20448 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20449 /*consume_paren=*/true);
20450 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20452 if (!invalid_inputs_p
&& !invalid_outputs_p
)
20454 /* Create the ASM_EXPR. */
20455 if (parser
->in_function_body
)
20457 asm_stmt
= finish_asm_stmt (asm_loc
, volatile_p
, string
, outputs
,
20458 inputs
, clobbers
, labels
, inline_p
);
20459 /* If the extended syntax was not used, mark the ASM_EXPR. */
20462 tree temp
= asm_stmt
;
20463 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
20464 temp
= TREE_OPERAND (temp
, 0);
20466 ASM_INPUT_P (temp
) = 1;
20470 symtab
->finalize_toplevel_asm (string
);
20474 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20475 type that comes from the decl-specifier-seq. */
20478 strip_declarator_types (tree type
, cp_declarator
*declarator
)
20480 for (cp_declarator
*d
= declarator
; d
;)
20490 if (TYPE_PTRMEMFUNC_P (type
))
20491 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
20492 type
= TREE_TYPE (type
);
20500 /* Declarators [gram.dcl.decl] */
20502 /* Parse an init-declarator.
20505 declarator initializer [opt]
20510 declarator asm-specification [opt] attributes [opt] initializer [opt]
20512 function-definition:
20513 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20515 decl-specifier-seq [opt] declarator function-try-block
20519 function-definition:
20520 __extension__ function-definition
20524 function-definition:
20525 decl-specifier-seq [opt] declarator function-transaction-block
20527 The parser flags FLAGS is used to control type-specifier parsing.
20529 The DECL_SPECIFIERS apply to this declarator. Returns a
20530 representation of the entity declared. If MEMBER_P is TRUE, then
20531 this declarator appears in a class scope. The new DECL created by
20532 this declarator is returned.
20534 The CHECKS are access checks that should be performed once we know
20535 what entity is being declared (and, therefore, what classes have
20538 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20539 for a function-definition here as well. If the declarator is a
20540 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20541 be TRUE upon return. By that point, the function-definition will
20542 have been completely parsed.
20544 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20547 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20548 parsed declaration if it is an uninitialized single declarator not followed
20549 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20550 if present, will not be consumed. If returned, this declarator will be
20551 created with SD_INITIALIZED but will not call cp_finish_decl.
20553 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20554 and there is an initializer, the pointed location_t is set to the
20555 location of the '=' or `(', or '{' in C++11 token introducing the
20559 cp_parser_init_declarator (cp_parser
* parser
,
20560 cp_parser_flags flags
,
20561 cp_decl_specifier_seq
*decl_specifiers
,
20562 vec
<deferred_access_check
, va_gc
> *checks
,
20563 bool function_definition_allowed_p
,
20565 int declares_class_or_enum
,
20566 bool* function_definition_p
,
20567 tree
* maybe_range_for_decl
,
20568 location_t
* init_loc
,
20571 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20572 *attributes_start_token
= NULL
;
20573 cp_declarator
*declarator
;
20574 tree prefix_attributes
;
20575 tree attributes
= NULL
;
20576 tree asm_specification
;
20578 tree decl
= NULL_TREE
;
20580 int is_initialized
;
20581 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20582 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20584 enum cpp_ttype initialization_kind
;
20585 bool is_direct_init
= false;
20586 bool is_non_constant_init
;
20587 int ctor_dtor_or_conv_p
;
20588 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20589 tree pushed_scope
= NULL_TREE
;
20590 bool range_for_decl_p
= false;
20591 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20592 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20594 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_consteval
))
20595 flags
|= CP_PARSER_FLAGS_CONSTEVAL
;
20597 /* Gather the attributes that were provided with the
20598 decl-specifiers. */
20599 prefix_attributes
= decl_specifiers
->attributes
;
20601 /* Assume that this is not the declarator for a function
20603 if (function_definition_p
)
20604 *function_definition_p
= false;
20606 /* Default arguments are only permitted for function parameters. */
20607 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20608 parser
->default_arg_ok_p
= false;
20610 /* Defer access checks while parsing the declarator; we cannot know
20611 what names are accessible until we know what is being
20613 resume_deferring_access_checks ();
20615 token
= cp_lexer_peek_token (parser
->lexer
);
20617 /* Parse the declarator. */
20619 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20620 flags
, &ctor_dtor_or_conv_p
,
20621 /*parenthesized_p=*/NULL
,
20622 member_p
, friend_p
, /*static_p=*/false);
20623 /* Gather up the deferred checks. */
20624 stop_deferring_access_checks ();
20626 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20628 /* If the DECLARATOR was erroneous, there's no need to go
20630 if (declarator
== cp_error_declarator
)
20631 return error_mark_node
;
20633 /* Check that the number of template-parameter-lists is OK. */
20634 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20636 return error_mark_node
;
20638 if (declares_class_or_enum
& 2)
20639 cp_parser_check_for_definition_in_return_type (declarator
,
20640 decl_specifiers
->type
,
20641 decl_specifiers
->locations
[ds_type_spec
]);
20643 /* Figure out what scope the entity declared by the DECLARATOR is
20644 located in. `grokdeclarator' sometimes changes the scope, so
20645 we compute it now. */
20646 scope
= get_scope_of_declarator (declarator
);
20648 /* Perform any lookups in the declared type which were thought to be
20649 dependent, but are not in the scope of the declarator. */
20650 decl_specifiers
->type
20651 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20653 /* If we're allowing GNU extensions, look for an
20654 asm-specification. */
20655 if (cp_parser_allow_gnu_extensions_p (parser
))
20657 /* Look for an asm-specification. */
20658 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20659 asm_specification
= cp_parser_asm_specification_opt (parser
);
20662 asm_specification
= NULL_TREE
;
20664 /* Look for attributes. */
20665 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20666 attributes
= cp_parser_attributes_opt (parser
);
20668 /* Peek at the next token. */
20669 token
= cp_lexer_peek_token (parser
->lexer
);
20671 bool bogus_implicit_tmpl
= false;
20673 if (function_declarator_p (declarator
))
20675 /* Handle C++17 deduction guides. */
20676 if (!decl_specifiers
->type
20677 && ctor_dtor_or_conv_p
<= 0
20678 && cxx_dialect
>= cxx17
)
20680 cp_declarator
*id
= get_id_declarator (declarator
);
20681 tree name
= id
->u
.id
.unqualified_name
;
20682 parser
->scope
= id
->u
.id
.qualifying_scope
;
20683 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20685 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20686 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20688 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20689 id
->u
.id
.sfk
= sfk_deduction_guide
;
20690 ctor_dtor_or_conv_p
= 1;
20694 /* Check to see if the token indicates the start of a
20695 function-definition. */
20696 if (cp_parser_token_starts_function_definition_p (token
))
20698 if (!function_definition_allowed_p
)
20700 /* If a function-definition should not appear here, issue an
20702 cp_parser_error (parser
,
20703 "a function-definition is not allowed here");
20704 return error_mark_node
;
20707 location_t func_brace_location
20708 = cp_lexer_peek_token (parser
->lexer
)->location
;
20710 /* Neither attributes nor an asm-specification are allowed
20711 on a function-definition. */
20712 if (asm_specification
)
20713 error_at (asm_spec_start_token
->location
,
20714 "an %<asm%> specification is not allowed "
20715 "on a function-definition");
20717 error_at (attributes_start_token
->location
,
20718 "attributes are not allowed "
20719 "on a function-definition");
20720 /* This is a function-definition. */
20721 *function_definition_p
= true;
20723 /* Parse the function definition. */
20725 decl
= cp_parser_save_member_function_body (parser
,
20728 prefix_attributes
);
20731 (cp_parser_function_definition_from_specifiers_and_declarator
20732 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20734 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20736 /* This is where the prologue starts... */
20737 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20738 = func_brace_location
;
20744 else if (parser
->fully_implicit_function_template_p
)
20746 /* A non-template declaration involving a function parameter list
20747 containing an implicit template parameter will be made into a
20748 template. If the resulting declaration is not going to be an
20749 actual function then finish the template scope here to prevent it.
20750 An error message will be issued once we have a decl to talk about.
20752 FIXME probably we should do type deduction rather than create an
20753 implicit template, but the standard currently doesn't allow it. */
20754 bogus_implicit_tmpl
= true;
20755 finish_fully_implicit_template (parser
, NULL_TREE
);
20760 Only in function declarations for constructors, destructors, type
20761 conversions, and deduction guides can the decl-specifier-seq be omitted.
20763 We explicitly postpone this check past the point where we handle
20764 function-definitions because we tolerate function-definitions
20765 that are missing their return types in some modes. */
20766 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20768 cp_parser_error (parser
,
20769 "expected constructor, destructor, or type conversion");
20770 return error_mark_node
;
20773 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20774 if (token
->type
== CPP_EQ
20775 || token
->type
== CPP_OPEN_PAREN
20776 || token
->type
== CPP_OPEN_BRACE
)
20778 is_initialized
= SD_INITIALIZED
;
20779 initialization_kind
= token
->type
;
20780 if (maybe_range_for_decl
)
20781 *maybe_range_for_decl
= error_mark_node
;
20782 tmp_init_loc
= token
->location
;
20783 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20784 *init_loc
= tmp_init_loc
;
20786 if (token
->type
== CPP_EQ
20787 && function_declarator_p (declarator
))
20789 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20790 if (t2
->keyword
== RID_DEFAULT
)
20791 is_initialized
= SD_DEFAULTED
;
20792 else if (t2
->keyword
== RID_DELETE
)
20793 is_initialized
= SD_DELETED
;
20798 /* If the init-declarator isn't initialized and isn't followed by a
20799 `,' or `;', it's not a valid init-declarator. */
20800 if (token
->type
!= CPP_COMMA
20801 && token
->type
!= CPP_SEMICOLON
)
20803 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20804 range_for_decl_p
= true;
20807 if (!maybe_range_for_decl
)
20808 cp_parser_error (parser
, "expected initializer");
20809 return error_mark_node
;
20812 is_initialized
= SD_UNINITIALIZED
;
20813 initialization_kind
= CPP_EOF
;
20816 /* Because start_decl has side-effects, we should only call it if we
20817 know we're going ahead. By this point, we know that we cannot
20818 possibly be looking at any other construct. */
20819 cp_parser_commit_to_tentative_parse (parser
);
20821 /* Enter the newly declared entry in the symbol table. If we're
20822 processing a declaration in a class-specifier, we wait until
20823 after processing the initializer. */
20826 if (parser
->in_unbraced_linkage_specification_p
)
20827 decl_specifiers
->storage_class
= sc_extern
;
20828 decl
= start_decl (declarator
, decl_specifiers
,
20829 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20830 attributes
, prefix_attributes
, &pushed_scope
);
20831 cp_finalize_omp_declare_simd (parser
, decl
);
20832 cp_finalize_oacc_routine (parser
, decl
, false);
20833 /* Adjust location of decl if declarator->id_loc is more appropriate:
20834 set, and decl wasn't merged with another decl, in which case its
20835 location would be different from input_location, and more accurate. */
20837 && declarator
->id_loc
!= UNKNOWN_LOCATION
20838 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20839 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20842 /* Enter the SCOPE. That way unqualified names appearing in the
20843 initializer will be looked up in SCOPE. */
20844 pushed_scope
= push_scope (scope
);
20846 /* Perform deferred access control checks, now that we know in which
20847 SCOPE the declared entity resides. */
20848 if (!member_p
&& decl
)
20850 tree saved_current_function_decl
= NULL_TREE
;
20852 /* If the entity being declared is a function, pretend that we
20853 are in its scope. If it is a `friend', it may have access to
20854 things that would not otherwise be accessible. */
20855 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20857 saved_current_function_decl
= current_function_decl
;
20858 current_function_decl
= decl
;
20861 /* Perform access checks for template parameters. */
20862 cp_parser_perform_template_parameter_access_checks (checks
);
20864 /* Perform the access control checks for the declarator and the
20865 decl-specifiers. */
20866 perform_deferred_access_checks (tf_warning_or_error
);
20868 /* Restore the saved value. */
20869 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20870 current_function_decl
= saved_current_function_decl
;
20873 /* Parse the initializer. */
20874 initializer
= NULL_TREE
;
20875 is_direct_init
= false;
20876 is_non_constant_init
= true;
20877 if (is_initialized
)
20879 if (function_declarator_p (declarator
))
20881 if (initialization_kind
== CPP_EQ
)
20882 initializer
= cp_parser_pure_specifier (parser
);
20885 /* If the declaration was erroneous, we don't really
20886 know what the user intended, so just silently
20887 consume the initializer. */
20888 if (decl
!= error_mark_node
)
20889 error_at (tmp_init_loc
, "initializer provided for function");
20890 cp_parser_skip_to_closing_parenthesis (parser
,
20891 /*recovering=*/true,
20892 /*or_comma=*/false,
20893 /*consume_paren=*/true);
20898 /* We want to record the extra mangling scope for in-class
20899 initializers of class members and initializers of static
20900 data member templates and namespace-scope initializers.
20901 The former involves deferring parsing of the initializer
20902 until end of class as with default arguments. So right
20903 here we only handle the latter two. */
20904 bool has_lambda_scope
= false;
20906 if (decl
!= error_mark_node
20908 && (processing_template_decl
|| DECL_NAMESPACE_SCOPE_P (decl
)))
20909 has_lambda_scope
= true;
20911 if (has_lambda_scope
)
20912 start_lambda_scope (decl
);
20913 initializer
= cp_parser_initializer (parser
,
20915 &is_non_constant_init
);
20916 if (has_lambda_scope
)
20917 finish_lambda_scope ();
20918 if (initializer
== error_mark_node
)
20919 cp_parser_skip_to_end_of_statement (parser
);
20923 /* The old parser allows attributes to appear after a parenthesized
20924 initializer. Mark Mitchell proposed removing this functionality
20925 on the GCC mailing lists on 2002-08-13. This parser accepts the
20926 attributes -- but ignores them. Made a permerror in GCC 8. */
20927 if (cp_parser_allow_gnu_extensions_p (parser
)
20928 && initialization_kind
== CPP_OPEN_PAREN
20929 && cp_parser_attributes_opt (parser
)
20930 && permerror (input_location
,
20931 "attributes after parenthesized initializer ignored"))
20934 if (flag_permissive
&& !hint
)
20937 inform (input_location
,
20938 "this flexibility is deprecated and will be removed");
20942 /* And now complain about a non-function implicit template. */
20943 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20944 error_at (DECL_SOURCE_LOCATION (decl
),
20945 "non-function %qD declared as implicit template", decl
);
20947 /* For an in-class declaration, use `grokfield' to create the
20953 pop_scope (pushed_scope
);
20954 pushed_scope
= NULL_TREE
;
20956 decl
= grokfield (declarator
, decl_specifiers
,
20957 initializer
, !is_non_constant_init
,
20958 /*asmspec=*/NULL_TREE
,
20959 attr_chainon (attributes
, prefix_attributes
));
20960 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20961 cp_parser_save_default_args (parser
, decl
);
20962 cp_finalize_omp_declare_simd (parser
, decl
);
20963 cp_finalize_oacc_routine (parser
, decl
, false);
20966 /* Finish processing the declaration. But, skip member
20968 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20970 int cf
= (decl_spec_seq_has_spec_p (decl_specifiers
, ds_constinit
)
20971 ? LOOKUP_CONSTINIT
: 0);
20972 cp_finish_decl (decl
,
20973 initializer
, !is_non_constant_init
,
20975 /* If the initializer is in parentheses, then this is
20976 a direct-initialization, which means that an
20977 `explicit' constructor is OK. Otherwise, an
20978 `explicit' constructor cannot be used. */
20979 ((is_direct_init
|| !is_initialized
)
20980 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
) | cf
);
20982 else if ((cxx_dialect
!= cxx98
) && friend_p
20983 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20984 /* Core issue #226 (C++0x only): A default template-argument
20985 shall not be specified in a friend class template
20987 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20988 /*is_partial=*/false, /*is_friend_decl=*/1);
20990 if (!friend_p
&& pushed_scope
)
20991 pop_scope (pushed_scope
);
20993 if (function_declarator_p (declarator
)
20994 && parser
->fully_implicit_function_template_p
)
20997 decl
= finish_fully_implicit_template (parser
, decl
);
20999 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
21002 if (auto_result
&& is_initialized
&& decl_specifiers
->type
21003 && type_uses_auto (decl_specifiers
->type
))
21004 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
21009 /* Parse a declarator.
21013 ptr-operator declarator
21015 abstract-declarator:
21016 ptr-operator abstract-declarator [opt]
21017 direct-abstract-declarator
21022 attributes [opt] direct-declarator
21023 attributes [opt] ptr-operator declarator
21025 abstract-declarator:
21026 attributes [opt] ptr-operator abstract-declarator [opt]
21027 attributes [opt] direct-abstract-declarator
21029 The parser flags FLAGS is used to control type-specifier parsing.
21031 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21032 detect constructors, destructors, deduction guides, or conversion operators.
21033 It is set to -1 if the declarator is a name, and +1 if it is a
21034 function. Otherwise it is set to zero. Usually you just want to
21035 test for >0, but internally the negative value is used.
21037 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21038 a decl-specifier-seq unless it declares a constructor, destructor,
21039 or conversion. It might seem that we could check this condition in
21040 semantic analysis, rather than parsing, but that makes it difficult
21041 to handle something like `f()'. We want to notice that there are
21042 no decl-specifiers, and therefore realize that this is an
21043 expression, not a declaration.)
21045 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21046 the declarator is a direct-declarator of the form "(...)".
21048 MEMBER_P is true iff this declarator is a member-declarator.
21050 FRIEND_P is true iff this declarator is a friend.
21052 STATIC_P is true iff the keyword static was seen. */
21054 static cp_declarator
*
21055 cp_parser_declarator (cp_parser
* parser
,
21056 cp_parser_declarator_kind dcl_kind
,
21057 cp_parser_flags flags
,
21058 int* ctor_dtor_or_conv_p
,
21059 bool* parenthesized_p
,
21060 bool member_p
, bool friend_p
, bool static_p
)
21062 cp_declarator
*declarator
;
21063 enum tree_code code
;
21064 cp_cv_quals cv_quals
;
21066 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
21068 /* Assume this is not a constructor, destructor, or type-conversion
21070 if (ctor_dtor_or_conv_p
)
21071 *ctor_dtor_or_conv_p
= 0;
21073 if (cp_parser_allow_gnu_extensions_p (parser
))
21074 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
21076 /* Check for the ptr-operator production. */
21077 cp_parser_parse_tentatively (parser
);
21078 /* Parse the ptr-operator. */
21079 code
= cp_parser_ptr_operator (parser
,
21084 /* If that worked, then we have a ptr-operator. */
21085 if (cp_parser_parse_definitely (parser
))
21087 /* If a ptr-operator was found, then this declarator was not
21089 if (parenthesized_p
)
21090 *parenthesized_p
= true;
21091 /* The dependent declarator is optional if we are parsing an
21092 abstract-declarator. */
21093 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21094 cp_parser_parse_tentatively (parser
);
21096 /* Parse the dependent declarator. */
21097 declarator
= cp_parser_declarator (parser
, dcl_kind
,
21098 CP_PARSER_FLAGS_NONE
,
21099 /*ctor_dtor_or_conv_p=*/NULL
,
21100 /*parenthesized_p=*/NULL
,
21101 /*member_p=*/false,
21102 friend_p
, /*static_p=*/false);
21104 /* If we are parsing an abstract-declarator, we must handle the
21105 case where the dependent declarator is absent. */
21106 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
21107 && !cp_parser_parse_definitely (parser
))
21110 declarator
= cp_parser_make_indirect_declarator
21111 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
21113 /* Everything else is a direct-declarator. */
21116 if (parenthesized_p
)
21117 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
21119 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
21120 flags
, ctor_dtor_or_conv_p
,
21121 member_p
, friend_p
, static_p
);
21124 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
21125 declarator
->attributes
= gnu_attributes
;
21129 /* Parse a direct-declarator or direct-abstract-declarator.
21133 direct-declarator ( parameter-declaration-clause )
21134 cv-qualifier-seq [opt]
21135 ref-qualifier [opt]
21136 exception-specification [opt]
21137 direct-declarator [ constant-expression [opt] ]
21140 direct-abstract-declarator:
21141 direct-abstract-declarator [opt]
21142 ( parameter-declaration-clause )
21143 cv-qualifier-seq [opt]
21144 ref-qualifier [opt]
21145 exception-specification [opt]
21146 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21147 ( abstract-declarator )
21149 Returns a representation of the declarator. DCL_KIND is
21150 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21151 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21152 we are parsing a direct-declarator. It is
21153 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21154 of ambiguity we prefer an abstract declarator, as per
21156 The parser flags FLAGS is used to control type-specifier parsing.
21157 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21158 as for cp_parser_declarator. */
21160 static cp_declarator
*
21161 cp_parser_direct_declarator (cp_parser
* parser
,
21162 cp_parser_declarator_kind dcl_kind
,
21163 cp_parser_flags flags
,
21164 int* ctor_dtor_or_conv_p
,
21165 bool member_p
, bool friend_p
, bool static_p
)
21168 cp_declarator
*declarator
= NULL
;
21169 tree scope
= NULL_TREE
;
21170 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21171 bool saved_in_declarator_p
= parser
->in_declarator_p
;
21173 tree pushed_scope
= NULL_TREE
;
21174 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
21178 /* Peek at the next token. */
21179 token
= cp_lexer_peek_token (parser
->lexer
);
21180 if (token
->type
== CPP_OPEN_PAREN
)
21182 /* This is either a parameter-declaration-clause, or a
21183 parenthesized declarator. When we know we are parsing a
21184 named declarator, it must be a parenthesized declarator
21185 if FIRST is true. For instance, `(int)' is a
21186 parameter-declaration-clause, with an omitted
21187 direct-abstract-declarator. But `((*))', is a
21188 parenthesized abstract declarator. Finally, when T is a
21189 template parameter `(T)' is a
21190 parameter-declaration-clause, and not a parenthesized
21193 We first try and parse a parameter-declaration-clause,
21194 and then try a nested declarator (if FIRST is true).
21196 It is not an error for it not to be a
21197 parameter-declaration-clause, even when FIRST is
21203 The first is the declaration of a function while the
21204 second is the definition of a variable, including its
21207 Having seen only the parenthesis, we cannot know which of
21208 these two alternatives should be selected. Even more
21209 complex are examples like:
21214 The former is a function-declaration; the latter is a
21215 variable initialization.
21217 Thus again, we try a parameter-declaration-clause, and if
21218 that fails, we back out and return. */
21220 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21223 bool is_declarator
= false;
21227 /* In a member-declarator, the only valid interpretation
21228 of a parenthesis is the start of a
21229 parameter-declaration-clause. (It is invalid to
21230 initialize a static data member with a parenthesized
21231 initializer; only the "=" form of initialization is
21234 cp_parser_parse_tentatively (parser
);
21236 /* Consume the `('. */
21237 matching_parens parens
;
21238 parens
.consume_open (parser
);
21241 /* If this is going to be an abstract declarator, we're
21242 in a declarator and we can't have default args. */
21243 parser
->default_arg_ok_p
= false;
21244 parser
->in_declarator_p
= true;
21247 begin_scope (sk_function_parms
, NULL_TREE
);
21249 /* Signal we are in the immediate function context. */
21250 if (flags
& CP_PARSER_FLAGS_CONSTEVAL
)
21251 current_binding_level
->immediate_fn_ctx_p
= true;
21253 /* Parse the parameter-declaration-clause. */
21255 = cp_parser_parameter_declaration_clause (parser
, flags
);
21257 /* Consume the `)'. */
21258 parens
.require_close (parser
);
21260 /* If all went well, parse the cv-qualifier-seq,
21261 ref-qualifier and the exception-specification. */
21262 if (member_p
|| cp_parser_parse_definitely (parser
))
21264 cp_cv_quals cv_quals
;
21265 cp_virt_specifiers virt_specifiers
;
21266 cp_ref_qualifier ref_qual
;
21267 tree exception_specification
;
21270 bool memfn
= (member_p
|| (pushed_scope
21271 && CLASS_TYPE_P (pushed_scope
)));
21272 unsigned char local_variables_forbidden_p
21273 = parser
->local_variables_forbidden_p
;
21274 /* 'this' is not allowed in static member functions. */
21275 if (static_p
|| friend_p
)
21276 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
21278 is_declarator
= true;
21280 if (ctor_dtor_or_conv_p
)
21281 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
21284 /* Parse the cv-qualifier-seq. */
21285 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21286 /* Parse the ref-qualifier. */
21287 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
21288 /* Parse the tx-qualifier. */
21289 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
21291 tree save_ccp
= current_class_ptr
;
21292 tree save_ccr
= current_class_ref
;
21294 /* DR 1207: 'this' is in scope after the cv-quals. */
21295 inject_this_parameter (current_class_type
, cv_quals
);
21297 /* If it turned out that this is e.g. a pointer to a
21298 function, we don't want to delay noexcept parsing. */
21299 if (declarator
== NULL
|| declarator
->kind
!= cdk_id
)
21300 flags
&= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
21302 /* Parse the exception-specification. */
21303 exception_specification
21304 = cp_parser_exception_specification_opt (parser
,
21307 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21309 /* In here, we handle cases where attribute is used after
21310 the function declaration. For example:
21311 void func (int x) __attribute__((vector(..))); */
21312 tree gnu_attrs
= NULL_TREE
;
21313 tree requires_clause
= NULL_TREE
;
21314 late_return
= (cp_parser_late_return_type_opt
21315 (parser
, declarator
, requires_clause
));
21317 /* Parse the virt-specifier-seq. */
21318 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
21320 /* Create the function-declarator. */
21321 declarator
= make_call_declarator (declarator
,
21327 exception_specification
,
21330 declarator
->std_attributes
= attrs
;
21331 declarator
->attributes
= gnu_attrs
;
21332 /* Any subsequent parameter lists are to do with
21333 return type, so are not those of the declared
21335 parser
->default_arg_ok_p
= false;
21337 current_class_ptr
= save_ccp
;
21338 current_class_ref
= save_ccr
;
21340 /* Restore the state of local_variables_forbidden_p. */
21341 parser
->local_variables_forbidden_p
21342 = local_variables_forbidden_p
;
21345 /* Remove the function parms from scope. */
21346 pop_bindings_and_leave_scope ();
21349 /* Repeat the main loop. */
21353 /* If this is the first, we can try a parenthesized
21357 bool saved_in_type_id_in_expr_p
;
21359 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21360 parser
->in_declarator_p
= saved_in_declarator_p
;
21362 open_paren
= token
;
21363 /* Consume the `('. */
21364 matching_parens parens
;
21365 parens
.consume_open (parser
);
21366 /* Parse the nested declarator. */
21367 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
21368 parser
->in_type_id_in_expr_p
= true;
21370 = cp_parser_declarator (parser
, dcl_kind
, flags
,
21371 ctor_dtor_or_conv_p
,
21372 /*parenthesized_p=*/NULL
,
21373 member_p
, friend_p
,
21374 /*static_p=*/false);
21375 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
21377 /* Expect a `)'. */
21378 close_paren
= cp_lexer_peek_token (parser
->lexer
);
21379 if (!parens
.require_close (parser
))
21380 declarator
= cp_error_declarator
;
21381 if (declarator
== cp_error_declarator
)
21384 goto handle_declarator
;
21386 /* Otherwise, we must be done. */
21390 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21391 && token
->type
== CPP_OPEN_SQUARE
21392 && !cp_next_tokens_can_be_attribute_p (parser
))
21394 /* Parse an array-declarator. */
21395 tree bounds
, attrs
;
21397 if (ctor_dtor_or_conv_p
)
21398 *ctor_dtor_or_conv_p
= 0;
21402 parser
->default_arg_ok_p
= false;
21403 parser
->in_declarator_p
= true;
21404 /* Consume the `['. */
21405 cp_lexer_consume_token (parser
->lexer
);
21406 /* Peek at the next token. */
21407 token
= cp_lexer_peek_token (parser
->lexer
);
21408 /* If the next token is `]', then there is no
21409 constant-expression. */
21410 if (token
->type
!= CPP_CLOSE_SQUARE
)
21412 bool non_constant_p
;
21414 = cp_parser_constant_expression (parser
,
21415 /*allow_non_constant=*/true,
21417 if (!non_constant_p
)
21419 else if (error_operand_p (bounds
))
21420 /* Already gave an error. */;
21421 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
21422 /* Let compute_array_index_type diagnose this. */;
21423 else if (!parser
->in_function_body
21424 || current_binding_level
->kind
== sk_function_parms
)
21426 /* Normally, the array bound must be an integral constant
21427 expression. However, as an extension, we allow VLAs
21428 in function scopes as long as they aren't part of a
21429 parameter declaration. */
21430 cp_parser_error (parser
,
21431 "array bound is not an integer constant");
21432 bounds
= error_mark_node
;
21434 else if (processing_template_decl
21435 && !type_dependent_expression_p (bounds
))
21437 /* Remember this wasn't a constant-expression. */
21438 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
21439 TREE_SIDE_EFFECTS (bounds
) = 1;
21443 bounds
= NULL_TREE
;
21444 /* Look for the closing `]'. */
21445 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21447 declarator
= cp_error_declarator
;
21451 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21452 declarator
= make_array_declarator (declarator
, bounds
);
21453 declarator
->std_attributes
= attrs
;
21455 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
21458 tree qualifying_scope
;
21459 tree unqualified_name
;
21461 special_function_kind sfk
;
21463 bool pack_expansion_p
= false;
21464 cp_token
*declarator_id_start_token
;
21466 /* Parse a declarator-id */
21467 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
21470 cp_parser_parse_tentatively (parser
);
21472 /* If we see an ellipsis, we should be looking at a
21474 if (token
->type
== CPP_ELLIPSIS
)
21476 /* Consume the `...' */
21477 cp_lexer_consume_token (parser
->lexer
);
21479 pack_expansion_p
= true;
21483 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
21485 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
21486 qualifying_scope
= parser
->scope
;
21491 if (!unqualified_name
&& pack_expansion_p
)
21493 /* Check whether an error occurred. */
21494 okay
= !cp_parser_error_occurred (parser
);
21496 /* We already consumed the ellipsis to mark a
21497 parameter pack, but we have no way to report it,
21498 so abort the tentative parse. We will be exiting
21499 immediately anyway. */
21500 cp_parser_abort_tentative_parse (parser
);
21503 okay
= cp_parser_parse_definitely (parser
);
21506 unqualified_name
= error_mark_node
;
21507 else if (unqualified_name
21508 && (qualifying_scope
21509 || (!identifier_p (unqualified_name
))))
21511 cp_parser_error (parser
, "expected unqualified-id");
21512 unqualified_name
= error_mark_node
;
21516 if (!unqualified_name
)
21518 if (unqualified_name
== error_mark_node
)
21520 declarator
= cp_error_declarator
;
21521 pack_expansion_p
= false;
21522 declarator
->parameter_pack_p
= false;
21526 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21528 if (qualifying_scope
&& at_namespace_scope_p ()
21529 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
21531 /* In the declaration of a member of a template class
21532 outside of the class itself, the SCOPE will sometimes
21533 be a TYPENAME_TYPE. For example, given:
21535 template <typename T>
21536 int S<T>::R::i = 3;
21538 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21539 this context, we must resolve S<T>::R to an ordinary
21540 type, rather than a typename type.
21542 The reason we normally avoid resolving TYPENAME_TYPEs
21543 is that a specialization of `S' might render
21544 `S<T>::R' not a type. However, if `S' is
21545 specialized, then this `i' will not be used, so there
21546 is no harm in resolving the types here. */
21549 /* Resolve the TYPENAME_TYPE. */
21550 type
= resolve_typename_type (qualifying_scope
,
21551 /*only_current_p=*/false);
21552 /* If that failed, the declarator is invalid. */
21553 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21555 if (typedef_variant_p (type
))
21556 error_at (declarator_id_start_token
->location
,
21557 "cannot define member of dependent typedef "
21560 error_at (declarator_id_start_token
->location
,
21561 "%<%T::%E%> is not a type",
21562 TYPE_CONTEXT (qualifying_scope
),
21563 TYPE_IDENTIFIER (qualifying_scope
));
21565 qualifying_scope
= type
;
21570 if (unqualified_name
)
21574 if (qualifying_scope
21575 && CLASS_TYPE_P (qualifying_scope
))
21576 class_type
= qualifying_scope
;
21578 class_type
= current_class_type
;
21580 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21582 tree name_type
= TREE_TYPE (unqualified_name
);
21584 if (!class_type
|| !same_type_p (name_type
, class_type
))
21586 /* We do not attempt to print the declarator
21587 here because we do not have enough
21588 information about its original syntactic
21590 cp_parser_error (parser
, "invalid declarator");
21591 declarator
= cp_error_declarator
;
21594 else if (qualifying_scope
21595 && CLASSTYPE_USE_TEMPLATE (name_type
))
21597 error_at (declarator_id_start_token
->location
,
21598 "invalid use of constructor as a template");
21599 inform (declarator_id_start_token
->location
,
21600 "use %<%T::%D%> instead of %<%T::%D%> to "
21601 "name the constructor in a qualified name",
21603 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21604 class_type
, name_type
);
21605 declarator
= cp_error_declarator
;
21608 unqualified_name
= constructor_name (class_type
);
21613 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21614 sfk
= sfk_destructor
;
21615 else if (identifier_p (unqualified_name
)
21616 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21617 sfk
= sfk_conversion
;
21618 else if (/* There's no way to declare a constructor
21619 for an unnamed type, even if the type
21620 got a name for linkage purposes. */
21621 !TYPE_WAS_UNNAMED (class_type
)
21622 /* Handle correctly (c++/19200):
21636 friend void N::S();
21638 && (!friend_p
|| class_type
== qualifying_scope
)
21639 && constructor_name_p (unqualified_name
,
21641 sfk
= sfk_constructor
;
21642 else if (is_overloaded_fn (unqualified_name
)
21643 && DECL_CONSTRUCTOR_P (get_first_fn
21644 (unqualified_name
)))
21645 sfk
= sfk_constructor
;
21647 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21648 *ctor_dtor_or_conv_p
= -1;
21651 declarator
= make_id_declarator (qualifying_scope
,
21653 sfk
, token
->location
);
21654 declarator
->std_attributes
= attrs
;
21655 declarator
->parameter_pack_p
= pack_expansion_p
;
21657 if (pack_expansion_p
)
21658 maybe_warn_variadic_templates ();
21660 /* We're looking for this case in [temp.res]:
21661 A qualified-id is assumed to name a type if [...]
21662 - it is a decl-specifier of the decl-specifier-seq of a
21663 parameter-declaration in a declarator of a function or
21664 function template declaration, ... */
21665 if (cxx_dialect
>= cxx20
21666 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21667 && declarator
->kind
== cdk_id
21668 && !at_class_scope_p ()
21669 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21671 /* ...whose declarator-id is qualified. If it isn't, never
21672 assume the parameters to refer to types. */
21673 if (qualifying_scope
== NULL_TREE
)
21674 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21677 /* Now we have something like
21678 template <typename T> int C::x(S::p);
21679 which can be a function template declaration or a
21680 variable template definition. If name lookup for
21681 the declarator-id C::x finds one or more function
21682 templates, assume S::p to name a type. Otherwise,
21685 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21687 if (!is_overloaded_fn (decl
)
21689 template<typename T>
21690 A<T>::A(T::type) { } */
21691 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
21692 && constructor_name_p (unqualified_name
,
21693 qualifying_scope
)))
21694 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21699 handle_declarator
:;
21700 scope
= get_scope_of_declarator (declarator
);
21703 /* Any names that appear after the declarator-id for a
21704 member are looked up in the containing scope. */
21705 if (at_function_scope_p ())
21707 /* But declarations with qualified-ids can't appear in a
21709 cp_parser_error (parser
, "qualified-id in declaration");
21710 declarator
= cp_error_declarator
;
21713 pushed_scope
= push_scope (scope
);
21715 parser
->in_declarator_p
= true;
21716 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21717 || (declarator
&& declarator
->kind
== cdk_id
))
21718 /* Default args are only allowed on function
21720 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21722 parser
->default_arg_ok_p
= false;
21731 /* For an abstract declarator, we might wind up with nothing at this
21732 point. That's an error; the declarator is not optional. */
21734 cp_parser_error (parser
, "expected declarator");
21735 else if (open_paren
)
21737 /* Record overly parenthesized declarator so we can give a
21738 diagnostic about confusing decl/expr disambiguation. */
21739 if (declarator
->kind
== cdk_array
)
21741 /* If the open and close parens are on different lines, this
21742 is probably a formatting thing, so ignore. */
21743 expanded_location open
= expand_location (open_paren
->location
);
21744 expanded_location close
= expand_location (close_paren
->location
);
21745 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21749 declarator
->parenthesized
= open_paren
->location
;
21752 /* If we entered a scope, we must exit it now. */
21754 pop_scope (pushed_scope
);
21756 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21757 parser
->in_declarator_p
= saved_in_declarator_p
;
21762 /* Parse a ptr-operator.
21765 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21766 * cv-qualifier-seq [opt]
21768 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21769 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21774 & cv-qualifier-seq [opt]
21776 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21777 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21778 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21779 filled in with the TYPE containing the member. *CV_QUALS is
21780 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21781 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21782 Note that the tree codes returned by this function have nothing
21783 to do with the types of trees that will be eventually be created
21784 to represent the pointer or reference type being parsed. They are
21785 just constants with suggestive names. */
21786 static enum tree_code
21787 cp_parser_ptr_operator (cp_parser
* parser
,
21789 cp_cv_quals
*cv_quals
,
21792 enum tree_code code
= ERROR_MARK
;
21794 tree attrs
= NULL_TREE
;
21796 /* Assume that it's not a pointer-to-member. */
21798 /* And that there are no cv-qualifiers. */
21799 *cv_quals
= TYPE_UNQUALIFIED
;
21801 /* Peek at the next token. */
21802 token
= cp_lexer_peek_token (parser
->lexer
);
21804 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21805 if (token
->type
== CPP_MULT
)
21806 code
= INDIRECT_REF
;
21807 else if (token
->type
== CPP_AND
)
21809 else if ((cxx_dialect
!= cxx98
) &&
21810 token
->type
== CPP_AND_AND
) /* C++0x only */
21811 code
= NON_LVALUE_EXPR
;
21813 if (code
!= ERROR_MARK
)
21815 /* Consume the `*', `&' or `&&'. */
21816 cp_lexer_consume_token (parser
->lexer
);
21818 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21819 `&', if we are allowing GNU extensions. (The only qualifier
21820 that can legally appear after `&' is `restrict', but that is
21821 enforced during semantic analysis. */
21822 if (code
== INDIRECT_REF
21823 || cp_parser_allow_gnu_extensions_p (parser
))
21824 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21826 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21827 if (attributes
!= NULL
)
21828 *attributes
= attrs
;
21832 /* Try the pointer-to-member case. */
21833 cp_parser_parse_tentatively (parser
);
21834 /* Look for the optional `::' operator. */
21835 cp_parser_global_scope_opt (parser
,
21836 /*current_scope_valid_p=*/false);
21837 /* Look for the nested-name specifier. */
21838 token
= cp_lexer_peek_token (parser
->lexer
);
21839 cp_parser_nested_name_specifier (parser
,
21840 /*typename_keyword_p=*/false,
21841 /*check_dependency_p=*/true,
21843 /*is_declaration=*/false);
21844 /* If we found it, and the next token is a `*', then we are
21845 indeed looking at a pointer-to-member operator. */
21846 if (!cp_parser_error_occurred (parser
)
21847 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21849 /* Indicate that the `*' operator was used. */
21850 code
= INDIRECT_REF
;
21852 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21853 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21854 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21855 error_at (token
->location
, "cannot form pointer to member of "
21856 "non-class %q#T", parser
->scope
);
21859 /* The type of which the member is a member is given by the
21861 *type
= parser
->scope
;
21862 /* The next name will not be qualified. */
21863 parser
->scope
= NULL_TREE
;
21864 parser
->qualifying_scope
= NULL_TREE
;
21865 parser
->object_scope
= NULL_TREE
;
21866 /* Look for optional c++11 attributes. */
21867 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21868 if (attributes
!= NULL
)
21869 *attributes
= attrs
;
21870 /* Look for the optional cv-qualifier-seq. */
21871 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21874 /* If that didn't work we don't have a ptr-operator. */
21875 if (!cp_parser_parse_definitely (parser
))
21876 cp_parser_error (parser
, "expected ptr-operator");
21882 /* Parse an (optional) cv-qualifier-seq.
21885 cv-qualifier cv-qualifier-seq [opt]
21896 Returns a bitmask representing the cv-qualifiers. */
21899 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21901 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21906 cp_cv_quals cv_qualifier
;
21908 /* Peek at the next token. */
21909 token
= cp_lexer_peek_token (parser
->lexer
);
21910 /* See if it's a cv-qualifier. */
21911 switch (token
->keyword
)
21914 cv_qualifier
= TYPE_QUAL_CONST
;
21918 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21922 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21926 cv_qualifier
= TYPE_UNQUALIFIED
;
21933 if (cv_quals
& cv_qualifier
)
21935 gcc_rich_location
richloc (token
->location
);
21936 richloc
.add_fixit_remove ();
21937 error_at (&richloc
, "duplicate cv-qualifier");
21938 cp_lexer_purge_token (parser
->lexer
);
21942 cp_lexer_consume_token (parser
->lexer
);
21943 cv_quals
|= cv_qualifier
;
21950 /* Parse an (optional) ref-qualifier
21956 Returns cp_ref_qualifier representing ref-qualifier. */
21958 static cp_ref_qualifier
21959 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21961 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21963 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21964 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21969 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21970 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21972 switch (token
->type
)
21975 curr_ref_qual
= REF_QUAL_LVALUE
;
21979 curr_ref_qual
= REF_QUAL_RVALUE
;
21983 curr_ref_qual
= REF_QUAL_NONE
;
21987 if (!curr_ref_qual
)
21991 error_at (token
->location
, "multiple ref-qualifiers");
21992 cp_lexer_purge_token (parser
->lexer
);
21996 ref_qual
= curr_ref_qual
;
21997 cp_lexer_consume_token (parser
->lexer
);
22004 /* Parse an optional tx-qualifier.
22008 transaction_safe_dynamic */
22011 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
22013 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22014 if (token
->type
== CPP_NAME
)
22016 tree name
= token
->u
.value
;
22017 const char *p
= IDENTIFIER_POINTER (name
);
22018 const int len
= strlen ("transaction_safe");
22019 if (!strncmp (p
, "transaction_safe", len
))
22023 || !strcmp (p
, "_dynamic"))
22025 cp_lexer_consume_token (parser
->lexer
);
22028 error ("%qE requires %<-fgnu-tm%>", name
);
22039 /* Parse an (optional) virt-specifier-seq.
22041 virt-specifier-seq:
22042 virt-specifier virt-specifier-seq [opt]
22048 Returns a bitmask representing the virt-specifiers. */
22050 static cp_virt_specifiers
22051 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
22053 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22058 cp_virt_specifiers virt_specifier
;
22060 /* Peek at the next token. */
22061 token
= cp_lexer_peek_token (parser
->lexer
);
22062 /* See if it's a virt-specifier-qualifier. */
22063 if (token
->type
!= CPP_NAME
)
22065 if (id_equal (token
->u
.value
, "override"))
22067 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
22068 virt_specifier
= VIRT_SPEC_OVERRIDE
;
22070 else if (id_equal (token
->u
.value
, "final"))
22072 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
22073 virt_specifier
= VIRT_SPEC_FINAL
;
22075 else if (id_equal (token
->u
.value
, "__final"))
22077 virt_specifier
= VIRT_SPEC_FINAL
;
22082 if (virt_specifiers
& virt_specifier
)
22084 gcc_rich_location
richloc (token
->location
);
22085 richloc
.add_fixit_remove ();
22086 error_at (&richloc
, "duplicate virt-specifier");
22087 cp_lexer_purge_token (parser
->lexer
);
22091 cp_lexer_consume_token (parser
->lexer
);
22092 virt_specifiers
|= virt_specifier
;
22095 return virt_specifiers
;
22098 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22099 is in scope even though it isn't real. */
22102 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
22106 if (current_class_ptr
)
22108 /* We don't clear this between NSDMIs. Is it already what we want? */
22109 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
22110 if (DECL_P (current_class_ptr
)
22111 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
22112 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
22113 && cp_type_quals (type
) == quals
)
22117 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
22118 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22119 current_class_ptr
= NULL_TREE
;
22121 = cp_build_fold_indirect_ref (this_parm
);
22122 current_class_ptr
= this_parm
;
22125 /* Return true iff our current scope is a non-static data member
22129 parsing_nsdmi (void)
22131 /* We recognize NSDMI context by the context-less 'this' pointer set up
22132 by the function above. */
22133 if (current_class_ptr
22134 && TREE_CODE (current_class_ptr
) == PARM_DECL
22135 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
22140 /* Parse a late-specified return type, if any. This is not a separate
22141 non-terminal, but part of a function declarator, which looks like
22143 -> trailing-type-specifier-seq abstract-declarator(opt)
22145 Returns the type indicated by the type-id.
22147 In addition to this, parse any queued up #pragma omp declare simd
22148 clauses, and #pragma acc routine clauses.
22150 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22154 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
22155 tree
& requires_clause
)
22158 tree type
= NULL_TREE
;
22159 bool declare_simd_p
= (parser
->omp_declare_simd
22161 && declarator
->kind
== cdk_id
);
22163 bool oacc_routine_p
= (parser
->oacc_routine
22165 && declarator
->kind
== cdk_id
);
22167 /* Peek at the next token. */
22168 token
= cp_lexer_peek_token (parser
->lexer
);
22169 /* A late-specified return type is indicated by an initial '->'. */
22170 if (token
->type
!= CPP_DEREF
22171 && token
->keyword
!= RID_REQUIRES
22172 && !(token
->type
== CPP_NAME
22173 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
22174 && !(declare_simd_p
|| oacc_routine_p
))
22177 if (token
->type
== CPP_DEREF
)
22179 /* Consume the ->. */
22180 cp_lexer_consume_token (parser
->lexer
);
22182 type
= cp_parser_trailing_type_id (parser
);
22185 /* Function declarations may be followed by a trailing
22186 requires-clause. */
22187 requires_clause
= cp_parser_requires_clause_opt (parser
, false);
22189 if (declare_simd_p
)
22190 declarator
->attributes
22191 = cp_parser_late_parsing_omp_declare_simd (parser
,
22192 declarator
->attributes
);
22193 if (oacc_routine_p
)
22194 declarator
->attributes
22195 = cp_parser_late_parsing_oacc_routine (parser
,
22196 declarator
->attributes
);
22201 /* Parse a declarator-id.
22205 :: [opt] nested-name-specifier [opt] type-name
22207 In the `id-expression' case, the value returned is as for
22208 cp_parser_id_expression if the id-expression was an unqualified-id.
22209 If the id-expression was a qualified-id, then a SCOPE_REF is
22210 returned. The first operand is the scope (either a NAMESPACE_DECL
22211 or TREE_TYPE), but the second is still just a representation of an
22215 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
22218 /* The expression must be an id-expression. Assume that qualified
22219 names are the names of types so that:
22222 int S<T>::R::i = 3;
22224 will work; we must treat `S<T>::R' as the name of a type.
22225 Similarly, assume that qualified names are templates, where
22229 int S<T>::R<T>::i = 3;
22232 id
= cp_parser_id_expression (parser
,
22233 /*template_keyword_p=*/false,
22234 /*check_dependency_p=*/false,
22235 /*template_p=*/NULL
,
22236 /*declarator_p=*/true,
22238 if (id
&& BASELINK_P (id
))
22239 id
= BASELINK_FUNCTIONS (id
);
22243 /* Parse a type-id.
22246 type-specifier-seq abstract-declarator [opt]
22248 The parser flags FLAGS is used to control type-specifier parsing.
22250 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22252 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22253 i.e. we've just seen "->".
22255 Returns the TYPE specified. */
22258 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
22259 bool is_template_arg
, bool is_trailing_return
,
22260 location_t
*type_location
)
22262 cp_decl_specifier_seq type_specifier_seq
;
22263 cp_declarator
*abstract_declarator
;
22265 /* Parse the type-specifier-seq. */
22266 cp_parser_type_specifier_seq (parser
, flags
,
22267 /*is_declaration=*/false,
22268 is_trailing_return
,
22269 &type_specifier_seq
);
22271 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
22273 if (is_template_arg
&& type_specifier_seq
.type
22274 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
22275 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
22276 /* A bare template name as a template argument is a template template
22277 argument, not a placeholder, so fail parsing it as a type argument. */
22279 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
22280 cp_parser_simulate_error (parser
);
22281 return error_mark_node
;
22283 if (type_specifier_seq
.type
== error_mark_node
)
22284 return error_mark_node
;
22286 /* There might or might not be an abstract declarator. */
22287 cp_parser_parse_tentatively (parser
);
22288 /* Look for the declarator. */
22289 abstract_declarator
22290 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
22291 CP_PARSER_FLAGS_NONE
, NULL
,
22292 /*parenthesized_p=*/NULL
,
22293 /*member_p=*/false,
22294 /*friend_p=*/false,
22295 /*static_p=*/false);
22296 /* Check to see if there really was a declarator. */
22297 if (!cp_parser_parse_definitely (parser
))
22298 abstract_declarator
= NULL
;
22300 if (type_specifier_seq
.type
22301 /* The concepts TS allows 'auto' as a type-id. */
22302 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
22303 /* None of the valid uses of 'auto' in C++14 involve the type-id
22304 nonterminal, but it is valid in a trailing-return-type. */
22305 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
22306 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
22308 /* A type-id with type 'auto' is only ok if the abstract declarator
22309 is a function declarator with a late-specified return type.
22311 A type-id with 'auto' is also valid in a trailing-return-type
22312 in a compound-requirement. */
22313 if (abstract_declarator
22314 && abstract_declarator
->kind
== cdk_function
22315 && abstract_declarator
->u
.function
.late_return_type
)
22317 else if (parser
->in_result_type_constraint_p
)
22321 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
22322 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
22324 error_at (loc
, "missing template arguments after %qT",
22326 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
22330 error_at (loc
, "invalid use of %qT", auto_node
);
22331 return error_mark_node
;
22335 return groktypename (&type_specifier_seq
, abstract_declarator
,
22339 /* Wrapper for cp_parser_type_id_1. */
22342 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
22343 location_t
*type_location
)
22345 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
22348 /* Wrapper for cp_parser_type_id_1. */
22351 cp_parser_template_type_arg (cp_parser
*parser
)
22354 const char *saved_message
= parser
->type_definition_forbidden_message
;
22355 parser
->type_definition_forbidden_message
22356 = G_("types may not be defined in template arguments");
22357 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
22358 parser
->type_definition_forbidden_message
= saved_message
;
22359 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
22361 error ("invalid use of %<auto%> in template argument");
22362 r
= error_mark_node
;
22367 /* Wrapper for cp_parser_type_id_1. */
22370 cp_parser_trailing_type_id (cp_parser
*parser
)
22372 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
22373 false, true, NULL
);
22376 /* Parse a type-specifier-seq.
22378 type-specifier-seq:
22379 type-specifier type-specifier-seq [opt]
22383 type-specifier-seq:
22384 attributes type-specifier-seq [opt]
22386 The parser flags FLAGS is used to control type-specifier parsing.
22388 If IS_DECLARATION is true, we are at the start of a "condition" or
22389 exception-declaration, so we might be followed by a declarator-id.
22391 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22392 i.e. we've just seen "->".
22394 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22397 cp_parser_type_specifier_seq (cp_parser
* parser
,
22398 cp_parser_flags flags
,
22399 bool is_declaration
,
22400 bool is_trailing_return
,
22401 cp_decl_specifier_seq
*type_specifier_seq
)
22403 bool seen_type_specifier
= false;
22404 cp_token
*start_token
= NULL
;
22406 /* Clear the TYPE_SPECIFIER_SEQ. */
22407 clear_decl_specs (type_specifier_seq
);
22409 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
22410 /* In the context of a trailing return type, enum E { } is an
22411 elaborated-type-specifier followed by a function-body, not an
22413 if (is_trailing_return
)
22414 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
22416 /* Parse the type-specifiers and attributes. */
22419 tree type_specifier
;
22420 bool is_cv_qualifier
;
22422 /* Check for attributes first. */
22423 if (cp_next_tokens_can_be_attribute_p (parser
))
22425 /* GNU attributes at the end of a declaration apply to the
22426 declaration as a whole, not to the trailing return type. So look
22427 ahead to see if these attributes are at the end. */
22428 if (seen_type_specifier
&& is_trailing_return
22429 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
22431 size_t n
= cp_parser_skip_attributes_opt (parser
, 1);
22432 cp_token
*tok
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
22433 if (tok
->type
== CPP_SEMICOLON
|| tok
->type
== CPP_COMMA
22434 || tok
->type
== CPP_EQ
|| tok
->type
== CPP_OPEN_BRACE
)
22437 type_specifier_seq
->attributes
22438 = attr_chainon (type_specifier_seq
->attributes
,
22439 cp_parser_attributes_opt (parser
));
22443 /* record the token of the beginning of the type specifier seq,
22444 for error reporting purposes*/
22446 start_token
= cp_lexer_peek_token (parser
->lexer
);
22448 /* Look for the type-specifier. */
22449 type_specifier
= cp_parser_type_specifier (parser
,
22451 type_specifier_seq
,
22452 /*is_declaration=*/false,
22455 if (!type_specifier
)
22457 /* If the first type-specifier could not be found, this is not a
22458 type-specifier-seq at all. */
22459 if (!seen_type_specifier
)
22461 /* Set in_declarator_p to avoid skipping to the semicolon. */
22462 int in_decl
= parser
->in_declarator_p
;
22463 parser
->in_declarator_p
= true;
22465 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
22466 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22467 cp_parser_error (parser
, "expected type-specifier");
22469 parser
->in_declarator_p
= in_decl
;
22471 type_specifier_seq
->type
= error_mark_node
;
22474 /* If subsequent type-specifiers could not be found, the
22475 type-specifier-seq is complete. */
22479 seen_type_specifier
= true;
22480 /* The standard says that a condition can be:
22482 type-specifier-seq declarator = assignment-expression
22489 we should treat the "S" as a declarator, not as a
22490 type-specifier. The standard doesn't say that explicitly for
22491 type-specifier-seq, but it does say that for
22492 decl-specifier-seq in an ordinary declaration. Perhaps it
22493 would be clearer just to allow a decl-specifier-seq here, and
22494 then add a semantic restriction that if any decl-specifiers
22495 that are not type-specifiers appear, the program is invalid. */
22496 if (is_declaration
&& !is_cv_qualifier
)
22497 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
22501 /* Return whether the function currently being declared has an associated
22502 template parameter list. */
22505 function_being_declared_is_template_p (cp_parser
* parser
)
22507 if (!current_template_parms
|| processing_template_parmlist
)
22510 if (parser
->implicit_template_scope
)
22513 if (at_class_scope_p ()
22514 && TYPE_BEING_DEFINED (current_class_type
))
22515 return parser
->num_template_parameter_lists
!= 0;
22517 return ((int) parser
->num_template_parameter_lists
> template_class_depth
22518 (current_class_type
));
22521 /* Parse a parameter-declaration-clause.
22523 parameter-declaration-clause:
22524 parameter-declaration-list [opt] ... [opt]
22525 parameter-declaration-list , ...
22527 The parser flags FLAGS is used to control type-specifier parsing.
22529 Returns a representation for the parameter declarations. A return
22530 value of NULL indicates a parameter-declaration-clause consisting
22531 only of an ellipsis. */
22534 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
22535 cp_parser_flags flags
)
22541 temp_override
<bool> cleanup
22542 (parser
->auto_is_implicit_function_template_parm_p
);
22544 if (!processing_specialization
22545 && !processing_template_parmlist
22546 && !processing_explicit_instantiation
22547 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22548 actual function or a random abstract declarator. */
22549 && parser
->default_arg_ok_p
)
22550 if (!current_function_decl
22551 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
22552 parser
->auto_is_implicit_function_template_parm_p
= true;
22554 /* Peek at the next token. */
22555 token
= cp_lexer_peek_token (parser
->lexer
);
22556 /* Check for trivial parameter-declaration-clauses. */
22557 if (token
->type
== CPP_ELLIPSIS
)
22559 /* Consume the `...' token. */
22560 cp_lexer_consume_token (parser
->lexer
);
22563 else if (token
->type
== CPP_CLOSE_PAREN
)
22564 /* There are no parameters. */
22565 return void_list_node
;
22566 /* Check for `(void)', too, which is a special case. */
22567 else if (token
->keyword
== RID_VOID
22568 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22569 == CPP_CLOSE_PAREN
))
22571 /* Consume the `void' token. */
22572 cp_lexer_consume_token (parser
->lexer
);
22573 /* There are no parameters. */
22574 return void_list_node
;
22577 /* Parse the parameter-declaration-list. */
22578 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22579 /* If a parse error occurred while parsing the
22580 parameter-declaration-list, then the entire
22581 parameter-declaration-clause is erroneous. */
22582 if (parameters
== error_mark_node
)
22585 /* Peek at the next token. */
22586 token
= cp_lexer_peek_token (parser
->lexer
);
22587 /* If it's a `,', the clause should terminate with an ellipsis. */
22588 if (token
->type
== CPP_COMMA
)
22590 /* Consume the `,'. */
22591 cp_lexer_consume_token (parser
->lexer
);
22592 /* Expect an ellipsis. */
22594 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22596 /* It might also be `...' if the optional trailing `,' was
22598 else if (token
->type
== CPP_ELLIPSIS
)
22600 /* Consume the `...' token. */
22601 cp_lexer_consume_token (parser
->lexer
);
22602 /* And remember that we saw it. */
22606 ellipsis_p
= false;
22608 /* Finish the parameter list. */
22610 parameters
= chainon (parameters
, void_list_node
);
22615 /* Parse a parameter-declaration-list.
22617 parameter-declaration-list:
22618 parameter-declaration
22619 parameter-declaration-list , parameter-declaration
22621 The parser flags FLAGS is used to control type-specifier parsing.
22623 Returns a representation of the parameter-declaration-list, as for
22624 cp_parser_parameter_declaration_clause. However, the
22625 `void_list_node' is never appended to the list. */
22628 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22630 tree parameters
= NULL_TREE
;
22631 tree
*tail
= ¶meters
;
22632 bool saved_in_unbraced_linkage_specification_p
;
22635 /* The special considerations that apply to a function within an
22636 unbraced linkage specifications do not apply to the parameters
22637 to the function. */
22638 saved_in_unbraced_linkage_specification_p
22639 = parser
->in_unbraced_linkage_specification_p
;
22640 parser
->in_unbraced_linkage_specification_p
= false;
22642 /* Look for more parameters. */
22645 cp_parameter_declarator
*parameter
;
22646 tree decl
= error_mark_node
;
22647 bool parenthesized_p
= false;
22649 /* Parse the parameter. */
22651 = cp_parser_parameter_declaration (parser
, flags
,
22652 /*template_parm_p=*/false,
22655 /* We don't know yet if the enclosing context is deprecated, so wait
22656 and warn in grokparms if appropriate. */
22657 deprecated_state
= DEPRECATED_SUPPRESS
;
22661 decl
= grokdeclarator (parameter
->declarator
,
22662 ¶meter
->decl_specifiers
,
22664 parameter
->default_argument
!= NULL_TREE
,
22665 ¶meter
->decl_specifiers
.attributes
);
22666 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22667 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22670 deprecated_state
= DEPRECATED_NORMAL
;
22672 /* If a parse error occurred parsing the parameter declaration,
22673 then the entire parameter-declaration-list is erroneous. */
22674 if (decl
== error_mark_node
)
22676 parameters
= error_mark_node
;
22680 if (parameter
->decl_specifiers
.attributes
)
22681 cplus_decl_attributes (&decl
,
22682 parameter
->decl_specifiers
.attributes
,
22684 if (DECL_NAME (decl
))
22685 decl
= pushdecl (decl
);
22687 if (decl
!= error_mark_node
)
22689 retrofit_lang_decl (decl
);
22690 DECL_PARM_INDEX (decl
) = ++index
;
22691 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22694 /* Add the new parameter to the list. */
22695 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22696 tail
= &TREE_CHAIN (*tail
);
22698 /* Peek at the next token. */
22699 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22700 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22701 /* These are for Objective-C++ */
22702 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22703 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22704 /* The parameter-declaration-list is complete. */
22706 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22710 /* Peek at the next token. */
22711 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22712 /* If it's an ellipsis, then the list is complete. */
22713 if (token
->type
== CPP_ELLIPSIS
)
22715 /* Otherwise, there must be more parameters. Consume the
22717 cp_lexer_consume_token (parser
->lexer
);
22718 /* When parsing something like:
22720 int i(float f, double d)
22722 we can tell after seeing the declaration for "f" that we
22723 are not looking at an initialization of a variable "i",
22724 but rather at the declaration of a function "i".
22726 Due to the fact that the parsing of template arguments
22727 (as specified to a template-id) requires backtracking we
22728 cannot use this technique when inside a template argument
22730 if (!parser
->in_template_argument_list_p
22731 && !parser
->in_type_id_in_expr_p
22732 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22733 /* However, a parameter-declaration of the form
22734 "float(f)" (which is a valid declaration of a
22735 parameter "f") can also be interpreted as an
22736 expression (the conversion of "f" to "float"). */
22737 && !parenthesized_p
)
22738 cp_parser_commit_to_tentative_parse (parser
);
22742 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22743 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22744 cp_parser_skip_to_closing_parenthesis (parser
,
22745 /*recovering=*/true,
22746 /*or_comma=*/false,
22747 /*consume_paren=*/false);
22752 parser
->in_unbraced_linkage_specification_p
22753 = saved_in_unbraced_linkage_specification_p
;
22755 /* Reset implicit_template_scope if we are about to leave the function
22756 parameter list that introduced it. Note that for out-of-line member
22757 definitions, there will be one or more class scopes before we get to
22758 the template parameter scope. */
22760 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22761 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22763 while (maybe_its
->kind
== sk_class
)
22764 maybe_its
= maybe_its
->level_chain
;
22765 if (maybe_its
== its
)
22767 parser
->implicit_template_parms
= 0;
22768 parser
->implicit_template_scope
= 0;
22775 /* Parse a parameter declaration.
22777 parameter-declaration:
22778 decl-specifier-seq ... [opt] declarator
22779 decl-specifier-seq declarator = assignment-expression
22780 decl-specifier-seq ... [opt] abstract-declarator [opt]
22781 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22783 The parser flags FLAGS is used to control type-specifier parsing.
22785 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22786 declares a template parameter. (In that case, a non-nested `>'
22787 token encountered during the parsing of the assignment-expression
22788 is not interpreted as a greater-than operator.)
22790 Returns a representation of the parameter, or NULL if an error
22791 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22792 true iff the declarator is of the form "(p)". */
22794 static cp_parameter_declarator
*
22795 cp_parser_parameter_declaration (cp_parser
*parser
,
22796 cp_parser_flags flags
,
22797 bool template_parm_p
,
22798 bool *parenthesized_p
)
22800 int declares_class_or_enum
;
22801 cp_decl_specifier_seq decl_specifiers
;
22802 cp_declarator
*declarator
;
22803 tree default_argument
;
22804 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22805 const char *saved_message
;
22806 bool template_parameter_pack_p
= false;
22808 /* In a template parameter, `>' is not an operator.
22812 When parsing a default template-argument for a non-type
22813 template-parameter, the first non-nested `>' is taken as the end
22814 of the template parameter-list rather than a greater-than
22817 /* Type definitions may not appear in parameter types. */
22818 saved_message
= parser
->type_definition_forbidden_message
;
22819 parser
->type_definition_forbidden_message
22820 = G_("types may not be defined in parameter types");
22822 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22823 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22824 (current_template_parms
)) : 0);
22826 /* Parse the declaration-specifiers. */
22827 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22828 cp_parser_decl_specifier_seq (parser
,
22831 &declares_class_or_enum
);
22833 /* Complain about missing 'typename' or other invalid type names. */
22834 if (!decl_specifiers
.any_type_specifiers_p
22835 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22836 decl_specifiers
.type
= error_mark_node
;
22838 /* If an error occurred, there's no reason to attempt to parse the
22839 rest of the declaration. */
22840 if (cp_parser_error_occurred (parser
))
22842 parser
->type_definition_forbidden_message
= saved_message
;
22846 /* Peek at the next token. */
22847 token
= cp_lexer_peek_token (parser
->lexer
);
22849 /* If the next token is a `)', `,', `=', `>', or `...', then there
22850 is no declarator. However, when variadic templates are enabled,
22851 there may be a declarator following `...'. */
22852 if (token
->type
== CPP_CLOSE_PAREN
22853 || token
->type
== CPP_COMMA
22854 || token
->type
== CPP_EQ
22855 || token
->type
== CPP_GREATER
)
22858 if (parenthesized_p
)
22859 *parenthesized_p
= false;
22861 /* Otherwise, there should be a declarator. */
22864 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22865 parser
->default_arg_ok_p
= false;
22867 /* After seeing a decl-specifier-seq, if the next token is not a
22868 "(", there is no possibility that the code is a valid
22869 expression. Therefore, if parsing tentatively, we commit at
22871 if (!parser
->in_template_argument_list_p
22872 /* In an expression context, having seen:
22876 we cannot be sure whether we are looking at a
22877 function-type (taking a "char" as a parameter) or a cast
22878 of some object of type "char" to "int". */
22879 && !parser
->in_type_id_in_expr_p
22880 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22881 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22882 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22883 cp_parser_commit_to_tentative_parse (parser
);
22884 /* Parse the declarator. */
22885 declarator_token_start
= token
;
22886 declarator
= cp_parser_declarator (parser
,
22887 CP_PARSER_DECLARATOR_EITHER
,
22888 CP_PARSER_FLAGS_NONE
,
22889 /*ctor_dtor_or_conv_p=*/NULL
,
22891 /*member_p=*/false,
22892 /*friend_p=*/false,
22893 /*static_p=*/false);
22894 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22895 /* After the declarator, allow more attributes. */
22896 decl_specifiers
.attributes
22897 = attr_chainon (decl_specifiers
.attributes
,
22898 cp_parser_attributes_opt (parser
));
22900 /* If the declarator is a template parameter pack, remember that and
22901 clear the flag in the declarator itself so we don't get errors
22902 from grokdeclarator. */
22903 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22905 declarator
->parameter_pack_p
= false;
22906 template_parameter_pack_p
= true;
22910 /* If the next token is an ellipsis, and we have not seen a declarator
22911 name, and if either the type of the declarator contains parameter
22912 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22913 for, eg, abbreviated integral type names), then we actually have a
22914 parameter pack expansion expression. Otherwise, leave the ellipsis
22915 for a C-style variadic function. */
22916 token
= cp_lexer_peek_token (parser
->lexer
);
22918 /* If a function parameter pack was specified and an implicit template
22919 parameter was introduced during cp_parser_parameter_declaration,
22920 change any implicit parameters introduced into packs. */
22921 if (parser
->implicit_template_parms
22922 && ((token
->type
== CPP_ELLIPSIS
22923 && declarator_can_be_parameter_pack (declarator
))
22924 || (declarator
&& declarator
->parameter_pack_p
)))
22926 int latest_template_parm_idx
= TREE_VEC_LENGTH
22927 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22929 if (latest_template_parm_idx
!= template_parm_idx
)
22930 decl_specifiers
.type
= convert_generic_types_to_packs
22931 (decl_specifiers
.type
,
22932 template_parm_idx
, latest_template_parm_idx
);
22935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22937 tree type
= decl_specifiers
.type
;
22939 if (type
&& DECL_P (type
))
22940 type
= TREE_TYPE (type
);
22943 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22944 && (template_parm_p
|| uses_parameter_packs (type
)))
22945 || (!type
&& template_parm_p
))
22946 && declarator_can_be_parameter_pack (declarator
))
22948 /* Consume the `...'. */
22949 cp_lexer_consume_token (parser
->lexer
);
22950 maybe_warn_variadic_templates ();
22952 /* Build a pack expansion type */
22953 if (template_parm_p
)
22954 template_parameter_pack_p
= true;
22955 else if (declarator
)
22956 declarator
->parameter_pack_p
= true;
22958 decl_specifiers
.type
= make_pack_expansion (type
);
22962 /* The restriction on defining new types applies only to the type
22963 of the parameter, not to the default argument. */
22964 parser
->type_definition_forbidden_message
= saved_message
;
22966 /* If the next token is `=', then process a default argument. */
22967 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22969 tree type
= decl_specifiers
.type
;
22970 token
= cp_lexer_peek_token (parser
->lexer
);
22971 /* If we are defining a class, then the tokens that make up the
22972 default argument must be saved and processed later. */
22973 if (!template_parm_p
&& at_class_scope_p ()
22974 && TYPE_BEING_DEFINED (current_class_type
)
22975 && !LAMBDA_TYPE_P (current_class_type
))
22976 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22978 /* A constrained-type-specifier may declare a type
22979 template-parameter. */
22980 else if (declares_constrained_type_template_parameter (type
))
22982 = cp_parser_default_type_template_argument (parser
);
22984 /* A constrained-type-specifier may declare a
22985 template-template-parameter. */
22986 else if (declares_constrained_template_template_parameter (type
))
22988 = cp_parser_default_template_template_argument (parser
);
22990 /* Outside of a class definition, we can just parse the
22991 assignment-expression. */
22994 = cp_parser_default_argument (parser
, template_parm_p
);
22996 if (!parser
->default_arg_ok_p
)
22998 permerror (token
->location
,
22999 "default arguments are only "
23000 "permitted for function parameters");
23002 else if ((declarator
&& declarator
->parameter_pack_p
)
23003 || template_parameter_pack_p
23004 || (decl_specifiers
.type
23005 && PACK_EXPANSION_P (decl_specifiers
.type
)))
23007 /* Find the name of the parameter pack. */
23008 cp_declarator
*id_declarator
= declarator
;
23009 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
23010 id_declarator
= id_declarator
->declarator
;
23012 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
23013 error_at (declarator_token_start
->location
,
23015 ? G_("template parameter pack %qD "
23016 "cannot have a default argument")
23017 : G_("parameter pack %qD cannot have "
23018 "a default argument"),
23019 id_declarator
->u
.id
.unqualified_name
);
23021 error_at (declarator_token_start
->location
,
23023 ? G_("template parameter pack cannot have "
23024 "a default argument")
23025 : G_("parameter pack cannot have a "
23026 "default argument"));
23028 default_argument
= NULL_TREE
;
23032 default_argument
= NULL_TREE
;
23034 if (default_argument
)
23035 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
23037 /* Generate a location for the parameter, ranging from the start of the
23038 initial token to the end of the final token (using input_location for
23039 the latter, set up by cp_lexer_set_source_position_from_token when
23042 If we have a identifier, then use it for the caret location, e.g.
23044 extern int callee (int one, int (*two)(int, int), float three);
23045 ~~~~~~^~~~~~~~~~~~~~
23047 otherwise, reuse the start location for the caret location e.g.:
23049 extern int callee (int one, int (*)(int, int), float three);
23053 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
23054 ? declarator
->id_loc
23055 : decl_spec_token_start
->location
);
23056 location_t param_loc
= make_location (caret_loc
,
23057 decl_spec_token_start
->location
,
23060 return make_parameter_declarator (&decl_specifiers
,
23064 template_parameter_pack_p
);
23067 /* Parse a default argument and return it.
23069 TEMPLATE_PARM_P is true if this is a default argument for a
23070 non-type template parameter. */
23072 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
23074 tree default_argument
= NULL_TREE
;
23075 bool saved_greater_than_is_operator_p
;
23076 unsigned char saved_local_variables_forbidden_p
;
23077 bool non_constant_p
, is_direct_init
;
23079 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23081 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
23082 parser
->greater_than_is_operator_p
= !template_parm_p
;
23083 /* Local variable names (and the `this' keyword) may not
23084 appear in a default argument. */
23085 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23086 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
23087 /* Parse the assignment-expression. */
23088 if (template_parm_p
)
23089 push_deferring_access_checks (dk_no_deferred
);
23090 tree saved_class_ptr
= NULL_TREE
;
23091 tree saved_class_ref
= NULL_TREE
;
23092 /* The "this" pointer is not valid in a default argument. */
23095 saved_class_ptr
= current_class_ptr
;
23096 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
23097 saved_class_ref
= current_class_ref
;
23098 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
23101 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
23102 /* Restore the "this" pointer. */
23105 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
23106 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
23108 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
23109 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23110 if (template_parm_p
)
23111 pop_deferring_access_checks ();
23112 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
23113 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23115 return default_argument
;
23118 /* Parse a function-body.
23121 compound_statement */
23124 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
23126 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
23127 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
23131 /* Parse a ctor-initializer-opt followed by a function-body. Return
23132 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23133 is true we are parsing a function-try-block. */
23136 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
23137 bool in_function_try_block
)
23140 const bool check_body_p
23141 = (DECL_CONSTRUCTOR_P (current_function_decl
)
23142 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
23145 if (in_function_try_block
23146 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
23147 && cxx_dialect
< cxx20
)
23149 if (DECL_CONSTRUCTOR_P (current_function_decl
))
23150 pedwarn (input_location
, 0,
23151 "function-try-block body of %<constexpr%> constructor only "
23152 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23154 pedwarn (input_location
, 0,
23155 "function-try-block body of %<constexpr%> function only "
23156 "available with %<-std=c++20%> or %<-std=gnu++20%>");
23159 /* Begin the function body. */
23160 body
= begin_function_body ();
23161 /* Parse the optional ctor-initializer. */
23162 cp_parser_ctor_initializer_opt (parser
);
23164 /* If we're parsing a constexpr constructor definition, we need
23165 to check that the constructor body is indeed empty. However,
23166 before we get to cp_parser_function_body lot of junk has been
23167 generated, so we can't just check that we have an empty block.
23168 Rather we take a snapshot of the outermost block, and check whether
23169 cp_parser_function_body changed its state. */
23172 list
= cur_stmt_list
;
23173 if (STATEMENT_LIST_TAIL (list
))
23174 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
23176 /* Parse the function-body. */
23177 cp_parser_function_body (parser
, in_function_try_block
);
23179 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
23180 /* Finish the function body. */
23181 finish_function_body (body
);
23184 /* Parse an initializer.
23187 = initializer-clause
23188 ( expression-list )
23190 Returns an expression representing the initializer. If no
23191 initializer is present, NULL_TREE is returned.
23193 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
23194 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
23195 set to TRUE if there is no initializer present. If there is an
23196 initializer, and it is not a constant-expression, *NON_CONSTANT_P
23197 is set to true; otherwise it is set to false. */
23200 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
23201 bool* non_constant_p
, bool subexpression_p
)
23206 /* Peek at the next token. */
23207 token
= cp_lexer_peek_token (parser
->lexer
);
23209 /* Let our caller know whether or not this initializer was
23211 *is_direct_init
= (token
->type
!= CPP_EQ
);
23212 /* Assume that the initializer is constant. */
23213 *non_constant_p
= false;
23215 if (token
->type
== CPP_EQ
)
23217 /* Consume the `='. */
23218 cp_lexer_consume_token (parser
->lexer
);
23219 /* Parse the initializer-clause. */
23220 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
23222 else if (token
->type
== CPP_OPEN_PAREN
)
23224 vec
<tree
, va_gc
> *vec
;
23225 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
23227 /*allow_expansion_p=*/true,
23230 return error_mark_node
;
23231 init
= build_tree_list_vec (vec
);
23232 release_tree_vector (vec
);
23234 else if (token
->type
== CPP_OPEN_BRACE
)
23236 cp_lexer_set_source_position (parser
->lexer
);
23237 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23238 init
= cp_parser_braced_list (parser
, non_constant_p
);
23239 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
23243 /* Anything else is an error. */
23244 cp_parser_error (parser
, "expected initializer");
23245 init
= error_mark_node
;
23248 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
23249 init
= error_mark_node
;
23254 /* Parse an initializer-clause.
23256 initializer-clause:
23257 assignment-expression
23260 Returns an expression representing the initializer.
23262 If the `assignment-expression' production is used the value
23263 returned is simply a representation for the expression.
23265 Otherwise, calls cp_parser_braced_list. */
23268 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
23270 cp_expr initializer
;
23272 /* Assume the expression is constant. */
23273 *non_constant_p
= false;
23275 /* If it is not a `{', then we are looking at an
23276 assignment-expression. */
23277 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23280 = cp_parser_constant_expression (parser
,
23281 /*allow_non_constant_p=*/true,
23285 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
23287 return initializer
;
23290 /* Parse a brace-enclosed initializer list.
23293 { initializer-list , [opt] }
23294 { designated-initializer-list , [opt] }
23297 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23298 the elements of the initializer-list (or NULL, if the last
23299 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23300 NULL_TREE. There is no way to detect whether or not the optional
23301 trailing `,' was provided. NON_CONSTANT_P is as for
23302 cp_parser_initializer. */
23305 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
23308 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23310 /* Consume the `{' token. */
23311 matching_braces braces
;
23312 braces
.require_open (parser
);
23313 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23314 initializer
= make_node (CONSTRUCTOR
);
23315 /* If it's not a `}', then there is a non-trivial initializer. */
23316 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
23319 /* Parse the initializer list. */
23320 CONSTRUCTOR_ELTS (initializer
)
23321 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
23322 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
23323 /* A trailing `,' token is allowed. */
23324 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23325 cp_lexer_consume_token (parser
->lexer
);
23328 *non_constant_p
= false;
23329 /* Now, there should be a trailing `}'. */
23330 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23331 braces
.require_close (parser
);
23332 TREE_TYPE (initializer
) = init_list_type_node
;
23334 cp_expr
result (initializer
);
23335 /* Build a location of the form:
23338 with caret==start at the open brace, finish at the close brace. */
23339 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
23340 result
.set_location (combined_loc
);
23344 /* Consume tokens up to, and including, the next non-nested closing `]'.
23345 Returns true iff we found a closing `]'. */
23348 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
23350 unsigned square_depth
= 0;
23354 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
23356 switch (token
->type
)
23358 case CPP_PRAGMA_EOL
:
23359 if (!parser
->lexer
->in_pragma
)
23363 /* If we've run out of tokens, then there is no closing `]'. */
23366 case CPP_OPEN_SQUARE
:
23370 case CPP_CLOSE_SQUARE
:
23371 if (!square_depth
--)
23373 cp_lexer_consume_token (parser
->lexer
);
23382 /* Consume the token. */
23383 cp_lexer_consume_token (parser
->lexer
);
23387 /* Return true if we are looking at an array-designator, false otherwise. */
23390 cp_parser_array_designator_p (cp_parser
*parser
)
23392 /* Consume the `['. */
23393 cp_lexer_consume_token (parser
->lexer
);
23395 cp_lexer_save_tokens (parser
->lexer
);
23397 /* Skip tokens until the next token is a closing square bracket.
23398 If we find the closing `]', and the next token is a `=', then
23399 we are looking at an array designator. */
23400 bool array_designator_p
23401 = (cp_parser_skip_to_closing_square_bracket (parser
)
23402 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
23404 /* Roll back the tokens we skipped. */
23405 cp_lexer_rollback_tokens (parser
->lexer
);
23407 return array_designator_p
;
23410 /* Parse an initializer-list.
23413 initializer-clause ... [opt]
23414 initializer-list , initializer-clause ... [opt]
23418 designated-initializer-list:
23419 designated-initializer-clause
23420 designated-initializer-list , designated-initializer-clause
23422 designated-initializer-clause:
23423 designator brace-or-equal-initializer
23431 designation initializer-clause ...[opt]
23432 initializer-list , designation initializer-clause ...[opt]
23437 [ constant-expression ] =
23439 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23440 for the initializer. If the INDEX of the elt is non-NULL, it is the
23441 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23442 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23443 are any designators. */
23445 static vec
<constructor_elt
, va_gc
> *
23446 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
23449 vec
<constructor_elt
, va_gc
> *v
= NULL
;
23450 bool first_p
= true;
23451 tree first_designator
= NULL_TREE
;
23453 /* Assume all of the expressions are constant. */
23454 *non_constant_p
= false;
23456 unsigned nelts
= 0;
23457 int suppress
= suppress_location_wrappers
;
23459 /* Parse the rest of the list. */
23465 bool clause_non_constant_p
;
23466 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23468 /* Handle the C++20 syntax, '. id ='. */
23469 if ((cxx_dialect
>= cxx20
23470 || cp_parser_allow_gnu_extensions_p (parser
))
23471 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
23472 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
23473 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
23474 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
23475 == CPP_OPEN_BRACE
)))
23477 if (cxx_dialect
< cxx20
)
23478 pedwarn (loc
, OPT_Wpedantic
,
23479 "C++ designated initializers only available with "
23480 "%<-std=c++20%> or %<-std=gnu++20%>");
23481 /* Consume the `.'. */
23482 cp_lexer_consume_token (parser
->lexer
);
23483 /* Consume the identifier. */
23484 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23486 /* Consume the `='. */
23487 cp_lexer_consume_token (parser
->lexer
);
23489 /* Also, if the next token is an identifier and the following one is a
23490 colon, we are looking at the GNU designated-initializer
23492 else if (cp_parser_allow_gnu_extensions_p (parser
)
23493 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
23494 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23497 /* Warn the user that they are using an extension. */
23498 pedwarn (loc
, OPT_Wpedantic
,
23499 "ISO C++ does not allow GNU designated initializers");
23500 /* Consume the identifier. */
23501 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23502 /* Consume the `:'. */
23503 cp_lexer_consume_token (parser
->lexer
);
23505 /* Also handle C99 array designators, '[ const ] ='. */
23506 else if (cp_parser_allow_gnu_extensions_p (parser
)
23507 && !c_dialect_objc ()
23508 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23510 /* In C++11, [ could start a lambda-introducer. */
23511 bool non_const
= false;
23513 cp_parser_parse_tentatively (parser
);
23515 if (!cp_parser_array_designator_p (parser
))
23517 cp_parser_simulate_error (parser
);
23518 designator
= NULL_TREE
;
23522 designator
= cp_parser_constant_expression (parser
, true,
23524 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23525 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23528 if (!cp_parser_parse_definitely (parser
))
23529 designator
= NULL_TREE
;
23531 && (!require_potential_rvalue_constant_expression
23533 designator
= NULL_TREE
;
23535 /* Warn the user that they are using an extension. */
23536 pedwarn (loc
, OPT_Wpedantic
,
23537 "ISO C++ does not allow C99 designated initializers");
23540 designator
= NULL_TREE
;
23544 first_designator
= designator
;
23547 else if (cxx_dialect
>= cxx20
23548 && first_designator
!= error_mark_node
23549 && (!first_designator
!= !designator
))
23551 error_at (loc
, "either all initializer clauses should be designated "
23552 "or none of them should be");
23553 first_designator
= error_mark_node
;
23555 else if (cxx_dialect
< cxx20
&& !first_designator
)
23556 first_designator
= designator
;
23558 /* Parse the initializer. */
23559 initializer
= cp_parser_initializer_clause (parser
,
23560 &clause_non_constant_p
);
23561 /* If any clause is non-constant, so is the entire initializer. */
23562 if (clause_non_constant_p
)
23563 *non_constant_p
= true;
23565 /* If we have an ellipsis, this is an initializer pack
23567 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23569 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23571 /* Consume the `...'. */
23572 cp_lexer_consume_token (parser
->lexer
);
23574 if (designator
&& cxx_dialect
>= cxx20
)
23576 "%<...%> not allowed in designated initializer list");
23578 /* Turn the initializer into an initializer expansion. */
23579 initializer
= make_pack_expansion (initializer
);
23582 /* Add it to the vector. */
23583 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23585 /* If the next token is not a comma, we have reached the end of
23587 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23590 /* Peek at the next token. */
23591 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23592 /* If the next token is a `}', then we're still done. An
23593 initializer-clause can have a trailing `,' after the
23594 initializer-list and before the closing `}'. */
23595 if (token
->type
== CPP_CLOSE_BRACE
)
23598 /* Suppress location wrappers in a long initializer to save memory
23599 (14179). The cutoff is chosen arbitrarily. */
23600 const unsigned loc_max
= 256;
23602 if (TREE_CODE (initializer
) == CONSTRUCTOR
)
23603 /* Look one level down because it's easy. Looking deeper would require
23604 passing down a nelts pointer, and I don't think multi-level massive
23605 initializers are common enough to justify this. */
23606 incr
= CONSTRUCTOR_NELTS (initializer
);
23608 if (nelts
>= loc_max
&& (nelts
- incr
) < loc_max
)
23609 ++suppress_location_wrappers
;
23611 /* Consume the `,' token. */
23612 cp_lexer_consume_token (parser
->lexer
);
23615 /* The same identifier shall not appear in multiple designators
23616 of a designated-initializer-list. */
23617 if (first_designator
)
23620 tree designator
, val
;
23621 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23622 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23624 if (IDENTIFIER_MARKED (designator
))
23626 error_at (cp_expr_loc_or_input_loc (val
),
23627 "%<.%s%> designator used multiple times in "
23628 "the same initializer list",
23629 IDENTIFIER_POINTER (designator
));
23630 (*v
)[i
].index
= error_mark_node
;
23633 IDENTIFIER_MARKED (designator
) = 1;
23635 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23636 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23637 IDENTIFIER_MARKED (designator
) = 0;
23640 suppress_location_wrappers
= suppress
;
23642 *designated
= first_designator
!= NULL_TREE
;
23646 /* Classes [gram.class] */
23648 /* Parse a class-name.
23654 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23655 to indicate that names looked up in dependent types should be
23656 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23657 keyword has been used to indicate that the name that appears next
23658 is a template. TAG_TYPE indicates the explicit tag given before
23659 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23660 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23661 is the class being defined in a class-head. If ENUM_OK is TRUE,
23662 enum-names are also accepted.
23664 Returns the TYPE_DECL representing the class. */
23667 cp_parser_class_name (cp_parser
*parser
,
23668 bool typename_keyword_p
,
23669 bool template_keyword_p
,
23670 enum tag_types tag_type
,
23671 bool check_dependency_p
,
23673 bool is_declaration
,
23680 tree identifier
= NULL_TREE
;
23682 /* All class-names start with an identifier. */
23683 token
= cp_lexer_peek_token (parser
->lexer
);
23684 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23686 cp_parser_error (parser
, "expected class-name");
23687 return error_mark_node
;
23690 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23691 to a template-id, so we save it here. Consider object scope too,
23692 so that make_typename_type below can use it (cp_parser_template_name
23693 considers object scope also). This may happen with code like
23695 p->template A<T>::a()
23697 where we first want to look up A<T>::a in the class of the object
23698 expression, as per [basic.lookup.classref]. */
23699 scope
= parser
->scope
? parser
->scope
: parser
->context
->object_type
;
23700 if (scope
== error_mark_node
)
23701 return error_mark_node
;
23703 /* Any name names a type if we're following the `typename' keyword
23704 in a qualified name where the enclosing scope is type-dependent. */
23705 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23706 && dependent_type_p (scope
));
23707 /* Handle the common case (an identifier, but not a template-id)
23709 if (token
->type
== CPP_NAME
23710 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23712 cp_token
*identifier_token
;
23715 /* Look for the identifier. */
23716 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23717 ambiguous_p
= identifier_token
->error_reported
;
23718 identifier
= cp_parser_identifier (parser
);
23719 /* If the next token isn't an identifier, we are certainly not
23720 looking at a class-name. */
23721 if (identifier
== error_mark_node
)
23722 decl
= error_mark_node
;
23723 /* If we know this is a type-name, there's no need to look it
23725 else if (typename_p
)
23729 tree ambiguous_decls
;
23730 /* If we already know that this lookup is ambiguous, then
23731 we've already issued an error message; there's no reason
23735 cp_parser_simulate_error (parser
);
23736 return error_mark_node
;
23738 /* If the next token is a `::', then the name must be a type
23741 [basic.lookup.qual]
23743 During the lookup for a name preceding the :: scope
23744 resolution operator, object, function, and enumerator
23745 names are ignored. */
23746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23747 tag_type
= scope_type
;
23748 /* Look up the name. */
23749 decl
= cp_parser_lookup_name (parser
, identifier
,
23751 /*is_template=*/false,
23752 /*is_namespace=*/false,
23753 check_dependency_p
,
23755 identifier_token
->location
);
23756 if (ambiguous_decls
)
23758 if (cp_parser_parsing_tentatively (parser
))
23759 cp_parser_simulate_error (parser
);
23760 return error_mark_node
;
23766 /* Try a template-id. */
23767 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23768 check_dependency_p
,
23771 if (decl
== error_mark_node
)
23772 return error_mark_node
;
23775 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23777 /* If this is a typename, create a TYPENAME_TYPE. */
23779 && decl
!= error_mark_node
23780 && !is_overloaded_fn (decl
))
23782 decl
= make_typename_type (scope
, decl
, typename_type
,
23783 /*complain=*/tf_error
);
23784 if (decl
!= error_mark_node
)
23785 decl
= TYPE_NAME (decl
);
23788 decl
= strip_using_decl (decl
);
23790 /* Check to see that it is really the name of a class. */
23791 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23792 && identifier_p (TREE_OPERAND (decl
, 0))
23793 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23794 /* Situations like this:
23796 template <typename T> struct A {
23797 typename T::template X<int>::I i;
23800 are problematic. Is `T::template X<int>' a class-name? The
23801 standard does not seem to be definitive, but there is no other
23802 valid interpretation of the following `::'. Therefore, those
23803 names are considered class-names. */
23805 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23806 if (decl
!= error_mark_node
)
23807 decl
= TYPE_NAME (decl
);
23809 else if (TREE_CODE (decl
) != TYPE_DECL
23810 || TREE_TYPE (decl
) == error_mark_node
23811 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23812 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23813 /* In Objective-C 2.0, a classname followed by '.' starts a
23814 dot-syntax expression, and it's not a type-name. */
23815 || (c_dialect_objc ()
23816 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23817 && objc_is_class_name (decl
)))
23818 decl
= error_mark_node
;
23820 if (decl
== error_mark_node
)
23821 cp_parser_error (parser
, "expected class-name");
23822 else if (identifier
&& !parser
->scope
)
23823 maybe_note_name_used_in_class (identifier
, decl
);
23828 /* Make sure that any member-function parameters are in scope.
23829 For instance, a function's noexcept-specifier can use the function's
23833 void fn (int p) noexcept(noexcept(p));
23836 so we need to make sure name lookup can find them. This is used
23837 when we delay parsing of the noexcept-specifier. */
23840 inject_parm_decls (tree decl
)
23842 begin_scope (sk_function_parms
, decl
);
23843 tree args
= DECL_ARGUMENTS (decl
);
23845 do_push_parm_decls (decl
, args
, /*nonparms=*/NULL
);
23847 if (args
&& is_this_parameter (args
))
23849 gcc_checking_assert (current_class_ptr
== NULL_TREE
);
23850 current_class_ptr
= NULL_TREE
;
23851 current_class_ref
= cp_build_fold_indirect_ref (args
);
23852 current_class_ptr
= args
;
23856 /* Undo the effects of inject_parm_decls. */
23859 pop_injected_parms (void)
23861 pop_bindings_and_leave_scope ();
23862 current_class_ptr
= current_class_ref
= NULL_TREE
;
23865 /* Parse a class-specifier.
23868 class-head { member-specification [opt] }
23870 Returns the TREE_TYPE representing the class. */
23873 cp_parser_class_specifier_1 (cp_parser
* parser
)
23876 tree attributes
= NULL_TREE
;
23877 bool nested_name_specifier_p
;
23878 unsigned saved_num_template_parameter_lists
;
23879 bool saved_in_function_body
;
23880 unsigned char in_statement
;
23881 bool in_switch_statement_p
;
23882 bool saved_in_unbraced_linkage_specification_p
;
23883 tree old_scope
= NULL_TREE
;
23884 tree scope
= NULL_TREE
;
23885 cp_token
*closing_brace
;
23887 push_deferring_access_checks (dk_no_deferred
);
23889 /* Parse the class-head. */
23890 type
= cp_parser_class_head (parser
,
23891 &nested_name_specifier_p
);
23892 /* If the class-head was a semantic disaster, skip the entire body
23896 cp_parser_skip_to_end_of_block_or_statement (parser
);
23897 pop_deferring_access_checks ();
23898 return error_mark_node
;
23901 /* Look for the `{'. */
23902 matching_braces braces
;
23903 if (!braces
.require_open (parser
))
23905 pop_deferring_access_checks ();
23906 return error_mark_node
;
23909 cp_ensure_no_omp_declare_simd (parser
);
23910 cp_ensure_no_oacc_routine (parser
);
23912 /* Issue an error message if type-definitions are forbidden here. */
23913 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23914 /* Remember that we are defining one more class. */
23915 ++parser
->num_classes_being_defined
;
23916 /* Inside the class, surrounding template-parameter-lists do not
23918 saved_num_template_parameter_lists
23919 = parser
->num_template_parameter_lists
;
23920 parser
->num_template_parameter_lists
= 0;
23921 /* We are not in a function body. */
23922 saved_in_function_body
= parser
->in_function_body
;
23923 parser
->in_function_body
= false;
23924 /* Or in a loop. */
23925 in_statement
= parser
->in_statement
;
23926 parser
->in_statement
= 0;
23927 /* Or in a switch. */
23928 in_switch_statement_p
= parser
->in_switch_statement_p
;
23929 parser
->in_switch_statement_p
= false;
23930 /* We are not immediately inside an extern "lang" block. */
23931 saved_in_unbraced_linkage_specification_p
23932 = parser
->in_unbraced_linkage_specification_p
;
23933 parser
->in_unbraced_linkage_specification_p
= false;
23935 // Associate constraints with the type.
23937 type
= associate_classtype_constraints (type
);
23939 /* Start the class. */
23940 if (nested_name_specifier_p
)
23942 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23943 /* SCOPE must be a scope nested inside current scope. */
23944 if (is_nested_namespace (current_namespace
,
23945 decl_namespace_context (scope
)))
23946 old_scope
= push_inner_scope (scope
);
23948 nested_name_specifier_p
= false;
23950 type
= begin_class_definition (type
);
23952 if (type
== error_mark_node
)
23953 /* If the type is erroneous, skip the entire body of the class. */
23954 cp_parser_skip_to_closing_brace (parser
);
23956 /* Parse the member-specification. */
23957 cp_parser_member_specification_opt (parser
);
23959 /* Look for the trailing `}'. */
23960 closing_brace
= braces
.require_close (parser
);
23961 /* Look for trailing attributes to apply to this class. */
23962 if (cp_parser_allow_gnu_extensions_p (parser
))
23963 attributes
= cp_parser_gnu_attributes_opt (parser
);
23964 if (type
!= error_mark_node
)
23965 type
= finish_struct (type
, attributes
);
23966 if (nested_name_specifier_p
)
23967 pop_inner_scope (old_scope
, scope
);
23969 /* We've finished a type definition. Check for the common syntax
23970 error of forgetting a semicolon after the definition. We need to
23971 be careful, as we can't just check for not-a-semicolon and be done
23972 with it; the user might have typed:
23974 class X { } c = ...;
23975 class X { } *p = ...;
23977 and so forth. Instead, enumerate all the possible tokens that
23978 might follow this production; if we don't see one of them, then
23979 complain and silently insert the semicolon. */
23981 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23982 bool want_semicolon
= true;
23984 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23985 /* Don't try to parse c++11 attributes here. As per the
23986 grammar, that should be a task for
23987 cp_parser_decl_specifier_seq. */
23988 want_semicolon
= false;
23990 switch (token
->type
)
23993 case CPP_SEMICOLON
:
23996 case CPP_OPEN_PAREN
:
23997 case CPP_CLOSE_PAREN
:
23999 want_semicolon
= false;
24002 /* While it's legal for type qualifiers and storage class
24003 specifiers to follow type definitions in the grammar, only
24004 compiler testsuites contain code like that. Assume that if
24005 we see such code, then what we're really seeing is a case
24009 const <type> var = ...;
24014 static <type> func (...) ...
24016 i.e. the qualifier or specifier applies to the next
24017 declaration. To do so, however, we need to look ahead one
24018 more token to see if *that* token is a type specifier.
24020 This code could be improved to handle:
24023 static const <type> var = ...; */
24025 if (keyword_is_decl_specifier (token
->keyword
))
24027 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24029 /* Handling user-defined types here would be nice, but very
24032 = (lookahead
->type
== CPP_KEYWORD
24033 && keyword_begins_type_specifier (lookahead
->keyword
));
24040 /* If we don't have a type, then something is very wrong and we
24041 shouldn't try to do anything clever. Likewise for not seeing the
24043 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
24045 /* Locate the closing brace. */
24046 cp_token_position prev
24047 = cp_lexer_previous_token_position (parser
->lexer
);
24048 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
24049 location_t loc
= prev_token
->location
;
24051 /* We want to suggest insertion of a ';' immediately *after* the
24052 closing brace, so, if we can, offset the location by 1 column. */
24053 location_t next_loc
= loc
;
24054 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
24055 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
24057 rich_location
richloc (line_table
, next_loc
);
24059 /* If we successfully offset the location, suggest the fix-it. */
24060 if (next_loc
!= loc
)
24061 richloc
.add_fixit_insert_before (next_loc
, ";");
24063 if (CLASSTYPE_DECLARED_CLASS (type
))
24064 error_at (&richloc
,
24065 "expected %<;%> after class definition");
24066 else if (TREE_CODE (type
) == RECORD_TYPE
)
24067 error_at (&richloc
,
24068 "expected %<;%> after struct definition");
24069 else if (TREE_CODE (type
) == UNION_TYPE
)
24070 error_at (&richloc
,
24071 "expected %<;%> after union definition");
24073 gcc_unreachable ();
24075 /* Unget one token and smash it to look as though we encountered
24076 a semicolon in the input stream. */
24077 cp_lexer_set_token_position (parser
->lexer
, prev
);
24078 token
= cp_lexer_peek_token (parser
->lexer
);
24079 token
->type
= CPP_SEMICOLON
;
24080 token
->keyword
= RID_MAX
;
24084 /* If this class is not itself within the scope of another class,
24085 then we need to parse the bodies of all of the queued function
24086 definitions. Note that the queued functions defined in a class
24087 are not always processed immediately following the
24088 class-specifier for that class. Consider:
24091 struct B { void f() { sizeof (A); } };
24094 If `f' were processed before the processing of `A' were
24095 completed, there would be no way to compute the size of `A'.
24096 Note that the nesting we are interested in here is lexical --
24097 not the semantic nesting given by TYPE_CONTEXT. In particular,
24100 struct A { struct B; };
24101 struct A::B { void f() { } };
24103 there is no need to delay the parsing of `A::B::f'. */
24104 if (--parser
->num_classes_being_defined
== 0)
24107 tree class_type
= NULL_TREE
;
24108 tree pushed_scope
= NULL_TREE
;
24110 cp_default_arg_entry
*e
;
24111 tree save_ccp
, save_ccr
;
24113 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
24115 /* Skip default arguments, NSDMIs, etc, in order to improve
24116 error recovery (c++/71169, c++/71832). */
24117 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
24118 vec_safe_truncate (unparsed_nsdmis
, 0);
24119 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
24122 /* In a first pass, parse default arguments to the functions.
24123 Then, in a second pass, parse the bodies of the functions.
24124 This two-phased approach handles cases like:
24132 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
24135 /* If there are default arguments that have not yet been processed,
24136 take care of them now. */
24137 if (class_type
!= e
->class_type
)
24140 pop_scope (pushed_scope
);
24141 class_type
= e
->class_type
;
24142 pushed_scope
= push_scope (class_type
);
24144 /* Make sure that any template parameters are in scope. */
24145 maybe_begin_member_template_processing (decl
);
24146 /* Parse the default argument expressions. */
24147 cp_parser_late_parsing_default_args (parser
, decl
);
24148 /* Remove any template parameters from the symbol table. */
24149 maybe_end_member_template_processing ();
24151 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
24152 /* Now parse any NSDMIs. */
24153 save_ccp
= current_class_ptr
;
24154 save_ccr
= current_class_ref
;
24155 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
24157 if (class_type
!= DECL_CONTEXT (decl
))
24160 pop_scope (pushed_scope
);
24161 class_type
= DECL_CONTEXT (decl
);
24162 pushed_scope
= push_scope (class_type
);
24164 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
24165 cp_parser_late_parsing_nsdmi (parser
, decl
);
24167 vec_safe_truncate (unparsed_nsdmis
, 0);
24168 current_class_ptr
= save_ccp
;
24169 current_class_ref
= save_ccr
;
24171 pop_scope (pushed_scope
);
24173 /* If there are noexcept-specifiers that have not yet been processed,
24174 take care of them now. */
24175 class_type
= NULL_TREE
;
24176 pushed_scope
= NULL_TREE
;
24177 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts
, ix
, decl
)
24179 tree ctx
= DECL_CONTEXT (decl
);
24180 if (class_type
!= ctx
)
24183 pop_scope (pushed_scope
);
24185 pushed_scope
= push_scope (class_type
);
24188 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
24189 spec
= TREE_PURPOSE (spec
);
24191 /* Make sure that any template parameters are in scope. */
24192 maybe_begin_member_template_processing (decl
);
24194 /* Make sure that any member-function parameters are in scope. */
24195 inject_parm_decls (decl
);
24197 /* 'this' is not allowed in static member functions. */
24198 unsigned char local_variables_forbidden_p
24199 = parser
->local_variables_forbidden_p
;
24200 if (DECL_THIS_STATIC (decl
))
24201 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
24203 /* Now we can parse the noexcept-specifier. */
24204 spec
= cp_parser_late_noexcept_specifier (parser
, spec
);
24206 if (spec
!= error_mark_node
)
24207 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
24209 /* Restore the state of local_variables_forbidden_p. */
24210 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
24212 /* The finish_struct call above performed various override checking,
24213 but it skipped unparsed noexcept-specifier operands. Now that we
24214 have resolved them, check again. */
24215 noexcept_override_late_checks (type
, decl
);
24217 /* Remove any member-function parameters from the symbol table. */
24218 pop_injected_parms ();
24220 /* Remove any template parameters from the symbol table. */
24221 maybe_end_member_template_processing ();
24223 vec_safe_truncate (unparsed_noexcepts
, 0);
24225 pop_scope (pushed_scope
);
24227 /* Now parse the body of the functions. */
24230 /* OpenMP UDRs need to be parsed before all other functions. */
24231 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
24232 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
24233 cp_parser_late_parsing_for_member (parser
, decl
);
24234 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
24235 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
24236 cp_parser_late_parsing_for_member (parser
, decl
);
24239 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
24240 cp_parser_late_parsing_for_member (parser
, decl
);
24241 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
24244 /* Put back any saved access checks. */
24245 pop_deferring_access_checks ();
24247 /* Restore saved state. */
24248 parser
->in_switch_statement_p
= in_switch_statement_p
;
24249 parser
->in_statement
= in_statement
;
24250 parser
->in_function_body
= saved_in_function_body
;
24251 parser
->num_template_parameter_lists
24252 = saved_num_template_parameter_lists
;
24253 parser
->in_unbraced_linkage_specification_p
24254 = saved_in_unbraced_linkage_specification_p
;
24260 cp_parser_class_specifier (cp_parser
* parser
)
24263 timevar_push (TV_PARSE_STRUCT
);
24264 ret
= cp_parser_class_specifier_1 (parser
);
24265 timevar_pop (TV_PARSE_STRUCT
);
24269 /* Parse a class-head.
24272 class-key identifier [opt] base-clause [opt]
24273 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
24274 class-key nested-name-specifier [opt] template-id
24277 class-virt-specifier:
24281 class-key attributes identifier [opt] base-clause [opt]
24282 class-key attributes nested-name-specifier identifier base-clause [opt]
24283 class-key attributes nested-name-specifier [opt] template-id
24286 Upon return BASES is initialized to the list of base classes (or
24287 NULL, if there are none) in the same form returned by
24288 cp_parser_base_clause.
24290 Returns the TYPE of the indicated class. Sets
24291 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
24292 involving a nested-name-specifier was used, and FALSE otherwise.
24294 Returns error_mark_node if this is not a class-head.
24296 Returns NULL_TREE if the class-head is syntactically valid, but
24297 semantically invalid in a way that means we should skip the entire
24298 body of the class. */
24301 cp_parser_class_head (cp_parser
* parser
,
24302 bool* nested_name_specifier_p
)
24304 tree nested_name_specifier
;
24305 enum tag_types class_key
;
24306 tree id
= NULL_TREE
;
24307 tree type
= NULL_TREE
;
24310 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
24311 bool template_id_p
= false;
24312 bool qualified_p
= false;
24313 bool invalid_nested_name_p
= false;
24314 bool invalid_explicit_specialization_p
= false;
24315 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24316 tree pushed_scope
= NULL_TREE
;
24317 unsigned num_templates
;
24318 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
24319 /* Assume no nested-name-specifier will be present. */
24320 *nested_name_specifier_p
= false;
24321 /* Assume no template parameter lists will be used in defining the
24324 parser
->colon_corrects_to_scope_p
= false;
24326 /* Look for the class-key. */
24327 class_key
= cp_parser_class_key (parser
);
24328 if (class_key
== none_type
)
24329 return error_mark_node
;
24331 location_t class_head_start_location
= input_location
;
24333 /* Parse the attributes. */
24334 attributes
= cp_parser_attributes_opt (parser
);
24336 /* If the next token is `::', that is invalid -- but sometimes
24337 people do try to write:
24341 Handle this gracefully by accepting the extra qualifier, and then
24342 issuing an error about it later if this really is a
24343 class-head. If it turns out just to be an elaborated type
24344 specifier, remain silent. */
24345 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
24346 qualified_p
= true;
24348 push_deferring_access_checks (dk_no_check
);
24350 /* Determine the name of the class. Begin by looking for an
24351 optional nested-name-specifier. */
24352 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
24353 nested_name_specifier
24354 = cp_parser_nested_name_specifier_opt (parser
,
24355 /*typename_keyword_p=*/false,
24356 /*check_dependency_p=*/false,
24358 /*is_declaration=*/false);
24359 /* If there was a nested-name-specifier, then there *must* be an
24362 cp_token
*bad_template_keyword
= NULL
;
24364 if (nested_name_specifier
)
24366 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24367 /* Although the grammar says `identifier', it really means
24368 `class-name' or `template-name'. You are only allowed to
24369 define a class that has already been declared with this
24372 The proposed resolution for Core Issue 180 says that wherever
24373 you see `class T::X' you should treat `X' as a type-name.
24375 It is OK to define an inaccessible class; for example:
24377 class A { class B; };
24380 We do not know if we will see a class-name, or a
24381 template-name. We look for a class-name first, in case the
24382 class-name is a template-id; if we looked for the
24383 template-name first we would stop after the template-name. */
24384 cp_parser_parse_tentatively (parser
);
24385 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24386 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
24387 type
= cp_parser_class_name (parser
,
24388 /*typename_keyword_p=*/false,
24389 /*template_keyword_p=*/false,
24391 /*check_dependency_p=*/false,
24392 /*class_head_p=*/true,
24393 /*is_declaration=*/false);
24394 /* If that didn't work, ignore the nested-name-specifier. */
24395 if (!cp_parser_parse_definitely (parser
))
24397 invalid_nested_name_p
= true;
24398 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24399 id
= cp_parser_identifier (parser
);
24400 if (id
== error_mark_node
)
24403 /* If we could not find a corresponding TYPE, treat this
24404 declaration like an unqualified declaration. */
24405 if (type
== error_mark_node
)
24406 nested_name_specifier
= NULL_TREE
;
24407 /* Otherwise, count the number of templates used in TYPE and its
24408 containing scopes. */
24410 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
24412 /* Otherwise, the identifier is optional. */
24415 /* We don't know whether what comes next is a template-id,
24416 an identifier, or nothing at all. */
24417 cp_parser_parse_tentatively (parser
);
24418 /* Check for a template-id. */
24419 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24420 id
= cp_parser_template_id (parser
,
24421 /*template_keyword_p=*/false,
24422 /*check_dependency_p=*/true,
24424 /*is_declaration=*/true);
24425 /* If that didn't work, it could still be an identifier. */
24426 if (!cp_parser_parse_definitely (parser
))
24428 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
24430 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24431 id
= cp_parser_identifier (parser
);
24438 template_id_p
= true;
24443 pop_deferring_access_checks ();
24447 cp_parser_check_for_invalid_template_id (parser
, id
,
24449 type_start_token
->location
);
24451 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
24453 /* If it's not a `:' or a `{' then we can't really be looking at a
24454 class-head, since a class-head only appears as part of a
24455 class-specifier. We have to detect this situation before calling
24456 xref_tag, since that has irreversible side-effects. */
24457 if (!cp_parser_next_token_starts_class_definition_p (parser
))
24459 cp_parser_error (parser
, "expected %<{%> or %<:%>");
24460 type
= error_mark_node
;
24464 /* At this point, we're going ahead with the class-specifier, even
24465 if some other problem occurs. */
24466 cp_parser_commit_to_tentative_parse (parser
);
24467 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
24469 cp_parser_error (parser
,
24470 "cannot specify %<override%> for a class");
24471 type
= error_mark_node
;
24474 /* Issue the error about the overly-qualified name now. */
24477 cp_parser_error (parser
,
24478 "global qualification of class name is invalid");
24479 type
= error_mark_node
;
24482 else if (invalid_nested_name_p
)
24484 cp_parser_error (parser
,
24485 "qualified name does not name a class");
24486 type
= error_mark_node
;
24489 else if (nested_name_specifier
)
24493 if (bad_template_keyword
)
24494 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24495 keyword template shall not appear at the top level. */
24496 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
24497 "keyword %<template%> not allowed in class-head-name");
24499 /* Reject typedef-names in class heads. */
24500 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
24502 error_at (type_start_token
->location
,
24503 "invalid class name in declaration of %qD",
24509 /* Figure out in what scope the declaration is being placed. */
24510 scope
= current_scope ();
24511 /* If that scope does not contain the scope in which the
24512 class was originally declared, the program is invalid. */
24513 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
24515 if (at_namespace_scope_p ())
24516 error_at (type_start_token
->location
,
24517 "declaration of %qD in namespace %qD which does not "
24519 type
, scope
, nested_name_specifier
);
24521 error_at (type_start_token
->location
,
24522 "declaration of %qD in %qD which does not enclose %qD",
24523 type
, scope
, nested_name_specifier
);
24529 A declarator-id shall not be qualified except for the
24530 definition of a ... nested class outside of its class
24531 ... [or] the definition or explicit instantiation of a
24532 class member of a namespace outside of its namespace. */
24533 if (scope
== nested_name_specifier
)
24534 permerror (nested_name_specifier_token_start
->location
,
24535 "extra qualification not allowed");
24537 /* An explicit-specialization must be preceded by "template <>". If
24538 it is not, try to recover gracefully. */
24539 if (at_namespace_scope_p ()
24540 && parser
->num_template_parameter_lists
== 0
24541 && !processing_template_parmlist
24544 /* Build a location of this form:
24545 struct typename <ARGS>
24546 ^~~~~~~~~~~~~~~~~~~~~~
24547 with caret==start at the start token, and
24548 finishing at the end of the type. */
24549 location_t reported_loc
24550 = make_location (class_head_start_location
,
24551 class_head_start_location
,
24552 get_finish (type_start_token
->location
));
24553 rich_location
richloc (line_table
, reported_loc
);
24554 richloc
.add_fixit_insert_before (class_head_start_location
,
24556 error_at (&richloc
,
24557 "an explicit specialization must be preceded by"
24558 " %<template <>%>");
24559 invalid_explicit_specialization_p
= true;
24560 /* Take the same action that would have been taken by
24561 cp_parser_explicit_specialization. */
24562 ++parser
->num_template_parameter_lists
;
24563 begin_specialization ();
24565 /* There must be no "return" statements between this point and the
24566 end of this function; set "type "to the correct return value and
24567 use "goto done;" to return. */
24568 /* Make sure that the right number of template parameters were
24570 if (!cp_parser_check_template_parameters (parser
, num_templates
,
24572 type_start_token
->location
,
24573 /*declarator=*/NULL
))
24575 /* If something went wrong, there is no point in even trying to
24576 process the class-definition. */
24581 /* Look up the type. */
24584 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
24585 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
24586 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
24588 error_at (type_start_token
->location
,
24589 "function template %qD redeclared as a class template", id
);
24590 type
= error_mark_node
;
24594 type
= TREE_TYPE (id
);
24595 type
= maybe_process_partial_specialization (type
);
24597 /* Check the scope while we still know whether or not we had a
24598 nested-name-specifier. */
24599 if (type
!= error_mark_node
)
24600 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
24602 if (nested_name_specifier
)
24603 pushed_scope
= push_scope (nested_name_specifier
);
24605 else if (nested_name_specifier
)
24611 template <typename T> struct S { struct T };
24612 template <typename T> struct S<T>::T { };
24614 we will get a TYPENAME_TYPE when processing the definition of
24615 `S::T'. We need to resolve it to the actual type before we
24616 try to define it. */
24617 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
24619 class_type
= resolve_typename_type (TREE_TYPE (type
),
24620 /*only_current_p=*/false);
24621 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
24622 type
= TYPE_NAME (class_type
);
24625 cp_parser_error (parser
, "could not resolve typename type");
24626 type
= error_mark_node
;
24630 if (maybe_process_partial_specialization (TREE_TYPE (type
))
24631 == error_mark_node
)
24637 class_type
= current_class_type
;
24638 /* Enter the scope indicated by the nested-name-specifier. */
24639 pushed_scope
= push_scope (nested_name_specifier
);
24640 /* Get the canonical version of this type. */
24641 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
24642 /* Call push_template_decl if it seems like we should be defining a
24643 template either from the template headers or the type we're
24644 defining, so that we diagnose both extra and missing headers. */
24645 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24646 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
24647 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
24649 type
= push_template_decl (type
);
24650 if (type
== error_mark_node
)
24657 type
= TREE_TYPE (type
);
24658 *nested_name_specifier_p
= true;
24660 else /* The name is not a nested name. */
24662 /* If the class was unnamed, create a dummy name. */
24664 id
= make_anon_name ();
24665 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
24666 ? ts_within_enclosing_non_class
24668 type
= xref_tag (class_key
, id
, tag_scope
,
24669 parser
->num_template_parameter_lists
);
24672 /* Diagnose class/struct/union mismatches. */
24673 cp_parser_check_class_key (parser
, UNKNOWN_LOCATION
, class_key
, type
,
24676 /* Indicate whether this class was declared as a `class' or as a
24678 if (TREE_CODE (type
) == RECORD_TYPE
)
24679 CLASSTYPE_DECLARED_CLASS (type
) = class_key
== class_type
;
24681 /* If this type was already complete, and we see another definition,
24682 that's an error. Likewise if the type is already being defined:
24683 this can happen, eg, when it's defined from within an expression
24685 if (type
!= error_mark_node
24686 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
24688 error_at (type_start_token
->location
, "redefinition of %q#T",
24690 inform (location_of (type
), "previous definition of %q#T",
24695 else if (type
== error_mark_node
)
24700 /* Apply attributes now, before any use of the class as a template
24701 argument in its base list. */
24702 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24703 fixup_attribute_variants (type
);
24706 /* We will have entered the scope containing the class; the names of
24707 base classes should be looked up in that context. For example:
24709 struct A { struct B {}; struct C; };
24710 struct A::C : B {};
24714 /* Get the list of base-classes, if there is one. */
24715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24717 /* PR59482: enter the class scope so that base-specifiers are looked
24721 bases
= cp_parser_base_clause (parser
);
24722 /* PR59482: get out of the previously pushed class scope so that the
24723 subsequent pops pop the right thing. */
24730 /* If we're really defining a class, process the base classes.
24731 If they're invalid, fail. */
24732 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24733 xref_basetypes (type
, bases
);
24736 /* Leave the scope given by the nested-name-specifier. We will
24737 enter the class scope itself while processing the members. */
24739 pop_scope (pushed_scope
);
24741 if (invalid_explicit_specialization_p
)
24743 end_specialization ();
24744 --parser
->num_template_parameter_lists
;
24748 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24749 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24750 CLASSTYPE_FINAL (type
) = 1;
24752 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24756 /* Parse a class-key.
24763 Returns the kind of class-key specified, or none_type to indicate
24766 static enum tag_types
24767 cp_parser_class_key (cp_parser
* parser
)
24770 enum tag_types tag_type
;
24772 /* Look for the class-key. */
24773 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24777 /* Check to see if the TOKEN is a class-key. */
24778 tag_type
= cp_parser_token_is_class_key (token
);
24780 cp_parser_error (parser
, "expected class-key");
24784 /* Parse a type-parameter-key.
24786 type-parameter-key:
24792 cp_parser_type_parameter_key (cp_parser
* parser
)
24794 /* Look for the type-parameter-key. */
24795 enum tag_types tag_type
= none_type
;
24796 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24797 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24799 cp_lexer_consume_token (parser
->lexer
);
24800 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24801 /* typename is not allowed in a template template parameter
24802 by the standard until C++17. */
24803 pedwarn (token
->location
, OPT_Wpedantic
,
24804 "ISO C++ forbids typename key in template template parameter;"
24805 " use %<-std=c++17%> or %<-std=gnu++17%>");
24808 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24813 /* Parse an (optional) member-specification.
24815 member-specification:
24816 member-declaration member-specification [opt]
24817 access-specifier : member-specification [opt] */
24820 cp_parser_member_specification_opt (cp_parser
* parser
)
24827 /* Peek at the next token. */
24828 token
= cp_lexer_peek_token (parser
->lexer
);
24829 /* If it's a `}', or EOF then we've seen all the members. */
24830 if (token
->type
== CPP_CLOSE_BRACE
24831 || token
->type
== CPP_EOF
24832 || token
->type
== CPP_PRAGMA_EOL
)
24835 /* See if this token is a keyword. */
24836 keyword
= token
->keyword
;
24840 case RID_PROTECTED
:
24842 /* Consume the access-specifier. */
24843 cp_lexer_consume_token (parser
->lexer
);
24844 /* Remember which access-specifier is active. */
24845 current_access_specifier
= token
->u
.value
;
24846 /* Look for the `:'. */
24847 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24851 /* Accept #pragmas at class scope. */
24852 if (token
->type
== CPP_PRAGMA
)
24854 cp_parser_pragma (parser
, pragma_member
, NULL
);
24858 /* Otherwise, the next construction must be a
24859 member-declaration. */
24860 cp_parser_member_declaration (parser
);
24865 /* Parse a member-declaration.
24867 member-declaration:
24868 decl-specifier-seq [opt] member-declarator-list [opt] ;
24869 function-definition ; [opt]
24870 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24872 template-declaration
24875 member-declarator-list:
24877 member-declarator-list , member-declarator
24880 declarator pure-specifier [opt]
24881 declarator constant-initializer [opt]
24882 identifier [opt] : constant-expression
24886 member-declaration:
24887 __extension__ member-declaration
24890 declarator attributes [opt] pure-specifier [opt]
24891 declarator attributes [opt] constant-initializer [opt]
24892 identifier [opt] attributes [opt] : constant-expression
24896 member-declaration:
24897 static_assert-declaration */
24900 cp_parser_member_declaration (cp_parser
* parser
)
24902 cp_decl_specifier_seq decl_specifiers
;
24903 tree prefix_attributes
;
24905 int declares_class_or_enum
;
24907 cp_token
*token
= NULL
;
24908 cp_token
*decl_spec_token_start
= NULL
;
24909 cp_token
*initializer_token_start
= NULL
;
24910 int saved_pedantic
;
24911 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24913 /* Check for the `__extension__' keyword. */
24914 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24917 cp_parser_member_declaration (parser
);
24918 /* Restore the old value of the PEDANTIC flag. */
24919 pedantic
= saved_pedantic
;
24924 /* Check for a template-declaration. */
24925 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24927 /* An explicit specialization here is an error condition, and we
24928 expect the specialization handler to detect and report this. */
24929 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24930 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24931 cp_parser_explicit_specialization (parser
);
24933 cp_parser_template_declaration (parser
, /*member_p=*/true);
24937 /* Check for a template introduction. */
24938 else if (cp_parser_template_declaration_after_export (parser
, true))
24941 /* Check for a using-declaration. */
24942 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24944 if (cxx_dialect
< cxx11
)
24946 /* Parse the using-declaration. */
24947 cp_parser_using_declaration (parser
,
24948 /*access_declaration_p=*/false);
24954 bool alias_decl_expected
;
24955 cp_parser_parse_tentatively (parser
);
24956 decl
= cp_parser_alias_declaration (parser
);
24957 /* Note that if we actually see the '=' token after the
24958 identifier, cp_parser_alias_declaration commits the
24959 tentative parse. In that case, we really expect an
24960 alias-declaration. Otherwise, we expect a using
24962 alias_decl_expected
=
24963 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24964 cp_parser_parse_definitely (parser
);
24966 if (alias_decl_expected
)
24967 finish_member_declaration (decl
);
24969 cp_parser_using_declaration (parser
,
24970 /*access_declaration_p=*/false);
24975 /* Check for @defs. */
24976 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24979 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24980 ivar
= ivar_chains
;
24984 ivar
= TREE_CHAIN (member
);
24985 TREE_CHAIN (member
) = NULL_TREE
;
24986 finish_member_declaration (member
);
24991 /* If the next token is `static_assert' we have a static assertion. */
24992 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24994 cp_parser_static_assert (parser
, /*member_p=*/true);
24998 parser
->colon_corrects_to_scope_p
= false;
25000 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
25003 /* Parse the decl-specifier-seq. */
25004 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
25005 cp_parser_decl_specifier_seq (parser
,
25006 (CP_PARSER_FLAGS_OPTIONAL
25007 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
25009 &declares_class_or_enum
);
25010 /* Check for an invalid type-name. */
25011 if (!decl_specifiers
.any_type_specifiers_p
25012 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
25014 /* If there is no declarator, then the decl-specifier-seq should
25016 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25018 /* If there was no decl-specifier-seq, and the next token is a
25019 `;', then we have something like:
25025 Each member-declaration shall declare at least one member
25026 name of the class. */
25027 if (!decl_specifiers
.any_specifiers_p
)
25029 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25030 if (!in_system_header_at (token
->location
))
25032 gcc_rich_location
richloc (token
->location
);
25033 richloc
.add_fixit_remove ();
25034 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
25041 /* See if this declaration is a friend. */
25042 friend_p
= cp_parser_friend_p (&decl_specifiers
);
25043 /* If there were decl-specifiers, check to see if there was
25044 a class-declaration. */
25045 type
= check_tag_decl (&decl_specifiers
,
25046 /*explicit_type_instantiation_p=*/false);
25047 /* Nested classes have already been added to the class, but
25048 a `friend' needs to be explicitly registered. */
25051 /* If the `friend' keyword was present, the friend must
25052 be introduced with a class-key. */
25053 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
25054 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
25055 "in C++03 a class-key must be used "
25056 "when declaring a friend");
25059 template <typename T> struct A {
25060 friend struct A<T>::B;
25063 A<T>::B will be represented by a TYPENAME_TYPE, and
25064 therefore not recognized by check_tag_decl. */
25067 type
= decl_specifiers
.type
;
25068 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
25069 type
= TREE_TYPE (type
);
25071 if (!type
|| !TYPE_P (type
))
25072 error_at (decl_spec_token_start
->location
,
25073 "friend declaration does not name a class or "
25076 make_friend_class (current_class_type
, type
,
25077 /*complain=*/true);
25079 /* If there is no TYPE, an error message will already have
25081 else if (!type
|| type
== error_mark_node
)
25083 /* An anonymous aggregate has to be handled specially; such
25084 a declaration really declares a data member (with a
25085 particular type), as opposed to a nested class. */
25086 else if (ANON_AGGR_TYPE_P (type
))
25089 if (decl_specifiers
.storage_class
!= sc_none
)
25090 error_at (decl_spec_token_start
->location
,
25091 "a storage class on an anonymous aggregate "
25092 "in class scope is not allowed");
25094 /* Remove constructors and such from TYPE, now that we
25095 know it is an anonymous aggregate. */
25096 fixup_anonymous_aggr (type
);
25097 /* And make the corresponding data member. */
25098 decl
= build_decl (decl_spec_token_start
->location
,
25099 FIELD_DECL
, NULL_TREE
, type
);
25100 /* Add it to the class. */
25101 finish_member_declaration (decl
);
25104 cp_parser_check_access_in_redeclaration
25106 decl_spec_token_start
->location
);
25111 bool assume_semicolon
= false;
25113 /* Clear attributes from the decl_specifiers but keep them
25114 around as prefix attributes that apply them to the entity
25116 prefix_attributes
= decl_specifiers
.attributes
;
25117 decl_specifiers
.attributes
= NULL_TREE
;
25119 /* See if these declarations will be friends. */
25120 friend_p
= cp_parser_friend_p (&decl_specifiers
);
25122 /* Keep going until we hit the `;' at the end of the
25124 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25126 tree attributes
= NULL_TREE
;
25127 tree first_attribute
;
25129 bool named_bitfld
= false;
25131 /* Peek at the next token. */
25132 token
= cp_lexer_peek_token (parser
->lexer
);
25134 /* The following code wants to know early if it is a bit-field
25135 or some other declaration. Attributes can appear before
25136 the `:' token. Skip over them without consuming any tokens
25137 to peek if they are followed by `:'. */
25138 if (cp_next_tokens_can_be_attribute_p (parser
)
25139 || (token
->type
== CPP_NAME
25140 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
25141 && (named_bitfld
= true)))
25144 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
25145 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
25148 /* Check for a bitfield declaration. */
25149 if (token
->type
== CPP_COLON
25150 || (token
->type
== CPP_NAME
25151 && token
== cp_lexer_peek_token (parser
->lexer
)
25152 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
25153 && (named_bitfld
= true)))
25157 tree late_attributes
= NULL_TREE
;
25158 location_t id_location
25159 = cp_lexer_peek_token (parser
->lexer
)->location
;
25162 identifier
= cp_parser_identifier (parser
);
25164 identifier
= NULL_TREE
;
25166 /* Look for attributes that apply to the bitfield. */
25167 attributes
= cp_parser_attributes_opt (parser
);
25169 /* Consume the `:' token. */
25170 cp_lexer_consume_token (parser
->lexer
);
25172 /* Get the width of the bitfield. */
25173 width
= cp_parser_constant_expression (parser
, false, NULL
,
25174 cxx_dialect
>= cxx11
);
25176 /* In C++20 and as extension for C++11 and above we allow
25177 default member initializers for bit-fields. */
25178 initializer
= NULL_TREE
;
25179 if (cxx_dialect
>= cxx11
25180 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
25181 || cp_lexer_next_token_is (parser
->lexer
,
25185 = cp_lexer_peek_token (parser
->lexer
)->location
;
25186 if (cxx_dialect
< cxx20
25187 && identifier
!= NULL_TREE
)
25189 "default member initializers for bit-fields "
25190 "only available with %<-std=c++20%> or "
25191 "%<-std=gnu++20%>");
25193 initializer
= cp_parser_save_nsdmi (parser
);
25194 if (identifier
== NULL_TREE
)
25196 error_at (loc
, "default member initializer for "
25197 "unnamed bit-field");
25198 initializer
= NULL_TREE
;
25203 /* Look for attributes that apply to the bitfield after
25204 the `:' token and width. This is where GCC used to
25205 parse attributes in the past, pedwarn if there is
25206 a std attribute. */
25207 if (cp_next_tokens_can_be_std_attribute_p (parser
))
25208 pedwarn (input_location
, OPT_Wpedantic
,
25209 "ISO C++ allows bit-field attributes only "
25210 "before the %<:%> token");
25212 late_attributes
= cp_parser_attributes_opt (parser
);
25215 attributes
= attr_chainon (attributes
, late_attributes
);
25217 /* Remember which attributes are prefix attributes and
25219 first_attribute
= attributes
;
25220 /* Combine the attributes. */
25221 attributes
= attr_chainon (prefix_attributes
, attributes
);
25223 /* Create the bitfield declaration. */
25224 decl
= grokbitfield (identifier
25225 ? make_id_declarator (NULL_TREE
,
25231 width
, initializer
,
25236 cp_declarator
*declarator
;
25237 tree asm_specification
;
25238 int ctor_dtor_or_conv_p
;
25239 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
25240 cp_parser_flags flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
25242 && !decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
25243 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
25245 /* Parse the declarator. */
25247 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
25249 &ctor_dtor_or_conv_p
,
25250 /*parenthesized_p=*/NULL
,
25252 friend_p
, static_p
);
25254 /* If something went wrong parsing the declarator, make sure
25255 that we at least consume some tokens. */
25256 if (declarator
== cp_error_declarator
)
25258 /* Skip to the end of the statement. */
25259 cp_parser_skip_to_end_of_statement (parser
);
25260 /* If the next token is not a semicolon, that is
25261 probably because we just skipped over the body of
25262 a function. So, we consume a semicolon if
25263 present, but do not issue an error message if it
25265 if (cp_lexer_next_token_is (parser
->lexer
,
25267 cp_lexer_consume_token (parser
->lexer
);
25271 if (declares_class_or_enum
& 2)
25272 cp_parser_check_for_definition_in_return_type
25273 (declarator
, decl_specifiers
.type
,
25274 decl_specifiers
.locations
[ds_type_spec
]);
25276 /* Look for an asm-specification. */
25277 asm_specification
= cp_parser_asm_specification_opt (parser
);
25278 /* Look for attributes that apply to the declaration. */
25279 attributes
= cp_parser_attributes_opt (parser
);
25280 /* Remember which attributes are prefix attributes and
25282 first_attribute
= attributes
;
25283 /* Combine the attributes. */
25284 attributes
= attr_chainon (prefix_attributes
, attributes
);
25286 /* If it's an `=', then we have a constant-initializer or a
25287 pure-specifier. It is not correct to parse the
25288 initializer before registering the member declaration
25289 since the member declaration should be in scope while
25290 its initializer is processed. However, the rest of the
25291 front end does not yet provide an interface that allows
25292 us to handle this correctly. */
25293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
25297 A pure-specifier shall be used only in the declaration of
25298 a virtual function.
25300 A member-declarator can contain a constant-initializer
25301 only if it declares a static member of integral or
25304 Therefore, if the DECLARATOR is for a function, we look
25305 for a pure-specifier; otherwise, we look for a
25306 constant-initializer. When we call `grokfield', it will
25307 perform more stringent semantics checks. */
25308 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
25309 if (function_declarator_p (declarator
)
25310 || (decl_specifiers
.type
25311 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
25312 && declarator
->kind
== cdk_id
25313 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
25314 == FUNCTION_TYPE
)))
25315 initializer
= cp_parser_pure_specifier (parser
);
25316 else if (decl_specifiers
.storage_class
!= sc_static
)
25317 initializer
= cp_parser_save_nsdmi (parser
);
25318 else if (cxx_dialect
>= cxx11
)
25321 /* Don't require a constant rvalue in C++11, since we
25322 might want a reference constant. We'll enforce
25323 constancy later. */
25324 cp_lexer_consume_token (parser
->lexer
);
25325 /* Parse the initializer. */
25326 initializer
= cp_parser_initializer_clause (parser
,
25330 /* Parse the initializer. */
25331 initializer
= cp_parser_constant_initializer (parser
);
25333 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
25334 && !function_declarator_p (declarator
))
25337 if (decl_specifiers
.storage_class
!= sc_static
)
25338 initializer
= cp_parser_save_nsdmi (parser
);
25340 initializer
= cp_parser_initializer (parser
, &x
, &x
);
25342 /* Detect invalid bit-field cases such as
25348 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
25349 /* If there were no type specifiers, it was a
25351 && decl_specifiers
.any_type_specifiers_p
)
25353 /* This is called for a decent diagnostic only. */
25354 tree d
= grokdeclarator (declarator
, &decl_specifiers
,
25355 BITFIELD
, /*initialized=*/false,
25357 if (!error_operand_p (d
))
25358 error_at (DECL_SOURCE_LOCATION (d
),
25359 "bit-field %qD has non-integral type %qT",
25361 cp_parser_skip_to_end_of_statement (parser
);
25362 /* Avoid "extra ;" pedwarns. */
25363 if (cp_lexer_next_token_is (parser
->lexer
,
25365 cp_lexer_consume_token (parser
->lexer
);
25368 /* Otherwise, there is no initializer. */
25370 initializer
= NULL_TREE
;
25372 /* See if we are probably looking at a function
25373 definition. We are certainly not looking at a
25374 member-declarator. Calling `grokfield' has
25375 side-effects, so we must not do it unless we are sure
25376 that we are looking at a member-declarator. */
25377 if (cp_parser_token_starts_function_definition_p
25378 (cp_lexer_peek_token (parser
->lexer
)))
25380 /* The grammar does not allow a pure-specifier to be
25381 used when a member function is defined. (It is
25382 possible that this fact is an oversight in the
25383 standard, since a pure function may be defined
25384 outside of the class-specifier. */
25385 if (initializer
&& initializer_token_start
)
25386 error_at (initializer_token_start
->location
,
25387 "pure-specifier on function-definition");
25388 decl
= cp_parser_save_member_function_body (parser
,
25392 if (parser
->fully_implicit_function_template_p
)
25393 decl
= finish_fully_implicit_template (parser
, decl
);
25394 /* If the member was not a friend, declare it here. */
25396 finish_member_declaration (decl
);
25397 /* Peek at the next token. */
25398 token
= cp_lexer_peek_token (parser
->lexer
);
25399 /* If the next token is a semicolon, consume it. */
25400 if (token
->type
== CPP_SEMICOLON
)
25402 location_t semicolon_loc
25403 = cp_lexer_consume_token (parser
->lexer
)->location
;
25404 gcc_rich_location
richloc (semicolon_loc
);
25405 richloc
.add_fixit_remove ();
25406 warning_at (&richloc
, OPT_Wextra_semi
,
25407 "extra %<;%> after in-class "
25408 "function definition");
25413 if (declarator
->kind
== cdk_function
)
25414 declarator
->id_loc
= token
->location
;
25415 /* Create the declaration. */
25416 decl
= grokfield (declarator
, &decl_specifiers
,
25417 initializer
, /*init_const_expr_p=*/true,
25418 asm_specification
, attributes
);
25419 if (parser
->fully_implicit_function_template_p
)
25422 finish_fully_implicit_template (parser
, 0);
25424 decl
= finish_fully_implicit_template (parser
, decl
);
25428 cp_finalize_omp_declare_simd (parser
, decl
);
25429 cp_finalize_oacc_routine (parser
, decl
, false);
25431 /* Reset PREFIX_ATTRIBUTES. */
25432 if (attributes
!= error_mark_node
)
25434 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25435 attributes
= TREE_CHAIN (attributes
);
25437 TREE_CHAIN (attributes
) = NULL_TREE
;
25440 /* If there is any qualification still in effect, clear it
25441 now; we will be starting fresh with the next declarator. */
25442 parser
->scope
= NULL_TREE
;
25443 parser
->qualifying_scope
= NULL_TREE
;
25444 parser
->object_scope
= NULL_TREE
;
25445 /* If it's a `,', then there are more declarators. */
25446 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25448 cp_lexer_consume_token (parser
->lexer
);
25449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25451 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
25452 gcc_rich_location
richloc (token
->location
);
25453 richloc
.add_fixit_remove ();
25454 error_at (&richloc
, "stray %<,%> at end of "
25455 "member declaration");
25458 /* If the next token isn't a `;', then we have a parse error. */
25459 else if (cp_lexer_next_token_is_not (parser
->lexer
,
25462 /* The next token might be a ways away from where the
25463 actual semicolon is missing. Find the previous token
25464 and use that for our error position. */
25465 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
25466 gcc_rich_location
richloc (token
->location
);
25467 richloc
.add_fixit_insert_after (";");
25468 error_at (&richloc
, "expected %<;%> at end of "
25469 "member declaration");
25471 /* Assume that the user meant to provide a semicolon. If
25472 we were to cp_parser_skip_to_end_of_statement, we might
25473 skip to a semicolon inside a member function definition
25474 and issue nonsensical error messages. */
25475 assume_semicolon
= true;
25480 /* Add DECL to the list of members. */
25482 /* Explicitly include, eg, NSDMIs, for better error
25483 recovery (c++/58650). */
25484 || !DECL_DECLARES_FUNCTION_P (decl
))
25485 finish_member_declaration (decl
);
25487 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25488 cp_parser_save_default_args (parser
, decl
);
25489 else if (TREE_CODE (decl
) == FIELD_DECL
25490 && DECL_INITIAL (decl
))
25491 /* Add DECL to the queue of NSDMI to be parsed later. */
25492 vec_safe_push (unparsed_nsdmis
, decl
);
25495 if (assume_semicolon
)
25500 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25502 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
25505 /* Parse a pure-specifier.
25510 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25511 Otherwise, ERROR_MARK_NODE is returned. */
25514 cp_parser_pure_specifier (cp_parser
* parser
)
25518 /* Look for the `=' token. */
25519 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25520 return error_mark_node
;
25521 /* Look for the `0' token. */
25522 token
= cp_lexer_peek_token (parser
->lexer
);
25524 if (token
->type
== CPP_EOF
25525 || token
->type
== CPP_PRAGMA_EOL
)
25526 return error_mark_node
;
25528 cp_lexer_consume_token (parser
->lexer
);
25530 /* Accept = default or = delete in c++0x mode. */
25531 if (token
->keyword
== RID_DEFAULT
25532 || token
->keyword
== RID_DELETE
)
25534 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
25535 return token
->u
.value
;
25538 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25539 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
25541 cp_parser_error (parser
,
25542 "invalid pure specifier (only %<= 0%> is allowed)");
25543 cp_parser_skip_to_end_of_statement (parser
);
25544 return error_mark_node
;
25546 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25548 error_at (token
->location
, "templates may not be %<virtual%>");
25549 return error_mark_node
;
25552 return integer_zero_node
;
25555 /* Parse a constant-initializer.
25557 constant-initializer:
25558 = constant-expression
25560 Returns a representation of the constant-expression. */
25563 cp_parser_constant_initializer (cp_parser
* parser
)
25565 /* Look for the `=' token. */
25566 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25567 return error_mark_node
;
25569 /* It is invalid to write:
25571 struct S { static const int i = { 7 }; };
25574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25576 cp_parser_error (parser
,
25577 "a brace-enclosed initializer is not allowed here");
25578 /* Consume the opening brace. */
25579 matching_braces braces
;
25580 braces
.consume_open (parser
);
25581 /* Skip the initializer. */
25582 cp_parser_skip_to_closing_brace (parser
);
25583 /* Look for the trailing `}'. */
25584 braces
.require_close (parser
);
25586 return error_mark_node
;
25589 return cp_parser_constant_expression (parser
);
25592 /* Derived classes [gram.class.derived] */
25594 /* Parse a base-clause.
25597 : base-specifier-list
25599 base-specifier-list:
25600 base-specifier ... [opt]
25601 base-specifier-list , base-specifier ... [opt]
25603 Returns a TREE_LIST representing the base-classes, in the order in
25604 which they were declared. The representation of each node is as
25605 described by cp_parser_base_specifier.
25607 In the case that no bases are specified, this function will return
25608 NULL_TREE, not ERROR_MARK_NODE. */
25611 cp_parser_base_clause (cp_parser
* parser
)
25613 tree bases
= NULL_TREE
;
25615 /* Look for the `:' that begins the list. */
25616 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25618 /* Scan the base-specifier-list. */
25623 bool pack_expansion_p
= false;
25625 /* Look for the base-specifier. */
25626 base
= cp_parser_base_specifier (parser
);
25627 /* Look for the (optional) ellipsis. */
25628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25630 /* Consume the `...'. */
25631 cp_lexer_consume_token (parser
->lexer
);
25633 pack_expansion_p
= true;
25636 /* Add BASE to the front of the list. */
25637 if (base
&& base
!= error_mark_node
)
25639 if (pack_expansion_p
)
25640 /* Make this a pack expansion type. */
25641 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
25643 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
25645 TREE_CHAIN (base
) = bases
;
25649 /* Peek at the next token. */
25650 token
= cp_lexer_peek_token (parser
->lexer
);
25651 /* If it's not a comma, then the list is complete. */
25652 if (token
->type
!= CPP_COMMA
)
25654 /* Consume the `,'. */
25655 cp_lexer_consume_token (parser
->lexer
);
25658 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25659 base class had a qualified name. However, the next name that
25660 appears is certainly not qualified. */
25661 parser
->scope
= NULL_TREE
;
25662 parser
->qualifying_scope
= NULL_TREE
;
25663 parser
->object_scope
= NULL_TREE
;
25665 return nreverse (bases
);
25668 /* Parse a base-specifier.
25671 :: [opt] nested-name-specifier [opt] class-name
25672 virtual access-specifier [opt] :: [opt] nested-name-specifier
25674 access-specifier virtual [opt] :: [opt] nested-name-specifier
25677 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25678 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25679 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25680 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25683 cp_parser_base_specifier (cp_parser
* parser
)
25687 bool virtual_p
= false;
25688 bool duplicate_virtual_error_issued_p
= false;
25689 bool duplicate_access_error_issued_p
= false;
25690 bool class_scope_p
, template_p
;
25691 tree access
= access_default_node
;
25694 /* Process the optional `virtual' and `access-specifier'. */
25697 /* Peek at the next token. */
25698 token
= cp_lexer_peek_token (parser
->lexer
);
25699 /* Process `virtual'. */
25700 switch (token
->keyword
)
25703 /* If `virtual' appears more than once, issue an error. */
25704 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
25706 cp_parser_error (parser
,
25707 "%<virtual%> specified more than once in base-specifier");
25708 duplicate_virtual_error_issued_p
= true;
25713 /* Consume the `virtual' token. */
25714 cp_lexer_consume_token (parser
->lexer
);
25719 case RID_PROTECTED
:
25721 /* If more than one access specifier appears, issue an
25723 if (access
!= access_default_node
25724 && !duplicate_access_error_issued_p
)
25726 cp_parser_error (parser
,
25727 "more than one access specifier in base-specifier");
25728 duplicate_access_error_issued_p
= true;
25731 access
= ridpointers
[(int) token
->keyword
];
25733 /* Consume the access-specifier. */
25734 cp_lexer_consume_token (parser
->lexer
);
25743 /* It is not uncommon to see programs mechanically, erroneously, use
25744 the 'typename' keyword to denote (dependent) qualified types
25745 as base classes. */
25746 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25748 token
= cp_lexer_peek_token (parser
->lexer
);
25749 if (!processing_template_decl
)
25750 error_at (token
->location
,
25751 "keyword %<typename%> not allowed outside of templates");
25753 error_at (token
->location
,
25754 "keyword %<typename%> not allowed in this context "
25755 "(the base class is implicitly a type)");
25756 cp_lexer_consume_token (parser
->lexer
);
25759 /* Look for the optional `::' operator. */
25760 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25761 /* Look for the nested-name-specifier. The simplest way to
25766 The keyword `typename' is not permitted in a base-specifier or
25767 mem-initializer; in these contexts a qualified name that
25768 depends on a template-parameter is implicitly assumed to be a
25771 is to pretend that we have seen the `typename' keyword at this
25773 cp_parser_nested_name_specifier_opt (parser
,
25774 /*typename_keyword_p=*/true,
25775 /*check_dependency_p=*/true,
25777 /*is_declaration=*/true);
25778 /* If the base class is given by a qualified name, assume that names
25779 we see are type names or templates, as appropriate. */
25780 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25781 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25784 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25785 /* DR 950 allows decltype as a base-specifier. */
25786 type
= cp_parser_decltype (parser
);
25789 /* Otherwise, look for the class-name. */
25790 type
= cp_parser_class_name (parser
,
25794 /*check_dependency_p=*/true,
25795 /*class_head_p=*/false,
25796 /*is_declaration=*/true);
25797 type
= TREE_TYPE (type
);
25800 if (type
== error_mark_node
)
25801 return error_mark_node
;
25803 return finish_base_specifier (type
, access
, virtual_p
);
25806 /* Exception handling [gram.exception] */
25808 /* Save the tokens that make up the noexcept-specifier for a member-function.
25809 Returns a DEFERRED_PARSE. */
25812 cp_parser_save_noexcept (cp_parser
*parser
)
25814 cp_token
*first
= parser
->lexer
->next_token
;
25815 /* We want everything up to, including, the final ')'. */
25816 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0);
25817 cp_token
*last
= parser
->lexer
->next_token
;
25819 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25820 to carry the information we will need. */
25821 tree expr
= make_node (DEFERRED_PARSE
);
25822 /* Save away the noexcept-specifier; we will process it when the
25823 class is complete. */
25824 DEFPARSE_TOKENS (expr
) = cp_token_cache_new (first
, last
);
25825 expr
= build_tree_list (expr
, NULL_TREE
);
25829 /* Used for late processing of noexcept-specifiers of member-functions.
25830 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25831 we saved for later; parse it now. DECL is the declaration of the
25832 member function. */
25835 cp_parser_late_noexcept_specifier (cp_parser
*parser
, tree default_arg
)
25837 /* Make sure we've gotten something that hasn't been parsed yet. */
25838 gcc_assert (TREE_CODE (default_arg
) == DEFERRED_PARSE
);
25840 push_unparsed_function_queues (parser
);
25842 /* Push the saved tokens for the noexcept-specifier onto the parser's
25844 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_arg
);
25845 cp_parser_push_lexer_for_tokens (parser
, tokens
);
25847 /* Parse the cached noexcept-specifier. */
25849 = cp_parser_noexcept_specification_opt (parser
,
25850 CP_PARSER_FLAGS_NONE
,
25851 /*require_constexpr=*/true,
25852 /*consumed_expr=*/NULL
,
25853 /*return_cond=*/false);
25855 /* Revert to the main lexer. */
25856 cp_parser_pop_lexer (parser
);
25858 /* Restore the queue. */
25859 pop_unparsed_function_queues (parser
);
25861 /* And we're done. */
25865 /* Perform late checking of overriding function with respect to their
25866 noexcept-specifiers. TYPE is the class and FNDECL is the function
25867 that potentially overrides some virtual function with the same
25871 noexcept_override_late_checks (tree type
, tree fndecl
)
25873 tree binfo
= TYPE_BINFO (type
);
25876 if (DECL_STATIC_FUNCTION_P (fndecl
))
25879 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
25881 tree basetype
= BINFO_TYPE (base_binfo
);
25883 if (!TYPE_POLYMORPHIC_P (basetype
))
25886 tree fn
= look_for_overrides_here (basetype
, fndecl
);
25888 maybe_check_overriding_exception_spec (fndecl
, fn
);
25892 /* Parse an (optional) noexcept-specification.
25894 noexcept-specification:
25895 noexcept ( constant-expression ) [opt]
25897 If no noexcept-specification is present, returns NULL_TREE.
25898 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25899 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25900 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25901 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25902 in which case a boolean condition is returned instead. The parser flags
25903 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
25904 the (member) function is `const'. */
25907 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25908 cp_parser_flags flags
,
25909 bool require_constexpr
,
25910 bool* consumed_expr
,
25914 const char *saved_message
;
25916 /* Peek at the next token. */
25917 token
= cp_lexer_peek_token (parser
->lexer
);
25919 /* Is it a noexcept-specification? */
25920 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25924 /* [class.mem]/6 says that a noexcept-specifer (within the
25925 member-specification of the class) is a complete-class context of
25926 a class. So, if the noexcept-specifier has the optional expression,
25927 just save the tokens, and reparse this after we're done with the
25929 const bool literal_p
25930 = ((cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
25931 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
25932 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
));
25934 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
)
25935 /* No need to delay parsing for a number literal or true/false. */
25937 && at_class_scope_p ()
25938 /* We don't delay parsing for friend member functions,
25939 alias-declarations, and typedefs, even though the standard seems
25941 && (flags
& CP_PARSER_FLAGS_DELAY_NOEXCEPT
)
25942 && TYPE_BEING_DEFINED (current_class_type
)
25943 && !LAMBDA_TYPE_P (current_class_type
))
25944 return cp_parser_save_noexcept (parser
);
25946 cp_lexer_consume_token (parser
->lexer
);
25948 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25950 matching_parens parens
;
25951 parens
.consume_open (parser
);
25953 if (require_constexpr
)
25955 /* Types may not be defined in an exception-specification. */
25956 saved_message
= parser
->type_definition_forbidden_message
;
25957 parser
->type_definition_forbidden_message
25958 = G_("types may not be defined in an exception-specification");
25960 bool non_constant_p
;
25962 = cp_parser_constant_expression (parser
,
25963 /*allow_non_constant=*/true,
25966 && !require_potential_rvalue_constant_expression (expr
))
25969 return_cond
= true;
25972 /* Restore the saved message. */
25973 parser
->type_definition_forbidden_message
= saved_message
;
25977 expr
= cp_parser_expression (parser
);
25978 *consumed_expr
= true;
25981 parens
.require_close (parser
);
25985 expr
= boolean_true_node
;
25986 if (!require_constexpr
)
25987 *consumed_expr
= false;
25990 /* We cannot build a noexcept-spec right away because this will check
25991 that expr is a constexpr. */
25993 return build_noexcept_spec (expr
, tf_warning_or_error
);
26001 /* Parse an (optional) exception-specification.
26003 exception-specification:
26004 throw ( type-id-list [opt] )
26006 Returns a TREE_LIST representing the exception-specification. The
26007 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26008 control parsing. QUALS are qualifiers indicating whether the (member)
26009 function is `const'. */
26012 cp_parser_exception_specification_opt (cp_parser
* parser
,
26013 cp_parser_flags flags
)
26017 const char *saved_message
;
26019 /* Peek at the next token. */
26020 token
= cp_lexer_peek_token (parser
->lexer
);
26022 /* Is it a noexcept-specification? */
26024 = cp_parser_noexcept_specification_opt (parser
, flags
,
26025 /*require_constexpr=*/true,
26026 /*consumed_expr=*/NULL
,
26027 /*return_cond=*/false);
26028 if (type_id_list
!= NULL_TREE
)
26029 return type_id_list
;
26031 /* If it's not `throw', then there's no exception-specification. */
26032 if (!cp_parser_is_keyword (token
, RID_THROW
))
26035 location_t loc
= token
->location
;
26037 /* Consume the `throw'. */
26038 cp_lexer_consume_token (parser
->lexer
);
26040 /* Look for the `('. */
26041 matching_parens parens
;
26042 parens
.require_open (parser
);
26044 /* Peek at the next token. */
26045 token
= cp_lexer_peek_token (parser
->lexer
);
26046 /* If it's not a `)', then there is a type-id-list. */
26047 if (token
->type
!= CPP_CLOSE_PAREN
)
26049 /* Types may not be defined in an exception-specification. */
26050 saved_message
= parser
->type_definition_forbidden_message
;
26051 parser
->type_definition_forbidden_message
26052 = G_("types may not be defined in an exception-specification");
26053 /* Parse the type-id-list. */
26054 type_id_list
= cp_parser_type_id_list (parser
);
26055 /* Restore the saved message. */
26056 parser
->type_definition_forbidden_message
= saved_message
;
26058 if (cxx_dialect
>= cxx17
)
26060 error_at (loc
, "ISO C++17 does not allow dynamic exception "
26062 type_id_list
= NULL_TREE
;
26064 else if (cxx_dialect
>= cxx11
)
26065 warning_at (loc
, OPT_Wdeprecated
,
26066 "dynamic exception specifications are deprecated in "
26069 /* In C++17, throw() is equivalent to noexcept (true). throw()
26070 is deprecated in C++11 and above as well, but is still widely used,
26071 so don't warn about it yet. */
26072 else if (cxx_dialect
>= cxx17
)
26073 type_id_list
= noexcept_true_spec
;
26075 type_id_list
= empty_except_spec
;
26077 /* Look for the `)'. */
26078 parens
.require_close (parser
);
26080 return type_id_list
;
26083 /* Parse an (optional) type-id-list.
26087 type-id-list , type-id ... [opt]
26089 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26090 in the order that the types were presented. */
26093 cp_parser_type_id_list (cp_parser
* parser
)
26095 tree types
= NULL_TREE
;
26102 token
= cp_lexer_peek_token (parser
->lexer
);
26104 /* Get the next type-id. */
26105 type
= cp_parser_type_id (parser
);
26106 /* Check for invalid 'auto'. */
26107 if (flag_concepts
&& type_uses_auto (type
))
26109 error_at (token
->location
,
26110 "invalid use of %<auto%> in exception-specification");
26111 type
= error_mark_node
;
26113 /* Parse the optional ellipsis. */
26114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26116 /* Consume the `...'. */
26117 cp_lexer_consume_token (parser
->lexer
);
26119 /* Turn the type into a pack expansion expression. */
26120 type
= make_pack_expansion (type
);
26122 /* Add it to the list. */
26123 types
= add_exception_specifier (types
, type
, /*complain=*/1);
26124 /* Peek at the next token. */
26125 token
= cp_lexer_peek_token (parser
->lexer
);
26126 /* If it is not a `,', we are done. */
26127 if (token
->type
!= CPP_COMMA
)
26129 /* Consume the `,'. */
26130 cp_lexer_consume_token (parser
->lexer
);
26133 return nreverse (types
);
26136 /* Parse a try-block.
26139 try compound-statement handler-seq */
26142 cp_parser_try_block (cp_parser
* parser
)
26146 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
26147 if (parser
->in_function_body
26148 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
26149 && cxx_dialect
< cxx20
)
26150 pedwarn (input_location
, 0,
26151 "%<try%> in %<constexpr%> function only "
26152 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26154 try_block
= begin_try_block ();
26155 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
26156 finish_try_block (try_block
);
26157 cp_parser_handler_seq (parser
);
26158 finish_handler_sequence (try_block
);
26163 /* Parse a function-try-block.
26165 function-try-block:
26166 try ctor-initializer [opt] function-body handler-seq */
26169 cp_parser_function_try_block (cp_parser
* parser
)
26171 tree compound_stmt
;
26174 /* Look for the `try' keyword. */
26175 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
26177 /* Let the rest of the front end know where we are. */
26178 try_block
= begin_function_try_block (&compound_stmt
);
26179 /* Parse the function-body. */
26180 cp_parser_ctor_initializer_opt_and_function_body
26181 (parser
, /*in_function_try_block=*/true);
26182 /* We're done with the `try' part. */
26183 finish_function_try_block (try_block
);
26184 /* Parse the handlers. */
26185 cp_parser_handler_seq (parser
);
26186 /* We're done with the handlers. */
26187 finish_function_handler_sequence (try_block
, compound_stmt
);
26190 /* Parse a handler-seq.
26193 handler handler-seq [opt] */
26196 cp_parser_handler_seq (cp_parser
* parser
)
26202 /* Parse the handler. */
26203 cp_parser_handler (parser
);
26204 /* Peek at the next token. */
26205 token
= cp_lexer_peek_token (parser
->lexer
);
26206 /* If it's not `catch' then there are no more handlers. */
26207 if (!cp_parser_is_keyword (token
, RID_CATCH
))
26212 /* Parse a handler.
26215 catch ( exception-declaration ) compound-statement */
26218 cp_parser_handler (cp_parser
* parser
)
26223 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
26224 handler
= begin_handler ();
26225 matching_parens parens
;
26226 parens
.require_open (parser
);
26227 declaration
= cp_parser_exception_declaration (parser
);
26228 finish_handler_parms (declaration
, handler
);
26229 parens
.require_close (parser
);
26230 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
26231 finish_handler (handler
);
26234 /* Parse an exception-declaration.
26236 exception-declaration:
26237 type-specifier-seq declarator
26238 type-specifier-seq abstract-declarator
26242 Returns a VAR_DECL for the declaration, or NULL_TREE if the
26243 ellipsis variant is used. */
26246 cp_parser_exception_declaration (cp_parser
* parser
)
26248 cp_decl_specifier_seq type_specifiers
;
26249 cp_declarator
*declarator
;
26250 const char *saved_message
;
26252 /* If it's an ellipsis, it's easy to handle. */
26253 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26255 /* Consume the `...' token. */
26256 cp_lexer_consume_token (parser
->lexer
);
26260 /* Types may not be defined in exception-declarations. */
26261 saved_message
= parser
->type_definition_forbidden_message
;
26262 parser
->type_definition_forbidden_message
26263 = G_("types may not be defined in exception-declarations");
26265 /* Parse the type-specifier-seq. */
26266 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
26267 /*is_declaration=*/true,
26268 /*is_trailing_return=*/false,
26270 /* If it's a `)', then there is no declarator. */
26271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
26274 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
26275 CP_PARSER_FLAGS_NONE
,
26276 /*ctor_dtor_or_conv_p=*/NULL
,
26277 /*parenthesized_p=*/NULL
,
26278 /*member_p=*/false,
26279 /*friend_p=*/false,
26280 /*static_p=*/false);
26282 /* Restore the saved message. */
26283 parser
->type_definition_forbidden_message
= saved_message
;
26285 if (!type_specifiers
.any_specifiers_p
)
26286 return error_mark_node
;
26288 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
26291 /* Parse a throw-expression.
26294 throw assignment-expression [opt]
26296 Returns a THROW_EXPR representing the throw-expression. */
26299 cp_parser_throw_expression (cp_parser
* parser
)
26303 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26305 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
26306 token
= cp_lexer_peek_token (parser
->lexer
);
26307 /* Figure out whether or not there is an assignment-expression
26308 following the "throw" keyword. */
26309 if (token
->type
== CPP_COMMA
26310 || token
->type
== CPP_SEMICOLON
26311 || token
->type
== CPP_CLOSE_PAREN
26312 || token
->type
== CPP_CLOSE_SQUARE
26313 || token
->type
== CPP_CLOSE_BRACE
26314 || token
->type
== CPP_COLON
)
26315 expression
= NULL_TREE
;
26317 expression
= cp_parser_assignment_expression (parser
);
26319 /* Construct a location e.g.:
26322 with caret == start at the start of the "throw" token, and
26323 the end at the end of the final token we consumed. */
26324 location_t combined_loc
= make_location (start_loc
, start_loc
,
26326 expression
= build_throw (combined_loc
, expression
);
26331 /* Parse a yield-expression.
26334 co_yield assignment-expression
26335 co_yield braced-init-list
26337 Returns a CO_YIELD_EXPR representing the yield-expression. */
26340 cp_parser_yield_expression (cp_parser
* parser
)
26344 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26345 location_t kw_loc
= token
->location
; /* Save for later. */
26347 cp_parser_require_keyword (parser
, RID_CO_YIELD
, RT_CO_YIELD
);
26349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26351 bool expr_non_constant_p
;
26352 cp_lexer_set_source_position (parser
->lexer
);
26353 /* ??? : probably a moot point? */
26354 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
26355 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
26358 expr
= cp_parser_assignment_expression (parser
);
26360 if (expr
== error_mark_node
)
26363 return finish_co_yield_expr (kw_loc
, expr
);
26366 /* GNU Extensions */
26368 /* Parse an (optional) asm-specification.
26371 asm ( string-literal )
26373 If the asm-specification is present, returns a STRING_CST
26374 corresponding to the string-literal. Otherwise, returns
26378 cp_parser_asm_specification_opt (cp_parser
* parser
)
26381 tree asm_specification
;
26383 /* Peek at the next token. */
26384 token
= cp_lexer_peek_token (parser
->lexer
);
26385 /* If the next token isn't the `asm' keyword, then there's no
26386 asm-specification. */
26387 if (!cp_parser_is_keyword (token
, RID_ASM
))
26390 /* Consume the `asm' token. */
26391 cp_lexer_consume_token (parser
->lexer
);
26392 /* Look for the `('. */
26393 matching_parens parens
;
26394 parens
.require_open (parser
);
26396 /* Look for the string-literal. */
26397 asm_specification
= cp_parser_string_literal (parser
, false, false);
26399 /* Look for the `)'. */
26400 parens
.require_close (parser
);
26402 return asm_specification
;
26405 /* Parse an asm-operand-list.
26409 asm-operand-list , asm-operand
26412 string-literal ( expression )
26413 [ string-literal ] string-literal ( expression )
26415 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26416 each node is the expression. The TREE_PURPOSE is itself a
26417 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26418 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26419 is a STRING_CST for the string literal before the parenthesis. Returns
26420 ERROR_MARK_NODE if any of the operands are invalid. */
26423 cp_parser_asm_operand_list (cp_parser
* parser
)
26425 tree asm_operands
= NULL_TREE
;
26426 bool invalid_operands
= false;
26430 tree string_literal
;
26434 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
26436 /* Consume the `[' token. */
26437 cp_lexer_consume_token (parser
->lexer
);
26438 /* Read the operand name. */
26439 name
= cp_parser_identifier (parser
);
26440 if (name
!= error_mark_node
)
26441 name
= build_string (IDENTIFIER_LENGTH (name
),
26442 IDENTIFIER_POINTER (name
));
26443 /* Look for the closing `]'. */
26444 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
26448 /* Look for the string-literal. */
26449 string_literal
= cp_parser_string_literal (parser
, false, false);
26451 /* Look for the `('. */
26452 matching_parens parens
;
26453 parens
.require_open (parser
);
26454 /* Parse the expression. */
26455 expression
= cp_parser_expression (parser
);
26456 /* Look for the `)'. */
26457 parens
.require_close (parser
);
26459 if (name
== error_mark_node
26460 || string_literal
== error_mark_node
26461 || expression
== error_mark_node
)
26462 invalid_operands
= true;
26464 /* Add this operand to the list. */
26465 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
26468 /* If the next token is not a `,', there are no more
26470 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26472 /* Consume the `,'. */
26473 cp_lexer_consume_token (parser
->lexer
);
26476 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
26479 /* Parse an asm-clobber-list.
26483 asm-clobber-list , string-literal
26485 Returns a TREE_LIST, indicating the clobbers in the order that they
26486 appeared. The TREE_VALUE of each node is a STRING_CST. */
26489 cp_parser_asm_clobber_list (cp_parser
* parser
)
26491 tree clobbers
= NULL_TREE
;
26495 tree string_literal
;
26497 /* Look for the string literal. */
26498 string_literal
= cp_parser_string_literal (parser
, false, false);
26499 /* Add it to the list. */
26500 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
26501 /* If the next token is not a `,', then the list is
26503 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26505 /* Consume the `,' token. */
26506 cp_lexer_consume_token (parser
->lexer
);
26512 /* Parse an asm-label-list.
26516 asm-label-list , identifier
26518 Returns a TREE_LIST, indicating the labels in the order that they
26519 appeared. The TREE_VALUE of each node is a label. */
26522 cp_parser_asm_label_list (cp_parser
* parser
)
26524 tree labels
= NULL_TREE
;
26528 tree identifier
, label
, name
;
26530 /* Look for the identifier. */
26531 identifier
= cp_parser_identifier (parser
);
26532 if (!error_operand_p (identifier
))
26534 label
= lookup_label (identifier
);
26535 if (TREE_CODE (label
) == LABEL_DECL
)
26537 TREE_USED (label
) = 1;
26538 check_goto (label
);
26539 name
= build_string (IDENTIFIER_LENGTH (identifier
),
26540 IDENTIFIER_POINTER (identifier
));
26541 labels
= tree_cons (name
, label
, labels
);
26544 /* If the next token is not a `,', then the list is
26546 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26548 /* Consume the `,' token. */
26549 cp_lexer_consume_token (parser
->lexer
);
26552 return nreverse (labels
);
26555 /* Return TRUE iff the next tokens in the stream are possibly the
26556 beginning of a GNU extension attribute. */
26559 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
26561 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
26564 /* Return TRUE iff the next tokens in the stream are possibly the
26565 beginning of a standard C++-11 attribute specifier. */
26568 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
26570 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
26573 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26574 beginning of a standard C++-11 attribute specifier. */
26577 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
26579 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26581 return (cxx_dialect
>= cxx11
26582 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
26583 || (token
->type
== CPP_OPEN_SQUARE
26584 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
26585 && token
->type
== CPP_OPEN_SQUARE
)));
26588 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26589 beginning of a GNU extension attribute. */
26592 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
26594 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26596 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
26599 /* Return true iff the next tokens can be the beginning of either a
26600 GNU attribute list, or a standard C++11 attribute sequence. */
26603 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
26605 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
26606 || cp_next_tokens_can_be_std_attribute_p (parser
));
26609 /* Return true iff the next Nth tokens can be the beginning of either
26610 a GNU attribute list, or a standard C++11 attribute sequence. */
26613 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
26615 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
26616 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
26619 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26620 of GNU attributes, or return NULL. */
26623 cp_parser_attributes_opt (cp_parser
*parser
)
26625 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
26626 return cp_parser_gnu_attributes_opt (parser
);
26627 return cp_parser_std_attribute_spec_seq (parser
);
26630 /* Parse an (optional) series of attributes.
26633 attributes attribute
26636 __attribute__ (( attribute-list [opt] ))
26638 The return value is as for cp_parser_gnu_attribute_list. */
26641 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
26643 tree attributes
= NULL_TREE
;
26645 temp_override
<bool> cleanup
26646 (parser
->auto_is_implicit_function_template_parm_p
, false);
26651 tree attribute_list
;
26654 /* Peek at the next token. */
26655 token
= cp_lexer_peek_token (parser
->lexer
);
26656 /* If it's not `__attribute__', then we're done. */
26657 if (token
->keyword
!= RID_ATTRIBUTE
)
26660 /* Consume the `__attribute__' keyword. */
26661 cp_lexer_consume_token (parser
->lexer
);
26662 /* Look for the two `(' tokens. */
26663 matching_parens outer_parens
;
26664 if (!outer_parens
.require_open (parser
))
26666 matching_parens inner_parens
;
26667 if (!inner_parens
.require_open (parser
))
26670 /* Peek at the next token. */
26671 token
= cp_lexer_peek_token (parser
->lexer
);
26672 if (token
->type
!= CPP_CLOSE_PAREN
)
26673 /* Parse the attribute-list. */
26674 attribute_list
= cp_parser_gnu_attribute_list (parser
);
26676 /* If the next token is a `)', then there is no attribute
26678 attribute_list
= NULL
;
26680 /* Look for the two `)' tokens. */
26681 if (!inner_parens
.require_close (parser
))
26683 if (!outer_parens
.require_close (parser
))
26686 cp_parser_skip_to_end_of_statement (parser
);
26688 /* Add these new attributes to the list. */
26689 attributes
= attr_chainon (attributes
, attribute_list
);
26695 /* Parse a GNU attribute-list.
26699 attribute-list , attribute
26703 identifier ( identifier )
26704 identifier ( identifier , expression-list )
26705 identifier ( expression-list )
26707 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26708 to an attribute. The TREE_PURPOSE of each node is the identifier
26709 indicating which attribute is in use. The TREE_VALUE represents
26710 the arguments, if any. */
26713 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
26715 tree attribute_list
= NULL_TREE
;
26716 bool save_translate_strings_p
= parser
->translate_strings_p
;
26718 /* Don't create wrapper nodes within attributes: the
26719 handlers don't know how to handle them. */
26720 auto_suppress_location_wrappers sentinel
;
26722 parser
->translate_strings_p
= false;
26729 /* Look for the identifier. We also allow keywords here; for
26730 example `__attribute__ ((const))' is legal. */
26731 token
= cp_lexer_peek_token (parser
->lexer
);
26732 if (token
->type
== CPP_NAME
26733 || token
->type
== CPP_KEYWORD
)
26735 tree arguments
= NULL_TREE
;
26737 /* Consume the token, but save it since we need it for the
26738 SIMD enabled function parsing. */
26739 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
26741 /* Save away the identifier that indicates which attribute
26743 identifier
= (token
->type
== CPP_KEYWORD
)
26744 /* For keywords, use the canonical spelling, not the
26745 parsed identifier. */
26746 ? ridpointers
[(int) token
->keyword
]
26747 : id_token
->u
.value
;
26749 identifier
= canonicalize_attr_name (identifier
);
26750 attribute
= build_tree_list (identifier
, NULL_TREE
);
26752 /* Peek at the next token. */
26753 token
= cp_lexer_peek_token (parser
->lexer
);
26754 /* If it's an `(', then parse the attribute arguments. */
26755 if (token
->type
== CPP_OPEN_PAREN
)
26757 vec
<tree
, va_gc
> *vec
;
26758 int attr_flag
= (attribute_takes_identifier_p (identifier
)
26759 ? id_attr
: normal_attr
);
26760 vec
= cp_parser_parenthesized_expression_list
26761 (parser
, attr_flag
, /*cast_p=*/false,
26762 /*allow_expansion_p=*/false,
26763 /*non_constant_p=*/NULL
);
26765 arguments
= error_mark_node
;
26768 arguments
= build_tree_list_vec (vec
);
26769 release_tree_vector (vec
);
26771 /* Save the arguments away. */
26772 TREE_VALUE (attribute
) = arguments
;
26775 if (arguments
!= error_mark_node
)
26777 /* Add this attribute to the list. */
26778 TREE_CHAIN (attribute
) = attribute_list
;
26779 attribute_list
= attribute
;
26782 token
= cp_lexer_peek_token (parser
->lexer
);
26784 /* Unless EXACTLY_ONE is set look for more attributes.
26785 If the next token isn't a `,', we're done. */
26786 if (exactly_one
|| token
->type
!= CPP_COMMA
)
26789 /* Consume the comma and keep going. */
26790 cp_lexer_consume_token (parser
->lexer
);
26792 parser
->translate_strings_p
= save_translate_strings_p
;
26794 /* We built up the list in reverse order. */
26795 return nreverse (attribute_list
);
26798 /* Parse a standard C++11 attribute.
26800 The returned representation is a TREE_LIST which TREE_PURPOSE is
26801 the scoped name of the attribute, and the TREE_VALUE is its
26804 Note that the scoped name of the attribute is itself a TREE_LIST
26805 which TREE_PURPOSE is the namespace of the attribute, and
26806 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26807 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26808 and which TREE_PURPOSE is directly the attribute name.
26810 Clients of the attribute code should use get_attribute_namespace
26811 and get_attribute_name to get the actual namespace and name of
26812 attributes, regardless of their being GNU or C++11 attributes.
26815 attribute-token attribute-argument-clause [opt]
26819 attribute-scoped-token
26821 attribute-scoped-token:
26822 attribute-namespace :: identifier
26824 attribute-namespace:
26827 attribute-argument-clause:
26828 ( balanced-token-seq )
26830 balanced-token-seq:
26831 balanced-token [opt]
26832 balanced-token-seq balanced-token
26835 ( balanced-token-seq )
26836 [ balanced-token-seq ]
26837 { balanced-token-seq }. */
26840 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
26842 tree attribute
, attr_id
= NULL_TREE
, arguments
;
26845 temp_override
<bool> cleanup
26846 (parser
->auto_is_implicit_function_template_parm_p
, false);
26848 /* First, parse name of the attribute, a.k.a attribute-token. */
26850 token
= cp_lexer_peek_token (parser
->lexer
);
26851 if (token
->type
== CPP_NAME
)
26852 attr_id
= token
->u
.value
;
26853 else if (token
->type
== CPP_KEYWORD
)
26854 attr_id
= ridpointers
[(int) token
->keyword
];
26855 else if (token
->flags
& NAMED_OP
)
26856 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26858 if (attr_id
== NULL_TREE
)
26861 cp_lexer_consume_token (parser
->lexer
);
26863 token
= cp_lexer_peek_token (parser
->lexer
);
26864 if (token
->type
== CPP_SCOPE
)
26866 /* We are seeing a scoped attribute token. */
26868 cp_lexer_consume_token (parser
->lexer
);
26870 error_at (token
->location
, "attribute using prefix used together "
26871 "with scoped attribute token");
26874 token
= cp_lexer_peek_token (parser
->lexer
);
26875 if (token
->type
== CPP_NAME
)
26876 attr_id
= token
->u
.value
;
26877 else if (token
->type
== CPP_KEYWORD
)
26878 attr_id
= ridpointers
[(int) token
->keyword
];
26879 else if (token
->flags
& NAMED_OP
)
26880 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26883 error_at (token
->location
,
26884 "expected an identifier for the attribute name");
26885 return error_mark_node
;
26887 cp_lexer_consume_token (parser
->lexer
);
26889 attr_ns
= canonicalize_attr_name (attr_ns
);
26890 attr_id
= canonicalize_attr_name (attr_id
);
26891 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26893 token
= cp_lexer_peek_token (parser
->lexer
);
26897 attr_ns
= canonicalize_attr_name (attr_ns
);
26898 attr_id
= canonicalize_attr_name (attr_id
);
26899 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26904 attr_id
= canonicalize_attr_name (attr_id
);
26905 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26907 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26908 but no longer: we have to be able to tell [[noreturn]] and
26909 __attribute__((noreturn)) apart. */
26910 /* C++14 deprecated attribute is equivalent to GNU's. */
26911 if (is_attribute_p ("deprecated", attr_id
))
26912 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26913 /* C++17 fallthrough attribute is equivalent to GNU's. */
26914 else if (is_attribute_p ("fallthrough", attr_id
))
26915 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26916 /* Transactional Memory TS optimize_for_synchronized attribute is
26917 equivalent to GNU transaction_callable. */
26918 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26919 TREE_PURPOSE (attribute
)
26920 = get_identifier ("transaction_callable");
26921 /* Transactional Memory attributes are GNU attributes. */
26922 else if (tm_attr_to_mask (attr_id
))
26923 TREE_PURPOSE (attribute
) = attr_id
;
26926 /* Now parse the optional argument clause of the attribute. */
26928 if (token
->type
!= CPP_OPEN_PAREN
)
26932 vec
<tree
, va_gc
> *vec
;
26933 int attr_flag
= normal_attr
;
26935 /* Maybe we don't expect to see any arguments for this attribute. */
26936 const attribute_spec
*as
26937 = lookup_attribute_spec (TREE_PURPOSE (attribute
));
26938 if (as
&& as
->max_length
== 0)
26940 error_at (token
->location
, "%qE attribute does not take any arguments",
26942 cp_parser_skip_to_closing_parenthesis (parser
,
26943 /*recovering=*/true,
26944 /*or_comma=*/false,
26945 /*consume_paren=*/true);
26946 return error_mark_node
;
26949 if (attr_ns
== gnu_identifier
26950 && attribute_takes_identifier_p (attr_id
))
26951 /* A GNU attribute that takes an identifier in parameter. */
26952 attr_flag
= id_attr
;
26956 /* For unknown attributes, just skip balanced tokens instead of
26957 trying to parse the arguments. */
26958 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1) - 1; n
; --n
)
26959 cp_lexer_consume_token (parser
->lexer
);
26963 vec
= cp_parser_parenthesized_expression_list
26964 (parser
, attr_flag
, /*cast_p=*/false,
26965 /*allow_expansion_p=*/true,
26966 /*non_constant_p=*/NULL
);
26968 arguments
= error_mark_node
;
26971 if (vec
->is_empty ())
26972 /* e.g. [[attr()]]. */
26973 error_at (token
->location
, "parentheses must be omitted if "
26974 "%qE attribute argument list is empty",
26976 arguments
= build_tree_list_vec (vec
);
26977 release_tree_vector (vec
);
26980 if (arguments
== error_mark_node
)
26981 attribute
= error_mark_node
;
26983 TREE_VALUE (attribute
) = arguments
;
26989 /* Check that the attribute ATTRIBUTE appears at most once in the
26990 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3),
26991 nodiscard, and deprecated (7.6.5). Note that
26992 carries_dependency (7.6.4) isn't implemented yet in GCC. */
26995 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26999 tree name
= get_attribute_name (attribute
);
27000 if (is_attribute_p ("noreturn", name
)
27001 && lookup_attribute ("noreturn", attributes
))
27002 error ("attribute %<noreturn%> can appear at most once "
27003 "in an attribute-list");
27004 else if (is_attribute_p ("deprecated", name
)
27005 && lookup_attribute ("deprecated", attributes
))
27006 error ("attribute %<deprecated%> can appear at most once "
27007 "in an attribute-list");
27008 else if (is_attribute_p ("nodiscard", name
)
27009 && lookup_attribute ("nodiscard", attributes
))
27010 error ("attribute %<nodiscard%> can appear at most once "
27011 "in an attribute-list");
27015 /* Parse a list of standard C++-11 attributes.
27019 attribute-list , attribute[opt]
27021 attribute-list , attribute ...
27025 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
27027 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
27028 cp_token
*token
= NULL
;
27032 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
27033 if (attribute
== error_mark_node
)
27035 if (attribute
!= NULL_TREE
)
27037 cp_parser_check_std_attribute (attributes
, attribute
);
27038 TREE_CHAIN (attribute
) = attributes
;
27039 attributes
= attribute
;
27041 token
= cp_lexer_peek_token (parser
->lexer
);
27042 if (token
->type
== CPP_ELLIPSIS
)
27044 cp_lexer_consume_token (parser
->lexer
);
27045 if (attribute
== NULL_TREE
)
27046 error_at (token
->location
,
27047 "expected attribute before %<...%>");
27050 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
27051 if (pack
== error_mark_node
)
27052 return error_mark_node
;
27053 TREE_VALUE (attribute
) = pack
;
27055 token
= cp_lexer_peek_token (parser
->lexer
);
27057 if (token
->type
!= CPP_COMMA
)
27059 cp_lexer_consume_token (parser
->lexer
);
27061 attributes
= nreverse (attributes
);
27065 /* Parse a standard C++-11 attribute specifier.
27067 attribute-specifier:
27068 [ [ attribute-using-prefix [opt] attribute-list ] ]
27069 alignment-specifier
27071 attribute-using-prefix:
27072 using attribute-namespace :
27074 alignment-specifier:
27075 alignas ( type-id ... [opt] )
27076 alignas ( alignment-expression ... [opt] ). */
27079 cp_parser_std_attribute_spec (cp_parser
*parser
)
27081 tree attributes
= NULL_TREE
;
27082 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27084 if (token
->type
== CPP_OPEN_SQUARE
27085 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
27087 tree attr_ns
= NULL_TREE
;
27089 cp_lexer_consume_token (parser
->lexer
);
27090 cp_lexer_consume_token (parser
->lexer
);
27092 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27094 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
27095 if (token
->type
== CPP_NAME
)
27096 attr_ns
= token
->u
.value
;
27097 else if (token
->type
== CPP_KEYWORD
)
27098 attr_ns
= ridpointers
[(int) token
->keyword
];
27099 else if (token
->flags
& NAMED_OP
)
27100 attr_ns
= get_identifier (cpp_type2name (token
->type
,
27103 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
27105 if (cxx_dialect
< cxx17
)
27106 pedwarn (input_location
, 0,
27107 "attribute using prefix only available "
27108 "with %<-std=c++17%> or %<-std=gnu++17%>");
27110 cp_lexer_consume_token (parser
->lexer
);
27111 cp_lexer_consume_token (parser
->lexer
);
27112 cp_lexer_consume_token (parser
->lexer
);
27115 attr_ns
= NULL_TREE
;
27118 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
27120 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
27121 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
27122 cp_parser_skip_to_end_of_statement (parser
);
27124 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27125 when we are sure that we have actually parsed them. */
27126 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
27132 /* Look for an alignment-specifier. */
27134 token
= cp_lexer_peek_token (parser
->lexer
);
27136 if (token
->type
!= CPP_KEYWORD
27137 || token
->keyword
!= RID_ALIGNAS
)
27140 cp_lexer_consume_token (parser
->lexer
);
27141 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
27143 matching_parens parens
;
27144 if (!parens
.require_open (parser
))
27145 return error_mark_node
;
27147 cp_parser_parse_tentatively (parser
);
27148 alignas_expr
= cp_parser_type_id (parser
);
27150 if (!cp_parser_parse_definitely (parser
))
27152 alignas_expr
= cp_parser_assignment_expression (parser
);
27153 if (alignas_expr
== error_mark_node
)
27154 cp_parser_skip_to_end_of_statement (parser
);
27155 if (alignas_expr
== NULL_TREE
27156 || alignas_expr
== error_mark_node
)
27157 return alignas_expr
;
27160 alignas_expr
= cxx_alignas_expr (alignas_expr
);
27161 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
27163 /* Handle alignas (pack...). */
27164 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27166 cp_lexer_consume_token (parser
->lexer
);
27167 alignas_expr
= make_pack_expansion (alignas_expr
);
27170 /* Something went wrong, so don't build the attribute. */
27171 if (alignas_expr
== error_mark_node
)
27172 return error_mark_node
;
27174 /* Missing ')' means the code cannot possibly be valid; go ahead
27175 and commit to make sure we issue a hard error. */
27176 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
27177 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
27178 cp_parser_commit_to_tentative_parse (parser
);
27180 if (!parens
.require_close (parser
))
27181 return error_mark_node
;
27183 /* Build the C++-11 representation of an 'aligned'
27186 = build_tree_list (build_tree_list (gnu_identifier
,
27187 aligned_identifier
), alignas_expr
);
27193 /* Parse a standard C++-11 attribute-specifier-seq.
27195 attribute-specifier-seq:
27196 attribute-specifier-seq [opt] attribute-specifier
27200 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
27202 tree attr_specs
= NULL_TREE
;
27203 tree attr_last
= NULL_TREE
;
27205 /* Don't create wrapper nodes within attributes: the
27206 handlers don't know how to handle them. */
27207 auto_suppress_location_wrappers sentinel
;
27211 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
27212 if (attr_spec
== NULL_TREE
)
27214 if (attr_spec
== error_mark_node
)
27215 return error_mark_node
;
27218 TREE_CHAIN (attr_last
) = attr_spec
;
27220 attr_specs
= attr_last
= attr_spec
;
27221 attr_last
= tree_last (attr_last
);
27227 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
27228 return index of the first token after balanced-token, or N on failure. */
27231 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
27234 int nparens
= 0, nbraces
= 0, nsquares
= 0;
27236 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
27238 case CPP_PRAGMA_EOL
:
27239 if (!parser
->lexer
->in_pragma
)
27243 /* Ran out of tokens. */
27245 case CPP_OPEN_PAREN
:
27248 case CPP_OPEN_BRACE
:
27251 case CPP_OPEN_SQUARE
:
27254 case CPP_CLOSE_PAREN
:
27257 case CPP_CLOSE_BRACE
:
27260 case CPP_CLOSE_SQUARE
:
27266 while (nparens
|| nbraces
|| nsquares
);
27270 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
27271 return index of the first token after the GNU attribute tokens, or N on
27275 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
27279 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
27280 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
27281 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
27284 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
27287 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
27294 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
27295 next token), return index of the first token after the standard C++11
27296 attribute tokens, or N on failure. */
27299 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
27303 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
27304 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
27306 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
27309 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
27313 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
27314 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
27316 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
27327 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
27328 as the next token), return index of the first token after the attribute
27329 tokens, or N on failure. */
27332 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
27334 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
27335 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
27336 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
27339 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27340 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27341 current value of the PEDANTIC flag, regardless of whether or not
27342 the `__extension__' keyword is present. The caller is responsible
27343 for restoring the value of the PEDANTIC flag. */
27346 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
27348 /* Save the old value of the PEDANTIC flag. */
27349 *saved_pedantic
= pedantic
;
27351 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
27353 /* Consume the `__extension__' token. */
27354 cp_lexer_consume_token (parser
->lexer
);
27355 /* We're not being pedantic while the `__extension__' keyword is
27365 /* Parse a label declaration.
27368 __label__ label-declarator-seq ;
27370 label-declarator-seq:
27371 identifier , label-declarator-seq
27375 cp_parser_label_declaration (cp_parser
* parser
)
27377 /* Look for the `__label__' keyword. */
27378 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
27384 /* Look for an identifier. */
27385 identifier
= cp_parser_identifier (parser
);
27386 /* If we failed, stop. */
27387 if (identifier
== error_mark_node
)
27389 /* Declare it as a label. */
27390 finish_label_decl (identifier
);
27391 /* If the next token is a `;', stop. */
27392 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27394 /* Look for the `,' separating the label declarations. */
27395 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
27398 /* Look for the final `;'. */
27399 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27402 // -------------------------------------------------------------------------- //
27403 // Concept definitions
27406 cp_parser_concept_definition (cp_parser
*parser
)
27408 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
));
27409 cp_lexer_consume_token (parser
->lexer
);
27411 cp_expr id
= cp_parser_identifier (parser
);
27412 if (id
== error_mark_node
)
27414 cp_parser_skip_to_end_of_statement (parser
);
27415 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27419 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27421 cp_parser_skip_to_end_of_statement (parser
);
27422 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27423 return error_mark_node
;
27426 processing_constraint_expression_sentinel parsing_constraint
;
27427 tree init
= cp_parser_constraint_expression (parser
);
27428 if (init
== error_mark_node
)
27429 cp_parser_skip_to_end_of_statement (parser
);
27431 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27432 but continue as if it were. */
27433 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27435 return finish_concept_definition (id
, init
);
27438 // -------------------------------------------------------------------------- //
27441 /* Diagnose an expression that should appear in ()'s within a requires-clause
27442 and suggest where to place those parentheses. */
27445 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc
)
27447 error_at (loc
, "expression must be enclosed in parentheses");
27451 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc
)
27453 gcc_rich_location
richloc (loc
);
27454 richloc
.add_fixit_insert_before ("(");
27455 richloc
.add_fixit_insert_after (")");
27456 error_at (&richloc
, "expression must be enclosed in parentheses");
27459 /* Characterizes the likely kind of expression intended by a mis-written
27460 primary constraint. */
27461 enum primary_constraint_error
27464 pce_maybe_operator
,
27468 /* Returns true if the token(s) following a primary-expression in a
27469 constraint-logical-* expression would require parentheses. */
27471 static primary_constraint_error
27472 cp_parser_constraint_requires_parens (cp_parser
*parser
, bool lambda_p
)
27474 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27475 switch (token
->type
)
27482 /* An equal sign may be part of the definition of a function,
27483 and not an assignment operator, when parsing the expression
27484 for a trailing requires-clause. For example:
27486 template<typename T>
27488 S() requires C<T> = default;
27491 Don't try to reparse this a binary operator. */
27492 if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DELETE
)
27493 || cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DEFAULT
))
27496 gcc_fallthrough ();
27499 /* Arithmetic operators. */
27505 /* Bitwise operators. */
27511 /* Relational operators. */
27517 case CPP_GREATER_EQ
:
27518 case CPP_SPACESHIP
:
27519 /* Pointer-to-member. */
27521 case CPP_DEREF_STAR
:
27522 /* Assignment operators. */
27531 case CPP_RSHIFT_EQ
:
27532 case CPP_LSHIFT_EQ
:
27533 /* Conditional operator */
27535 /* Unenclosed binary or conditional operator. */
27536 return pce_maybe_operator
;
27538 case CPP_OPEN_PAREN
:
27540 /* A primary constraint that precedes the parameter-list of a
27541 lambda expression is followed by an open paren.
27543 []<typename T> requires C (T a, T b) { ... }
27545 Don't try to re-parse this as a postfix expression. */
27549 gcc_fallthrough ();
27551 case CPP_OPEN_SQUARE
:
27553 /* A primary-constraint-expression followed by a '[[' is not a
27554 postfix expression. */
27555 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
))
27558 gcc_fallthrough ();
27560 case CPP_PLUS_PLUS
:
27561 case CPP_MINUS_MINUS
:
27564 /* Unenclosed postfix operator. */
27565 return pce_maybe_postfix
;
27569 /* Returns true if the next token begins a unary expression, preceded by
27570 an operator or keyword. */
27573 cp_parser_unary_constraint_requires_parens (cp_parser
*parser
)
27575 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27576 switch (token
->type
)
27583 case CPP_PLUS_PLUS
:
27584 case CPP_MINUS_MINUS
:
27589 switch (token
->keyword
)
27593 case RID_REINTCAST
:
27594 case RID_CONSTCAST
:
27616 /* Parse a primary expression within a constraint. */
27619 cp_parser_constraint_primary_expression (cp_parser
*parser
, bool lambda_p
)
27621 /* If this looks like a unary expression, parse it as such, but diagnose
27622 it as ill-formed; it requires parens. */
27623 if (cp_parser_unary_constraint_requires_parens (parser
))
27625 cp_expr e
= cp_parser_assignment_expression (parser
, NULL
, false, false);
27626 cp_parser_diagnose_ungrouped_constraint_rich (e
.get_location());
27630 cp_lexer_save_tokens (parser
->lexer
);
27632 location_t loc
= input_location
;
27633 cp_expr expr
= cp_parser_primary_expression (parser
,
27634 /*address_p=*/false,
27636 /*template_arg_p=*/false,
27638 expr
.maybe_add_location_wrapper ();
27640 primary_constraint_error pce
= pce_ok
;
27641 if (expr
!= error_mark_node
)
27643 /* The primary-expression could be part of an unenclosed non-logical
27644 compound expression. */
27645 pce
= cp_parser_constraint_requires_parens (parser
, lambda_p
);
27649 cp_lexer_commit_tokens (parser
->lexer
);
27650 return finish_constraint_primary_expr (expr
);
27653 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27654 error, but return the expression as if it were valid. */
27655 cp_lexer_rollback_tokens (parser
->lexer
);
27656 cp_parser_parse_tentatively (parser
);
27657 if (pce
== pce_maybe_operator
)
27658 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
27660 expr
= cp_parser_simple_cast_expression (parser
);
27661 if (cp_parser_parse_definitely (parser
))
27663 cp_parser_diagnose_ungrouped_constraint_rich (expr
.get_location());
27667 /* Otherwise, something has gone very wrong, and we can't generate a more
27668 meaningful diagnostic or recover. */
27669 cp_parser_diagnose_ungrouped_constraint_plain (loc
);
27670 return error_mark_node
;
27673 /* Parse a constraint-logical-and-expression.
27675 constraint-logical-and-expression:
27677 constraint-logical-and-expression '&&' primary-expression */
27680 cp_parser_constraint_logical_and_expression (cp_parser
*parser
, bool lambda_p
)
27682 cp_expr lhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
27683 while (cp_lexer_next_token_is (parser
->lexer
, CPP_AND_AND
))
27685 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
27686 tree rhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
27687 lhs
= finish_constraint_and_expr (op
->location
, lhs
, rhs
);
27692 /* Parse a constraint-logical-or-expression.
27694 constraint-logical-or-expression:
27695 constraint-logical-and-expression
27696 constraint-logical-or-expression '||' constraint-logical-and-expression */
27699 cp_parser_constraint_logical_or_expression (cp_parser
*parser
, bool lambda_p
)
27701 cp_expr lhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
27702 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OR_OR
))
27704 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
27705 cp_expr rhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
27706 lhs
= finish_constraint_or_expr (op
->location
, lhs
, rhs
);
27711 /* Parse the expression after a requires-clause. This has a different grammar
27712 than that in the concepts TS. */
27715 cp_parser_requires_clause_expression (cp_parser
*parser
, bool lambda_p
)
27717 processing_constraint_expression_sentinel parsing_constraint
;
27718 temp_override
<int> ovr (processing_template_decl
);
27719 if (!processing_template_decl
)
27720 /* Adjust processing_template_decl so that we always obtain template
27721 trees here. We don't do the usual ++processing_template_decl
27722 because that would skew the template parameter depth of a lambda
27723 within if we're already inside a template. */
27724 processing_template_decl
= 1;
27725 cp_expr expr
= cp_parser_constraint_logical_or_expression (parser
, lambda_p
);
27726 if (check_for_bare_parameter_packs (expr
))
27727 expr
= error_mark_node
;
27731 /* Parse a expression after a requires clause.
27733 constraint-expression:
27734 logical-or-expression
27736 The required logical-or-expression must be a constant expression. Note
27737 that we don't check that the expression is constepxr here. We defer until
27738 we analyze constraints and then, we only check atomic constraints. */
27741 cp_parser_constraint_expression (cp_parser
*parser
)
27743 processing_constraint_expression_sentinel parsing_constraint
;
27744 temp_override
<int> ovr (processing_template_decl
);
27745 if (!processing_template_decl
)
27746 /* As in cp_parser_requires_clause_expression. */
27747 processing_template_decl
= 1;
27748 cp_expr expr
= cp_parser_binary_expression (parser
, false, true,
27749 PREC_NOT_OPERATOR
, NULL
);
27750 if (check_for_bare_parameter_packs (expr
))
27751 expr
= error_mark_node
;
27752 expr
.maybe_add_location_wrapper ();
27756 /* Optionally parse a requires clause:
27759 `requires` constraint-logical-or-expression.
27761 `requires constraint-expression.
27763 LAMBDA_P is true when the requires-clause is parsed before the
27764 parameter-list of a lambda-declarator. */
27767 cp_parser_requires_clause_opt (cp_parser
*parser
, bool lambda_p
)
27769 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
27770 if (tok
->keyword
!= RID_REQUIRES
)
27772 if (!flag_concepts
&& tok
->type
== CPP_NAME
27773 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
27775 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27776 "%<requires%> only available with "
27777 "%<-std=c++20%> or %<-fconcepts%>");
27778 /* Parse and discard the requires-clause. */
27779 cp_lexer_consume_token (parser
->lexer
);
27780 cp_parser_constraint_expression (parser
);
27785 cp_token
*tok2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
27786 if (tok2
->type
== CPP_OPEN_BRACE
)
27788 /* An opening brace following the start of a requires-clause is
27789 ill-formed; the user likely forgot the second `requires' that
27790 would start a requires-expression. */
27791 gcc_rich_location
richloc (tok2
->location
);
27792 richloc
.add_fixit_insert_after (tok
->location
, " requires");
27793 error_at (&richloc
, "missing additional %<requires%> to start "
27794 "a requires-expression");
27795 /* Don't consume the `requires', so that it's reused as the start of a
27796 requires-expression. */
27799 cp_lexer_consume_token (parser
->lexer
);
27801 if (!flag_concepts_ts
)
27802 return cp_parser_requires_clause_expression (parser
, lambda_p
);
27804 return cp_parser_constraint_expression (parser
);
27807 /*---------------------------------------------------------------------------
27808 Requires expressions
27809 ---------------------------------------------------------------------------*/
27811 /* Parse a requires expression
27813 requirement-expression:
27814 'requires' requirement-parameter-list [opt] requirement-body */
27817 cp_parser_requires_expression (cp_parser
*parser
)
27819 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
27820 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
27822 /* Avoid committing to outer tentative parse. */
27823 tentative_firewall
firewall (parser
);
27825 /* This is definitely a requires-expression. */
27826 cp_parser_commit_to_tentative_parse (parser
);
27830 /* Local parameters are delared as variables within the scope
27831 of the expression. They are not visible past the end of
27832 the expression. Expressions within the requires-expression
27833 are unevaluated. */
27834 struct scope_sentinel
27838 ++cp_unevaluated_operand
;
27839 begin_scope (sk_block
, NULL_TREE
);
27844 pop_bindings_and_leave_scope ();
27845 --cp_unevaluated_operand
;
27849 /* Parse the optional parameter list. */
27850 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27852 parms
= cp_parser_requirement_parameter_list (parser
);
27853 if (parms
== error_mark_node
)
27854 return error_mark_node
;
27859 /* Parse the requirement body. */
27860 temp_override
<int> ovr (processing_template_decl
);
27861 if (!processing_template_decl
)
27862 /* As in cp_parser_requires_clause_expression. */
27863 processing_template_decl
= 1;
27864 reqs
= cp_parser_requirement_body (parser
);
27865 if (reqs
== error_mark_node
)
27866 return error_mark_node
;
27869 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
27871 grokparms (parms
, &parms
);
27872 loc
= make_location (loc
, loc
, parser
->lexer
);
27873 tree expr
= finish_requires_expr (loc
, parms
, reqs
);
27874 if (!processing_template_decl
)
27876 /* Perform semantic processing now to diagnose any invalid types and
27878 int saved_errorcount
= errorcount
;
27879 tsubst_requires_expr (expr
, NULL_TREE
, tf_warning_or_error
, NULL_TREE
);
27880 if (errorcount
> saved_errorcount
)
27881 return error_mark_node
;
27886 /* Parse a parameterized requirement.
27888 requirement-parameter-list:
27889 '(' parameter-declaration-clause ')' */
27892 cp_parser_requirement_parameter_list (cp_parser
*parser
)
27894 matching_parens parens
;
27895 if (!parens
.require_open (parser
))
27896 return error_mark_node
;
27898 tree parms
= (cp_parser_parameter_declaration_clause
27899 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
27901 if (!parens
.require_close (parser
))
27902 return error_mark_node
;
27907 /* Parse the body of a requirement.
27910 '{' requirement-list '}' */
27912 cp_parser_requirement_body (cp_parser
*parser
)
27914 matching_braces braces
;
27915 if (!braces
.require_open (parser
))
27916 return error_mark_node
;
27918 tree reqs
= cp_parser_requirement_seq (parser
);
27920 if (!braces
.require_close (parser
))
27921 return error_mark_node
;
27926 /* Parse a sequence of requirements.
27930 requirement-seq requirement */
27933 cp_parser_requirement_seq (cp_parser
*parser
)
27935 tree result
= NULL_TREE
;
27938 tree req
= cp_parser_requirement (parser
);
27939 if (req
!= error_mark_node
)
27940 result
= tree_cons (NULL_TREE
, req
, result
);
27941 } while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
));
27943 /* If there are no valid requirements, this is not a valid expression. */
27945 return error_mark_node
;
27947 /* Reverse the order of requirements so they are analyzed in order. */
27948 return nreverse (result
);
27951 /* Parse a syntactic requirement or type requirement.
27955 compound-requirement
27957 nested-requirement */
27960 cp_parser_requirement (cp_parser
*parser
)
27962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27963 return cp_parser_compound_requirement (parser
);
27964 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
27965 return cp_parser_type_requirement (parser
);
27966 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
27967 return cp_parser_nested_requirement (parser
);
27969 return cp_parser_simple_requirement (parser
);
27972 /* Parse a simple requirement.
27974 simple-requirement:
27978 cp_parser_simple_requirement (cp_parser
*parser
)
27980 location_t start
= cp_lexer_peek_token (parser
->lexer
)->location
;
27981 cp_expr expr
= cp_parser_expression (parser
, NULL
, false, false);
27982 if (expr
== error_mark_node
)
27983 cp_parser_skip_to_end_of_statement (parser
);
27985 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27987 if (!expr
|| expr
== error_mark_node
)
27988 return error_mark_node
;
27990 /* Sometimes we don't get locations, so use the cached token location
27991 as a reasonable approximation. */
27992 if (expr
.get_location() == UNKNOWN_LOCATION
)
27993 expr
.set_location (start
);
27995 return finish_simple_requirement (expr
.get_location (), expr
);
27998 /* Parse a type requirement
28001 nested-name-specifier [opt] required-type-name ';'
28003 required-type-name:
28005 'template' [opt] simple-template-id */
28008 cp_parser_type_requirement (cp_parser
*parser
)
28010 cp_token
*start_tok
= cp_lexer_consume_token (parser
->lexer
);
28011 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28013 // Save the scope before parsing name specifiers.
28014 tree saved_scope
= parser
->scope
;
28015 tree saved_object_scope
= parser
->object_scope
;
28016 tree saved_qualifying_scope
= parser
->qualifying_scope
;
28017 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
28018 cp_parser_nested_name_specifier_opt (parser
,
28019 /*typename_keyword_p=*/true,
28020 /*check_dependency_p=*/false,
28022 /*is_declaration=*/false);
28025 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28027 cp_lexer_consume_token (parser
->lexer
);
28028 type
= cp_parser_template_id (parser
,
28029 /*template_keyword_p=*/true,
28030 /*check_dependency=*/false,
28031 /*tag_type=*/none_type
,
28032 /*is_declaration=*/false);
28033 type
= make_typename_type (parser
->scope
, type
, typename_type
,
28034 /*complain=*/tf_error
);
28037 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
28039 if (TREE_CODE (type
) == TYPE_DECL
)
28040 type
= TREE_TYPE (type
);
28042 parser
->scope
= saved_scope
;
28043 parser
->object_scope
= saved_object_scope
;
28044 parser
->qualifying_scope
= saved_qualifying_scope
;
28046 if (type
== error_mark_node
)
28047 cp_parser_skip_to_end_of_statement (parser
);
28049 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28051 if (type
== error_mark_node
)
28052 return error_mark_node
;
28054 loc
= make_location (loc
, start_tok
->location
, parser
->lexer
);
28055 return finish_type_requirement (loc
, type
);
28058 /* Parse a compound requirement
28060 compound-requirement:
28061 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28064 cp_parser_compound_requirement (cp_parser
*parser
)
28066 /* Parse an expression enclosed in '{ }'s. */
28067 matching_braces braces
;
28068 if (!braces
.require_open (parser
))
28069 return error_mark_node
;
28071 cp_token
*expr_token
= cp_lexer_peek_token (parser
->lexer
);
28073 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
28074 if (expr
== error_mark_node
)
28075 cp_parser_skip_to_closing_brace (parser
);
28077 if (!braces
.require_close (parser
))
28079 cp_parser_skip_to_end_of_statement (parser
);
28080 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28081 return error_mark_node
;
28084 /* If the expression was invalid, skip the remainder of the requirement. */
28085 if (!expr
|| expr
== error_mark_node
)
28087 cp_parser_skip_to_end_of_statement (parser
);
28088 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28089 return error_mark_node
;
28092 /* Parse the optional noexcept. */
28093 bool noexcept_p
= false;
28094 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
28096 cp_lexer_consume_token (parser
->lexer
);
28100 /* Parse the optional trailing return type. */
28101 tree type
= NULL_TREE
;
28102 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
28104 cp_lexer_consume_token (parser
->lexer
);
28105 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
28107 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
28108 parser
->in_result_type_constraint_p
= true;
28109 /* C++20 allows either a type-id or a type-constraint. Parsing
28110 a type-id will subsume the parsing for a type-constraint but
28111 allow for more syntactic forms (e.g., const C<T>*). */
28112 type
= cp_parser_trailing_type_id (parser
);
28113 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
28114 if (type
== error_mark_node
)
28115 return error_mark_node
;
28117 location_t type_loc
= make_location (tok
->location
, tok
->location
,
28120 /* Check that we haven't written something like 'const C<T>*'. */
28121 if (type_uses_auto (type
))
28123 if (!is_auto (type
))
28125 error_at (type_loc
,
28126 "result type is not a plain type-constraint");
28127 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28128 return error_mark_node
;
28131 else if (!flag_concepts_ts
)
28132 /* P1452R2 removed the trailing-return-type option. */
28133 error_at (type_loc
,
28134 "return-type-requirement is not a type-constraint");
28137 location_t loc
= make_location (expr_token
->location
,
28138 braces
.open_location (),
28141 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28143 if (expr
== error_mark_node
|| type
== error_mark_node
)
28144 return error_mark_node
;
28146 return finish_compound_requirement (loc
, expr
, type
, noexcept_p
);
28149 /* Parse a nested requirement. This is the same as a requires clause.
28151 nested-requirement:
28155 cp_parser_nested_requirement (cp_parser
*parser
)
28157 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
28158 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
28159 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28160 tree req
= cp_parser_constraint_expression (parser
);
28161 if (req
== error_mark_node
)
28162 cp_parser_skip_to_end_of_statement (parser
);
28163 loc
= make_location (loc
, tok
->location
, parser
->lexer
);
28164 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28165 if (req
== error_mark_node
)
28166 return error_mark_node
;
28167 return finish_nested_requirement (loc
, req
);
28170 /* Support Functions */
28172 /* Return the appropriate prefer_type argument for lookup_name_real based on
28173 tag_type and template_mem_access. */
28176 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
28178 /* DR 141: When looking in the current enclosing context for a template-name
28179 after -> or ., only consider class templates. */
28180 if (template_mem_access
)
28184 case none_type
: return 0; // No preference.
28185 case scope_type
: return 1; // Type or namespace.
28186 default: return 2; // Type only.
28190 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
28191 NAME should have one of the representations used for an
28192 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
28193 is returned. If PARSER->SCOPE is a dependent type, then a
28194 SCOPE_REF is returned.
28196 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
28197 returned; the name was already resolved when the TEMPLATE_ID_EXPR
28198 was formed. Abstractly, such entities should not be passed to this
28199 function, because they do not need to be looked up, but it is
28200 simpler to check for this special case here, rather than at the
28203 In cases not explicitly covered above, this function returns a
28204 DECL, OVERLOAD, or baselink representing the result of the lookup.
28205 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
28208 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
28209 (e.g., "struct") that was used. In that case bindings that do not
28210 refer to types are ignored.
28212 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
28215 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
28218 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
28221 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
28222 TREE_LIST of candidates if name-lookup results in an ambiguity, and
28223 NULL_TREE otherwise. */
28226 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
28227 enum tag_types tag_type
,
28230 bool check_dependency
,
28231 tree
*ambiguous_decls
,
28232 location_t name_location
)
28235 tree object_type
= parser
->context
->object_type
;
28237 /* Assume that the lookup will be unambiguous. */
28238 if (ambiguous_decls
)
28239 *ambiguous_decls
= NULL_TREE
;
28241 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
28242 no longer valid. Note that if we are parsing tentatively, and
28243 the parse fails, OBJECT_TYPE will be automatically restored. */
28244 parser
->context
->object_type
= NULL_TREE
;
28246 if (name
== error_mark_node
)
28247 return error_mark_node
;
28249 /* A template-id has already been resolved; there is no lookup to
28251 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
28253 if (BASELINK_P (name
))
28255 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
28256 == TEMPLATE_ID_EXPR
);
28260 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
28261 it should already have been checked to make sure that the name
28262 used matches the type being destroyed. */
28263 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
28267 /* Figure out to which type this destructor applies. */
28269 type
= parser
->scope
;
28270 else if (object_type
)
28271 type
= object_type
;
28273 type
= current_class_type
;
28274 /* If that's not a class type, there is no destructor. */
28275 if (!type
|| !CLASS_TYPE_P (type
))
28276 return error_mark_node
;
28278 /* In a non-static member function, check implicit this->. */
28279 if (current_class_ref
)
28280 return lookup_destructor (current_class_ref
, parser
->scope
, name
,
28281 tf_warning_or_error
);
28283 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
28284 lazily_declare_fn (sfk_destructor
, type
);
28286 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
28289 return error_mark_node
;
28292 /* By this point, the NAME should be an ordinary identifier. If
28293 the id-expression was a qualified name, the qualifying scope is
28294 stored in PARSER->SCOPE at this point. */
28295 gcc_assert (identifier_p (name
));
28297 /* Perform the lookup. */
28302 if (parser
->scope
== error_mark_node
)
28303 return error_mark_node
;
28305 /* If the SCOPE is dependent, the lookup must be deferred until
28306 the template is instantiated -- unless we are explicitly
28307 looking up names in uninstantiated templates. Even then, we
28308 cannot look up the name if the scope is not a class type; it
28309 might, for example, be a template type parameter. */
28310 dependent_p
= (TYPE_P (parser
->scope
)
28311 && dependent_scope_p (parser
->scope
));
28312 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
28314 /* Defer lookup. */
28315 decl
= error_mark_node
;
28318 tree pushed_scope
= NULL_TREE
;
28320 /* If PARSER->SCOPE is a dependent type, then it must be a
28321 class type, and we must not be checking dependencies;
28322 otherwise, we would have processed this lookup above. So
28323 that PARSER->SCOPE is not considered a dependent base by
28324 lookup_member, we must enter the scope here. */
28326 pushed_scope
= push_scope (parser
->scope
);
28328 /* If the PARSER->SCOPE is a template specialization, it
28329 may be instantiated during name lookup. In that case,
28330 errors may be issued. Even if we rollback the current
28331 tentative parse, those errors are valid. */
28332 decl
= lookup_qualified_name (parser
->scope
, name
,
28333 prefer_type_arg (tag_type
),
28334 /*complain=*/true);
28336 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
28337 lookup result and the nested-name-specifier nominates a class C:
28338 * if the name specified after the nested-name-specifier, when
28339 looked up in C, is the injected-class-name of C (Clause 9), or
28340 * if the name specified after the nested-name-specifier is the
28341 same as the identifier or the simple-template-id's template-
28342 name in the last component of the nested-name-specifier,
28343 the name is instead considered to name the constructor of
28344 class C. [ Note: for example, the constructor is not an
28345 acceptable lookup result in an elaborated-type-specifier so
28346 the constructor would not be used in place of the
28347 injected-class-name. --end note ] Such a constructor name
28348 shall be used only in the declarator-id of a declaration that
28349 names a constructor or in a using-declaration. */
28350 if (tag_type
== none_type
28351 && DECL_SELF_REFERENCE_P (decl
)
28352 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
28353 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
28354 prefer_type_arg (tag_type
),
28355 /*complain=*/true);
28357 /* If we have a single function from a using decl, pull it out. */
28358 if (TREE_CODE (decl
) == OVERLOAD
28359 && !really_overloaded_fn (decl
))
28360 decl
= OVL_FUNCTION (decl
);
28363 pop_scope (pushed_scope
);
28366 /* If the scope is a dependent type and either we deferred lookup or
28367 we did lookup but didn't find the name, rememeber the name. */
28368 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
28369 && dependent_type_p (parser
->scope
))
28375 /* The resolution to Core Issue 180 says that `struct
28376 A::B' should be considered a type-name, even if `A'
28378 type
= make_typename_type (parser
->scope
, name
, tag_type
,
28379 /*complain=*/tf_error
);
28380 if (type
!= error_mark_node
)
28381 decl
= TYPE_NAME (type
);
28383 else if (is_template
28384 && (cp_parser_next_token_ends_template_argument_p (parser
)
28385 || cp_lexer_next_token_is (parser
->lexer
,
28387 decl
= make_unbound_class_template (parser
->scope
,
28389 /*complain=*/tf_error
);
28391 decl
= build_qualified_name (/*type=*/NULL_TREE
,
28392 parser
->scope
, name
,
28395 parser
->qualifying_scope
= parser
->scope
;
28396 parser
->object_scope
= NULL_TREE
;
28398 else if (object_type
)
28400 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28401 OBJECT_TYPE is not a class. */
28402 if (CLASS_TYPE_P (object_type
))
28403 /* If the OBJECT_TYPE is a template specialization, it may
28404 be instantiated during name lookup. In that case, errors
28405 may be issued. Even if we rollback the current tentative
28406 parse, those errors are valid. */
28407 decl
= lookup_member (object_type
,
28410 prefer_type_arg (tag_type
),
28411 tf_warning_or_error
);
28416 /* Look it up in the enclosing context. DR 141: When looking for a
28417 template-name after -> or ., only consider class templates. */
28418 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
28420 /*block_p=*/true, is_namespace
, 0);
28421 parser
->object_scope
= object_type
;
28422 parser
->qualifying_scope
= NULL_TREE
;
28426 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
28428 /*block_p=*/true, is_namespace
, 0);
28429 parser
->qualifying_scope
= NULL_TREE
;
28430 parser
->object_scope
= NULL_TREE
;
28433 /* If the lookup failed, let our caller know. */
28434 if (!decl
|| decl
== error_mark_node
)
28435 return error_mark_node
;
28437 /* Pull out the template from an injected-class-name (or multiple). */
28439 decl
= maybe_get_template_decl_from_type_decl (decl
);
28441 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28442 if (TREE_CODE (decl
) == TREE_LIST
)
28444 if (ambiguous_decls
)
28445 *ambiguous_decls
= decl
;
28446 /* The error message we have to print is too complicated for
28447 cp_parser_error, so we incorporate its actions directly. */
28448 if (!cp_parser_simulate_error (parser
))
28450 error_at (name_location
, "reference to %qD is ambiguous",
28452 print_candidates (decl
);
28454 return error_mark_node
;
28457 gcc_assert (DECL_P (decl
)
28458 || TREE_CODE (decl
) == OVERLOAD
28459 || TREE_CODE (decl
) == SCOPE_REF
28460 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
28461 || BASELINK_P (decl
));
28463 /* If we have resolved the name of a member declaration, check to
28464 see if the declaration is accessible. When the name resolves to
28465 set of overloaded functions, accessibility is checked when
28466 overload resolution is done.
28468 During an explicit instantiation, access is not checked at all,
28469 as per [temp.explicit]. */
28471 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
,
28472 tf_warning_or_error
);
28474 maybe_record_typedef_use (decl
);
28476 return cp_expr (decl
, name_location
);
28479 /* Like cp_parser_lookup_name, but for use in the typical case where
28480 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28481 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28484 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
28486 return cp_parser_lookup_name (parser
, name
,
28488 /*is_template=*/false,
28489 /*is_namespace=*/false,
28490 /*check_dependency=*/true,
28491 /*ambiguous_decls=*/NULL
,
28495 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28496 the current context, return the TYPE_DECL. If TAG_NAME_P is
28497 true, the DECL indicates the class being defined in a class-head,
28498 or declared in an elaborated-type-specifier.
28500 Otherwise, return DECL. */
28503 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
28505 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28506 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28509 template <typename T> struct B;
28512 template <typename T> struct A::B {};
28514 Similarly, in an elaborated-type-specifier:
28516 namespace N { struct X{}; }
28519 template <typename T> friend struct N::X;
28522 However, if the DECL refers to a class type, and we are in
28523 the scope of the class, then the name lookup automatically
28524 finds the TYPE_DECL created by build_self_reference rather
28525 than a TEMPLATE_DECL. For example, in:
28527 template <class T> struct S {
28531 there is no need to handle such case. */
28533 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
28534 return DECL_TEMPLATE_RESULT (decl
);
28539 /* If too many, or too few, template-parameter lists apply to the
28540 declarator, issue an error message. Returns TRUE if all went well,
28541 and FALSE otherwise. */
28544 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
28545 cp_declarator
*declarator
,
28546 location_t declarator_location
)
28548 switch (declarator
->kind
)
28552 unsigned num_templates
= 0;
28553 tree scope
= declarator
->u
.id
.qualifying_scope
;
28554 bool template_id_p
= false;
28557 num_templates
= num_template_headers_for_class (scope
);
28558 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
28559 == TEMPLATE_ID_EXPR
)
28561 /* If the DECLARATOR has the form `X<y>' then it uses one
28562 additional level of template parameters. */
28564 template_id_p
= true;
28567 return cp_parser_check_template_parameters
28568 (parser
, num_templates
, template_id_p
, declarator_location
,
28575 case cdk_reference
:
28577 return (cp_parser_check_declarator_template_parameters
28578 (parser
, declarator
->declarator
, declarator_location
));
28585 gcc_unreachable ();
28590 /* NUM_TEMPLATES were used in the current declaration. If that is
28591 invalid, return FALSE and issue an error messages. Otherwise,
28592 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28593 declarator and we can print more accurate diagnostics. */
28596 cp_parser_check_template_parameters (cp_parser
* parser
,
28597 unsigned num_templates
,
28598 bool template_id_p
,
28599 location_t location
,
28600 cp_declarator
*declarator
)
28602 /* If there are the same number of template classes and parameter
28603 lists, that's OK. */
28604 if (parser
->num_template_parameter_lists
== num_templates
)
28606 /* If there are more, but only one more, and the name ends in an identifier,
28607 then we are declaring a primary template. That's OK too. */
28609 && parser
->num_template_parameter_lists
== num_templates
+ 1)
28612 if (cp_parser_simulate_error (parser
))
28615 /* If there are more template classes than parameter lists, we have
28618 template <class T> void S<T>::R<T>::f (); */
28619 if (parser
->num_template_parameter_lists
< num_templates
)
28621 if (declarator
&& !current_function_decl
)
28622 error_at (location
, "specializing member %<%T::%E%> "
28623 "requires %<template<>%> syntax",
28624 declarator
->u
.id
.qualifying_scope
,
28625 declarator
->u
.id
.unqualified_name
);
28626 else if (declarator
)
28627 error_at (location
, "invalid declaration of %<%T::%E%>",
28628 declarator
->u
.id
.qualifying_scope
,
28629 declarator
->u
.id
.unqualified_name
);
28631 error_at (location
, "too few template-parameter-lists");
28634 /* Otherwise, there are too many template parameter lists. We have
28637 template <class T> template <class U> void S::f(); */
28638 error_at (location
, "too many template-parameter-lists");
28642 /* Parse an optional `::' token indicating that the following name is
28643 from the global namespace. If so, PARSER->SCOPE is set to the
28644 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28645 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28646 Returns the new value of PARSER->SCOPE, if the `::' token is
28647 present, and NULL_TREE otherwise. */
28650 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
28654 /* Peek at the next token. */
28655 token
= cp_lexer_peek_token (parser
->lexer
);
28656 /* If we're looking at a `::' token then we're starting from the
28657 global namespace, not our current location. */
28658 if (token
->type
== CPP_SCOPE
)
28660 /* Consume the `::' token. */
28661 cp_lexer_consume_token (parser
->lexer
);
28662 /* Set the SCOPE so that we know where to start the lookup. */
28663 parser
->scope
= global_namespace
;
28664 parser
->qualifying_scope
= global_namespace
;
28665 parser
->object_scope
= NULL_TREE
;
28667 return parser
->scope
;
28669 else if (!current_scope_valid_p
)
28671 parser
->scope
= NULL_TREE
;
28672 parser
->qualifying_scope
= NULL_TREE
;
28673 parser
->object_scope
= NULL_TREE
;
28679 /* Returns TRUE if the upcoming token sequence is the start of a
28680 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28681 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28682 is used to control type-specifier parsing. */
28685 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
28688 bool constructor_p
;
28689 bool outside_class_specifier_p
;
28690 tree nested_name_specifier
;
28691 cp_token
*next_token
;
28693 /* The common case is that this is not a constructor declarator, so
28694 try to avoid doing lots of work if at all possible. It's not
28695 valid declare a constructor at function scope. */
28696 if (parser
->in_function_body
)
28698 /* And only certain tokens can begin a constructor declarator. */
28699 next_token
= cp_lexer_peek_token (parser
->lexer
);
28700 if (next_token
->type
!= CPP_NAME
28701 && next_token
->type
!= CPP_SCOPE
28702 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
28703 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
28704 declarator-id of a constructor or destructor. */
28705 && (next_token
->type
!= CPP_TEMPLATE_ID
|| cxx_dialect
>= cxx20
))
28708 /* Parse tentatively; we are going to roll back all of the tokens
28710 cp_parser_parse_tentatively (parser
);
28711 /* Assume that we are looking at a constructor declarator. */
28712 constructor_p
= true;
28714 /* Look for the optional `::' operator. */
28715 cp_parser_global_scope_opt (parser
,
28716 /*current_scope_valid_p=*/false);
28717 /* Look for the nested-name-specifier. */
28718 nested_name_specifier
28719 = (cp_parser_nested_name_specifier_opt (parser
,
28720 /*typename_keyword_p=*/false,
28721 /*check_dependency_p=*/false,
28723 /*is_declaration=*/false));
28725 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
28726 if (nested_name_specifier
28727 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
28729 tree s
= resolve_typename_type (nested_name_specifier
,
28730 /*only_current_p=*/false);
28731 if (TREE_CODE (s
) != TYPENAME_TYPE
)
28732 nested_name_specifier
= s
;
28735 outside_class_specifier_p
= (!at_class_scope_p ()
28736 || !TYPE_BEING_DEFINED (current_class_type
)
28739 /* Outside of a class-specifier, there must be a
28740 nested-name-specifier. Except in C++17 mode, where we
28741 might be declaring a guiding declaration. */
28742 if (!nested_name_specifier
&& outside_class_specifier_p
28743 && cxx_dialect
< cxx17
)
28744 constructor_p
= false;
28745 else if (nested_name_specifier
== error_mark_node
)
28746 constructor_p
= false;
28748 /* If we have a class scope, this is easy; DR 147 says that S::S always
28749 names the constructor, and no other qualified name could. */
28750 if (constructor_p
&& nested_name_specifier
28751 && CLASS_TYPE_P (nested_name_specifier
))
28753 tree id
= cp_parser_unqualified_id (parser
,
28754 /*template_keyword_p=*/false,
28755 /*check_dependency_p=*/false,
28756 /*declarator_p=*/true,
28757 /*optional_p=*/false);
28758 if (is_overloaded_fn (id
))
28759 id
= DECL_NAME (get_first_fn (id
));
28760 if (!constructor_name_p (id
, nested_name_specifier
))
28761 constructor_p
= false;
28763 /* If we still think that this might be a constructor-declarator,
28764 look for a class-name. */
28765 else if (constructor_p
)
28769 template <typename T> struct S {
28773 we must recognize that the nested `S' names a class. */
28774 if (cxx_dialect
>= cxx17
)
28775 cp_parser_parse_tentatively (parser
);
28778 type_decl
= cp_parser_class_name (parser
,
28779 /*typename_keyword_p=*/false,
28780 /*template_keyword_p=*/false,
28782 /*check_dependency_p=*/false,
28783 /*class_head_p=*/false,
28784 /*is_declaration=*/false);
28786 if (cxx_dialect
>= cxx17
28787 && !cp_parser_parse_definitely (parser
))
28789 type_decl
= NULL_TREE
;
28790 tree tmpl
= cp_parser_template_name (parser
,
28791 /*template_keyword*/false,
28792 /*check_dependency_p*/false,
28793 /*is_declaration*/false,
28795 /*is_identifier*/NULL
);
28796 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28797 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28798 /* It's a deduction guide, return true. */;
28800 cp_parser_simulate_error (parser
);
28803 /* If there was no class-name, then this is not a constructor.
28804 Otherwise, if we are in a class-specifier and we aren't
28805 handling a friend declaration, check that its type matches
28806 current_class_type (c++/38313). Note: error_mark_node
28807 is left alone for error recovery purposes. */
28808 constructor_p
= (!cp_parser_error_occurred (parser
)
28809 && (outside_class_specifier_p
28810 || type_decl
== NULL_TREE
28811 || type_decl
== error_mark_node
28812 || same_type_p (current_class_type
,
28813 TREE_TYPE (type_decl
))));
28815 /* If we're still considering a constructor, we have to see a `(',
28816 to begin the parameter-declaration-clause, followed by either a
28817 `)', an `...', or a decl-specifier. We need to check for a
28818 type-specifier to avoid being fooled into thinking that:
28822 is a constructor. (It is actually a function named `f' that
28823 takes one parameter (of type `int') and returns a value of type
28826 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28827 constructor_p
= false;
28830 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
28831 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
28832 /* A parameter declaration begins with a decl-specifier,
28833 which is either the "attribute" keyword, a storage class
28834 specifier, or (usually) a type-specifier. */
28835 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
28836 /* GNU attributes can actually appear both at the start of
28837 a parameter and parenthesized declarator.
28838 S (__attribute__((unused)) int);
28839 is a constructor, but
28840 S (__attribute__((unused)) foo) (int);
28841 is a function declaration. */
28842 || (cp_parser_allow_gnu_extensions_p (parser
)
28843 && cp_next_tokens_can_be_gnu_attribute_p (parser
)))
28844 /* A parameter declaration can also begin with [[attribute]]. */
28845 && !cp_next_tokens_can_be_std_attribute_p (parser
))
28848 tree pushed_scope
= NULL_TREE
;
28849 unsigned saved_num_template_parameter_lists
;
28851 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
28853 unsigned int n
= cp_parser_skip_gnu_attributes_opt (parser
, 1);
28855 cp_lexer_consume_token (parser
->lexer
);
28858 /* Names appearing in the type-specifier should be looked up
28859 in the scope of the class. */
28860 if (current_class_type
)
28862 else if (type_decl
)
28864 type
= TREE_TYPE (type_decl
);
28865 if (TREE_CODE (type
) == TYPENAME_TYPE
)
28867 type
= resolve_typename_type (type
,
28868 /*only_current_p=*/false);
28869 if (TREE_CODE (type
) == TYPENAME_TYPE
)
28871 cp_parser_abort_tentative_parse (parser
);
28875 pushed_scope
= push_scope (type
);
28878 /* Inside the constructor parameter list, surrounding
28879 template-parameter-lists do not apply. */
28880 saved_num_template_parameter_lists
28881 = parser
->num_template_parameter_lists
;
28882 parser
->num_template_parameter_lists
= 0;
28884 /* Look for the type-specifier. It's not optional, but its typename
28885 might be. Unless this is a friend declaration; we don't want to
28888 friend S (T::fn)(int);
28890 as a constructor, but with P0634, we might assume a type when
28891 looking for the type-specifier. It is actually a function named
28892 `T::fn' that takes one parameter (of type `int') and returns a
28893 value of type `S'. Constructors can be friends, but they must
28894 use a qualified name.
28896 Parse with an empty set of declaration specifiers since we're
28897 trying to match a decl-specifier-seq of the first parameter.
28898 This must be non-null so that cp_parser_simple_type_specifier
28899 will recognize a constrained placeholder type such as:
28900 'C<int> auto' where C is a type concept. */
28901 cp_decl_specifier_seq ctor_specs
;
28902 clear_decl_specs (&ctor_specs
);
28903 cp_parser_type_specifier (parser
,
28904 (friend_p
? CP_PARSER_FLAGS_NONE
28905 : (flags
& ~CP_PARSER_FLAGS_OPTIONAL
)),
28906 /*decl_specs=*/&ctor_specs
,
28907 /*is_declarator=*/true,
28908 /*declares_class_or_enum=*/NULL
,
28909 /*is_cv_qualifier=*/NULL
);
28911 parser
->num_template_parameter_lists
28912 = saved_num_template_parameter_lists
;
28914 /* Leave the scope of the class. */
28916 pop_scope (pushed_scope
);
28918 constructor_p
= !cp_parser_error_occurred (parser
);
28922 /* We did not really want to consume any tokens. */
28923 cp_parser_abort_tentative_parse (parser
);
28925 return constructor_p
;
28928 /* Parse the definition of the function given by the DECL_SPECIFIERS,
28929 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
28930 they must be performed once we are in the scope of the function.
28932 Returns the function defined. */
28935 cp_parser_function_definition_from_specifiers_and_declarator
28936 (cp_parser
* parser
,
28937 cp_decl_specifier_seq
*decl_specifiers
,
28939 const cp_declarator
*declarator
)
28944 /* Begin the function-definition. */
28945 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
28947 /* The things we're about to see are not directly qualified by any
28948 template headers we've seen thus far. */
28949 reset_specialization ();
28951 /* If there were names looked up in the decl-specifier-seq that we
28952 did not check, check them now. We must wait until we are in the
28953 scope of the function to perform the checks, since the function
28954 might be a friend. */
28955 perform_deferred_access_checks (tf_warning_or_error
);
28959 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
28960 parser
->omp_declare_simd
= NULL
;
28961 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
28962 parser
->oacc_routine
= NULL
;
28967 /* Skip the entire function. */
28968 cp_parser_skip_to_end_of_block_or_statement (parser
);
28969 fn
= error_mark_node
;
28971 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
28973 /* Seen already, skip it. An error message has already been output. */
28974 cp_parser_skip_to_end_of_block_or_statement (parser
);
28975 fn
= current_function_decl
;
28976 current_function_decl
= NULL_TREE
;
28977 /* If this is a function from a class, pop the nested class. */
28978 if (current_class_name
)
28979 pop_nested_class ();
28984 if (DECL_DECLARED_INLINE_P (current_function_decl
))
28985 tv
= TV_PARSE_INLINE
;
28987 tv
= TV_PARSE_FUNC
;
28989 fn
= cp_parser_function_definition_after_declarator (parser
,
28990 /*inline_p=*/false);
28997 /* Parse the part of a function-definition that follows the
28998 declarator. INLINE_P is TRUE iff this function is an inline
28999 function defined within a class-specifier.
29001 Returns the function defined. */
29004 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
29008 bool saved_in_unbraced_linkage_specification_p
;
29009 bool saved_in_function_body
;
29010 unsigned saved_num_template_parameter_lists
;
29012 bool fully_implicit_function_template_p
29013 = parser
->fully_implicit_function_template_p
;
29014 parser
->fully_implicit_function_template_p
= false;
29015 tree implicit_template_parms
29016 = parser
->implicit_template_parms
;
29017 parser
->implicit_template_parms
= 0;
29018 cp_binding_level
* implicit_template_scope
29019 = parser
->implicit_template_scope
;
29020 parser
->implicit_template_scope
= 0;
29022 saved_in_function_body
= parser
->in_function_body
;
29023 parser
->in_function_body
= true;
29024 /* If the next token is `return', then the code may be trying to
29025 make use of the "named return value" extension that G++ used to
29027 token
= cp_lexer_peek_token (parser
->lexer
);
29028 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
29030 /* Consume the `return' keyword. */
29031 cp_lexer_consume_token (parser
->lexer
);
29032 /* Look for the identifier that indicates what value is to be
29034 cp_parser_identifier (parser
);
29035 /* Issue an error message. */
29036 error_at (token
->location
,
29037 "named return values are no longer supported");
29038 /* Skip tokens until we reach the start of the function body. */
29041 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29042 if (token
->type
== CPP_OPEN_BRACE
29043 || token
->type
== CPP_EOF
29044 || token
->type
== CPP_PRAGMA_EOL
)
29046 cp_lexer_consume_token (parser
->lexer
);
29049 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29050 anything declared inside `f'. */
29051 saved_in_unbraced_linkage_specification_p
29052 = parser
->in_unbraced_linkage_specification_p
;
29053 parser
->in_unbraced_linkage_specification_p
= false;
29054 /* Inside the function, surrounding template-parameter-lists do not
29056 saved_num_template_parameter_lists
29057 = parser
->num_template_parameter_lists
;
29058 parser
->num_template_parameter_lists
= 0;
29060 /* If the next token is `try', `__transaction_atomic', or
29061 `__transaction_relaxed`, then we are looking at either function-try-block
29062 or function-transaction-block. Note that all of these include the
29064 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
29065 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
29066 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
29067 RID_TRANSACTION_RELAXED
))
29068 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
29069 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
29070 cp_parser_function_try_block (parser
);
29072 cp_parser_ctor_initializer_opt_and_function_body
29073 (parser
, /*in_function_try_block=*/false);
29075 /* Finish the function. */
29076 fn
= finish_function (inline_p
);
29077 /* Generate code for it, if necessary. */
29078 expand_or_defer_fn (fn
);
29079 /* Restore the saved values. */
29080 parser
->in_unbraced_linkage_specification_p
29081 = saved_in_unbraced_linkage_specification_p
;
29082 parser
->num_template_parameter_lists
29083 = saved_num_template_parameter_lists
;
29084 parser
->in_function_body
= saved_in_function_body
;
29086 parser
->fully_implicit_function_template_p
29087 = fully_implicit_function_template_p
;
29088 parser
->implicit_template_parms
29089 = implicit_template_parms
;
29090 parser
->implicit_template_scope
29091 = implicit_template_scope
;
29093 if (parser
->fully_implicit_function_template_p
)
29094 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
29099 /* Parse a template-declaration body (following argument list). */
29102 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
29103 tree parameter_list
,
29106 tree decl
= NULL_TREE
;
29107 bool friend_p
= false;
29109 /* We just processed one more parameter list. */
29110 ++parser
->num_template_parameter_lists
;
29112 /* Get the deferred access checks from the parameter list. These
29113 will be checked once we know what is being declared, as for a
29114 member template the checks must be performed in the scope of the
29115 class containing the member. */
29116 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
29118 /* Tentatively parse for a new template parameter list, which can either be
29119 the template keyword or a template introduction. */
29120 if (cp_parser_template_declaration_after_export (parser
, member_p
))
29122 else if (cxx_dialect
>= cxx11
29123 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
29124 decl
= cp_parser_alias_declaration (parser
);
29125 else if (cxx_dialect
>= cxx20
/* Implies flag_concept. */
29126 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
)
29127 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_BOOL
))
29128 /* Allow 'concept bool' to be handled as per the TS. */
29129 decl
= cp_parser_concept_definition (parser
);
29132 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29133 decl
= cp_parser_single_declaration (parser
,
29136 /*explicit_specialization_p=*/false,
29139 /* If this is a member template declaration, let the front
29141 if (member_p
&& !friend_p
&& decl
)
29143 if (TREE_CODE (decl
) == TYPE_DECL
)
29144 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
29146 decl
= finish_member_template_decl (decl
);
29148 else if (friend_p
&& decl
29149 && DECL_DECLARES_TYPE_P (decl
))
29150 make_friend_class (current_class_type
, TREE_TYPE (decl
),
29151 /*complain=*/true);
29153 /* We are done with the current parameter list. */
29154 --parser
->num_template_parameter_lists
;
29156 pop_deferring_access_checks ();
29159 finish_template_decl (parameter_list
);
29161 /* Check the template arguments for a literal operator template. */
29163 && DECL_DECLARES_FUNCTION_P (decl
)
29164 && UDLIT_OPER_P (DECL_NAME (decl
)))
29167 if (parameter_list
== NULL_TREE
)
29171 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
29172 if (num_parms
== 1)
29174 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
29175 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
29176 if (TREE_CODE (parm
) != PARM_DECL
)
29178 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
))
29179 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
29180 /* OK, C++20 string literal operator template. We don't need
29181 to warn in lower dialects here because we will have already
29182 warned about the template parameter. */;
29183 else if (TREE_TYPE (parm
) != char_type_node
29184 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
29187 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
29189 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
29190 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
29191 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
29192 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
29193 if (TREE_CODE (parm
) != PARM_DECL
29194 || TREE_TYPE (parm
) != TREE_TYPE (type
)
29195 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
29198 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
29199 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
29200 "ISO C++ did not adopt string literal operator templa"
29201 "tes taking an argument pack of characters");
29208 if (cxx_dialect
> cxx17
)
29209 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
29210 "template %qD has invalid parameter list; expected "
29211 "non-type template parameter pack %<<char...>%> or "
29212 "single non-type parameter of class type",
29215 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
29216 "template %qD has invalid parameter list; expected "
29217 "non-type template parameter pack %<<char...>%>",
29222 /* Register member declarations. */
29223 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
29224 finish_member_declaration (decl
);
29225 /* If DECL is a function template, we must return to parse it later.
29226 (Even though there is no definition, there might be default
29227 arguments that need handling.) */
29228 if (member_p
&& decl
29229 && DECL_DECLARES_FUNCTION_P (decl
))
29230 vec_safe_push (unparsed_funs_with_definitions
, decl
);
29233 /* Parse a template introduction header for a template-declaration. Returns
29234 false if tentative parse fails. */
29237 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
29239 cp_parser_parse_tentatively (parser
);
29241 tree saved_scope
= parser
->scope
;
29242 tree saved_object_scope
= parser
->object_scope
;
29243 tree saved_qualifying_scope
= parser
->qualifying_scope
;
29245 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
29247 /* Look for the optional `::' operator. */
29248 cp_parser_global_scope_opt (parser
,
29249 /*current_scope_valid_p=*/false);
29250 /* Look for the nested-name-specifier. */
29251 cp_parser_nested_name_specifier_opt (parser
,
29252 /*typename_keyword_p=*/false,
29253 /*check_dependency_p=*/true,
29255 /*is_declaration=*/false);
29257 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29258 tree concept_name
= cp_parser_identifier (parser
);
29260 /* Look up the concept for which we will be matching
29261 template parameters. */
29262 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
29264 parser
->scope
= saved_scope
;
29265 parser
->object_scope
= saved_object_scope
;
29266 parser
->qualifying_scope
= saved_qualifying_scope
;
29268 if (concept_name
== error_mark_node
29269 || (seen_error () && !concept_definition_p (tmpl_decl
)))
29270 cp_parser_simulate_error (parser
);
29272 /* Look for opening brace for introduction. */
29273 matching_braces braces
;
29274 braces
.require_open (parser
);
29275 location_t open_loc
= input_location
;
29277 if (!cp_parser_parse_definitely (parser
))
29280 push_deferring_access_checks (dk_deferred
);
29282 /* Build vector of placeholder parameters and grab
29283 matching identifiers. */
29284 tree introduction_list
= cp_parser_introduction_list (parser
);
29286 /* Look for closing brace for introduction. */
29287 if (!braces
.require_close (parser
))
29290 /* The introduction-list shall not be empty. */
29291 int nargs
= TREE_VEC_LENGTH (introduction_list
);
29294 /* In cp_parser_introduction_list we have already issued an error. */
29298 if (tmpl_decl
== error_mark_node
)
29300 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
29305 /* Build and associate the constraint. */
29306 location_t introduction_loc
= make_location (open_loc
,
29307 start_token
->location
,
29309 tree parms
= finish_template_introduction (tmpl_decl
,
29312 if (parms
&& parms
!= error_mark_node
)
29314 if (!flag_concepts_ts
)
29315 pedwarn (introduction_loc
, 0, "template-introductions"
29316 " are not part of C++20 concepts [-fconcepts-ts]");
29318 cp_parser_template_declaration_after_parameters (parser
, parms
,
29323 if (parms
== NULL_TREE
)
29324 error_at (token
->location
, "no matching concept for template-introduction");
29329 /* Parse a normal template-declaration following the template keyword. */
29332 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
29334 tree parameter_list
;
29335 bool need_lang_pop
;
29336 location_t location
= input_location
;
29338 /* Look for the `<' token. */
29339 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
29341 if (at_class_scope_p () && current_function_decl
)
29343 /* 14.5.2.2 [temp.mem]
29345 A local class shall not have member templates. */
29346 error_at (location
,
29347 "invalid declaration of member template in local class");
29348 cp_parser_skip_to_end_of_block_or_statement (parser
);
29353 A template ... shall not have C linkage. */
29354 if (current_lang_name
== lang_name_c
)
29356 error_at (location
, "template with C linkage");
29357 maybe_show_extern_c_location ();
29358 /* Give it C++ linkage to avoid confusing other parts of the
29360 push_lang_context (lang_name_cplusplus
);
29361 need_lang_pop
= true;
29364 need_lang_pop
= false;
29366 /* We cannot perform access checks on the template parameter
29367 declarations until we know what is being declared, just as we
29368 cannot check the decl-specifier list. */
29369 push_deferring_access_checks (dk_deferred
);
29371 /* If the next token is `>', then we have an invalid
29372 specialization. Rather than complain about an invalid template
29373 parameter, issue an error message here. */
29374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
29376 cp_parser_error (parser
, "invalid explicit specialization");
29377 begin_specialization ();
29378 parameter_list
= NULL_TREE
;
29382 /* Parse the template parameters. */
29383 parameter_list
= cp_parser_template_parameter_list (parser
);
29386 /* Look for the `>'. */
29387 cp_parser_skip_to_end_of_template_parameter_list (parser
);
29389 /* Manage template requirements */
29392 tree reqs
= get_shorthand_constraints (current_template_parms
);
29393 if (tree treqs
= cp_parser_requires_clause_opt (parser
, false))
29394 reqs
= combine_constraint_expressions (reqs
, treqs
);
29395 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
29398 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
29401 /* For the erroneous case of a template with C linkage, we pushed an
29402 implicit C++ linkage scope; exit that scope now. */
29404 pop_lang_context ();
29407 /* Parse a template-declaration, assuming that the `export' (and
29408 `extern') keywords, if present, has already been scanned. MEMBER_P
29409 is as for cp_parser_template_declaration. */
29412 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
29414 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29416 cp_lexer_consume_token (parser
->lexer
);
29417 cp_parser_explicit_template_declaration (parser
, member_p
);
29420 else if (flag_concepts
)
29421 return cp_parser_template_introduction (parser
, member_p
);
29426 /* Perform the deferred access checks from a template-parameter-list.
29427 CHECKS is a TREE_LIST of access checks, as returned by
29428 get_deferred_access_checks. */
29431 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
29433 ++processing_template_parmlist
;
29434 perform_access_checks (checks
, tf_warning_or_error
);
29435 --processing_template_parmlist
;
29438 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29439 `function-definition' sequence that follows a template header.
29440 If MEMBER_P is true, this declaration appears in a class scope.
29442 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29443 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29446 cp_parser_single_declaration (cp_parser
* parser
,
29447 vec
<deferred_access_check
, va_gc
> *checks
,
29449 bool explicit_specialization_p
,
29452 int declares_class_or_enum
;
29453 tree decl
= NULL_TREE
;
29454 cp_decl_specifier_seq decl_specifiers
;
29455 bool function_definition_p
= false;
29456 cp_token
*decl_spec_token_start
;
29458 /* This function is only used when processing a template
29460 gcc_assert (innermost_scope_kind () == sk_template_parms
29461 || innermost_scope_kind () == sk_template_spec
);
29463 /* Defer access checks until we know what is being declared. */
29464 push_deferring_access_checks (dk_deferred
);
29466 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29468 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
29469 cp_parser_decl_specifier_seq (parser
,
29470 (CP_PARSER_FLAGS_OPTIONAL
29471 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
29473 &declares_class_or_enum
);
29475 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
29477 /* There are no template typedefs. */
29478 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
29480 error_at (decl_spec_token_start
->location
,
29481 "template declaration of %<typedef%>");
29482 decl
= error_mark_node
;
29485 /* Gather up the access checks that occurred the
29486 decl-specifier-seq. */
29487 stop_deferring_access_checks ();
29489 /* Check for the declaration of a template class. */
29490 if (declares_class_or_enum
)
29492 if (cp_parser_declares_only_class_p (parser
)
29493 || (declares_class_or_enum
& 2))
29495 /* If this is a declaration, but not a definition, associate
29496 any constraints with the type declaration. Constraints
29497 are associated with definitions in cp_parser_class_specifier. */
29498 if (declares_class_or_enum
== 1)
29499 associate_classtype_constraints (decl_specifiers
.type
);
29501 decl
= shadow_tag (&decl_specifiers
);
29506 friend template <typename T> struct A<T>::B;
29509 A<T>::B will be represented by a TYPENAME_TYPE, and
29510 therefore not recognized by shadow_tag. */
29511 if (friend_p
&& *friend_p
29513 && decl_specifiers
.type
29514 && TYPE_P (decl_specifiers
.type
))
29515 decl
= decl_specifiers
.type
;
29517 if (decl
&& decl
!= error_mark_node
)
29518 decl
= TYPE_NAME (decl
);
29520 decl
= error_mark_node
;
29522 /* Perform access checks for template parameters. */
29523 cp_parser_perform_template_parameter_access_checks (checks
);
29525 /* Give a helpful diagnostic for
29526 template <class T> struct A { } a;
29527 if we aren't already recovering from an error. */
29528 if (!cp_parser_declares_only_class_p (parser
)
29531 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29532 "a class template declaration must not declare "
29534 cp_parser_skip_to_end_of_block_or_statement (parser
);
29540 /* Complain about missing 'typename' or other invalid type names. */
29541 if (!decl_specifiers
.any_type_specifiers_p
29542 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
29544 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29545 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29546 the rest of this declaration. */
29547 decl
= error_mark_node
;
29551 /* If it's not a template class, try for a template function. If
29552 the next token is a `;', then this declaration does not declare
29553 anything. But, if there were errors in the decl-specifiers, then
29554 the error might well have come from an attempted class-specifier.
29555 In that case, there's no need to warn about a missing declarator. */
29557 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
29558 || decl_specifiers
.type
!= error_mark_node
))
29560 decl
= cp_parser_init_declarator (parser
,
29561 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
29564 /*function_definition_allowed_p=*/true,
29566 declares_class_or_enum
,
29567 &function_definition_p
,
29570 /* 7.1.1-1 [dcl.stc]
29572 A storage-class-specifier shall not be specified in an explicit
29573 specialization... */
29575 && explicit_specialization_p
29576 && decl_specifiers
.storage_class
!= sc_none
)
29578 error_at (decl_spec_token_start
->location
,
29579 "explicit template specialization cannot have a storage class");
29580 decl
= error_mark_node
;
29583 if (decl
&& VAR_P (decl
))
29584 check_template_variable (decl
);
29587 /* Look for a trailing `;' after the declaration. */
29588 if (!function_definition_p
29589 && (decl
== error_mark_node
29590 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
29591 cp_parser_skip_to_end_of_block_or_statement (parser
);
29594 pop_deferring_access_checks ();
29596 /* Clear any current qualification; whatever comes next is the start
29597 of something new. */
29598 parser
->scope
= NULL_TREE
;
29599 parser
->qualifying_scope
= NULL_TREE
;
29600 parser
->object_scope
= NULL_TREE
;
29605 /* Parse a cast-expression that is not the operand of a unary "&". */
29608 cp_parser_simple_cast_expression (cp_parser
*parser
)
29610 return cp_parser_cast_expression (parser
, /*address_p=*/false,
29611 /*cast_p=*/false, /*decltype*/false, NULL
);
29614 /* Parse a functional cast to TYPE. Returns an expression
29615 representing the cast. */
29618 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
29620 vec
<tree
, va_gc
> *vec
;
29621 tree expression_list
;
29625 location_t start_loc
= input_location
;
29628 type
= error_mark_node
;
29630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29632 cp_lexer_set_source_position (parser
->lexer
);
29633 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
29634 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
29635 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
29636 if (TREE_CODE (type
) == TYPE_DECL
)
29637 type
= TREE_TYPE (type
);
29639 cast
= finish_compound_literal (type
, expression_list
,
29640 tf_warning_or_error
, fcl_functional
);
29641 /* Create a location of the form:
29644 with caret == start at the start of the type name,
29645 finishing at the closing brace. */
29646 location_t combined_loc
= make_location (start_loc
, start_loc
,
29648 cast
.set_location (combined_loc
);
29653 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
29655 /*allow_expansion_p=*/true,
29656 /*non_constant_p=*/NULL
);
29658 expression_list
= error_mark_node
;
29661 expression_list
= build_tree_list_vec (vec
);
29662 release_tree_vector (vec
);
29665 /* Create a location of the form:
29668 with caret == start at the start of the type name,
29669 finishing at the closing paren. */
29670 location_t combined_loc
= make_location (start_loc
, start_loc
,
29672 cast
= build_functional_cast (combined_loc
, type
, expression_list
,
29673 tf_warning_or_error
);
29675 /* [expr.const]/1: In an integral constant expression "only type
29676 conversions to integral or enumeration type can be used". */
29677 if (TREE_CODE (type
) == TYPE_DECL
)
29678 type
= TREE_TYPE (type
);
29679 if (cast
!= error_mark_node
29680 && !cast_valid_in_integral_constant_expression_p (type
)
29681 && cp_parser_non_integral_constant_expression (parser
,
29683 return error_mark_node
;
29688 /* Save the tokens that make up the body of a member function defined
29689 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29690 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29691 specifiers applied to the declaration. Returns the FUNCTION_DECL
29692 for the member function. */
29695 cp_parser_save_member_function_body (cp_parser
* parser
,
29696 cp_decl_specifier_seq
*decl_specifiers
,
29697 cp_declarator
*declarator
,
29703 bool function_try_block
= false;
29705 /* Create the FUNCTION_DECL. */
29706 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
29707 cp_finalize_omp_declare_simd (parser
, fn
);
29708 cp_finalize_oacc_routine (parser
, fn
, true);
29709 /* If something went badly wrong, bail out now. */
29710 if (fn
== error_mark_node
)
29712 /* If there's a function-body, skip it. */
29713 if (cp_parser_token_starts_function_definition_p
29714 (cp_lexer_peek_token (parser
->lexer
)))
29715 cp_parser_skip_to_end_of_block_or_statement (parser
);
29716 return error_mark_node
;
29719 /* Remember it, if there are default args to post process. */
29720 cp_parser_save_default_args (parser
, fn
);
29722 /* Save away the tokens that make up the body of the
29724 first
= parser
->lexer
->next_token
;
29726 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
29727 cp_lexer_consume_token (parser
->lexer
);
29728 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
29729 RID_TRANSACTION_ATOMIC
))
29731 cp_lexer_consume_token (parser
->lexer
);
29732 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
29733 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
29734 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
29735 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
29736 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
29737 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
29738 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
29740 cp_lexer_consume_token (parser
->lexer
);
29741 cp_lexer_consume_token (parser
->lexer
);
29742 cp_lexer_consume_token (parser
->lexer
);
29743 cp_lexer_consume_token (parser
->lexer
);
29744 cp_lexer_consume_token (parser
->lexer
);
29747 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
29748 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
29750 cp_lexer_consume_token (parser
->lexer
);
29751 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
29756 /* Handle function try blocks. */
29757 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
29759 cp_lexer_consume_token (parser
->lexer
);
29760 function_try_block
= true;
29762 /* We can have braced-init-list mem-initializers before the fn body. */
29763 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
29765 cp_lexer_consume_token (parser
->lexer
);
29766 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
29768 /* cache_group will stop after an un-nested { } pair, too. */
29769 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
29772 /* variadic mem-inits have ... after the ')'. */
29773 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29774 cp_lexer_consume_token (parser
->lexer
);
29777 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29778 /* Handle function try blocks. */
29779 if (function_try_block
)
29780 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
29781 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29782 last
= parser
->lexer
->next_token
;
29784 /* Save away the inline definition; we will process it when the
29785 class is complete. */
29786 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
29787 DECL_PENDING_INLINE_P (fn
) = 1;
29789 /* We need to know that this was defined in the class, so that
29790 friend templates are handled correctly. */
29791 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
29793 /* Add FN to the queue of functions to be parsed later. */
29794 vec_safe_push (unparsed_funs_with_definitions
, fn
);
29799 /* Save the tokens that make up the in-class initializer for a non-static
29800 data member. Returns a DEFERRED_PARSE. */
29803 cp_parser_save_nsdmi (cp_parser
* parser
)
29805 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
29808 /* Parse a template-argument-list, as well as the trailing ">" (but
29809 not the opening "<"). See cp_parser_template_argument_list for the
29813 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
29817 tree saved_qualifying_scope
;
29818 tree saved_object_scope
;
29819 bool saved_greater_than_is_operator_p
;
29823 When parsing a template-id, the first non-nested `>' is taken as
29824 the end of the template-argument-list rather than a greater-than
29826 saved_greater_than_is_operator_p
29827 = parser
->greater_than_is_operator_p
;
29828 parser
->greater_than_is_operator_p
= false;
29829 /* Parsing the argument list may modify SCOPE, so we save it
29831 saved_scope
= parser
->scope
;
29832 saved_qualifying_scope
= parser
->qualifying_scope
;
29833 saved_object_scope
= parser
->object_scope
;
29834 /* We need to evaluate the template arguments, even though this
29835 template-id may be nested within a "sizeof". */
29837 /* Parse the template-argument-list itself. */
29838 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
29839 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
29840 arguments
= NULL_TREE
;
29842 arguments
= cp_parser_template_argument_list (parser
);
29843 /* Look for the `>' that ends the template-argument-list. If we find
29844 a '>>' instead, it's probably just a typo. */
29845 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
29847 if (cxx_dialect
!= cxx98
)
29849 /* In C++0x, a `>>' in a template argument list or cast
29850 expression is considered to be two separate `>'
29851 tokens. So, change the current token to a `>', but don't
29852 consume it: it will be consumed later when the outer
29853 template argument list (or cast expression) is parsed.
29854 Note that this replacement of `>' for `>>' is necessary
29855 even if we are parsing tentatively: in the tentative
29856 case, after calling
29857 cp_parser_enclosed_template_argument_list we will always
29858 throw away all of the template arguments and the first
29859 closing `>', either because the template argument list
29860 was erroneous or because we are replacing those tokens
29861 with a CPP_TEMPLATE_ID token. The second `>' (which will
29862 not have been thrown away) is needed either to close an
29863 outer template argument list or to complete a new-style
29865 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29866 token
->type
= CPP_GREATER
;
29868 else if (!saved_greater_than_is_operator_p
)
29870 /* If we're in a nested template argument list, the '>>' has
29871 to be a typo for '> >'. We emit the error message, but we
29872 continue parsing and we push a '>' as next token, so that
29873 the argument list will be parsed correctly. Note that the
29874 global source location is still on the token before the
29875 '>>', so we need to say explicitly where we want it. */
29876 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29877 gcc_rich_location
richloc (token
->location
);
29878 richloc
.add_fixit_replace ("> >");
29879 error_at (&richloc
, "%<>>%> should be %<> >%> "
29880 "within a nested template argument list");
29882 token
->type
= CPP_GREATER
;
29886 /* If this is not a nested template argument list, the '>>'
29887 is a typo for '>'. Emit an error message and continue.
29888 Same deal about the token location, but here we can get it
29889 right by consuming the '>>' before issuing the diagnostic. */
29890 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29891 error_at (token
->location
,
29892 "spurious %<>>%>, use %<>%> to terminate "
29893 "a template argument list");
29897 cp_parser_skip_to_end_of_template_parameter_list (parser
);
29898 /* The `>' token might be a greater-than operator again now. */
29899 parser
->greater_than_is_operator_p
29900 = saved_greater_than_is_operator_p
;
29901 /* Restore the SAVED_SCOPE. */
29902 parser
->scope
= saved_scope
;
29903 parser
->qualifying_scope
= saved_qualifying_scope
;
29904 parser
->object_scope
= saved_object_scope
;
29909 /* MEMBER_FUNCTION is a member function, or a friend. If default
29910 arguments, or the body of the function have not yet been parsed,
29914 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
29916 timevar_push (TV_PARSE_INMETH
);
29917 /* If this member is a template, get the underlying
29919 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
29920 member_function
= DECL_TEMPLATE_RESULT (member_function
);
29922 /* There should not be any class definitions in progress at this
29923 point; the bodies of members are only parsed outside of all class
29925 gcc_assert (parser
->num_classes_being_defined
== 0);
29926 /* While we're parsing the member functions we might encounter more
29927 classes. We want to handle them right away, but we don't want
29928 them getting mixed up with functions that are currently in the
29930 push_unparsed_function_queues (parser
);
29932 /* Make sure that any template parameters are in scope. */
29933 maybe_begin_member_template_processing (member_function
);
29935 /* If the body of the function has not yet been parsed, parse it
29937 if (DECL_PENDING_INLINE_P (member_function
))
29939 tree function_scope
;
29940 cp_token_cache
*tokens
;
29942 /* The function is no longer pending; we are processing it. */
29943 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
29944 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
29945 DECL_PENDING_INLINE_P (member_function
) = 0;
29947 /* If this is a local class, enter the scope of the containing
29949 function_scope
= current_function_decl
;
29950 if (function_scope
)
29951 push_function_context ();
29953 /* Push the body of the function onto the lexer stack. */
29954 cp_parser_push_lexer_for_tokens (parser
, tokens
);
29956 /* Let the front end know that we going to be defining this
29958 start_preparsed_function (member_function
, NULL_TREE
,
29959 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
29961 /* Don't do access checking if it is a templated function. */
29962 if (processing_template_decl
)
29963 push_deferring_access_checks (dk_no_check
);
29965 /* #pragma omp declare reduction needs special parsing. */
29966 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
29968 parser
->lexer
->in_pragma
= true;
29969 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
29970 finish_function (/*inline_p=*/true);
29971 cp_check_omp_declare_reduction (member_function
);
29974 /* Now, parse the body of the function. */
29975 cp_parser_function_definition_after_declarator (parser
,
29976 /*inline_p=*/true);
29978 if (processing_template_decl
)
29979 pop_deferring_access_checks ();
29981 /* Leave the scope of the containing function. */
29982 if (function_scope
)
29983 pop_function_context ();
29984 cp_parser_pop_lexer (parser
);
29987 /* Remove any template parameters from the symbol table. */
29988 maybe_end_member_template_processing ();
29990 /* Restore the queue. */
29991 pop_unparsed_function_queues (parser
);
29992 timevar_pop (TV_PARSE_INMETH
);
29995 /* If DECL contains any default args, remember it on the unparsed
29996 functions queue. */
29999 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
30003 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
30005 probe
= TREE_CHAIN (probe
))
30006 if (TREE_PURPOSE (probe
))
30008 cp_default_arg_entry entry
= {current_class_type
, decl
};
30009 vec_safe_push (unparsed_funs_with_default_args
, entry
);
30013 /* Remember if there is a noexcept-specifier to post process. */
30014 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
30015 if (UNPARSED_NOEXCEPT_SPEC_P (spec
))
30016 vec_safe_push (unparsed_noexcepts
, decl
);
30019 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30020 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30021 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30022 from the parameter-type-list. */
30025 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
30026 tree default_arg
, tree parmtype
)
30028 cp_token_cache
*tokens
;
30032 if (default_arg
== error_mark_node
)
30033 return error_mark_node
;
30035 /* Push the saved tokens for the default argument onto the parser's
30037 tokens
= DEFPARSE_TOKENS (default_arg
);
30038 cp_parser_push_lexer_for_tokens (parser
, tokens
);
30040 start_lambda_scope (decl
);
30042 /* Parse the default argument. */
30043 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
30044 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
30045 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
30047 finish_lambda_scope ();
30049 if (parsed_arg
== error_mark_node
)
30050 cp_parser_skip_to_end_of_statement (parser
);
30052 if (!processing_template_decl
)
30054 /* In a non-template class, check conversions now. In a template,
30055 we'll wait and instantiate these as needed. */
30056 if (TREE_CODE (decl
) == PARM_DECL
)
30057 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
30058 tf_warning_or_error
);
30059 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
30060 parsed_arg
= error_mark_node
;
30062 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
30065 /* If the token stream has not been completely used up, then
30066 there was extra junk after the end of the default
30068 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30070 if (TREE_CODE (decl
) == PARM_DECL
)
30071 cp_parser_error (parser
, "expected %<,%>");
30073 cp_parser_error (parser
, "expected %<;%>");
30076 /* Revert to the main lexer. */
30077 cp_parser_pop_lexer (parser
);
30082 /* FIELD is a non-static data member with an initializer which we saved for
30083 later; parse it now. */
30086 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
30090 maybe_begin_member_template_processing (field
);
30092 push_unparsed_function_queues (parser
);
30093 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
30094 DECL_INITIAL (field
),
30096 pop_unparsed_function_queues (parser
);
30098 maybe_end_member_template_processing ();
30100 DECL_INITIAL (field
) = def
;
30103 /* FN is a FUNCTION_DECL which may contains a parameter with an
30104 unparsed DEFERRED_PARSE. Parse the default args now. This function
30105 assumes that the current scope is the scope in which the default
30106 argument should be processed. */
30109 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
30111 unsigned char saved_local_variables_forbidden_p
;
30112 tree parm
, parmdecl
;
30114 /* While we're parsing the default args, we might (due to the
30115 statement expression extension) encounter more classes. We want
30116 to handle them right away, but we don't want them getting mixed
30117 up with default args that are currently in the queue. */
30118 push_unparsed_function_queues (parser
);
30120 /* Local variable names (and the `this' keyword) may not appear
30121 in a default argument. */
30122 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
30123 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
30125 push_defarg_context (fn
);
30127 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
30128 parmdecl
= DECL_ARGUMENTS (fn
);
30129 parm
&& parm
!= void_list_node
;
30130 parm
= TREE_CHAIN (parm
),
30131 parmdecl
= DECL_CHAIN (parmdecl
))
30133 tree default_arg
= TREE_PURPOSE (parm
);
30135 vec
<tree
, va_gc
> *insts
;
30142 if (TREE_CODE (default_arg
) != DEFERRED_PARSE
)
30143 /* This can happen for a friend declaration for a function
30144 already declared with default arguments. */
30148 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
30150 TREE_VALUE (parm
));
30151 TREE_PURPOSE (parm
) = parsed_arg
;
30153 /* Update any instantiations we've already created. */
30154 for (insts
= DEFPARSE_INSTANTIATIONS (default_arg
), ix
= 0;
30155 vec_safe_iterate (insts
, ix
, ©
); ix
++)
30156 TREE_PURPOSE (copy
) = parsed_arg
;
30159 pop_defarg_context ();
30161 /* Make sure no default arg is missing. */
30162 check_default_args (fn
);
30164 /* Restore the state of local_variables_forbidden_p. */
30165 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
30167 /* Restore the queue. */
30168 pop_unparsed_function_queues (parser
);
30171 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
30173 sizeof ... ( identifier )
30175 where the 'sizeof' token has already been consumed. */
30178 cp_parser_sizeof_pack (cp_parser
*parser
)
30180 /* Consume the `...'. */
30181 cp_lexer_consume_token (parser
->lexer
);
30182 maybe_warn_variadic_templates ();
30184 matching_parens parens
;
30185 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
30187 parens
.consume_open (parser
);
30189 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
30190 "%<sizeof...%> argument must be surrounded by parentheses");
30192 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30193 tree name
= cp_parser_identifier (parser
);
30194 if (name
== error_mark_node
)
30195 return error_mark_node
;
30196 /* The name is not qualified. */
30197 parser
->scope
= NULL_TREE
;
30198 parser
->qualifying_scope
= NULL_TREE
;
30199 parser
->object_scope
= NULL_TREE
;
30200 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
30201 if (expr
== error_mark_node
)
30202 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
30204 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
30205 expr
= TREE_TYPE (expr
);
30206 else if (TREE_CODE (expr
) == CONST_DECL
)
30207 expr
= DECL_INITIAL (expr
);
30208 expr
= make_pack_expansion (expr
);
30209 PACK_EXPANSION_SIZEOF_P (expr
) = true;
30212 parens
.require_close (parser
);
30217 /* Parse the operand of `sizeof' (or a similar operator). Returns
30218 either a TYPE or an expression, depending on the form of the
30219 input. The KEYWORD indicates which kind of expression we have
30223 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
30225 tree expr
= NULL_TREE
;
30226 const char *saved_message
;
30227 const char *saved_message_arg
;
30228 bool saved_integral_constant_expression_p
;
30229 bool saved_non_integral_constant_expression_p
;
30231 /* If it's a `...', then we are computing the length of a parameter
30233 if (keyword
== RID_SIZEOF
30234 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30235 return cp_parser_sizeof_pack (parser
);
30237 /* Types cannot be defined in a `sizeof' expression. Save away the
30239 saved_message
= parser
->type_definition_forbidden_message
;
30240 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
30241 parser
->type_definition_forbidden_message
30242 = G_("types may not be defined in %qs expressions");
30243 parser
->type_definition_forbidden_message_arg
30244 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
30246 /* The restrictions on constant-expressions do not apply inside
30247 sizeof expressions. */
30248 saved_integral_constant_expression_p
30249 = parser
->integral_constant_expression_p
;
30250 saved_non_integral_constant_expression_p
30251 = parser
->non_integral_constant_expression_p
;
30252 parser
->integral_constant_expression_p
= false;
30254 /* Do not actually evaluate the expression. */
30255 ++cp_unevaluated_operand
;
30256 ++c_inhibit_evaluation_warnings
;
30257 /* If it's a `(', then we might be looking at the type-id
30259 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
30261 tree type
= NULL_TREE
;
30263 tentative_firewall
firewall (parser
);
30265 /* We can't be sure yet whether we're looking at a type-id or an
30267 cp_parser_parse_tentatively (parser
);
30269 matching_parens parens
;
30270 parens
.consume_open (parser
);
30272 /* Note: as a GNU Extension, compound literals are considered
30273 postfix-expressions as they are in C99, so they are valid
30274 arguments to sizeof. See comment in cp_parser_cast_expression
30276 if (cp_parser_compound_literal_p (parser
))
30277 cp_parser_simulate_error (parser
);
30280 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
30281 parser
->in_type_id_in_expr_p
= true;
30282 /* Look for the type-id. */
30283 type
= cp_parser_type_id (parser
);
30284 /* Look for the closing `)'. */
30285 parens
.require_close (parser
);
30286 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
30289 /* If all went well, then we're done. */
30290 if (cp_parser_parse_definitely (parser
))
30294 /* Commit to the tentative_firewall so we get syntax errors. */
30295 cp_parser_commit_to_tentative_parse (parser
);
30297 expr
= cp_parser_unary_expression (parser
);
30301 expr
= cp_parser_unary_expression (parser
);
30303 /* Go back to evaluating expressions. */
30304 --cp_unevaluated_operand
;
30305 --c_inhibit_evaluation_warnings
;
30307 /* And restore the old one. */
30308 parser
->type_definition_forbidden_message
= saved_message
;
30309 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
30310 parser
->integral_constant_expression_p
30311 = saved_integral_constant_expression_p
;
30312 parser
->non_integral_constant_expression_p
30313 = saved_non_integral_constant_expression_p
;
30318 /* If the current declaration has no declarator, return true. */
30321 cp_parser_declares_only_class_p (cp_parser
*parser
)
30323 /* If the next token is a `;' or a `,' then there is no
30325 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
30326 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
30329 /* Update the DECL_SPECS to reflect the storage class indicated by
30333 cp_parser_set_storage_class (cp_parser
*parser
,
30334 cp_decl_specifier_seq
*decl_specs
,
30338 cp_storage_class storage_class
;
30340 if (parser
->in_unbraced_linkage_specification_p
)
30342 error_at (token
->location
, "invalid use of %qD in linkage specification",
30343 ridpointers
[keyword
]);
30346 else if (decl_specs
->storage_class
!= sc_none
)
30348 decl_specs
->conflicting_specifiers_p
= true;
30352 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
30353 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
30354 && decl_specs
->gnu_thread_keyword_p
)
30356 pedwarn (decl_specs
->locations
[ds_thread
], 0,
30357 "%<__thread%> before %qD", ridpointers
[keyword
]);
30363 storage_class
= sc_auto
;
30366 storage_class
= sc_register
;
30369 storage_class
= sc_static
;
30372 storage_class
= sc_extern
;
30375 storage_class
= sc_mutable
;
30378 gcc_unreachable ();
30380 decl_specs
->storage_class
= storage_class
;
30381 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
30383 /* A storage class specifier cannot be applied alongside a typedef
30384 specifier. If there is a typedef specifier present then set
30385 conflicting_specifiers_p which will trigger an error later
30386 on in grokdeclarator. */
30387 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
30388 decl_specs
->conflicting_specifiers_p
= true;
30391 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
30392 is true, the type is a class or enum definition. */
30395 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
30398 bool type_definition_p
)
30400 decl_specs
->any_specifiers_p
= true;
30402 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30403 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30404 this is what happened. In system headers, we ignore these
30405 declarations so that G++ can work with system headers that are not
30407 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
30408 && !type_definition_p
30409 && (type_spec
== boolean_type_node
30410 || type_spec
== char8_type_node
30411 || type_spec
== char16_type_node
30412 || type_spec
== char32_type_node
30413 || type_spec
== wchar_type_node
)
30414 && (decl_specs
->type
30415 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
30416 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
30417 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
30418 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
30420 decl_specs
->redefined_builtin_type
= type_spec
;
30421 set_and_check_decl_spec_loc (decl_specs
,
30422 ds_redefined_builtin_type_spec
,
30424 if (!decl_specs
->type
)
30426 decl_specs
->type
= type_spec
;
30427 decl_specs
->type_definition_p
= false;
30428 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
30431 else if (decl_specs
->type
)
30432 decl_specs
->multiple_types_p
= true;
30435 decl_specs
->type
= type_spec
;
30436 decl_specs
->type_definition_p
= type_definition_p
;
30437 decl_specs
->redefined_builtin_type
= NULL_TREE
;
30438 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
30442 /* True iff TOKEN is the GNU keyword __thread. */
30445 token_is__thread (cp_token
*token
)
30447 gcc_assert (token
->keyword
== RID_THREAD
);
30448 return id_equal (token
->u
.value
, "__thread");
30451 /* Set the location for a declarator specifier and check if it is
30454 DECL_SPECS is the sequence of declarator specifiers onto which to
30457 DS is the single declarator specifier to set which location is to
30458 be set onto the existing sequence of declarators.
30460 LOCATION is the location for the declarator specifier to
30464 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
30465 cp_decl_spec ds
, cp_token
*token
)
30467 gcc_assert (ds
< ds_last
);
30469 if (decl_specs
== NULL
)
30472 location_t location
= token
->location
;
30474 if (decl_specs
->locations
[ds
] == 0)
30476 decl_specs
->locations
[ds
] = location
;
30477 if (ds
== ds_thread
)
30478 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
30484 if (decl_specs
->locations
[ds_long_long
] != 0)
30485 error_at (location
,
30486 "%<long long long%> is too long for GCC");
30489 decl_specs
->locations
[ds_long_long
] = location
;
30490 pedwarn_cxx98 (location
,
30492 "ISO C++ 1998 does not support %<long long%>");
30495 else if (ds
== ds_thread
)
30497 bool gnu
= token_is__thread (token
);
30498 gcc_rich_location
richloc (location
);
30499 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
30501 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
30502 error_at (&richloc
,
30503 "both %<__thread%> and %<thread_local%> specified");
30507 richloc
.add_fixit_remove ();
30508 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
30513 static const char *const decl_spec_names
[] = {
30532 gcc_rich_location
richloc (location
);
30533 richloc
.add_fixit_remove ();
30534 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
30539 /* Return true iff the declarator specifier DS is present in the
30540 sequence of declarator specifiers DECL_SPECS. */
30543 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
30546 gcc_assert (ds
< ds_last
);
30548 if (decl_specs
== NULL
)
30551 return decl_specs
->locations
[ds
] != 0;
30554 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30555 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30558 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
30560 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
30563 /* Issue an error message indicating that TOKEN_DESC was expected.
30564 If KEYWORD is true, it indicated this function is called by
30565 cp_parser_require_keword and the required token can only be
30566 a indicated keyword.
30568 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30569 within any error as the location of an "opening" token matching
30570 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30571 RT_CLOSE_PAREN). */
30574 cp_parser_required_error (cp_parser
*parser
,
30575 required_token token_desc
,
30577 location_t matching_location
)
30579 if (cp_parser_simulate_error (parser
))
30582 const char *gmsgid
= NULL
;
30583 switch (token_desc
)
30586 gmsgid
= G_("expected %<new%>");
30589 gmsgid
= G_("expected %<delete%>");
30592 gmsgid
= G_("expected %<return%>");
30595 gmsgid
= G_("expected %<while%>");
30598 gmsgid
= G_("expected %<extern%>");
30600 case RT_STATIC_ASSERT
:
30601 gmsgid
= G_("expected %<static_assert%>");
30604 gmsgid
= G_("expected %<decltype%>");
30607 gmsgid
= G_("expected %<operator%>");
30610 gmsgid
= G_("expected %<class%>");
30613 gmsgid
= G_("expected %<template%>");
30616 gmsgid
= G_("expected %<namespace%>");
30619 gmsgid
= G_("expected %<using%>");
30622 gmsgid
= G_("expected %<asm%>");
30625 gmsgid
= G_("expected %<try%>");
30628 gmsgid
= G_("expected %<catch%>");
30631 gmsgid
= G_("expected %<throw%>");
30634 gmsgid
= G_("expected %<auto%>");
30637 gmsgid
= G_("expected %<__label__%>");
30640 gmsgid
= G_("expected %<@try%>");
30642 case RT_AT_SYNCHRONIZED
:
30643 gmsgid
= G_("expected %<@synchronized%>");
30646 gmsgid
= G_("expected %<@throw%>");
30648 case RT_TRANSACTION_ATOMIC
:
30649 gmsgid
= G_("expected %<__transaction_atomic%>");
30651 case RT_TRANSACTION_RELAXED
:
30652 gmsgid
= G_("expected %<__transaction_relaxed%>");
30655 gmsgid
= G_("expected %<co_yield%>");
30661 if (!gmsgid
&& !keyword
)
30663 switch (token_desc
)
30666 gmsgid
= G_("expected %<;%>");
30668 case RT_OPEN_PAREN
:
30669 gmsgid
= G_("expected %<(%>");
30671 case RT_CLOSE_BRACE
:
30672 gmsgid
= G_("expected %<}%>");
30674 case RT_OPEN_BRACE
:
30675 gmsgid
= G_("expected %<{%>");
30677 case RT_CLOSE_SQUARE
:
30678 gmsgid
= G_("expected %<]%>");
30680 case RT_OPEN_SQUARE
:
30681 gmsgid
= G_("expected %<[%>");
30684 gmsgid
= G_("expected %<,%>");
30687 gmsgid
= G_("expected %<::%>");
30690 gmsgid
= G_("expected %<<%>");
30693 gmsgid
= G_("expected %<>%>");
30696 gmsgid
= G_("expected %<=%>");
30699 gmsgid
= G_("expected %<...%>");
30702 gmsgid
= G_("expected %<*%>");
30705 gmsgid
= G_("expected %<~%>");
30708 gmsgid
= G_("expected %<:%>");
30710 case RT_COLON_SCOPE
:
30711 gmsgid
= G_("expected %<:%> or %<::%>");
30713 case RT_CLOSE_PAREN
:
30714 gmsgid
= G_("expected %<)%>");
30716 case RT_COMMA_CLOSE_PAREN
:
30717 gmsgid
= G_("expected %<,%> or %<)%>");
30719 case RT_PRAGMA_EOL
:
30720 gmsgid
= G_("expected end of line");
30723 gmsgid
= G_("expected identifier");
30726 gmsgid
= G_("expected selection-statement");
30729 gmsgid
= G_("expected iteration-statement");
30732 gmsgid
= G_("expected jump-statement");
30735 gmsgid
= G_("expected class-key");
30737 case RT_CLASS_TYPENAME_TEMPLATE
:
30738 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
30741 gcc_unreachable ();
30746 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
30750 /* If the next token is of the indicated TYPE, consume it. Otherwise,
30751 issue an error message indicating that TOKEN_DESC was expected.
30753 Returns the token consumed, if the token had the appropriate type.
30754 Otherwise, returns NULL.
30756 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30757 within any error as the location of an "opening" token matching
30758 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30759 RT_CLOSE_PAREN). */
30762 cp_parser_require (cp_parser
* parser
,
30763 enum cpp_ttype type
,
30764 required_token token_desc
,
30765 location_t matching_location
)
30767 if (cp_lexer_next_token_is (parser
->lexer
, type
))
30768 return cp_lexer_consume_token (parser
->lexer
);
30771 /* Output the MESSAGE -- unless we're parsing tentatively. */
30772 if (!cp_parser_simulate_error (parser
))
30773 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
30774 matching_location
);
30779 /* An error message is produced if the next token is not '>'.
30780 All further tokens are skipped until the desired token is
30781 found or '{', '}', ';' or an unbalanced ')' or ']'. */
30784 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
30786 /* Current level of '< ... >'. */
30787 unsigned level
= 0;
30788 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
30789 unsigned nesting_depth
= 0;
30791 /* Are we ready, yet? If not, issue error message. */
30792 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
30795 /* Skip tokens until the desired token is found. */
30798 /* Peek at the next token. */
30799 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30802 if (!nesting_depth
)
30807 if (cxx_dialect
== cxx98
)
30808 /* C++0x views the `>>' operator as two `>' tokens, but
30811 else if (!nesting_depth
&& level
-- == 0)
30813 /* We've hit a `>>' where the first `>' closes the
30814 template argument list, and the second `>' is
30815 spurious. Just consume the `>>' and stop; we've
30816 already produced at least one error. */
30817 cp_lexer_consume_token (parser
->lexer
);
30820 /* Fall through for C++0x, so we handle the second `>' in
30822 gcc_fallthrough ();
30825 if (!nesting_depth
&& level
-- == 0)
30827 /* We've reached the token we want, consume it and stop. */
30828 cp_lexer_consume_token (parser
->lexer
);
30833 case CPP_OPEN_PAREN
:
30834 case CPP_OPEN_SQUARE
:
30838 case CPP_CLOSE_PAREN
:
30839 case CPP_CLOSE_SQUARE
:
30840 if (nesting_depth
-- == 0)
30845 case CPP_PRAGMA_EOL
:
30846 case CPP_SEMICOLON
:
30847 case CPP_OPEN_BRACE
:
30848 case CPP_CLOSE_BRACE
:
30849 /* The '>' was probably forgotten, don't look further. */
30856 /* Consume this token. */
30857 cp_lexer_consume_token (parser
->lexer
);
30861 /* If the next token is the indicated keyword, consume it. Otherwise,
30862 issue an error message indicating that TOKEN_DESC was expected.
30864 Returns the token consumed, if the token had the appropriate type.
30865 Otherwise, returns NULL. */
30868 cp_parser_require_keyword (cp_parser
* parser
,
30870 required_token token_desc
)
30872 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
30874 if (token
&& token
->keyword
!= keyword
)
30876 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
30884 /* Returns TRUE iff TOKEN is a token that can begin the body of a
30885 function-definition. */
30888 cp_parser_token_starts_function_definition_p (cp_token
* token
)
30890 return (/* An ordinary function-body begins with an `{'. */
30891 token
->type
== CPP_OPEN_BRACE
30892 /* A ctor-initializer begins with a `:'. */
30893 || token
->type
== CPP_COLON
30894 /* A function-try-block begins with `try'. */
30895 || token
->keyword
== RID_TRY
30896 /* A function-transaction-block begins with `__transaction_atomic'
30897 or `__transaction_relaxed'. */
30898 || token
->keyword
== RID_TRANSACTION_ATOMIC
30899 || token
->keyword
== RID_TRANSACTION_RELAXED
30900 /* The named return value extension begins with `return'. */
30901 || token
->keyword
== RID_RETURN
);
30904 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
30908 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
30912 token
= cp_lexer_peek_token (parser
->lexer
);
30913 return (token
->type
== CPP_OPEN_BRACE
30914 || (token
->type
== CPP_COLON
30915 && !parser
->colon_doesnt_start_class_def_p
));
30918 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30919 C++0x) ending a template-argument. */
30922 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
30926 token
= cp_lexer_peek_token (parser
->lexer
);
30927 return (token
->type
== CPP_COMMA
30928 || token
->type
== CPP_GREATER
30929 || token
->type
== CPP_ELLIPSIS
30930 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
30933 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30934 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
30937 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
30942 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
30943 if (token
->type
== CPP_LESS
)
30945 /* Check for the sequence `<::' in the original code. It would be lexed as
30946 `[:', where `[' is a digraph, and there is no whitespace before
30948 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
30951 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
30952 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
30958 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
30959 or none_type otherwise. */
30961 static enum tag_types
30962 cp_parser_token_is_class_key (cp_token
* token
)
30964 switch (token
->keyword
)
30969 return record_type
;
30978 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
30979 or none_type otherwise or if the token is null. */
30981 static enum tag_types
30982 cp_parser_token_is_type_parameter_key (cp_token
* token
)
30987 switch (token
->keyword
)
30992 return typename_type
;
30999 /* Diagnose redundant enum-keys. */
31002 cp_parser_maybe_warn_enum_key (cp_parser
*parser
, location_t key_loc
,
31003 tree type
, rid scoped_key
)
31005 if (!warn_redundant_tags
)
31008 tree type_decl
= TYPE_MAIN_DECL (type
);
31009 tree name
= DECL_NAME (type_decl
);
31010 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31011 push_deferring_access_checks (dk_no_check
);
31012 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
31013 pop_deferring_access_checks ();
31015 /* The enum-key is redundant for uses of the TYPE that are not
31016 declarations and for which name lookup returns just the type
31018 if (decl
!= type_decl
)
31021 if (scoped_key
!= RID_CLASS
31022 && scoped_key
!= RID_STRUCT
31023 && current_lang_name
!= lang_name_cplusplus
31024 && current_namespace
== global_namespace
)
31026 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31027 enum tag in shared C/C++ code in files (such as headers) included
31028 in the main source file. */
31029 const line_map_ordinary
*map
= NULL
;
31030 linemap_resolve_location (line_table
, key_loc
,
31031 LRK_MACRO_DEFINITION_LOCATION
,
31033 if (!MAIN_FILE_P (map
))
31037 gcc_rich_location
richloc (key_loc
);
31038 richloc
.add_fixit_remove (key_loc
);
31039 warning_at (&richloc
, OPT_Wredundant_tags
,
31040 "redundant enum-key %<enum%s%> in reference to %q#T",
31041 (scoped_key
== RID_CLASS
? " class"
31042 : scoped_key
== RID_STRUCT
? " struct" : ""), type
);
31045 /* Describes the set of declarations of a struct, class, or class template
31046 or its specializations. Used for -Wmismatched-tags. */
31048 class class_decl_loc_t
31052 class_decl_loc_t ()
31053 : locvec (), idxdef (), def_class_key ()
31058 /* Constructs an object for a single declaration of a class with
31059 CLASS_KEY at the current location in the current function (or
31060 at another scope). KEY_REDUNDANT is true if the class-key may
31061 be omitted in the current context without an ambiguity with
31062 another symbol with the same name.
31063 DEF_P is true for a class declaration that is a definition.
31064 CURLOC is the associated location. */
31065 class_decl_loc_t (tag_types class_key
, bool key_redundant
, bool def_p
,
31066 location_t curloc
= input_location
)
31067 : locvec (), idxdef (def_p
? 0 : UINT_MAX
), def_class_key (class_key
)
31070 class_key_loc_t
ckl (current_function_decl
, curloc
, class_key
,
31072 locvec
.quick_push (ckl
);
31075 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31076 isn't safely copyable and assignable and doesn't release storage
31078 class_decl_loc_t (const class_decl_loc_t
&rhs
)
31079 : locvec (rhs
.locvec
.copy ()), idxdef (rhs
.idxdef
),
31080 def_class_key (rhs
.def_class_key
)
31083 class_decl_loc_t
& operator= (const class_decl_loc_t
&rhs
)
31088 locvec
= rhs
.locvec
.copy ();
31089 idxdef
= rhs
.idxdef
;
31090 def_class_key
= rhs
.def_class_key
;
31094 ~class_decl_loc_t ()
31099 /* Issues -Wmismatched-tags for a single class. */
31100 void diag_mismatched_tags (tree
);
31102 /* Issues -Wmismatched-tags for all classes. */
31103 static void diag_mismatched_tags ();
31105 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31106 redundant tags (if -Wredundant-tags is enabled). */
31107 static void add (cp_parser
*, location_t
, tag_types
, tree
, bool, bool);
31109 /* Either adds this decl to the collection of class decls
31110 or diagnoses it, whichever is appropriate. */
31111 void add_or_diag_mismatched_tag (tree
, tag_types
, bool, bool);
31115 tree
function (unsigned i
) const
31117 return locvec
[i
].func
;
31120 location_t
location (unsigned i
) const
31122 return locvec
[i
].loc
;
31125 bool key_redundant (unsigned i
) const
31127 return locvec
[i
].key_redundant
;
31130 tag_types
class_key (unsigned i
) const
31132 return locvec
[i
].class_key
;
31135 /* True if a definition for the class has been seen. */
31136 bool def_p () const
31138 return idxdef
< locvec
.length ();
31141 /* The location of a single mention of a class type with the given
31143 struct class_key_loc_t
31145 class_key_loc_t (tree func
, location_t loc
, tag_types key
, bool redundant
)
31146 : func (func
), loc (loc
), class_key (key
), key_redundant (redundant
)
31149 /* The function the type is mentioned in. */
31151 /* The exact location. */
31153 /* The class-key used in the mention of the type. */
31154 tag_types class_key
;
31155 /* True when the class-key could be omitted at this location
31156 without an ambiguity with another symbol of the same name. */
31157 bool key_redundant
;
31159 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
31160 vec
<class_key_loc_t
> locvec
;
31161 /* LOCVEC index of the definition or UINT_MAX if none exists. */
31163 /* The class-key the class was last declared with or none_type when
31164 it has been declared with a mismatched key. */
31165 tag_types def_class_key
;
31167 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
31168 description above. */
31169 typedef hash_map
<tree_decl_hash
, class_decl_loc_t
> class_to_loc_map_t
;
31170 static class_to_loc_map_t class2loc
;
31173 class_decl_loc_t::class_to_loc_map_t
class_decl_loc_t::class2loc
;
31175 /* Issue an error message if the CLASS_KEY does not match the TYPE.
31176 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31177 is set for a declaration of class TYPE and clear for a reference to
31178 it that is not a declaration of it. */
31181 cp_parser_check_class_key (cp_parser
*parser
, location_t key_loc
,
31182 tag_types class_key
, tree type
, bool def_p
,
31185 if (type
== error_mark_node
)
31188 bool seen_as_union
= TREE_CODE (type
) == UNION_TYPE
;
31189 if (seen_as_union
!= (class_key
== union_type
))
31191 if (permerror (input_location
, "%qs tag used in naming %q#T",
31192 class_key
== union_type
? "union"
31193 : class_key
== record_type
? "struct" : "class",
31195 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
31196 "%q#T was previously declared here", type
);
31200 if (!warn_mismatched_tags
&& !warn_redundant_tags
)
31203 /* Only consider the true class-keys below and ignore typename_type,
31204 etc. that are not C++ class-keys. */
31205 if (class_key
!= class_type
31206 && class_key
!= record_type
31207 && class_key
!= union_type
)
31210 class_decl_loc_t::add (parser
, key_loc
, class_key
, type
, def_p
, decl_p
);
31213 /* Returns the template or specialization of one to which the RECORD_TYPE
31214 TYPE corresponds. */
31217 specialization_of (tree type
)
31221 /* Determine the template or its partial specialization to which TYPE
31223 if (tree spec
= most_specialized_partial_spec (type
, tf_none
))
31224 if (spec
!= error_mark_node
)
31225 ret
= TREE_TYPE (TREE_VALUE (spec
));
31228 ret
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type
);
31230 return TYPE_MAIN_DECL (ret
);
31234 /* Adds the class TYPE to the collection of class decls and diagnoses
31235 redundant tags (if -Wredundant-tags is enabled).
31236 DEF_P is expected to be set for a definition of class TYPE. DECL_P
31237 is set for a (likely, based on syntactic context) declaration of class
31238 TYPE and clear for a reference to it that is not a declaration of it. */
31241 class_decl_loc_t::add (cp_parser
*parser
, location_t key_loc
,
31242 tag_types class_key
, tree type
, bool def_p
, bool decl_p
)
31244 tree type_decl
= TYPE_MAIN_DECL (type
);
31245 tree name
= DECL_NAME (type_decl
);
31246 /* Look up the NAME to see if it unambiguously refers to the TYPE
31247 and set KEY_REDUNDANT if so. */
31248 push_deferring_access_checks (dk_no_check
);
31249 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
31250 pop_deferring_access_checks ();
31252 /* The class-key is redundant for uses of the CLASS_TYPE that are
31253 neither definitions of it nor declarations, and for which name
31254 lookup returns just the type itself. */
31255 bool key_redundant
= (!def_p
&& !decl_p
31256 && (decl
== type_decl
31257 || TREE_CODE (decl
) == TEMPLATE_DECL
31258 || TYPE_BEING_DEFINED (type
)));
31261 && class_key
!= class_type
31262 && current_lang_name
!= lang_name_cplusplus
31263 && current_namespace
== global_namespace
)
31265 /* Avoid issuing the diagnostic for apparently redundant struct
31266 and union class-keys in shared C/C++ code in files (such as
31267 headers) included in the main source file. */
31268 const line_map_ordinary
*map
= NULL
;
31269 linemap_resolve_location (line_table
, key_loc
,
31270 LRK_MACRO_DEFINITION_LOCATION
,
31272 if (!MAIN_FILE_P (map
))
31273 key_redundant
= false;
31276 /* Set if a declaration of TYPE has previously been seen or if it must
31277 exist in a precompiled header. */
31279 class_decl_loc_t
*rdl
= &class2loc
.get_or_insert (type_decl
, &exist
);
31282 tree type
= TREE_TYPE (type_decl
);
31283 if (def_p
|| !COMPLETE_TYPE_P (type
))
31285 /* TYPE_DECL is the first declaration or definition of the type
31286 (outside precompiled headers -- see below). Just create
31287 a new entry for it and return unless it's a declaration
31288 involving a template that may need to be diagnosed by
31289 -Wredundant-tags. */
31290 *rdl
= class_decl_loc_t (class_key
, false, def_p
);
31291 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
31296 /* TYPE was previously defined in some unknown precompiled hdeader.
31297 Simply add a record of its definition at an unknown location and
31298 proceed below to add a reference to it at the current location.
31299 (Declarations in precompiled headers that are not definitions
31302 = CLASSTYPE_DECLARED_CLASS (type
) ? class_type
: record_type
;
31303 location_t def_loc
= DECL_SOURCE_LOCATION (type_decl
);
31304 *rdl
= class_decl_loc_t (def_key
, false, true, def_loc
);
31309 /* A prior declaration of TYPE_DECL has been seen. */
31313 gcc_rich_location
richloc (key_loc
);
31314 richloc
.add_fixit_remove (key_loc
);
31315 warning_at (&richloc
, OPT_Wredundant_tags
,
31316 "redundant class-key %qs in reference to %q#T",
31317 class_key
== union_type
? "union"
31318 : class_key
== record_type
? "struct" : "class",
31323 /* Do nothing if this is the first declaration of the type. */
31326 if (rdl
->idxdef
!= UINT_MAX
&& rdl
->def_class_key
== class_key
)
31327 /* Do nothing if the class-key in this declaration matches
31331 rdl
->add_or_diag_mismatched_tag (type_decl
, class_key
, key_redundant
,
31335 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
31336 of class decls or diagnoses it, whichever is appropriate. */
31339 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl
,
31340 tag_types class_key
,
31344 /* Reset the CLASS_KEY associated with this type on mismatch.
31345 This is an optimization that lets the diagnostic code skip
31346 over classes that use the same class-key in all declarations. */
31347 if (def_class_key
!= class_key
)
31348 def_class_key
= none_type
;
31350 /* Set IDXDEF to the index of the vector corresponding to
31353 idxdef
= locvec
.length ();
31355 /* Append a record of this declaration to the vector. */
31356 class_key_loc_t
ckl (current_function_decl
, input_location
, class_key
,
31358 locvec
.safe_push (ckl
);
31360 if (idxdef
== UINT_MAX
)
31363 /* As a space optimization diagnose declarations of a class
31364 whose definition has been seen and purge the LOCVEC of
31365 all entries except the definition. */
31366 diag_mismatched_tags (type_decl
);
31369 class_decl_loc_t::class_key_loc_t ent
= locvec
[idxdef
];
31371 locvec
.reserve (2);
31372 locvec
.safe_push (ent
);
31376 /* Pop the entry pushed above for this declaration. */
31380 /* Issues -Wmismatched-tags for a single class. */
31383 class_decl_loc_t::diag_mismatched_tags (tree type_decl
)
31385 if (!warn_mismatched_tags
)
31388 /* Number of uses of the class. */
31389 const unsigned ndecls
= locvec
.length ();
31391 /* The class (or template) declaration guiding the decisions about
31392 the diagnostic. For ordinary classes it's the same as THIS. For
31393 uses of instantiations of templates other than their declarations
31394 it points to the record for the declaration of the corresponding
31395 primary template or partial specialization. */
31396 class_decl_loc_t
*cdlguide
= this;
31398 tree type
= TREE_TYPE (type_decl
);
31399 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
))
31401 /* For implicit instantiations of a primary template look up
31402 the primary or partial specialization and use it as
31403 the expected class-key rather than using the class-key of
31404 the first reference to the instantiation. The primary must
31405 be (and inevitably is) at index zero. */
31406 tree spec
= specialization_of (type
);
31407 cdlguide
= class2loc
.get (spec
);
31408 gcc_assert (cdlguide
!= NULL
);
31412 /* Skip declarations that consistently use the same class-key. */
31413 if (def_class_key
!= none_type
)
31417 /* Set if a definition for the class has been seen. */
31418 const bool def_p
= cdlguide
->def_p ();
31420 /* The index of the declaration whose class-key this declaration
31421 is expected to match. It's either the class-key of the class
31422 definition if one exists or the first declaration otherwise. */
31423 const unsigned idxguide
= def_p
? cdlguide
->idxdef
: 0;
31425 /* The class-key the class is expected to be declared with: it's
31426 either the key used in its definition or the first declaration
31427 if no definition has been provided.
31428 For implicit instantiations of a primary template it's
31429 the class-key used to declare the primary with. The primary
31430 must be at index zero. */
31431 const tag_types xpect_key
= cdlguide
->class_key (idxguide
);
31434 /* Advance IDX to the first declaration that either is not
31435 a definition or that doesn't match the first declaration
31436 if no definition is provided. */
31437 while (class_key (idx
) == xpect_key
)
31438 if (++idx
== ndecls
)
31441 /* Save the current function before changing it below. */
31442 tree save_func
= current_function_decl
;
31443 /* Set the function declaration to print in diagnostic context. */
31444 current_function_decl
= function (idx
);
31446 const char *xmatchkstr
= xpect_key
== record_type
? "class" : "struct";
31447 const char *xpectkstr
= xpect_key
== record_type
? "struct" : "class";
31449 location_t loc
= location (idx
);
31450 bool key_redundant_p
= key_redundant (idx
);
31451 auto_diagnostic_group d
;
31452 /* Issue a warning for the first mismatched declaration.
31453 Avoid using "%#qT" since the class-key for the same type will
31454 be the same regardless of which one was used in the declaraion. */
31455 if (warning_at (loc
, OPT_Wmismatched_tags
,
31456 "%qT declared with a mismatched class-key %qs",
31457 type_decl
, xmatchkstr
))
31459 /* Suggest how to avoid the warning for each instance since
31460 the guidance may be different depending on context. */
31463 ? G_("remove the class-key or replace it with %qs")
31464 : G_("replace the class-key with %qs")),
31467 /* Also point to the first declaration or definition that guided
31468 the decision to issue the warning above. */
31469 inform (cdlguide
->location (idxguide
),
31471 ? G_("%qT defined as %qs here")
31472 : G_("%qT first declared as %qs here")),
31473 type_decl
, xpectkstr
);
31476 /* Issue warnings for the remaining inconsistent declarations. */
31477 for (unsigned i
= idx
+ 1; i
!= ndecls
; ++i
)
31479 tag_types clskey
= class_key (i
);
31480 /* Skip over the declarations that match either the definition
31481 if one was provided or the first declaration. */
31482 if (clskey
== xpect_key
)
31485 loc
= location (i
);
31486 key_redundant_p
= key_redundant (i
);
31487 /* Set the function declaration to print in diagnostic context. */
31488 current_function_decl
= function (i
);
31489 if (warning_at (loc
, OPT_Wmismatched_tags
,
31490 "%qT declared with a mismatched class-key %qs",
31491 type_decl
, xmatchkstr
))
31492 /* Suggest how to avoid the warning for each instance since
31493 the guidance may be different depending on context. */
31496 ? G_("remove the class-key or replace it with %qs")
31497 : G_("replace the class-key with %qs")),
31501 /* Restore the current function in case it was replaced above. */
31502 current_function_decl
= save_func
;
31505 /* Issues -Wmismatched-tags for all classes. Called at the end
31506 of processing a translation unit, after declarations of all class
31507 types and their uses have been recorded. */
31510 class_decl_loc_t::diag_mismatched_tags ()
31512 /* CLASS2LOC should be empty if both -Wmismatched-tags and
31513 -Wredundant-tags are disabled. */
31514 gcc_assert (warn_mismatched_tags
31515 || warn_redundant_tags
31516 || class2loc
.is_empty ());
31518 /* Save the current function before changing on return. It should
31519 be null at this point. */
31520 temp_override
<tree
> cleanup (current_function_decl
);
31522 if (warn_mismatched_tags
)
31524 /* Iterate over the collected class/struct/template declarations. */
31525 typedef class_to_loc_map_t::iterator iter_t
;
31526 for (iter_t it
= class2loc
.begin (); it
!= class2loc
.end (); ++it
)
31528 tree type_decl
= (*it
).first
;
31529 class_decl_loc_t
&recloc
= (*it
).second
;
31530 recloc
.diag_mismatched_tags (type_decl
);
31534 class2loc
.empty ();
31537 /* Issue an error message if DECL is redeclared with different
31538 access than its original declaration [class.access.spec/3].
31539 This applies to nested classes, nested class templates and
31540 enumerations [class.mem/1]. */
31543 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
31546 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
31547 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
31550 if ((TREE_PRIVATE (decl
)
31551 != (current_access_specifier
== access_private_node
))
31552 || (TREE_PROTECTED (decl
)
31553 != (current_access_specifier
== access_protected_node
)))
31554 error_at (location
, "%qD redeclared with different access", decl
);
31557 /* Look for the `template' keyword, as a syntactic disambiguator.
31558 Return TRUE iff it is present, in which case it will be
31562 cp_parser_optional_template_keyword (cp_parser
*parser
)
31564 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
31566 /* In C++98 the `template' keyword can only be used within templates;
31567 outside templates the parser can always figure out what is a
31568 template and what is not. In C++11, per the resolution of DR 468,
31569 `template' is allowed in cases where it is not strictly necessary. */
31570 if (!processing_template_decl
31571 && pedantic
&& cxx_dialect
== cxx98
)
31573 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31574 pedwarn (token
->location
, OPT_Wpedantic
,
31575 "in C++98 %<template%> (as a disambiguator) is only "
31576 "allowed within templates");
31577 /* If this part of the token stream is rescanned, the same
31578 error message would be generated. So, we purge the token
31579 from the stream. */
31580 cp_lexer_purge_token (parser
->lexer
);
31585 /* Consume the `template' keyword. */
31586 cp_lexer_consume_token (parser
->lexer
);
31593 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
31594 set PARSER->SCOPE, and perform other related actions. */
31597 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
31599 struct tree_check
*check_value
;
31601 /* Get the stored value. */
31602 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
31603 /* Set the scope from the stored value. */
31604 parser
->scope
= saved_checks_value (check_value
);
31605 parser
->qualifying_scope
= check_value
->qualifying_scope
;
31606 parser
->object_scope
= NULL_TREE
;
31609 /* Consume tokens up through a non-nested END token. Returns TRUE if we
31610 encounter the end of a block before what we were looking for. */
31613 cp_parser_cache_group (cp_parser
*parser
,
31614 enum cpp_ttype end
,
31619 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31621 /* Abort a parenthesized expression if we encounter a semicolon. */
31622 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
31623 && token
->type
== CPP_SEMICOLON
)
31625 /* If we've reached the end of the file, stop. */
31626 if (token
->type
== CPP_EOF
31627 || (end
!= CPP_PRAGMA_EOL
31628 && token
->type
== CPP_PRAGMA_EOL
))
31630 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
31631 /* We've hit the end of an enclosing block, so there's been some
31632 kind of syntax error. */
31635 /* Consume the token. */
31636 cp_lexer_consume_token (parser
->lexer
);
31637 /* See if it starts a new group. */
31638 if (token
->type
== CPP_OPEN_BRACE
)
31640 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
31641 /* In theory this should probably check end == '}', but
31642 cp_parser_save_member_function_body needs it to exit
31643 after either '}' or ')' when called with ')'. */
31647 else if (token
->type
== CPP_OPEN_PAREN
)
31649 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
31650 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
31653 else if (token
->type
== CPP_PRAGMA
)
31654 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
31655 else if (token
->type
== end
)
31660 /* Like above, for caching a default argument or NSDMI. Both of these are
31661 terminated by a non-nested comma, but it can be unclear whether or not a
31662 comma is nested in a template argument list unless we do more parsing.
31663 In order to handle this ambiguity, when we encounter a ',' after a '<'
31664 we try to parse what follows as a parameter-declaration-list (in the
31665 case of a default argument) or a member-declarator (in the case of an
31666 NSDMI). If that succeeds, then we stop caching. */
31669 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
31671 unsigned depth
= 0;
31672 int maybe_template_id
= 0;
31673 cp_token
*first_token
;
31675 tree default_argument
;
31677 /* Add tokens until we have processed the entire default
31678 argument. We add the range [first_token, token). */
31679 first_token
= cp_lexer_peek_token (parser
->lexer
);
31680 if (first_token
->type
== CPP_OPEN_BRACE
)
31682 /* For list-initialization, this is straightforward. */
31683 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
31684 token
= cp_lexer_peek_token (parser
->lexer
);
31690 /* Peek at the next token. */
31691 token
= cp_lexer_peek_token (parser
->lexer
);
31692 /* What we do depends on what token we have. */
31693 switch (token
->type
)
31695 /* In valid code, a default argument must be
31696 immediately followed by a `,' `)', or `...'. */
31698 if (depth
== 0 && maybe_template_id
)
31700 /* If we've seen a '<', we might be in a
31701 template-argument-list. Until Core issue 325 is
31702 resolved, we don't know how this situation ought
31703 to be handled, so try to DTRT. We check whether
31704 what comes after the comma is a valid parameter
31705 declaration list. If it is, then the comma ends
31706 the default argument; otherwise the default
31707 argument continues. */
31708 bool error
= false;
31711 /* Set ITALP so cp_parser_parameter_declaration_list
31712 doesn't decide to commit to this parse. */
31713 bool saved_italp
= parser
->in_template_argument_list_p
;
31714 parser
->in_template_argument_list_p
= true;
31716 cp_parser_parse_tentatively (parser
);
31720 /* Parse declarators until we reach a non-comma or
31721 somthing that cannot be an initializer.
31722 Just checking whether we're looking at a single
31723 declarator is insufficient. Consider:
31724 int var = tuple<T,U>::x;
31725 The template parameter 'U' looks exactly like a
31729 int ctor_dtor_or_conv_p
;
31730 cp_lexer_consume_token (parser
->lexer
);
31731 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
31732 CP_PARSER_FLAGS_NONE
,
31733 &ctor_dtor_or_conv_p
,
31734 /*parenthesized_p=*/NULL
,
31736 /*friend_p=*/false,
31737 /*static_p=*/false);
31738 peek
= cp_lexer_peek_token (parser
->lexer
);
31739 if (cp_parser_error_occurred (parser
))
31742 while (peek
->type
== CPP_COMMA
);
31743 /* If we met an '=' or ';' then the original comma
31744 was the end of the NSDMI. Otherwise assume
31745 we're still in the NSDMI. */
31746 error
= (peek
->type
!= CPP_EQ
31747 && peek
->type
!= CPP_SEMICOLON
);
31751 cp_lexer_consume_token (parser
->lexer
);
31752 begin_scope (sk_function_parms
, NULL_TREE
);
31753 tree t
= cp_parser_parameter_declaration_list
31754 (parser
, CP_PARSER_FLAGS_NONE
);
31755 if (t
== error_mark_node
)
31757 pop_bindings_and_leave_scope ();
31759 if (!cp_parser_error_occurred (parser
) && !error
)
31761 cp_parser_abort_tentative_parse (parser
);
31763 parser
->in_template_argument_list_p
= saved_italp
;
31767 case CPP_CLOSE_PAREN
:
31769 /* If we run into a non-nested `;', `}', or `]',
31770 then the code is invalid -- but the default
31771 argument is certainly over. */
31772 case CPP_SEMICOLON
:
31773 case CPP_CLOSE_BRACE
:
31774 case CPP_CLOSE_SQUARE
:
31776 /* Handle correctly int n = sizeof ... ( p ); */
31777 && token
->type
!= CPP_ELLIPSIS
)
31779 /* Update DEPTH, if necessary. */
31780 else if (token
->type
== CPP_CLOSE_PAREN
31781 || token
->type
== CPP_CLOSE_BRACE
31782 || token
->type
== CPP_CLOSE_SQUARE
)
31786 case CPP_OPEN_PAREN
:
31787 case CPP_OPEN_SQUARE
:
31788 case CPP_OPEN_BRACE
:
31794 /* This might be the comparison operator, or it might
31795 start a template argument list. */
31796 ++maybe_template_id
;
31800 if (cxx_dialect
== cxx98
)
31802 /* Fall through for C++0x, which treats the `>>'
31803 operator like two `>' tokens in certain
31805 gcc_fallthrough ();
31810 /* This might be an operator, or it might close a
31811 template argument list. But if a previous '<'
31812 started a template argument list, this will have
31813 closed it, so we can't be in one anymore. */
31814 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
31815 if (maybe_template_id
< 0)
31816 maybe_template_id
= 0;
31820 /* If we run out of tokens, issue an error message. */
31822 case CPP_PRAGMA_EOL
:
31823 error_at (token
->location
, "file ends in default argument");
31824 return error_mark_node
;
31828 /* In these cases, we should look for template-ids.
31829 For example, if the default argument is
31830 `X<int, double>()', we need to do name lookup to
31831 figure out whether or not `X' is a template; if
31832 so, the `,' does not end the default argument.
31834 That is not yet done. */
31841 /* If we've reached the end, stop. */
31845 /* Add the token to the token block. */
31846 token
= cp_lexer_consume_token (parser
->lexer
);
31849 /* Create a DEFERRED_PARSE to represent the unparsed default
31851 default_argument
= make_node (DEFERRED_PARSE
);
31852 DEFPARSE_TOKENS (default_argument
)
31853 = cp_token_cache_new (first_token
, token
);
31854 DEFPARSE_INSTANTIATIONS (default_argument
) = NULL
;
31856 return default_argument
;
31859 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
31862 defparse_location (tree default_argument
)
31864 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_argument
);
31865 location_t start
= tokens
->first
->location
;
31866 location_t end
= tokens
->last
->location
;
31867 return make_location (start
, start
, end
);
31870 /* Begin parsing tentatively. We always save tokens while parsing
31871 tentatively so that if the tentative parsing fails we can restore the
31875 cp_parser_parse_tentatively (cp_parser
* parser
)
31877 /* Enter a new parsing context. */
31878 parser
->context
= cp_parser_context_new (parser
->context
);
31879 /* Begin saving tokens. */
31880 cp_lexer_save_tokens (parser
->lexer
);
31881 /* In order to avoid repetitive access control error messages,
31882 access checks are queued up until we are no longer parsing
31884 push_deferring_access_checks (dk_deferred
);
31887 /* Commit to the currently active tentative parse. */
31890 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
31892 cp_parser_context
*context
;
31895 /* Mark all of the levels as committed. */
31896 lexer
= parser
->lexer
;
31897 for (context
= parser
->context
; context
->next
; context
= context
->next
)
31899 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
31901 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
31902 while (!cp_lexer_saving_tokens (lexer
))
31903 lexer
= lexer
->next
;
31904 cp_lexer_commit_tokens (lexer
);
31908 /* Commit to the topmost currently active tentative parse.
31910 Note that this function shouldn't be called when there are
31911 irreversible side-effects while in a tentative state. For
31912 example, we shouldn't create a permanent entry in the symbol
31913 table, or issue an error message that might not apply if the
31914 tentative parse is aborted. */
31917 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
31919 cp_parser_context
*context
= parser
->context
;
31920 cp_lexer
*lexer
= parser
->lexer
;
31924 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
31926 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
31928 while (!cp_lexer_saving_tokens (lexer
))
31929 lexer
= lexer
->next
;
31930 cp_lexer_commit_tokens (lexer
);
31934 /* Abort the currently active tentative parse. All consumed tokens
31935 will be rolled back, and no diagnostics will be issued. */
31938 cp_parser_abort_tentative_parse (cp_parser
* parser
)
31940 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
31941 || errorcount
> 0);
31942 cp_parser_simulate_error (parser
);
31943 /* Now, pretend that we want to see if the construct was
31944 successfully parsed. */
31945 cp_parser_parse_definitely (parser
);
31948 /* Stop parsing tentatively. If a parse error has occurred, restore the
31949 token stream. Otherwise, commit to the tokens we have consumed.
31950 Returns true if no error occurred; false otherwise. */
31953 cp_parser_parse_definitely (cp_parser
* parser
)
31955 bool error_occurred
;
31956 cp_parser_context
*context
;
31958 /* Remember whether or not an error occurred, since we are about to
31959 destroy that information. */
31960 error_occurred
= cp_parser_error_occurred (parser
);
31961 /* Remove the topmost context from the stack. */
31962 context
= parser
->context
;
31963 parser
->context
= context
->next
;
31964 /* If no parse errors occurred, commit to the tentative parse. */
31965 if (!error_occurred
)
31967 /* Commit to the tokens read tentatively, unless that was
31969 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
31970 cp_lexer_commit_tokens (parser
->lexer
);
31972 pop_to_parent_deferring_access_checks ();
31974 /* Otherwise, if errors occurred, roll back our state so that things
31975 are just as they were before we began the tentative parse. */
31978 cp_lexer_rollback_tokens (parser
->lexer
);
31979 pop_deferring_access_checks ();
31981 /* Add the context to the front of the free list. */
31982 context
->next
= cp_parser_context_free_list
;
31983 cp_parser_context_free_list
= context
;
31985 return !error_occurred
;
31988 /* Returns true if we are parsing tentatively and are not committed to
31989 this tentative parse. */
31992 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
31994 return (cp_parser_parsing_tentatively (parser
)
31995 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
31998 /* Returns nonzero iff an error has occurred during the most recent
31999 tentative parse. */
32002 cp_parser_error_occurred (cp_parser
* parser
)
32004 return (cp_parser_parsing_tentatively (parser
)
32005 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
32008 /* Returns nonzero if GNU extensions are allowed. */
32011 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
32013 return parser
->allow_gnu_extensions_p
;
32016 /* Objective-C++ Productions */
32019 /* Parse an Objective-C expression, which feeds into a primary-expression
32023 objc-message-expression
32024 objc-string-literal
32025 objc-encode-expression
32026 objc-protocol-expression
32027 objc-selector-expression
32029 Returns a tree representation of the expression. */
32032 cp_parser_objc_expression (cp_parser
* parser
)
32034 /* Try to figure out what kind of declaration is present. */
32035 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
32039 case CPP_OPEN_SQUARE
:
32040 return cp_parser_objc_message_expression (parser
);
32042 case CPP_OBJC_STRING
:
32043 kwd
= cp_lexer_consume_token (parser
->lexer
);
32044 return objc_build_string_object (kwd
->u
.value
);
32047 switch (kwd
->keyword
)
32049 case RID_AT_ENCODE
:
32050 return cp_parser_objc_encode_expression (parser
);
32052 case RID_AT_PROTOCOL
:
32053 return cp_parser_objc_protocol_expression (parser
);
32055 case RID_AT_SELECTOR
:
32056 return cp_parser_objc_selector_expression (parser
);
32063 error_at (kwd
->location
,
32064 "misplaced %<@%D%> Objective-C++ construct",
32066 cp_parser_skip_to_end_of_block_or_statement (parser
);
32069 return error_mark_node
;
32072 /* Parse an Objective-C message expression.
32074 objc-message-expression:
32075 [ objc-message-receiver objc-message-args ]
32077 Returns a representation of an Objective-C message. */
32080 cp_parser_objc_message_expression (cp_parser
* parser
)
32082 tree receiver
, messageargs
;
32084 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32085 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
32086 receiver
= cp_parser_objc_message_receiver (parser
);
32087 messageargs
= cp_parser_objc_message_args (parser
);
32088 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32089 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32091 tree result
= objc_build_message_expr (receiver
, messageargs
);
32093 /* Construct a location e.g.
32096 ranging from the '[' to the ']', with the caret at the start. */
32097 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
32098 protected_set_expr_location (result
, combined_loc
);
32103 /* Parse an objc-message-receiver.
32105 objc-message-receiver:
32107 simple-type-specifier
32109 Returns a representation of the type or expression. */
32112 cp_parser_objc_message_receiver (cp_parser
* parser
)
32116 /* An Objective-C message receiver may be either (1) a type
32117 or (2) an expression. */
32118 cp_parser_parse_tentatively (parser
);
32119 rcv
= cp_parser_expression (parser
);
32121 /* If that worked out, fine. */
32122 if (cp_parser_parse_definitely (parser
))
32125 cp_parser_parse_tentatively (parser
);
32126 rcv
= cp_parser_simple_type_specifier (parser
,
32127 /*decl_specs=*/NULL
,
32128 CP_PARSER_FLAGS_NONE
);
32130 if (cp_parser_parse_definitely (parser
))
32131 return objc_get_class_reference (rcv
);
32133 cp_parser_error (parser
, "objective-c++ message receiver expected");
32134 return error_mark_node
;
32137 /* Parse the arguments and selectors comprising an Objective-C message.
32142 objc-selector-args , objc-comma-args
32144 objc-selector-args:
32145 objc-selector [opt] : assignment-expression
32146 objc-selector-args objc-selector [opt] : assignment-expression
32149 assignment-expression
32150 objc-comma-args , assignment-expression
32152 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
32153 selector arguments and TREE_VALUE containing a list of comma
32157 cp_parser_objc_message_args (cp_parser
* parser
)
32159 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
32160 bool maybe_unary_selector_p
= true;
32161 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32163 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
32165 tree selector
= NULL_TREE
, arg
;
32167 if (token
->type
!= CPP_COLON
)
32168 selector
= cp_parser_objc_selector (parser
);
32170 /* Detect if we have a unary selector. */
32171 if (maybe_unary_selector_p
32172 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
32173 return build_tree_list (selector
, NULL_TREE
);
32175 maybe_unary_selector_p
= false;
32176 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
32177 arg
= cp_parser_assignment_expression (parser
);
32180 = chainon (sel_args
,
32181 build_tree_list (selector
, arg
));
32183 token
= cp_lexer_peek_token (parser
->lexer
);
32186 /* Handle non-selector arguments, if any. */
32187 while (token
->type
== CPP_COMMA
)
32191 cp_lexer_consume_token (parser
->lexer
);
32192 arg
= cp_parser_assignment_expression (parser
);
32195 = chainon (addl_args
,
32196 build_tree_list (NULL_TREE
, arg
));
32198 token
= cp_lexer_peek_token (parser
->lexer
);
32201 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
32203 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
32204 return build_tree_list (error_mark_node
, error_mark_node
);
32207 return build_tree_list (sel_args
, addl_args
);
32210 /* Parse an Objective-C encode expression.
32212 objc-encode-expression:
32213 @encode objc-typename
32215 Returns an encoded representation of the type argument. */
32218 cp_parser_objc_encode_expression (cp_parser
* parser
)
32222 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32224 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
32225 matching_parens parens
;
32226 parens
.require_open (parser
);
32227 token
= cp_lexer_peek_token (parser
->lexer
);
32228 type
= complete_type (cp_parser_type_id (parser
));
32229 parens
.require_close (parser
);
32233 error_at (token
->location
,
32234 "%<@encode%> must specify a type as an argument");
32235 return error_mark_node
;
32238 /* This happens if we find @encode(T) (where T is a template
32239 typename or something dependent on a template typename) when
32240 parsing a template. In that case, we can't compile it
32241 immediately, but we rather create an AT_ENCODE_EXPR which will
32242 need to be instantiated when the template is used.
32244 if (dependent_type_p (type
))
32246 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
32247 TREE_READONLY (value
) = 1;
32252 /* Build a location of the form:
32255 with caret==start at the @ token, finishing at the close paren. */
32256 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
32258 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
32261 /* Parse an Objective-C @defs expression. */
32264 cp_parser_objc_defs_expression (cp_parser
*parser
)
32268 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
32269 matching_parens parens
;
32270 parens
.require_open (parser
);
32271 name
= cp_parser_identifier (parser
);
32272 parens
.require_close (parser
);
32274 return objc_get_class_ivars (name
);
32277 /* Parse an Objective-C protocol expression.
32279 objc-protocol-expression:
32280 @protocol ( identifier )
32282 Returns a representation of the protocol expression. */
32285 cp_parser_objc_protocol_expression (cp_parser
* parser
)
32288 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32290 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
32291 matching_parens parens
;
32292 parens
.require_open (parser
);
32293 proto
= cp_parser_identifier (parser
);
32294 parens
.require_close (parser
);
32296 /* Build a location of the form:
32299 with caret==start at the @ token, finishing at the close paren. */
32300 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
32301 tree result
= objc_build_protocol_expr (proto
);
32302 protected_set_expr_location (result
, combined_loc
);
32306 /* Parse an Objective-C selector expression.
32308 objc-selector-expression:
32309 @selector ( objc-method-signature )
32311 objc-method-signature:
32317 objc-selector-seq objc-selector :
32319 Returns a representation of the method selector. */
32322 cp_parser_objc_selector_expression (cp_parser
* parser
)
32324 tree sel_seq
= NULL_TREE
;
32325 bool maybe_unary_selector_p
= true;
32327 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32329 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
32330 matching_parens parens
;
32331 parens
.require_open (parser
);
32332 token
= cp_lexer_peek_token (parser
->lexer
);
32334 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
32335 || token
->type
== CPP_SCOPE
)
32337 tree selector
= NULL_TREE
;
32339 if (token
->type
!= CPP_COLON
32340 || token
->type
== CPP_SCOPE
)
32341 selector
= cp_parser_objc_selector (parser
);
32343 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
32344 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
32346 /* Detect if we have a unary selector. */
32347 if (maybe_unary_selector_p
)
32349 sel_seq
= selector
;
32350 goto finish_selector
;
32354 cp_parser_error (parser
, "expected %<:%>");
32357 maybe_unary_selector_p
= false;
32358 token
= cp_lexer_consume_token (parser
->lexer
);
32360 if (token
->type
== CPP_SCOPE
)
32363 = chainon (sel_seq
,
32364 build_tree_list (selector
, NULL_TREE
));
32366 = chainon (sel_seq
,
32367 build_tree_list (NULL_TREE
, NULL_TREE
));
32371 = chainon (sel_seq
,
32372 build_tree_list (selector
, NULL_TREE
));
32374 token
= cp_lexer_peek_token (parser
->lexer
);
32378 parens
.require_close (parser
);
32381 /* Build a location of the form:
32384 with caret==start at the @ token, finishing at the close paren. */
32385 location_t combined_loc
= make_location (loc
, loc
, parser
->lexer
);
32386 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
32387 /* TODO: objc_build_selector_expr doesn't always honor the location. */
32388 protected_set_expr_location (result
, combined_loc
);
32392 /* Parse a list of identifiers.
32394 objc-identifier-list:
32396 objc-identifier-list , identifier
32398 Returns a TREE_LIST of identifier nodes. */
32401 cp_parser_objc_identifier_list (cp_parser
* parser
)
32407 identifier
= cp_parser_identifier (parser
);
32408 if (identifier
== error_mark_node
)
32409 return error_mark_node
;
32411 list
= build_tree_list (NULL_TREE
, identifier
);
32412 sep
= cp_lexer_peek_token (parser
->lexer
);
32414 while (sep
->type
== CPP_COMMA
)
32416 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
32417 identifier
= cp_parser_identifier (parser
);
32418 if (identifier
== error_mark_node
)
32421 list
= chainon (list
, build_tree_list (NULL_TREE
,
32423 sep
= cp_lexer_peek_token (parser
->lexer
);
32429 /* Parse an Objective-C alias declaration.
32431 objc-alias-declaration:
32432 @compatibility_alias identifier identifier ;
32434 This function registers the alias mapping with the Objective-C front end.
32435 It returns nothing. */
32438 cp_parser_objc_alias_declaration (cp_parser
* parser
)
32442 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
32443 alias
= cp_parser_identifier (parser
);
32444 orig
= cp_parser_identifier (parser
);
32445 objc_declare_alias (alias
, orig
);
32446 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32449 /* Parse an Objective-C class forward-declaration.
32451 objc-class-declaration:
32452 @class objc-identifier-list ;
32454 The function registers the forward declarations with the Objective-C
32455 front end. It returns nothing. */
32458 cp_parser_objc_class_declaration (cp_parser
* parser
)
32460 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
32465 id
= cp_parser_identifier (parser
);
32466 if (id
== error_mark_node
)
32469 objc_declare_class (id
);
32471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32472 cp_lexer_consume_token (parser
->lexer
);
32476 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32479 /* Parse a list of Objective-C protocol references.
32481 objc-protocol-refs-opt:
32482 objc-protocol-refs [opt]
32484 objc-protocol-refs:
32485 < objc-identifier-list >
32487 Returns a TREE_LIST of identifiers, if any. */
32490 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
32492 tree protorefs
= NULL_TREE
;
32494 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
32496 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
32497 protorefs
= cp_parser_objc_identifier_list (parser
);
32498 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
32504 /* Parse a Objective-C visibility specification. */
32507 cp_parser_objc_visibility_spec (cp_parser
* parser
)
32509 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
32511 switch (vis
->keyword
)
32513 case RID_AT_PRIVATE
:
32514 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
32516 case RID_AT_PROTECTED
:
32517 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
32519 case RID_AT_PUBLIC
:
32520 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
32522 case RID_AT_PACKAGE
:
32523 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
32529 /* Eat '@private'/'@protected'/'@public'. */
32530 cp_lexer_consume_token (parser
->lexer
);
32533 /* Parse an Objective-C method type. Return 'true' if it is a class
32534 (+) method, and 'false' if it is an instance (-) method. */
32537 cp_parser_objc_method_type (cp_parser
* parser
)
32539 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
32545 /* Parse an Objective-C protocol qualifier. */
32548 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
32550 tree quals
= NULL_TREE
, node
;
32551 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32553 node
= token
->u
.value
;
32555 while (node
&& identifier_p (node
)
32556 && (node
== ridpointers
[(int) RID_IN
]
32557 || node
== ridpointers
[(int) RID_OUT
]
32558 || node
== ridpointers
[(int) RID_INOUT
]
32559 || node
== ridpointers
[(int) RID_BYCOPY
]
32560 || node
== ridpointers
[(int) RID_BYREF
]
32561 || node
== ridpointers
[(int) RID_ONEWAY
]))
32563 quals
= tree_cons (NULL_TREE
, node
, quals
);
32564 cp_lexer_consume_token (parser
->lexer
);
32565 token
= cp_lexer_peek_token (parser
->lexer
);
32566 node
= token
->u
.value
;
32572 /* Parse an Objective-C typename. */
32575 cp_parser_objc_typename (cp_parser
* parser
)
32577 tree type_name
= NULL_TREE
;
32579 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32581 tree proto_quals
, cp_type
= NULL_TREE
;
32583 matching_parens parens
;
32584 parens
.consume_open (parser
); /* Eat '('. */
32585 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
32587 /* An ObjC type name may consist of just protocol qualifiers, in which
32588 case the type shall default to 'id'. */
32589 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
32591 cp_type
= cp_parser_type_id (parser
);
32593 /* If the type could not be parsed, an error has already
32594 been produced. For error recovery, behave as if it had
32595 not been specified, which will use the default type
32597 if (cp_type
== error_mark_node
)
32599 cp_type
= NULL_TREE
;
32600 /* We need to skip to the closing parenthesis as
32601 cp_parser_type_id() does not seem to do it for
32603 cp_parser_skip_to_closing_parenthesis (parser
,
32604 /*recovering=*/true,
32605 /*or_comma=*/false,
32606 /*consume_paren=*/false);
32610 parens
.require_close (parser
);
32611 type_name
= build_tree_list (proto_quals
, cp_type
);
32617 /* Check to see if TYPE refers to an Objective-C selector name. */
32620 cp_parser_objc_selector_p (enum cpp_ttype type
)
32622 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
32623 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
32624 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
32625 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
32626 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
32629 /* Parse an Objective-C selector. */
32632 cp_parser_objc_selector (cp_parser
* parser
)
32634 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
32636 if (!cp_parser_objc_selector_p (token
->type
))
32638 error_at (token
->location
, "invalid Objective-C++ selector name");
32639 return error_mark_node
;
32642 /* C++ operator names are allowed to appear in ObjC selectors. */
32643 switch (token
->type
)
32645 case CPP_AND_AND
: return get_identifier ("and");
32646 case CPP_AND_EQ
: return get_identifier ("and_eq");
32647 case CPP_AND
: return get_identifier ("bitand");
32648 case CPP_OR
: return get_identifier ("bitor");
32649 case CPP_COMPL
: return get_identifier ("compl");
32650 case CPP_NOT
: return get_identifier ("not");
32651 case CPP_NOT_EQ
: return get_identifier ("not_eq");
32652 case CPP_OR_OR
: return get_identifier ("or");
32653 case CPP_OR_EQ
: return get_identifier ("or_eq");
32654 case CPP_XOR
: return get_identifier ("xor");
32655 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
32656 default: return token
->u
.value
;
32660 /* Parse an Objective-C params list. */
32663 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
32665 tree params
= NULL_TREE
;
32666 bool maybe_unary_selector_p
= true;
32667 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32669 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
32671 tree selector
= NULL_TREE
, type_name
, identifier
;
32672 tree parm_attr
= NULL_TREE
;
32674 if (token
->keyword
== RID_ATTRIBUTE
)
32677 if (token
->type
!= CPP_COLON
)
32678 selector
= cp_parser_objc_selector (parser
);
32680 /* Detect if we have a unary selector. */
32681 if (maybe_unary_selector_p
32682 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
32684 params
= selector
; /* Might be followed by attributes. */
32688 maybe_unary_selector_p
= false;
32689 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
32691 /* Something went quite wrong. There should be a colon
32692 here, but there is not. Stop parsing parameters. */
32695 type_name
= cp_parser_objc_typename (parser
);
32696 /* New ObjC allows attributes on parameters too. */
32697 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
32698 parm_attr
= cp_parser_attributes_opt (parser
);
32699 identifier
= cp_parser_identifier (parser
);
32703 objc_build_keyword_decl (selector
,
32708 token
= cp_lexer_peek_token (parser
->lexer
);
32711 if (params
== NULL_TREE
)
32713 cp_parser_error (parser
, "objective-c++ method declaration is expected");
32714 return error_mark_node
;
32717 /* We allow tail attributes for the method. */
32718 if (token
->keyword
== RID_ATTRIBUTE
)
32720 *attributes
= cp_parser_attributes_opt (parser
);
32721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
32722 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
32724 cp_parser_error (parser
,
32725 "method attributes must be specified at the end");
32726 return error_mark_node
;
32729 if (params
== NULL_TREE
)
32731 cp_parser_error (parser
, "objective-c++ method declaration is expected");
32732 return error_mark_node
;
32737 /* Parse the non-keyword Objective-C params. */
32740 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
32743 tree params
= make_node (TREE_LIST
);
32744 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32745 *ellipsisp
= false; /* Initially, assume no ellipsis. */
32747 while (token
->type
== CPP_COMMA
)
32749 cp_parameter_declarator
*parmdecl
;
32752 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
32753 token
= cp_lexer_peek_token (parser
->lexer
);
32755 if (token
->type
== CPP_ELLIPSIS
)
32757 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
32759 token
= cp_lexer_peek_token (parser
->lexer
);
32763 /* TODO: parse attributes for tail parameters. */
32764 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
32766 parm
= grokdeclarator (parmdecl
->declarator
,
32767 &parmdecl
->decl_specifiers
,
32768 PARM
, /*initialized=*/0,
32769 /*attrlist=*/NULL
);
32771 chainon (params
, build_tree_list (NULL_TREE
, parm
));
32772 token
= cp_lexer_peek_token (parser
->lexer
);
32775 /* We allow tail attributes for the method. */
32776 if (token
->keyword
== RID_ATTRIBUTE
)
32778 if (*attributes
== NULL_TREE
)
32780 *attributes
= cp_parser_attributes_opt (parser
);
32781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
32782 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
32786 /* We have an error, but parse the attributes, so that we can
32788 *attributes
= cp_parser_attributes_opt (parser
);
32790 cp_parser_error (parser
,
32791 "method attributes must be specified at the end");
32792 return error_mark_node
;
32798 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
32801 cp_parser_objc_interstitial_code (cp_parser
* parser
)
32803 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32805 /* If the next token is `extern' and the following token is a string
32806 literal, then we have a linkage specification. */
32807 if (token
->keyword
== RID_EXTERN
32808 && cp_parser_is_pure_string_literal
32809 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
32810 cp_parser_linkage_specification (parser
);
32811 /* Handle #pragma, if any. */
32812 else if (token
->type
== CPP_PRAGMA
)
32813 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
32814 /* Allow stray semicolons. */
32815 else if (token
->type
== CPP_SEMICOLON
)
32816 cp_lexer_consume_token (parser
->lexer
);
32817 /* Mark methods as optional or required, when building protocols. */
32818 else if (token
->keyword
== RID_AT_OPTIONAL
)
32820 cp_lexer_consume_token (parser
->lexer
);
32821 objc_set_method_opt (true);
32823 else if (token
->keyword
== RID_AT_REQUIRED
)
32825 cp_lexer_consume_token (parser
->lexer
);
32826 objc_set_method_opt (false);
32828 else if (token
->keyword
== RID_NAMESPACE
)
32829 cp_parser_namespace_definition (parser
);
32830 /* Other stray characters must generate errors. */
32831 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
32833 cp_lexer_consume_token (parser
->lexer
);
32834 error ("stray %qs between Objective-C++ methods",
32835 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
32837 /* Finally, try to parse a block-declaration, or a function-definition. */
32839 cp_parser_block_declaration (parser
, /*statement_p=*/false);
32842 /* Parse a method signature. */
32845 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
32847 tree rettype
, kwdparms
, optparms
;
32848 bool ellipsis
= false;
32849 bool is_class_method
;
32851 is_class_method
= cp_parser_objc_method_type (parser
);
32852 rettype
= cp_parser_objc_typename (parser
);
32853 *attributes
= NULL_TREE
;
32854 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
32855 if (kwdparms
== error_mark_node
)
32856 return error_mark_node
;
32857 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
32858 if (optparms
== error_mark_node
)
32859 return error_mark_node
;
32861 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
32865 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
32868 cp_lexer_save_tokens (parser
->lexer
);
32869 tattr
= cp_parser_attributes_opt (parser
);
32870 gcc_assert (tattr
) ;
32872 /* If the attributes are followed by a method introducer, this is not allowed.
32873 Dump the attributes and flag the situation. */
32874 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
32875 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
32878 /* Otherwise, the attributes introduce some interstitial code, possibly so
32879 rewind to allow that check. */
32880 cp_lexer_rollback_tokens (parser
->lexer
);
32884 /* Parse an Objective-C method prototype list. */
32887 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
32889 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32891 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
32893 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
32895 tree attributes
, sig
;
32896 bool is_class_method
;
32897 if (token
->type
== CPP_PLUS
)
32898 is_class_method
= true;
32900 is_class_method
= false;
32901 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
32902 if (sig
== error_mark_node
)
32904 cp_parser_skip_to_end_of_block_or_statement (parser
);
32905 token
= cp_lexer_peek_token (parser
->lexer
);
32908 objc_add_method_declaration (is_class_method
, sig
, attributes
);
32909 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32911 else if (token
->keyword
== RID_AT_PROPERTY
)
32912 cp_parser_objc_at_property_declaration (parser
);
32913 else if (token
->keyword
== RID_ATTRIBUTE
32914 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
32915 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
32917 "prefix attributes are ignored for methods");
32919 /* Allow for interspersed non-ObjC++ code. */
32920 cp_parser_objc_interstitial_code (parser
);
32922 token
= cp_lexer_peek_token (parser
->lexer
);
32925 if (token
->type
!= CPP_EOF
)
32926 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
32928 cp_parser_error (parser
, "expected %<@end%>");
32930 objc_finish_interface ();
32933 /* Parse an Objective-C method definition list. */
32936 cp_parser_objc_method_definition_list (cp_parser
* parser
)
32938 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32940 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
32944 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
32947 tree sig
, attribute
;
32948 bool is_class_method
;
32949 if (token
->type
== CPP_PLUS
)
32950 is_class_method
= true;
32952 is_class_method
= false;
32953 push_deferring_access_checks (dk_deferred
);
32954 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
32955 if (sig
== error_mark_node
)
32957 cp_parser_skip_to_end_of_block_or_statement (parser
);
32958 token
= cp_lexer_peek_token (parser
->lexer
);
32961 objc_start_method_definition (is_class_method
, sig
, attribute
,
32964 /* For historical reasons, we accept an optional semicolon. */
32965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32966 cp_lexer_consume_token (parser
->lexer
);
32968 ptk
= cp_lexer_peek_token (parser
->lexer
);
32969 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
32970 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
32972 perform_deferred_access_checks (tf_warning_or_error
);
32973 stop_deferring_access_checks ();
32974 meth
= cp_parser_function_definition_after_declarator (parser
,
32976 pop_deferring_access_checks ();
32977 objc_finish_method_definition (meth
);
32980 /* The following case will be removed once @synthesize is
32981 completely implemented. */
32982 else if (token
->keyword
== RID_AT_PROPERTY
)
32983 cp_parser_objc_at_property_declaration (parser
);
32984 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
32985 cp_parser_objc_at_synthesize_declaration (parser
);
32986 else if (token
->keyword
== RID_AT_DYNAMIC
)
32987 cp_parser_objc_at_dynamic_declaration (parser
);
32988 else if (token
->keyword
== RID_ATTRIBUTE
32989 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
32990 warning_at (token
->location
, OPT_Wattributes
,
32991 "prefix attributes are ignored for methods");
32993 /* Allow for interspersed non-ObjC++ code. */
32994 cp_parser_objc_interstitial_code (parser
);
32996 token
= cp_lexer_peek_token (parser
->lexer
);
32999 if (token
->type
!= CPP_EOF
)
33000 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33002 cp_parser_error (parser
, "expected %<@end%>");
33004 objc_finish_implementation ();
33007 /* Parse Objective-C ivars. */
33010 cp_parser_objc_class_ivars (cp_parser
* parser
)
33012 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33014 if (token
->type
!= CPP_OPEN_BRACE
)
33015 return; /* No ivars specified. */
33017 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
33018 token
= cp_lexer_peek_token (parser
->lexer
);
33020 while (token
->type
!= CPP_CLOSE_BRACE
33021 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33023 cp_decl_specifier_seq declspecs
;
33024 int decl_class_or_enum_p
;
33025 tree prefix_attributes
;
33027 cp_parser_objc_visibility_spec (parser
);
33029 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33032 cp_parser_decl_specifier_seq (parser
,
33033 CP_PARSER_FLAGS_OPTIONAL
,
33035 &decl_class_or_enum_p
);
33037 /* auto, register, static, extern, mutable. */
33038 if (declspecs
.storage_class
!= sc_none
)
33040 cp_parser_error (parser
, "invalid type for instance variable");
33041 declspecs
.storage_class
= sc_none
;
33044 /* thread_local. */
33045 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
33047 cp_parser_error (parser
, "invalid type for instance variable");
33048 declspecs
.locations
[ds_thread
] = 0;
33052 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
33054 cp_parser_error (parser
, "invalid type for instance variable");
33055 declspecs
.locations
[ds_typedef
] = 0;
33058 prefix_attributes
= declspecs
.attributes
;
33059 declspecs
.attributes
= NULL_TREE
;
33061 /* Keep going until we hit the `;' at the end of the
33063 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33065 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
33066 cp_declarator
*declarator
= NULL
;
33067 int ctor_dtor_or_conv_p
;
33069 /* Check for a (possibly unnamed) bitfield declaration. */
33070 token
= cp_lexer_peek_token (parser
->lexer
);
33071 if (token
->type
== CPP_COLON
)
33074 if (token
->type
== CPP_NAME
33075 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
33078 /* Get the name of the bitfield. */
33079 declarator
= make_id_declarator (NULL_TREE
,
33080 cp_parser_identifier (parser
),
33081 sfk_none
, token
->location
);
33084 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
33085 /* Get the width of the bitfield. */
33087 = cp_parser_constant_expression (parser
);
33091 /* Parse the declarator. */
33093 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
33094 CP_PARSER_FLAGS_NONE
,
33095 &ctor_dtor_or_conv_p
,
33096 /*parenthesized_p=*/NULL
,
33097 /*member_p=*/false,
33098 /*friend_p=*/false,
33099 /*static_p=*/false);
33102 /* Look for attributes that apply to the ivar. */
33103 attributes
= cp_parser_attributes_opt (parser
);
33104 /* Remember which attributes are prefix attributes and
33106 first_attribute
= attributes
;
33107 /* Combine the attributes. */
33108 attributes
= attr_chainon (prefix_attributes
, attributes
);
33111 /* Create the bitfield declaration. */
33112 decl
= grokbitfield (declarator
, &declspecs
,
33113 width
, NULL_TREE
, attributes
);
33115 decl
= grokfield (declarator
, &declspecs
,
33116 NULL_TREE
, /*init_const_expr_p=*/false,
33117 NULL_TREE
, attributes
);
33119 /* Add the instance variable. */
33120 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
33121 objc_add_instance_variable (decl
);
33123 /* Reset PREFIX_ATTRIBUTES. */
33124 if (attributes
!= error_mark_node
)
33126 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
33127 attributes
= TREE_CHAIN (attributes
);
33129 TREE_CHAIN (attributes
) = NULL_TREE
;
33132 token
= cp_lexer_peek_token (parser
->lexer
);
33134 if (token
->type
== CPP_COMMA
)
33136 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33142 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33143 token
= cp_lexer_peek_token (parser
->lexer
);
33146 if (token
->keyword
== RID_AT_END
)
33147 cp_parser_error (parser
, "expected %<}%>");
33149 /* Do not consume the RID_AT_END, so it will be read again as terminating
33150 the @interface of @implementation. */
33151 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33152 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
33154 /* For historical reasons, we accept an optional semicolon. */
33155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33156 cp_lexer_consume_token (parser
->lexer
);
33159 /* Parse an Objective-C protocol declaration. */
33162 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
33164 tree proto
, protorefs
;
33167 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
33168 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
33170 tok
= cp_lexer_peek_token (parser
->lexer
);
33171 error_at (tok
->location
, "identifier expected after %<@protocol%>");
33172 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33176 /* See if we have a forward declaration or a definition. */
33177 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
33179 /* Try a forward declaration first. */
33180 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
33186 id
= cp_parser_identifier (parser
);
33187 if (id
== error_mark_node
)
33190 objc_declare_protocol (id
, attributes
);
33192 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33193 cp_lexer_consume_token (parser
->lexer
);
33197 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33200 /* Ok, we got a full-fledged definition (or at least should). */
33203 proto
= cp_parser_identifier (parser
);
33204 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
33205 objc_start_protocol (proto
, protorefs
, attributes
);
33206 cp_parser_objc_method_prototype_list (parser
);
33210 /* Parse an Objective-C superclass or category. */
33213 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
33216 tree
*categ
, bool *is_class_extension
)
33218 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
33220 *super
= *categ
= NULL_TREE
;
33221 *is_class_extension
= false;
33222 if (next
->type
== CPP_COLON
)
33224 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
33225 *super
= cp_parser_identifier (parser
);
33227 else if (next
->type
== CPP_OPEN_PAREN
)
33229 matching_parens parens
;
33230 parens
.consume_open (parser
); /* Eat '('. */
33232 /* If there is no category name, and this is an @interface, we
33233 have a class extension. */
33234 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33236 *categ
= NULL_TREE
;
33237 *is_class_extension
= true;
33240 *categ
= cp_parser_identifier (parser
);
33242 parens
.require_close (parser
);
33246 /* Parse an Objective-C class interface. */
33249 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
33251 tree name
, super
, categ
, protos
;
33252 bool is_class_extension
;
33254 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
33255 name
= cp_parser_identifier (parser
);
33256 if (name
== error_mark_node
)
33258 /* It's hard to recover because even if valid @interface stuff
33259 is to follow, we can't compile it (or validate it) if we
33260 don't even know which class it refers to. Let's assume this
33261 was a stray '@interface' token in the stream and skip it.
33265 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
33266 &is_class_extension
);
33267 protos
= cp_parser_objc_protocol_refs_opt (parser
);
33269 /* We have either a class or a category on our hands. */
33270 if (categ
|| is_class_extension
)
33271 objc_start_category_interface (name
, categ
, protos
, attributes
);
33274 objc_start_class_interface (name
, super
, protos
, attributes
);
33275 /* Handle instance variable declarations, if any. */
33276 cp_parser_objc_class_ivars (parser
);
33277 objc_continue_interface ();
33280 cp_parser_objc_method_prototype_list (parser
);
33283 /* Parse an Objective-C class implementation. */
33286 cp_parser_objc_class_implementation (cp_parser
* parser
)
33288 tree name
, super
, categ
;
33289 bool is_class_extension
;
33291 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
33292 name
= cp_parser_identifier (parser
);
33293 if (name
== error_mark_node
)
33295 /* It's hard to recover because even if valid @implementation
33296 stuff is to follow, we can't compile it (or validate it) if
33297 we don't even know which class it refers to. Let's assume
33298 this was a stray '@implementation' token in the stream and
33303 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
33304 &is_class_extension
);
33306 /* We have either a class or a category on our hands. */
33308 objc_start_category_implementation (name
, categ
);
33311 objc_start_class_implementation (name
, super
);
33312 /* Handle instance variable declarations, if any. */
33313 cp_parser_objc_class_ivars (parser
);
33314 objc_continue_implementation ();
33317 cp_parser_objc_method_definition_list (parser
);
33320 /* Consume the @end token and finish off the implementation. */
33323 cp_parser_objc_end_implementation (cp_parser
* parser
)
33325 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33326 objc_finish_implementation ();
33329 /* Parse an Objective-C declaration. */
33332 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
33334 /* Try to figure out what kind of declaration is present. */
33335 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
33338 switch (kwd
->keyword
)
33343 error_at (kwd
->location
, "attributes may not be specified before"
33344 " the %<@%D%> Objective-C++ keyword",
33348 case RID_AT_IMPLEMENTATION
:
33349 warning_at (kwd
->location
, OPT_Wattributes
,
33350 "prefix attributes are ignored before %<@%D%>",
33357 switch (kwd
->keyword
)
33360 cp_parser_objc_alias_declaration (parser
);
33363 cp_parser_objc_class_declaration (parser
);
33365 case RID_AT_PROTOCOL
:
33366 cp_parser_objc_protocol_declaration (parser
, attributes
);
33368 case RID_AT_INTERFACE
:
33369 cp_parser_objc_class_interface (parser
, attributes
);
33371 case RID_AT_IMPLEMENTATION
:
33372 cp_parser_objc_class_implementation (parser
);
33375 cp_parser_objc_end_implementation (parser
);
33378 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
33380 cp_parser_skip_to_end_of_block_or_statement (parser
);
33384 /* Parse an Objective-C try-catch-finally statement.
33386 objc-try-catch-finally-stmt:
33387 @try compound-statement objc-catch-clause-seq [opt]
33388 objc-finally-clause [opt]
33390 objc-catch-clause-seq:
33391 objc-catch-clause objc-catch-clause-seq [opt]
33394 @catch ( objc-exception-declaration ) compound-statement
33396 objc-finally-clause:
33397 @finally compound-statement
33399 objc-exception-declaration:
33400 parameter-declaration
33403 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
33407 PS: This function is identical to c_parser_objc_try_catch_finally_statement
33408 for C. Keep them in sync. */
33411 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
33413 location_t location
;
33416 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
33417 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33418 objc_maybe_warn_exceptions (location
);
33419 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
33420 node, lest it get absorbed into the surrounding block. */
33421 stmt
= push_stmt_list ();
33422 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33423 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
33425 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
33427 cp_parameter_declarator
*parm
;
33428 tree parameter_declaration
= error_mark_node
;
33429 bool seen_open_paren
= false;
33430 matching_parens parens
;
33432 cp_lexer_consume_token (parser
->lexer
);
33433 if (parens
.require_open (parser
))
33434 seen_open_paren
= true;
33435 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
33437 /* We have "@catch (...)" (where the '...' are literally
33438 what is in the code). Skip the '...'.
33439 parameter_declaration is set to NULL_TREE, and
33440 objc_being_catch_clauses() knows that that means
33442 cp_lexer_consume_token (parser
->lexer
);
33443 parameter_declaration
= NULL_TREE
;
33447 /* We have "@catch (NSException *exception)" or something
33448 like that. Parse the parameter declaration. */
33449 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
33452 parameter_declaration
= error_mark_node
;
33454 parameter_declaration
= grokdeclarator (parm
->declarator
,
33455 &parm
->decl_specifiers
,
33456 PARM
, /*initialized=*/0,
33457 /*attrlist=*/NULL
);
33459 if (seen_open_paren
)
33460 parens
.require_close (parser
);
33463 /* If there was no open parenthesis, we are recovering from
33464 an error, and we are trying to figure out what mistake
33465 the user has made. */
33467 /* If there is an immediate closing parenthesis, the user
33468 probably forgot the opening one (ie, they typed "@catch
33469 NSException *e)". Parse the closing parenthesis and keep
33471 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
33472 cp_lexer_consume_token (parser
->lexer
);
33474 /* If these is no immediate closing parenthesis, the user
33475 probably doesn't know that parenthesis are required at
33476 all (ie, they typed "@catch NSException *e"). So, just
33477 forget about the closing parenthesis and keep going. */
33479 objc_begin_catch_clause (parameter_declaration
);
33480 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33481 objc_finish_catch_clause ();
33483 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
33485 cp_lexer_consume_token (parser
->lexer
);
33486 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33487 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
33488 node, lest it get absorbed into the surrounding block. */
33489 stmt
= push_stmt_list ();
33490 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33491 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
33494 return objc_finish_try_stmt ();
33497 /* Parse an Objective-C synchronized statement.
33499 objc-synchronized-stmt:
33500 @synchronized ( expression ) compound-statement
33502 Returns NULL_TREE. */
33505 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
33507 location_t location
;
33510 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
33512 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33513 objc_maybe_warn_exceptions (location
);
33514 matching_parens parens
;
33515 parens
.require_open (parser
);
33516 lock
= cp_parser_expression (parser
);
33517 parens
.require_close (parser
);
33519 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
33520 node, lest it get absorbed into the surrounding block. */
33521 stmt
= push_stmt_list ();
33522 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
33524 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
33527 /* Parse an Objective-C throw statement.
33530 @throw assignment-expression [opt] ;
33532 Returns a constructed '@throw' statement. */
33535 cp_parser_objc_throw_statement (cp_parser
*parser
)
33537 tree expr
= NULL_TREE
;
33538 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33540 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
33542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33543 expr
= cp_parser_expression (parser
);
33545 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33547 return objc_build_throw_stmt (loc
, expr
);
33550 /* Parse an Objective-C statement. */
33553 cp_parser_objc_statement (cp_parser
* parser
)
33555 /* Try to figure out what kind of declaration is present. */
33556 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
33558 switch (kwd
->keyword
)
33561 return cp_parser_objc_try_catch_finally_statement (parser
);
33562 case RID_AT_SYNCHRONIZED
:
33563 return cp_parser_objc_synchronized_statement (parser
);
33565 return cp_parser_objc_throw_statement (parser
);
33567 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
33569 cp_parser_skip_to_end_of_block_or_statement (parser
);
33572 return error_mark_node
;
33575 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
33576 look ahead to see if an objc keyword follows the attributes. This
33577 is to detect the use of prefix attributes on ObjC @interface and
33581 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
33583 cp_lexer_save_tokens (parser
->lexer
);
33584 *attrib
= cp_parser_attributes_opt (parser
);
33585 gcc_assert (*attrib
);
33586 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
33588 cp_lexer_commit_tokens (parser
->lexer
);
33591 cp_lexer_rollback_tokens (parser
->lexer
);
33595 /* This routine is a minimal replacement for
33596 c_parser_struct_declaration () used when parsing the list of
33597 types/names or ObjC++ properties. For example, when parsing the
33600 @property (readonly) int a, b, c;
33602 this function is responsible for parsing "int a, int b, int c" and
33603 returning the declarations as CHAIN of DECLs.
33605 TODO: Share this code with cp_parser_objc_class_ivars. It's very
33606 similar parsing. */
33608 cp_parser_objc_struct_declaration (cp_parser
*parser
)
33610 tree decls
= NULL_TREE
;
33611 cp_decl_specifier_seq declspecs
;
33612 int decl_class_or_enum_p
;
33613 tree prefix_attributes
;
33615 cp_parser_decl_specifier_seq (parser
,
33616 CP_PARSER_FLAGS_NONE
,
33618 &decl_class_or_enum_p
);
33620 if (declspecs
.type
== error_mark_node
)
33621 return error_mark_node
;
33623 /* auto, register, static, extern, mutable. */
33624 if (declspecs
.storage_class
!= sc_none
)
33626 cp_parser_error (parser
, "invalid type for property");
33627 declspecs
.storage_class
= sc_none
;
33630 /* thread_local. */
33631 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
33633 cp_parser_error (parser
, "invalid type for property");
33634 declspecs
.locations
[ds_thread
] = 0;
33638 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
33640 cp_parser_error (parser
, "invalid type for property");
33641 declspecs
.locations
[ds_typedef
] = 0;
33644 prefix_attributes
= declspecs
.attributes
;
33645 declspecs
.attributes
= NULL_TREE
;
33647 /* Keep going until we hit the `;' at the end of the declaration. */
33648 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33650 tree attributes
, first_attribute
, decl
;
33651 cp_declarator
*declarator
;
33654 /* Parse the declarator. */
33655 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
33656 CP_PARSER_FLAGS_NONE
,
33657 NULL
, NULL
, false, false, false);
33659 /* Look for attributes that apply to the ivar. */
33660 attributes
= cp_parser_attributes_opt (parser
);
33661 /* Remember which attributes are prefix attributes and
33663 first_attribute
= attributes
;
33664 /* Combine the attributes. */
33665 attributes
= attr_chainon (prefix_attributes
, attributes
);
33667 decl
= grokfield (declarator
, &declspecs
,
33668 NULL_TREE
, /*init_const_expr_p=*/false,
33669 NULL_TREE
, attributes
);
33671 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
33672 return error_mark_node
;
33674 /* Reset PREFIX_ATTRIBUTES. */
33675 if (attributes
!= error_mark_node
)
33677 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
33678 attributes
= TREE_CHAIN (attributes
);
33680 TREE_CHAIN (attributes
) = NULL_TREE
;
33683 DECL_CHAIN (decl
) = decls
;
33686 token
= cp_lexer_peek_token (parser
->lexer
);
33687 if (token
->type
== CPP_COMMA
)
33689 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33698 /* Parse an Objective-C @property declaration. The syntax is:
33700 objc-property-declaration:
33701 '@property' objc-property-attributes[opt] struct-declaration ;
33703 objc-property-attributes:
33704 '(' objc-property-attribute-list ')'
33706 objc-property-attribute-list:
33707 objc-property-attribute
33708 objc-property-attribute-list, objc-property-attribute
33710 objc-property-attribute
33711 'getter' = identifier
33712 'setter' = identifier
33721 @property NSString *name;
33722 @property (readonly) id object;
33723 @property (retain, nonatomic, getter=getTheName) id name;
33724 @property int a, b, c;
33726 PS: This function is identical to
33727 c_parser_objc_at_property_declaration for C. Keep them in sync. */
33729 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
33731 /* The following variables hold the attributes of the properties as
33732 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
33733 seen. When we see an attribute, we set them to 'true' (if they
33734 are boolean properties) or to the identifier (if they have an
33735 argument, ie, for getter and setter). Note that here we only
33736 parse the list of attributes, check the syntax and accumulate the
33737 attributes that we find. objc_add_property_declaration() will
33738 then process the information. */
33739 bool property_assign
= false;
33740 bool property_copy
= false;
33741 tree property_getter_ident
= NULL_TREE
;
33742 bool property_nonatomic
= false;
33743 bool property_readonly
= false;
33744 bool property_readwrite
= false;
33745 bool property_retain
= false;
33746 tree property_setter_ident
= NULL_TREE
;
33748 /* 'properties' is the list of properties that we read. Usually a
33749 single one, but maybe more (eg, in "@property int a, b, c;" there
33754 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33756 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
33758 /* Parse the optional attribute list... */
33759 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33762 matching_parens parens
;
33763 parens
.consume_open (parser
);
33767 bool syntax_error
= false;
33768 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33771 if (token
->type
!= CPP_NAME
)
33773 cp_parser_error (parser
, "expected identifier");
33776 keyword
= C_RID_CODE (token
->u
.value
);
33777 cp_lexer_consume_token (parser
->lexer
);
33780 case RID_ASSIGN
: property_assign
= true; break;
33781 case RID_COPY
: property_copy
= true; break;
33782 case RID_NONATOMIC
: property_nonatomic
= true; break;
33783 case RID_READONLY
: property_readonly
= true; break;
33784 case RID_READWRITE
: property_readwrite
= true; break;
33785 case RID_RETAIN
: property_retain
= true; break;
33789 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
33791 if (keyword
== RID_GETTER
)
33792 cp_parser_error (parser
,
33793 "missing %<=%> (after %<getter%> attribute)");
33795 cp_parser_error (parser
,
33796 "missing %<=%> (after %<setter%> attribute)");
33797 syntax_error
= true;
33800 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
33801 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
33803 cp_parser_error (parser
, "expected identifier");
33804 syntax_error
= true;
33807 if (keyword
== RID_SETTER
)
33809 if (property_setter_ident
!= NULL_TREE
)
33811 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
33812 cp_lexer_consume_token (parser
->lexer
);
33815 property_setter_ident
= cp_parser_objc_selector (parser
);
33816 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
33817 cp_parser_error (parser
, "setter name must terminate with %<:%>");
33819 cp_lexer_consume_token (parser
->lexer
);
33823 if (property_getter_ident
!= NULL_TREE
)
33825 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
33826 cp_lexer_consume_token (parser
->lexer
);
33829 property_getter_ident
= cp_parser_objc_selector (parser
);
33833 cp_parser_error (parser
, "unknown property attribute");
33834 syntax_error
= true;
33841 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33842 cp_lexer_consume_token (parser
->lexer
);
33847 /* FIXME: "@property (setter, assign);" will generate a spurious
33848 "error: expected ‘)’ before ‘,’ token". This is because
33849 cp_parser_require, unlike the C counterpart, will produce an
33850 error even if we are in error recovery. */
33851 if (!parens
.require_close (parser
))
33853 cp_parser_skip_to_closing_parenthesis (parser
,
33854 /*recovering=*/true,
33855 /*or_comma=*/false,
33856 /*consume_paren=*/true);
33860 /* ... and the property declaration(s). */
33861 properties
= cp_parser_objc_struct_declaration (parser
);
33863 if (properties
== error_mark_node
)
33865 cp_parser_skip_to_end_of_statement (parser
);
33866 /* If the next token is now a `;', consume it. */
33867 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33868 cp_lexer_consume_token (parser
->lexer
);
33872 if (properties
== NULL_TREE
)
33873 cp_parser_error (parser
, "expected identifier");
33876 /* Comma-separated properties are chained together in
33877 reverse order; add them one by one. */
33878 properties
= nreverse (properties
);
33880 for (; properties
; properties
= TREE_CHAIN (properties
))
33881 objc_add_property_declaration (loc
, copy_node (properties
),
33882 property_readonly
, property_readwrite
,
33883 property_assign
, property_retain
,
33884 property_copy
, property_nonatomic
,
33885 property_getter_ident
, property_setter_ident
);
33888 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33891 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
33893 objc-synthesize-declaration:
33894 @synthesize objc-synthesize-identifier-list ;
33896 objc-synthesize-identifier-list:
33897 objc-synthesize-identifier
33898 objc-synthesize-identifier-list, objc-synthesize-identifier
33900 objc-synthesize-identifier
33902 identifier = identifier
33905 @synthesize MyProperty;
33906 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
33908 PS: This function is identical to c_parser_objc_at_synthesize_declaration
33909 for C. Keep them in sync.
33912 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
33914 tree list
= NULL_TREE
;
33916 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33918 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
33921 tree property
, ivar
;
33922 property
= cp_parser_identifier (parser
);
33923 if (property
== error_mark_node
)
33925 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33928 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
33930 cp_lexer_consume_token (parser
->lexer
);
33931 ivar
= cp_parser_identifier (parser
);
33932 if (ivar
== error_mark_node
)
33934 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33940 list
= chainon (list
, build_tree_list (ivar
, property
));
33941 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33942 cp_lexer_consume_token (parser
->lexer
);
33946 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33947 objc_add_synthesize_declaration (loc
, list
);
33950 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
33952 objc-dynamic-declaration:
33953 @dynamic identifier-list ;
33956 @dynamic MyProperty;
33957 @dynamic MyProperty, AnotherProperty;
33959 PS: This function is identical to c_parser_objc_at_dynamic_declaration
33960 for C. Keep them in sync.
33963 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
33965 tree list
= NULL_TREE
;
33967 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33969 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
33973 property
= cp_parser_identifier (parser
);
33974 if (property
== error_mark_node
)
33976 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33979 list
= chainon (list
, build_tree_list (NULL
, property
));
33980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33981 cp_lexer_consume_token (parser
->lexer
);
33985 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33986 objc_add_dynamic_declaration (loc
, list
);
33990 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
33992 /* Returns name of the next clause.
33993 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
33994 the token is not consumed. Otherwise appropriate pragma_omp_clause is
33995 returned and the token is consumed. */
33997 static pragma_omp_clause
33998 cp_parser_omp_clause_name (cp_parser
*parser
)
34000 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
34002 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
34003 result
= PRAGMA_OACC_CLAUSE_AUTO
;
34004 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
34005 result
= PRAGMA_OMP_CLAUSE_IF
;
34006 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
34007 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
34008 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
34009 result
= PRAGMA_OACC_CLAUSE_DELETE
;
34010 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
34011 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
34012 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34013 result
= PRAGMA_OMP_CLAUSE_FOR
;
34014 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34016 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34017 const char *p
= IDENTIFIER_POINTER (id
);
34022 if (!strcmp ("aligned", p
))
34023 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
34024 else if (!strcmp ("async", p
))
34025 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
34026 else if (!strcmp ("attach", p
))
34027 result
= PRAGMA_OACC_CLAUSE_ATTACH
;
34030 if (!strcmp ("bind", p
))
34031 result
= PRAGMA_OMP_CLAUSE_BIND
;
34034 if (!strcmp ("collapse", p
))
34035 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
34036 else if (!strcmp ("copy", p
))
34037 result
= PRAGMA_OACC_CLAUSE_COPY
;
34038 else if (!strcmp ("copyin", p
))
34039 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
34040 else if (!strcmp ("copyout", p
))
34041 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
34042 else if (!strcmp ("copyprivate", p
))
34043 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
34044 else if (!strcmp ("create", p
))
34045 result
= PRAGMA_OACC_CLAUSE_CREATE
;
34048 if (!strcmp ("defaultmap", p
))
34049 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
34050 else if (!strcmp ("depend", p
))
34051 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
34052 else if (!strcmp ("detach", p
))
34053 result
= PRAGMA_OACC_CLAUSE_DETACH
;
34054 else if (!strcmp ("device", p
))
34055 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
34056 else if (!strcmp ("deviceptr", p
))
34057 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
34058 else if (!strcmp ("device_resident", p
))
34059 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
34060 else if (!strcmp ("device_type", p
))
34061 result
= PRAGMA_OMP_CLAUSE_DEVICE_TYPE
;
34062 else if (!strcmp ("dist_schedule", p
))
34063 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
34066 if (!strcmp ("final", p
))
34067 result
= PRAGMA_OMP_CLAUSE_FINAL
;
34068 else if (!strcmp ("finalize", p
))
34069 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
34070 else if (!strcmp ("firstprivate", p
))
34071 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
34072 else if (!strcmp ("from", p
))
34073 result
= PRAGMA_OMP_CLAUSE_FROM
;
34076 if (!strcmp ("gang", p
))
34077 result
= PRAGMA_OACC_CLAUSE_GANG
;
34078 else if (!strcmp ("grainsize", p
))
34079 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
34082 if (!strcmp ("hint", p
))
34083 result
= PRAGMA_OMP_CLAUSE_HINT
;
34084 else if (!strcmp ("host", p
))
34085 result
= PRAGMA_OACC_CLAUSE_HOST
;
34088 if (!strcmp ("if_present", p
))
34089 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
34090 else if (!strcmp ("in_reduction", p
))
34091 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
34092 else if (!strcmp ("inbranch", p
))
34093 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
34094 else if (!strcmp ("independent", p
))
34095 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
34096 else if (!strcmp ("is_device_ptr", p
))
34097 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
34100 if (!strcmp ("lastprivate", p
))
34101 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
34102 else if (!strcmp ("linear", p
))
34103 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
34104 else if (!strcmp ("link", p
))
34105 result
= PRAGMA_OMP_CLAUSE_LINK
;
34108 if (!strcmp ("map", p
))
34109 result
= PRAGMA_OMP_CLAUSE_MAP
;
34110 else if (!strcmp ("mergeable", p
))
34111 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
34114 if (!strcmp ("no_create", p
))
34115 result
= PRAGMA_OACC_CLAUSE_NO_CREATE
;
34116 else if (!strcmp ("nogroup", p
))
34117 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
34118 else if (!strcmp ("nontemporal", p
))
34119 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
34120 else if (!strcmp ("notinbranch", p
))
34121 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
34122 else if (!strcmp ("nowait", p
))
34123 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
34124 else if (!strcmp ("num_gangs", p
))
34125 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
34126 else if (!strcmp ("num_tasks", p
))
34127 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
34128 else if (!strcmp ("num_teams", p
))
34129 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
34130 else if (!strcmp ("num_threads", p
))
34131 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
34132 else if (!strcmp ("num_workers", p
))
34133 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
34136 if (!strcmp ("ordered", p
))
34137 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
34138 else if (!strcmp ("order", p
))
34139 result
= PRAGMA_OMP_CLAUSE_ORDER
;
34142 if (!strcmp ("parallel", p
))
34143 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
34144 else if (!strcmp ("present", p
))
34145 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
34146 else if (!strcmp ("present_or_copy", p
)
34147 || !strcmp ("pcopy", p
))
34148 result
= PRAGMA_OACC_CLAUSE_COPY
;
34149 else if (!strcmp ("present_or_copyin", p
)
34150 || !strcmp ("pcopyin", p
))
34151 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
34152 else if (!strcmp ("present_or_copyout", p
)
34153 || !strcmp ("pcopyout", p
))
34154 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
34155 else if (!strcmp ("present_or_create", p
)
34156 || !strcmp ("pcreate", p
))
34157 result
= PRAGMA_OACC_CLAUSE_CREATE
;
34158 else if (!strcmp ("priority", p
))
34159 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
34160 else if (!strcmp ("proc_bind", p
))
34161 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
34164 if (!strcmp ("reduction", p
))
34165 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
34168 if (!strcmp ("safelen", p
))
34169 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
34170 else if (!strcmp ("schedule", p
))
34171 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
34172 else if (!strcmp ("sections", p
))
34173 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
34174 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
34175 result
= PRAGMA_OACC_CLAUSE_HOST
;
34176 else if (!strcmp ("seq", p
))
34177 result
= PRAGMA_OACC_CLAUSE_SEQ
;
34178 else if (!strcmp ("shared", p
))
34179 result
= PRAGMA_OMP_CLAUSE_SHARED
;
34180 else if (!strcmp ("simd", p
))
34181 result
= PRAGMA_OMP_CLAUSE_SIMD
;
34182 else if (!strcmp ("simdlen", p
))
34183 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
34186 if (!strcmp ("task_reduction", p
))
34187 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
34188 else if (!strcmp ("taskgroup", p
))
34189 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
34190 else if (!strcmp ("thread_limit", p
))
34191 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
34192 else if (!strcmp ("threads", p
))
34193 result
= PRAGMA_OMP_CLAUSE_THREADS
;
34194 else if (!strcmp ("tile", p
))
34195 result
= PRAGMA_OACC_CLAUSE_TILE
;
34196 else if (!strcmp ("to", p
))
34197 result
= PRAGMA_OMP_CLAUSE_TO
;
34200 if (!strcmp ("uniform", p
))
34201 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
34202 else if (!strcmp ("untied", p
))
34203 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
34204 else if (!strcmp ("use_device", p
))
34205 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
34206 else if (!strcmp ("use_device_addr", p
))
34207 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
;
34208 else if (!strcmp ("use_device_ptr", p
))
34209 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
34212 if (!strcmp ("vector", p
))
34213 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
34214 else if (!strcmp ("vector_length", p
))
34215 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
34218 if (!strcmp ("wait", p
))
34219 result
= PRAGMA_OACC_CLAUSE_WAIT
;
34220 else if (!strcmp ("worker", p
))
34221 result
= PRAGMA_OACC_CLAUSE_WORKER
;
34226 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
34227 cp_lexer_consume_token (parser
->lexer
);
34232 /* Validate that a clause of the given type does not already exist. */
34235 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
34236 const char *name
, location_t location
)
34238 if (omp_find_clause (clauses
, code
))
34239 error_at (location
, "too many %qs clauses", name
);
34245 variable-list , identifier
34247 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
34248 colon). An opening parenthesis will have been consumed by the caller.
34250 If KIND is nonzero, create the appropriate node and install the decl
34251 in OMP_CLAUSE_DECL and add the node to the head of the list.
34253 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
34254 return the list created.
34256 COLON can be NULL if only closing parenthesis should end the list,
34257 or pointer to bool which will receive false if the list is terminated
34258 by closing parenthesis or true if the list is terminated by colon.
34260 The optional ALLOW_DEREF argument is true if list items can use the deref
34264 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
34265 tree list
, bool *colon
,
34266 bool allow_deref
= false)
34269 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
34272 parser
->colon_corrects_to_scope_p
= false;
34279 if (kind
== OMP_CLAUSE_DEPEND
)
34280 cp_parser_parse_tentatively (parser
);
34281 token
= cp_lexer_peek_token (parser
->lexer
);
34283 && current_class_ptr
34284 && cp_parser_is_keyword (token
, RID_THIS
))
34286 decl
= finish_this_expr ();
34287 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
34288 || CONVERT_EXPR_P (decl
))
34289 decl
= TREE_OPERAND (decl
, 0);
34290 cp_lexer_consume_token (parser
->lexer
);
34292 else if (cp_parser_is_keyword (token
, RID_FUNCTION_NAME
)
34293 || cp_parser_is_keyword (token
, RID_PRETTY_FUNCTION_NAME
)
34294 || cp_parser_is_keyword (token
, RID_C99_FUNCTION_NAME
))
34297 decl
= cp_parser_primary_expression (parser
, false, false, false,
34302 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
34303 /*check_dependency_p=*/true,
34304 /*template_p=*/NULL
,
34305 /*declarator_p=*/false,
34306 /*optional_p=*/false);
34307 if (name
== error_mark_node
)
34309 if (kind
== OMP_CLAUSE_DEPEND
34310 && cp_parser_simulate_error (parser
))
34311 goto depend_lvalue
;
34315 if (identifier_p (name
))
34316 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
34319 if (decl
== error_mark_node
)
34321 if (kind
== OMP_CLAUSE_DEPEND
34322 && cp_parser_simulate_error (parser
))
34323 goto depend_lvalue
;
34324 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
34328 if (outer_automatic_var_p (decl
))
34329 decl
= process_outer_var_ref (decl
, tf_warning_or_error
);
34330 if (decl
== error_mark_node
)
34332 else if (kind
!= 0)
34336 case OMP_CLAUSE__CACHE_
:
34337 /* The OpenACC cache directive explicitly only allows "array
34338 elements or subarrays". */
34339 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
34341 error_at (token
->location
, "expected %<[%>");
34342 decl
= error_mark_node
;
34346 case OMP_CLAUSE_MAP
:
34347 case OMP_CLAUSE_FROM
:
34348 case OMP_CLAUSE_TO
:
34349 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
34351 && cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
)))
34354 = cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
34355 ? CPP_DOT
: CPP_DEREF
;
34357 = cp_lexer_peek_token (parser
->lexer
)->location
;
34358 cp_id_kind idk
= CP_ID_KIND_NONE
;
34359 cp_lexer_consume_token (parser
->lexer
);
34360 decl
= convert_from_reference (decl
);
34362 = cp_parser_postfix_dot_deref_expression (parser
, ttype
,
34367 case OMP_CLAUSE_DEPEND
:
34368 case OMP_CLAUSE_REDUCTION
:
34369 case OMP_CLAUSE_IN_REDUCTION
:
34370 case OMP_CLAUSE_TASK_REDUCTION
:
34371 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
34373 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
34375 parser
->colon_corrects_to_scope_p
= false;
34376 cp_lexer_consume_token (parser
->lexer
);
34377 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34378 low_bound
= cp_parser_expression (parser
);
34380 parser
->colon_corrects_to_scope_p
34381 = saved_colon_corrects_to_scope_p
;
34382 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
34383 length
= integer_one_node
;
34386 /* Look for `:'. */
34387 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34389 if (kind
== OMP_CLAUSE_DEPEND
34390 && cp_parser_simulate_error (parser
))
34391 goto depend_lvalue
;
34394 if (kind
== OMP_CLAUSE_DEPEND
)
34395 cp_parser_commit_to_tentative_parse (parser
);
34396 if (!cp_lexer_next_token_is (parser
->lexer
,
34398 length
= cp_parser_expression (parser
);
34400 /* Look for the closing `]'. */
34401 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
34404 if (kind
== OMP_CLAUSE_DEPEND
34405 && cp_parser_simulate_error (parser
))
34406 goto depend_lvalue
;
34410 decl
= tree_cons (low_bound
, length
, decl
);
34417 if (kind
== OMP_CLAUSE_DEPEND
)
34419 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
34420 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
34421 && cp_parser_simulate_error (parser
))
34424 cp_parser_abort_tentative_parse (parser
);
34425 decl
= cp_parser_assignment_expression (parser
, NULL
,
34429 cp_parser_parse_definitely (parser
);
34432 tree u
= build_omp_clause (token
->location
, kind
);
34433 OMP_CLAUSE_DECL (u
) = decl
;
34434 OMP_CLAUSE_CHAIN (u
) = list
;
34438 list
= tree_cons (decl
, NULL_TREE
, list
);
34441 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
34443 cp_lexer_consume_token (parser
->lexer
);
34447 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34449 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
34452 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34456 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
34460 /* Try to resync to an unnested comma. Copied from
34461 cp_parser_parenthesized_expression_list. */
34464 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34465 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
34466 /*recovering=*/true,
34468 /*consume_paren=*/true);
34476 /* Similarly, but expect leading and trailing parenthesis. This is a very
34477 common case for omp clauses. */
34480 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
,
34481 bool allow_deref
= false)
34483 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34484 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
,
34490 copy ( variable-list )
34491 copyin ( variable-list )
34492 copyout ( variable-list )
34493 create ( variable-list )
34494 delete ( variable-list )
34495 present ( variable-list )
34498 no_create ( variable-list )
34499 attach ( variable-list )
34500 detach ( variable-list ) */
34503 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
34506 enum gomp_map_kind kind
;
34509 case PRAGMA_OACC_CLAUSE_ATTACH
:
34510 kind
= GOMP_MAP_ATTACH
;
34512 case PRAGMA_OACC_CLAUSE_COPY
:
34513 kind
= GOMP_MAP_TOFROM
;
34515 case PRAGMA_OACC_CLAUSE_COPYIN
:
34516 kind
= GOMP_MAP_TO
;
34518 case PRAGMA_OACC_CLAUSE_COPYOUT
:
34519 kind
= GOMP_MAP_FROM
;
34521 case PRAGMA_OACC_CLAUSE_CREATE
:
34522 kind
= GOMP_MAP_ALLOC
;
34524 case PRAGMA_OACC_CLAUSE_DELETE
:
34525 kind
= GOMP_MAP_RELEASE
;
34527 case PRAGMA_OACC_CLAUSE_DETACH
:
34528 kind
= GOMP_MAP_DETACH
;
34530 case PRAGMA_OACC_CLAUSE_DEVICE
:
34531 kind
= GOMP_MAP_FORCE_TO
;
34533 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
34534 kind
= GOMP_MAP_DEVICE_RESIDENT
;
34536 case PRAGMA_OACC_CLAUSE_HOST
:
34537 kind
= GOMP_MAP_FORCE_FROM
;
34539 case PRAGMA_OACC_CLAUSE_LINK
:
34540 kind
= GOMP_MAP_LINK
;
34542 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
34543 kind
= GOMP_MAP_IF_PRESENT
;
34545 case PRAGMA_OACC_CLAUSE_PRESENT
:
34546 kind
= GOMP_MAP_FORCE_PRESENT
;
34549 gcc_unreachable ();
34552 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
, true);
34554 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34555 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
34561 deviceptr ( variable-list ) */
34564 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
34566 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34569 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
34570 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
34571 variable-list must only allow for pointer variables. */
34572 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
34573 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
34575 tree v
= TREE_PURPOSE (t
);
34576 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
34577 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
34578 OMP_CLAUSE_DECL (u
) = v
;
34579 OMP_CLAUSE_CHAIN (u
) = list
;
34594 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
34597 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
34599 tree c
= build_omp_clause (loc
, code
);
34600 OMP_CLAUSE_CHAIN (c
) = list
;
34606 num_gangs ( expression )
34607 num_workers ( expression )
34608 vector_length ( expression ) */
34611 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
34612 const char *str
, tree list
)
34614 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34616 matching_parens parens
;
34617 if (!parens
.require_open (parser
))
34620 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
34622 if (t
== error_mark_node
34623 || !parens
.require_close (parser
))
34625 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34626 /*or_comma=*/false,
34627 /*consume_paren=*/true);
34631 check_no_duplicate_clause (list
, code
, str
, loc
);
34633 tree c
= build_omp_clause (loc
, code
);
34634 OMP_CLAUSE_OPERAND (c
, 0) = t
;
34635 OMP_CLAUSE_CHAIN (c
) = list
;
34641 gang [( gang-arg-list )]
34642 worker [( [num:] int-expr )]
34643 vector [( [length:] int-expr )]
34645 where gang-arg is one of:
34650 and size-expr may be:
34657 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
34658 omp_clause_code kind
,
34659 const char *str
, tree list
)
34661 const char *id
= "num";
34662 cp_lexer
*lexer
= parser
->lexer
;
34663 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
34665 if (kind
== OMP_CLAUSE_VECTOR
)
34668 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
34670 matching_parens parens
;
34671 parens
.consume_open (parser
);
34675 cp_token
*next
= cp_lexer_peek_token (lexer
);
34678 /* Gang static argument. */
34679 if (kind
== OMP_CLAUSE_GANG
34680 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
34682 cp_lexer_consume_token (lexer
);
34684 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34685 goto cleanup_error
;
34688 if (ops
[idx
] != NULL
)
34690 cp_parser_error (parser
, "too many %<static%> arguments");
34691 goto cleanup_error
;
34694 /* Check for the '*' argument. */
34695 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
34696 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
34697 || cp_lexer_nth_token_is (parser
->lexer
, 2,
34700 cp_lexer_consume_token (lexer
);
34701 ops
[idx
] = integer_minus_one_node
;
34703 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
34705 cp_lexer_consume_token (lexer
);
34711 /* Worker num: argument and vector length: arguments. */
34712 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
34713 && id_equal (next
->u
.value
, id
)
34714 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
34716 cp_lexer_consume_token (lexer
); /* id */
34717 cp_lexer_consume_token (lexer
); /* ':' */
34720 /* Now collect the actual argument. */
34721 if (ops
[idx
] != NULL_TREE
)
34723 cp_parser_error (parser
, "unexpected argument");
34724 goto cleanup_error
;
34727 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
34729 if (expr
== error_mark_node
)
34730 goto cleanup_error
;
34732 mark_exp_read (expr
);
34735 if (kind
== OMP_CLAUSE_GANG
34736 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
34738 cp_lexer_consume_token (lexer
);
34745 if (!parens
.require_close (parser
))
34746 goto cleanup_error
;
34749 check_no_duplicate_clause (list
, kind
, str
, loc
);
34751 c
= build_omp_clause (loc
, kind
);
34754 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
34756 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
34757 OMP_CLAUSE_CHAIN (c
) = list
;
34762 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
34767 tile ( size-expr-list ) */
34770 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
34772 tree c
, expr
= error_mark_node
;
34773 tree tile
= NULL_TREE
;
34775 /* Collapse and tile are mutually exclusive. (The spec doesn't say
34776 so, but the spec authors never considered such a case and have
34777 differing opinions on what it might mean, including 'not
34779 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
34780 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
34783 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34788 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
34791 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
34792 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
34793 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
34795 cp_lexer_consume_token (parser
->lexer
);
34796 expr
= integer_zero_node
;
34799 expr
= cp_parser_constant_expression (parser
);
34801 tile
= tree_cons (NULL_TREE
, expr
, tile
);
34803 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
34805 /* Consume the trailing ')'. */
34806 cp_lexer_consume_token (parser
->lexer
);
34808 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
34809 tile
= nreverse (tile
);
34810 OMP_CLAUSE_TILE_LIST (c
) = tile
;
34811 OMP_CLAUSE_CHAIN (c
) = list
;
34816 Parse wait clause or directive parameters. */
34819 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
34821 vec
<tree
, va_gc
> *args
;
34824 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
34826 /*allow_expansion_p=*/true,
34827 /*non_constant_p=*/NULL
);
34829 if (args
== NULL
|| args
->length () == 0)
34833 cp_parser_error (parser
, "expected integer expression list");
34834 release_tree_vector (args
);
34839 args_tree
= build_tree_list_vec (args
);
34841 release_tree_vector (args
);
34843 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
34845 tree targ
= TREE_VALUE (t
);
34847 if (targ
!= error_mark_node
)
34849 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
34850 error ("%<wait%> expression must be integral");
34853 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
34855 targ
= mark_rvalue_use (targ
);
34856 OMP_CLAUSE_DECL (c
) = targ
;
34857 OMP_CLAUSE_CHAIN (c
) = list
;
34867 wait [( int-expr-list )] */
34870 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
34872 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
34874 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
34875 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
34878 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
34880 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
34881 OMP_CLAUSE_CHAIN (c
) = list
;
34889 collapse ( constant-expression ) */
34892 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
34898 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34899 matching_parens parens
;
34900 if (!parens
.require_open (parser
))
34903 num
= cp_parser_constant_expression (parser
);
34905 if (!parens
.require_close (parser
))
34906 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34907 /*or_comma=*/false,
34908 /*consume_paren=*/true);
34910 if (num
== error_mark_node
)
34912 num
= fold_non_dependent_expr (num
);
34913 if (!tree_fits_shwi_p (num
)
34914 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
34915 || (n
= tree_to_shwi (num
)) <= 0
34918 error_at (loc
, "collapse argument needs positive constant integer expression");
34922 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
34923 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
34924 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
34925 OMP_CLAUSE_CHAIN (c
) = list
;
34926 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
34932 default ( none | shared )
34935 default ( none | present ) */
34938 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
34939 location_t location
, bool is_oacc
)
34941 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
34944 matching_parens parens
;
34945 if (!parens
.require_open (parser
))
34947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34949 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34950 const char *p
= IDENTIFIER_POINTER (id
);
34955 if (strcmp ("none", p
) != 0)
34957 kind
= OMP_CLAUSE_DEFAULT_NONE
;
34961 if (strcmp ("present", p
) != 0 || !is_oacc
)
34963 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
34967 if (strcmp ("shared", p
) != 0 || is_oacc
)
34969 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
34976 cp_lexer_consume_token (parser
->lexer
);
34982 cp_parser_error (parser
, "expected %<none%> or %<present%>");
34984 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
34987 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
34988 || !parens
.require_close (parser
))
34989 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34990 /*or_comma=*/false,
34991 /*consume_paren=*/true);
34993 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
34996 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
34997 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
34998 OMP_CLAUSE_CHAIN (c
) = list
;
34999 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
35005 final ( expression ) */
35008 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
35012 matching_parens parens
;
35013 if (!parens
.require_open (parser
))
35016 t
= cp_parser_assignment_expression (parser
);
35018 if (t
== error_mark_node
35019 || !parens
.require_close (parser
))
35020 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35021 /*or_comma=*/false,
35022 /*consume_paren=*/true);
35024 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
35026 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
35027 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
35028 OMP_CLAUSE_CHAIN (c
) = list
;
35037 if ( directive-name-modifier : expression )
35039 directive-name-modifier:
35040 parallel | task | taskloop | target data | target | target update
35041 | target enter data | target exit data
35044 directive-name-modifier:
35045 ... | simd | cancel */
35048 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
35052 enum tree_code if_modifier
= ERROR_MARK
;
35054 matching_parens parens
;
35055 if (!parens
.require_open (parser
))
35058 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35060 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35061 const char *p
= IDENTIFIER_POINTER (id
);
35064 if (strcmp ("cancel", p
) == 0)
35065 if_modifier
= VOID_CST
;
35066 else if (strcmp ("parallel", p
) == 0)
35067 if_modifier
= OMP_PARALLEL
;
35068 else if (strcmp ("simd", p
) == 0)
35069 if_modifier
= OMP_SIMD
;
35070 else if (strcmp ("task", p
) == 0)
35071 if_modifier
= OMP_TASK
;
35072 else if (strcmp ("taskloop", p
) == 0)
35073 if_modifier
= OMP_TASKLOOP
;
35074 else if (strcmp ("target", p
) == 0)
35076 if_modifier
= OMP_TARGET
;
35077 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35079 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
35080 p
= IDENTIFIER_POINTER (id
);
35081 if (strcmp ("data", p
) == 0)
35082 if_modifier
= OMP_TARGET_DATA
;
35083 else if (strcmp ("update", p
) == 0)
35084 if_modifier
= OMP_TARGET_UPDATE
;
35085 else if (strcmp ("enter", p
) == 0)
35086 if_modifier
= OMP_TARGET_ENTER_DATA
;
35087 else if (strcmp ("exit", p
) == 0)
35088 if_modifier
= OMP_TARGET_EXIT_DATA
;
35089 if (if_modifier
!= OMP_TARGET
)
35094 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
35095 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
35097 if_modifier
= ERROR_MARK
;
35099 if (if_modifier
== OMP_TARGET_ENTER_DATA
35100 || if_modifier
== OMP_TARGET_EXIT_DATA
)
35102 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
35104 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
35105 p
= IDENTIFIER_POINTER (id
);
35106 if (strcmp ("data", p
) == 0)
35112 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
35113 error_at (loc
, "expected %<data%>");
35114 if_modifier
= ERROR_MARK
;
35119 if (if_modifier
!= ERROR_MARK
)
35121 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
35124 cp_lexer_consume_token (parser
->lexer
);
35131 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
35132 error_at (loc
, "expected %<:%>");
35134 if_modifier
= ERROR_MARK
;
35139 t
= cp_parser_assignment_expression (parser
);
35141 if (t
== error_mark_node
35142 || !parens
.require_close (parser
))
35143 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35144 /*or_comma=*/false,
35145 /*consume_paren=*/true);
35147 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35148 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
35150 if (if_modifier
!= ERROR_MARK
35151 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
35153 const char *p
= NULL
;
35154 switch (if_modifier
)
35156 case VOID_CST
: p
= "cancel"; break;
35157 case OMP_PARALLEL
: p
= "parallel"; break;
35158 case OMP_SIMD
: p
= "simd"; break;
35159 case OMP_TASK
: p
= "task"; break;
35160 case OMP_TASKLOOP
: p
= "taskloop"; break;
35161 case OMP_TARGET_DATA
: p
= "target data"; break;
35162 case OMP_TARGET
: p
= "target"; break;
35163 case OMP_TARGET_UPDATE
: p
= "target update"; break;
35164 case OMP_TARGET_ENTER_DATA
: p
= "target enter data"; break;
35165 case OMP_TARGET_EXIT_DATA
: p
= "target exit data"; break;
35166 default: gcc_unreachable ();
35168 error_at (location
, "too many %<if%> clauses with %qs modifier",
35172 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
35175 error_at (location
, "too many %<if%> clauses");
35177 error_at (location
, "too many %<if%> clauses without modifier");
35180 else if (if_modifier
== ERROR_MARK
35181 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
35183 error_at (location
, "if any %<if%> clause has modifier, then all "
35184 "%<if%> clauses have to use modifier");
35189 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
35190 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
35191 OMP_CLAUSE_IF_EXPR (c
) = t
;
35192 OMP_CLAUSE_CHAIN (c
) = list
;
35201 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
35202 tree list
, location_t location
)
35206 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
35209 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
35210 OMP_CLAUSE_CHAIN (c
) = list
;
35218 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
35219 tree list
, location_t location
)
35223 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
35225 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
35226 OMP_CLAUSE_CHAIN (c
) = list
;
35231 num_threads ( expression ) */
35234 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
35235 location_t location
)
35239 matching_parens parens
;
35240 if (!parens
.require_open (parser
))
35243 t
= cp_parser_assignment_expression (parser
);
35245 if (t
== error_mark_node
35246 || !parens
.require_close (parser
))
35247 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35248 /*or_comma=*/false,
35249 /*consume_paren=*/true);
35251 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
35252 "num_threads", location
);
35254 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
35255 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
35256 OMP_CLAUSE_CHAIN (c
) = list
;
35262 num_tasks ( expression ) */
35265 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
35266 location_t location
)
35270 matching_parens parens
;
35271 if (!parens
.require_open (parser
))
35274 t
= cp_parser_assignment_expression (parser
);
35276 if (t
== error_mark_node
35277 || !parens
.require_close (parser
))
35278 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35279 /*or_comma=*/false,
35280 /*consume_paren=*/true);
35282 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
35283 "num_tasks", location
);
35285 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
35286 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
35287 OMP_CLAUSE_CHAIN (c
) = list
;
35293 grainsize ( expression ) */
35296 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
35297 location_t location
)
35301 matching_parens parens
;
35302 if (!parens
.require_open (parser
))
35305 t
= cp_parser_assignment_expression (parser
);
35307 if (t
== error_mark_node
35308 || !parens
.require_close (parser
))
35309 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35310 /*or_comma=*/false,
35311 /*consume_paren=*/true);
35313 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
35314 "grainsize", location
);
35316 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
35317 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
35318 OMP_CLAUSE_CHAIN (c
) = list
;
35324 priority ( expression ) */
35327 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
35328 location_t location
)
35332 matching_parens parens
;
35333 if (!parens
.require_open (parser
))
35336 t
= cp_parser_assignment_expression (parser
);
35338 if (t
== error_mark_node
35339 || !parens
.require_close (parser
))
35340 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35341 /*or_comma=*/false,
35342 /*consume_paren=*/true);
35344 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
35345 "priority", location
);
35347 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
35348 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
35349 OMP_CLAUSE_CHAIN (c
) = list
;
35355 hint ( expression ) */
35358 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
35362 matching_parens parens
;
35363 if (!parens
.require_open (parser
))
35366 t
= cp_parser_assignment_expression (parser
);
35368 if (t
== error_mark_node
35369 || !parens
.require_close (parser
))
35370 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35371 /*or_comma=*/false,
35372 /*consume_paren=*/true);
35374 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
35376 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
35377 OMP_CLAUSE_HINT_EXPR (c
) = t
;
35378 OMP_CLAUSE_CHAIN (c
) = list
;
35384 defaultmap ( tofrom : scalar )
35387 defaultmap ( implicit-behavior [ : variable-category ] ) */
35390 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
35391 location_t location
)
35395 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
35396 enum omp_clause_defaultmap_kind category
35397 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
35399 matching_parens parens
;
35400 if (!parens
.require_open (parser
))
35403 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
35405 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35408 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
35409 "%<tofrom%>, %<firstprivate%>, %<none%> "
35415 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35416 p
= IDENTIFIER_POINTER (id
);
35422 if (strcmp ("alloc", p
) == 0)
35423 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
35425 goto invalid_behavior
;
35429 if (strcmp ("default", p
) == 0)
35430 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
35432 goto invalid_behavior
;
35436 if (strcmp ("firstprivate", p
) == 0)
35437 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
35438 else if (strcmp ("from", p
) == 0)
35439 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
35441 goto invalid_behavior
;
35445 if (strcmp ("none", p
) == 0)
35446 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
35448 goto invalid_behavior
;
35452 if (strcmp ("tofrom", p
) == 0)
35453 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
35454 else if (strcmp ("to", p
) == 0)
35455 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
35457 goto invalid_behavior
;
35461 goto invalid_behavior
;
35463 cp_lexer_consume_token (parser
->lexer
);
35465 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
35467 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35470 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35473 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
35477 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35478 p
= IDENTIFIER_POINTER (id
);
35483 if (strcmp ("aggregate", p
) == 0)
35484 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
35486 goto invalid_category
;
35490 if (strcmp ("pointer", p
) == 0)
35491 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
35493 goto invalid_category
;
35497 if (strcmp ("scalar", p
) == 0)
35498 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
35500 goto invalid_category
;
35504 goto invalid_category
;
35507 cp_lexer_consume_token (parser
->lexer
);
35509 if (!parens
.require_close (parser
))
35512 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
35513 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
35514 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
35515 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
35516 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
35517 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
35519 enum omp_clause_defaultmap_kind cat
= category
;
35520 location_t loc
= OMP_CLAUSE_LOCATION (c
);
35521 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
35522 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
35526 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
35529 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
35532 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
35535 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
35539 gcc_unreachable ();
35542 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
35545 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
35550 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
35551 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
35552 OMP_CLAUSE_CHAIN (c
) = list
;
35556 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35557 /*or_comma=*/false,
35558 /*consume_paren=*/true);
35563 order ( concurrent ) */
35566 cp_parser_omp_clause_order (cp_parser
*parser
, tree list
, location_t location
)
35571 matching_parens parens
;
35572 if (!parens
.require_open (parser
))
35575 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35577 cp_parser_error (parser
, "expected %<concurrent%>");
35582 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35583 p
= IDENTIFIER_POINTER (id
);
35585 if (strcmp (p
, "concurrent") != 0)
35587 cp_parser_error (parser
, "expected %<concurrent%>");
35590 cp_lexer_consume_token (parser
->lexer
);
35591 if (!parens
.require_close (parser
))
35594 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
35595 c
= build_omp_clause (location
, OMP_CLAUSE_ORDER
);
35596 OMP_CLAUSE_CHAIN (c
) = list
;
35600 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35601 /*or_comma=*/false,
35602 /*consume_paren=*/true);
35607 bind ( teams | parallel | thread ) */
35610 cp_parser_omp_clause_bind (cp_parser
*parser
, tree list
,
35611 location_t location
)
35615 enum omp_clause_bind_kind kind
= OMP_CLAUSE_BIND_THREAD
;
35617 matching_parens parens
;
35618 if (!parens
.require_open (parser
))
35621 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35624 cp_parser_error (parser
,
35625 "expected %<teams%>, %<parallel%> or %<thread%>");
35630 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35631 p
= IDENTIFIER_POINTER (id
);
35633 if (strcmp (p
, "teams") == 0)
35634 kind
= OMP_CLAUSE_BIND_TEAMS
;
35635 else if (strcmp (p
, "parallel") == 0)
35636 kind
= OMP_CLAUSE_BIND_PARALLEL
;
35637 else if (strcmp (p
, "thread") != 0)
35639 cp_lexer_consume_token (parser
->lexer
);
35640 if (!parens
.require_close (parser
))
35643 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
35644 c
= build_omp_clause (location
, OMP_CLAUSE_BIND
);
35645 OMP_CLAUSE_BIND_KIND (c
) = kind
;
35646 OMP_CLAUSE_CHAIN (c
) = list
;
35650 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35651 /*or_comma=*/false,
35652 /*consume_paren=*/true);
35660 ordered ( constant-expression ) */
35663 cp_parser_omp_clause_ordered (cp_parser
*parser
,
35664 tree list
, location_t location
)
35666 tree c
, num
= NULL_TREE
;
35669 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
35670 "ordered", location
);
35672 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
35674 matching_parens parens
;
35675 parens
.consume_open (parser
);
35677 num
= cp_parser_constant_expression (parser
);
35679 if (!parens
.require_close (parser
))
35680 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35681 /*or_comma=*/false,
35682 /*consume_paren=*/true);
35684 if (num
== error_mark_node
)
35686 num
= fold_non_dependent_expr (num
);
35687 if (!tree_fits_shwi_p (num
)
35688 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
35689 || (n
= tree_to_shwi (num
)) <= 0
35692 error_at (location
,
35693 "ordered argument needs positive constant integer "
35699 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
35700 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
35701 OMP_CLAUSE_CHAIN (c
) = list
;
35706 reduction ( reduction-operator : variable-list )
35708 reduction-operator:
35709 One of: + * - & ^ | && ||
35713 reduction-operator:
35714 One of: + * - & ^ | && || min max
35718 reduction-operator:
35719 One of: + * - & ^ | && ||
35723 reduction ( reduction-modifier, reduction-operator : variable-list )
35724 in_reduction ( reduction-operator : variable-list )
35725 task_reduction ( reduction-operator : variable-list ) */
35728 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
35729 bool is_omp
, tree list
)
35731 enum tree_code code
= ERROR_MARK
;
35732 tree nlist
, c
, id
= NULL_TREE
;
35734 bool inscan
= false;
35736 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35739 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
35741 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
35742 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
35744 cp_lexer_consume_token (parser
->lexer
);
35745 cp_lexer_consume_token (parser
->lexer
);
35747 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
35748 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
35750 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35751 const char *p
= IDENTIFIER_POINTER (id
);
35752 if (strcmp (p
, "task") == 0)
35754 else if (strcmp (p
, "inscan") == 0)
35756 if (task
|| inscan
)
35758 cp_lexer_consume_token (parser
->lexer
);
35759 cp_lexer_consume_token (parser
->lexer
);
35764 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
35766 case CPP_PLUS
: code
= PLUS_EXPR
; break;
35767 case CPP_MULT
: code
= MULT_EXPR
; break;
35768 case CPP_MINUS
: code
= MINUS_EXPR
; break;
35769 case CPP_AND
: code
= BIT_AND_EXPR
; break;
35770 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
35771 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
35772 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
35773 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
35777 if (code
!= ERROR_MARK
)
35778 cp_lexer_consume_token (parser
->lexer
);
35781 bool saved_colon_corrects_to_scope_p
;
35782 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
35783 parser
->colon_corrects_to_scope_p
= false;
35784 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
35785 /*check_dependency_p=*/true,
35786 /*template_p=*/NULL
,
35787 /*declarator_p=*/false,
35788 /*optional_p=*/false);
35789 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
35790 if (identifier_p (id
))
35792 const char *p
= IDENTIFIER_POINTER (id
);
35794 if (strcmp (p
, "min") == 0)
35796 else if (strcmp (p
, "max") == 0)
35798 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
35800 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
35802 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
35804 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
35805 code
= BIT_AND_EXPR
;
35806 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
35807 code
= BIT_IOR_EXPR
;
35808 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
35809 code
= BIT_XOR_EXPR
;
35810 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
35811 code
= TRUTH_ANDIF_EXPR
;
35812 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
35813 code
= TRUTH_ORIF_EXPR
;
35814 id
= omp_reduction_id (code
, id
, NULL_TREE
);
35815 tree scope
= parser
->scope
;
35817 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
35818 parser
->scope
= NULL_TREE
;
35819 parser
->qualifying_scope
= NULL_TREE
;
35820 parser
->object_scope
= NULL_TREE
;
35824 error ("invalid reduction-identifier");
35826 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35827 /*or_comma=*/false,
35828 /*consume_paren=*/true);
35833 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35836 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
35838 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35840 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
35842 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
35844 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
35845 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
35852 schedule ( schedule-kind )
35853 schedule ( schedule-kind , expression )
35856 static | dynamic | guided | runtime | auto
35859 schedule ( schedule-modifier : schedule-kind )
35860 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
35868 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
35871 int modifiers
= 0, nmodifiers
= 0;
35873 matching_parens parens
;
35874 if (!parens
.require_open (parser
))
35877 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
35879 location_t comma
= UNKNOWN_LOCATION
;
35880 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35882 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35883 const char *p
= IDENTIFIER_POINTER (id
);
35884 if (strcmp ("simd", p
) == 0)
35885 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
35886 else if (strcmp ("monotonic", p
) == 0)
35887 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
35888 else if (strcmp ("nonmonotonic", p
) == 0)
35889 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
35892 comma
= UNKNOWN_LOCATION
;
35893 cp_lexer_consume_token (parser
->lexer
);
35894 if (nmodifiers
++ == 0
35895 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35897 comma
= cp_lexer_peek_token (parser
->lexer
)->location
;
35898 cp_lexer_consume_token (parser
->lexer
);
35902 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
35906 if (comma
!= UNKNOWN_LOCATION
)
35907 error_at (comma
, "expected %<:%>");
35909 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35911 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35912 const char *p
= IDENTIFIER_POINTER (id
);
35917 if (strcmp ("dynamic", p
) != 0)
35919 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
35923 if (strcmp ("guided", p
) != 0)
35925 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
35929 if (strcmp ("runtime", p
) != 0)
35931 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
35938 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
35939 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
35940 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
35941 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
35944 cp_lexer_consume_token (parser
->lexer
);
35946 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
35947 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
35948 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
35949 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
35951 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
35956 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35959 cp_lexer_consume_token (parser
->lexer
);
35961 token
= cp_lexer_peek_token (parser
->lexer
);
35962 t
= cp_parser_assignment_expression (parser
);
35964 if (t
== error_mark_node
)
35966 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
35967 error_at (token
->location
, "schedule %<runtime%> does not take "
35968 "a %<chunk_size%> parameter");
35969 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
35970 error_at (token
->location
, "schedule %<auto%> does not take "
35971 "a %<chunk_size%> parameter");
35973 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
35975 if (!parens
.require_close (parser
))
35978 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
35981 OMP_CLAUSE_SCHEDULE_KIND (c
)
35982 = (enum omp_clause_schedule_kind
)
35983 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
35985 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
35986 OMP_CLAUSE_CHAIN (c
) = list
;
35990 cp_parser_error (parser
, "invalid schedule kind");
35992 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35993 /*or_comma=*/false,
35994 /*consume_paren=*/true);
36002 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
36003 tree list
, location_t location
)
36007 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
36009 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
36010 OMP_CLAUSE_CHAIN (c
) = list
;
36019 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
36020 tree list
, location_t location
)
36022 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
36023 tree c
= build_omp_clause (location
, code
);
36024 OMP_CLAUSE_CHAIN (c
) = list
;
36035 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
36036 enum omp_clause_code code
,
36037 tree list
, location_t location
)
36039 tree c
= build_omp_clause (location
, code
);
36040 OMP_CLAUSE_CHAIN (c
) = list
;
36048 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
36049 tree list
, location_t location
)
36051 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
36052 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
36053 OMP_CLAUSE_CHAIN (c
) = list
;
36062 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
36063 enum omp_clause_code code
,
36064 tree list
, location_t location
)
36066 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
36067 tree c
= build_omp_clause (location
, code
);
36068 OMP_CLAUSE_CHAIN (c
) = list
;
36073 num_teams ( expression ) */
36076 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
36077 location_t location
)
36081 matching_parens parens
;
36082 if (!parens
.require_open (parser
))
36085 t
= cp_parser_assignment_expression (parser
);
36087 if (t
== error_mark_node
36088 || !parens
.require_close (parser
))
36089 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36090 /*or_comma=*/false,
36091 /*consume_paren=*/true);
36093 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
36094 "num_teams", location
);
36096 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
36097 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
36098 OMP_CLAUSE_CHAIN (c
) = list
;
36104 thread_limit ( expression ) */
36107 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
36108 location_t location
)
36112 matching_parens parens
;
36113 if (!parens
.require_open (parser
))
36116 t
= cp_parser_assignment_expression (parser
);
36118 if (t
== error_mark_node
36119 || !parens
.require_close (parser
))
36120 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36121 /*or_comma=*/false,
36122 /*consume_paren=*/true);
36124 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
36125 "thread_limit", location
);
36127 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
36128 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
36129 OMP_CLAUSE_CHAIN (c
) = list
;
36135 aligned ( variable-list )
36136 aligned ( variable-list : constant-expression ) */
36139 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
36141 tree nlist
, c
, alignment
= NULL_TREE
;
36144 matching_parens parens
;
36145 if (!parens
.require_open (parser
))
36148 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
36153 alignment
= cp_parser_constant_expression (parser
);
36155 if (!parens
.require_close (parser
))
36156 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36157 /*or_comma=*/false,
36158 /*consume_paren=*/true);
36160 if (alignment
== error_mark_node
)
36161 alignment
= NULL_TREE
;
36164 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36165 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
36171 lastprivate ( variable-list )
36174 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
36177 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
36179 bool conditional
= false;
36181 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36184 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36185 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
36187 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36188 const char *p
= IDENTIFIER_POINTER (id
);
36190 if (strcmp ("conditional", p
) == 0)
36192 conditional
= true;
36193 cp_lexer_consume_token (parser
->lexer
);
36194 cp_lexer_consume_token (parser
->lexer
);
36198 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
36202 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36203 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
36208 linear ( variable-list )
36209 linear ( variable-list : expression )
36212 linear ( modifier ( variable-list ) )
36213 linear ( modifier ( variable-list ) : expression ) */
36216 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
36219 tree nlist
, c
, step
= integer_one_node
;
36221 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
36223 matching_parens parens
;
36224 if (!parens
.require_open (parser
))
36227 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36229 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36230 const char *p
= IDENTIFIER_POINTER (id
);
36232 if (strcmp ("ref", p
) == 0)
36233 kind
= OMP_CLAUSE_LINEAR_REF
;
36234 else if (strcmp ("val", p
) == 0)
36235 kind
= OMP_CLAUSE_LINEAR_VAL
;
36236 else if (strcmp ("uval", p
) == 0)
36237 kind
= OMP_CLAUSE_LINEAR_UVAL
;
36238 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
36239 cp_lexer_consume_token (parser
->lexer
);
36241 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
36244 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
36245 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
36249 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
36250 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
36252 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36253 else if (!parens
.require_close (parser
))
36254 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36255 /*or_comma=*/false,
36256 /*consume_paren=*/true);
36263 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36264 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
36266 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
36267 cp_parser_parse_tentatively (parser
);
36268 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
36269 /*check_dependency_p=*/true,
36270 /*template_p=*/NULL
,
36271 /*declarator_p=*/false,
36272 /*optional_p=*/false);
36273 if (step
!= error_mark_node
)
36274 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
36275 if (step
== error_mark_node
)
36278 cp_parser_abort_tentative_parse (parser
);
36280 else if (!cp_parser_parse_definitely (parser
))
36284 step
= cp_parser_assignment_expression (parser
);
36286 if (!parens
.require_close (parser
))
36287 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36288 /*or_comma=*/false,
36289 /*consume_paren=*/true);
36291 if (step
== error_mark_node
)
36295 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36297 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
36298 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
36305 safelen ( constant-expression ) */
36308 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
36309 location_t location
)
36313 matching_parens parens
;
36314 if (!parens
.require_open (parser
))
36317 t
= cp_parser_constant_expression (parser
);
36319 if (t
== error_mark_node
36320 || !parens
.require_close (parser
))
36321 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36322 /*or_comma=*/false,
36323 /*consume_paren=*/true);
36325 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
36327 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
36328 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
36329 OMP_CLAUSE_CHAIN (c
) = list
;
36335 simdlen ( constant-expression ) */
36338 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
36339 location_t location
)
36343 matching_parens parens
;
36344 if (!parens
.require_open (parser
))
36347 t
= cp_parser_constant_expression (parser
);
36349 if (t
== error_mark_node
36350 || !parens
.require_close (parser
))
36351 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36352 /*or_comma=*/false,
36353 /*consume_paren=*/true);
36355 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
36357 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
36358 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
36359 OMP_CLAUSE_CHAIN (c
) = list
;
36366 identifier [+/- integer]
36367 vec , identifier [+/- integer]
36371 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
36376 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36378 cp_parser_error (parser
, "expected identifier");
36382 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36384 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36385 tree t
, identifier
= cp_parser_identifier (parser
);
36386 tree addend
= NULL
;
36388 if (identifier
== error_mark_node
)
36389 t
= error_mark_node
;
36392 t
= cp_parser_lookup_name_simple
36393 (parser
, identifier
,
36394 cp_lexer_peek_token (parser
->lexer
)->location
);
36395 if (t
== error_mark_node
)
36396 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
36401 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
36403 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
36405 addend
= integer_zero_node
;
36406 goto add_to_vector
;
36408 cp_lexer_consume_token (parser
->lexer
);
36410 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
36412 cp_parser_error (parser
, "expected integer");
36416 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36417 if (TREE_CODE (addend
) != INTEGER_CST
)
36419 cp_parser_error (parser
, "expected integer");
36422 cp_lexer_consume_token (parser
->lexer
);
36425 if (t
!= error_mark_node
)
36427 vec
= tree_cons (addend
, t
, vec
);
36429 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
36432 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
36435 cp_lexer_consume_token (parser
->lexer
);
36438 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
36440 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
36441 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
36442 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
36443 OMP_CLAUSE_CHAIN (u
) = list
;
36450 iterators ( iterators-definition )
36452 iterators-definition:
36454 iterator-specifier , iterators-definition
36456 iterator-specifier:
36457 identifier = range-specification
36458 iterator-type identifier = range-specification
36460 range-specification:
36462 begin : end : step */
36465 cp_parser_omp_iterators (cp_parser
*parser
)
36467 tree ret
= NULL_TREE
, *last
= &ret
;
36468 cp_lexer_consume_token (parser
->lexer
);
36470 matching_parens parens
;
36471 if (!parens
.require_open (parser
))
36472 return error_mark_node
;
36474 bool saved_colon_corrects_to_scope_p
36475 = parser
->colon_corrects_to_scope_p
;
36476 bool saved_colon_doesnt_start_class_def_p
36477 = parser
->colon_doesnt_start_class_def_p
;
36482 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36483 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
36484 iter_type
= integer_type_node
;
36487 const char *saved_message
36488 = parser
->type_definition_forbidden_message
;
36489 parser
->type_definition_forbidden_message
36490 = G_("types may not be defined in iterator type");
36492 iter_type
= cp_parser_type_id (parser
);
36494 parser
->type_definition_forbidden_message
= saved_message
;
36497 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36498 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36500 cp_parser_error (parser
, "expected identifier");
36504 tree id
= cp_parser_identifier (parser
);
36505 if (id
== error_mark_node
)
36508 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36511 parser
->colon_corrects_to_scope_p
= false;
36512 parser
->colon_doesnt_start_class_def_p
= true;
36513 tree begin
= cp_parser_assignment_expression (parser
);
36515 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36518 tree end
= cp_parser_assignment_expression (parser
);
36520 tree step
= integer_one_node
;
36521 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
36523 cp_lexer_consume_token (parser
->lexer
);
36524 step
= cp_parser_assignment_expression (parser
);
36527 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
36528 DECL_ARTIFICIAL (iter_var
) = 1;
36529 DECL_CONTEXT (iter_var
) = current_function_decl
;
36530 pushdecl (iter_var
);
36532 *last
= make_tree_vec (6);
36533 TREE_VEC_ELT (*last
, 0) = iter_var
;
36534 TREE_VEC_ELT (*last
, 1) = begin
;
36535 TREE_VEC_ELT (*last
, 2) = end
;
36536 TREE_VEC_ELT (*last
, 3) = step
;
36537 last
= &TREE_CHAIN (*last
);
36539 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36541 cp_lexer_consume_token (parser
->lexer
);
36548 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36549 parser
->colon_doesnt_start_class_def_p
36550 = saved_colon_doesnt_start_class_def_p
;
36552 if (!parens
.require_close (parser
))
36553 cp_parser_skip_to_closing_parenthesis (parser
,
36554 /*recovering=*/true,
36555 /*or_comma=*/false,
36556 /*consume_paren=*/true);
36558 return ret
? ret
: error_mark_node
;
36562 depend ( depend-kind : variable-list )
36570 depend ( sink : vec )
36573 depend ( depend-modifier , depend-kind: variable-list )
36576 in | out | inout | mutexinoutset | depobj
36579 iterator ( iterators-definition ) */
36582 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
36584 tree nlist
, c
, iterators
= NULL_TREE
;
36585 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
36587 matching_parens parens
;
36588 if (!parens
.require_open (parser
))
36593 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
36596 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36597 const char *p
= IDENTIFIER_POINTER (id
);
36599 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
36601 begin_scope (sk_omp
, NULL
);
36602 iterators
= cp_parser_omp_iterators (parser
);
36603 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
36606 if (strcmp ("in", p
) == 0)
36607 kind
= OMP_CLAUSE_DEPEND_IN
;
36608 else if (strcmp ("inout", p
) == 0)
36609 kind
= OMP_CLAUSE_DEPEND_INOUT
;
36610 else if (strcmp ("mutexinoutset", p
) == 0)
36611 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
36612 else if (strcmp ("out", p
) == 0)
36613 kind
= OMP_CLAUSE_DEPEND_OUT
;
36614 else if (strcmp ("depobj", p
) == 0)
36615 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
36616 else if (strcmp ("sink", p
) == 0)
36617 kind
= OMP_CLAUSE_DEPEND_SINK
;
36618 else if (strcmp ("source", p
) == 0)
36619 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
36626 cp_lexer_consume_token (parser
->lexer
);
36629 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
36631 poplevel (0, 1, 0);
36632 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
36633 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
36634 iterators
= NULL_TREE
;
36637 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
36639 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
36640 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
36641 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
36642 OMP_CLAUSE_CHAIN (c
) = list
;
36643 if (!parens
.require_close (parser
))
36644 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36645 /*or_comma=*/false,
36646 /*consume_paren=*/true);
36650 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36653 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
36654 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
36657 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
36662 tree block
= poplevel (1, 1, 0);
36663 if (iterators
== error_mark_node
)
36664 iterators
= NULL_TREE
;
36666 TREE_VEC_ELT (iterators
, 5) = block
;
36669 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36671 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
36673 OMP_CLAUSE_DECL (c
)
36674 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
36680 cp_parser_error (parser
, "invalid depend kind");
36683 poplevel (0, 1, 0);
36684 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36685 /*or_comma=*/false,
36686 /*consume_paren=*/true);
36691 map ( map-kind : variable-list )
36692 map ( variable-list )
36695 alloc | to | from | tofrom
36699 alloc | to | from | tofrom | release | delete
36701 map ( always [,] map-kind: variable-list ) */
36704 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
36707 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
36708 bool always
= false;
36710 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36713 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36715 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36716 const char *p
= IDENTIFIER_POINTER (id
);
36718 if (strcmp ("always", p
) == 0)
36721 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
36723 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
36724 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
36726 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
36730 cp_lexer_consume_token (parser
->lexer
);
36732 cp_lexer_consume_token (parser
->lexer
);
36737 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36738 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
36740 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36741 const char *p
= IDENTIFIER_POINTER (id
);
36743 if (strcmp ("alloc", p
) == 0)
36744 kind
= GOMP_MAP_ALLOC
;
36745 else if (strcmp ("to", p
) == 0)
36746 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
36747 else if (strcmp ("from", p
) == 0)
36748 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
36749 else if (strcmp ("tofrom", p
) == 0)
36750 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
36751 else if (strcmp ("release", p
) == 0)
36752 kind
= GOMP_MAP_RELEASE
;
36755 cp_parser_error (parser
, "invalid map kind");
36756 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36757 /*or_comma=*/false,
36758 /*consume_paren=*/true);
36761 cp_lexer_consume_token (parser
->lexer
);
36762 cp_lexer_consume_token (parser
->lexer
);
36764 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
36765 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
36767 kind
= GOMP_MAP_DELETE
;
36768 cp_lexer_consume_token (parser
->lexer
);
36769 cp_lexer_consume_token (parser
->lexer
);
36772 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
36775 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36776 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
36782 device ( expression ) */
36785 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
36786 location_t location
)
36790 matching_parens parens
;
36791 if (!parens
.require_open (parser
))
36794 t
= cp_parser_assignment_expression (parser
);
36796 if (t
== error_mark_node
36797 || !parens
.require_close (parser
))
36798 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36799 /*or_comma=*/false,
36800 /*consume_paren=*/true);
36802 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
36803 "device", location
);
36805 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
36806 OMP_CLAUSE_DEVICE_ID (c
) = t
;
36807 OMP_CLAUSE_CHAIN (c
) = list
;
36813 dist_schedule ( static )
36814 dist_schedule ( static , expression ) */
36817 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
36818 location_t location
)
36822 matching_parens parens
;
36823 if (!parens
.require_open (parser
))
36826 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
36828 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
36830 cp_lexer_consume_token (parser
->lexer
);
36832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36834 cp_lexer_consume_token (parser
->lexer
);
36836 t
= cp_parser_assignment_expression (parser
);
36838 if (t
== error_mark_node
)
36840 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
36842 if (!parens
.require_close (parser
))
36845 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
36848 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
36849 "dist_schedule", location); */
36850 if (omp_find_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
))
36851 warning_at (location
, 0, "too many %qs clauses", "dist_schedule");
36852 OMP_CLAUSE_CHAIN (c
) = list
;
36856 cp_parser_error (parser
, "invalid dist_schedule kind");
36858 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36859 /*or_comma=*/false,
36860 /*consume_paren=*/true);
36865 proc_bind ( proc-bind-kind )
36868 master | close | spread */
36871 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
36872 location_t location
)
36875 enum omp_clause_proc_bind_kind kind
;
36877 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36882 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36883 const char *p
= IDENTIFIER_POINTER (id
);
36885 if (strcmp ("master", p
) == 0)
36886 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
36887 else if (strcmp ("close", p
) == 0)
36888 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
36889 else if (strcmp ("spread", p
) == 0)
36890 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
36897 cp_lexer_consume_token (parser
->lexer
);
36898 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
36901 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
36902 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
36904 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
36905 OMP_CLAUSE_CHAIN (c
) = list
;
36909 cp_parser_error (parser
, "invalid depend kind");
36911 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36912 /*or_comma=*/false,
36913 /*consume_paren=*/true);
36918 device_type ( host | nohost | any ) */
36921 cp_parser_omp_clause_device_type (cp_parser
*parser
, tree list
,
36922 location_t location
)
36925 enum omp_clause_device_type_kind kind
;
36927 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36930 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36932 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36933 const char *p
= IDENTIFIER_POINTER (id
);
36935 if (strcmp ("host", p
) == 0)
36936 kind
= OMP_CLAUSE_DEVICE_TYPE_HOST
;
36937 else if (strcmp ("nohost", p
) == 0)
36938 kind
= OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
36939 else if (strcmp ("any", p
) == 0)
36940 kind
= OMP_CLAUSE_DEVICE_TYPE_ANY
;
36947 cp_lexer_consume_token (parser
->lexer
);
36948 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
36951 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE_TYPE
);
36952 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
36954 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = kind
;
36955 OMP_CLAUSE_CHAIN (c
) = list
;
36959 cp_parser_error (parser
, "invalid depend kind");
36961 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36962 /*or_comma=*/false,
36963 /*consume_paren=*/true);
36968 async [( int-expr )] */
36971 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
36974 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36976 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
36978 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36980 matching_parens parens
;
36981 parens
.consume_open (parser
);
36983 t
= cp_parser_expression (parser
);
36984 if (t
== error_mark_node
36985 || !parens
.require_close (parser
))
36986 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36987 /*or_comma=*/false,
36988 /*consume_paren=*/true);
36991 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
36993 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
36994 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
36995 OMP_CLAUSE_CHAIN (c
) = list
;
37001 /* Parse all OpenACC clauses. The set clauses allowed by the directive
37002 is a bitmask in MASK. Return the list of clauses found. */
37005 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
37006 const char *where
, cp_token
*pragma_tok
,
37007 bool finish_p
= true)
37009 tree clauses
= NULL
;
37012 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37015 pragma_omp_clause c_kind
;
37016 omp_clause_code code
;
37017 const char *c_name
;
37018 tree prev
= clauses
;
37020 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37021 cp_lexer_consume_token (parser
->lexer
);
37023 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
37024 c_kind
= cp_parser_omp_clause_name (parser
);
37028 case PRAGMA_OACC_CLAUSE_ASYNC
:
37029 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
37032 case PRAGMA_OACC_CLAUSE_AUTO
:
37033 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
37037 case PRAGMA_OACC_CLAUSE_ATTACH
:
37038 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37041 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
37042 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
37043 c_name
= "collapse";
37045 case PRAGMA_OACC_CLAUSE_COPY
:
37046 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37049 case PRAGMA_OACC_CLAUSE_COPYIN
:
37050 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37053 case PRAGMA_OACC_CLAUSE_COPYOUT
:
37054 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37055 c_name
= "copyout";
37057 case PRAGMA_OACC_CLAUSE_CREATE
:
37058 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37061 case PRAGMA_OACC_CLAUSE_DELETE
:
37062 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37065 case PRAGMA_OMP_CLAUSE_DEFAULT
:
37066 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
37067 c_name
= "default";
37069 case PRAGMA_OACC_CLAUSE_DETACH
:
37070 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37073 case PRAGMA_OACC_CLAUSE_DEVICE
:
37074 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37077 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
37078 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
37079 c_name
= "deviceptr";
37081 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
37082 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37083 c_name
= "device_resident";
37085 case PRAGMA_OACC_CLAUSE_FINALIZE
:
37086 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
37088 c_name
= "finalize";
37090 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
37091 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
37093 c_name
= "firstprivate";
37095 case PRAGMA_OACC_CLAUSE_GANG
:
37097 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
37100 case PRAGMA_OACC_CLAUSE_HOST
:
37101 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37104 case PRAGMA_OACC_CLAUSE_IF
:
37105 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
37108 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
37109 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
37111 c_name
= "if_present";
37113 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
37114 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
37116 c_name
= "independent";
37118 case PRAGMA_OACC_CLAUSE_LINK
:
37119 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37122 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
37123 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37124 c_name
= "no_create";
37126 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
37127 code
= OMP_CLAUSE_NUM_GANGS
;
37128 c_name
= "num_gangs";
37129 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
37132 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
37133 c_name
= "num_workers";
37134 code
= OMP_CLAUSE_NUM_WORKERS
;
37135 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
37138 case PRAGMA_OACC_CLAUSE_PRESENT
:
37139 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
37140 c_name
= "present";
37142 case PRAGMA_OACC_CLAUSE_PRIVATE
:
37143 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
37145 c_name
= "private";
37147 case PRAGMA_OACC_CLAUSE_REDUCTION
:
37149 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
37151 c_name
= "reduction";
37153 case PRAGMA_OACC_CLAUSE_SEQ
:
37154 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
37158 case PRAGMA_OACC_CLAUSE_TILE
:
37159 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
37162 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
37163 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
37165 c_name
= "use_device";
37167 case PRAGMA_OACC_CLAUSE_VECTOR
:
37169 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
37173 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
37174 c_name
= "vector_length";
37175 code
= OMP_CLAUSE_VECTOR_LENGTH
;
37176 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
37179 case PRAGMA_OACC_CLAUSE_WAIT
:
37180 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
37183 case PRAGMA_OACC_CLAUSE_WORKER
:
37185 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
37190 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
37196 if (((mask
>> c_kind
) & 1) == 0)
37198 /* Remove the invalid clause(s) from the list to avoid
37199 confusing the rest of the compiler. */
37201 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
37206 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37209 return finish_omp_clauses (clauses
, C_ORT_ACC
);
37214 /* Parse all OpenMP clauses. The set clauses allowed by the directive
37215 is a bitmask in MASK. Return the list of clauses found.
37216 FINISH_P set if finish_omp_clauses should be called.
37217 NESTED non-zero if clauses should be terminated by closing paren instead
37218 of end of pragma. If it is 2, additionally commas are required in between
37222 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
37223 const char *where
, cp_token
*pragma_tok
,
37224 bool finish_p
= true, int nested
= 0)
37226 tree clauses
= NULL
;
37228 cp_token
*token
= NULL
;
37230 /* Don't create location wrapper nodes within OpenMP clauses. */
37231 auto_suppress_location_wrappers sentinel
;
37233 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37235 pragma_omp_clause c_kind
;
37236 const char *c_name
;
37237 tree prev
= clauses
;
37239 if (nested
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
37244 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37245 cp_lexer_consume_token (parser
->lexer
);
37246 else if (nested
== 2)
37247 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37248 "clauses in %<simd%> trait should be separated "
37252 token
= cp_lexer_peek_token (parser
->lexer
);
37253 c_kind
= cp_parser_omp_clause_name (parser
);
37257 case PRAGMA_OMP_CLAUSE_BIND
:
37258 clauses
= cp_parser_omp_clause_bind (parser
, clauses
,
37262 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
37263 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
37265 c_name
= "collapse";
37267 case PRAGMA_OMP_CLAUSE_COPYIN
:
37268 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
37271 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
37272 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
37274 c_name
= "copyprivate";
37276 case PRAGMA_OMP_CLAUSE_DEFAULT
:
37277 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
37278 token
->location
, false);
37279 c_name
= "default";
37281 case PRAGMA_OMP_CLAUSE_FINAL
:
37282 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
37285 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
37286 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
37288 c_name
= "firstprivate";
37290 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
37291 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
37293 c_name
= "grainsize";
37295 case PRAGMA_OMP_CLAUSE_HINT
:
37296 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
37300 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
37301 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
37303 c_name
= "defaultmap";
37305 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
37306 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
37308 c_name
= "use_device_ptr";
37310 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
:
37311 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_ADDR
,
37313 c_name
= "use_device_addr";
37315 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
37316 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
37318 c_name
= "is_device_ptr";
37320 case PRAGMA_OMP_CLAUSE_IF
:
37321 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
37325 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
37327 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
37329 c_name
= "in_reduction";
37331 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
37332 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
37333 c_name
= "lastprivate";
37335 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
37336 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
37338 c_name
= "mergeable";
37340 case PRAGMA_OMP_CLAUSE_NOWAIT
:
37341 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
,
37345 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
37346 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
37348 c_name
= "num_tasks";
37350 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
37351 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
37353 c_name
= "num_threads";
37355 case PRAGMA_OMP_CLAUSE_ORDER
:
37356 clauses
= cp_parser_omp_clause_order (parser
, clauses
,
37360 case PRAGMA_OMP_CLAUSE_ORDERED
:
37361 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
37363 c_name
= "ordered";
37365 case PRAGMA_OMP_CLAUSE_PRIORITY
:
37366 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
37368 c_name
= "priority";
37370 case PRAGMA_OMP_CLAUSE_PRIVATE
:
37371 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
37373 c_name
= "private";
37375 case PRAGMA_OMP_CLAUSE_REDUCTION
:
37377 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
37379 c_name
= "reduction";
37381 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
37382 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
37384 c_name
= "schedule";
37386 case PRAGMA_OMP_CLAUSE_SHARED
:
37387 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
37391 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
37393 = cp_parser_omp_clause_reduction (parser
,
37394 OMP_CLAUSE_TASK_REDUCTION
,
37396 c_name
= "task_reduction";
37398 case PRAGMA_OMP_CLAUSE_UNTIED
:
37399 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
37403 case PRAGMA_OMP_CLAUSE_INBRANCH
:
37404 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
37405 clauses
, token
->location
);
37406 c_name
= "inbranch";
37408 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
37409 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
37411 c_name
= "nontemporal";
37413 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
37414 clauses
= cp_parser_omp_clause_branch (parser
,
37415 OMP_CLAUSE_NOTINBRANCH
,
37416 clauses
, token
->location
);
37417 c_name
= "notinbranch";
37419 case PRAGMA_OMP_CLAUSE_PARALLEL
:
37420 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
37421 clauses
, token
->location
);
37422 c_name
= "parallel";
37426 error_at (token
->location
, "%qs must be the first clause of %qs",
37431 case PRAGMA_OMP_CLAUSE_FOR
:
37432 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
37433 clauses
, token
->location
);
37436 goto clause_not_first
;
37438 case PRAGMA_OMP_CLAUSE_SECTIONS
:
37439 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
37440 clauses
, token
->location
);
37441 c_name
= "sections";
37443 goto clause_not_first
;
37445 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
37446 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
37447 clauses
, token
->location
);
37448 c_name
= "taskgroup";
37450 goto clause_not_first
;
37452 case PRAGMA_OMP_CLAUSE_LINK
:
37453 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
37456 case PRAGMA_OMP_CLAUSE_TO
:
37457 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
37458 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
37461 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
37464 case PRAGMA_OMP_CLAUSE_FROM
:
37465 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
37468 case PRAGMA_OMP_CLAUSE_UNIFORM
:
37469 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
37471 c_name
= "uniform";
37473 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
37474 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
37476 c_name
= "num_teams";
37478 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
37479 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
37481 c_name
= "thread_limit";
37483 case PRAGMA_OMP_CLAUSE_ALIGNED
:
37484 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
37485 c_name
= "aligned";
37487 case PRAGMA_OMP_CLAUSE_LINEAR
:
37489 bool declare_simd
= false;
37490 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
37491 declare_simd
= true;
37492 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
37496 case PRAGMA_OMP_CLAUSE_DEPEND
:
37497 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
37501 case PRAGMA_OMP_CLAUSE_MAP
:
37502 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
37505 case PRAGMA_OMP_CLAUSE_DEVICE
:
37506 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
37510 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
37511 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
37513 c_name
= "dist_schedule";
37515 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
37516 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
37518 c_name
= "proc_bind";
37520 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE
:
37521 clauses
= cp_parser_omp_clause_device_type (parser
, clauses
,
37523 c_name
= "device_type";
37525 case PRAGMA_OMP_CLAUSE_SAFELEN
:
37526 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
37528 c_name
= "safelen";
37530 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
37531 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
37533 c_name
= "simdlen";
37535 case PRAGMA_OMP_CLAUSE_NOGROUP
:
37536 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
37538 c_name
= "nogroup";
37540 case PRAGMA_OMP_CLAUSE_THREADS
:
37542 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
37543 clauses
, token
->location
);
37544 c_name
= "threads";
37546 case PRAGMA_OMP_CLAUSE_SIMD
:
37548 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
37549 clauses
, token
->location
);
37553 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
37559 if (((mask
>> c_kind
) & 1) == 0)
37561 /* Remove the invalid clause(s) from the list to avoid
37562 confusing the rest of the compiler. */
37564 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
37569 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37572 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
37573 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
37575 return finish_omp_clauses (clauses
, C_ORT_OMP
);
37584 In practice, we're also interested in adding the statement to an
37585 outer node. So it is convenient if we work around the fact that
37586 cp_parser_statement calls add_stmt. */
37589 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
37591 unsigned save
= parser
->in_statement
;
37593 /* Only move the values to IN_OMP_BLOCK if they weren't false.
37594 This preserves the "not within loop or switch" style error messages
37595 for nonsense cases like
37601 if (parser
->in_statement
)
37602 parser
->in_statement
= IN_OMP_BLOCK
;
37608 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
37610 parser
->in_statement
= save
;
37614 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
37616 tree stmt
= begin_omp_structured_block ();
37617 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
37619 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
37621 cp_parser_end_omp_structured_block (parser
, save
);
37622 return finish_omp_structured_block (stmt
);
37626 # pragma omp atomic new-line
37630 x binop= expr | x++ | ++x | x-- | --x
37632 +, *, -, /, &, ^, |, <<, >>
37634 where x is an lvalue expression with scalar type.
37637 # pragma omp atomic new-line
37640 # pragma omp atomic read new-line
37643 # pragma omp atomic write new-line
37646 # pragma omp atomic update new-line
37649 # pragma omp atomic capture new-line
37652 # pragma omp atomic capture new-line
37660 expression-stmt | x = x binop expr
37662 v = expression-stmt
37664 { v = x; update-stmt; } | { update-stmt; v = x; }
37668 expression-stmt | x = x binop expr | x = expr binop x
37672 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
37674 where x and v are lvalue expressions with scalar type. */
37677 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
37679 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
37680 tree rhs1
= NULL_TREE
, orig_lhs
;
37681 location_t loc
= pragma_tok
->location
;
37682 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
37683 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
37684 bool structured_block
= false;
37686 tree clauses
= NULL_TREE
;
37688 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
37690 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37691 cp_lexer_consume_token (parser
->lexer
);
37695 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37697 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37698 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37699 const char *p
= IDENTIFIER_POINTER (id
);
37700 enum tree_code new_code
= ERROR_MARK
;
37701 enum omp_memory_order new_memory_order
37702 = OMP_MEMORY_ORDER_UNSPECIFIED
;
37704 if (!strcmp (p
, "read"))
37705 new_code
= OMP_ATOMIC_READ
;
37706 else if (!strcmp (p
, "write"))
37707 new_code
= NOP_EXPR
;
37708 else if (!strcmp (p
, "update"))
37709 new_code
= OMP_ATOMIC
;
37710 else if (!strcmp (p
, "capture"))
37711 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
37712 else if (!strcmp (p
, "seq_cst"))
37713 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
37714 else if (!strcmp (p
, "acq_rel"))
37715 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
37716 else if (!strcmp (p
, "release"))
37717 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
37718 else if (!strcmp (p
, "acquire"))
37719 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
37720 else if (!strcmp (p
, "relaxed"))
37721 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
37722 else if (!strcmp (p
, "hint"))
37724 cp_lexer_consume_token (parser
->lexer
);
37725 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
37731 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
37732 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
37733 "%<release%>, %<relaxed%> or %<hint%> clause");
37737 if (new_code
!= ERROR_MARK
)
37739 if (code
!= ERROR_MARK
)
37740 error_at (cloc
, "too many atomic clauses");
37744 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
37746 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
37747 error_at (cloc
, "too many memory order clauses");
37749 memory_order
= new_memory_order
;
37751 cp_lexer_consume_token (parser
->lexer
);
37757 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37759 if (code
== ERROR_MARK
)
37761 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
37764 = (enum omp_requires
) (omp_requires_mask
37765 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
37766 switch ((enum omp_memory_order
)
37767 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
37769 case OMP_MEMORY_ORDER_UNSPECIFIED
:
37770 case OMP_MEMORY_ORDER_RELAXED
:
37771 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
37773 case OMP_MEMORY_ORDER_SEQ_CST
:
37774 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
37776 case OMP_MEMORY_ORDER_ACQ_REL
:
37779 case OMP_ATOMIC_READ
:
37780 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
37782 case NOP_EXPR
: /* atomic write */
37784 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
37787 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
37792 gcc_unreachable ();
37798 case OMP_ATOMIC_READ
:
37799 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
37800 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
37802 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
37803 "%<acq_rel%> or %<release%> clauses");
37804 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
37807 case NOP_EXPR
: /* atomic write */
37808 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
37809 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
37811 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
37812 "%<acq_rel%> or %<acquire%> clauses");
37813 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
37817 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
37818 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
37820 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
37821 "%<acq_rel%> or %<acquire%> clauses");
37822 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
37831 case OMP_ATOMIC_READ
:
37832 case NOP_EXPR
: /* atomic write */
37833 v
= cp_parser_unary_expression (parser
);
37834 if (v
== error_mark_node
)
37836 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
37838 if (code
== NOP_EXPR
)
37839 lhs
= cp_parser_expression (parser
);
37841 lhs
= cp_parser_unary_expression (parser
);
37842 if (lhs
== error_mark_node
)
37844 if (code
== NOP_EXPR
)
37846 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
37854 case OMP_ATOMIC_CAPTURE_NEW
:
37855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
37857 cp_lexer_consume_token (parser
->lexer
);
37858 structured_block
= true;
37862 v
= cp_parser_unary_expression (parser
);
37863 if (v
== error_mark_node
)
37865 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
37873 lhs
= cp_parser_unary_expression (parser
);
37875 switch (TREE_CODE (lhs
))
37880 case POSTINCREMENT_EXPR
:
37881 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
37882 code
= OMP_ATOMIC_CAPTURE_OLD
;
37884 case PREINCREMENT_EXPR
:
37885 lhs
= TREE_OPERAND (lhs
, 0);
37886 opcode
= PLUS_EXPR
;
37887 rhs
= integer_one_node
;
37890 case POSTDECREMENT_EXPR
:
37891 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
37892 code
= OMP_ATOMIC_CAPTURE_OLD
;
37894 case PREDECREMENT_EXPR
:
37895 lhs
= TREE_OPERAND (lhs
, 0);
37896 opcode
= MINUS_EXPR
;
37897 rhs
= integer_one_node
;
37900 case COMPOUND_EXPR
:
37901 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
37902 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
37903 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
37904 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
37905 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
37906 (TREE_OPERAND (lhs
, 1), 0), 0)))
37908 /* Undo effects of boolean_increment for post {in,de}crement. */
37909 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
37912 if (TREE_CODE (lhs
) == MODIFY_EXPR
37913 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
37915 /* Undo effects of boolean_increment. */
37916 if (integer_onep (TREE_OPERAND (lhs
, 1)))
37918 /* This is pre or post increment. */
37919 rhs
= TREE_OPERAND (lhs
, 1);
37920 lhs
= TREE_OPERAND (lhs
, 0);
37922 if (code
== OMP_ATOMIC_CAPTURE_NEW
37923 && !structured_block
37924 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
37925 code
= OMP_ATOMIC_CAPTURE_OLD
;
37931 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
37934 opcode
= MULT_EXPR
;
37937 opcode
= TRUNC_DIV_EXPR
;
37940 opcode
= PLUS_EXPR
;
37943 opcode
= MINUS_EXPR
;
37945 case CPP_LSHIFT_EQ
:
37946 opcode
= LSHIFT_EXPR
;
37948 case CPP_RSHIFT_EQ
:
37949 opcode
= RSHIFT_EXPR
;
37952 opcode
= BIT_AND_EXPR
;
37955 opcode
= BIT_IOR_EXPR
;
37958 opcode
= BIT_XOR_EXPR
;
37961 enum cp_parser_prec oprec
;
37963 cp_lexer_consume_token (parser
->lexer
);
37964 cp_parser_parse_tentatively (parser
);
37965 rhs1
= cp_parser_simple_cast_expression (parser
);
37966 if (rhs1
== error_mark_node
)
37968 cp_parser_abort_tentative_parse (parser
);
37969 cp_parser_simple_cast_expression (parser
);
37972 token
= cp_lexer_peek_token (parser
->lexer
);
37973 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
37975 cp_parser_abort_tentative_parse (parser
);
37976 cp_parser_parse_tentatively (parser
);
37977 rhs
= cp_parser_binary_expression (parser
, false, true,
37978 PREC_NOT_OPERATOR
, NULL
);
37979 if (rhs
== error_mark_node
)
37981 cp_parser_abort_tentative_parse (parser
);
37982 cp_parser_binary_expression (parser
, false, true,
37983 PREC_NOT_OPERATOR
, NULL
);
37986 switch (TREE_CODE (rhs
))
37989 case TRUNC_DIV_EXPR
:
37998 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
38000 if (cp_parser_parse_definitely (parser
))
38002 opcode
= TREE_CODE (rhs
);
38003 rhs1
= TREE_OPERAND (rhs
, 0);
38004 rhs
= TREE_OPERAND (rhs
, 1);
38014 cp_parser_abort_tentative_parse (parser
);
38015 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
38017 rhs
= cp_parser_expression (parser
);
38018 if (rhs
== error_mark_node
)
38024 cp_parser_error (parser
,
38025 "invalid form of %<#pragma omp atomic%>");
38028 if (!cp_parser_parse_definitely (parser
))
38030 switch (token
->type
)
38032 case CPP_SEMICOLON
:
38033 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
38035 code
= OMP_ATOMIC_CAPTURE_OLD
;
38040 cp_lexer_consume_token (parser
->lexer
);
38043 else if (structured_block
)
38050 cp_parser_error (parser
,
38051 "invalid form of %<#pragma omp atomic%>");
38054 opcode
= MULT_EXPR
;
38057 opcode
= TRUNC_DIV_EXPR
;
38060 opcode
= PLUS_EXPR
;
38063 opcode
= MINUS_EXPR
;
38066 opcode
= LSHIFT_EXPR
;
38069 opcode
= RSHIFT_EXPR
;
38072 opcode
= BIT_AND_EXPR
;
38075 opcode
= BIT_IOR_EXPR
;
38078 opcode
= BIT_XOR_EXPR
;
38081 cp_parser_error (parser
,
38082 "invalid operator for %<#pragma omp atomic%>");
38085 oprec
= TOKEN_PRECEDENCE (token
);
38086 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
38087 if (commutative_tree_code (opcode
))
38088 oprec
= (enum cp_parser_prec
) (oprec
- 1);
38089 cp_lexer_consume_token (parser
->lexer
);
38090 rhs
= cp_parser_binary_expression (parser
, false, false,
38092 if (rhs
== error_mark_node
)
38097 cp_parser_error (parser
,
38098 "invalid operator for %<#pragma omp atomic%>");
38101 cp_lexer_consume_token (parser
->lexer
);
38103 rhs
= cp_parser_expression (parser
);
38104 if (rhs
== error_mark_node
)
38109 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
38111 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
38113 v
= cp_parser_unary_expression (parser
);
38114 if (v
== error_mark_node
)
38116 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38118 lhs1
= cp_parser_unary_expression (parser
);
38119 if (lhs1
== error_mark_node
)
38122 if (structured_block
)
38124 cp_parser_consume_semicolon_at_end_of_statement (parser
);
38125 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
38128 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38129 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
38130 rhs1
, clauses
, memory_order
);
38131 if (!structured_block
)
38132 cp_parser_consume_semicolon_at_end_of_statement (parser
);
38136 cp_parser_skip_to_end_of_block_or_statement (parser
);
38137 if (structured_block
)
38139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
38140 cp_lexer_consume_token (parser
->lexer
);
38141 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
38143 cp_parser_skip_to_end_of_block_or_statement (parser
);
38144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
38145 cp_lexer_consume_token (parser
->lexer
);
38152 # pragma omp barrier new-line */
38155 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
38157 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38158 finish_omp_barrier ();
38162 # pragma omp critical [(name)] new-line
38166 # pragma omp critical [(name) [hint(expression)]] new-line
38167 structured-block */
38169 #define OMP_CRITICAL_CLAUSE_MASK \
38170 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
38173 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
38175 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
38177 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38179 matching_parens parens
;
38180 parens
.consume_open (parser
);
38182 name
= cp_parser_identifier (parser
);
38184 if (name
== error_mark_node
38185 || !parens
.require_close (parser
))
38186 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38187 /*or_comma=*/false,
38188 /*consume_paren=*/true);
38189 if (name
== error_mark_node
)
38192 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
38193 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
38194 cp_lexer_consume_token (parser
->lexer
);
38196 clauses
= cp_parser_omp_all_clauses (parser
,
38197 OMP_CRITICAL_CLAUSE_MASK
,
38198 "#pragma omp critical", pragma_tok
);
38201 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38203 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
38204 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
38208 # pragma omp depobj ( depobj ) depobj-clause new-line
38211 depend (dependence-type : locator)
38213 update (dependence-type)
38222 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
38224 location_t loc
= pragma_tok
->location
;
38225 matching_parens parens
;
38226 if (!parens
.require_open (parser
))
38228 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38232 tree depobj
= cp_parser_assignment_expression (parser
);
38234 if (!parens
.require_close (parser
))
38235 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38236 /*or_comma=*/false,
38237 /*consume_paren=*/true);
38239 tree clause
= NULL_TREE
;
38240 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
38241 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38242 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38244 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38245 const char *p
= IDENTIFIER_POINTER (id
);
38247 cp_lexer_consume_token (parser
->lexer
);
38248 if (!strcmp ("depend", p
))
38250 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
38252 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
38254 clause
= error_mark_node
;
38256 else if (!strcmp ("destroy", p
))
38257 kind
= OMP_CLAUSE_DEPEND_LAST
;
38258 else if (!strcmp ("update", p
))
38260 matching_parens c_parens
;
38261 if (c_parens
.require_open (parser
))
38264 = cp_lexer_peek_token (parser
->lexer
)->location
;
38265 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38267 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38268 const char *p2
= IDENTIFIER_POINTER (id2
);
38270 cp_lexer_consume_token (parser
->lexer
);
38271 if (!strcmp ("in", p2
))
38272 kind
= OMP_CLAUSE_DEPEND_IN
;
38273 else if (!strcmp ("out", p2
))
38274 kind
= OMP_CLAUSE_DEPEND_OUT
;
38275 else if (!strcmp ("inout", p2
))
38276 kind
= OMP_CLAUSE_DEPEND_INOUT
;
38277 else if (!strcmp ("mutexinoutset", p2
))
38278 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
38280 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
38282 clause
= error_mark_node
;
38283 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
38284 "%<mutexinoutset%>");
38286 if (!c_parens
.require_close (parser
))
38287 cp_parser_skip_to_closing_parenthesis (parser
,
38288 /*recovering=*/true,
38289 /*or_comma=*/false,
38290 /*consume_paren=*/true);
38293 clause
= error_mark_node
;
38296 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
38298 clause
= error_mark_node
;
38299 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
38301 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38303 finish_omp_depobj (loc
, depobj
, kind
, clause
);
38308 # pragma omp flush flush-vars[opt] new-line
38314 # pragma omp flush memory-order-clause new-line */
38317 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
38319 enum memmodel mo
= MEMMODEL_LAST
;
38320 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38322 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38323 const char *p
= IDENTIFIER_POINTER (id
);
38324 if (!strcmp (p
, "acq_rel"))
38325 mo
= MEMMODEL_ACQ_REL
;
38326 else if (!strcmp (p
, "release"))
38327 mo
= MEMMODEL_RELEASE
;
38328 else if (!strcmp (p
, "acquire"))
38329 mo
= MEMMODEL_ACQUIRE
;
38331 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38332 "expected %<acq_rel%>, %<release%> or %<acquire%>");
38333 cp_lexer_consume_token (parser
->lexer
);
38335 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
38337 if (mo
!= MEMMODEL_LAST
)
38338 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38339 "%<flush%> list specified together with memory order "
38341 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
38343 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38345 finish_omp_flush (mo
);
38348 /* Helper function, to parse omp for increment expression. */
38351 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
38353 tree cond
= cp_parser_binary_expression (parser
, false, true,
38354 PREC_NOT_OPERATOR
, NULL
);
38355 if (cond
== error_mark_node
38356 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
38358 cp_parser_skip_to_end_of_statement (parser
);
38359 return error_mark_node
;
38362 switch (TREE_CODE (cond
))
38370 if (code
!= OACC_LOOP
)
38372 gcc_fallthrough ();
38374 return error_mark_node
;
38377 /* If decl is an iterator, preserve LHS and RHS of the relational
38378 expr until finish_omp_for. */
38380 && (type_dependent_expression_p (decl
)
38381 || CLASS_TYPE_P (TREE_TYPE (decl
))))
38384 return build_x_binary_op (cp_expr_loc_or_input_loc (cond
),
38386 TREE_OPERAND (cond
, 0), ERROR_MARK
,
38387 TREE_OPERAND (cond
, 1), ERROR_MARK
,
38388 /*overload=*/NULL
, tf_warning_or_error
);
38391 /* Helper function, to parse omp for increment expression. */
38394 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
38396 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38402 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
38404 op
= (token
->type
== CPP_PLUS_PLUS
38405 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
38406 cp_lexer_consume_token (parser
->lexer
);
38407 lhs
= cp_parser_simple_cast_expression (parser
);
38409 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
38410 return error_mark_node
;
38411 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
38414 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
38416 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
38417 return error_mark_node
;
38419 token
= cp_lexer_peek_token (parser
->lexer
);
38420 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
38422 op
= (token
->type
== CPP_PLUS_PLUS
38423 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
38424 cp_lexer_consume_token (parser
->lexer
);
38425 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
38428 op
= cp_parser_assignment_operator_opt (parser
);
38429 if (op
== ERROR_MARK
)
38430 return error_mark_node
;
38432 if (op
!= NOP_EXPR
)
38434 rhs
= cp_parser_assignment_expression (parser
);
38435 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
38436 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
38439 lhs
= cp_parser_binary_expression (parser
, false, false,
38440 PREC_ADDITIVE_EXPRESSION
, NULL
);
38441 token
= cp_lexer_peek_token (parser
->lexer
);
38442 decl_first
= (lhs
== decl
38443 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
38446 if (token
->type
!= CPP_PLUS
38447 && token
->type
!= CPP_MINUS
)
38448 return error_mark_node
;
38452 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
38453 cp_lexer_consume_token (parser
->lexer
);
38454 rhs
= cp_parser_binary_expression (parser
, false, false,
38455 PREC_ADDITIVE_EXPRESSION
, NULL
);
38456 token
= cp_lexer_peek_token (parser
->lexer
);
38457 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
38459 if (lhs
== NULL_TREE
)
38461 if (op
== PLUS_EXPR
)
38464 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
38465 tf_warning_or_error
);
38468 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
38469 ERROR_MARK
, NULL
, tf_warning_or_error
);
38472 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
38477 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
38478 || op
== MINUS_EXPR
)
38479 return error_mark_node
;
38480 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
38483 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
38485 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
38488 /* Parse the initialization statement of an OpenMP for loop.
38490 Return true if the resulting construct should have an
38491 OMP_CLAUSE_PRIVATE added to it. */
38494 cp_parser_omp_for_loop_init (cp_parser
*parser
,
38495 tree
&this_pre_body
,
38496 releasing_vec
&for_block
,
38502 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
38505 tree add_private_clause
= NULL_TREE
;
38507 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
38511 integer-type var = lb
38512 random-access-iterator-type var = lb
38513 pointer-type var = lb
38515 cp_decl_specifier_seq type_specifiers
;
38517 /* First, try to parse as an initialized declaration. See
38518 cp_parser_condition, from whence the bulk of this is copied. */
38520 cp_parser_parse_tentatively (parser
);
38521 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
38522 /*is_declaration=*/true,
38523 /*is_trailing_return=*/false,
38525 if (cp_parser_parse_definitely (parser
))
38527 /* If parsing a type specifier seq succeeded, then this
38528 MUST be a initialized declaration. */
38529 tree asm_specification
, attributes
;
38530 cp_declarator
*declarator
;
38532 declarator
= cp_parser_declarator (parser
,
38533 CP_PARSER_DECLARATOR_NAMED
,
38534 CP_PARSER_FLAGS_NONE
,
38535 /*ctor_dtor_or_conv_p=*/NULL
,
38536 /*parenthesized_p=*/NULL
,
38537 /*member_p=*/false,
38538 /*friend_p=*/false,
38539 /*static_p=*/false);
38540 attributes
= cp_parser_attributes_opt (parser
);
38541 asm_specification
= cp_parser_asm_specification_opt (parser
);
38543 if (declarator
== cp_error_declarator
)
38544 cp_parser_skip_to_end_of_statement (parser
);
38548 tree pushed_scope
, auto_node
;
38550 decl
= start_decl (declarator
, &type_specifiers
,
38551 SD_INITIALIZED
, attributes
,
38552 /*prefix_attributes=*/NULL_TREE
,
38555 auto_node
= type_uses_auto (TREE_TYPE (decl
));
38556 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
38558 if (cp_lexer_next_token_is (parser
->lexer
,
38560 error ("parenthesized initialization is not allowed in "
38561 "OpenMP %<for%> loop");
38563 /* Trigger an error. */
38564 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
38566 init
= error_mark_node
;
38567 cp_parser_skip_to_end_of_statement (parser
);
38569 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
38570 || type_dependent_expression_p (decl
)
38573 bool is_direct_init
, is_non_constant_init
;
38575 init
= cp_parser_initializer (parser
,
38577 &is_non_constant_init
);
38582 = do_auto_deduction (TREE_TYPE (decl
), init
,
38585 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
38586 && !type_dependent_expression_p (decl
))
38590 cp_finish_decl (decl
, init
, !is_non_constant_init
,
38592 LOOKUP_ONLYCONVERTING
);
38594 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
38596 vec_safe_push (for_block
, this_pre_body
);
38601 init
= pop_stmt_list (this_pre_body
);
38602 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
38604 tree_stmt_iterator i
= tsi_start (init
);
38605 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
38606 while (!tsi_end_p (i
))
38608 tree t
= tsi_stmt (i
);
38609 if (TREE_CODE (t
) == DECL_EXPR
38610 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
38613 vec_safe_push (for_block
, t
);
38618 if (tsi_one_before_end_p (i
))
38620 tree t
= tsi_stmt (i
);
38622 free_stmt_list (init
);
38627 this_pre_body
= NULL_TREE
;
38632 cp_lexer_consume_token (parser
->lexer
);
38633 init
= cp_parser_assignment_expression (parser
);
38636 if (TYPE_REF_P (TREE_TYPE (decl
)))
38637 init
= error_mark_node
;
38639 cp_finish_decl (decl
, NULL_TREE
,
38640 /*init_const_expr_p=*/false,
38642 LOOKUP_ONLYCONVERTING
);
38646 pop_scope (pushed_scope
);
38652 /* If parsing a type specifier sequence failed, then
38653 this MUST be a simple expression. */
38654 cp_parser_parse_tentatively (parser
);
38655 decl
= cp_parser_primary_expression (parser
, false, false,
38657 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
38658 if (!cp_parser_error_occurred (parser
)
38660 && (TREE_CODE (decl
) == COMPONENT_REF
38661 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
38663 cp_parser_abort_tentative_parse (parser
);
38664 cp_parser_parse_tentatively (parser
);
38665 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
38666 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
38667 /*check_dependency_p=*/true,
38668 /*template_p=*/NULL
,
38669 /*declarator_p=*/false,
38670 /*optional_p=*/false);
38671 if (name
!= error_mark_node
38672 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
38674 decl
= cp_parser_lookup_name_simple (parser
, name
,
38676 if (TREE_CODE (decl
) == FIELD_DECL
)
38677 add_private_clause
= omp_privatize_field (decl
, false);
38679 cp_parser_abort_tentative_parse (parser
);
38680 cp_parser_parse_tentatively (parser
);
38681 decl
= cp_parser_primary_expression (parser
, false, false,
38684 if (!cp_parser_error_occurred (parser
)
38687 && CLASS_TYPE_P (TREE_TYPE (decl
)))
38691 cp_parser_parse_definitely (parser
);
38692 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
38693 rhs
= cp_parser_assignment_expression (parser
);
38695 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
38698 tf_warning_or_error
));
38699 if (!add_private_clause
)
38700 add_private_clause
= decl
;
38705 cp_parser_abort_tentative_parse (parser
);
38706 init
= cp_parser_expression (parser
);
38709 if (TREE_CODE (init
) == MODIFY_EXPR
38710 || TREE_CODE (init
) == MODOP_EXPR
)
38711 real_decl
= TREE_OPERAND (init
, 0);
38715 return add_private_clause
;
38718 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
38721 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
38722 tree
&decl
, tree
&orig_decl
, tree
&init
,
38723 tree
&orig_init
, tree
&cond
, tree
&incr
)
38725 tree begin
, end
, range_temp_decl
= NULL_TREE
;
38726 tree iter_type
, begin_expr
, end_expr
;
38728 if (processing_template_decl
)
38730 if (check_for_bare_parameter_packs (init
))
38731 init
= error_mark_node
;
38732 if (!type_dependent_expression_p (init
)
38733 /* do_auto_deduction doesn't mess with template init-lists. */
38734 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
38737 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
38739 tree v
= DECL_VALUE_EXPR (decl
);
38740 if (TREE_CODE (v
) == ARRAY_REF
38741 && VAR_P (TREE_OPERAND (v
, 0))
38742 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
38743 d
= TREE_OPERAND (v
, 0);
38745 do_range_for_auto_deduction (d
, init
);
38747 cond
= global_namespace
;
38751 this_pre_body
= pop_stmt_list (this_pre_body
);
38755 init
= mark_lvalue_use (init
);
38757 if (decl
== error_mark_node
|| init
== error_mark_node
)
38758 /* If an error happened previously do nothing or else a lot of
38759 unhelpful errors would be issued. */
38760 begin_expr
= end_expr
= iter_type
= error_mark_node
;
38766 && array_of_runtime_bound_p (TREE_TYPE (init
)))
38767 /* Can't bind a reference to an array of runtime bound. */
38771 range_temp
= build_range_temp (init
);
38772 DECL_NAME (range_temp
) = NULL_TREE
;
38773 pushdecl (range_temp
);
38774 cp_finish_decl (range_temp
, init
,
38775 /*is_constant_init*/false, NULL_TREE
,
38776 LOOKUP_ONLYCONVERTING
);
38777 range_temp_decl
= range_temp
;
38778 range_temp
= convert_from_reference (range_temp
);
38780 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
38781 &begin_expr
, &end_expr
);
38784 tree end_iter_type
= iter_type
;
38785 if (cxx_dialect
>= cxx17
)
38786 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
38787 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
38788 TREE_USED (end
) = 1;
38789 DECL_ARTIFICIAL (end
) = 1;
38791 cp_finish_decl (end
, end_expr
,
38792 /*is_constant_init*/false, NULL_TREE
,
38793 LOOKUP_ONLYCONVERTING
);
38795 /* The new for initialization statement. */
38796 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
38797 TREE_USED (begin
) = 1;
38798 DECL_ARTIFICIAL (begin
) = 1;
38801 if (CLASS_TYPE_P (iter_type
))
38806 begin_expr
= NULL_TREE
;
38808 cp_finish_decl (begin
, begin_expr
,
38809 /*is_constant_init*/false, NULL_TREE
,
38810 LOOKUP_ONLYCONVERTING
);
38812 /* The new for condition. */
38813 if (CLASS_TYPE_P (iter_type
))
38814 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
38816 cond
= build_x_binary_op (input_location
, NE_EXPR
,
38819 NULL
, tf_warning_or_error
);
38821 /* The new increment expression. */
38822 if (CLASS_TYPE_P (iter_type
))
38823 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
38825 incr
= finish_unary_op_expr (input_location
,
38826 PREINCREMENT_EXPR
, begin
,
38827 tf_warning_or_error
);
38833 vec_safe_push (for_block
, this_pre_body
);
38834 this_pre_body
= NULL_TREE
;
38837 tree decomp_first_name
= NULL_TREE
;
38838 unsigned decomp_cnt
= 0;
38839 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
38841 tree v
= DECL_VALUE_EXPR (orig_decl
);
38842 if (TREE_CODE (v
) == ARRAY_REF
38843 && VAR_P (TREE_OPERAND (v
, 0))
38844 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
38846 tree d
= orig_decl
;
38847 orig_decl
= TREE_OPERAND (v
, 0);
38848 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
38849 decomp_first_name
= d
;
38853 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
38856 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
38858 if (!error_operand_p (t
))
38859 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
38863 tree v
= make_tree_vec (decomp_cnt
+ 3);
38864 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
38865 TREE_VEC_ELT (v
, 1) = end
;
38866 TREE_VEC_ELT (v
, 2) = orig_decl
;
38867 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
38869 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
38870 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
38872 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
38875 /* Helper for cp_parser_omp_for_loop, finalize part of range for
38876 inside of the collapsed body. */
38879 cp_finish_omp_range_for (tree orig
, tree begin
)
38881 gcc_assert (TREE_CODE (orig
) == TREE_LIST
38882 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
38883 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
38884 tree decomp_first_name
= NULL_TREE
;
38885 unsigned int decomp_cnt
= 0;
38887 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
38889 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
38890 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
38891 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
38894 /* The declaration is initialized with *__begin inside the loop body. */
38895 cp_finish_decl (decl
,
38896 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
38897 tf_warning_or_error
),
38898 /*is_constant_init*/false, NULL_TREE
,
38899 LOOKUP_ONLYCONVERTING
);
38900 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
38901 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
38907 { structured-block scan-directive structured-block } */
38910 cp_parser_omp_scan_loop_body (cp_parser
*parser
)
38912 tree substmt
, clauses
= NULL_TREE
;
38914 matching_braces braces
;
38915 if (!braces
.require_open (parser
))
38918 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
38919 substmt
= build2 (OMP_SCAN
, void_type_node
, substmt
, NULL_TREE
);
38920 add_stmt (substmt
);
38922 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
38923 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SCAN
)
38925 enum omp_clause_code clause
= OMP_CLAUSE_ERROR
;
38927 cp_lexer_consume_token (parser
->lexer
);
38929 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38931 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38932 const char *p
= IDENTIFIER_POINTER (id
);
38933 if (strcmp (p
, "inclusive") == 0)
38934 clause
= OMP_CLAUSE_INCLUSIVE
;
38935 else if (strcmp (p
, "exclusive") == 0)
38936 clause
= OMP_CLAUSE_EXCLUSIVE
;
38938 if (clause
!= OMP_CLAUSE_ERROR
)
38940 cp_lexer_consume_token (parser
->lexer
);
38941 clauses
= cp_parser_omp_var_list (parser
, clause
, NULL_TREE
);
38944 cp_parser_error (parser
, "expected %<inclusive%> or "
38945 "%<exclusive%> clause");
38947 cp_parser_require_pragma_eol (parser
, tok
);
38950 error ("expected %<#pragma omp scan%>");
38952 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
38953 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
38954 substmt
= build2_loc (tok
->location
, OMP_SCAN
, void_type_node
, substmt
,
38956 add_stmt (substmt
);
38958 braces
.require_close (parser
);
38961 /* Parse the restricted form of the for statement allowed by OpenMP. */
38964 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
38965 tree
*cclauses
, bool *if_p
)
38967 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
38969 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
38970 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
38971 location_t loc_first
;
38972 bool collapse_err
= false;
38973 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
38974 releasing_vec for_block
;
38975 auto_vec
<tree
, 4> orig_inits
;
38976 bool tiling
= false;
38977 bool inscan
= false;
38979 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
38980 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
38981 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
38982 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
38985 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
38987 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
38988 && OMP_CLAUSE_ORDERED_EXPR (cl
))
38991 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
38993 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_REDUCTION
38994 && OMP_CLAUSE_REDUCTION_INSCAN (cl
)
38995 && (code
== OMP_SIMD
|| code
== OMP_FOR
))
38998 if (ordered
&& ordered
< collapse
)
39000 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
39001 "%<ordered%> clause parameter is less than %<collapse%>");
39002 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
39003 = build_int_cst (NULL_TREE
, collapse
);
39004 ordered
= collapse
;
39008 for (tree
*pc
= &clauses
; *pc
; )
39009 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
39011 error_at (OMP_CLAUSE_LOCATION (*pc
),
39012 "%<linear%> clause may not be specified together "
39013 "with %<ordered%> clause with a parameter");
39014 *pc
= OMP_CLAUSE_CHAIN (*pc
);
39017 pc
= &OMP_CLAUSE_CHAIN (*pc
);
39020 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
39021 count
= ordered
? ordered
: collapse
;
39023 declv
= make_tree_vec (count
);
39024 initv
= make_tree_vec (count
);
39025 condv
= make_tree_vec (count
);
39026 incrv
= make_tree_vec (count
);
39027 orig_declv
= NULL_TREE
;
39029 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
39031 for (i
= 0; i
< count
; i
++)
39033 int bracecount
= 0;
39034 tree add_private_clause
= NULL_TREE
;
39037 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
39040 cp_parser_error (parser
, "for statement expected");
39043 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
39045 /* Don't create location wrapper nodes within an OpenMP "for"
39047 auto_suppress_location_wrappers sentinel
;
39049 matching_parens parens
;
39050 if (!parens
.require_open (parser
))
39053 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
39054 this_pre_body
= push_stmt_list ();
39056 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
39058 /* Save tokens so that we can put them back. */
39059 cp_lexer_save_tokens (parser
->lexer
);
39061 /* Look for ':' that is not nested in () or {}. */
39063 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
39064 /*recovering=*/false,
39069 /* Roll back the tokens we skipped. */
39070 cp_lexer_rollback_tokens (parser
->lexer
);
39074 bool saved_colon_corrects_to_scope_p
39075 = parser
->colon_corrects_to_scope_p
;
39077 /* A colon is used in range-based for. */
39078 parser
->colon_corrects_to_scope_p
= false;
39080 /* Parse the declaration. */
39081 cp_parser_simple_declaration (parser
,
39082 /*function_definition_allowed_p=*/
39084 parser
->colon_corrects_to_scope_p
39085 = saved_colon_corrects_to_scope_p
;
39087 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
39089 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
39092 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
39093 orig_decl
, init
, orig_init
,
39100 pre_body
= push_stmt_list ();
39102 add_stmt (this_pre_body
);
39103 pre_body
= pop_stmt_list (pre_body
);
39106 pre_body
= this_pre_body
;
39110 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
39111 "%<ordered%> clause with parameter on "
39112 "range-based %<for%> loop");
39114 goto parse_close_paren
;
39119 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
39120 init
, orig_init
, decl
, real_decl
);
39122 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
39125 this_pre_body
= pop_stmt_list (this_pre_body
);
39129 pre_body
= push_stmt_list ();
39131 add_stmt (this_pre_body
);
39132 pre_body
= pop_stmt_list (pre_body
);
39135 pre_body
= this_pre_body
;
39140 if (cclauses
!= NULL
39141 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
39142 && real_decl
!= NULL_TREE
39143 && code
!= OMP_LOOP
)
39146 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
39147 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
39148 && OMP_CLAUSE_DECL (*c
) == real_decl
)
39150 error_at (loc
, "iteration variable %qD"
39151 " should not be firstprivate", real_decl
);
39152 *c
= OMP_CLAUSE_CHAIN (*c
);
39154 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
39155 && OMP_CLAUSE_DECL (*c
) == real_decl
)
39157 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
39159 *c
= OMP_CLAUSE_CHAIN (*c
);
39160 if (code
== OMP_SIMD
)
39162 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
39163 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
39167 OMP_CLAUSE_CHAIN (l
) = clauses
;
39170 add_private_clause
= NULL_TREE
;
39174 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
39175 && OMP_CLAUSE_DECL (*c
) == real_decl
)
39176 add_private_clause
= NULL_TREE
;
39177 c
= &OMP_CLAUSE_CHAIN (*c
);
39181 if (add_private_clause
)
39184 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
39186 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
39187 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
39188 && OMP_CLAUSE_DECL (c
) == decl
)
39190 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
39191 && OMP_CLAUSE_DECL (c
) == decl
)
39192 error_at (loc
, "iteration variable %qD "
39193 "should not be firstprivate",
39195 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
39196 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
39197 && OMP_CLAUSE_DECL (c
) == decl
)
39198 error_at (loc
, "iteration variable %qD should not be reduction",
39203 if ((code
== OMP_SIMD
&& collapse
!= 1) || code
== OMP_LOOP
)
39204 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
39205 else if (code
!= OMP_SIMD
)
39206 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
39208 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
39209 OMP_CLAUSE_DECL (c
) = add_private_clause
;
39210 c
= finish_omp_clauses (c
, C_ORT_OMP
);
39213 OMP_CLAUSE_CHAIN (c
) = clauses
;
39215 /* For linear, signal that we need to fill up
39216 the so far unknown linear step. */
39217 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
39218 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
39224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
39225 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
39226 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
39229 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
39231 /* If decl is an iterator, preserve the operator on decl
39232 until finish_omp_for. */
39234 && ((processing_template_decl
39235 && (TREE_TYPE (real_decl
) == NULL_TREE
39236 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
39237 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
39238 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
39240 incr
= cp_parser_expression (parser
);
39241 protected_set_expr_location_if_unset (incr
, input_location
);
39245 if (!parens
.require_close (parser
))
39246 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39247 /*or_comma=*/false,
39248 /*consume_paren=*/true);
39250 TREE_VEC_ELT (declv
, i
) = decl
;
39251 TREE_VEC_ELT (initv
, i
) = init
;
39252 TREE_VEC_ELT (condv
, i
) = cond
;
39253 TREE_VEC_ELT (incrv
, i
) = incr
;
39256 orig_inits
.safe_grow_cleared (i
+ 1);
39257 orig_inits
[i
] = orig_init
;
39262 orig_declv
= copy_node (declv
);
39263 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
39265 else if (orig_declv
)
39266 TREE_VEC_ELT (orig_declv
, i
) = decl
;
39268 if (i
== count
- 1)
39271 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
39272 in between the collapsed for loops to be still considered perfectly
39273 nested. Hopefully the final version clarifies this.
39274 For now handle (multiple) {'s and empty statements. */
39275 cp_parser_parse_tentatively (parser
);
39278 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
39280 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
39282 cp_lexer_consume_token (parser
->lexer
);
39285 else if (bracecount
39286 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
39287 cp_lexer_consume_token (parser
->lexer
);
39290 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39291 error_at (loc
, "not enough for loops to collapse");
39292 collapse_err
= true;
39293 cp_parser_abort_tentative_parse (parser
);
39301 cp_parser_parse_definitely (parser
);
39302 nbraces
+= bracecount
;
39309 /* Note that we saved the original contents of this flag when we entered
39310 the structured block, and so we don't need to re-save it here. */
39311 parser
->in_statement
= IN_OMP_FOR
;
39313 /* Note that the grammar doesn't call for a structured block here,
39314 though the loop as a whole is a structured block. */
39317 body
= begin_omp_structured_block ();
39318 for (i
= 0; i
< count
; i
++)
39319 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
39320 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
39321 TREE_VEC_ELT (declv
, i
));
39324 body
= push_stmt_list ();
39326 cp_parser_omp_scan_loop_body (parser
);
39328 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39330 body
= finish_omp_structured_block (body
);
39332 body
= pop_stmt_list (body
);
39334 if (declv
== NULL_TREE
)
39337 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
39338 incrv
, body
, pre_body
, &orig_inits
, clauses
);
39342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
39344 cp_lexer_consume_token (parser
->lexer
);
39347 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
39348 cp_lexer_consume_token (parser
->lexer
);
39353 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39354 "collapsed loops not perfectly nested");
39356 collapse_err
= true;
39357 cp_parser_statement_seq_opt (parser
, NULL
);
39358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
39363 while (!for_block
->is_empty ())
39365 tree t
= for_block
->pop ();
39366 if (TREE_CODE (t
) == STATEMENT_LIST
)
39367 add_stmt (pop_stmt_list (t
));
39375 /* Helper function for OpenMP parsing, split clauses and call
39376 finish_omp_clauses on each of the set of clauses afterwards. */
39379 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
39380 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
39383 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
39384 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
39386 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
39390 #pragma omp loop loop-clause[optseq] new-line
39393 #define OMP_LOOP_CLAUSE_MASK \
39394 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
39399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39402 cp_parser_omp_loop (cp_parser
*parser
, cp_token
*pragma_tok
,
39403 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39406 tree clauses
, sb
, ret
;
39408 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39410 strcat (p_name
, " loop");
39411 mask
|= OMP_LOOP_CLAUSE_MASK
;
39413 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39417 cp_omp_split_clauses (loc
, OMP_LOOP
, mask
, clauses
, cclauses
);
39418 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_LOOP
];
39421 keep_next_level (true);
39422 sb
= begin_omp_structured_block ();
39423 save
= cp_parser_begin_omp_structured_block (parser
);
39425 ret
= cp_parser_omp_for_loop (parser
, OMP_LOOP
, clauses
, cclauses
, if_p
);
39427 cp_parser_end_omp_structured_block (parser
, save
);
39428 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39434 #pragma omp simd simd-clause[optseq] new-line
39437 #define OMP_SIMD_CLAUSE_MASK \
39438 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
39439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
39440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
39442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
39448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39451 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
39452 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39455 tree clauses
, sb
, ret
;
39457 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39459 strcat (p_name
, " simd");
39460 mask
|= OMP_SIMD_CLAUSE_MASK
;
39462 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39466 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
39467 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
39468 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
39469 OMP_CLAUSE_ORDERED
);
39470 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
39472 error_at (OMP_CLAUSE_LOCATION (c
),
39473 "%<ordered%> clause with parameter may not be specified "
39474 "on %qs construct", p_name
);
39475 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
39479 keep_next_level (true);
39480 sb
= begin_omp_structured_block ();
39481 save
= cp_parser_begin_omp_structured_block (parser
);
39483 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
39485 cp_parser_end_omp_structured_block (parser
, save
);
39486 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39492 #pragma omp for for-clause[optseq] new-line
39496 #pragma omp for simd for-simd-clause[optseq] new-line
39499 #define OMP_FOR_CLAUSE_MASK \
39500 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
39506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
39507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
39509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
39512 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
39513 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39516 tree clauses
, sb
, ret
;
39518 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39520 strcat (p_name
, " for");
39521 mask
|= OMP_FOR_CLAUSE_MASK
;
39522 /* parallel for{, simd} disallows nowait clause, but for
39523 target {teams distribute ,}parallel for{, simd} it should be accepted. */
39524 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
39525 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
39526 /* Composite distribute parallel for{, simd} disallows ordered clause. */
39527 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
39528 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
39530 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39532 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39533 const char *p
= IDENTIFIER_POINTER (id
);
39535 if (strcmp (p
, "simd") == 0)
39537 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39538 if (cclauses
== NULL
)
39539 cclauses
= cclauses_buf
;
39541 cp_lexer_consume_token (parser
->lexer
);
39542 if (!flag_openmp
) /* flag_openmp_simd */
39543 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39545 sb
= begin_omp_structured_block ();
39546 save
= cp_parser_begin_omp_structured_block (parser
);
39547 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39549 cp_parser_end_omp_structured_block (parser
, save
);
39550 tree body
= finish_omp_structured_block (sb
);
39553 ret
= make_node (OMP_FOR
);
39554 TREE_TYPE (ret
) = void_type_node
;
39555 OMP_FOR_BODY (ret
) = body
;
39556 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
39557 SET_EXPR_LOCATION (ret
, loc
);
39562 if (!flag_openmp
) /* flag_openmp_simd */
39564 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39568 /* Composite distribute parallel for disallows linear clause. */
39569 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
39570 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
39572 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39576 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
39577 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
39580 keep_next_level (true);
39581 sb
= begin_omp_structured_block ();
39582 save
= cp_parser_begin_omp_structured_block (parser
);
39584 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
39586 cp_parser_end_omp_structured_block (parser
, save
);
39587 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39592 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
39593 omp_clause_mask
, tree
*, bool *);
39596 # pragma omp master new-line
39597 structured-block */
39600 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
39601 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39604 tree clauses
, sb
, ret
;
39606 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39608 strcat (p_name
, " master");
39610 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39612 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39613 const char *p
= IDENTIFIER_POINTER (id
);
39615 if (strcmp (p
, "taskloop") == 0)
39617 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39618 if (cclauses
== NULL
)
39619 cclauses
= cclauses_buf
;
39621 cp_lexer_consume_token (parser
->lexer
);
39622 if (!flag_openmp
) /* flag_openmp_simd */
39623 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
39625 sb
= begin_omp_structured_block ();
39626 save
= cp_parser_begin_omp_structured_block (parser
);
39627 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
39629 cp_parser_end_omp_structured_block (parser
, save
);
39630 tree body
= finish_omp_structured_block (sb
);
39633 return c_finish_omp_master (loc
, body
);
39636 if (!flag_openmp
) /* flag_openmp_simd */
39638 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39644 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39646 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
39649 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39651 return c_finish_omp_master (loc
,
39652 cp_parser_omp_structured_block (parser
, if_p
));
39656 # pragma omp ordered new-line
39660 # pragma omp ordered ordered-clauses new-line
39661 structured-block */
39663 #define OMP_ORDERED_CLAUSE_MASK \
39664 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
39665 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
39667 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
39668 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39671 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
39672 enum pragma_context context
, bool *if_p
)
39674 location_t loc
= pragma_tok
->location
;
39676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39678 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39679 const char *p
= IDENTIFIER_POINTER (id
);
39681 if (strcmp (p
, "depend") == 0)
39683 if (!flag_openmp
) /* flag_openmp_simd */
39685 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39688 if (context
== pragma_stmt
)
39690 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
39691 "%<depend%> clause may only be used in compound "
39693 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39697 = cp_parser_omp_all_clauses (parser
,
39698 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
39699 "#pragma omp ordered", pragma_tok
);
39700 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
39706 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
39707 "#pragma omp ordered", pragma_tok
);
39709 if (!flag_openmp
/* flag_openmp_simd */
39710 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
39713 c_finish_omp_ordered (loc
, clauses
,
39714 cp_parser_omp_structured_block (parser
, if_p
));
39721 { section-sequence }
39724 section-directive[opt] structured-block
39725 section-sequence section-directive structured-block */
39728 cp_parser_omp_sections_scope (cp_parser
*parser
)
39730 tree stmt
, substmt
;
39731 bool error_suppress
= false;
39734 matching_braces braces
;
39735 if (!braces
.require_open (parser
))
39738 stmt
= push_stmt_list ();
39740 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
39741 != PRAGMA_OMP_SECTION
)
39743 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
39744 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
39745 add_stmt (substmt
);
39750 tok
= cp_lexer_peek_token (parser
->lexer
);
39751 if (tok
->type
== CPP_CLOSE_BRACE
)
39753 if (tok
->type
== CPP_EOF
)
39756 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
39758 cp_lexer_consume_token (parser
->lexer
);
39759 cp_parser_require_pragma_eol (parser
, tok
);
39760 error_suppress
= false;
39762 else if (!error_suppress
)
39764 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
39765 error_suppress
= true;
39768 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
39769 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
39770 add_stmt (substmt
);
39772 braces
.require_close (parser
);
39774 substmt
= pop_stmt_list (stmt
);
39776 stmt
= make_node (OMP_SECTIONS
);
39777 TREE_TYPE (stmt
) = void_type_node
;
39778 OMP_SECTIONS_BODY (stmt
) = substmt
;
39785 # pragma omp sections sections-clause[optseq] newline
39788 #define OMP_SECTIONS_CLAUSE_MASK \
39789 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39792 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39793 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39796 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
39797 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
39800 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39802 strcat (p_name
, " sections");
39803 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
39805 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
39807 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39811 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
39812 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
39815 ret
= cp_parser_omp_sections_scope (parser
);
39817 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
39823 # pragma omp parallel parallel-clause[optseq] new-line
39825 # pragma omp parallel for parallel-for-clause[optseq] new-line
39827 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
39831 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
39832 structured-block */
39834 #define OMP_PARALLEL_CLAUSE_MASK \
39835 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
39841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39842 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
39843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
39846 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
39847 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39850 tree stmt
, clauses
, block
;
39852 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39854 strcat (p_name
, " parallel");
39855 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
39856 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
39857 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
39858 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
39859 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
39861 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
39863 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39864 if (cclauses
== NULL
)
39865 cclauses
= cclauses_buf
;
39867 cp_lexer_consume_token (parser
->lexer
);
39868 if (!flag_openmp
) /* flag_openmp_simd */
39869 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
39871 block
= begin_omp_parallel ();
39872 save
= cp_parser_begin_omp_structured_block (parser
);
39873 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
39875 cp_parser_end_omp_structured_block (parser
, save
);
39876 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
39878 if (ret
== NULL_TREE
)
39880 OMP_PARALLEL_COMBINED (stmt
) = 1;
39883 /* When combined with distribute, parallel has to be followed by for.
39884 #pragma omp target parallel is allowed though. */
39886 && (mask
& (OMP_CLAUSE_MASK_1
39887 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
39889 error_at (loc
, "expected %<for%> after %qs", p_name
);
39890 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39893 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39895 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39896 const char *p
= IDENTIFIER_POINTER (id
);
39897 if (cclauses
== NULL
&& strcmp (p
, "master") == 0)
39899 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39900 cclauses
= cclauses_buf
;
39902 cp_lexer_consume_token (parser
->lexer
);
39903 block
= begin_omp_parallel ();
39904 save
= cp_parser_begin_omp_structured_block (parser
);
39905 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
39907 cp_parser_end_omp_structured_block (parser
, save
);
39908 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
39910 if (ret
== NULL_TREE
)
39912 OMP_PARALLEL_COMBINED (stmt
) = 1;
39915 else if (strcmp (p
, "loop") == 0)
39917 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39918 if (cclauses
== NULL
)
39919 cclauses
= cclauses_buf
;
39921 cp_lexer_consume_token (parser
->lexer
);
39922 if (!flag_openmp
) /* flag_openmp_simd */
39923 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
39925 block
= begin_omp_parallel ();
39926 save
= cp_parser_begin_omp_structured_block (parser
);
39927 tree ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
39929 cp_parser_end_omp_structured_block (parser
, save
);
39930 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
39932 if (ret
== NULL_TREE
)
39934 OMP_PARALLEL_COMBINED (stmt
) = 1;
39937 else if (!flag_openmp
) /* flag_openmp_simd */
39939 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39942 else if (cclauses
== NULL
&& strcmp (p
, "sections") == 0)
39944 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39945 cclauses
= cclauses_buf
;
39947 cp_lexer_consume_token (parser
->lexer
);
39948 block
= begin_omp_parallel ();
39949 save
= cp_parser_begin_omp_structured_block (parser
);
39950 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
39951 cp_parser_end_omp_structured_block (parser
, save
);
39952 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
39954 OMP_PARALLEL_COMBINED (stmt
) = 1;
39958 else if (!flag_openmp
) /* flag_openmp_simd */
39960 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39964 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39968 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
39969 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
39972 block
= begin_omp_parallel ();
39973 save
= cp_parser_begin_omp_structured_block (parser
);
39974 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39975 cp_parser_end_omp_structured_block (parser
, save
);
39976 stmt
= finish_omp_parallel (clauses
, block
);
39981 # pragma omp single single-clause[optseq] new-line
39982 structured-block */
39984 #define OMP_SINGLE_CLAUSE_MASK \
39985 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
39988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39991 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39993 tree stmt
= make_node (OMP_SINGLE
);
39994 TREE_TYPE (stmt
) = void_type_node
;
39995 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39997 OMP_SINGLE_CLAUSES (stmt
)
39998 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
39999 "#pragma omp single", pragma_tok
);
40000 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40002 return add_stmt (stmt
);
40006 # pragma omp task task-clause[optseq] new-line
40007 structured-block */
40009 #define OMP_TASK_CLAUSE_MASK \
40010 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
40012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
40017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
40018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
40020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
40023 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40025 tree clauses
, block
;
40028 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
40029 "#pragma omp task", pragma_tok
);
40030 block
= begin_omp_task ();
40031 save
= cp_parser_begin_omp_structured_block (parser
);
40032 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40033 cp_parser_end_omp_structured_block (parser
, save
);
40034 return finish_omp_task (clauses
, block
);
40038 # pragma omp taskwait new-line
40041 # pragma omp taskwait taskwait-clause[opt] new-line */
40043 #define OMP_TASKWAIT_CLAUSE_MASK \
40044 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40047 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
40050 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
40051 "#pragma omp taskwait", pragma_tok
);
40055 tree stmt
= make_node (OMP_TASK
);
40056 TREE_TYPE (stmt
) = void_node
;
40057 OMP_TASK_CLAUSES (stmt
) = clauses
;
40058 OMP_TASK_BODY (stmt
) = NULL_TREE
;
40059 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40063 finish_omp_taskwait ();
40067 # pragma omp taskyield new-line */
40070 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
40072 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40073 finish_omp_taskyield ();
40077 # pragma omp taskgroup new-line
40081 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
40083 #define OMP_TASKGROUP_CLAUSE_MASK \
40084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
40087 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40090 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
40091 "#pragma omp taskgroup", pragma_tok
);
40092 return c_finish_omp_taskgroup (input_location
,
40093 cp_parser_omp_structured_block (parser
,
40100 # pragma omp threadprivate (variable-list) */
40103 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
40107 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
40108 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40110 finish_omp_threadprivate (vars
);
40114 # pragma omp cancel cancel-clause[optseq] new-line */
40116 #define OMP_CANCEL_CLAUSE_MASK \
40117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
40121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
40124 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
40126 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
40127 "#pragma omp cancel", pragma_tok
);
40128 finish_omp_cancel (clauses
);
40132 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
40134 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
40135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
40136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
40137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
40138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
40141 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
40142 enum pragma_context context
)
40145 bool point_seen
= false;
40147 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40149 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40150 const char *p
= IDENTIFIER_POINTER (id
);
40152 if (strcmp (p
, "point") == 0)
40154 cp_lexer_consume_token (parser
->lexer
);
40160 cp_parser_error (parser
, "expected %<point%>");
40161 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40165 if (context
!= pragma_compound
)
40167 if (context
== pragma_stmt
)
40168 error_at (pragma_tok
->location
,
40169 "%<#pragma %s%> may only be used in compound statements",
40170 "omp cancellation point");
40172 cp_parser_error (parser
, "expected declaration specifiers");
40173 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40177 clauses
= cp_parser_omp_all_clauses (parser
,
40178 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
40179 "#pragma omp cancellation point",
40181 finish_omp_cancellation_point (clauses
);
40185 #pragma omp distribute distribute-clause[optseq] new-line
40188 #define OMP_DISTRIBUTE_CLAUSE_MASK \
40189 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40190 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40191 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40192 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
40193 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
40196 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
40197 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40200 tree clauses
, sb
, ret
;
40202 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40204 strcat (p_name
, " distribute");
40205 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
40207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40209 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40210 const char *p
= IDENTIFIER_POINTER (id
);
40212 bool parallel
= false;
40214 if (strcmp (p
, "simd") == 0)
40217 parallel
= strcmp (p
, "parallel") == 0;
40218 if (parallel
|| simd
)
40220 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40221 if (cclauses
== NULL
)
40222 cclauses
= cclauses_buf
;
40223 cp_lexer_consume_token (parser
->lexer
);
40224 if (!flag_openmp
) /* flag_openmp_simd */
40227 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40230 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
40233 sb
= begin_omp_structured_block ();
40234 save
= cp_parser_begin_omp_structured_block (parser
);
40236 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40239 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
40241 cp_parser_end_omp_structured_block (parser
, save
);
40242 tree body
= finish_omp_structured_block (sb
);
40245 ret
= make_node (OMP_DISTRIBUTE
);
40246 TREE_TYPE (ret
) = void_type_node
;
40247 OMP_FOR_BODY (ret
) = body
;
40248 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
40249 SET_EXPR_LOCATION (ret
, loc
);
40254 if (!flag_openmp
) /* flag_openmp_simd */
40256 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40260 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40264 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
40265 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
40268 keep_next_level (true);
40269 sb
= begin_omp_structured_block ();
40270 save
= cp_parser_begin_omp_structured_block (parser
);
40272 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
40274 cp_parser_end_omp_structured_block (parser
, save
);
40275 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40281 # pragma omp teams teams-clause[optseq] new-line
40282 structured-block */
40284 #define OMP_TEAMS_CLAUSE_MASK \
40285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
40290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
40291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
40294 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
40295 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40298 tree clauses
, sb
, ret
;
40300 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40302 strcat (p_name
, " teams");
40303 mask
|= OMP_TEAMS_CLAUSE_MASK
;
40305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40307 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40308 const char *p
= IDENTIFIER_POINTER (id
);
40309 if (strcmp (p
, "distribute") == 0)
40311 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40312 if (cclauses
== NULL
)
40313 cclauses
= cclauses_buf
;
40315 cp_lexer_consume_token (parser
->lexer
);
40316 if (!flag_openmp
) /* flag_openmp_simd */
40317 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
40319 keep_next_level (true);
40320 sb
= begin_omp_structured_block ();
40321 save
= cp_parser_begin_omp_structured_block (parser
);
40322 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
40324 cp_parser_end_omp_structured_block (parser
, save
);
40325 tree body
= finish_omp_structured_block (sb
);
40328 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40329 ret
= make_node (OMP_TEAMS
);
40330 TREE_TYPE (ret
) = void_type_node
;
40331 OMP_TEAMS_CLAUSES (ret
) = clauses
;
40332 OMP_TEAMS_BODY (ret
) = body
;
40333 OMP_TEAMS_COMBINED (ret
) = 1;
40334 SET_EXPR_LOCATION (ret
, loc
);
40335 return add_stmt (ret
);
40337 else if (strcmp (p
, "loop") == 0)
40339 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40340 if (cclauses
== NULL
)
40341 cclauses
= cclauses_buf
;
40343 cp_lexer_consume_token (parser
->lexer
);
40344 if (!flag_openmp
) /* flag_openmp_simd */
40345 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40347 keep_next_level (true);
40348 sb
= begin_omp_structured_block ();
40349 save
= cp_parser_begin_omp_structured_block (parser
);
40350 ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40352 cp_parser_end_omp_structured_block (parser
, save
);
40353 tree body
= finish_omp_structured_block (sb
);
40356 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40357 ret
= make_node (OMP_TEAMS
);
40358 TREE_TYPE (ret
) = void_type_node
;
40359 OMP_TEAMS_CLAUSES (ret
) = clauses
;
40360 OMP_TEAMS_BODY (ret
) = body
;
40361 OMP_TEAMS_COMBINED (ret
) = 1;
40362 SET_EXPR_LOCATION (ret
, loc
);
40363 return add_stmt (ret
);
40366 if (!flag_openmp
) /* flag_openmp_simd */
40368 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40372 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40376 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
40377 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40380 tree stmt
= make_node (OMP_TEAMS
);
40381 TREE_TYPE (stmt
) = void_type_node
;
40382 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
40383 keep_next_level (true);
40384 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40385 SET_EXPR_LOCATION (stmt
, loc
);
40387 return add_stmt (stmt
);
40391 # pragma omp target data target-data-clause[optseq] new-line
40392 structured-block */
40394 #define OMP_TARGET_DATA_CLAUSE_MASK \
40395 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
40399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
40402 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40405 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
40406 "#pragma omp target data", pragma_tok
);
40408 for (tree
*pc
= &clauses
; *pc
;)
40410 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40411 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40414 case GOMP_MAP_ALWAYS_TO
:
40415 case GOMP_MAP_FROM
:
40416 case GOMP_MAP_ALWAYS_FROM
:
40417 case GOMP_MAP_TOFROM
:
40418 case GOMP_MAP_ALWAYS_TOFROM
:
40419 case GOMP_MAP_ALLOC
:
40422 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40423 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40424 case GOMP_MAP_ALWAYS_POINTER
:
40428 error_at (OMP_CLAUSE_LOCATION (*pc
),
40429 "%<#pragma omp target data%> with map-type other "
40430 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40431 "on %<map%> clause");
40432 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40435 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
40436 || OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_ADDR
)
40438 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40444 error_at (pragma_tok
->location
,
40445 "%<#pragma omp target data%> must contain at least "
40446 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
40451 tree stmt
= make_node (OMP_TARGET_DATA
);
40452 TREE_TYPE (stmt
) = void_type_node
;
40453 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
40455 keep_next_level (true);
40456 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40458 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40459 return add_stmt (stmt
);
40463 # pragma omp target enter data target-enter-data-clause[optseq] new-line
40464 structured-block */
40466 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
40467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40474 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
40475 enum pragma_context context
)
40477 bool data_seen
= false;
40478 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40480 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40481 const char *p
= IDENTIFIER_POINTER (id
);
40483 if (strcmp (p
, "data") == 0)
40485 cp_lexer_consume_token (parser
->lexer
);
40491 cp_parser_error (parser
, "expected %<data%>");
40492 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40496 if (context
== pragma_stmt
)
40498 error_at (pragma_tok
->location
,
40499 "%<#pragma %s%> may only be used in compound statements",
40500 "omp target enter data");
40501 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40506 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
40507 "#pragma omp target enter data", pragma_tok
);
40509 for (tree
*pc
= &clauses
; *pc
;)
40511 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40512 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40515 case GOMP_MAP_ALWAYS_TO
:
40516 case GOMP_MAP_ALLOC
:
40519 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40520 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40521 case GOMP_MAP_ALWAYS_POINTER
:
40525 error_at (OMP_CLAUSE_LOCATION (*pc
),
40526 "%<#pragma omp target enter data%> with map-type other "
40527 "than %<to%> or %<alloc%> on %<map%> clause");
40528 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40531 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40537 error_at (pragma_tok
->location
,
40538 "%<#pragma omp target enter data%> must contain at least "
40539 "one %<map%> clause");
40543 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
40544 TREE_TYPE (stmt
) = void_type_node
;
40545 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
40546 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40547 return add_stmt (stmt
);
40551 # pragma omp target exit data target-enter-data-clause[optseq] new-line
40552 structured-block */
40554 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
40555 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40562 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
40563 enum pragma_context context
)
40565 bool data_seen
= false;
40566 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40568 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40569 const char *p
= IDENTIFIER_POINTER (id
);
40571 if (strcmp (p
, "data") == 0)
40573 cp_lexer_consume_token (parser
->lexer
);
40579 cp_parser_error (parser
, "expected %<data%>");
40580 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40584 if (context
== pragma_stmt
)
40586 error_at (pragma_tok
->location
,
40587 "%<#pragma %s%> may only be used in compound statements",
40588 "omp target exit data");
40589 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40594 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
40595 "#pragma omp target exit data", pragma_tok
);
40597 for (tree
*pc
= &clauses
; *pc
;)
40599 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40600 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40602 case GOMP_MAP_FROM
:
40603 case GOMP_MAP_ALWAYS_FROM
:
40604 case GOMP_MAP_RELEASE
:
40605 case GOMP_MAP_DELETE
:
40608 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40609 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40610 case GOMP_MAP_ALWAYS_POINTER
:
40614 error_at (OMP_CLAUSE_LOCATION (*pc
),
40615 "%<#pragma omp target exit data%> with map-type other "
40616 "than %<from%>, %<release%> or %<delete%> on %<map%>"
40618 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40621 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40627 error_at (pragma_tok
->location
,
40628 "%<#pragma omp target exit data%> must contain at least "
40629 "one %<map%> clause");
40633 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
40634 TREE_TYPE (stmt
) = void_type_node
;
40635 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
40636 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40637 return add_stmt (stmt
);
40641 # pragma omp target update target-update-clause[optseq] new-line */
40643 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
40644 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
40645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
40646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40647 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40648 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40649 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40652 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
40653 enum pragma_context context
)
40655 if (context
== pragma_stmt
)
40657 error_at (pragma_tok
->location
,
40658 "%<#pragma %s%> may only be used in compound statements",
40659 "omp target update");
40660 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40665 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
40666 "#pragma omp target update", pragma_tok
);
40667 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
40668 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
40670 error_at (pragma_tok
->location
,
40671 "%<#pragma omp target update%> must contain at least one "
40672 "%<from%> or %<to%> clauses");
40676 tree stmt
= make_node (OMP_TARGET_UPDATE
);
40677 TREE_TYPE (stmt
) = void_type_node
;
40678 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
40679 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40685 # pragma omp target target-clause[optseq] new-line
40686 structured-block */
40688 #define OMP_TARGET_CLAUSE_MASK \
40689 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
40690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
40691 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40692 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
40693 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40694 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40695 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40696 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
40697 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
40700 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
40701 enum pragma_context context
, bool *if_p
)
40703 tree
*pc
= NULL
, stmt
;
40707 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
40709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40711 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40712 const char *p
= IDENTIFIER_POINTER (id
);
40713 enum tree_code ccode
= ERROR_MARK
;
40715 if (strcmp (p
, "teams") == 0)
40717 else if (strcmp (p
, "parallel") == 0)
40718 ccode
= OMP_PARALLEL
;
40719 else if (strcmp (p
, "simd") == 0)
40721 if (ccode
!= ERROR_MARK
)
40723 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
40724 char p_name
[sizeof ("#pragma omp target teams distribute "
40725 "parallel for simd")];
40727 cp_lexer_consume_token (parser
->lexer
);
40728 strcpy (p_name
, "#pragma omp target");
40729 if (!flag_openmp
) /* flag_openmp_simd */
40735 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
40736 OMP_TARGET_CLAUSE_MASK
,
40740 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
40741 OMP_TARGET_CLAUSE_MASK
,
40745 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
40746 OMP_TARGET_CLAUSE_MASK
,
40750 gcc_unreachable ();
40752 return stmt
!= NULL_TREE
;
40754 keep_next_level (true);
40755 tree sb
= begin_omp_structured_block (), ret
;
40756 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
40760 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
40761 OMP_TARGET_CLAUSE_MASK
, cclauses
,
40765 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
40766 OMP_TARGET_CLAUSE_MASK
, cclauses
,
40770 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
40771 OMP_TARGET_CLAUSE_MASK
, cclauses
,
40775 gcc_unreachable ();
40777 cp_parser_end_omp_structured_block (parser
, save
);
40778 tree body
= finish_omp_structured_block (sb
);
40779 if (ret
== NULL_TREE
)
40781 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
40783 /* For combined target teams, ensure the num_teams and
40784 thread_limit clause expressions are evaluated on the host,
40785 before entering the target construct. */
40787 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
40788 c
; c
= OMP_CLAUSE_CHAIN (c
))
40789 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
40790 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
40791 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
40793 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
40794 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
40795 if (expr
== error_mark_node
)
40797 tree tmp
= TARGET_EXPR_SLOT (expr
);
40799 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
40800 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
40801 OMP_CLAUSE_FIRSTPRIVATE
);
40802 OMP_CLAUSE_DECL (tc
) = tmp
;
40803 OMP_CLAUSE_CHAIN (tc
)
40804 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
40805 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
40808 tree stmt
= make_node (OMP_TARGET
);
40809 TREE_TYPE (stmt
) = void_type_node
;
40810 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
40811 OMP_TARGET_BODY (stmt
) = body
;
40812 OMP_TARGET_COMBINED (stmt
) = 1;
40813 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40815 pc
= &OMP_TARGET_CLAUSES (stmt
);
40816 goto check_clauses
;
40818 else if (!flag_openmp
) /* flag_openmp_simd */
40820 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40823 else if (strcmp (p
, "data") == 0)
40825 cp_lexer_consume_token (parser
->lexer
);
40826 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
40829 else if (strcmp (p
, "enter") == 0)
40831 cp_lexer_consume_token (parser
->lexer
);
40832 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
40835 else if (strcmp (p
, "exit") == 0)
40837 cp_lexer_consume_token (parser
->lexer
);
40838 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
40841 else if (strcmp (p
, "update") == 0)
40843 cp_lexer_consume_token (parser
->lexer
);
40844 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
40847 if (!flag_openmp
) /* flag_openmp_simd */
40849 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40853 stmt
= make_node (OMP_TARGET
);
40854 TREE_TYPE (stmt
) = void_type_node
;
40856 OMP_TARGET_CLAUSES (stmt
)
40857 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
40858 "#pragma omp target", pragma_tok
);
40859 pc
= &OMP_TARGET_CLAUSES (stmt
);
40860 keep_next_level (true);
40861 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
40863 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40869 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
40870 switch (OMP_CLAUSE_MAP_KIND (*pc
))
40873 case GOMP_MAP_ALWAYS_TO
:
40874 case GOMP_MAP_FROM
:
40875 case GOMP_MAP_ALWAYS_FROM
:
40876 case GOMP_MAP_TOFROM
:
40877 case GOMP_MAP_ALWAYS_TOFROM
:
40878 case GOMP_MAP_ALLOC
:
40879 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40880 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
40881 case GOMP_MAP_ALWAYS_POINTER
:
40884 error_at (OMP_CLAUSE_LOCATION (*pc
),
40885 "%<#pragma omp target%> with map-type other "
40886 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
40887 "on %<map%> clause");
40888 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40891 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40897 # pragma acc cache (variable-list) new-line
40901 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
40903 tree stmt
, clauses
;
40905 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
40906 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
40908 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
40910 stmt
= make_node (OACC_CACHE
);
40911 TREE_TYPE (stmt
) = void_type_node
;
40912 OACC_CACHE_CLAUSES (stmt
) = clauses
;
40913 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40920 # pragma acc data oacc-data-clause[optseq] new-line
40921 structured-block */
40923 #define OACC_DATA_CLAUSE_MASK \
40924 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
40925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
40930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
40933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40936 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40938 tree stmt
, clauses
, block
;
40941 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
40942 "#pragma acc data", pragma_tok
);
40944 block
= begin_omp_parallel ();
40945 save
= cp_parser_begin_omp_structured_block (parser
);
40946 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40947 cp_parser_end_omp_structured_block (parser
, save
);
40948 stmt
= finish_oacc_data (clauses
, block
);
40953 # pragma acc host_data <clauses> new-line
40954 structured-block */
40956 #define OACC_HOST_DATA_CLAUSE_MASK \
40957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
40958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
40962 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
40964 tree stmt
, clauses
, block
;
40967 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
40968 "#pragma acc host_data", pragma_tok
);
40970 block
= begin_omp_parallel ();
40971 save
= cp_parser_begin_omp_structured_block (parser
);
40972 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40973 cp_parser_end_omp_structured_block (parser
, save
);
40974 stmt
= finish_oacc_host_data (clauses
, block
);
40979 # pragma acc declare oacc-data-clause[optseq] new-line
40982 #define OACC_DECLARE_CLAUSE_MASK \
40983 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
40989 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
40990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40993 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
40995 tree clauses
, stmt
;
40996 bool error
= false;
40997 bool found_in_scope
= global_bindings_p ();
40999 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
41000 "#pragma acc declare", pragma_tok
, true);
41003 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
41005 error_at (pragma_tok
->location
,
41006 "no valid clauses specified in %<#pragma acc declare%>");
41010 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
41012 location_t loc
= OMP_CLAUSE_LOCATION (t
);
41013 tree decl
= OMP_CLAUSE_DECL (t
);
41014 if (!DECL_P (decl
))
41016 error_at (loc
, "array section in %<#pragma acc declare%>");
41020 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
41021 switch (OMP_CLAUSE_MAP_KIND (t
))
41023 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41024 case GOMP_MAP_ALLOC
:
41026 case GOMP_MAP_FORCE_DEVICEPTR
:
41027 case GOMP_MAP_DEVICE_RESIDENT
:
41030 case GOMP_MAP_LINK
:
41031 if (!global_bindings_p ()
41032 && (TREE_STATIC (decl
)
41033 || !DECL_EXTERNAL (decl
)))
41036 "%qD must be a global variable in "
41037 "%<#pragma acc declare link%>",
41045 if (global_bindings_p ())
41047 error_at (loc
, "invalid OpenACC clause at file scope");
41051 if (DECL_EXTERNAL (decl
))
41054 "invalid use of %<extern%> variable %qD "
41055 "in %<#pragma acc declare%>", decl
);
41059 else if (TREE_PUBLIC (decl
))
41062 "invalid use of %<global%> variable %qD "
41063 "in %<#pragma acc declare%>", decl
);
41070 if (!found_in_scope
)
41071 for (tree d
= current_binding_level
->names
; d
; d
= TREE_CHAIN (d
))
41074 found_in_scope
= true;
41077 if (!found_in_scope
)
41080 "%qD must be a variable declared in the same scope as "
41081 "%<#pragma acc declare%>", decl
);
41086 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
41087 || lookup_attribute ("omp declare target link",
41088 DECL_ATTRIBUTES (decl
)))
41090 error_at (loc
, "variable %qD used more than once with "
41091 "%<#pragma acc declare%>", decl
);
41100 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
41101 id
= get_identifier ("omp declare target link");
41103 id
= get_identifier ("omp declare target");
41105 DECL_ATTRIBUTES (decl
)
41106 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
41107 if (current_binding_level
->kind
== sk_namespace
)
41109 symtab_node
*node
= symtab_node::get (decl
);
41112 node
->offloadable
= 1;
41113 if (ENABLE_OFFLOADING
)
41115 g
->have_offload
= true;
41116 if (is_a
<varpool_node
*> (node
))
41117 vec_safe_push (offload_vars
, decl
);
41124 if (error
|| current_binding_level
->kind
== sk_namespace
)
41127 stmt
= make_node (OACC_DECLARE
);
41128 TREE_TYPE (stmt
) = void_type_node
;
41129 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
41130 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41138 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
41142 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
41144 LOC is the location of the #pragma token.
41147 #define OACC_ENTER_DATA_CLAUSE_MASK \
41148 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41149 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41150 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41155 #define OACC_EXIT_DATA_CLAUSE_MASK \
41156 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
41160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
41161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
41162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41165 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
41168 location_t loc
= pragma_tok
->location
;
41169 tree stmt
, clauses
;
41170 const char *p
= "";
41172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41173 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
41175 if (strcmp (p
, "data") != 0)
41177 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
41178 enter
? "enter" : "exit");
41179 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41183 cp_lexer_consume_token (parser
->lexer
);
41186 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
41187 "#pragma acc enter data", pragma_tok
);
41189 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
41190 "#pragma acc exit data", pragma_tok
);
41192 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
41194 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
41195 enter
? "enter" : "exit");
41199 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
41200 TREE_TYPE (stmt
) = void_type_node
;
41201 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
41202 SET_EXPR_LOCATION (stmt
, loc
);
41208 # pragma acc loop oacc-loop-clause[optseq] new-line
41209 structured-block */
41211 #define OACC_LOOP_CLAUSE_MASK \
41212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
41213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
41219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
41220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
41221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
41224 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
41225 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
41227 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
41229 strcat (p_name
, " loop");
41230 mask
|= OACC_LOOP_CLAUSE_MASK
;
41232 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
41236 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
41238 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
41240 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
41243 tree block
= begin_omp_structured_block ();
41244 int save
= cp_parser_begin_omp_structured_block (parser
);
41245 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
41246 cp_parser_end_omp_structured_block (parser
, save
);
41247 add_stmt (finish_omp_structured_block (block
));
41253 # pragma acc kernels oacc-kernels-clause[optseq] new-line
41258 # pragma acc parallel oacc-parallel-clause[optseq] new-line
41263 # pragma acc serial oacc-serial-clause[optseq] new-line
41266 #define OACC_KERNELS_CLAUSE_MASK \
41267 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41283 #define OACC_PARALLEL_CLAUSE_MASK \
41284 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
41296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
41297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
41301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41303 #define OACC_SERIAL_CLAUSE_MASK \
41304 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
41306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
41307 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
41308 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
41309 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
41310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
41311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
41312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
41314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
41315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
41316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
41317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
41318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
41321 cp_parser_oacc_compute (cp_parser
*parser
, cp_token
*pragma_tok
,
41322 char *p_name
, bool *if_p
)
41324 omp_clause_mask mask
;
41325 enum tree_code code
;
41326 switch (cp_parser_pragma_kind (pragma_tok
))
41328 case PRAGMA_OACC_KERNELS
:
41329 strcat (p_name
, " kernels");
41330 mask
= OACC_KERNELS_CLAUSE_MASK
;
41331 code
= OACC_KERNELS
;
41333 case PRAGMA_OACC_PARALLEL
:
41334 strcat (p_name
, " parallel");
41335 mask
= OACC_PARALLEL_CLAUSE_MASK
;
41336 code
= OACC_PARALLEL
;
41338 case PRAGMA_OACC_SERIAL
:
41339 strcat (p_name
, " serial");
41340 mask
= OACC_SERIAL_CLAUSE_MASK
;
41341 code
= OACC_SERIAL
;
41344 gcc_unreachable ();
41347 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41350 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
41351 if (strcmp (p
, "loop") == 0)
41353 cp_lexer_consume_token (parser
->lexer
);
41354 tree block
= begin_omp_parallel ();
41356 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
41358 protected_set_expr_location (stmt
, pragma_tok
->location
);
41359 return finish_omp_construct (code
, block
, clauses
);
41363 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
41365 tree block
= begin_omp_parallel ();
41366 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
41367 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
41368 cp_parser_end_omp_structured_block (parser
, save
);
41369 return finish_omp_construct (code
, block
, clauses
);
41373 # pragma acc update oacc-update-clause[optseq] new-line
41376 #define OACC_UPDATE_CLAUSE_MASK \
41377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
41378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
41379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
41380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
41381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
41382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
41385 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
41387 tree stmt
, clauses
;
41389 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
41390 "#pragma acc update", pragma_tok
);
41392 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
41394 error_at (pragma_tok
->location
,
41395 "%<#pragma acc update%> must contain at least one "
41396 "%<device%> or %<host%> or %<self%> clause");
41400 stmt
= make_node (OACC_UPDATE
);
41401 TREE_TYPE (stmt
) = void_type_node
;
41402 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
41403 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41409 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
41411 LOC is the location of the #pragma token.
41414 #define OACC_WAIT_CLAUSE_MASK \
41415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
41418 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
41420 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
41421 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41423 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
41424 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
41426 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
41427 "#pragma acc wait", pragma_tok
);
41429 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
41430 stmt
= finish_expr_stmt (stmt
);
41436 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
41438 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
41439 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
41440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
41441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
41442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
41443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
41444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
41447 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
41448 enum pragma_context context
,
41451 bool first_p
= parser
->omp_declare_simd
== NULL
;
41452 cp_omp_declare_simd_data data
;
41455 data
.error_seen
= false;
41456 data
.fndecl_seen
= false;
41457 data
.variant_p
= variant_p
;
41458 data
.tokens
= vNULL
;
41459 data
.clauses
= NULL_TREE
;
41460 /* It is safe to take the address of a local variable; it will only be
41461 used while this scope is live. */
41462 parser
->omp_declare_simd
= &data
;
41464 else if (parser
->omp_declare_simd
->variant_p
!= variant_p
)
41466 error_at (pragma_tok
->location
,
41467 "%<#pragma omp declare %s%> followed by "
41468 "%<#pragma omp declare %s%>",
41469 parser
->omp_declare_simd
->variant_p
? "variant" : "simd",
41470 parser
->omp_declare_simd
->variant_p
? "simd" : "variant");
41471 parser
->omp_declare_simd
->error_seen
= true;
41474 /* Store away all pragma tokens. */
41475 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
41476 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
41477 cp_lexer_consume_token (parser
->lexer
);
41478 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
41479 parser
->omp_declare_simd
->error_seen
= true;
41480 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41481 struct cp_token_cache
*cp
41482 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
41483 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
41487 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
41488 cp_parser_pragma (parser
, context
, NULL
);
41491 case pragma_external
:
41492 cp_parser_declaration (parser
);
41494 case pragma_member
:
41495 cp_parser_member_declaration (parser
);
41497 case pragma_objc_icode
:
41498 cp_parser_block_declaration (parser
, /*statement_p=*/false);
41501 cp_parser_declaration_statement (parser
);
41504 if (parser
->omp_declare_simd
41505 && !parser
->omp_declare_simd
->error_seen
41506 && !parser
->omp_declare_simd
->fndecl_seen
)
41507 error_at (pragma_tok
->location
,
41508 "%<#pragma omp declare %s%> not immediately followed by "
41509 "function declaration or definition",
41510 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
41511 data
.tokens
.release ();
41512 parser
->omp_declare_simd
= NULL
;
41516 static const char *const omp_construct_selectors
[] = {
41517 "simd", "target", "teams", "parallel", "for", NULL
};
41518 static const char *const omp_device_selectors
[] = {
41519 "kind", "isa", "arch", NULL
};
41520 static const char *const omp_implementation_selectors
[] = {
41521 "vendor", "extension", "atomic_default_mem_order", "unified_address",
41522 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL
};
41523 static const char *const omp_user_selectors
[] = {
41524 "condition", NULL
};
41529 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
41532 score(score-expression) */
41535 cp_parser_omp_context_selector (cp_parser
*parser
, tree set
, bool has_parms_p
)
41537 tree ret
= NULL_TREE
;
41541 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
41542 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41543 selector
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41546 cp_parser_error (parser
, "expected trait selector name");
41547 return error_mark_node
;
41550 tree properties
= NULL_TREE
;
41551 const char *const *selectors
= NULL
;
41552 bool allow_score
= true;
41553 bool allow_user
= false;
41554 int property_limit
= 0;
41555 enum { CTX_PROPERTY_NONE
, CTX_PROPERTY_USER
, CTX_PROPERTY_NAME_LIST
,
41556 CTX_PROPERTY_ID
, CTX_PROPERTY_EXPR
,
41557 CTX_PROPERTY_SIMD
} property_kind
= CTX_PROPERTY_NONE
;
41558 switch (IDENTIFIER_POINTER (set
)[0])
41560 case 'c': /* construct */
41561 selectors
= omp_construct_selectors
;
41562 allow_score
= false;
41563 property_limit
= 1;
41564 property_kind
= CTX_PROPERTY_SIMD
;
41566 case 'd': /* device */
41567 selectors
= omp_device_selectors
;
41568 allow_score
= false;
41570 property_limit
= 3;
41571 property_kind
= CTX_PROPERTY_NAME_LIST
;
41573 case 'i': /* implementation */
41574 selectors
= omp_implementation_selectors
;
41576 property_limit
= 3;
41577 property_kind
= CTX_PROPERTY_NAME_LIST
;
41579 case 'u': /* user */
41580 selectors
= omp_user_selectors
;
41581 property_limit
= 1;
41582 property_kind
= CTX_PROPERTY_EXPR
;
41585 gcc_unreachable ();
41587 for (int i
= 0; ; i
++)
41589 if (selectors
[i
] == NULL
)
41593 property_kind
= CTX_PROPERTY_USER
;
41598 error ("selector %qs not allowed for context selector "
41599 "set %qs", IDENTIFIER_POINTER (selector
),
41600 IDENTIFIER_POINTER (set
));
41601 cp_lexer_consume_token (parser
->lexer
);
41602 return error_mark_node
;
41605 if (i
== property_limit
)
41606 property_kind
= CTX_PROPERTY_NONE
;
41607 if (strcmp (selectors
[i
], IDENTIFIER_POINTER (selector
)) == 0)
41610 if (property_kind
== CTX_PROPERTY_NAME_LIST
41611 && IDENTIFIER_POINTER (set
)[0] == 'i'
41612 && strcmp (IDENTIFIER_POINTER (selector
),
41613 "atomic_default_mem_order") == 0)
41614 property_kind
= CTX_PROPERTY_ID
;
41616 cp_lexer_consume_token (parser
->lexer
);
41618 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
41620 if (property_kind
== CTX_PROPERTY_NONE
)
41622 error ("selector %qs does not accept any properties",
41623 IDENTIFIER_POINTER (selector
));
41624 return error_mark_node
;
41627 matching_parens parens
;
41628 parens
.consume_open (parser
);
41630 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
41632 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
41633 && strcmp (IDENTIFIER_POINTER (token
->u
.value
), "score") == 0
41634 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
41636 cp_lexer_save_tokens (parser
->lexer
);
41637 cp_lexer_consume_token (parser
->lexer
);
41638 cp_lexer_consume_token (parser
->lexer
);
41639 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
41641 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
41643 cp_lexer_rollback_tokens (parser
->lexer
);
41644 cp_lexer_consume_token (parser
->lexer
);
41646 matching_parens parens2
;
41647 parens2
.require_open (parser
);
41648 tree score
= cp_parser_constant_expression (parser
);
41649 if (!parens2
.require_close (parser
))
41650 cp_parser_skip_to_closing_parenthesis (parser
, true,
41652 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
41653 if (score
!= error_mark_node
)
41655 score
= fold_non_dependent_expr (score
);
41656 if (value_dependent_expression_p (score
))
41657 properties
= tree_cons (get_identifier (" score"),
41658 score
, properties
);
41659 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score
))
41660 || TREE_CODE (score
) != INTEGER_CST
)
41661 error_at (token
->location
, "score argument must be "
41662 "constant integer expression");
41663 else if (tree_int_cst_sgn (score
) < 0)
41664 error_at (token
->location
, "score argument must be "
41667 properties
= tree_cons (get_identifier (" score"),
41668 score
, properties
);
41672 cp_lexer_rollback_tokens (parser
->lexer
);
41674 token
= cp_lexer_peek_token (parser
->lexer
);
41677 switch (property_kind
)
41680 case CTX_PROPERTY_USER
:
41683 t
= cp_parser_constant_expression (parser
);
41684 if (t
!= error_mark_node
)
41686 t
= fold_non_dependent_expr (t
);
41687 if (TREE_CODE (t
) == STRING_CST
)
41688 properties
= tree_cons (NULL_TREE
, t
, properties
);
41689 else if (!value_dependent_expression_p (t
)
41690 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
41691 || !tree_fits_shwi_p (t
)))
41692 error_at (token
->location
, "property must be "
41693 "constant integer expression or string "
41696 properties
= tree_cons (NULL_TREE
, t
, properties
);
41699 return error_mark_node
;
41701 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41702 cp_lexer_consume_token (parser
->lexer
);
41708 case CTX_PROPERTY_ID
:
41709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
41710 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41712 tree prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41713 cp_lexer_consume_token (parser
->lexer
);
41714 properties
= tree_cons (prop
, NULL_TREE
, properties
);
41718 cp_parser_error (parser
, "expected identifier");
41719 return error_mark_node
;
41722 case CTX_PROPERTY_NAME_LIST
:
41725 tree prop
= NULL_TREE
, value
= NULL_TREE
;
41726 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
41727 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41729 prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41730 cp_lexer_consume_token (parser
->lexer
);
41732 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_STRING
))
41733 value
= cp_parser_string_literal (parser
, false, false);
41736 cp_parser_error (parser
, "expected identifier or "
41738 return error_mark_node
;
41741 properties
= tree_cons (prop
, value
, properties
);
41743 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41744 cp_lexer_consume_token (parser
->lexer
);
41750 case CTX_PROPERTY_EXPR
:
41751 t
= cp_parser_constant_expression (parser
);
41752 if (t
!= error_mark_node
)
41754 t
= fold_non_dependent_expr (t
);
41755 if (!value_dependent_expression_p (t
)
41756 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
41757 || !tree_fits_shwi_p (t
)))
41758 error_at (token
->location
, "property must be "
41759 "constant integer expression");
41761 properties
= tree_cons (NULL_TREE
, t
, properties
);
41764 return error_mark_node
;
41766 case CTX_PROPERTY_SIMD
:
41769 error_at (token
->location
, "properties for %<simd%> "
41770 "selector may not be specified in "
41771 "%<metadirective%>");
41772 return error_mark_node
;
41775 = cp_parser_omp_all_clauses (parser
,
41776 OMP_DECLARE_SIMD_CLAUSE_MASK
,
41777 "simd", NULL
, true, 2);
41780 gcc_unreachable ();
41783 if (!parens
.require_close (parser
))
41784 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
41786 properties
= nreverse (properties
);
41788 else if (property_kind
== CTX_PROPERTY_NAME_LIST
41789 || property_kind
== CTX_PROPERTY_ID
41790 || property_kind
== CTX_PROPERTY_EXPR
)
41792 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
41793 return error_mark_node
;
41796 ret
= tree_cons (selector
, properties
, ret
);
41798 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41799 cp_lexer_consume_token (parser
->lexer
);
41805 return nreverse (ret
);
41810 trait-set-selector[,trait-set-selector[,...]]
41812 trait-set-selector:
41813 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
41815 trait-set-selector-name:
41822 cp_parser_omp_context_selector_specification (cp_parser
*parser
,
41825 tree ret
= NULL_TREE
;
41828 const char *setp
= "";
41829 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41831 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
41835 if (strcmp (setp
, "construct") == 0)
41839 if (strcmp (setp
, "device") == 0)
41843 if (strcmp (setp
, "implementation") == 0)
41847 if (strcmp (setp
, "user") == 0)
41855 cp_parser_error (parser
, "expected %<construct%>, %<device%>, "
41856 "%<implementation%> or %<user%>");
41857 return error_mark_node
;
41860 tree set
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41861 cp_lexer_consume_token (parser
->lexer
);
41863 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
41864 return error_mark_node
;
41866 matching_braces braces
;
41867 if (!braces
.require_open (parser
))
41868 return error_mark_node
;
41871 = cp_parser_omp_context_selector (parser
, set
, has_parms_p
);
41872 if (selectors
== error_mark_node
)
41874 cp_parser_skip_to_closing_brace (parser
);
41875 ret
= error_mark_node
;
41877 else if (ret
!= error_mark_node
)
41878 ret
= tree_cons (set
, selectors
, ret
);
41880 braces
.require_close (parser
);
41882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41883 cp_lexer_consume_token (parser
->lexer
);
41889 if (ret
== error_mark_node
)
41891 return nreverse (ret
);
41894 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
41895 that into "omp declare variant base" attribute. */
41898 cp_finish_omp_declare_variant (cp_parser
*parser
, cp_token
*pragma_tok
,
41901 matching_parens parens
;
41902 if (!parens
.require_open (parser
))
41905 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41910 cp_id_kind idk
= CP_ID_KIND_NONE
;
41911 cp_token
*varid_token
= cp_lexer_peek_token (parser
->lexer
);
41913 = cp_parser_id_expression (parser
, /*template_keyword_p=*/false,
41914 /*check_dependency_p=*/true,
41915 /*template_p=*/&template_p
,
41916 /*declarator_p=*/false,
41917 /*optional_p=*/false);
41918 parens
.require_close (parser
);
41921 if (TREE_CODE (varid
) == TEMPLATE_ID_EXPR
41922 || TREE_CODE (varid
) == TYPE_DECL
41923 || varid
== error_mark_node
)
41925 else if (varid_token
->type
== CPP_NAME
&& varid_token
->error_reported
)
41926 variant
= NULL_TREE
;
41929 tree ambiguous_decls
;
41930 variant
= cp_parser_lookup_name (parser
, varid
, none_type
,
41931 template_p
, /*is_namespace=*/false,
41932 /*check_dependency=*/true,
41934 varid
.get_location ());
41935 if (ambiguous_decls
)
41936 variant
= NULL_TREE
;
41938 if (variant
== NULL_TREE
)
41939 variant
= error_mark_node
;
41940 else if (TREE_CODE (variant
) != SCOPE_REF
)
41942 const char *error_msg
;
41944 = finish_id_expression (varid
, variant
, parser
->scope
,
41946 &parser
->non_integral_constant_expression_p
,
41947 template_p
, true, false, false, &error_msg
,
41948 varid
.get_location ());
41950 cp_parser_error (parser
, error_msg
);
41952 location_t caret_loc
= get_pure_location (varid
.get_location ());
41953 location_t start_loc
= get_start (varid_token
->location
);
41954 location_t finish_loc
= get_finish (varid
.get_location ());
41955 location_t varid_loc
= make_location (caret_loc
, start_loc
, finish_loc
);
41957 const char *clause
= "";
41958 location_t match_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41959 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41960 clause
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
41961 if (strcmp (clause
, "match"))
41963 cp_parser_error (parser
, "expected %<match%>");
41967 cp_lexer_consume_token (parser
->lexer
);
41969 if (!parens
.require_open (parser
))
41972 tree ctx
= cp_parser_omp_context_selector_specification (parser
, true);
41973 if (ctx
== error_mark_node
)
41975 ctx
= c_omp_check_context_selector (match_loc
, ctx
);
41976 if (ctx
!= error_mark_node
&& variant
!= error_mark_node
)
41978 tree match_loc_node
= maybe_wrap_with_location (integer_zero_node
,
41980 tree loc_node
= maybe_wrap_with_location (integer_zero_node
, varid_loc
);
41981 loc_node
= tree_cons (match_loc_node
,
41982 build_int_cst (integer_type_node
, idk
),
41983 build_tree_list (loc_node
, integer_zero_node
));
41984 attrs
= tree_cons (get_identifier ("omp declare variant base"),
41985 tree_cons (variant
, ctx
, loc_node
), attrs
);
41986 if (processing_template_decl
)
41987 ATTR_IS_DEPENDENT (attrs
) = 1;
41990 parens
.require_close (parser
);
41991 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41996 /* Finalize #pragma omp declare simd clauses after direct declarator has
41997 been parsed, and put that into "omp declare simd" attribute. */
42000 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
42002 struct cp_token_cache
*ce
;
42003 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
42006 if (!data
->error_seen
&& data
->fndecl_seen
)
42008 error ("%<#pragma omp declare %s%> not immediately followed by "
42009 "a single function declaration or definition",
42010 data
->variant_p
? "variant" : "simd");
42011 data
->error_seen
= true;
42013 if (data
->error_seen
)
42016 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
42020 cp_parser_push_lexer_for_tokens (parser
, ce
);
42021 parser
->lexer
->in_pragma
= true;
42022 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
42023 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
42024 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42025 const char *kind
= IDENTIFIER_POINTER (id
);
42026 cp_lexer_consume_token (parser
->lexer
);
42027 if (strcmp (kind
, "simd") == 0)
42029 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
42030 "#pragma omp declare simd",
42033 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
42034 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
42035 TREE_CHAIN (c
) = attrs
;
42036 if (processing_template_decl
)
42037 ATTR_IS_DEPENDENT (c
) = 1;
42042 gcc_assert (strcmp (kind
, "variant") == 0);
42043 attrs
= cp_finish_omp_declare_variant (parser
, pragma_tok
, attrs
);
42045 cp_parser_pop_lexer (parser
);
42048 data
->fndecl_seen
= true;
42054 # pragma omp declare target new-line
42055 declarations and definitions
42056 # pragma omp end declare target new-line
42059 # pragma omp declare target ( extended-list ) new-line
42061 # pragma omp declare target declare-target-clauses[seq] new-line */
42063 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
42064 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
42065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
42066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
42069 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
42071 tree clauses
= NULL_TREE
;
42072 int device_type
= 0;
42073 bool only_device_type
= true;
42074 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42076 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
42077 "#pragma omp declare target", pragma_tok
);
42078 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42080 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
42082 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
42083 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42087 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42088 scope_chain
->omp_declare_target_attribute
++;
42091 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
42092 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
42093 device_type
|= OMP_CLAUSE_DEVICE_TYPE_KIND (c
);
42094 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
42096 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
42098 tree t
= OMP_CLAUSE_DECL (c
), id
;
42099 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
42100 tree at2
= lookup_attribute ("omp declare target link",
42101 DECL_ATTRIBUTES (t
));
42102 only_device_type
= false;
42103 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
42105 id
= get_identifier ("omp declare target link");
42106 std::swap (at1
, at2
);
42109 id
= get_identifier ("omp declare target");
42112 error_at (OMP_CLAUSE_LOCATION (c
),
42113 "%qD specified both in declare target %<link%> and %<to%>"
42119 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
42120 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
42123 symtab_node
*node
= symtab_node::get (t
);
42126 node
->offloadable
= 1;
42127 if (ENABLE_OFFLOADING
)
42129 g
->have_offload
= true;
42130 if (is_a
<varpool_node
*> (node
))
42131 vec_safe_push (offload_vars
, t
);
42135 if (TREE_CODE (t
) != FUNCTION_DECL
)
42137 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0)
42139 tree at3
= lookup_attribute ("omp declare target host",
42140 DECL_ATTRIBUTES (t
));
42141 if (at3
== NULL_TREE
)
42143 id
= get_identifier ("omp declare target host");
42144 DECL_ATTRIBUTES (t
)
42145 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
42148 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0)
42150 tree at3
= lookup_attribute ("omp declare target nohost",
42151 DECL_ATTRIBUTES (t
));
42152 if (at3
== NULL_TREE
)
42154 id
= get_identifier ("omp declare target nohost");
42155 DECL_ATTRIBUTES (t
)
42156 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
42160 if (device_type
&& only_device_type
)
42161 warning_at (OMP_CLAUSE_LOCATION (clauses
), 0,
42162 "directive with only %<device_type%> clauses ignored");
42166 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
42168 const char *p
= "";
42169 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42171 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42172 p
= IDENTIFIER_POINTER (id
);
42174 if (strcmp (p
, "declare") == 0)
42176 cp_lexer_consume_token (parser
->lexer
);
42178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42180 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42181 p
= IDENTIFIER_POINTER (id
);
42183 if (strcmp (p
, "target") == 0)
42184 cp_lexer_consume_token (parser
->lexer
);
42187 cp_parser_error (parser
, "expected %<target%>");
42188 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42194 cp_parser_error (parser
, "expected %<declare%>");
42195 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42198 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42199 if (!scope_chain
->omp_declare_target_attribute
)
42200 error_at (pragma_tok
->location
,
42201 "%<#pragma omp end declare target%> without corresponding "
42202 "%<#pragma omp declare target%>");
42204 scope_chain
->omp_declare_target_attribute
--;
42207 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
42208 expression and optional initializer clause of
42209 #pragma omp declare reduction. We store the expression(s) as
42210 either 3, 6 or 7 special statements inside of the artificial function's
42211 body. The first two statements are DECL_EXPRs for the artificial
42212 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
42213 expression that uses those variables.
42214 If there was any INITIALIZER clause, this is followed by further statements,
42215 the fourth and fifth statements are DECL_EXPRs for the artificial
42216 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
42217 constructor variant (first token after open paren is not omp_priv),
42218 then the sixth statement is a statement with the function call expression
42219 that uses the OMP_PRIV and optionally OMP_ORIG variable.
42220 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
42221 to initialize the OMP_PRIV artificial variable and there is seventh
42222 statement, a DECL_EXPR of the OMP_PRIV statement again. */
42225 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
42227 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
42228 gcc_assert (TYPE_REF_P (type
));
42229 type
= TREE_TYPE (type
);
42230 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
42231 DECL_ARTIFICIAL (omp_out
) = 1;
42232 pushdecl (omp_out
);
42233 add_decl_expr (omp_out
);
42234 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
42235 DECL_ARTIFICIAL (omp_in
) = 1;
42237 add_decl_expr (omp_in
);
42239 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
42241 keep_next_level (true);
42242 tree block
= begin_omp_structured_block ();
42243 combiner
= cp_parser_expression (parser
);
42244 finish_expr_stmt (combiner
);
42245 block
= finish_omp_structured_block (block
);
42246 if (processing_template_decl
)
42247 block
= build_stmt (input_location
, EXPR_STMT
, block
);
42250 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
42253 const char *p
= "";
42254 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42256 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42257 p
= IDENTIFIER_POINTER (id
);
42260 if (strcmp (p
, "initializer") == 0)
42262 cp_lexer_consume_token (parser
->lexer
);
42263 matching_parens parens
;
42264 if (!parens
.require_open (parser
))
42268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42270 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42271 p
= IDENTIFIER_POINTER (id
);
42274 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
42275 DECL_ARTIFICIAL (omp_priv
) = 1;
42276 pushdecl (omp_priv
);
42277 add_decl_expr (omp_priv
);
42278 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
42279 DECL_ARTIFICIAL (omp_orig
) = 1;
42280 pushdecl (omp_orig
);
42281 add_decl_expr (omp_orig
);
42283 keep_next_level (true);
42284 block
= begin_omp_structured_block ();
42287 if (strcmp (p
, "omp_priv") == 0)
42289 bool is_direct_init
, is_non_constant_init
;
42291 cp_lexer_consume_token (parser
->lexer
);
42292 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
42293 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
42294 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
42295 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
42297 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
42298 == CPP_CLOSE_PAREN
))
42300 finish_omp_structured_block (block
);
42301 error ("invalid initializer clause");
42304 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
42305 &is_non_constant_init
);
42306 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
42307 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
42311 cp_parser_parse_tentatively (parser
);
42312 /* Don't create location wrapper nodes here. */
42313 auto_suppress_location_wrappers sentinel
;
42314 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
42315 /*check_dependency_p=*/true,
42316 /*template_p=*/NULL
,
42317 /*declarator_p=*/false,
42318 /*optional_p=*/false);
42319 vec
<tree
, va_gc
> *args
;
42320 if (fn_name
== error_mark_node
42321 || cp_parser_error_occurred (parser
)
42322 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
42323 || ((args
= cp_parser_parenthesized_expression_list
42324 (parser
, non_attr
, /*cast_p=*/false,
42325 /*allow_expansion_p=*/true,
42326 /*non_constant_p=*/NULL
)),
42327 cp_parser_error_occurred (parser
)))
42329 finish_omp_structured_block (block
);
42330 cp_parser_abort_tentative_parse (parser
);
42331 cp_parser_error (parser
, "expected id-expression (arguments)");
42336 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
42337 if (arg
== omp_priv
42338 || (TREE_CODE (arg
) == ADDR_EXPR
42339 && TREE_OPERAND (arg
, 0) == omp_priv
))
42341 cp_parser_abort_tentative_parse (parser
);
42342 if (arg
== NULL_TREE
)
42343 error ("one of the initializer call arguments should be %<omp_priv%>"
42344 " or %<&omp_priv%>");
42345 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
42347 finish_expr_stmt (initializer
);
42350 block
= finish_omp_structured_block (block
);
42351 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
42352 if (processing_template_decl
)
42353 block
= build_stmt (input_location
, EXPR_STMT
, block
);
42357 add_decl_expr (omp_orig
);
42359 if (!parens
.require_close (parser
))
42363 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
42364 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
42371 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42372 initializer-clause[opt] new-line
42374 initializer-clause:
42375 initializer (omp_priv initializer)
42376 initializer (function-name (argument-list)) */
42379 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
42380 enum pragma_context
)
42382 auto_vec
<tree
> types
;
42383 enum tree_code reduc_code
= ERROR_MARK
;
42384 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
42386 cp_token
*first_token
;
42387 cp_token_cache
*cp
;
42391 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
42392 p
= obstack_alloc (&declarator_obstack
, 0);
42394 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
42397 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
42400 reduc_code
= PLUS_EXPR
;
42403 reduc_code
= MULT_EXPR
;
42406 reduc_code
= MINUS_EXPR
;
42409 reduc_code
= BIT_AND_EXPR
;
42412 reduc_code
= BIT_XOR_EXPR
;
42415 reduc_code
= BIT_IOR_EXPR
;
42418 reduc_code
= TRUTH_ANDIF_EXPR
;
42421 reduc_code
= TRUTH_ORIF_EXPR
;
42424 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
42427 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
42428 "%<|%>, %<&&%>, %<||%> or identifier");
42432 if (reduc_code
!= ERROR_MARK
)
42433 cp_lexer_consume_token (parser
->lexer
);
42435 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
42436 if (reduc_id
== error_mark_node
)
42439 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
42442 /* Types may not be defined in declare reduction type list. */
42443 const char *saved_message
;
42444 saved_message
= parser
->type_definition_forbidden_message
;
42445 parser
->type_definition_forbidden_message
42446 = G_("types may not be defined in declare reduction type list");
42447 bool saved_colon_corrects_to_scope_p
;
42448 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
42449 parser
->colon_corrects_to_scope_p
= false;
42450 bool saved_colon_doesnt_start_class_def_p
;
42451 saved_colon_doesnt_start_class_def_p
42452 = parser
->colon_doesnt_start_class_def_p
;
42453 parser
->colon_doesnt_start_class_def_p
= true;
42457 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42458 type
= cp_parser_type_id (parser
);
42459 if (type
== error_mark_node
)
42461 else if (ARITHMETIC_TYPE_P (type
)
42462 && (orig_reduc_id
== NULL_TREE
42463 || (TREE_CODE (type
) != COMPLEX_TYPE
42464 && (id_equal (orig_reduc_id
, "min")
42465 || id_equal (orig_reduc_id
, "max")))))
42466 error_at (loc
, "predeclared arithmetic type %qT in "
42467 "%<#pragma omp declare reduction%>", type
);
42468 else if (FUNC_OR_METHOD_TYPE_P (type
)
42469 || TREE_CODE (type
) == ARRAY_TYPE
)
42470 error_at (loc
, "function or array type %qT in "
42471 "%<#pragma omp declare reduction%>", type
);
42472 else if (TYPE_REF_P (type
))
42473 error_at (loc
, "reference type %qT in "
42474 "%<#pragma omp declare reduction%>", type
);
42475 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
42476 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
42477 "type %qT in %<#pragma omp declare reduction%>", type
);
42479 types
.safe_push (type
);
42481 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42482 cp_lexer_consume_token (parser
->lexer
);
42487 /* Restore the saved message. */
42488 parser
->type_definition_forbidden_message
= saved_message
;
42489 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
42490 parser
->colon_doesnt_start_class_def_p
42491 = saved_colon_doesnt_start_class_def_p
;
42493 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
42494 || types
.is_empty ())
42497 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42501 first_token
= cp_lexer_peek_token (parser
->lexer
);
42504 FOR_EACH_VEC_ELT (types
, i
, type
)
42507 = build_function_type_list (void_type_node
,
42508 cp_build_reference_type (type
, false),
42510 tree this_reduc_id
= reduc_id
;
42511 if (!dependent_type_p (type
))
42512 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
42513 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
42514 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
42515 DECL_ARTIFICIAL (fndecl
) = 1;
42516 DECL_EXTERNAL (fndecl
) = 1;
42517 DECL_DECLARED_INLINE_P (fndecl
) = 1;
42518 DECL_IGNORED_P (fndecl
) = 1;
42519 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
42520 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
42521 DECL_ATTRIBUTES (fndecl
)
42522 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
42523 DECL_ATTRIBUTES (fndecl
));
42524 if (processing_template_decl
)
42525 fndecl
= push_template_decl (fndecl
);
42526 bool block_scope
= false;
42527 tree block
= NULL_TREE
;
42528 if (current_function_decl
)
42530 block_scope
= true;
42531 DECL_CONTEXT (fndecl
) = global_namespace
;
42532 if (!processing_template_decl
)
42535 else if (current_class_type
)
42539 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
42540 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
42541 cp_lexer_consume_token (parser
->lexer
);
42542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
42544 cp
= cp_token_cache_new (first_token
,
42545 cp_lexer_peek_nth_token (parser
->lexer
,
42548 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
42549 finish_member_declaration (fndecl
);
42550 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
42551 DECL_PENDING_INLINE_P (fndecl
) = 1;
42552 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
42557 DECL_CONTEXT (fndecl
) = current_namespace
;
42561 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
42563 block
= begin_omp_structured_block ();
42566 cp_parser_push_lexer_for_tokens (parser
, cp
);
42567 parser
->lexer
->in_pragma
= true;
42569 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
42572 finish_function (/*inline_p=*/false);
42574 DECL_CONTEXT (fndecl
) = current_function_decl
;
42576 cp_parser_pop_lexer (parser
);
42580 cp_parser_pop_lexer (parser
);
42582 finish_function (/*inline_p=*/false);
42585 DECL_CONTEXT (fndecl
) = current_function_decl
;
42586 block
= finish_omp_structured_block (block
);
42587 if (TREE_CODE (block
) == BIND_EXPR
)
42588 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
42589 else if (TREE_CODE (block
) == STATEMENT_LIST
)
42590 DECL_SAVED_TREE (fndecl
) = block
;
42591 if (processing_template_decl
)
42592 add_decl_expr (fndecl
);
42594 cp_check_omp_declare_reduction (fndecl
);
42595 if (cp
== NULL
&& types
.length () > 1)
42596 cp
= cp_token_cache_new (first_token
,
42597 cp_lexer_peek_nth_token (parser
->lexer
, 2));
42598 if (errs
!= errorcount
)
42602 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42605 /* Free any declarators allocated. */
42606 obstack_free (&declarator_obstack
, p
);
42610 #pragma omp declare simd declare-simd-clauses[optseq] new-line
42611 #pragma omp declare reduction (reduction-id : typename-list : expression) \
42612 initializer-clause[opt] new-line
42613 #pragma omp declare target new-line
42616 #pragma omp declare variant (identifier) match (context-selector) */
42619 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
42620 enum pragma_context context
)
42622 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42624 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42625 const char *p
= IDENTIFIER_POINTER (id
);
42627 if (strcmp (p
, "simd") == 0)
42629 cp_lexer_consume_token (parser
->lexer
);
42630 cp_parser_omp_declare_simd (parser
, pragma_tok
,
42634 if (flag_openmp
&& strcmp (p
, "variant") == 0)
42636 cp_lexer_consume_token (parser
->lexer
);
42637 cp_parser_omp_declare_simd (parser
, pragma_tok
,
42641 cp_ensure_no_omp_declare_simd (parser
);
42642 if (strcmp (p
, "reduction") == 0)
42644 cp_lexer_consume_token (parser
->lexer
);
42645 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
42649 if (!flag_openmp
) /* flag_openmp_simd */
42651 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42654 if (strcmp (p
, "target") == 0)
42656 cp_lexer_consume_token (parser
->lexer
);
42657 cp_parser_omp_declare_target (parser
, pragma_tok
);
42661 cp_parser_error (parser
, "expected %<simd%>, %<reduction%>, "
42662 "%<target%> or %<variant%>");
42663 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42668 #pragma omp requires clauses[optseq] new-line */
42671 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
42674 enum omp_requires new_req
= (enum omp_requires
) 0;
42676 location_t loc
= pragma_tok
->location
;
42677 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
42679 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42680 cp_lexer_consume_token (parser
->lexer
);
42684 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42686 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42687 const char *p
= IDENTIFIER_POINTER (id
);
42688 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42689 enum omp_requires this_req
= (enum omp_requires
) 0;
42691 if (!strcmp (p
, "unified_address"))
42692 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
42693 else if (!strcmp (p
, "unified_shared_memory"))
42694 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
42695 else if (!strcmp (p
, "dynamic_allocators"))
42696 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
42697 else if (!strcmp (p
, "reverse_offload"))
42698 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
42699 else if (!strcmp (p
, "atomic_default_mem_order"))
42701 cp_lexer_consume_token (parser
->lexer
);
42703 matching_parens parens
;
42704 if (parens
.require_open (parser
))
42706 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42708 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42709 p
= IDENTIFIER_POINTER (id
);
42711 if (!strcmp (p
, "seq_cst"))
42713 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
42714 else if (!strcmp (p
, "relaxed"))
42716 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
42717 else if (!strcmp (p
, "acq_rel"))
42719 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
42723 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
42724 "expected %<seq_cst%>, %<relaxed%> or "
42726 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
42728 cp_lexer_consume_token (parser
->lexer
);
42731 cp_lexer_consume_token (parser
->lexer
);
42733 if (!parens
.require_close (parser
))
42734 cp_parser_skip_to_closing_parenthesis (parser
,
42735 /*recovering=*/true,
42736 /*or_comma=*/false,
42742 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42750 error_at (cloc
, "expected %<unified_address%>, "
42751 "%<unified_shared_memory%>, "
42752 "%<dynamic_allocators%>, "
42753 "%<reverse_offload%> "
42754 "or %<atomic_default_mem_order%> clause");
42755 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42759 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
42760 "supported yet", p
);
42762 cp_lexer_consume_token (parser
->lexer
);
42765 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
42767 if ((this_req
& new_req
) != 0)
42768 error_at (cloc
, "too many %qs clauses", p
);
42769 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
42770 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
42771 error_at (cloc
, "%qs clause used lexically after first "
42772 "target construct or offloading API", p
);
42774 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
42776 error_at (cloc
, "too many %qs clauses",
42777 "atomic_default_mem_order");
42778 this_req
= (enum omp_requires
) 0;
42780 else if ((omp_requires_mask
42781 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
42783 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
42784 " clause in a single compilation unit");
42786 = (enum omp_requires
)
42788 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
42790 else if ((omp_requires_mask
42791 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
42792 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
42793 "lexically after first %<atomic%> construct "
42794 "without memory order clause");
42795 new_req
= (enum omp_requires
) (new_req
| this_req
);
42797 = (enum omp_requires
) (omp_requires_mask
| this_req
);
42803 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42806 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
42812 #pragma omp taskloop taskloop-clause[optseq] new-line
42815 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
42818 #define OMP_TASKLOOP_CLAUSE_MASK \
42819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
42820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
42821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
42822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
42823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
42824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
42825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
42826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
42827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
42828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
42829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
42830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
42831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
42832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
42833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
42834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
42837 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
42838 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
42841 tree clauses
, sb
, ret
;
42843 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42845 strcat (p_name
, " taskloop");
42846 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
42847 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
42849 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
42850 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
42852 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42854 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42855 const char *p
= IDENTIFIER_POINTER (id
);
42857 if (strcmp (p
, "simd") == 0)
42859 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
42860 if (cclauses
== NULL
)
42861 cclauses
= cclauses_buf
;
42863 cp_lexer_consume_token (parser
->lexer
);
42864 if (!flag_openmp
) /* flag_openmp_simd */
42865 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
42867 sb
= begin_omp_structured_block ();
42868 save
= cp_parser_begin_omp_structured_block (parser
);
42869 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
42871 cp_parser_end_omp_structured_block (parser
, save
);
42872 tree body
= finish_omp_structured_block (sb
);
42875 ret
= make_node (OMP_TASKLOOP
);
42876 TREE_TYPE (ret
) = void_type_node
;
42877 OMP_FOR_BODY (ret
) = body
;
42878 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
42879 SET_EXPR_LOCATION (ret
, loc
);
42884 if (!flag_openmp
) /* flag_openmp_simd */
42886 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42890 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
42894 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
42895 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
42898 keep_next_level (true);
42899 sb
= begin_omp_structured_block ();
42900 save
= cp_parser_begin_omp_structured_block (parser
);
42902 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
42905 cp_parser_end_omp_structured_block (parser
, save
);
42906 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
42913 # pragma acc routine oacc-routine-clause[optseq] new-line
42914 function-definition
42916 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
42919 #define OACC_ROUTINE_CLAUSE_MASK \
42920 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
42926 /* Parse the OpenACC routine pragma. This has an optional '( name )'
42927 component, which must resolve to a declared namespace-scope
42928 function. The clauses are either processed directly (for a named
42929 function), or defered until the immediatley following declaration
42933 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
42934 enum pragma_context context
)
42936 gcc_checking_assert (context
== pragma_external
);
42937 /* The checking for "another pragma following this one" in the "no optional
42938 '( name )'" case makes sure that we dont re-enter. */
42939 gcc_checking_assert (parser
->oacc_routine
== NULL
);
42941 cp_oacc_routine_data data
;
42942 data
.error_seen
= false;
42943 data
.fndecl_seen
= false;
42944 data
.tokens
= vNULL
;
42945 data
.clauses
= NULL_TREE
;
42946 data
.loc
= pragma_tok
->location
;
42947 /* It is safe to take the address of a local variable; it will only be
42948 used while this scope is live. */
42949 parser
->oacc_routine
= &data
;
42951 /* Look for optional '( name )'. */
42952 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42954 matching_parens parens
;
42955 parens
.consume_open (parser
); /* '(' */
42957 /* We parse the name as an id-expression. If it resolves to
42958 anything other than a non-overloaded function at namespace
42959 scope, it's an error. */
42960 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42961 tree name
= cp_parser_id_expression (parser
,
42962 /*template_keyword_p=*/false,
42963 /*check_dependency_p=*/false,
42964 /*template_p=*/NULL
,
42965 /*declarator_p=*/false,
42966 /*optional_p=*/false);
42967 tree decl
= (identifier_p (name
)
42968 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
42970 if (name
!= error_mark_node
&& decl
== error_mark_node
)
42971 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
42973 if (decl
== error_mark_node
42974 || !parens
.require_close (parser
))
42976 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42977 parser
->oacc_routine
= NULL
;
42982 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
42983 "#pragma acc routine",
42984 cp_lexer_peek_token (parser
->lexer
));
42985 /* The clauses are in reverse order; fix that to make later diagnostic
42986 emission easier. */
42987 data
.clauses
= nreverse (data
.clauses
);
42989 if (decl
&& is_overloaded_fn (decl
)
42990 && (TREE_CODE (decl
) != FUNCTION_DECL
42991 || DECL_FUNCTION_TEMPLATE_P (decl
)))
42993 error_at (name_loc
,
42994 "%<#pragma acc routine%> names a set of overloads");
42995 parser
->oacc_routine
= NULL
;
42999 /* Perhaps we should use the same rule as declarations in different
43001 if (!DECL_NAMESPACE_SCOPE_P (decl
))
43003 error_at (name_loc
,
43004 "%qD does not refer to a namespace scope function", decl
);
43005 parser
->oacc_routine
= NULL
;
43009 if (TREE_CODE (decl
) != FUNCTION_DECL
)
43011 error_at (name_loc
, "%qD does not refer to a function", decl
);
43012 parser
->oacc_routine
= NULL
;
43016 cp_finalize_oacc_routine (parser
, decl
, false);
43017 parser
->oacc_routine
= NULL
;
43019 else /* No optional '( name )'. */
43021 /* Store away all pragma tokens. */
43022 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
43023 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
43024 cp_lexer_consume_token (parser
->lexer
);
43025 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
43026 parser
->oacc_routine
->error_seen
= true;
43027 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43028 struct cp_token_cache
*cp
43029 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
43030 parser
->oacc_routine
->tokens
.safe_push (cp
);
43032 /* Emit a helpful diagnostic if there's another pragma following this
43034 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
43036 cp_ensure_no_oacc_routine (parser
);
43037 data
.tokens
.release ();
43038 /* ..., and then just keep going. */
43042 /* We only have to consider the pragma_external case here. */
43043 cp_parser_declaration (parser
);
43044 if (parser
->oacc_routine
43045 && !parser
->oacc_routine
->fndecl_seen
)
43046 cp_ensure_no_oacc_routine (parser
);
43048 parser
->oacc_routine
= NULL
;
43049 data
.tokens
.release ();
43053 /* Finalize #pragma acc routine clauses after direct declarator has
43057 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
43059 struct cp_token_cache
*ce
;
43060 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
43062 if (!data
->error_seen
&& data
->fndecl_seen
)
43064 error_at (data
->loc
,
43065 "%<#pragma acc routine%> not immediately followed by "
43066 "a single function declaration or definition");
43067 data
->error_seen
= true;
43069 if (data
->error_seen
)
43072 gcc_checking_assert (data
->tokens
.length () == 1);
43073 ce
= data
->tokens
[0];
43075 cp_parser_push_lexer_for_tokens (parser
, ce
);
43076 parser
->lexer
->in_pragma
= true;
43077 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
43079 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
43080 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
43081 parser
->oacc_routine
->clauses
43082 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
43083 "#pragma acc routine", pragma_tok
);
43084 /* The clauses are in reverse order; fix that to make later diagnostic
43085 emission easier. */
43086 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
43087 cp_parser_pop_lexer (parser
);
43088 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
43094 /* Apply any saved OpenACC routine clauses to a just-parsed
43098 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
43100 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
43102 /* Keep going if we're in error reporting mode. */
43103 if (parser
->oacc_routine
->error_seen
43104 || fndecl
== error_mark_node
)
43107 if (parser
->oacc_routine
->fndecl_seen
)
43109 error_at (parser
->oacc_routine
->loc
,
43110 "%<#pragma acc routine%> not immediately followed by"
43111 " a single function declaration or definition");
43112 parser
->oacc_routine
= NULL
;
43115 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
43117 cp_ensure_no_oacc_routine (parser
);
43122 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
43123 parser
->oacc_routine
->loc
,
43124 "#pragma acc routine");
43125 if (compatible
< 0)
43127 parser
->oacc_routine
= NULL
;
43130 if (compatible
> 0)
43135 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
43137 error_at (parser
->oacc_routine
->loc
,
43139 ? G_("%<#pragma acc routine%> must be applied before"
43141 : G_("%<#pragma acc routine%> must be applied before"
43143 parser
->oacc_routine
= NULL
;
43147 /* Set the routine's level of parallelism. */
43148 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
43149 oacc_replace_fn_attrib (fndecl
, dims
);
43151 /* Add an "omp declare target" attribute. */
43152 DECL_ATTRIBUTES (fndecl
)
43153 = tree_cons (get_identifier ("omp declare target"),
43154 parser
->oacc_routine
->clauses
,
43155 DECL_ATTRIBUTES (fndecl
));
43158 /* Don't unset parser->oacc_routine here: we may still need it to
43159 diagnose wrong usage. But, remember that we've used this "#pragma acc
43161 parser
->oacc_routine
->fndecl_seen
= true;
43165 /* Main entry point to OpenMP statement pragmas. */
43168 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
43171 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
43172 omp_clause_mask
mask (0);
43174 switch (cp_parser_pragma_kind (pragma_tok
))
43176 case PRAGMA_OACC_ATOMIC
:
43177 cp_parser_omp_atomic (parser
, pragma_tok
);
43179 case PRAGMA_OACC_CACHE
:
43180 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
43182 case PRAGMA_OACC_DATA
:
43183 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
43185 case PRAGMA_OACC_ENTER_DATA
:
43186 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
43188 case PRAGMA_OACC_EXIT_DATA
:
43189 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
43191 case PRAGMA_OACC_HOST_DATA
:
43192 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
43194 case PRAGMA_OACC_KERNELS
:
43195 case PRAGMA_OACC_PARALLEL
:
43196 case PRAGMA_OACC_SERIAL
:
43197 strcpy (p_name
, "#pragma acc");
43198 stmt
= cp_parser_oacc_compute (parser
, pragma_tok
, p_name
, if_p
);
43200 case PRAGMA_OACC_LOOP
:
43201 strcpy (p_name
, "#pragma acc");
43202 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
43205 case PRAGMA_OACC_UPDATE
:
43206 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
43208 case PRAGMA_OACC_WAIT
:
43209 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
43211 case PRAGMA_OMP_ATOMIC
:
43212 cp_parser_omp_atomic (parser
, pragma_tok
);
43214 case PRAGMA_OMP_CRITICAL
:
43215 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
43217 case PRAGMA_OMP_DISTRIBUTE
:
43218 strcpy (p_name
, "#pragma omp");
43219 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
43222 case PRAGMA_OMP_FOR
:
43223 strcpy (p_name
, "#pragma omp");
43224 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
43227 case PRAGMA_OMP_LOOP
:
43228 strcpy (p_name
, "#pragma omp");
43229 stmt
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
43232 case PRAGMA_OMP_MASTER
:
43233 strcpy (p_name
, "#pragma omp");
43234 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
43237 case PRAGMA_OMP_PARALLEL
:
43238 strcpy (p_name
, "#pragma omp");
43239 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
43242 case PRAGMA_OMP_SECTIONS
:
43243 strcpy (p_name
, "#pragma omp");
43244 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
43246 case PRAGMA_OMP_SIMD
:
43247 strcpy (p_name
, "#pragma omp");
43248 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
43251 case PRAGMA_OMP_SINGLE
:
43252 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
43254 case PRAGMA_OMP_TASK
:
43255 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
43257 case PRAGMA_OMP_TASKGROUP
:
43258 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
43260 case PRAGMA_OMP_TASKLOOP
:
43261 strcpy (p_name
, "#pragma omp");
43262 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
43265 case PRAGMA_OMP_TEAMS
:
43266 strcpy (p_name
, "#pragma omp");
43267 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
43271 gcc_unreachable ();
43274 protected_set_expr_location (stmt
, pragma_tok
->location
);
43277 /* Transactional Memory parsing routines. */
43279 /* Parse a transaction attribute.
43285 We use this instead of cp_parser_attributes_opt for transactions to avoid
43286 the pedwarn in C++98 mode. */
43289 cp_parser_txn_attribute_opt (cp_parser
*parser
)
43292 tree attr_name
, attr
= NULL
;
43294 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
43295 return cp_parser_attributes_opt (parser
);
43297 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
43299 cp_lexer_consume_token (parser
->lexer
);
43300 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
43303 token
= cp_lexer_peek_token (parser
->lexer
);
43304 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
43306 token
= cp_lexer_consume_token (parser
->lexer
);
43308 attr_name
= (token
->type
== CPP_KEYWORD
43309 /* For keywords, use the canonical spelling,
43310 not the parsed identifier. */
43311 ? ridpointers
[(int) token
->keyword
]
43313 attr
= build_tree_list (attr_name
, NULL_TREE
);
43316 cp_parser_error (parser
, "expected identifier");
43318 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
43320 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
43324 /* Parse a __transaction_atomic or __transaction_relaxed statement.
43326 transaction-statement:
43327 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
43329 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
43333 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
43335 unsigned char old_in
= parser
->in_transaction
;
43336 unsigned char this_in
= 1, new_in
;
43337 enum rid keyword
= token
->keyword
;
43338 tree stmt
, attrs
, noex
;
43340 cp_lexer_consume_token (parser
->lexer
);
43342 if (keyword
== RID_TRANSACTION_RELAXED
43343 || keyword
== RID_SYNCHRONIZED
)
43344 this_in
|= TM_STMT_ATTR_RELAXED
;
43347 attrs
= cp_parser_txn_attribute_opt (parser
);
43349 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
43352 /* Parse a noexcept specification. */
43353 if (keyword
== RID_ATOMIC_NOEXCEPT
)
43354 noex
= boolean_true_node
;
43355 else if (keyword
== RID_ATOMIC_CANCEL
)
43357 /* cancel-and-throw is unimplemented. */
43358 sorry ("%<atomic_cancel%>");
43362 noex
= cp_parser_noexcept_specification_opt (parser
,
43363 CP_PARSER_FLAGS_NONE
,
43364 /*require_constexpr=*/true,
43365 /*consumed_expr=*/NULL
,
43366 /*return_cond=*/true);
43368 /* Keep track if we're in the lexical scope of an outer transaction. */
43369 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
43371 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
43373 parser
->in_transaction
= new_in
;
43374 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
43375 parser
->in_transaction
= old_in
;
43377 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
43382 /* Parse a __transaction_atomic or __transaction_relaxed expression.
43384 transaction-expression:
43385 __transaction_atomic txn-noexcept-spec[opt] ( expression )
43386 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
43390 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
43392 unsigned char old_in
= parser
->in_transaction
;
43393 unsigned char this_in
= 1;
43397 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43399 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
43400 || keyword
== RID_TRANSACTION_RELAXED
);
43404 keyword
== RID_TRANSACTION_RELAXED
43405 ? G_("%<__transaction_relaxed%> without transactional memory "
43407 : G_("%<__transaction_atomic%> without transactional memory "
43408 "support enabled"));
43410 token
= cp_parser_require_keyword (parser
, keyword
,
43411 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
43412 : RT_TRANSACTION_RELAXED
));
43413 gcc_assert (token
!= NULL
);
43415 if (keyword
== RID_TRANSACTION_RELAXED
)
43416 this_in
|= TM_STMT_ATTR_RELAXED
;
43418 /* Set this early. This might mean that we allow transaction_cancel in
43419 an expression that we find out later actually has to be a constexpr.
43420 However, we expect that cxx_constant_value will be able to deal with
43421 this; also, if the noexcept has no constexpr, then what we parse next
43422 really is a transaction's body. */
43423 parser
->in_transaction
= this_in
;
43425 /* Parse a noexcept specification. */
43426 noex
= cp_parser_noexcept_specification_opt (parser
,
43427 CP_PARSER_FLAGS_NONE
,
43428 /*require_constexpr=*/false,
43430 /*return_cond=*/true);
43432 if (!noex
|| !noex_expr
43433 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
43435 matching_parens parens
;
43436 parens
.require_open (parser
);
43438 expr
= cp_parser_expression (parser
);
43439 expr
= finish_parenthesized_expr (expr
);
43441 parens
.require_close (parser
);
43445 /* The only expression that is available got parsed for the noexcept
43446 already. noexcept is true then. */
43448 noex
= boolean_true_node
;
43451 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
43452 parser
->in_transaction
= old_in
;
43454 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
43455 return error_mark_node
;
43457 return (flag_tm
? expr
: error_mark_node
);
43460 /* Parse a function-transaction-block.
43462 function-transaction-block:
43463 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
43465 __transaction_atomic txn-attribute[opt] function-try-block
43466 __transaction_relaxed ctor-initializer[opt] function-body
43467 __transaction_relaxed function-try-block
43471 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
43473 unsigned char old_in
= parser
->in_transaction
;
43474 unsigned char new_in
= 1;
43475 tree compound_stmt
, stmt
, attrs
;
43478 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
43479 || keyword
== RID_TRANSACTION_RELAXED
);
43480 token
= cp_parser_require_keyword (parser
, keyword
,
43481 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
43482 : RT_TRANSACTION_RELAXED
));
43483 gcc_assert (token
!= NULL
);
43485 if (keyword
== RID_TRANSACTION_RELAXED
)
43486 new_in
|= TM_STMT_ATTR_RELAXED
;
43489 attrs
= cp_parser_txn_attribute_opt (parser
);
43491 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
43494 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
43496 parser
->in_transaction
= new_in
;
43498 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
43499 cp_parser_function_try_block (parser
);
43501 cp_parser_ctor_initializer_opt_and_function_body
43502 (parser
, /*in_function_try_block=*/false);
43504 parser
->in_transaction
= old_in
;
43506 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
43509 /* Parse a __transaction_cancel statement.
43512 __transaction_cancel txn-attribute[opt] ;
43513 __transaction_cancel txn-attribute[opt] throw-expression ;
43515 ??? Cancel and throw is not yet implemented. */
43518 cp_parser_transaction_cancel (cp_parser
*parser
)
43521 bool is_outer
= false;
43524 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
43525 RT_TRANSACTION_CANCEL
);
43526 gcc_assert (token
!= NULL
);
43528 attrs
= cp_parser_txn_attribute_opt (parser
);
43530 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
43532 /* ??? Parse cancel-and-throw here. */
43534 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
43538 error_at (token
->location
, "%<__transaction_cancel%> without "
43539 "transactional memory support enabled");
43540 return error_mark_node
;
43542 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
43544 error_at (token
->location
, "%<__transaction_cancel%> within a "
43545 "%<__transaction_relaxed%>");
43546 return error_mark_node
;
43550 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
43551 && !is_tm_may_cancel_outer (current_function_decl
))
43553 error_at (token
->location
, "outer %<__transaction_cancel%> not "
43554 "within outer %<__transaction_atomic%>");
43555 error_at (token
->location
,
43556 " or a %<transaction_may_cancel_outer%> function");
43557 return error_mark_node
;
43560 else if (parser
->in_transaction
== 0)
43562 error_at (token
->location
, "%<__transaction_cancel%> not within "
43563 "%<__transaction_atomic%>");
43564 return error_mark_node
;
43567 stmt
= build_tm_abort_call (token
->location
, is_outer
);
43575 static GTY (()) cp_parser
*the_parser
;
43578 /* Special handling for the first token or line in the file. The first
43579 thing in the file might be #pragma GCC pch_preprocess, which loads a
43580 PCH file, which is a GC collection point. So we need to handle this
43581 first pragma without benefit of an existing lexer structure.
43583 Always returns one token to the caller in *FIRST_TOKEN. This is
43584 either the true first token of the file, or the first token after
43585 the initial pragma. */
43588 cp_parser_initial_pragma (cp_token
*first_token
)
43592 cp_lexer_get_preprocessor_token (NULL
, first_token
);
43593 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
43595 c_common_no_more_pch ();
43599 cp_lexer_get_preprocessor_token (NULL
, first_token
);
43600 if (first_token
->type
== CPP_STRING
)
43602 name
= first_token
->u
.value
;
43604 cp_lexer_get_preprocessor_token (NULL
, first_token
);
43605 if (first_token
->type
!= CPP_PRAGMA_EOL
)
43606 error_at (first_token
->location
,
43607 "junk at end of %<#pragma GCC pch_preprocess%>");
43610 error_at (first_token
->location
, "expected string literal");
43612 /* Skip to the end of the pragma. */
43613 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
43614 cp_lexer_get_preprocessor_token (NULL
, first_token
);
43616 /* Now actually load the PCH file. */
43618 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
43620 /* Read one more token to return to our caller. We have to do this
43621 after reading the PCH file in, since its pointers have to be
43623 cp_lexer_get_preprocessor_token (NULL
, first_token
);
43626 /* Parse a pragma GCC ivdep. */
43629 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
43631 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43635 /* Parse a pragma GCC unroll. */
43637 static unsigned short
43638 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
43640 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
43641 tree expr
= cp_parser_constant_expression (parser
);
43642 unsigned short unroll
;
43643 expr
= maybe_constant_value (expr
);
43644 HOST_WIDE_INT lunroll
= 0;
43645 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
43646 || TREE_CODE (expr
) != INTEGER_CST
43647 || (lunroll
= tree_to_shwi (expr
)) < 0
43648 || lunroll
>= USHRT_MAX
)
43650 error_at (location
, "%<#pragma GCC unroll%> requires an"
43651 " assignment-expression that evaluates to a non-negative"
43652 " integral constant less than %u", USHRT_MAX
);
43657 unroll
= (unsigned short)lunroll
;
43661 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43665 /* Normal parsing of a pragma token. Here we can (and must) use the
43669 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
43671 cp_token
*pragma_tok
;
43676 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
43677 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
43678 parser
->lexer
->in_pragma
= true;
43680 id
= cp_parser_pragma_kind (pragma_tok
);
43681 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
43682 cp_ensure_no_omp_declare_simd (parser
);
43685 case PRAGMA_GCC_PCH_PREPROCESS
:
43686 error_at (pragma_tok
->location
,
43687 "%<#pragma GCC pch_preprocess%> must be first");
43690 case PRAGMA_OMP_BARRIER
:
43693 case pragma_compound
:
43694 cp_parser_omp_barrier (parser
, pragma_tok
);
43697 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
43698 "used in compound statements", "omp barrier");
43705 case PRAGMA_OMP_DEPOBJ
:
43708 case pragma_compound
:
43709 cp_parser_omp_depobj (parser
, pragma_tok
);
43712 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
43713 "used in compound statements", "omp depobj");
43720 case PRAGMA_OMP_FLUSH
:
43723 case pragma_compound
:
43724 cp_parser_omp_flush (parser
, pragma_tok
);
43727 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
43728 "used in compound statements", "omp flush");
43735 case PRAGMA_OMP_TASKWAIT
:
43738 case pragma_compound
:
43739 cp_parser_omp_taskwait (parser
, pragma_tok
);
43742 error_at (pragma_tok
->location
,
43743 "%<#pragma %s%> may only be used in compound statements",
43751 case PRAGMA_OMP_TASKYIELD
:
43754 case pragma_compound
:
43755 cp_parser_omp_taskyield (parser
, pragma_tok
);
43758 error_at (pragma_tok
->location
,
43759 "%<#pragma %s%> may only be used in compound statements",
43767 case PRAGMA_OMP_CANCEL
:
43770 case pragma_compound
:
43771 cp_parser_omp_cancel (parser
, pragma_tok
);
43774 error_at (pragma_tok
->location
,
43775 "%<#pragma %s%> may only be used in compound statements",
43783 case PRAGMA_OMP_CANCELLATION_POINT
:
43784 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
43787 case PRAGMA_OMP_THREADPRIVATE
:
43788 cp_parser_omp_threadprivate (parser
, pragma_tok
);
43791 case PRAGMA_OMP_DECLARE
:
43792 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
43794 case PRAGMA_OACC_DECLARE
:
43795 cp_parser_oacc_declare (parser
, pragma_tok
);
43798 case PRAGMA_OACC_ENTER_DATA
:
43799 if (context
== pragma_stmt
)
43801 error_at (pragma_tok
->location
,
43802 "%<#pragma %s%> may only be used in compound statements",
43806 else if (context
!= pragma_compound
)
43808 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
43811 case PRAGMA_OACC_EXIT_DATA
:
43812 if (context
== pragma_stmt
)
43814 error_at (pragma_tok
->location
,
43815 "%<#pragma %s%> may only be used in compound statements",
43819 else if (context
!= pragma_compound
)
43821 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
43824 case PRAGMA_OACC_ROUTINE
:
43825 if (context
!= pragma_external
)
43827 error_at (pragma_tok
->location
,
43828 "%<#pragma acc routine%> must be at file scope");
43831 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
43834 case PRAGMA_OACC_UPDATE
:
43835 if (context
== pragma_stmt
)
43837 error_at (pragma_tok
->location
,
43838 "%<#pragma %s%> may only be used in compound statements",
43842 else if (context
!= pragma_compound
)
43844 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
43847 case PRAGMA_OACC_WAIT
:
43848 if (context
== pragma_stmt
)
43850 error_at (pragma_tok
->location
,
43851 "%<#pragma %s%> may only be used in compound statements",
43855 else if (context
!= pragma_compound
)
43857 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
43860 case PRAGMA_OACC_ATOMIC
:
43861 case PRAGMA_OACC_CACHE
:
43862 case PRAGMA_OACC_DATA
:
43863 case PRAGMA_OACC_HOST_DATA
:
43864 case PRAGMA_OACC_KERNELS
:
43865 case PRAGMA_OACC_LOOP
:
43866 case PRAGMA_OACC_PARALLEL
:
43867 case PRAGMA_OACC_SERIAL
:
43868 case PRAGMA_OMP_ATOMIC
:
43869 case PRAGMA_OMP_CRITICAL
:
43870 case PRAGMA_OMP_DISTRIBUTE
:
43871 case PRAGMA_OMP_FOR
:
43872 case PRAGMA_OMP_LOOP
:
43873 case PRAGMA_OMP_MASTER
:
43874 case PRAGMA_OMP_PARALLEL
:
43875 case PRAGMA_OMP_SECTIONS
:
43876 case PRAGMA_OMP_SIMD
:
43877 case PRAGMA_OMP_SINGLE
:
43878 case PRAGMA_OMP_TASK
:
43879 case PRAGMA_OMP_TASKGROUP
:
43880 case PRAGMA_OMP_TASKLOOP
:
43881 case PRAGMA_OMP_TEAMS
:
43882 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
43884 stmt
= push_omp_privatization_clauses (false);
43885 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
43886 pop_omp_privatization_clauses (stmt
);
43889 case PRAGMA_OMP_REQUIRES
:
43890 if (context
!= pragma_external
)
43892 error_at (pragma_tok
->location
,
43893 "%<#pragma omp requires%> may only be used at file or "
43894 "namespace scope");
43897 return cp_parser_omp_requires (parser
, pragma_tok
);
43899 case PRAGMA_OMP_ORDERED
:
43900 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
43902 stmt
= push_omp_privatization_clauses (false);
43903 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
43904 pop_omp_privatization_clauses (stmt
);
43907 case PRAGMA_OMP_TARGET
:
43908 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
43910 stmt
= push_omp_privatization_clauses (false);
43911 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
43912 pop_omp_privatization_clauses (stmt
);
43915 case PRAGMA_OMP_END_DECLARE_TARGET
:
43916 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
43919 case PRAGMA_OMP_SCAN
:
43920 error_at (pragma_tok
->location
,
43921 "%<#pragma omp scan%> may only be used in "
43922 "a loop construct with %<inscan%> %<reduction%> clause");
43925 case PRAGMA_OMP_SECTION
:
43926 error_at (pragma_tok
->location
,
43927 "%<#pragma omp section%> may only be used in "
43928 "%<#pragma omp sections%> construct");
43933 if (context
== pragma_external
)
43935 error_at (pragma_tok
->location
,
43936 "%<#pragma GCC ivdep%> must be inside a function");
43939 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
43940 unsigned short unroll
;
43941 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
43942 if (tok
->type
== CPP_PRAGMA
43943 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
43945 tok
= cp_lexer_consume_token (parser
->lexer
);
43946 unroll
= cp_parser_pragma_unroll (parser
, tok
);
43947 tok
= cp_lexer_peek_token (the_parser
->lexer
);
43951 if (tok
->type
!= CPP_KEYWORD
43952 || (tok
->keyword
!= RID_FOR
43953 && tok
->keyword
!= RID_WHILE
43954 && tok
->keyword
!= RID_DO
))
43956 cp_parser_error (parser
, "for, while or do statement expected");
43959 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
43963 case PRAGMA_UNROLL
:
43965 if (context
== pragma_external
)
43967 error_at (pragma_tok
->location
,
43968 "%<#pragma GCC unroll%> must be inside a function");
43971 const unsigned short unroll
43972 = cp_parser_pragma_unroll (parser
, pragma_tok
);
43974 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
43975 if (tok
->type
== CPP_PRAGMA
43976 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
43978 tok
= cp_lexer_consume_token (parser
->lexer
);
43979 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
43980 tok
= cp_lexer_peek_token (the_parser
->lexer
);
43984 if (tok
->type
!= CPP_KEYWORD
43985 || (tok
->keyword
!= RID_FOR
43986 && tok
->keyword
!= RID_WHILE
43987 && tok
->keyword
!= RID_DO
))
43989 cp_parser_error (parser
, "for, while or do statement expected");
43992 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
43997 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
43998 c_invoke_pragma_handler (id
);
44002 cp_parser_error (parser
, "expected declaration specifiers");
44006 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44010 /* The interface the pragma parsers have to the lexer. */
44013 pragma_lex (tree
*value
, location_t
*loc
)
44015 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
44016 enum cpp_ttype ret
= tok
->type
;
44018 *value
= tok
->u
.value
;
44020 *loc
= tok
->location
;
44022 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
44024 else if (ret
== CPP_STRING
)
44025 *value
= cp_parser_string_literal (the_parser
, false, false);
44028 if (ret
== CPP_KEYWORD
)
44030 cp_lexer_consume_token (the_parser
->lexer
);
44037 /* External interface. */
44039 /* Parse one entire translation unit. */
44042 c_parse_file (void)
44044 static bool already_called
= false;
44046 if (already_called
)
44047 fatal_error (input_location
,
44048 "inter-module optimizations not implemented for C++");
44049 already_called
= true;
44051 the_parser
= cp_parser_new ();
44052 push_deferring_access_checks (flag_access_control
44053 ? dk_no_deferred
: dk_no_check
);
44054 cp_parser_translation_unit (the_parser
);
44055 class_decl_loc_t::diag_mismatched_tags ();
44059 finish_translation_unit ();
44062 /* Create an identifier for a generic parameter type (a synthesized
44063 template parameter implied by `auto' or a concept identifier). */
44065 static GTY(()) int generic_parm_count
;
44067 make_generic_type_name ()
44070 sprintf (buf
, "auto:%d", ++generic_parm_count
);
44071 return get_identifier (buf
);
44074 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
44075 (creating a new template parameter list if necessary). Returns the newly
44076 created template type parm. */
44079 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
44081 /* A requires-clause is not a function and cannot have placeholders. */
44082 if (current_binding_level
->kind
== sk_block
)
44084 error ("placeholder type not allowed in this context");
44085 return error_mark_node
;
44088 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
44090 /* We are either continuing a function template that already contains implicit
44091 template parameters, creating a new fully-implicit function template, or
44092 extending an existing explicit function template with implicit template
44095 cp_binding_level
*const entry_scope
= current_binding_level
;
44097 bool become_template
= false;
44098 cp_binding_level
*parent_scope
= 0;
44100 if (parser
->implicit_template_scope
)
44102 gcc_assert (parser
->implicit_template_parms
);
44104 current_binding_level
= parser
->implicit_template_scope
;
44108 /* Roll back to the existing template parameter scope (in the case of
44109 extending an explicit function template) or introduce a new template
44110 parameter scope ahead of the function parameter scope (or class scope
44111 in the case of out-of-line member definitions). The function scope is
44112 added back after template parameter synthesis below. */
44114 cp_binding_level
*scope
= entry_scope
;
44116 while (scope
->kind
== sk_function_parms
)
44118 parent_scope
= scope
;
44119 scope
= scope
->level_chain
;
44121 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
44123 /* If not defining a class, then any class scope is a scope level in
44124 an out-of-line member definition. In this case simply wind back
44125 beyond the first such scope to inject the template parameter list.
44126 Otherwise wind back to the class being defined. The latter can
44127 occur in class member friend declarations such as:
44133 friend void A::foo (auto);
44136 The template parameter list synthesized for the friend declaration
44137 must be injected in the scope of 'B'. This can also occur in
44138 erroneous cases such as:
44144 void B::foo (auto) {}
44147 Here the attempted definition of 'B::foo' within 'A' is ill-formed
44148 but, nevertheless, the template parameter list synthesized for the
44149 declarator should be injected into the scope of 'A' as if the
44150 ill-formed template was specified explicitly. */
44152 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
44154 parent_scope
= scope
;
44155 scope
= scope
->level_chain
;
44159 current_binding_level
= scope
;
44161 if (scope
->kind
!= sk_template_parms
44162 || !function_being_declared_is_template_p (parser
))
44164 /* Introduce a new template parameter list for implicit template
44167 become_template
= true;
44169 parser
->implicit_template_scope
44170 = begin_scope (sk_template_parms
, NULL
);
44172 ++processing_template_decl
;
44174 parser
->fully_implicit_function_template_p
= true;
44175 ++parser
->num_template_parameter_lists
;
44179 /* Synthesize implicit template parameters at the end of the explicit
44180 template parameter list. */
44182 gcc_assert (current_template_parms
);
44184 parser
->implicit_template_scope
= scope
;
44186 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
44187 parser
->implicit_template_parms
44188 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
44192 /* Synthesize a new template parameter and track the current template
44193 parameter chain with implicit_template_parms. */
44195 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
44196 tree synth_id
= make_generic_type_name ();
44197 tree synth_tmpl_parm
;
44198 bool non_type
= false;
44200 /* Synthesize the type template parameter. */
44201 gcc_assert(!proto
|| TREE_CODE (proto
) == TYPE_DECL
);
44202 synth_tmpl_parm
= finish_template_type_parm (class_type_node
, synth_id
);
44204 /* Attach the constraint to the parm before processing. */
44205 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
44206 TREE_TYPE (node
) = constr
;
44208 = process_template_parm (parser
->implicit_template_parms
,
44211 /*non_type=*/non_type
,
44212 /*param_pack=*/false);
44214 /* Mark the synthetic declaration "virtual". This is used when
44215 comparing template-heads to determine if whether an abbreviated
44216 function template is equivalent to an explicit template.
44218 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
44219 DECL_VIRTUAL_P (TREE_VALUE (new_parm
)) = true;
44221 // Chain the new parameter to the list of implicit parameters.
44222 if (parser
->implicit_template_parms
)
44223 parser
->implicit_template_parms
44224 = TREE_CHAIN (parser
->implicit_template_parms
);
44226 parser
->implicit_template_parms
= new_parm
;
44228 tree new_decl
= get_local_decls ();
44230 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
44231 new_decl
= DECL_INITIAL (new_decl
);
44233 /* If creating a fully implicit function template, start the new implicit
44234 template parameter list with this synthesized type, otherwise grow the
44235 current template parameter list. */
44237 if (become_template
)
44239 parent_scope
->level_chain
= current_binding_level
;
44241 tree new_parms
= make_tree_vec (1);
44242 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
44243 current_template_parms
= tree_cons (size_int (processing_template_decl
),
44244 new_parms
, current_template_parms
);
44248 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
44249 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
44250 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
44251 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
44254 /* If the new parameter was constrained, we need to add that to the
44255 constraints in the template parameter list. */
44256 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
44258 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
44259 reqs
= combine_constraint_expressions (reqs
, req
);
44260 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
44263 current_binding_level
= entry_scope
;
44268 /* Finish the declaration of a fully implicit function template. Such a
44269 template has no explicit template parameter list so has not been through the
44270 normal template head and tail processing. synthesize_implicit_template_parm
44271 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
44272 provided if the declaration is a class member such that its template
44273 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
44274 form is returned. Otherwise NULL_TREE is returned. */
44277 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
44279 gcc_assert (parser
->fully_implicit_function_template_p
);
44281 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
44282 && DECL_VIRTUAL_P (member_decl_opt
))
44284 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
44285 "implicit templates may not be %<virtual%>");
44286 DECL_VIRTUAL_P (member_decl_opt
) = false;
44289 if (member_decl_opt
)
44290 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
44291 end_template_decl ();
44293 parser
->fully_implicit_function_template_p
= false;
44294 parser
->implicit_template_parms
= 0;
44295 parser
->implicit_template_scope
= 0;
44296 --parser
->num_template_parameter_lists
;
44298 return member_decl_opt
;
44301 /* Like finish_fully_implicit_template, but to be used in error
44302 recovery, rearranging scopes so that we restore the state we had
44303 before synthesize_implicit_template_parm inserted the implement
44304 template parms scope. */
44307 abort_fully_implicit_template (cp_parser
*parser
)
44309 cp_binding_level
*return_to_scope
= current_binding_level
;
44311 if (parser
->implicit_template_scope
44312 && return_to_scope
!= parser
->implicit_template_scope
)
44314 cp_binding_level
*child
= return_to_scope
;
44315 for (cp_binding_level
*scope
= child
->level_chain
;
44316 scope
!= parser
->implicit_template_scope
;
44317 scope
= child
->level_chain
)
44319 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
44320 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
44321 current_binding_level
= parser
->implicit_template_scope
;
44324 return_to_scope
= return_to_scope
->level_chain
;
44326 finish_fully_implicit_template (parser
, NULL
);
44328 gcc_assert (current_binding_level
== return_to_scope
);
44331 /* Helper function for diagnostics that have complained about things
44332 being used with 'extern "C"' linkage.
44334 Attempt to issue a note showing where the 'extern "C"' linkage began. */
44337 maybe_show_extern_c_location (void)
44339 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
44340 inform (the_parser
->innermost_linkage_specification_location
,
44341 "%<extern \"C\"%> linkage started here");
44344 #include "gt-cp-parser.h"