2 Copyright (C) 2000-2019 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"
52 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
53 and c-lex.c) and the C++ parser. */
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant
{
58 /* floating-point literal */
62 /* %<__FUNCTION__%> */
64 /* %<__PRETTY_FUNCTION__%> */
72 /* %<typeid%> operator */
74 /* non-constant compound literals */
82 /* an array reference */
88 /* the address of a label */
102 /* calls to overloaded operators */
106 /* a comma operator */
108 /* a call to a constructor */
110 /* a transaction expression */
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error
{
120 /* is not a class or namespace */
122 /* is not a class, namespace, or enumeration */
126 /* The various kinds of required token */
127 enum required_token
{
129 RT_SEMICOLON
, /* ';' */
130 RT_OPEN_PAREN
, /* '(' */
131 RT_CLOSE_BRACE
, /* '}' */
132 RT_OPEN_BRACE
, /* '{' */
133 RT_CLOSE_SQUARE
, /* ']' */
134 RT_OPEN_SQUARE
, /* '[' */
138 RT_GREATER
, /* '>' */
140 RT_ELLIPSIS
, /* '...' */
144 RT_COLON_SCOPE
, /* ':' or '::' */
145 RT_CLOSE_PAREN
, /* ')' */
146 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
147 RT_PRAGMA_EOL
, /* end of line */
148 RT_NAME
, /* identifier */
150 /* The type is CPP_KEYWORD */
152 RT_DELETE
, /* delete */
153 RT_RETURN
, /* return */
154 RT_WHILE
, /* while */
155 RT_EXTERN
, /* extern */
156 RT_STATIC_ASSERT
, /* static_assert */
157 RT_DECLTYPE
, /* decltype */
158 RT_OPERATOR
, /* operator */
159 RT_CLASS
, /* class */
160 RT_TEMPLATE
, /* template */
161 RT_NAMESPACE
, /* namespace */
162 RT_USING
, /* using */
165 RT_CATCH
, /* catch */
166 RT_THROW
, /* throw */
168 RT_LABEL
, /* __label__ */
169 RT_AT_TRY
, /* @try */
170 RT_AT_SYNCHRONIZED
, /* @synchronized */
171 RT_AT_THROW
, /* @throw */
173 RT_SELECT
, /* selection-statement */
174 RT_ITERATION
, /* iteration-statement */
175 RT_JUMP
, /* jump-statement */
176 RT_CLASS_KEY
, /* class-key */
177 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
178 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
179 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
180 RT_TRANSACTION_CANCEL
/* __transaction_cancel */
183 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
184 reverting it on destruction. */
186 class type_id_in_expr_sentinel
191 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
193 saved (parser
->in_type_id_in_expr_p
)
194 { parser
->in_type_id_in_expr_p
= set
; }
195 ~type_id_in_expr_sentinel ()
196 { parser
->in_type_id_in_expr_p
= saved
; }
201 static cp_lexer
*cp_lexer_new_main
203 static cp_lexer
*cp_lexer_new_from_tokens
204 (cp_token_cache
*tokens
);
205 static void cp_lexer_destroy
207 static int cp_lexer_saving_tokens
209 static cp_token
*cp_lexer_token_at
210 (cp_lexer
*, cp_token_position
);
211 static void cp_lexer_get_preprocessor_token
212 (cp_lexer
*, cp_token
*);
213 static inline cp_token
*cp_lexer_peek_token
215 static cp_token
*cp_lexer_peek_nth_token
216 (cp_lexer
*, size_t);
217 static inline bool cp_lexer_next_token_is
218 (cp_lexer
*, enum cpp_ttype
);
219 static bool cp_lexer_next_token_is_not
220 (cp_lexer
*, enum cpp_ttype
);
221 static bool cp_lexer_next_token_is_keyword
222 (cp_lexer
*, enum rid
);
223 static cp_token
*cp_lexer_consume_token
225 static void cp_lexer_purge_token
227 static void cp_lexer_purge_tokens_after
228 (cp_lexer
*, cp_token_position
);
229 static void cp_lexer_save_tokens
231 static void cp_lexer_commit_tokens
233 static void cp_lexer_rollback_tokens
235 static void cp_lexer_print_token
236 (FILE *, cp_token
*);
237 static inline bool cp_lexer_debugging_p
239 static void cp_lexer_start_debugging
240 (cp_lexer
*) ATTRIBUTE_UNUSED
;
241 static void cp_lexer_stop_debugging
242 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static cp_token_cache
*cp_token_cache_new
245 (cp_token
*, cp_token
*);
246 static tree cp_parser_late_noexcept_specifier
248 static void noexcept_override_late_checks
251 static void cp_parser_initial_pragma
254 static bool cp_parser_omp_declare_reduction_exprs
256 static void cp_finalize_oacc_routine
257 (cp_parser
*, tree
, bool);
259 /* Manifest constants. */
260 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
261 #define CP_SAVED_TOKEN_STACK 5
265 /* The stream to which debugging output should be written. */
266 static FILE *cp_lexer_debug_stream
;
268 /* Nonzero if we are parsing an unevaluated operand: an operand to
269 sizeof, typeof, or alignof. */
270 int cp_unevaluated_operand
;
272 /* Dump up to NUM tokens in BUFFER to FILE starting with token
273 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
274 first token in BUFFER. If NUM is 0, dump all the tokens. If
275 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
276 highlighted by surrounding it in [[ ]]. */
279 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
280 cp_token
*start_token
, unsigned num
,
281 cp_token
*curr_token
)
283 unsigned i
, nprinted
;
287 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
293 num
= buffer
->length ();
295 if (start_token
== NULL
)
296 start_token
= buffer
->address ();
298 if (start_token
> buffer
->address ())
300 cp_lexer_print_token (file
, &(*buffer
)[0]);
301 fprintf (file
, " ... ");
306 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
308 if (token
== start_token
)
315 if (token
== curr_token
)
316 fprintf (file
, "[[");
318 cp_lexer_print_token (file
, token
);
320 if (token
== curr_token
)
321 fprintf (file
, "]]");
327 case CPP_CLOSE_BRACE
:
337 if (i
== num
&& i
< buffer
->length ())
339 fprintf (file
, " ... ");
340 cp_lexer_print_token (file
, &buffer
->last ());
343 fprintf (file
, "\n");
347 /* Dump all tokens in BUFFER to stderr. */
350 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
352 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
356 debug (vec
<cp_token
, va_gc
> &ref
)
358 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
362 debug (vec
<cp_token
, va_gc
> *ptr
)
367 fprintf (stderr
, "<nil>\n");
371 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
372 description for T. */
375 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
379 fprintf (file
, "%s: ", desc
);
380 print_node_brief (file
, "", t
, 0);
385 /* Dump parser context C to FILE. */
388 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
390 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
391 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
392 print_node_brief (file
, "", c
->object_type
, 0);
393 fprintf (file
, "}\n");
397 /* Print the stack of parsing contexts to FILE starting with FIRST. */
400 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
403 cp_parser_context
*c
;
405 fprintf (file
, "Parsing context stack:\n");
406 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
408 fprintf (file
, "\t#%u: ", i
);
409 cp_debug_print_context (file
, c
);
414 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
417 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
420 fprintf (file
, "%s: true\n", desc
);
424 /* Print an unparsed function entry UF to FILE. */
427 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
430 cp_default_arg_entry
*default_arg_fn
;
433 fprintf (file
, "\tFunctions with default args:\n");
435 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
438 fprintf (file
, "\t\tClass type: ");
439 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
440 fprintf (file
, "\t\tDeclaration: ");
441 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
442 fprintf (file
, "\n");
445 fprintf (file
, "\n\tFunctions with definitions that require "
446 "post-processing\n\t\t");
447 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
449 print_node_brief (file
, "", fn
, 0);
452 fprintf (file
, "\n");
454 fprintf (file
, "\n\tNon-static data members with initializers that require "
455 "post-processing\n\t\t");
456 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
458 print_node_brief (file
, "", fn
, 0);
461 fprintf (file
, "\n");
465 /* Print the stack of unparsed member functions S to FILE. */
468 cp_debug_print_unparsed_queues (FILE *file
,
469 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
472 cp_unparsed_functions_entry
*uf
;
474 fprintf (file
, "Unparsed functions\n");
475 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
477 fprintf (file
, "#%u:\n", i
);
478 cp_debug_print_unparsed_function (file
, uf
);
483 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
484 the given PARSER. If FILE is NULL, the output is printed on stderr. */
487 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
489 cp_token
*next_token
, *first_token
, *start_token
;
494 next_token
= parser
->lexer
->next_token
;
495 first_token
= parser
->lexer
->buffer
->address ();
496 start_token
= (next_token
> first_token
+ window_size
/ 2)
497 ? next_token
- window_size
/ 2
499 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
504 /* Dump debugging information for the given PARSER. If FILE is NULL,
505 the output is printed on stderr. */
508 cp_debug_parser (FILE *file
, cp_parser
*parser
)
510 const size_t window_size
= 20;
512 expanded_location eloc
;
517 fprintf (file
, "Parser state\n\n");
518 fprintf (file
, "Number of tokens: %u\n",
519 vec_safe_length (parser
->lexer
->buffer
));
520 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
521 cp_debug_print_tree_if_set (file
, "Object scope",
522 parser
->object_scope
);
523 cp_debug_print_tree_if_set (file
, "Qualifying scope",
524 parser
->qualifying_scope
);
525 cp_debug_print_context_stack (file
, parser
->context
);
526 cp_debug_print_flag (file
, "Allow GNU extensions",
527 parser
->allow_gnu_extensions_p
);
528 cp_debug_print_flag (file
, "'>' token is greater-than",
529 parser
->greater_than_is_operator_p
);
530 cp_debug_print_flag (file
, "Default args allowed in current "
531 "parameter list", parser
->default_arg_ok_p
);
532 cp_debug_print_flag (file
, "Parsing integral constant-expression",
533 parser
->integral_constant_expression_p
);
534 cp_debug_print_flag (file
, "Allow non-constant expression in current "
535 "constant-expression",
536 parser
->allow_non_integral_constant_expression_p
);
537 cp_debug_print_flag (file
, "Seen non-constant expression",
538 parser
->non_integral_constant_expression_p
);
539 cp_debug_print_flag (file
, "Local names forbidden in current context",
540 (parser
->local_variables_forbidden_p
541 & LOCAL_VARS_FORBIDDEN
));
542 cp_debug_print_flag (file
, "'this' forbidden in current context",
543 (parser
->local_variables_forbidden_p
545 cp_debug_print_flag (file
, "In unbraced linkage specification",
546 parser
->in_unbraced_linkage_specification_p
);
547 cp_debug_print_flag (file
, "Parsing a declarator",
548 parser
->in_declarator_p
);
549 cp_debug_print_flag (file
, "In template argument list",
550 parser
->in_template_argument_list_p
);
551 cp_debug_print_flag (file
, "Parsing an iteration statement",
552 parser
->in_statement
& IN_ITERATION_STMT
);
553 cp_debug_print_flag (file
, "Parsing a switch statement",
554 parser
->in_statement
& IN_SWITCH_STMT
);
555 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
556 parser
->in_statement
& IN_OMP_BLOCK
);
557 cp_debug_print_flag (file
, "Parsing a an OpenMP loop",
558 parser
->in_statement
& IN_OMP_FOR
);
559 cp_debug_print_flag (file
, "Parsing an if statement",
560 parser
->in_statement
& IN_IF_STMT
);
561 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
562 "context", parser
->in_type_id_in_expr_p
);
563 cp_debug_print_flag (file
, "String expressions should be translated "
564 "to execution character set",
565 parser
->translate_strings_p
);
566 cp_debug_print_flag (file
, "Parsing function body outside of a "
567 "local class", parser
->in_function_body
);
568 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
569 parser
->colon_corrects_to_scope_p
);
570 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
571 parser
->colon_doesnt_start_class_def_p
);
572 if (parser
->type_definition_forbidden_message
)
573 fprintf (file
, "Error message for forbidden type definitions: %s %s\n",
574 parser
->type_definition_forbidden_message
,
575 parser
->type_definition_forbidden_message_arg
576 ? parser
->type_definition_forbidden_message_arg
: "<none>");
577 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
578 fprintf (file
, "Number of class definitions in progress: %u\n",
579 parser
->num_classes_being_defined
);
580 fprintf (file
, "Number of template parameter lists for the current "
581 "declaration: %u\n", parser
->num_template_parameter_lists
);
582 cp_debug_parser_tokens (file
, parser
, window_size
);
583 token
= parser
->lexer
->next_token
;
584 fprintf (file
, "Next token to parse:\n");
585 fprintf (file
, "\tToken: ");
586 cp_lexer_print_token (file
, token
);
587 eloc
= expand_location (token
->location
);
588 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
589 fprintf (file
, "\tLine: %d\n", eloc
.line
);
590 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
594 debug (cp_parser
&ref
)
596 cp_debug_parser (stderr
, &ref
);
600 debug (cp_parser
*ptr
)
605 fprintf (stderr
, "<nil>\n");
608 /* Allocate memory for a new lexer object and return it. */
611 cp_lexer_alloc (void)
615 c_common_no_more_pch ();
617 /* Allocate the memory. */
618 lexer
= ggc_cleared_alloc
<cp_lexer
> ();
620 /* Initially we are not debugging. */
621 lexer
->debugging_p
= false;
623 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
625 /* Create the buffer. */
626 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
632 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 cp_lexer_new_main (void)
641 /* It's possible that parsing the first pragma will load a PCH file,
642 which is a GC collection point. So we have to do that before
643 allocating any memory. */
644 cp_parser_initial_pragma (&token
);
646 lexer
= cp_lexer_alloc ();
648 /* Put the first token in the buffer. */
649 lexer
->buffer
->quick_push (token
);
651 /* Get the remaining tokens from the preprocessor. */
652 while (token
.type
!= CPP_EOF
)
654 cp_lexer_get_preprocessor_token (lexer
, &token
);
655 vec_safe_push (lexer
->buffer
, token
);
658 lexer
->next_token
= lexer
->buffer
->address ();
659 lexer
->last_token
= lexer
->next_token
660 + lexer
->buffer
->length ()
663 /* Subsequent preprocessor diagnostics should use compiler
664 diagnostic functions to get the compiler source location. */
667 gcc_assert (!lexer
->next_token
->purged_p
);
671 /* Create a new lexer whose token stream is primed with the tokens in
672 CACHE. When these tokens are exhausted, no new tokens will be read. */
675 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
677 cp_token
*first
= cache
->first
;
678 cp_token
*last
= cache
->last
;
679 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
681 /* We do not own the buffer. */
682 lexer
->buffer
= NULL
;
684 /* Insert an EOF token. */
685 lexer
->saved_type
= last
->type
;
686 lexer
->saved_keyword
= last
->keyword
;
687 last
->type
= CPP_EOF
;
688 last
->keyword
= RID_MAX
;
690 lexer
->next_token
= first
;
691 lexer
->last_token
= last
;
693 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
695 /* Initially we are not debugging. */
696 lexer
->debugging_p
= false;
698 gcc_assert (!lexer
->next_token
->purged_p
);
702 /* Frees all resources associated with LEXER. */
705 cp_lexer_destroy (cp_lexer
*lexer
)
708 vec_free (lexer
->buffer
);
711 /* Restore the token we overwrite with EOF. */
712 lexer
->last_token
->type
= lexer
->saved_type
;
713 lexer
->last_token
->keyword
= lexer
->saved_keyword
;
715 lexer
->saved_tokens
.release ();
719 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
720 be used. The point of this flag is to help the compiler to fold away calls
721 to cp_lexer_debugging_p within this source file at compile time, when the
722 lexer is not being debugged. */
724 #define LEXER_DEBUGGING_ENABLED_P false
726 /* Returns nonzero if debugging information should be output. */
729 cp_lexer_debugging_p (cp_lexer
*lexer
)
731 if (!LEXER_DEBUGGING_ENABLED_P
)
734 return lexer
->debugging_p
;
738 static inline cp_token_position
739 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
741 return lexer
->next_token
- previous_p
;
744 static inline cp_token
*
745 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
751 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
753 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
756 static inline cp_token_position
757 cp_lexer_previous_token_position (cp_lexer
*lexer
)
759 return cp_lexer_token_position (lexer
, true);
762 static inline cp_token
*
763 cp_lexer_previous_token (cp_lexer
*lexer
)
765 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
767 /* Skip past purged tokens. */
770 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
774 return cp_lexer_token_at (lexer
, tp
);
777 /* Overload for make_location, taking the lexer to mean the location of the
780 static inline location_t
781 make_location (location_t caret
, location_t start
, cp_lexer
*lexer
)
783 cp_token
*t
= cp_lexer_previous_token (lexer
);
784 return make_location (caret
, start
, t
->location
);
787 /* nonzero if we are presently saving tokens. */
790 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
792 return lexer
->saved_tokens
.length () != 0;
795 /* Store the next token from the preprocessor in *TOKEN. Return true
796 if we reach EOF. If LEXER is NULL, assume we are handling an
797 initial #pragma pch_preprocess, and thus want the lexer to return
798 processed strings. */
801 cp_lexer_get_preprocessor_token (cp_lexer
*lexer
, cp_token
*token
)
803 static int is_extern_c
= 0;
805 /* Get a new token from the preprocessor. */
807 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
808 lexer
== NULL
? 0 : C_LEX_STRING_NO_JOIN
);
809 token
->keyword
= RID_MAX
;
810 token
->purged_p
= false;
811 token
->error_reported
= false;
812 token
->tree_check_p
= false;
814 /* On some systems, some header files are surrounded by an
815 implicit extern "C" block. Set a flag in the token if it
816 comes from such a header. */
817 is_extern_c
+= pending_lang_change
;
818 pending_lang_change
= 0;
819 token
->implicit_extern_c
= is_extern_c
> 0;
821 /* Check to see if this token is a keyword. */
822 if (token
->type
== CPP_NAME
)
824 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
826 /* Mark this token as a keyword. */
827 token
->type
= CPP_KEYWORD
;
828 /* Record which keyword. */
829 token
->keyword
= C_RID_CODE (token
->u
.value
);
833 if (warn_cxx11_compat
834 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
835 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
837 /* Warn about the C++0x keyword (but still treat it as
839 warning_at (token
->location
, OPT_Wc__11_compat
,
840 "identifier %qE is a keyword in C++11",
843 /* Clear out the C_RID_CODE so we don't warn about this
844 particular identifier-turned-keyword again. */
845 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
847 if (warn_cxx20_compat
848 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX20
849 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX20
)
851 /* Warn about the C++20 keyword (but still treat it as
853 warning_at (token
->location
, OPT_Wc__20_compat
,
854 "identifier %qE is a keyword in C++20",
857 /* Clear out the C_RID_CODE so we don't warn about this
858 particular identifier-turned-keyword again. */
859 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
862 token
->keyword
= RID_MAX
;
865 else if (token
->type
== CPP_AT_NAME
)
867 /* This only happens in Objective-C++; it must be a keyword. */
868 token
->type
= CPP_KEYWORD
;
869 switch (C_RID_CODE (token
->u
.value
))
871 /* Replace 'class' with '@class', 'private' with '@private',
872 etc. This prevents confusion with the C++ keyword
873 'class', and makes the tokens consistent with other
874 Objective-C 'AT' keywords. For example '@class' is
875 reported as RID_AT_CLASS which is consistent with
876 '@synchronized', which is reported as
879 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
880 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
881 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
882 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
883 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
884 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
885 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
886 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
887 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
892 /* Update the globals input_location and the input file stack from TOKEN. */
894 cp_lexer_set_source_position_from_token (cp_token
*token
)
896 if (token
->type
!= CPP_EOF
)
898 input_location
= token
->location
;
902 /* Update the globals input_location and the input file stack from LEXER. */
904 cp_lexer_set_source_position (cp_lexer
*lexer
)
906 cp_token
*token
= cp_lexer_peek_token (lexer
);
907 cp_lexer_set_source_position_from_token (token
);
910 /* Return a pointer to the next token in the token stream, but do not
913 static inline cp_token
*
914 cp_lexer_peek_token (cp_lexer
*lexer
)
916 if (cp_lexer_debugging_p (lexer
))
918 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
919 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
920 putc ('\n', cp_lexer_debug_stream
);
922 return lexer
->next_token
;
925 /* Return true if the next token has the indicated TYPE. */
928 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
930 return cp_lexer_peek_token (lexer
)->type
== type
;
933 /* Return true if the next token does not have the indicated TYPE. */
936 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
938 return !cp_lexer_next_token_is (lexer
, type
);
941 /* Return true if the next token is the indicated KEYWORD. */
944 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
946 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
950 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
952 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
956 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
958 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
961 /* Return true if KEYWORD can start a decl-specifier. */
964 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
968 /* auto specifier: storage-class-specifier in C++,
969 simple-type-specifier in C++0x. */
971 /* Storage classes. */
977 /* Elaborated type specifiers. */
983 /* Simple type specifiers. */
998 /* GNU extensions. */
1001 /* C++11 extensions. */
1003 case RID_UNDERLYING_TYPE
:
1005 /* C++20 extensions. */
1011 if (keyword
>= RID_FIRST_INT_N
1012 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
1013 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
1019 /* Return true if the next token is a keyword for a decl-specifier. */
1022 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1026 token
= cp_lexer_peek_token (lexer
);
1027 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1030 /* Returns TRUE iff the token T begins a decltype type. */
1033 token_is_decltype (cp_token
*t
)
1035 return (t
->keyword
== RID_DECLTYPE
1036 || t
->type
== CPP_DECLTYPE
);
1039 /* Returns TRUE iff the next token begins a decltype type. */
1042 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1044 cp_token
*t
= cp_lexer_peek_token (lexer
);
1045 return token_is_decltype (t
);
1048 /* Called when processing a token with tree_check_value; perform or defer the
1049 associated checks and return the value. */
1052 saved_checks_value (struct tree_check
*check_value
)
1054 /* Perform any access checks that were deferred. */
1055 vec
<deferred_access_check
, va_gc
> *checks
;
1056 deferred_access_check
*chk
;
1057 checks
= check_value
->checks
;
1061 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1062 perform_or_defer_access_check (chk
->binfo
,
1064 chk
->diag_decl
, tf_warning_or_error
);
1066 /* Return the stored value. */
1067 return check_value
->value
;
1070 /* Return a pointer to the Nth token in the token stream. If N is 1,
1071 then this is precisely equivalent to cp_lexer_peek_token (except
1072 that it is not inline). One would like to disallow that case, but
1073 there is one case (cp_parser_nth_token_starts_template_id) where
1074 the caller passes a variable for N and it might be 1. */
1077 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1081 /* N is 1-based, not zero-based. */
1084 if (cp_lexer_debugging_p (lexer
))
1085 fprintf (cp_lexer_debug_stream
,
1086 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1089 token
= lexer
->next_token
;
1090 while (n
&& token
->type
!= CPP_EOF
)
1093 if (!token
->purged_p
)
1097 if (cp_lexer_debugging_p (lexer
))
1099 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1100 putc ('\n', cp_lexer_debug_stream
);
1106 /* Return the next token, and advance the lexer's next_token pointer
1107 to point to the next non-purged token. */
1110 cp_lexer_consume_token (cp_lexer
* lexer
)
1112 cp_token
*token
= lexer
->next_token
;
1114 gcc_assert (!lexer
->in_pragma
|| token
->type
!= CPP_PRAGMA_EOL
);
1118 gcc_assert (token
->type
!= CPP_EOF
);
1119 lexer
->next_token
++;
1121 while (lexer
->next_token
->purged_p
);
1123 cp_lexer_set_source_position_from_token (token
);
1125 /* Provide debugging output. */
1126 if (cp_lexer_debugging_p (lexer
))
1128 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1129 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1130 putc ('\n', cp_lexer_debug_stream
);
1136 /* Permanently remove the next token from the token stream, and
1137 advance the next_token pointer to refer to the next non-purged
1141 cp_lexer_purge_token (cp_lexer
*lexer
)
1143 cp_token
*tok
= lexer
->next_token
;
1145 gcc_assert (tok
->type
!= CPP_EOF
);
1146 tok
->purged_p
= true;
1147 tok
->location
= UNKNOWN_LOCATION
;
1148 tok
->u
.value
= NULL_TREE
;
1149 tok
->keyword
= RID_MAX
;
1153 while (tok
->purged_p
);
1154 lexer
->next_token
= tok
;
1157 /* Permanently remove all tokens after TOK, up to, but not
1158 including, the token that will be returned next by
1159 cp_lexer_peek_token. */
1162 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1164 cp_token
*peek
= lexer
->next_token
;
1166 gcc_assert (tok
< peek
);
1168 for (tok
++; tok
!= peek
; tok
++)
1170 tok
->purged_p
= true;
1171 tok
->location
= UNKNOWN_LOCATION
;
1172 tok
->u
.value
= NULL_TREE
;
1173 tok
->keyword
= RID_MAX
;
1177 /* Begin saving tokens. All tokens consumed after this point will be
1181 cp_lexer_save_tokens (cp_lexer
* lexer
)
1183 /* Provide debugging output. */
1184 if (cp_lexer_debugging_p (lexer
))
1185 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1187 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1190 /* Commit to the portion of the token stream most recently saved. */
1193 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1195 /* Provide debugging output. */
1196 if (cp_lexer_debugging_p (lexer
))
1197 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1199 lexer
->saved_tokens
.pop ();
1202 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1203 to the token stream. Stop saving tokens. */
1206 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1208 /* Provide debugging output. */
1209 if (cp_lexer_debugging_p (lexer
))
1210 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1212 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1215 /* RAII wrapper around the above functions, with sanity checking. Creating
1216 a variable saves tokens, which are committed when the variable is
1217 destroyed unless they are explicitly rolled back by calling the rollback
1220 struct saved_token_sentinel
1225 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1227 len
= lexer
->saved_tokens
.length ();
1228 cp_lexer_save_tokens (lexer
);
1232 cp_lexer_rollback_tokens (lexer
);
1235 ~saved_token_sentinel()
1238 cp_lexer_commit_tokens (lexer
);
1239 gcc_assert (lexer
->saved_tokens
.length () == len
);
1243 /* Print a representation of the TOKEN on the STREAM. */
1246 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1248 /* We don't use cpp_type2name here because the parser defines
1249 a few tokens of its own. */
1250 static const char *const token_names
[] = {
1251 /* cpplib-defined token types */
1252 #define OP(e, s) #e,
1253 #define TK(e, s) #e,
1257 /* C++ parser token types - see "Manifest constants", above. */
1260 "NESTED_NAME_SPECIFIER",
1263 /* For some tokens, print the associated data. */
1264 switch (token
->type
)
1267 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1268 For example, `struct' is mapped to an INTEGER_CST. */
1269 if (!identifier_p (token
->u
.value
))
1273 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1280 case CPP_UTF8STRING
:
1281 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1285 print_generic_expr (stream
, token
->u
.value
);
1289 /* If we have a name for the token, print it out. Otherwise, we
1290 simply give the numeric code. */
1291 if (token
->type
< ARRAY_SIZE(token_names
))
1292 fputs (token_names
[token
->type
], stream
);
1294 fprintf (stream
, "[%d]", token
->type
);
1300 debug (cp_token
&ref
)
1302 cp_lexer_print_token (stderr
, &ref
);
1303 fprintf (stderr
, "\n");
1307 debug (cp_token
*ptr
)
1312 fprintf (stderr
, "<nil>\n");
1316 /* Start emitting debugging information. */
1319 cp_lexer_start_debugging (cp_lexer
* lexer
)
1321 if (!LEXER_DEBUGGING_ENABLED_P
)
1322 fatal_error (input_location
,
1323 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1325 lexer
->debugging_p
= true;
1326 cp_lexer_debug_stream
= stderr
;
1329 /* Stop emitting debugging information. */
1332 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1334 if (!LEXER_DEBUGGING_ENABLED_P
)
1335 fatal_error (input_location
,
1336 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1338 lexer
->debugging_p
= false;
1339 cp_lexer_debug_stream
= NULL
;
1342 /* Create a new cp_token_cache, representing a range of tokens. */
1344 static cp_token_cache
*
1345 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1347 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1348 cache
->first
= first
;
1353 /* Diagnose if #pragma omp declare simd isn't followed immediately
1354 by function declaration or definition. */
1357 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1359 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1361 error ("%<#pragma omp declare %s%> not immediately followed by "
1362 "function declaration or definition",
1363 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1364 parser
->omp_declare_simd
= NULL
;
1368 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1369 and put that into "omp declare simd" attribute. */
1372 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1374 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1376 if (fndecl
== error_mark_node
)
1378 parser
->omp_declare_simd
= NULL
;
1381 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1383 cp_ensure_no_omp_declare_simd (parser
);
1389 /* Diagnose if #pragma acc routine isn't followed immediately by function
1390 declaration or definition. */
1393 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1395 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1397 error_at (parser
->oacc_routine
->loc
,
1398 "%<#pragma acc routine%> not immediately followed by "
1399 "function declaration or definition");
1400 parser
->oacc_routine
= NULL
;
1404 /* Decl-specifiers. */
1406 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1409 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1411 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1416 /* Nothing other than the parser should be creating declarators;
1417 declarators are a semi-syntactic representation of C++ entities.
1418 Other parts of the front end that need to create entities (like
1419 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1421 static cp_declarator
*make_call_declarator
1422 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
, tree
, tree
, tree
, tree
);
1423 static cp_declarator
*make_array_declarator
1424 (cp_declarator
*, tree
);
1425 static cp_declarator
*make_pointer_declarator
1426 (cp_cv_quals
, cp_declarator
*, tree
);
1427 static cp_declarator
*make_reference_declarator
1428 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1429 static cp_declarator
*make_ptrmem_declarator
1430 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1432 /* An erroneous declarator. */
1433 static cp_declarator
*cp_error_declarator
;
1435 /* The obstack on which declarators and related data structures are
1437 static struct obstack declarator_obstack
;
1439 /* Alloc BYTES from the declarator memory pool. */
1441 static inline void *
1442 alloc_declarator (size_t bytes
)
1444 return obstack_alloc (&declarator_obstack
, bytes
);
1447 /* Allocate a declarator of the indicated KIND. Clear fields that are
1448 common to all declarators. */
1450 static cp_declarator
*
1451 make_declarator (cp_declarator_kind kind
)
1453 cp_declarator
*declarator
;
1455 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1456 declarator
->kind
= kind
;
1457 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1458 declarator
->attributes
= NULL_TREE
;
1459 declarator
->std_attributes
= NULL_TREE
;
1460 declarator
->declarator
= NULL
;
1461 declarator
->parameter_pack_p
= false;
1462 declarator
->id_loc
= UNKNOWN_LOCATION
;
1467 /* Make a declarator for a generalized identifier. If
1468 QUALIFYING_SCOPE is non-NULL, the identifier is
1469 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1470 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1473 static cp_declarator
*
1474 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1475 special_function_kind sfk
, location_t id_location
)
1477 cp_declarator
*declarator
;
1479 /* It is valid to write:
1481 class C { void f(); };
1485 The standard is not clear about whether `typedef const C D' is
1486 legal; as of 2002-09-15 the committee is considering that
1487 question. EDG 3.0 allows that syntax. Therefore, we do as
1489 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1490 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1492 gcc_assert (identifier_p (unqualified_name
)
1493 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1494 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1496 declarator
= make_declarator (cdk_id
);
1497 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1498 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1499 declarator
->u
.id
.sfk
= sfk
;
1500 declarator
->id_loc
= id_location
;
1505 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1506 of modifiers such as const or volatile to apply to the pointer
1507 type, represented as identifiers. ATTRIBUTES represent the attributes that
1508 appertain to the pointer or reference. */
1511 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1514 cp_declarator
*declarator
;
1516 declarator
= make_declarator (cdk_pointer
);
1517 declarator
->declarator
= target
;
1518 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1519 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1522 declarator
->id_loc
= target
->id_loc
;
1523 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1524 target
->parameter_pack_p
= false;
1527 declarator
->parameter_pack_p
= false;
1529 declarator
->std_attributes
= attributes
;
1534 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1535 represent the attributes that appertain to the pointer or
1539 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1540 bool rvalue_ref
, tree attributes
)
1542 cp_declarator
*declarator
;
1544 declarator
= make_declarator (cdk_reference
);
1545 declarator
->declarator
= target
;
1546 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1547 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1550 declarator
->id_loc
= target
->id_loc
;
1551 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1552 target
->parameter_pack_p
= false;
1555 declarator
->parameter_pack_p
= false;
1557 declarator
->std_attributes
= attributes
;
1562 /* Like make_pointer_declarator -- but for a pointer to a non-static
1563 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1564 appertain to the pointer or reference. */
1567 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1568 cp_declarator
*pointee
,
1571 cp_declarator
*declarator
;
1573 declarator
= make_declarator (cdk_ptrmem
);
1574 declarator
->declarator
= pointee
;
1575 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1576 declarator
->u
.pointer
.class_type
= class_type
;
1580 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1581 pointee
->parameter_pack_p
= false;
1584 declarator
->parameter_pack_p
= false;
1586 declarator
->std_attributes
= attributes
;
1591 /* Make a declarator for the function given by TARGET, with the
1592 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1593 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1594 indicates what exceptions can be thrown. */
1597 make_call_declarator (cp_declarator
*target
,
1599 cp_cv_quals cv_qualifiers
,
1600 cp_virt_specifiers virt_specifiers
,
1601 cp_ref_qualifier ref_qualifier
,
1603 tree exception_specification
,
1604 tree late_return_type
,
1605 tree requires_clause
)
1607 cp_declarator
*declarator
;
1609 declarator
= make_declarator (cdk_function
);
1610 declarator
->declarator
= target
;
1611 declarator
->u
.function
.parameters
= parms
;
1612 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1613 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1614 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1615 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1616 declarator
->u
.function
.exception_specification
= exception_specification
;
1617 declarator
->u
.function
.late_return_type
= late_return_type
;
1618 declarator
->u
.function
.requires_clause
= requires_clause
;
1621 declarator
->id_loc
= target
->id_loc
;
1622 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1623 target
->parameter_pack_p
= false;
1626 declarator
->parameter_pack_p
= false;
1631 /* Make a declarator for an array of BOUNDS elements, each of which is
1632 defined by ELEMENT. */
1635 make_array_declarator (cp_declarator
*element
, tree bounds
)
1637 cp_declarator
*declarator
;
1639 declarator
= make_declarator (cdk_array
);
1640 declarator
->declarator
= element
;
1641 declarator
->u
.array
.bounds
= bounds
;
1644 declarator
->id_loc
= element
->id_loc
;
1645 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1646 element
->parameter_pack_p
= false;
1649 declarator
->parameter_pack_p
= false;
1654 /* Determine whether the declarator we've seen so far can be a
1655 parameter pack, when followed by an ellipsis. */
1657 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1659 if (declarator
&& declarator
->parameter_pack_p
)
1660 /* We already saw an ellipsis. */
1663 /* Search for a declarator name, or any other declarator that goes
1664 after the point where the ellipsis could appear in a parameter
1665 pack. If we find any of these, then this declarator cannot be
1666 made into a parameter pack. */
1668 while (declarator
&& !found
)
1670 switch ((int)declarator
->kind
)
1682 declarator
= declarator
->declarator
;
1690 cp_parameter_declarator
*no_parameters
;
1692 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1693 DECLARATOR and DEFAULT_ARGUMENT. */
1695 cp_parameter_declarator
*
1696 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1697 cp_declarator
*declarator
,
1698 tree default_argument
,
1700 bool template_parameter_pack_p
= false)
1702 cp_parameter_declarator
*parameter
;
1704 parameter
= ((cp_parameter_declarator
*)
1705 alloc_declarator (sizeof (cp_parameter_declarator
)));
1706 parameter
->next
= NULL
;
1707 if (decl_specifiers
)
1708 parameter
->decl_specifiers
= *decl_specifiers
;
1710 clear_decl_specs (¶meter
->decl_specifiers
);
1711 parameter
->declarator
= declarator
;
1712 parameter
->default_argument
= default_argument
;
1713 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1714 parameter
->loc
= loc
;
1719 /* Returns true iff DECLARATOR is a declaration for a function. */
1722 function_declarator_p (const cp_declarator
*declarator
)
1726 if (declarator
->kind
== cdk_function
1727 && declarator
->declarator
->kind
== cdk_id
)
1729 if (declarator
->kind
== cdk_id
1730 || declarator
->kind
== cdk_decomp
1731 || declarator
->kind
== cdk_error
)
1733 declarator
= declarator
->declarator
;
1743 A cp_parser parses the token stream as specified by the C++
1744 grammar. Its job is purely parsing, not semantic analysis. For
1745 example, the parser breaks the token stream into declarators,
1746 expressions, statements, and other similar syntactic constructs.
1747 It does not check that the types of the expressions on either side
1748 of an assignment-statement are compatible, or that a function is
1749 not declared with a parameter of type `void'.
1751 The parser invokes routines elsewhere in the compiler to perform
1752 semantic analysis and to build up the abstract syntax tree for the
1755 The parser (and the template instantiation code, which is, in a
1756 way, a close relative of parsing) are the only parts of the
1757 compiler that should be calling push_scope and pop_scope, or
1758 related functions. The parser (and template instantiation code)
1759 keeps track of what scope is presently active; everything else
1760 should simply honor that. (The code that generates static
1761 initializers may also need to set the scope, in order to check
1762 access control correctly when emitting the initializers.)
1767 The parser is of the standard recursive-descent variety. Upcoming
1768 tokens in the token stream are examined in order to determine which
1769 production to use when parsing a non-terminal. Some C++ constructs
1770 require arbitrary look ahead to disambiguate. For example, it is
1771 impossible, in the general case, to tell whether a statement is an
1772 expression or declaration without scanning the entire statement.
1773 Therefore, the parser is capable of "parsing tentatively." When the
1774 parser is not sure what construct comes next, it enters this mode.
1775 Then, while we attempt to parse the construct, the parser queues up
1776 error messages, rather than issuing them immediately, and saves the
1777 tokens it consumes. If the construct is parsed successfully, the
1778 parser "commits", i.e., it issues any queued error messages and
1779 the tokens that were being preserved are permanently discarded.
1780 If, however, the construct is not parsed successfully, the parser
1781 rolls back its state completely so that it can resume parsing using
1782 a different alternative.
1787 The performance of the parser could probably be improved substantially.
1788 We could often eliminate the need to parse tentatively by looking ahead
1789 a little bit. In some places, this approach might not entirely eliminate
1790 the need to parse tentatively, but it might still speed up the average
1793 /* Flags that are passed to some parsing functions. These values can
1794 be bitwise-ored together. */
1799 CP_PARSER_FLAGS_NONE
= 0x0,
1800 /* The construct is optional. If it is not present, then no error
1801 should be issued. */
1802 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1803 /* When parsing a type-specifier, treat user-defined type-names
1804 as non-type identifiers. */
1805 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1806 /* When parsing a type-specifier, do not try to parse a class-specifier
1807 or enum-specifier. */
1808 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1809 /* When parsing a decl-specifier-seq, only allow type-specifier or
1811 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1812 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1813 for C++2A consteval. */
1814 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1815 /* When parsing a decl-specifier-seq, allow missing typename. */
1816 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20,
1817 /* When parsing of the noexcept-specifier should be delayed. */
1818 CP_PARSER_FLAGS_DELAY_NOEXCEPT
= 0x40,
1819 /* When parsing a consteval declarator. */
1820 CP_PARSER_FLAGS_CONSTEVAL
= 0x80
1823 /* This type is used for parameters and variables which hold
1824 combinations of the above flags. */
1825 typedef int cp_parser_flags
;
1827 /* The different kinds of declarators we want to parse. */
1829 enum cp_parser_declarator_kind
1831 /* We want an abstract declarator. */
1832 CP_PARSER_DECLARATOR_ABSTRACT
,
1833 /* We want a named declarator. */
1834 CP_PARSER_DECLARATOR_NAMED
,
1835 /* We don't mind, but the name must be an unqualified-id. */
1836 CP_PARSER_DECLARATOR_EITHER
1839 /* The precedence values used to parse binary expressions. The minimum value
1840 of PREC must be 1, because zero is reserved to quickly discriminate
1841 binary operators from other tokens. */
1846 PREC_LOGICAL_OR_EXPRESSION
,
1847 PREC_LOGICAL_AND_EXPRESSION
,
1848 PREC_INCLUSIVE_OR_EXPRESSION
,
1849 PREC_EXCLUSIVE_OR_EXPRESSION
,
1850 PREC_AND_EXPRESSION
,
1851 PREC_EQUALITY_EXPRESSION
,
1852 PREC_RELATIONAL_EXPRESSION
,
1853 PREC_SPACESHIP_EXPRESSION
,
1854 PREC_SHIFT_EXPRESSION
,
1855 PREC_ADDITIVE_EXPRESSION
,
1856 PREC_MULTIPLICATIVE_EXPRESSION
,
1858 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1861 /* A mapping from a token type to a corresponding tree node type, with a
1862 precedence value. */
1864 struct cp_parser_binary_operations_map_node
1866 /* The token type. */
1867 enum cpp_ttype token_type
;
1868 /* The corresponding tree code. */
1869 enum tree_code tree_type
;
1870 /* The precedence of this operator. */
1871 enum cp_parser_prec prec
;
1874 struct cp_parser_expression_stack_entry
1876 /* Left hand side of the binary operation we are currently
1879 /* Original tree code for left hand side, if it was a binary
1880 expression itself (used for -Wparentheses). */
1881 enum tree_code lhs_type
;
1882 /* Tree code for the binary operation we are parsing. */
1883 enum tree_code tree_type
;
1884 /* Precedence of the binary operation we are parsing. */
1885 enum cp_parser_prec prec
;
1886 /* Location of the binary operation we are parsing. */
1890 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1891 entries because precedence levels on the stack are monotonically
1893 typedef struct cp_parser_expression_stack_entry
1894 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1898 /* Constructors and destructors. */
1900 static cp_parser_context
*cp_parser_context_new
1901 (cp_parser_context
*);
1903 /* Class variables. */
1905 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1907 /* The operator-precedence table used by cp_parser_binary_expression.
1908 Transformed into an associative array (binops_by_token) by
1911 static const cp_parser_binary_operations_map_node binops
[] = {
1912 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1913 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1915 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1916 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1917 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1919 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1920 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1922 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1923 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1925 { CPP_SPACESHIP
, SPACESHIP_EXPR
, PREC_SPACESHIP_EXPRESSION
},
1927 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1928 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1929 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1930 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1932 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1933 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1935 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1937 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1939 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1941 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1943 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
1946 /* The same as binops, but initialized by cp_parser_new so that
1947 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1949 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
1951 /* Constructors and destructors. */
1953 /* Construct a new context. The context below this one on the stack
1954 is given by NEXT. */
1956 static cp_parser_context
*
1957 cp_parser_context_new (cp_parser_context
* next
)
1959 cp_parser_context
*context
;
1961 /* Allocate the storage. */
1962 if (cp_parser_context_free_list
!= NULL
)
1964 /* Pull the first entry from the free list. */
1965 context
= cp_parser_context_free_list
;
1966 cp_parser_context_free_list
= context
->next
;
1967 memset (context
, 0, sizeof (*context
));
1970 context
= ggc_cleared_alloc
<cp_parser_context
> ();
1972 /* No errors have occurred yet in this context. */
1973 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
1974 /* If this is not the bottommost context, copy information that we
1975 need from the previous context. */
1978 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1979 expression, then we are parsing one in this context, too. */
1980 context
->object_type
= next
->object_type
;
1981 /* Thread the stack. */
1982 context
->next
= next
;
1988 /* Managing the unparsed function queues. */
1990 #define unparsed_funs_with_default_args \
1991 parser->unparsed_queues->last ().funs_with_default_args
1992 #define unparsed_funs_with_definitions \
1993 parser->unparsed_queues->last ().funs_with_definitions
1994 #define unparsed_nsdmis \
1995 parser->unparsed_queues->last ().nsdmis
1996 #define unparsed_noexcepts \
1997 parser->unparsed_queues->last ().noexcepts
2000 push_unparsed_function_queues (cp_parser
*parser
)
2002 cp_unparsed_functions_entry e
= { NULL
, make_tree_vector (), NULL
, NULL
};
2003 vec_safe_push (parser
->unparsed_queues
, e
);
2007 pop_unparsed_function_queues (cp_parser
*parser
)
2009 release_tree_vector (unparsed_funs_with_definitions
);
2010 parser
->unparsed_queues
->pop ();
2015 /* Constructors and destructors. */
2017 static cp_parser
*cp_parser_new
2020 /* Routines to parse various constructs.
2022 Those that return `tree' will return the error_mark_node (rather
2023 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2024 Sometimes, they will return an ordinary node if error-recovery was
2025 attempted, even though a parse error occurred. So, to check
2026 whether or not a parse error occurred, you should always use
2027 cp_parser_error_occurred. If the construct is optional (indicated
2028 either by an `_opt' in the name of the function that does the
2029 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2030 the construct is not present. */
2032 /* Lexical conventions [gram.lex] */
2034 static cp_expr cp_parser_identifier
2036 static cp_expr cp_parser_string_literal
2037 (cp_parser
*, bool, bool, bool);
2038 static cp_expr cp_parser_userdef_char_literal
2040 static tree cp_parser_userdef_string_literal
2042 static cp_expr cp_parser_userdef_numeric_literal
2045 /* Basic concepts [gram.basic] */
2047 static void cp_parser_translation_unit (cp_parser
*);
2049 /* Expressions [gram.expr] */
2051 static cp_expr cp_parser_primary_expression
2052 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2053 static cp_expr cp_parser_id_expression
2054 (cp_parser
*, bool, bool, bool *, bool, bool);
2055 static cp_expr cp_parser_unqualified_id
2056 (cp_parser
*, bool, bool, bool, bool);
2057 static tree cp_parser_nested_name_specifier_opt
2058 (cp_parser
*, bool, bool, bool, bool, bool = false);
2059 static tree cp_parser_nested_name_specifier
2060 (cp_parser
*, bool, bool, bool, bool);
2061 static tree cp_parser_qualifying_entity
2062 (cp_parser
*, bool, bool, bool, bool, bool);
2063 static cp_expr cp_parser_postfix_expression
2064 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2065 static tree cp_parser_postfix_open_square_expression
2066 (cp_parser
*, tree
, bool, bool);
2067 static tree cp_parser_postfix_dot_deref_expression
2068 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2069 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2070 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2072 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2073 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2074 static void cp_parser_pseudo_destructor_name
2075 (cp_parser
*, tree
, tree
*, tree
*);
2076 static cp_expr cp_parser_unary_expression
2077 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2078 static enum tree_code cp_parser_unary_operator
2080 static tree cp_parser_has_attribute_expression
2082 static tree cp_parser_new_expression
2084 static vec
<tree
, va_gc
> *cp_parser_new_placement
2086 static tree cp_parser_new_type_id
2087 (cp_parser
*, tree
*);
2088 static cp_declarator
*cp_parser_new_declarator_opt
2090 static cp_declarator
*cp_parser_direct_new_declarator
2092 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2094 static tree cp_parser_delete_expression
2096 static cp_expr cp_parser_cast_expression
2097 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2098 static cp_expr cp_parser_binary_expression
2099 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2100 static tree cp_parser_question_colon_clause
2101 (cp_parser
*, cp_expr
);
2102 static cp_expr cp_parser_assignment_expression
2103 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2104 static enum tree_code cp_parser_assignment_operator_opt
2106 static cp_expr cp_parser_expression
2107 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2108 static cp_expr cp_parser_constant_expression
2109 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2110 static cp_expr cp_parser_builtin_offsetof
2112 static cp_expr cp_parser_lambda_expression
2114 static void cp_parser_lambda_introducer
2115 (cp_parser
*, tree
);
2116 static bool cp_parser_lambda_declarator_opt
2117 (cp_parser
*, tree
);
2118 static void cp_parser_lambda_body
2119 (cp_parser
*, tree
);
2121 /* Statements [gram.stmt.stmt] */
2123 static void cp_parser_statement
2124 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2125 static void cp_parser_label_for_labeled_statement
2126 (cp_parser
*, tree
);
2127 static tree cp_parser_expression_statement
2128 (cp_parser
*, tree
);
2129 static tree cp_parser_compound_statement
2130 (cp_parser
*, tree
, int, bool);
2131 static void cp_parser_statement_seq_opt
2132 (cp_parser
*, tree
);
2133 static tree cp_parser_selection_statement
2134 (cp_parser
*, bool *, vec
<tree
> *);
2135 static tree cp_parser_condition
2137 static tree cp_parser_iteration_statement
2138 (cp_parser
*, bool *, bool, unsigned short);
2139 static bool cp_parser_init_statement
2140 (cp_parser
*, tree
*decl
);
2141 static tree cp_parser_for
2142 (cp_parser
*, bool, unsigned short);
2143 static tree cp_parser_c_for
2144 (cp_parser
*, tree
, tree
, bool, unsigned short);
2145 static tree cp_parser_range_for
2146 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2147 static void do_range_for_auto_deduction
2149 static tree cp_parser_perform_range_for_lookup
2150 (tree
, tree
*, tree
*);
2151 static tree cp_parser_range_for_member_function
2153 static tree cp_parser_jump_statement
2155 static void cp_parser_declaration_statement
2158 static tree cp_parser_implicitly_scoped_statement
2159 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2160 static void cp_parser_already_scoped_statement
2161 (cp_parser
*, bool *, const token_indent_info
&);
2163 /* Declarations [gram.dcl.dcl] */
2165 static void cp_parser_declaration_seq_opt
2167 static void cp_parser_declaration
2169 static void cp_parser_toplevel_declaration
2171 static void cp_parser_block_declaration
2172 (cp_parser
*, bool);
2173 static void cp_parser_simple_declaration
2174 (cp_parser
*, bool, tree
*);
2175 static void cp_parser_decl_specifier_seq
2176 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2177 static tree cp_parser_storage_class_specifier_opt
2179 static tree cp_parser_function_specifier_opt
2180 (cp_parser
*, cp_decl_specifier_seq
*);
2181 static tree cp_parser_type_specifier
2182 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2184 static tree cp_parser_simple_type_specifier
2185 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2186 static tree cp_parser_placeholder_type_specifier
2187 (cp_parser
*, location_t
, tree
, bool);
2188 static tree cp_parser_type_name
2189 (cp_parser
*, bool);
2190 static tree cp_parser_nonclass_name
2191 (cp_parser
* parser
);
2192 static tree cp_parser_elaborated_type_specifier
2193 (cp_parser
*, bool, bool);
2194 static tree cp_parser_enum_specifier
2196 static void cp_parser_enumerator_list
2197 (cp_parser
*, tree
);
2198 static void cp_parser_enumerator_definition
2199 (cp_parser
*, tree
);
2200 static tree cp_parser_namespace_name
2202 static void cp_parser_namespace_definition
2204 static void cp_parser_namespace_body
2206 static tree cp_parser_qualified_namespace_specifier
2208 static void cp_parser_namespace_alias_definition
2210 static bool cp_parser_using_declaration
2211 (cp_parser
*, bool);
2212 static void cp_parser_using_directive
2214 static tree cp_parser_alias_declaration
2216 static void cp_parser_asm_definition
2218 static void cp_parser_linkage_specification
2220 static void cp_parser_static_assert
2221 (cp_parser
*, bool);
2222 static tree cp_parser_decltype
2224 static tree cp_parser_decomposition_declaration
2225 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2227 /* Declarators [gram.dcl.decl] */
2229 static tree cp_parser_init_declarator
2230 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2231 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2232 location_t
*, tree
*);
2233 static cp_declarator
*cp_parser_declarator
2234 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2236 static cp_declarator
*cp_parser_direct_declarator
2237 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2239 static enum tree_code cp_parser_ptr_operator
2240 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2241 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2243 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2245 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2247 static tree cp_parser_tx_qualifier_opt
2249 static tree cp_parser_late_return_type_opt
2250 (cp_parser
*, cp_declarator
*, tree
&, cp_cv_quals
);
2251 static tree cp_parser_declarator_id
2252 (cp_parser
*, bool);
2253 static tree cp_parser_type_id
2254 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2255 static tree cp_parser_template_type_arg
2257 static tree
cp_parser_trailing_type_id (cp_parser
*);
2258 static tree cp_parser_type_id_1
2259 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2260 static void cp_parser_type_specifier_seq
2261 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2262 static tree cp_parser_parameter_declaration_clause
2263 (cp_parser
*, cp_parser_flags
);
2264 static tree cp_parser_parameter_declaration_list
2265 (cp_parser
*, cp_parser_flags
);
2266 static cp_parameter_declarator
*cp_parser_parameter_declaration
2267 (cp_parser
*, cp_parser_flags
, bool, bool *);
2268 static tree cp_parser_default_argument
2269 (cp_parser
*, bool);
2270 static void cp_parser_function_body
2271 (cp_parser
*, bool);
2272 static tree cp_parser_initializer
2273 (cp_parser
*, bool *, bool *, bool = false);
2274 static cp_expr cp_parser_initializer_clause
2275 (cp_parser
*, bool *);
2276 static cp_expr cp_parser_braced_list
2277 (cp_parser
*, bool*);
2278 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2279 (cp_parser
*, bool *, bool *);
2281 static void cp_parser_ctor_initializer_opt_and_function_body
2282 (cp_parser
*, bool);
2284 static tree cp_parser_late_parsing_omp_declare_simd
2285 (cp_parser
*, tree
);
2287 static tree cp_parser_late_parsing_oacc_routine
2288 (cp_parser
*, tree
);
2290 static tree synthesize_implicit_template_parm
2291 (cp_parser
*, tree
);
2292 static tree finish_fully_implicit_template
2293 (cp_parser
*, tree
);
2294 static void abort_fully_implicit_template
2297 /* Classes [gram.class] */
2299 static tree cp_parser_class_name
2300 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2301 static tree cp_parser_class_specifier
2303 static tree cp_parser_class_head
2304 (cp_parser
*, bool *);
2305 static enum tag_types cp_parser_class_key
2307 static void cp_parser_type_parameter_key
2308 (cp_parser
* parser
);
2309 static void cp_parser_member_specification_opt
2311 static void cp_parser_member_declaration
2313 static tree cp_parser_pure_specifier
2315 static tree cp_parser_constant_initializer
2318 /* Derived classes [gram.class.derived] */
2320 static tree cp_parser_base_clause
2322 static tree cp_parser_base_specifier
2325 /* Special member functions [gram.special] */
2327 static tree cp_parser_conversion_function_id
2329 static tree cp_parser_conversion_type_id
2331 static cp_declarator
*cp_parser_conversion_declarator_opt
2333 static void cp_parser_ctor_initializer_opt
2335 static void cp_parser_mem_initializer_list
2337 static tree cp_parser_mem_initializer
2339 static tree cp_parser_mem_initializer_id
2342 /* Overloading [gram.over] */
2344 static cp_expr cp_parser_operator_function_id
2346 static cp_expr cp_parser_operator
2347 (cp_parser
*, location_t
);
2349 /* Templates [gram.temp] */
2351 static void cp_parser_template_declaration
2352 (cp_parser
*, bool);
2353 static tree cp_parser_template_parameter_list
2355 static tree cp_parser_template_parameter
2356 (cp_parser
*, bool *, bool *);
2357 static tree cp_parser_type_parameter
2358 (cp_parser
*, bool *);
2359 static tree cp_parser_template_id
2360 (cp_parser
*, bool, bool, enum tag_types
, bool);
2361 static tree cp_parser_template_id_expr
2362 (cp_parser
*, bool, bool, bool);
2363 static tree cp_parser_template_name
2364 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2365 static tree cp_parser_template_argument_list
2367 static tree cp_parser_template_argument
2369 static void cp_parser_explicit_instantiation
2371 static void cp_parser_explicit_specialization
2374 /* Exception handling [gram.except] */
2376 static tree cp_parser_try_block
2378 static void cp_parser_function_try_block
2380 static void cp_parser_handler_seq
2382 static void cp_parser_handler
2384 static tree cp_parser_exception_declaration
2386 static tree cp_parser_throw_expression
2388 static tree cp_parser_exception_specification_opt
2389 (cp_parser
*, cp_parser_flags
);
2390 static tree cp_parser_type_id_list
2392 static tree cp_parser_noexcept_specification_opt
2393 (cp_parser
*, cp_parser_flags
, bool, bool *, bool);
2395 /* GNU Extensions */
2397 static tree cp_parser_asm_specification_opt
2399 static tree cp_parser_asm_operand_list
2401 static tree cp_parser_asm_clobber_list
2403 static tree cp_parser_asm_label_list
2405 static bool cp_next_tokens_can_be_attribute_p
2407 static bool cp_next_tokens_can_be_gnu_attribute_p
2409 static bool cp_next_tokens_can_be_std_attribute_p
2411 static bool cp_nth_tokens_can_be_std_attribute_p
2412 (cp_parser
*, size_t);
2413 static bool cp_nth_tokens_can_be_gnu_attribute_p
2414 (cp_parser
*, size_t);
2415 static bool cp_nth_tokens_can_be_attribute_p
2416 (cp_parser
*, size_t);
2417 static tree cp_parser_attributes_opt
2419 static tree cp_parser_gnu_attributes_opt
2421 static tree cp_parser_gnu_attribute_list
2422 (cp_parser
*, bool = false);
2423 static tree cp_parser_std_attribute
2424 (cp_parser
*, tree
);
2425 static tree cp_parser_std_attribute_spec
2427 static tree cp_parser_std_attribute_spec_seq
2429 static size_t cp_parser_skip_attributes_opt
2430 (cp_parser
*, size_t);
2431 static bool cp_parser_extension_opt
2432 (cp_parser
*, int *);
2433 static void cp_parser_label_declaration
2436 /* Concept Extensions */
2438 static tree cp_parser_concept_definition
2440 static tree cp_parser_constraint_expression
2442 static tree cp_parser_requires_clause_opt
2443 (cp_parser
*, bool);
2444 static tree cp_parser_requires_expression
2446 static tree cp_parser_requirement_parameter_list
2448 static tree cp_parser_requirement_body
2450 static tree cp_parser_requirement_seq
2452 static tree cp_parser_requirement
2454 static tree cp_parser_simple_requirement
2456 static tree cp_parser_compound_requirement
2458 static tree cp_parser_type_requirement
2460 static tree cp_parser_nested_requirement
2463 /* Transactional Memory Extensions */
2465 static tree cp_parser_transaction
2466 (cp_parser
*, cp_token
*);
2467 static tree cp_parser_transaction_expression
2468 (cp_parser
*, enum rid
);
2469 static void cp_parser_function_transaction
2470 (cp_parser
*, enum rid
);
2471 static tree cp_parser_transaction_cancel
2474 enum pragma_context
{
2481 static bool cp_parser_pragma
2482 (cp_parser
*, enum pragma_context
, bool *);
2484 /* Objective-C++ Productions */
2486 static tree cp_parser_objc_message_receiver
2488 static tree cp_parser_objc_message_args
2490 static tree cp_parser_objc_message_expression
2492 static cp_expr cp_parser_objc_encode_expression
2494 static tree cp_parser_objc_defs_expression
2496 static tree cp_parser_objc_protocol_expression
2498 static tree cp_parser_objc_selector_expression
2500 static cp_expr cp_parser_objc_expression
2502 static bool cp_parser_objc_selector_p
2504 static tree cp_parser_objc_selector
2506 static tree cp_parser_objc_protocol_refs_opt
2508 static void cp_parser_objc_declaration
2509 (cp_parser
*, tree
);
2510 static tree cp_parser_objc_statement
2512 static bool cp_parser_objc_valid_prefix_attributes
2513 (cp_parser
*, tree
*);
2514 static void cp_parser_objc_at_property_declaration
2516 static void cp_parser_objc_at_synthesize_declaration
2518 static void cp_parser_objc_at_dynamic_declaration
2520 static tree cp_parser_objc_struct_declaration
2523 /* Utility Routines */
2525 static cp_expr cp_parser_lookup_name
2526 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2527 static tree cp_parser_lookup_name_simple
2528 (cp_parser
*, tree
, location_t
);
2529 static tree cp_parser_maybe_treat_template_as_class
2531 static bool cp_parser_check_declarator_template_parameters
2532 (cp_parser
*, cp_declarator
*, location_t
);
2533 static bool cp_parser_check_template_parameters
2534 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2535 static cp_expr cp_parser_simple_cast_expression
2537 static tree cp_parser_global_scope_opt
2538 (cp_parser
*, bool);
2539 static bool cp_parser_constructor_declarator_p
2540 (cp_parser
*, cp_parser_flags
, bool);
2541 static tree cp_parser_function_definition_from_specifiers_and_declarator
2542 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2543 static tree cp_parser_function_definition_after_declarator
2544 (cp_parser
*, bool);
2545 static bool cp_parser_template_declaration_after_export
2546 (cp_parser
*, bool);
2547 static void cp_parser_perform_template_parameter_access_checks
2548 (vec
<deferred_access_check
, va_gc
> *);
2549 static tree cp_parser_single_declaration
2550 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2551 static cp_expr cp_parser_functional_cast
2552 (cp_parser
*, tree
);
2553 static tree cp_parser_save_member_function_body
2554 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2555 static tree cp_parser_save_nsdmi
2557 static tree cp_parser_enclosed_template_argument_list
2559 static void cp_parser_save_default_args
2560 (cp_parser
*, tree
);
2561 static void cp_parser_late_parsing_for_member
2562 (cp_parser
*, tree
);
2563 static tree cp_parser_late_parse_one_default_arg
2564 (cp_parser
*, tree
, tree
, tree
);
2565 static void cp_parser_late_parsing_nsdmi
2566 (cp_parser
*, tree
);
2567 static void cp_parser_late_parsing_default_args
2568 (cp_parser
*, tree
);
2569 static tree cp_parser_sizeof_operand
2570 (cp_parser
*, enum rid
);
2571 static cp_expr cp_parser_trait_expr
2572 (cp_parser
*, enum rid
);
2573 static bool cp_parser_declares_only_class_p
2575 static void cp_parser_set_storage_class
2576 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2577 static void cp_parser_set_decl_spec_type
2578 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2579 static void set_and_check_decl_spec_loc
2580 (cp_decl_specifier_seq
*decl_specs
,
2581 cp_decl_spec ds
, cp_token
*);
2582 static bool cp_parser_friend_p
2583 (const cp_decl_specifier_seq
*);
2584 static void cp_parser_required_error
2585 (cp_parser
*, required_token
, bool, location_t
);
2586 static cp_token
*cp_parser_require
2587 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2588 static cp_token
*cp_parser_require_keyword
2589 (cp_parser
*, enum rid
, required_token
);
2590 static bool cp_parser_token_starts_function_definition_p
2592 static bool cp_parser_next_token_starts_class_definition_p
2594 static bool cp_parser_next_token_ends_template_argument_p
2596 static bool cp_parser_nth_token_starts_template_argument_list_p
2597 (cp_parser
*, size_t);
2598 static enum tag_types cp_parser_token_is_class_key
2600 static enum tag_types cp_parser_token_is_type_parameter_key
2602 static void cp_parser_check_class_key
2603 (enum tag_types
, tree type
);
2604 static void cp_parser_check_access_in_redeclaration
2605 (tree type
, location_t location
);
2606 static bool cp_parser_optional_template_keyword
2608 static void cp_parser_pre_parsed_nested_name_specifier
2610 static bool cp_parser_cache_group
2611 (cp_parser
*, enum cpp_ttype
, unsigned);
2612 static tree cp_parser_cache_defarg
2613 (cp_parser
*parser
, bool nsdmi
);
2614 static void cp_parser_parse_tentatively
2616 static void cp_parser_commit_to_tentative_parse
2618 static void cp_parser_commit_to_topmost_tentative_parse
2620 static void cp_parser_abort_tentative_parse
2622 static bool cp_parser_parse_definitely
2624 static inline bool cp_parser_parsing_tentatively
2626 static bool cp_parser_uncommitted_to_tentative_parse_p
2628 static void cp_parser_error
2629 (cp_parser
*, const char *);
2630 static void cp_parser_name_lookup_error
2631 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2632 static bool cp_parser_simulate_error
2634 static bool cp_parser_check_type_definition
2636 static void cp_parser_check_for_definition_in_return_type
2637 (cp_declarator
*, tree
, location_t type_location
);
2638 static void cp_parser_check_for_invalid_template_id
2639 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2640 static bool cp_parser_non_integral_constant_expression
2641 (cp_parser
*, non_integral_constant
);
2642 static void cp_parser_diagnose_invalid_type_name
2643 (cp_parser
*, tree
, location_t
);
2644 static bool cp_parser_parse_and_diagnose_invalid_type_name
2646 static int cp_parser_skip_to_closing_parenthesis
2647 (cp_parser
*, bool, bool, bool);
2648 static void cp_parser_skip_to_end_of_statement
2650 static void cp_parser_consume_semicolon_at_end_of_statement
2652 static void cp_parser_skip_to_end_of_block_or_statement
2654 static bool cp_parser_skip_to_closing_brace
2656 static void cp_parser_skip_to_end_of_template_parameter_list
2658 static void cp_parser_skip_to_pragma_eol
2659 (cp_parser
*, cp_token
*);
2660 static bool cp_parser_error_occurred
2662 static bool cp_parser_allow_gnu_extensions_p
2664 static bool cp_parser_is_pure_string_literal
2666 static bool cp_parser_is_string_literal
2668 static bool cp_parser_is_keyword
2669 (cp_token
*, enum rid
);
2670 static tree cp_parser_make_typename_type
2671 (cp_parser
*, tree
, location_t location
);
2672 static cp_declarator
* cp_parser_make_indirect_declarator
2673 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2674 static bool cp_parser_compound_literal_p
2676 static bool cp_parser_array_designator_p
2678 static bool cp_parser_init_statement_p
2680 static bool cp_parser_skip_to_closing_square_bracket
2682 static size_t cp_parser_skip_balanced_tokens (cp_parser
*, size_t);
2684 // -------------------------------------------------------------------------- //
2685 // Unevaluated Operand Guard
2687 // Implementation of an RAII helper for unevaluated operand parsing.
2688 cp_unevaluated::cp_unevaluated ()
2690 ++cp_unevaluated_operand
;
2691 ++c_inhibit_evaluation_warnings
;
2694 cp_unevaluated::~cp_unevaluated ()
2696 --c_inhibit_evaluation_warnings
;
2697 --cp_unevaluated_operand
;
2700 // -------------------------------------------------------------------------- //
2701 // Tentative Parsing
2703 /* Returns nonzero if we are parsing tentatively. */
2706 cp_parser_parsing_tentatively (cp_parser
* parser
)
2708 return parser
->context
->next
!= NULL
;
2711 /* Returns nonzero if TOKEN is a string literal. */
2714 cp_parser_is_pure_string_literal (cp_token
* token
)
2716 return (token
->type
== CPP_STRING
||
2717 token
->type
== CPP_STRING16
||
2718 token
->type
== CPP_STRING32
||
2719 token
->type
== CPP_WSTRING
||
2720 token
->type
== CPP_UTF8STRING
);
2723 /* Returns nonzero if TOKEN is a string literal
2724 of a user-defined string literal. */
2727 cp_parser_is_string_literal (cp_token
* token
)
2729 return (cp_parser_is_pure_string_literal (token
) ||
2730 token
->type
== CPP_STRING_USERDEF
||
2731 token
->type
== CPP_STRING16_USERDEF
||
2732 token
->type
== CPP_STRING32_USERDEF
||
2733 token
->type
== CPP_WSTRING_USERDEF
||
2734 token
->type
== CPP_UTF8STRING_USERDEF
);
2737 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2740 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2742 return token
->keyword
== keyword
;
2745 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2748 static enum pragma_kind
2749 cp_parser_pragma_kind (cp_token
*token
)
2751 if (token
->type
!= CPP_PRAGMA
)
2753 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2754 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2757 /* Helper function for cp_parser_error.
2758 Having peeked a token of kind TOK1_KIND that might signify
2759 a conflict marker, peek successor tokens to determine
2760 if we actually do have a conflict marker.
2761 Specifically, we consider a run of 7 '<', '=' or '>' characters
2762 at the start of a line as a conflict marker.
2763 These come through the lexer as three pairs and a single,
2764 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2765 If it returns true, *OUT_LOC is written to with the location/range
2769 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2770 location_t
*out_loc
)
2772 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2773 if (token2
->type
!= tok1_kind
)
2775 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2776 if (token3
->type
!= tok1_kind
)
2778 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2779 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2782 /* It must be at the start of the line. */
2783 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2784 if (LOCATION_COLUMN (start_loc
) != 1)
2787 /* We have a conflict marker. Construct a location of the form:
2790 with start == caret, finishing at the end of the marker. */
2791 location_t finish_loc
= get_finish (token4
->location
);
2792 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2797 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2801 get_matching_symbol (required_token token_desc
)
2808 case RT_CLOSE_BRACE
:
2810 case RT_CLOSE_PAREN
:
2815 /* Attempt to convert TOKEN_DESC from a required_token to an
2816 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2818 static enum cpp_ttype
2819 get_required_cpp_ttype (required_token token_desc
)
2824 return CPP_SEMICOLON
;
2826 return CPP_OPEN_PAREN
;
2827 case RT_CLOSE_BRACE
:
2828 return CPP_CLOSE_BRACE
;
2830 return CPP_OPEN_BRACE
;
2831 case RT_CLOSE_SQUARE
:
2832 return CPP_CLOSE_SQUARE
;
2833 case RT_OPEN_SQUARE
:
2834 return CPP_OPEN_SQUARE
;
2839 case RT_CLOSE_PAREN
:
2840 return CPP_CLOSE_PAREN
;
2843 /* Use CPP_EOF as a "no completions possible" code. */
2849 /* Subroutine of cp_parser_error and cp_parser_required_error.
2851 Issue a diagnostic of the form
2852 FILE:LINE: MESSAGE before TOKEN
2853 where TOKEN is the next token in the input stream. MESSAGE
2854 (specified by the caller) is usually of the form "expected
2857 This bypasses the check for tentative passing, and potentially
2858 adds material needed by cp_parser_required_error.
2860 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2861 suggesting insertion of the missing token.
2863 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2864 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2868 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2869 required_token missing_token_desc
,
2870 location_t matching_location
)
2872 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2873 /* This diagnostic makes more sense if it is tagged to the line
2874 of the token we just peeked at. */
2875 cp_lexer_set_source_position_from_token (token
);
2877 if (token
->type
== CPP_PRAGMA
)
2879 error_at (token
->location
,
2880 "%<#pragma%> is not allowed here");
2881 cp_parser_skip_to_pragma_eol (parser
, token
);
2885 /* If this is actually a conflict marker, report it as such. */
2886 if (token
->type
== CPP_LSHIFT
2887 || token
->type
== CPP_RSHIFT
2888 || token
->type
== CPP_EQ_EQ
)
2891 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2893 error_at (loc
, "version control conflict marker in file");
2894 expanded_location token_exploc
= expand_location (token
->location
);
2895 /* Consume tokens until the end of the source line. */
2898 cp_lexer_consume_token (parser
->lexer
);
2899 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2900 if (next
->type
== CPP_EOF
)
2902 if (next
->location
== UNKNOWN_LOCATION
2903 || loc
== UNKNOWN_LOCATION
)
2906 expanded_location next_exploc
= expand_location (next
->location
);
2907 if (next_exploc
.file
!= token_exploc
.file
)
2909 if (next_exploc
.line
!= token_exploc
.line
)
2916 gcc_rich_location
richloc (input_location
);
2918 bool added_matching_location
= false;
2920 if (missing_token_desc
!= RT_NONE
)
2922 /* Potentially supply a fix-it hint, suggesting to add the
2923 missing token immediately after the *previous* token.
2924 This may move the primary location within richloc. */
2925 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
2926 location_t prev_token_loc
2927 = cp_lexer_previous_token (parser
->lexer
)->location
;
2928 maybe_suggest_missing_token_insertion (&richloc
, ttype
, prev_token_loc
);
2930 /* If matching_location != UNKNOWN_LOCATION, highlight it.
2931 Attempt to consolidate diagnostics by printing it as a
2932 secondary range within the main diagnostic. */
2933 if (matching_location
!= UNKNOWN_LOCATION
)
2934 added_matching_location
2935 = richloc
.add_location_if_nearby (matching_location
);
2938 /* Actually emit the error. */
2939 c_parse_error (gmsgid
,
2940 /* Because c_parser_error does not understand
2941 CPP_KEYWORD, keywords are treated like
2943 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
2944 token
->u
.value
, token
->flags
, &richloc
);
2946 if (missing_token_desc
!= RT_NONE
)
2948 /* If we weren't able to consolidate matching_location, then
2949 print it as a secondary diagnostic. */
2950 if (matching_location
!= UNKNOWN_LOCATION
2951 && !added_matching_location
)
2952 inform (matching_location
, "to match this %qs",
2953 get_matching_symbol (missing_token_desc
));
2957 /* If not parsing tentatively, issue a diagnostic of the form
2958 FILE:LINE: MESSAGE before TOKEN
2959 where TOKEN is the next token in the input stream. MESSAGE
2960 (specified by the caller) is usually of the form "expected
2964 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
2966 if (!cp_parser_simulate_error (parser
))
2967 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
2970 /* Issue an error about name-lookup failing. NAME is the
2971 IDENTIFIER_NODE DECL is the result of
2972 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2973 the thing that we hoped to find. */
2976 cp_parser_name_lookup_error (cp_parser
* parser
,
2979 name_lookup_error desired
,
2980 location_t location
)
2982 /* If name lookup completely failed, tell the user that NAME was not
2984 if (decl
== error_mark_node
)
2986 if (parser
->scope
&& parser
->scope
!= global_namespace
)
2987 error_at (location
, "%<%E::%E%> has not been declared",
2988 parser
->scope
, name
);
2989 else if (parser
->scope
== global_namespace
)
2990 error_at (location
, "%<::%E%> has not been declared", name
);
2991 else if (parser
->object_scope
2992 && !CLASS_TYPE_P (parser
->object_scope
))
2993 error_at (location
, "request for member %qE in non-class type %qT",
2994 name
, parser
->object_scope
);
2995 else if (parser
->object_scope
)
2996 error_at (location
, "%<%T::%E%> has not been declared",
2997 parser
->object_scope
, name
);
2999 error_at (location
, "%qE has not been declared", name
);
3001 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
3006 error_at (location
, "%<%E::%E%> is not a type",
3007 parser
->scope
, name
);
3010 error_at (location
, "%<%E::%E%> is not a class or namespace",
3011 parser
->scope
, name
);
3015 "%<%E::%E%> is not a class, namespace, or enumeration",
3016 parser
->scope
, name
);
3023 else if (parser
->scope
== global_namespace
)
3028 error_at (location
, "%<::%E%> is not a type", name
);
3031 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3035 "%<::%E%> is not a class, namespace, or enumeration",
3047 error_at (location
, "%qE is not a type", name
);
3050 error_at (location
, "%qE is not a class or namespace", name
);
3054 "%qE is not a class, namespace, or enumeration", name
);
3062 /* If we are parsing tentatively, remember that an error has occurred
3063 during this tentative parse. Returns true if the error was
3064 simulated; false if a message should be issued by the caller. */
3067 cp_parser_simulate_error (cp_parser
* parser
)
3069 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3071 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3077 /* This function is called when a type is defined. If type
3078 definitions are forbidden at this point, an error message is
3082 cp_parser_check_type_definition (cp_parser
* parser
)
3084 /* If types are forbidden here, issue a message. */
3085 if (parser
->type_definition_forbidden_message
)
3087 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3088 or %qs in the message need to be interpreted. */
3089 error (parser
->type_definition_forbidden_message
,
3090 parser
->type_definition_forbidden_message_arg
);
3096 /* This function is called when the DECLARATOR is processed. The TYPE
3097 was a type defined in the decl-specifiers. If it is invalid to
3098 define a type in the decl-specifiers for DECLARATOR, an error is
3099 issued. TYPE_LOCATION is the location of TYPE and is used
3100 for error reporting. */
3103 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3104 tree type
, location_t type_location
)
3106 /* [dcl.fct] forbids type definitions in return types.
3107 Unfortunately, it's not easy to know whether or not we are
3108 processing a return type until after the fact. */
3110 && (declarator
->kind
== cdk_pointer
3111 || declarator
->kind
== cdk_reference
3112 || declarator
->kind
== cdk_ptrmem
))
3113 declarator
= declarator
->declarator
;
3115 && declarator
->kind
== cdk_function
)
3117 error_at (type_location
,
3118 "new types may not be defined in a return type");
3119 inform (type_location
,
3120 "(perhaps a semicolon is missing after the definition of %qT)",
3125 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3126 "<" in any valid C++ program. If the next token is indeed "<",
3127 issue a message warning the user about what appears to be an
3128 invalid attempt to form a template-id. LOCATION is the location
3129 of the type-specifier (TYPE) */
3132 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3134 enum tag_types tag_type
,
3135 location_t location
)
3137 cp_token_position start
= 0;
3139 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3141 if (TREE_CODE (type
) == TYPE_DECL
)
3142 type
= TREE_TYPE (type
);
3143 if (TYPE_P (type
) && !template_placeholder_p (type
))
3144 error_at (location
, "%qT is not a template", type
);
3145 else if (identifier_p (type
))
3147 if (tag_type
!= none_type
)
3148 error_at (location
, "%qE is not a class template", type
);
3150 error_at (location
, "%qE is not a template", type
);
3153 error_at (location
, "invalid template-id");
3154 /* Remember the location of the invalid "<". */
3155 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3156 start
= cp_lexer_token_position (parser
->lexer
, true);
3157 /* Consume the "<". */
3158 cp_lexer_consume_token (parser
->lexer
);
3159 /* Parse the template arguments. */
3160 cp_parser_enclosed_template_argument_list (parser
);
3161 /* Permanently remove the invalid template arguments so that
3162 this error message is not issued again. */
3164 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3168 /* If parsing an integral constant-expression, issue an error message
3169 about the fact that THING appeared and return true. Otherwise,
3170 return false. In either case, set
3171 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3174 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3175 non_integral_constant thing
)
3177 parser
->non_integral_constant_expression_p
= true;
3178 if (parser
->integral_constant_expression_p
)
3180 if (!parser
->allow_non_integral_constant_expression_p
)
3182 const char *msg
= NULL
;
3186 pedwarn (input_location
, OPT_Wpedantic
,
3187 "ISO C++ forbids using a floating-point literal "
3188 "in a constant-expression");
3191 error ("a cast to a type other than an integral or "
3192 "enumeration type cannot appear in a "
3193 "constant-expression");
3196 error ("%<typeid%> operator "
3197 "cannot appear in a constant-expression");
3200 error ("non-constant compound literals "
3201 "cannot appear in a constant-expression");
3204 error ("a function call "
3205 "cannot appear in a constant-expression");
3208 error ("an increment "
3209 "cannot appear in a constant-expression");
3212 error ("an decrement "
3213 "cannot appear in a constant-expression");
3216 error ("an array reference "
3217 "cannot appear in a constant-expression");
3219 case NIC_ADDR_LABEL
:
3220 error ("the address of a label "
3221 "cannot appear in a constant-expression");
3223 case NIC_OVERLOADED
:
3224 error ("calls to overloaded operators "
3225 "cannot appear in a constant-expression");
3227 case NIC_ASSIGNMENT
:
3228 error ("an assignment cannot appear in a constant-expression");
3231 error ("a comma operator "
3232 "cannot appear in a constant-expression");
3234 case NIC_CONSTRUCTOR
:
3235 error ("a call to a constructor "
3236 "cannot appear in a constant-expression");
3238 case NIC_TRANSACTION
:
3239 error ("a transaction expression "
3240 "cannot appear in a constant-expression");
3246 msg
= "__FUNCTION__";
3248 case NIC_PRETTY_FUNC
:
3249 msg
= "__PRETTY_FUNCTION__";
3269 case NIC_PREINCREMENT
:
3272 case NIC_PREDECREMENT
:
3285 error ("%qs cannot appear in a constant-expression", msg
);
3292 /* Emit a diagnostic for an invalid type name. This function commits
3293 to the current active tentative parse, if any. (Otherwise, the
3294 problematic construct might be encountered again later, resulting
3295 in duplicate error messages.) LOCATION is the location of ID. */
3298 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3299 location_t location
)
3301 tree decl
, ambiguous_decls
;
3302 cp_parser_commit_to_tentative_parse (parser
);
3303 /* Try to lookup the identifier. */
3304 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3305 /*is_template=*/false,
3306 /*is_namespace=*/false,
3307 /*check_dependency=*/true,
3308 &ambiguous_decls
, location
);
3309 if (ambiguous_decls
)
3310 /* If the lookup was ambiguous, an error will already have
3313 /* If the lookup found a template-name, it means that the user forgot
3314 to specify an argument list. Emit a useful error message. */
3315 if (DECL_TYPE_TEMPLATE_P (decl
))
3317 auto_diagnostic_group d
;
3319 "invalid use of template-name %qE without an argument list",
3321 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3322 inform (location
, "class template argument deduction is only available "
3323 "with %<-std=c++17%> or %<-std=gnu++17%>");
3324 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3326 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3327 error_at (location
, "invalid use of destructor %qD as a type", id
);
3328 else if (TREE_CODE (decl
) == TYPE_DECL
)
3329 /* Something like 'unsigned A a;' */
3330 error_at (location
, "invalid combination of multiple type-specifiers");
3331 else if (!parser
->scope
)
3333 /* Issue an error message. */
3334 auto_diagnostic_group d
;
3336 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3337 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3338 if (const char *suggestion
= hint
.suggestion ())
3340 gcc_rich_location
richloc (location
);
3341 richloc
.add_fixit_replace (suggestion
);
3343 "%qE does not name a type; did you mean %qs?",
3347 error_at (location
, "%qE does not name a type", id
);
3348 /* If we're in a template class, it's possible that the user was
3349 referring to a type from a base class. For example:
3351 template <typename T> struct A { typedef T X; };
3352 template <typename T> struct B : public A<T> { X x; };
3354 The user should have said "typename A<T>::X". */
3355 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3356 inform (location
, "C++11 %<constexpr%> only available with "
3357 "%<-std=c++11%> or %<-std=gnu++11%>");
3358 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3359 inform (location
, "C++11 %<noexcept%> only available with "
3360 "%<-std=c++11%> or %<-std=gnu++11%>");
3361 else if (cxx_dialect
< cxx11
3362 && TREE_CODE (id
) == IDENTIFIER_NODE
3363 && id_equal (id
, "thread_local"))
3364 inform (location
, "C++11 %<thread_local%> only available with "
3365 "%<-std=c++11%> or %<-std=gnu++11%>");
3366 else if (cxx_dialect
< cxx2a
&& id
== ridpointers
[(int)RID_CONSTINIT
])
3367 inform (location
, "C++20 %<constinit%> only available with "
3368 "%<-std=c++2a%> or %<-std=gnu++2a%>");
3369 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3370 inform (location
, "%<concept%> only available with %<-fconcepts%>");
3371 else if (processing_template_decl
&& current_class_type
3372 && TYPE_BINFO (current_class_type
))
3376 for (b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3380 tree base_type
= BINFO_TYPE (b
);
3381 if (CLASS_TYPE_P (base_type
)
3382 && dependent_type_p (base_type
))
3385 /* Go from a particular instantiation of the
3386 template (which will have an empty TYPE_FIELDs),
3387 to the main version. */
3388 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3389 for (field
= TYPE_FIELDS (base_type
);
3391 field
= DECL_CHAIN (field
))
3392 if (TREE_CODE (field
) == TYPE_DECL
3393 && DECL_NAME (field
) == id
)
3396 "(perhaps %<typename %T::%E%> was intended)",
3397 BINFO_TYPE (b
), id
);
3406 /* Here we diagnose qualified-ids where the scope is actually correct,
3407 but the identifier does not resolve to a valid type name. */
3408 else if (parser
->scope
!= error_mark_node
)
3410 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3412 auto_diagnostic_group d
;
3414 if (decl
== error_mark_node
)
3415 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3417 const char *suggestion
= hint
.suggestion ();
3418 gcc_rich_location
richloc (location_of (id
));
3420 richloc
.add_fixit_replace (suggestion
);
3421 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3425 "%qE in namespace %qE does not name a template"
3426 " type; did you mean %qs?",
3427 id
, parser
->scope
, suggestion
);
3430 "%qE in namespace %qE does not name a template type",
3433 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3437 "%qE in namespace %qE does not name a template"
3438 " type; did you mean %qs?",
3439 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3442 "%qE in namespace %qE does not name a template"
3444 TREE_OPERAND (id
, 0), parser
->scope
);
3450 "%qE in namespace %qE does not name a type"
3451 "; did you mean %qs?",
3452 id
, parser
->scope
, suggestion
);
3455 "%qE in namespace %qE does not name a type",
3459 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3461 else if (CLASS_TYPE_P (parser
->scope
)
3462 && constructor_name_p (id
, parser
->scope
))
3465 auto_diagnostic_group d
;
3466 error_at (location
, "%<%T::%E%> names the constructor, not"
3467 " the type", parser
->scope
, id
);
3468 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3469 error_at (location
, "and %qT has no template constructors",
3472 else if (TYPE_P (parser
->scope
)
3473 && dependent_scope_p (parser
->scope
))
3475 gcc_rich_location
richloc (location
);
3476 richloc
.add_fixit_insert_before ("typename ");
3477 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3479 "need %<typename%> before %<%T::%D::%E%> because "
3480 "%<%T::%D%> is a dependent scope",
3481 TYPE_CONTEXT (parser
->scope
),
3482 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3484 TYPE_CONTEXT (parser
->scope
),
3485 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3487 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3488 "%qT is a dependent scope",
3489 parser
->scope
, id
, parser
->scope
);
3491 else if (TYPE_P (parser
->scope
))
3493 auto_diagnostic_group d
;
3494 if (!COMPLETE_TYPE_P (parser
->scope
))
3495 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3497 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3498 error_at (location_of (id
),
3499 "%qE in %q#T does not name a template type",
3501 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3502 error_at (location_of (id
),
3503 "%qE in %q#T does not name a template type",
3504 TREE_OPERAND (id
, 0), parser
->scope
);
3506 error_at (location_of (id
),
3507 "%qE in %q#T does not name a type",
3510 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3517 /* Check for a common situation where a type-name should be present,
3518 but is not, and issue a sensible error message. Returns true if an
3519 invalid type-name was detected.
3521 The situation handled by this function are variable declarations of the
3522 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3523 Usually, `ID' should name a type, but if we got here it means that it
3524 does not. We try to emit the best possible error message depending on
3525 how exactly the id-expression looks like. */
3528 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3531 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3533 /* Avoid duplicate error about ambiguous lookup. */
3534 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3536 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3537 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3541 cp_parser_parse_tentatively (parser
);
3542 id
= cp_parser_id_expression (parser
,
3543 /*template_keyword_p=*/false,
3544 /*check_dependency_p=*/true,
3545 /*template_p=*/NULL
,
3546 /*declarator_p=*/false,
3547 /*optional_p=*/false);
3548 /* If the next token is a (, this is a function with no explicit return
3549 type, i.e. constructor, destructor or conversion op. */
3550 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3551 || TREE_CODE (id
) == TYPE_DECL
)
3553 cp_parser_abort_tentative_parse (parser
);
3556 if (!cp_parser_parse_definitely (parser
))
3559 /* Emit a diagnostic for the invalid type. */
3560 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3562 /* If we aren't in the middle of a declarator (i.e. in a
3563 parameter-declaration-clause), skip to the end of the declaration;
3564 there's no point in trying to process it. */
3565 if (!parser
->in_declarator_p
)
3566 cp_parser_skip_to_end_of_block_or_statement (parser
);
3570 /* Consume tokens up to, and including, the next non-nested closing `)'.
3571 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3572 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3573 found an unnested token of that type. */
3576 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3581 unsigned paren_depth
= 0;
3582 unsigned brace_depth
= 0;
3583 unsigned square_depth
= 0;
3584 unsigned condop_depth
= 0;
3586 if (recovering
&& or_ttype
== CPP_EOF
3587 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3592 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3594 /* Have we found what we're looking for before the closing paren? */
3595 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3596 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3599 switch (token
->type
)
3601 case CPP_PRAGMA_EOL
:
3602 if (!parser
->lexer
->in_pragma
)
3606 /* If we've run out of tokens, then there is no closing `)'. */
3609 /* This is good for lambda expression capture-lists. */
3610 case CPP_OPEN_SQUARE
:
3613 case CPP_CLOSE_SQUARE
:
3614 if (!square_depth
--)
3619 /* This matches the processing in skip_to_end_of_statement. */
3624 case CPP_OPEN_BRACE
:
3627 case CPP_CLOSE_BRACE
:
3632 case CPP_OPEN_PAREN
:
3637 case CPP_CLOSE_PAREN
:
3638 if (!brace_depth
&& !paren_depth
--)
3641 cp_lexer_consume_token (parser
->lexer
);
3647 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3652 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3660 /* Consume the token. */
3661 cp_lexer_consume_token (parser
->lexer
);
3665 /* Consume tokens up to, and including, the next non-nested closing `)'.
3666 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3667 are doing error recovery. Returns -1 if OR_COMMA is true and we
3668 found an unnested token of that type. */
3671 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3676 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3677 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3678 ttype
, consume_paren
);
3681 /* Consume tokens until we reach the end of the current statement.
3682 Normally, that will be just before consuming a `;'. However, if a
3683 non-nested `}' comes first, then we stop before consuming that. */
3686 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3688 unsigned nesting_depth
= 0;
3690 /* Unwind generic function template scope if necessary. */
3691 if (parser
->fully_implicit_function_template_p
)
3692 abort_fully_implicit_template (parser
);
3696 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3698 switch (token
->type
)
3700 case CPP_PRAGMA_EOL
:
3701 if (!parser
->lexer
->in_pragma
)
3705 /* If we've run out of tokens, stop. */
3709 /* If the next token is a `;', we have reached the end of the
3715 case CPP_CLOSE_BRACE
:
3716 /* If this is a non-nested '}', stop before consuming it.
3717 That way, when confronted with something like:
3721 we stop before consuming the closing '}', even though we
3722 have not yet reached a `;'. */
3723 if (nesting_depth
== 0)
3726 /* If it is the closing '}' for a block that we have
3727 scanned, stop -- but only after consuming the token.
3733 we will stop after the body of the erroneously declared
3734 function, but before consuming the following `typedef'
3736 if (--nesting_depth
== 0)
3738 cp_lexer_consume_token (parser
->lexer
);
3743 case CPP_OPEN_BRACE
:
3751 /* Consume the token. */
3752 cp_lexer_consume_token (parser
->lexer
);
3756 /* This function is called at the end of a statement or declaration.
3757 If the next token is a semicolon, it is consumed; otherwise, error
3758 recovery is attempted. */
3761 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3763 /* Look for the trailing `;'. */
3764 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3766 /* If there is additional (erroneous) input, skip to the end of
3768 cp_parser_skip_to_end_of_statement (parser
);
3769 /* If the next token is now a `;', consume it. */
3770 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3771 cp_lexer_consume_token (parser
->lexer
);
3775 /* Skip tokens until we have consumed an entire block, or until we
3776 have consumed a non-nested `;'. */
3779 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3781 int nesting_depth
= 0;
3783 /* Unwind generic function template scope if necessary. */
3784 if (parser
->fully_implicit_function_template_p
)
3785 abort_fully_implicit_template (parser
);
3787 while (nesting_depth
>= 0)
3789 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3791 switch (token
->type
)
3793 case CPP_PRAGMA_EOL
:
3794 if (!parser
->lexer
->in_pragma
)
3798 /* If we've run out of tokens, stop. */
3802 /* Stop if this is an unnested ';'. */
3807 case CPP_CLOSE_BRACE
:
3808 /* Stop if this is an unnested '}', or closes the outermost
3811 if (nesting_depth
< 0)
3817 case CPP_OPEN_BRACE
:
3826 /* Consume the token. */
3827 cp_lexer_consume_token (parser
->lexer
);
3831 /* Skip tokens until a non-nested closing curly brace is the next
3832 token, or there are no more tokens. Return true in the first case,
3836 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3838 unsigned nesting_depth
= 0;
3842 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3844 switch (token
->type
)
3846 case CPP_PRAGMA_EOL
:
3847 if (!parser
->lexer
->in_pragma
)
3851 /* If we've run out of tokens, stop. */
3854 case CPP_CLOSE_BRACE
:
3855 /* If the next token is a non-nested `}', then we have reached
3856 the end of the current block. */
3857 if (nesting_depth
-- == 0)
3861 case CPP_OPEN_BRACE
:
3862 /* If it the next token is a `{', then we are entering a new
3863 block. Consume the entire block. */
3871 /* Consume the token. */
3872 cp_lexer_consume_token (parser
->lexer
);
3876 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3877 parameter is the PRAGMA token, allowing us to purge the entire pragma
3881 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
3885 parser
->lexer
->in_pragma
= false;
3888 token
= cp_lexer_consume_token (parser
->lexer
);
3889 while (token
->type
!= CPP_PRAGMA_EOL
&& token
->type
!= CPP_EOF
);
3891 /* Ensure that the pragma is not parsed again. */
3892 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
3895 /* Require pragma end of line, resyncing with it as necessary. The
3896 arguments are as for cp_parser_skip_to_pragma_eol. */
3899 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
3901 parser
->lexer
->in_pragma
= false;
3902 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
3903 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
3906 /* This is a simple wrapper around make_typename_type. When the id is
3907 an unresolved identifier node, we can provide a superior diagnostic
3908 using cp_parser_diagnose_invalid_type_name. */
3911 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
3912 location_t id_location
)
3915 if (identifier_p (id
))
3917 result
= make_typename_type (parser
->scope
, id
, typename_type
,
3918 /*complain=*/tf_none
);
3919 if (result
== error_mark_node
)
3920 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
3923 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
3926 /* This is a wrapper around the
3927 make_{pointer,ptrmem,reference}_declarator functions that decides
3928 which one to call based on the CODE and CLASS_TYPE arguments. The
3929 CODE argument should be one of the values returned by
3930 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3931 appertain to the pointer or reference. */
3933 static cp_declarator
*
3934 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
3935 cp_cv_quals cv_qualifiers
,
3936 cp_declarator
*target
,
3939 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
3940 return cp_error_declarator
;
3942 if (code
== INDIRECT_REF
)
3943 if (class_type
== NULL_TREE
)
3944 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
3946 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
3947 target
, attributes
);
3948 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
3949 return make_reference_declarator (cv_qualifiers
, target
,
3951 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
3952 return make_reference_declarator (cv_qualifiers
, target
,
3957 /* Create a new C++ parser. */
3960 cp_parser_new (void)
3966 /* cp_lexer_new_main is called before doing GC allocation because
3967 cp_lexer_new_main might load a PCH file. */
3968 lexer
= cp_lexer_new_main ();
3970 /* Initialize the binops_by_token so that we can get the tree
3971 directly from the token. */
3972 for (i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
3973 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
3975 parser
= ggc_cleared_alloc
<cp_parser
> ();
3976 parser
->lexer
= lexer
;
3977 parser
->context
= cp_parser_context_new (NULL
);
3979 /* For now, we always accept GNU extensions. */
3980 parser
->allow_gnu_extensions_p
= 1;
3982 /* The `>' token is a greater-than operator, not the end of a
3984 parser
->greater_than_is_operator_p
= true;
3986 parser
->default_arg_ok_p
= true;
3988 /* We are not parsing a constant-expression. */
3989 parser
->integral_constant_expression_p
= false;
3990 parser
->allow_non_integral_constant_expression_p
= false;
3991 parser
->non_integral_constant_expression_p
= false;
3993 /* Local variable names are not forbidden. */
3994 parser
->local_variables_forbidden_p
= 0;
3996 /* We are not processing an `extern "C"' declaration. */
3997 parser
->in_unbraced_linkage_specification_p
= false;
3999 /* We are not processing a declarator. */
4000 parser
->in_declarator_p
= false;
4002 /* We are not processing a template-argument-list. */
4003 parser
->in_template_argument_list_p
= false;
4005 /* We are not in an iteration statement. */
4006 parser
->in_statement
= 0;
4008 /* We are not in a switch statement. */
4009 parser
->in_switch_statement_p
= false;
4011 /* We are not parsing a type-id inside an expression. */
4012 parser
->in_type_id_in_expr_p
= false;
4014 /* String literals should be translated to the execution character set. */
4015 parser
->translate_strings_p
= true;
4017 /* We are not parsing a function body. */
4018 parser
->in_function_body
= false;
4020 /* We can correct until told otherwise. */
4021 parser
->colon_corrects_to_scope_p
= true;
4023 /* The unparsed function queue is empty. */
4024 push_unparsed_function_queues (parser
);
4026 /* There are no classes being defined. */
4027 parser
->num_classes_being_defined
= 0;
4029 /* No template parameters apply. */
4030 parser
->num_template_parameter_lists
= 0;
4032 /* Special parsing data structures. */
4033 parser
->omp_declare_simd
= NULL
;
4034 parser
->oacc_routine
= NULL
;
4036 /* Not declaring an implicit function template. */
4037 parser
->auto_is_implicit_function_template_parm_p
= false;
4038 parser
->fully_implicit_function_template_p
= false;
4039 parser
->implicit_template_parms
= 0;
4040 parser
->implicit_template_scope
= 0;
4042 /* Allow constrained-type-specifiers. */
4043 parser
->prevent_constrained_type_specifiers
= 0;
4045 /* We haven't yet seen an 'extern "C"'. */
4046 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4051 /* Create a cp_lexer structure which will emit the tokens in CACHE
4052 and push it onto the parser's lexer stack. This is used for delayed
4053 parsing of in-class method bodies and default arguments, and should
4054 not be confused with tentative parsing. */
4056 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4058 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4059 lexer
->next
= parser
->lexer
;
4060 parser
->lexer
= lexer
;
4062 /* Move the current source position to that of the first token in the
4064 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4067 /* Pop the top lexer off the parser stack. This is never used for the
4068 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4070 cp_parser_pop_lexer (cp_parser
*parser
)
4072 cp_lexer
*lexer
= parser
->lexer
;
4073 parser
->lexer
= lexer
->next
;
4074 cp_lexer_destroy (lexer
);
4076 /* Put the current source position back where it was before this
4077 lexer was pushed. */
4078 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4081 /* Lexical conventions [gram.lex] */
4083 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4087 cp_parser_identifier (cp_parser
* parser
)
4091 /* Look for the identifier. */
4092 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4093 /* Return the value. */
4095 return cp_expr (token
->u
.value
, token
->location
);
4097 return error_mark_node
;
4100 /* Parse a sequence of adjacent string constants. Returns a
4101 TREE_STRING representing the combined, nul-terminated string
4102 constant. If TRANSLATE is true, translate the string to the
4103 execution character set. If WIDE_OK is true, a wide string is
4106 C++98 [lex.string] says that if a narrow string literal token is
4107 adjacent to a wide string literal token, the behavior is undefined.
4108 However, C99 6.4.5p4 says that this results in a wide string literal.
4109 We follow C99 here, for consistency with the C front end.
4111 This code is largely lifted from lex_string() in c-lex.c.
4113 FUTURE: ObjC++ will need to handle @-strings here. */
4115 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4116 bool lookup_udlit
= true)
4120 struct obstack str_ob
;
4121 struct obstack loc_ob
;
4122 cpp_string str
, istr
, *strs
;
4124 enum cpp_ttype type
, curr_type
;
4125 int have_suffix_p
= 0;
4127 tree suffix_id
= NULL_TREE
;
4128 bool curr_tok_is_userdef_p
= false;
4130 tok
= cp_lexer_peek_token (parser
->lexer
);
4131 if (!cp_parser_is_string_literal (tok
))
4133 cp_parser_error (parser
, "expected string-literal");
4134 return error_mark_node
;
4137 location_t loc
= tok
->location
;
4139 if (cpp_userdef_string_p (tok
->type
))
4141 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4142 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4143 curr_tok_is_userdef_p
= true;
4147 string_tree
= tok
->u
.value
;
4148 curr_type
= tok
->type
;
4152 /* Try to avoid the overhead of creating and destroying an obstack
4153 for the common case of just one string. */
4154 if (!cp_parser_is_string_literal
4155 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4157 cp_lexer_consume_token (parser
->lexer
);
4159 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4160 str
.len
= TREE_STRING_LENGTH (string_tree
);
4163 if (curr_tok_is_userdef_p
)
4165 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4167 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4170 curr_type
= tok
->type
;
4176 location_t last_tok_loc
= tok
->location
;
4177 gcc_obstack_init (&str_ob
);
4178 gcc_obstack_init (&loc_ob
);
4183 cp_lexer_consume_token (parser
->lexer
);
4185 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4186 str
.len
= TREE_STRING_LENGTH (string_tree
);
4188 if (curr_tok_is_userdef_p
)
4190 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4191 if (have_suffix_p
== 0)
4193 suffix_id
= curr_suffix_id
;
4196 else if (have_suffix_p
== 1
4197 && curr_suffix_id
!= suffix_id
)
4199 error ("inconsistent user-defined literal suffixes"
4200 " %qD and %qD in string literal",
4201 suffix_id
, curr_suffix_id
);
4204 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4207 curr_type
= tok
->type
;
4209 if (type
!= curr_type
)
4211 if (type
== CPP_STRING
)
4213 else if (curr_type
!= CPP_STRING
)
4215 rich_location
rich_loc (line_table
, tok
->location
);
4216 rich_loc
.add_range (last_tok_loc
);
4217 error_at (&rich_loc
,
4218 "unsupported non-standard concatenation "
4219 "of string literals");
4223 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4224 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4226 last_tok_loc
= tok
->location
;
4228 tok
= cp_lexer_peek_token (parser
->lexer
);
4229 if (cpp_userdef_string_p (tok
->type
))
4231 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4232 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4233 curr_tok_is_userdef_p
= true;
4237 string_tree
= tok
->u
.value
;
4238 curr_type
= tok
->type
;
4239 curr_tok_is_userdef_p
= false;
4242 while (cp_parser_is_string_literal (tok
));
4244 /* A string literal built by concatenation has its caret=start at
4245 the start of the initial string, and its finish at the finish of
4246 the final string literal. */
4247 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4249 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4252 if (type
!= CPP_STRING
&& !wide_ok
)
4254 cp_parser_error (parser
, "a wide string is invalid in this context");
4258 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4259 (parse_in
, strs
, count
, &istr
, type
))
4261 value
= build_string (istr
.len
, (const char *)istr
.text
);
4262 free (CONST_CAST (unsigned char *, istr
.text
));
4265 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4266 gcc_assert (g_string_concat_db
);
4267 g_string_concat_db
->record_string_concatenation (count
, locs
);
4274 TREE_TYPE (value
) = char_array_type_node
;
4276 case CPP_UTF8STRING
:
4278 TREE_TYPE (value
) = char8_array_type_node
;
4280 TREE_TYPE (value
) = char_array_type_node
;
4283 TREE_TYPE (value
) = char16_array_type_node
;
4286 TREE_TYPE (value
) = char32_array_type_node
;
4289 TREE_TYPE (value
) = wchar_array_type_node
;
4293 value
= fix_string_type (value
);
4297 tree literal
= build_userdef_literal (suffix_id
, value
,
4298 OT_NONE
, NULL_TREE
);
4300 value
= cp_parser_userdef_string_literal (literal
);
4306 /* cpp_interpret_string has issued an error. */
4307 value
= error_mark_node
;
4311 obstack_free (&str_ob
, 0);
4312 obstack_free (&loc_ob
, 0);
4315 return cp_expr (value
, loc
);
4318 /* Look up a literal operator with the name and the exact arguments. */
4321 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4323 tree decl
= lookup_name (name
);
4324 if (!decl
|| !is_overloaded_fn (decl
))
4325 return error_mark_node
;
4327 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4331 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4337 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4338 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4340 tree tparm
= TREE_VALUE (parmtypes
);
4341 tree targ
= TREE_TYPE ((*args
)[ix
]);
4342 bool ptr
= TYPE_PTR_P (tparm
);
4343 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4344 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4346 || !same_type_p (TREE_TYPE (tparm
),
4352 && ix
== vec_safe_length (args
)
4353 /* May be this should be sufficient_parms_p instead,
4354 depending on how exactly should user-defined literals
4355 work in presence of default arguments on the literal
4356 operator parameters. */
4357 && parmtypes
== void_list_node
)
4362 return error_mark_node
;
4365 /* Parse a user-defined char constant. Returns a call to a user-defined
4366 literal operator taking the character as an argument. */
4369 cp_parser_userdef_char_literal (cp_parser
*parser
)
4371 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4372 tree literal
= token
->u
.value
;
4373 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4374 tree value
= USERDEF_LITERAL_VALUE (literal
);
4375 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4378 /* Build up a call to the user-defined operator */
4379 /* Lookup the name we got back from the id-expression. */
4381 vec_safe_push (args
, value
);
4382 decl
= lookup_literal_operator (name
, args
);
4383 if (!decl
|| decl
== error_mark_node
)
4385 error ("unable to find character literal operator %qD with %qT argument",
4386 name
, TREE_TYPE (value
));
4387 return error_mark_node
;
4389 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4393 /* A subroutine of cp_parser_userdef_numeric_literal to
4394 create a char... template parameter pack from a string node. */
4397 make_char_string_pack (tree value
)
4400 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4401 const char *str
= TREE_STRING_POINTER (value
);
4402 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4403 tree argvec
= make_tree_vec (1);
4405 /* Fill in CHARVEC with all of the parameters. */
4406 charvec
= make_tree_vec (len
);
4407 for (i
= 0; i
< len
; ++i
)
4409 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4410 cpp_string in
= { 3, s
};
4411 cpp_string out
= { 0, 0 };
4412 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4414 gcc_assert (out
.len
== 2);
4415 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4419 /* Build the argument packs. */
4420 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4422 TREE_VEC_ELT (argvec
, 0) = argpack
;
4427 /* A subroutine of cp_parser_userdef_numeric_literal to
4428 create a char... template parameter pack from a string node. */
4431 make_string_pack (tree value
)
4434 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4435 const unsigned char *str
4436 = (const unsigned char *) TREE_STRING_POINTER (value
);
4437 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4438 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4439 tree argvec
= make_tree_vec (2);
4441 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4442 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4444 /* First template parm is character type. */
4445 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4447 /* Fill in CHARVEC with all of the parameters. */
4448 charvec
= make_tree_vec (len
);
4449 for (int i
= 0; i
< len
; ++i
)
4450 TREE_VEC_ELT (charvec
, i
)
4451 = double_int_to_tree (str_char_type_node
,
4452 double_int::from_buffer (str
+ i
* sz
, sz
));
4454 /* Build the argument packs. */
4455 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4457 TREE_VEC_ELT (argvec
, 1) = argpack
;
4462 /* Parse a user-defined numeric constant. returns a call to a user-defined
4463 literal operator. */
4466 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4468 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4469 tree literal
= token
->u
.value
;
4470 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4471 tree value
= USERDEF_LITERAL_VALUE (literal
);
4472 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4473 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4474 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4477 /* Look for a literal operator taking the exact type of numeric argument
4478 as the literal value. */
4480 vec_safe_push (args
, value
);
4481 decl
= lookup_literal_operator (name
, args
);
4482 if (decl
&& decl
!= error_mark_node
)
4484 result
= finish_call_expr (decl
, &args
, false, true,
4485 tf_warning_or_error
);
4487 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4489 warning_at (token
->location
, OPT_Woverflow
,
4490 "integer literal exceeds range of %qT type",
4491 long_long_unsigned_type_node
);
4496 warning_at (token
->location
, OPT_Woverflow
,
4497 "floating literal exceeds range of %qT type",
4498 long_double_type_node
);
4499 else if (overflow
< 0)
4500 warning_at (token
->location
, OPT_Woverflow
,
4501 "floating literal truncated to zero");
4507 /* If the numeric argument didn't work, look for a raw literal
4508 operator taking a const char* argument consisting of the number
4509 in string format. */
4511 vec_safe_push (args
, num_string
);
4512 decl
= lookup_literal_operator (name
, args
);
4513 if (decl
&& decl
!= error_mark_node
)
4515 result
= finish_call_expr (decl
, &args
, false, true,
4516 tf_warning_or_error
);
4520 /* If the raw literal didn't work, look for a non-type template
4521 function with parameter pack char.... Call the function with
4522 template parameter characters representing the number. */
4524 decl
= lookup_literal_operator (name
, args
);
4525 if (decl
&& decl
!= error_mark_node
)
4527 tree tmpl_args
= make_char_string_pack (num_string
);
4528 if (tmpl_args
== NULL_TREE
)
4530 error ("failed to translate literal to execution character set %qT",
4532 return error_mark_node
;
4534 decl
= lookup_template_function (decl
, tmpl_args
);
4535 result
= finish_call_expr (decl
, &args
, false, true,
4536 tf_warning_or_error
);
4540 /* In C++14 the standard library defines complex number suffixes that
4541 conflict with GNU extensions. Prefer them if <complex> is #included. */
4542 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4543 bool i14
= (cxx_dialect
> cxx11
4544 && (id_equal (suffix_id
, "i")
4545 || id_equal (suffix_id
, "if")
4546 || id_equal (suffix_id
, "il")));
4547 diagnostic_t kind
= DK_ERROR
;
4552 tree cxlit
= lookup_qualified_name (std_node
, "complex_literals",
4554 if (cxlit
== error_mark_node
)
4556 /* No <complex>, so pedwarn and use GNU semantics. */
4558 opt
= OPT_Wpedantic
;
4563 = emit_diagnostic (kind
, input_location
, opt
,
4564 "unable to find numeric literal operator %qD", name
);
4567 /* Don't inform either. */;
4570 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4571 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4574 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4575 "GNU built-in suffix");
4578 inform (token
->location
, "use %<-fext-numeric-literals%> "
4579 "to enable more built-in suffixes");
4581 if (kind
== DK_ERROR
)
4582 value
= error_mark_node
;
4585 /* Use the built-in semantics. */
4587 if (id_equal (suffix_id
, "i"))
4589 if (TREE_CODE (value
) == INTEGER_CST
)
4590 type
= integer_type_node
;
4592 type
= double_type_node
;
4594 else if (id_equal (suffix_id
, "if"))
4595 type
= float_type_node
;
4596 else /* if (id_equal (suffix_id, "il")) */
4597 type
= long_double_type_node
;
4599 value
= build_complex (build_complex_type (type
),
4600 fold_convert (type
, integer_zero_node
),
4601 fold_convert (type
, value
));
4604 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4605 /* Avoid repeated diagnostics. */
4606 token
->u
.value
= value
;
4610 /* Parse a user-defined string constant. Returns a call to a user-defined
4611 literal operator taking a character pointer and the length of the string
4615 cp_parser_userdef_string_literal (tree literal
)
4617 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4618 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4619 tree value
= USERDEF_LITERAL_VALUE (literal
);
4620 int len
= TREE_STRING_LENGTH (value
)
4621 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4624 /* Build up a call to the user-defined operator. */
4625 /* Lookup the name we got back from the id-expression. */
4627 vec_safe_push (args
, value
);
4628 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4629 decl
= lookup_literal_operator (name
, args
);
4631 if (decl
&& decl
!= error_mark_node
)
4632 return finish_call_expr (decl
, &args
, false, true,
4633 tf_warning_or_error
);
4635 /* Look for a suitable template function, either (C++20) with a single
4636 parameter of class type, or (N3599) with typename parameter CharT and
4637 parameter pack CharT... */
4639 decl
= lookup_literal_operator (name
, args
);
4640 if (decl
&& decl
!= error_mark_node
)
4642 /* Use resolve_nondeduced_context to try to choose one form of template
4644 tree tmpl_args
= make_tree_vec (1);
4645 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4646 decl
= lookup_template_function (decl
, tmpl_args
);
4647 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4652 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4653 res
= resolve_nondeduced_context (decl
, tf_none
);
4657 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4658 TREE_OPERAND (decl
, 1) = tmpl_args
;
4659 return finish_call_expr (decl
, &args
, false, true,
4660 tf_warning_or_error
);
4663 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4664 name
, TREE_TYPE (value
), size_type_node
);
4665 return error_mark_node
;
4669 /* Basic concepts [gram.basic] */
4671 /* Parse a translation-unit.
4674 declaration-seq [opt] */
4677 cp_parser_translation_unit (cp_parser
* parser
)
4679 gcc_checking_assert (!cp_error_declarator
);
4681 /* Create the declarator obstack. */
4682 gcc_obstack_init (&declarator_obstack
);
4683 /* Create the error declarator. */
4684 cp_error_declarator
= make_declarator (cdk_error
);
4685 /* Create the empty parameter list. */
4686 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4688 /* Remember where the base of the declarator obstack lies. */
4689 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4691 bool implicit_extern_c
= false;
4695 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4697 /* If we're entering or exiting a region that's implicitly
4698 extern "C", modify the lang context appropriately. */
4699 if (implicit_extern_c
4700 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4702 implicit_extern_c
= !implicit_extern_c
;
4703 if (implicit_extern_c
)
4704 push_lang_context (lang_name_c
);
4706 pop_lang_context ();
4709 if (token
->type
== CPP_EOF
)
4712 if (token
->type
== CPP_CLOSE_BRACE
)
4714 cp_parser_error (parser
, "expected declaration");
4715 cp_lexer_consume_token (parser
->lexer
);
4716 /* If the next token is now a `;', consume it. */
4717 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4718 cp_lexer_consume_token (parser
->lexer
);
4721 cp_parser_toplevel_declaration (parser
);
4724 /* Get rid of the token array; we don't need it any more. */
4725 cp_lexer_destroy (parser
->lexer
);
4726 parser
->lexer
= NULL
;
4728 /* The EOF should have reset this. */
4729 gcc_checking_assert (!implicit_extern_c
);
4731 /* Make sure the declarator obstack was fully cleaned up. */
4732 gcc_assert (obstack_next_free (&declarator_obstack
)
4733 == declarator_obstack_base
);
4736 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4737 decltype context. */
4739 static inline tsubst_flags_t
4740 complain_flags (bool decltype_p
)
4742 tsubst_flags_t complain
= tf_warning_or_error
;
4744 complain
|= tf_decltype
;
4748 /* We're about to parse a collection of statements. If we're currently
4749 parsing tentatively, set up a firewall so that any nested
4750 cp_parser_commit_to_tentative_parse won't affect the current context. */
4752 static cp_token_position
4753 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4755 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4758 cp_parser_parse_tentatively (parser
);
4759 cp_parser_commit_to_topmost_tentative_parse (parser
);
4760 return cp_lexer_token_position (parser
->lexer
, false);
4763 /* We've finished parsing the collection of statements. Wrap up the
4764 firewall and replace the relevant tokens with the parsed form. */
4767 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4773 /* Finish the firewall level. */
4774 cp_parser_parse_definitely (parser
);
4775 /* And remember the result of the parse for when we try again. */
4776 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4777 token
->type
= CPP_PREPARSED_EXPR
;
4778 token
->u
.value
= expr
;
4779 token
->keyword
= RID_MAX
;
4780 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4783 /* Like the above functions, but let the user modify the tokens. Used by
4784 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4785 later parses, so it makes sense to localize the effects of
4786 cp_parser_commit_to_tentative_parse. */
4788 struct tentative_firewall
4793 tentative_firewall (cp_parser
*p
): parser(p
)
4795 /* If we're currently parsing tentatively, start a committed level as a
4796 firewall and then an inner tentative parse. */
4797 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
4799 cp_parser_parse_tentatively (parser
);
4800 cp_parser_commit_to_topmost_tentative_parse (parser
);
4801 cp_parser_parse_tentatively (parser
);
4805 ~tentative_firewall()
4809 /* Finish the inner tentative parse and the firewall, propagating any
4810 uncommitted error state to the outer tentative parse. */
4811 bool err
= cp_parser_error_occurred (parser
);
4812 cp_parser_parse_definitely (parser
);
4813 cp_parser_parse_definitely (parser
);
4815 cp_parser_simulate_error (parser
);
4820 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4821 This class is for tracking such a matching pair of symbols.
4822 In particular, it tracks the location of the first token,
4823 so that if the second token is missing, we can highlight the
4824 location of the first token when notifying the user about the
4827 template <typename traits_t
>
4831 /* token_pair's ctor. */
4832 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
4834 /* If the next token is the opening symbol for this pair, consume it and
4836 Otherwise, issue an error and return false.
4837 In either case, record the location of the opening token. */
4839 bool require_open (cp_parser
*parser
)
4841 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4842 return cp_parser_require (parser
, traits_t::open_token_type
,
4843 traits_t::required_token_open
);
4846 /* Consume the next token from PARSER, recording its location as
4847 that of the opening token within the pair. */
4849 cp_token
* consume_open (cp_parser
*parser
)
4851 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
4852 gcc_assert (tok
->type
== traits_t::open_token_type
);
4853 m_open_loc
= tok
->location
;
4857 /* If the next token is the closing symbol for this pair, consume it
4859 Otherwise, issue an error, highlighting the location of the
4860 corresponding opening token, and return NULL. */
4862 cp_token
*require_close (cp_parser
*parser
) const
4864 return cp_parser_require (parser
, traits_t::close_token_type
,
4865 traits_t::required_token_close
,
4869 location_t
open_location () const { return m_open_loc
; }
4872 location_t m_open_loc
;
4875 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4877 struct matching_paren_traits
4879 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
4880 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
4881 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
4882 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
4885 /* "matching_parens" is a token_pair<T> class for tracking matching
4886 pairs of parentheses. */
4888 typedef token_pair
<matching_paren_traits
> matching_parens
;
4890 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4892 struct matching_brace_traits
4894 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
4895 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
4896 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
4897 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
4900 /* "matching_braces" is a token_pair<T> class for tracking matching
4903 typedef token_pair
<matching_brace_traits
> matching_braces
;
4906 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4907 enclosing parentheses. */
4910 cp_parser_statement_expr (cp_parser
*parser
)
4912 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
4914 /* Consume the '('. */
4915 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4916 matching_parens parens
;
4917 parens
.consume_open (parser
);
4918 /* Start the statement-expression. */
4919 tree expr
= begin_stmt_expr ();
4920 /* Parse the compound-statement. */
4921 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
4923 expr
= finish_stmt_expr (expr
, false);
4924 /* Consume the ')'. */
4925 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
4926 if (!parens
.require_close (parser
))
4927 cp_parser_skip_to_end_of_statement (parser
);
4929 cp_parser_end_tentative_firewall (parser
, start
, expr
);
4930 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
4931 return cp_expr (expr
, combined_loc
);
4934 /* Expressions [gram.expr] */
4936 /* Parse a fold-operator.
4939 - * / % ^ & | = < > << >>
4940 = -= *= /= %= ^= &= |= <<= >>=
4941 == != <= >= && || , .* ->*
4943 This returns the tree code corresponding to the matched operator
4944 as an int. When the current token matches a compound assignment
4945 operator, the resulting tree code is the negative value of the
4946 non-assignment operator. */
4949 cp_parser_fold_operator (cp_token
*token
)
4951 switch (token
->type
)
4953 case CPP_PLUS
: return PLUS_EXPR
;
4954 case CPP_MINUS
: return MINUS_EXPR
;
4955 case CPP_MULT
: return MULT_EXPR
;
4956 case CPP_DIV
: return TRUNC_DIV_EXPR
;
4957 case CPP_MOD
: return TRUNC_MOD_EXPR
;
4958 case CPP_XOR
: return BIT_XOR_EXPR
;
4959 case CPP_AND
: return BIT_AND_EXPR
;
4960 case CPP_OR
: return BIT_IOR_EXPR
;
4961 case CPP_LSHIFT
: return LSHIFT_EXPR
;
4962 case CPP_RSHIFT
: return RSHIFT_EXPR
;
4964 case CPP_EQ
: return -NOP_EXPR
;
4965 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
4966 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
4967 case CPP_MULT_EQ
: return -MULT_EXPR
;
4968 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
4969 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
4970 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
4971 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
4972 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
4973 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
4974 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
4976 case CPP_EQ_EQ
: return EQ_EXPR
;
4977 case CPP_NOT_EQ
: return NE_EXPR
;
4978 case CPP_LESS
: return LT_EXPR
;
4979 case CPP_GREATER
: return GT_EXPR
;
4980 case CPP_LESS_EQ
: return LE_EXPR
;
4981 case CPP_GREATER_EQ
: return GE_EXPR
;
4983 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
4984 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
4986 case CPP_COMMA
: return COMPOUND_EXPR
;
4988 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
4989 case CPP_DEREF_STAR
: return MEMBER_REF
;
4991 default: return ERROR_MARK
;
4995 /* Returns true if CODE indicates a binary expression, which is not allowed in
4996 the LHS of a fold-expression. More codes will need to be added to use this
4997 function in other contexts. */
5000 is_binary_op (tree_code code
)
5005 case POINTER_PLUS_EXPR
:
5008 case TRUNC_DIV_EXPR
:
5009 case TRUNC_MOD_EXPR
:
5025 case TRUTH_ANDIF_EXPR
:
5026 case TRUTH_ORIF_EXPR
:
5039 /* If the next token is a suitable fold operator, consume it and return as
5040 the function above. */
5043 cp_parser_fold_operator (cp_parser
*parser
)
5045 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5046 int code
= cp_parser_fold_operator (token
);
5047 if (code
!= ERROR_MARK
)
5048 cp_lexer_consume_token (parser
->lexer
);
5052 /* Parse a fold-expression.
5055 ( ... folding-operator cast-expression)
5056 ( cast-expression folding-operator ... )
5057 ( cast-expression folding operator ... folding-operator cast-expression)
5059 Note that the '(' and ')' are matched in primary expression. */
5062 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5067 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5069 cp_lexer_consume_token (parser
->lexer
);
5070 int op
= cp_parser_fold_operator (parser
);
5071 if (op
== ERROR_MARK
)
5073 cp_parser_error (parser
, "expected binary operator");
5074 return error_mark_node
;
5077 tree expr
= cp_parser_cast_expression (parser
, false, false,
5079 if (expr
== error_mark_node
)
5080 return error_mark_node
;
5081 return finish_left_unary_fold_expr (expr
, op
);
5084 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5085 int op
= cp_parser_fold_operator (parser
);
5086 if (op
== ERROR_MARK
)
5088 cp_parser_error (parser
, "expected binary operator");
5089 return error_mark_node
;
5092 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5094 cp_parser_error (parser
, "expected ...");
5095 return error_mark_node
;
5097 cp_lexer_consume_token (parser
->lexer
);
5099 /* The operands of a fold-expression are cast-expressions, so binary or
5100 conditional expressions are not allowed. We check this here to avoid
5101 tentative parsing. */
5102 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5103 /* OK, the expression was parenthesized. */;
5104 else if (is_binary_op (TREE_CODE (expr1
)))
5105 error_at (location_of (expr1
),
5106 "binary expression in operand of fold-expression");
5107 else if (TREE_CODE (expr1
) == COND_EXPR
5108 || (REFERENCE_REF_P (expr1
)
5109 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5110 error_at (location_of (expr1
),
5111 "conditional expression in operand of fold-expression");
5114 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5115 return finish_right_unary_fold_expr (expr1
, op
);
5117 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5119 cp_parser_error (parser
, "mismatched operator in fold-expression");
5120 return error_mark_node
;
5122 cp_lexer_consume_token (parser
->lexer
);
5124 // Binary left or right fold.
5125 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5126 if (expr2
== error_mark_node
)
5127 return error_mark_node
;
5128 return finish_binary_fold_expr (expr1
, expr2
, op
);
5131 /* Parse a primary-expression.
5138 lambda-expression (C++11)
5143 ( compound-statement )
5144 __builtin_va_arg ( assignment-expression , type-id )
5145 __builtin_offsetof ( type-id , offsetof-expression )
5148 __has_nothrow_assign ( type-id )
5149 __has_nothrow_constructor ( type-id )
5150 __has_nothrow_copy ( type-id )
5151 __has_trivial_assign ( type-id )
5152 __has_trivial_constructor ( type-id )
5153 __has_trivial_copy ( type-id )
5154 __has_trivial_destructor ( type-id )
5155 __has_virtual_destructor ( type-id )
5156 __is_abstract ( type-id )
5157 __is_base_of ( type-id , type-id )
5158 __is_class ( type-id )
5159 __is_empty ( type-id )
5160 __is_enum ( type-id )
5161 __is_final ( type-id )
5162 __is_literal_type ( type-id )
5163 __is_pod ( type-id )
5164 __is_polymorphic ( type-id )
5165 __is_std_layout ( type-id )
5166 __is_trivial ( type-id )
5167 __is_union ( type-id )
5169 Objective-C++ Extension:
5177 ADDRESS_P is true iff this expression was immediately preceded by
5178 "&" and therefore might denote a pointer-to-member. CAST_P is true
5179 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5180 true iff this expression is a template argument.
5182 Returns a representation of the expression. Upon return, *IDK
5183 indicates what kind of id-expression (if any) was present. */
5186 cp_parser_primary_expression (cp_parser
*parser
,
5189 bool template_arg_p
,
5193 cp_token
*token
= NULL
;
5195 /* Assume the primary expression is not an id-expression. */
5196 *idk
= CP_ID_KIND_NONE
;
5198 /* Peek at the next token. */
5199 token
= cp_lexer_peek_token (parser
->lexer
);
5200 switch ((int) token
->type
)
5209 user-defined-literal */
5216 case CPP_PREPARSED_EXPR
:
5217 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5218 return cp_parser_userdef_numeric_literal (parser
);
5219 token
= cp_lexer_consume_token (parser
->lexer
);
5220 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5222 error_at (token
->location
,
5223 "fixed-point types not supported in C++");
5224 return error_mark_node
;
5226 /* Floating-point literals are only allowed in an integral
5227 constant expression if they are cast to an integral or
5228 enumeration type. */
5229 if (TREE_CODE (token
->u
.value
) == REAL_CST
5230 && parser
->integral_constant_expression_p
5233 /* CAST_P will be set even in invalid code like "int(2.7 +
5234 ...)". Therefore, we have to check that the next token
5235 is sure to end the cast. */
5238 cp_token
*next_token
;
5240 next_token
= cp_lexer_peek_token (parser
->lexer
);
5241 if (/* The comma at the end of an
5242 enumerator-definition. */
5243 next_token
->type
!= CPP_COMMA
5244 /* The curly brace at the end of an enum-specifier. */
5245 && next_token
->type
!= CPP_CLOSE_BRACE
5246 /* The end of a statement. */
5247 && next_token
->type
!= CPP_SEMICOLON
5248 /* The end of the cast-expression. */
5249 && next_token
->type
!= CPP_CLOSE_PAREN
5250 /* The end of an array bound. */
5251 && next_token
->type
!= CPP_CLOSE_SQUARE
5252 /* The closing ">" in a template-argument-list. */
5253 && (next_token
->type
!= CPP_GREATER
5254 || parser
->greater_than_is_operator_p
)
5255 /* C++0x only: A ">>" treated like two ">" tokens,
5256 in a template-argument-list. */
5257 && (next_token
->type
!= CPP_RSHIFT
5258 || (cxx_dialect
== cxx98
)
5259 || parser
->greater_than_is_operator_p
))
5263 /* If we are within a cast, then the constraint that the
5264 cast is to an integral or enumeration type will be
5265 checked at that point. If we are not within a cast, then
5266 this code is invalid. */
5268 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5270 return (cp_expr (token
->u
.value
, token
->location
)
5271 .maybe_add_location_wrapper ());
5273 case CPP_CHAR_USERDEF
:
5274 case CPP_CHAR16_USERDEF
:
5275 case CPP_CHAR32_USERDEF
:
5276 case CPP_WCHAR_USERDEF
:
5277 case CPP_UTF8CHAR_USERDEF
:
5278 return cp_parser_userdef_char_literal (parser
);
5284 case CPP_UTF8STRING
:
5285 case CPP_STRING_USERDEF
:
5286 case CPP_STRING16_USERDEF
:
5287 case CPP_STRING32_USERDEF
:
5288 case CPP_WSTRING_USERDEF
:
5289 case CPP_UTF8STRING_USERDEF
:
5290 /* ??? Should wide strings be allowed when parser->translate_strings_p
5291 is false (i.e. in attributes)? If not, we can kill the third
5292 argument to cp_parser_string_literal. */
5293 return (cp_parser_string_literal (parser
,
5294 parser
->translate_strings_p
,
5296 .maybe_add_location_wrapper ());
5298 case CPP_OPEN_PAREN
:
5299 /* If we see `( { ' then we are looking at the beginning of
5300 a GNU statement-expression. */
5301 if (cp_parser_allow_gnu_extensions_p (parser
)
5302 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5304 /* Statement-expressions are not allowed by the standard. */
5305 pedwarn (token
->location
, OPT_Wpedantic
,
5306 "ISO C++ forbids braced-groups within expressions");
5308 /* And they're not allowed outside of a function-body; you
5309 cannot, for example, write:
5311 int i = ({ int j = 3; j + 1; });
5313 at class or namespace scope. */
5314 if (!parser
->in_function_body
5315 || parser
->in_template_argument_list_p
)
5317 error_at (token
->location
,
5318 "statement-expressions are not allowed outside "
5319 "functions nor in template-argument lists");
5320 cp_parser_skip_to_end_of_block_or_statement (parser
);
5321 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5322 cp_lexer_consume_token (parser
->lexer
);
5323 return error_mark_node
;
5326 return cp_parser_statement_expr (parser
);
5328 /* Otherwise it's a normal parenthesized expression. */
5331 bool saved_greater_than_is_operator_p
;
5333 location_t open_paren_loc
= token
->location
;
5335 /* Consume the `('. */
5336 matching_parens parens
;
5337 parens
.consume_open (parser
);
5338 /* Within a parenthesized expression, a `>' token is always
5339 the greater-than operator. */
5340 saved_greater_than_is_operator_p
5341 = parser
->greater_than_is_operator_p
;
5342 parser
->greater_than_is_operator_p
= true;
5344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5345 /* Left fold expression. */
5348 /* Parse the parenthesized expression. */
5349 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5351 token
= cp_lexer_peek_token (parser
->lexer
);
5352 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5354 expr
= cp_parser_fold_expression (parser
, expr
);
5355 if (expr
!= error_mark_node
5356 && cxx_dialect
< cxx17
)
5357 pedwarn (input_location
, 0, "fold-expressions only available "
5358 "with %<-std=c++17%> or %<-std=gnu++17%>");
5361 /* Let the front end know that this expression was
5362 enclosed in parentheses. This matters in case, for
5363 example, the expression is of the form `A::B', since
5364 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5366 expr
= finish_parenthesized_expr (expr
);
5368 /* DR 705: Wrapping an unqualified name in parentheses
5369 suppresses arg-dependent lookup. We want to pass back
5370 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5371 (c++/37862), but none of the others. */
5372 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5373 *idk
= CP_ID_KIND_NONE
;
5375 /* The `>' token might be the end of a template-id or
5376 template-parameter-list now. */
5377 parser
->greater_than_is_operator_p
5378 = saved_greater_than_is_operator_p
;
5380 /* Consume the `)'. */
5381 token
= cp_lexer_peek_token (parser
->lexer
);
5382 location_t close_paren_loc
= token
->location
;
5383 expr
.set_range (open_paren_loc
, close_paren_loc
);
5384 if (!parens
.require_close (parser
)
5385 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5386 cp_parser_skip_to_end_of_statement (parser
);
5391 case CPP_OPEN_SQUARE
:
5393 if (c_dialect_objc ())
5395 /* We might have an Objective-C++ message. */
5396 cp_parser_parse_tentatively (parser
);
5397 tree msg
= cp_parser_objc_message_expression (parser
);
5398 /* If that works out, we're done ... */
5399 if (cp_parser_parse_definitely (parser
))
5401 /* ... else, fall though to see if it's a lambda. */
5403 cp_expr lam
= cp_parser_lambda_expression (parser
);
5404 /* Don't warn about a failed tentative parse. */
5405 if (cp_parser_error_occurred (parser
))
5406 return error_mark_node
;
5407 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5411 case CPP_OBJC_STRING
:
5412 if (c_dialect_objc ())
5413 /* We have an Objective-C++ string literal. */
5414 return cp_parser_objc_expression (parser
);
5415 cp_parser_error (parser
, "expected primary-expression");
5416 return error_mark_node
;
5419 switch (token
->keyword
)
5421 /* These two are the boolean literals. */
5423 cp_lexer_consume_token (parser
->lexer
);
5424 return cp_expr (boolean_true_node
, token
->location
);
5426 cp_lexer_consume_token (parser
->lexer
);
5427 return cp_expr (boolean_false_node
, token
->location
);
5429 /* The `__null' literal. */
5431 cp_lexer_consume_token (parser
->lexer
);
5432 return cp_expr (null_node
, token
->location
);
5434 /* The `nullptr' literal. */
5436 cp_lexer_consume_token (parser
->lexer
);
5437 return cp_expr (nullptr_node
, token
->location
);
5439 /* Recognize the `this' keyword. */
5441 cp_lexer_consume_token (parser
->lexer
);
5442 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5444 error_at (token
->location
,
5445 "%<this%> may not be used in this context");
5446 return error_mark_node
;
5448 /* Pointers cannot appear in constant-expressions. */
5449 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5450 return error_mark_node
;
5451 return cp_expr (finish_this_expr (), token
->location
);
5453 /* The `operator' keyword can be the beginning of an
5458 case RID_FUNCTION_NAME
:
5459 case RID_PRETTY_FUNCTION_NAME
:
5460 case RID_C99_FUNCTION_NAME
:
5462 non_integral_constant name
;
5464 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5465 __func__ are the names of variables -- but they are
5466 treated specially. Therefore, they are handled here,
5467 rather than relying on the generic id-expression logic
5468 below. Grammatically, these names are id-expressions.
5470 Consume the token. */
5471 token
= cp_lexer_consume_token (parser
->lexer
);
5473 switch (token
->keyword
)
5475 case RID_FUNCTION_NAME
:
5476 name
= NIC_FUNC_NAME
;
5478 case RID_PRETTY_FUNCTION_NAME
:
5479 name
= NIC_PRETTY_FUNC
;
5481 case RID_C99_FUNCTION_NAME
:
5482 name
= NIC_C99_FUNC
;
5488 if (cp_parser_non_integral_constant_expression (parser
, name
))
5489 return error_mark_node
;
5491 /* Look up the name. */
5492 return finish_fname (token
->u
.value
);
5499 location_t type_location
;
5500 location_t start_loc
5501 = cp_lexer_peek_token (parser
->lexer
)->location
;
5502 /* The `__builtin_va_arg' construct is used to handle
5503 `va_arg'. Consume the `__builtin_va_arg' token. */
5504 cp_lexer_consume_token (parser
->lexer
);
5505 /* Look for the opening `('. */
5506 matching_parens parens
;
5507 parens
.require_open (parser
);
5508 /* Now, parse the assignment-expression. */
5509 expression
= cp_parser_assignment_expression (parser
);
5510 /* Look for the `,'. */
5511 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5512 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5513 /* Parse the type-id. */
5515 type_id_in_expr_sentinel
s (parser
);
5516 type
= cp_parser_type_id (parser
);
5518 /* Look for the closing `)'. */
5519 location_t finish_loc
5520 = cp_lexer_peek_token (parser
->lexer
)->location
;
5521 parens
.require_close (parser
);
5522 /* Using `va_arg' in a constant-expression is not
5524 if (cp_parser_non_integral_constant_expression (parser
,
5526 return error_mark_node
;
5527 /* Construct a location of the form:
5528 __builtin_va_arg (v, int)
5529 ~~~~~~~~~~~~~~~~~~~~~^~~~
5530 with the caret at the type, ranging from the start of the
5531 "__builtin_va_arg" token to the close paren. */
5532 location_t combined_loc
5533 = make_location (type_location
, start_loc
, finish_loc
);
5534 return build_x_va_arg (combined_loc
, expression
, type
);
5538 return cp_parser_builtin_offsetof (parser
);
5540 case RID_HAS_NOTHROW_ASSIGN
:
5541 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5542 case RID_HAS_NOTHROW_COPY
:
5543 case RID_HAS_TRIVIAL_ASSIGN
:
5544 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5545 case RID_HAS_TRIVIAL_COPY
:
5546 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5547 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5548 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5549 case RID_IS_ABSTRACT
:
5550 case RID_IS_AGGREGATE
:
5551 case RID_IS_BASE_OF
:
5556 case RID_IS_LITERAL_TYPE
:
5558 case RID_IS_POLYMORPHIC
:
5559 case RID_IS_SAME_AS
:
5560 case RID_IS_STD_LAYOUT
:
5561 case RID_IS_TRIVIAL
:
5562 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5563 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5564 case RID_IS_TRIVIALLY_COPYABLE
:
5566 case RID_IS_ASSIGNABLE
:
5567 case RID_IS_CONSTRUCTIBLE
:
5568 return cp_parser_trait_expr (parser
, token
->keyword
);
5572 return cp_parser_requires_expression (parser
);
5574 /* Objective-C++ expressions. */
5576 case RID_AT_PROTOCOL
:
5577 case RID_AT_SELECTOR
:
5578 return cp_parser_objc_expression (parser
);
5581 if (parser
->in_function_body
5582 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5585 error_at (token
->location
,
5586 "a template declaration cannot appear at block scope");
5587 cp_parser_skip_to_end_of_block_or_statement (parser
);
5588 return error_mark_node
;
5592 cp_parser_error (parser
, "expected primary-expression");
5593 return error_mark_node
;
5596 /* An id-expression can start with either an identifier, a
5597 `::' as the beginning of a qualified-id, or the "operator"
5601 case CPP_TEMPLATE_ID
:
5602 case CPP_NESTED_NAME_SPECIFIER
:
5605 cp_expr id_expression
;
5607 const char *error_msg
;
5610 cp_token
*id_expr_token
;
5612 /* Parse the id-expression. */
5614 = cp_parser_id_expression (parser
,
5615 /*template_keyword_p=*/false,
5616 /*check_dependency_p=*/true,
5618 /*declarator_p=*/false,
5619 /*optional_p=*/false);
5620 if (id_expression
== error_mark_node
)
5621 return error_mark_node
;
5622 id_expr_token
= token
;
5623 token
= cp_lexer_peek_token (parser
->lexer
);
5624 done
= (token
->type
!= CPP_OPEN_SQUARE
5625 && token
->type
!= CPP_OPEN_PAREN
5626 && token
->type
!= CPP_DOT
5627 && token
->type
!= CPP_DEREF
5628 && token
->type
!= CPP_PLUS_PLUS
5629 && token
->type
!= CPP_MINUS_MINUS
);
5630 /* If we have a template-id, then no further lookup is
5631 required. If the template-id was for a template-class, we
5632 will sometimes have a TYPE_DECL at this point. */
5633 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5634 || TREE_CODE (id_expression
) == TYPE_DECL
)
5635 decl
= id_expression
;
5636 /* Look up the name. */
5639 tree ambiguous_decls
;
5641 /* If we already know that this lookup is ambiguous, then
5642 we've already issued an error message; there's no reason
5644 if (id_expr_token
->type
== CPP_NAME
5645 && id_expr_token
->error_reported
)
5647 cp_parser_simulate_error (parser
);
5648 return error_mark_node
;
5651 decl
= cp_parser_lookup_name (parser
, id_expression
,
5654 /*is_namespace=*/false,
5655 /*check_dependency=*/true,
5657 id_expression
.get_location ());
5658 /* If the lookup was ambiguous, an error will already have
5660 if (ambiguous_decls
)
5661 return error_mark_node
;
5663 /* In Objective-C++, we may have an Objective-C 2.0
5664 dot-syntax for classes here. */
5665 if (c_dialect_objc ()
5666 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5667 && TREE_CODE (decl
) == TYPE_DECL
5668 && objc_is_class_name (decl
))
5671 cp_lexer_consume_token (parser
->lexer
);
5672 component
= cp_parser_identifier (parser
);
5673 if (component
== error_mark_node
)
5674 return error_mark_node
;
5676 tree result
= objc_build_class_component_ref (id_expression
,
5678 /* Build a location of the form:
5681 with caret at the start of the component name (at
5682 input_location), ranging from the start of the id_expression
5683 to the end of the component name. */
5684 location_t combined_loc
5685 = make_location (input_location
, id_expression
.get_start (),
5686 get_finish (input_location
));
5687 protected_set_expr_location (result
, combined_loc
);
5691 /* In Objective-C++, an instance variable (ivar) may be preferred
5692 to whatever cp_parser_lookup_name() found.
5693 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5694 rest of c-family, we have to do a little extra work to preserve
5695 any location information in cp_expr "decl". Given that
5696 objc_lookup_ivar is implemented in "c-family" and "objc", we
5697 have a trip through the pure "tree" type, rather than cp_expr.
5698 Naively copying it back to "decl" would implicitly give the
5699 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5700 store an EXPR_LOCATION. Hence we only update "decl" (and
5701 hence its location_t) if we get back a different tree node. */
5702 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5704 if (decl_tree
!= decl
.get_value ())
5705 decl
= cp_expr (decl_tree
);
5707 /* If name lookup gives us a SCOPE_REF, then the
5708 qualifying scope was dependent. */
5709 if (TREE_CODE (decl
) == SCOPE_REF
)
5711 /* At this point, we do not know if DECL is a valid
5712 integral constant expression. We assume that it is
5713 in fact such an expression, so that code like:
5715 template <int N> struct A {
5719 is accepted. At template-instantiation time, we
5720 will check that B<N>::i is actually a constant. */
5723 /* Check to see if DECL is a local variable in a context
5724 where that is forbidden. */
5725 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5726 && local_variable_p (decl
))
5728 error_at (id_expression
.get_location (),
5729 "local variable %qD may not appear in this context",
5731 return error_mark_node
;
5735 decl
= (finish_id_expression
5736 (id_expression
, decl
, parser
->scope
,
5738 parser
->integral_constant_expression_p
,
5739 parser
->allow_non_integral_constant_expression_p
,
5740 &parser
->non_integral_constant_expression_p
,
5741 template_p
, done
, address_p
,
5744 id_expression
.get_location ()));
5746 cp_parser_error (parser
, error_msg
);
5747 /* Build a location for an id-expression of the form:
5753 i.e. from the start of the first token to the end of the final
5754 token, with the caret at the start of the unqualified-id. */
5755 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5756 location_t start_loc
= get_start (id_expr_token
->location
);
5757 location_t finish_loc
= get_finish (id_expression
.get_location ());
5758 location_t combined_loc
5759 = make_location (caret_loc
, start_loc
, finish_loc
);
5761 decl
.set_location (combined_loc
);
5765 /* Anything else is an error. */
5767 cp_parser_error (parser
, "expected primary-expression");
5768 return error_mark_node
;
5772 static inline cp_expr
5773 cp_parser_primary_expression (cp_parser
*parser
,
5776 bool template_arg_p
,
5779 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
5780 /*decltype*/false, idk
);
5783 /* Parse an id-expression.
5790 :: [opt] nested-name-specifier template [opt] unqualified-id
5792 :: operator-function-id
5795 Return a representation of the unqualified portion of the
5796 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5797 a `::' or nested-name-specifier.
5799 Often, if the id-expression was a qualified-id, the caller will
5800 want to make a SCOPE_REF to represent the qualified-id. This
5801 function does not do this in order to avoid wastefully creating
5802 SCOPE_REFs when they are not required.
5804 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5807 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5808 uninstantiated templates.
5810 If *TEMPLATE_P is non-NULL, it is set to true iff the
5811 `template' keyword is used to explicitly indicate that the entity
5812 named is a template.
5814 If DECLARATOR_P is true, the id-expression is appearing as part of
5815 a declarator, rather than as part of an expression. */
5818 cp_parser_id_expression (cp_parser
*parser
,
5819 bool template_keyword_p
,
5820 bool check_dependency_p
,
5825 bool global_scope_p
;
5826 bool nested_name_specifier_p
;
5828 /* Assume the `template' keyword was not used. */
5830 *template_p
= template_keyword_p
;
5832 /* Look for the optional `::' operator. */
5834 = (!template_keyword_p
5835 && (cp_parser_global_scope_opt (parser
,
5836 /*current_scope_valid_p=*/false)
5839 /* Look for the optional nested-name-specifier. */
5840 nested_name_specifier_p
5841 = (cp_parser_nested_name_specifier_opt (parser
,
5842 /*typename_keyword_p=*/false,
5849 /* If there is a nested-name-specifier, then we are looking at
5850 the first qualified-id production. */
5851 if (nested_name_specifier_p
)
5854 tree saved_object_scope
;
5855 tree saved_qualifying_scope
;
5856 cp_expr unqualified_id
;
5859 /* See if the next token is the `template' keyword. */
5861 template_p
= &is_template
;
5862 *template_p
= cp_parser_optional_template_keyword (parser
);
5863 /* Name lookup we do during the processing of the
5864 unqualified-id might obliterate SCOPE. */
5865 saved_scope
= parser
->scope
;
5866 saved_object_scope
= parser
->object_scope
;
5867 saved_qualifying_scope
= parser
->qualifying_scope
;
5868 /* Process the final unqualified-id. */
5869 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
5872 /*optional_p=*/false);
5873 /* Restore the SAVED_SCOPE for our caller. */
5874 parser
->scope
= saved_scope
;
5875 parser
->object_scope
= saved_object_scope
;
5876 parser
->qualifying_scope
= saved_qualifying_scope
;
5878 return unqualified_id
;
5880 /* Otherwise, if we are in global scope, then we are looking at one
5881 of the other qualified-id productions. */
5882 else if (global_scope_p
)
5887 /* Peek at the next token. */
5888 token
= cp_lexer_peek_token (parser
->lexer
);
5890 /* If it's an identifier, and the next token is not a "<", then
5891 we can avoid the template-id case. This is an optimization
5892 for this common case. */
5893 if (token
->type
== CPP_NAME
5894 && !cp_parser_nth_token_starts_template_argument_list_p
5896 return cp_parser_identifier (parser
);
5898 cp_parser_parse_tentatively (parser
);
5899 /* Try a template-id. */
5900 id
= cp_parser_template_id_expr (parser
,
5901 /*template_keyword_p=*/false,
5902 /*check_dependency_p=*/true,
5904 /* If that worked, we're done. */
5905 if (cp_parser_parse_definitely (parser
))
5908 /* Peek at the next token. (Changes in the token buffer may
5909 have invalidated the pointer obtained above.) */
5910 token
= cp_lexer_peek_token (parser
->lexer
);
5912 switch (token
->type
)
5915 return cp_parser_identifier (parser
);
5918 if (token
->keyword
== RID_OPERATOR
)
5919 return cp_parser_operator_function_id (parser
);
5923 cp_parser_error (parser
, "expected id-expression");
5924 return error_mark_node
;
5928 return cp_parser_unqualified_id (parser
, template_keyword_p
,
5929 /*check_dependency_p=*/true,
5934 /* Parse an unqualified-id.
5938 operator-function-id
5939 conversion-function-id
5943 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5944 keyword, in a construct like `A::template ...'.
5946 Returns a representation of unqualified-id. For the `identifier'
5947 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5948 production a BIT_NOT_EXPR is returned; the operand of the
5949 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5950 other productions, see the documentation accompanying the
5951 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5952 names are looked up in uninstantiated templates. If DECLARATOR_P
5953 is true, the unqualified-id is appearing as part of a declarator,
5954 rather than as part of an expression. */
5957 cp_parser_unqualified_id (cp_parser
* parser
,
5958 bool template_keyword_p
,
5959 bool check_dependency_p
,
5965 /* Peek at the next token. */
5966 token
= cp_lexer_peek_token (parser
->lexer
);
5968 switch ((int) token
->type
)
5974 /* We don't know yet whether or not this will be a
5976 cp_parser_parse_tentatively (parser
);
5977 /* Try a template-id. */
5978 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
5981 /* If it worked, we're done. */
5982 if (cp_parser_parse_definitely (parser
))
5984 /* Otherwise, it's an ordinary identifier. */
5985 return cp_parser_identifier (parser
);
5988 case CPP_TEMPLATE_ID
:
5989 return cp_parser_template_id_expr (parser
, template_keyword_p
,
5996 tree qualifying_scope
;
6000 location_t tilde_loc
= token
->location
;
6002 /* Consume the `~' token. */
6003 cp_lexer_consume_token (parser
->lexer
);
6004 /* Parse the class-name. The standard, as written, seems to
6007 template <typename T> struct S { ~S (); };
6008 template <typename T> S<T>::~S() {}
6010 is invalid, since `~' must be followed by a class-name, but
6011 `S<T>' is dependent, and so not known to be a class.
6012 That's not right; we need to look in uninstantiated
6013 templates. A further complication arises from:
6015 template <typename T> void f(T t) {
6019 Here, it is not possible to look up `T' in the scope of `T'
6020 itself. We must look in both the current scope, and the
6021 scope of the containing complete expression.
6023 Yet another issue is:
6032 The standard does not seem to say that the `S' in `~S'
6033 should refer to the type `S' and not the data member
6036 /* DR 244 says that we look up the name after the "~" in the
6037 same scope as we looked up the qualifying name. That idea
6038 isn't fully worked out; it's more complicated than that. */
6039 scope
= parser
->scope
;
6040 object_scope
= parser
->object_scope
;
6041 qualifying_scope
= parser
->qualifying_scope
;
6043 /* Check for invalid scopes. */
6044 if (scope
== error_mark_node
)
6046 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6047 cp_lexer_consume_token (parser
->lexer
);
6048 return error_mark_node
;
6050 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6052 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6053 error_at (token
->location
,
6054 "scope %qT before %<~%> is not a class-name",
6056 cp_parser_simulate_error (parser
);
6057 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6058 cp_lexer_consume_token (parser
->lexer
);
6059 return error_mark_node
;
6061 gcc_assert (!scope
|| TYPE_P (scope
));
6063 token
= cp_lexer_peek_token (parser
->lexer
);
6065 /* Create a location with caret == start at the tilde,
6066 finishing at the end of the peeked token, e.g:
6070 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6072 /* If the name is of the form "X::~X" it's OK even if X is a
6076 && token
->type
== CPP_NAME
6077 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6079 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6080 || (CLASS_TYPE_P (scope
)
6081 && constructor_name_p (token
->u
.value
, scope
))))
6083 cp_lexer_consume_token (parser
->lexer
);
6084 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6087 /* ~auto means the destructor of whatever the object is. */
6088 if (cp_parser_is_keyword (token
, RID_AUTO
))
6090 if (cxx_dialect
< cxx14
)
6092 "%<~auto%> only available with "
6093 "%<-std=c++14%> or %<-std=gnu++14%>");
6094 cp_lexer_consume_token (parser
->lexer
);
6095 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, make_auto ());
6098 /* If there was an explicit qualification (S::~T), first look
6099 in the scope given by the qualification (i.e., S).
6101 Note: in the calls to cp_parser_class_name below we pass
6102 typename_type so that lookup finds the injected-class-name
6103 rather than the constructor. */
6105 type_decl
= NULL_TREE
;
6108 cp_parser_parse_tentatively (parser
);
6109 type_decl
= cp_parser_class_name (parser
,
6110 /*typename_keyword_p=*/false,
6111 /*template_keyword_p=*/false,
6113 /*check_dependency=*/false,
6114 /*class_head_p=*/false,
6116 if (cp_parser_parse_definitely (parser
))
6119 /* In "N::S::~S", look in "N" as well. */
6120 if (!done
&& scope
&& qualifying_scope
)
6122 cp_parser_parse_tentatively (parser
);
6123 parser
->scope
= qualifying_scope
;
6124 parser
->object_scope
= NULL_TREE
;
6125 parser
->qualifying_scope
= NULL_TREE
;
6127 = cp_parser_class_name (parser
,
6128 /*typename_keyword_p=*/false,
6129 /*template_keyword_p=*/false,
6131 /*check_dependency=*/false,
6132 /*class_head_p=*/false,
6134 if (cp_parser_parse_definitely (parser
))
6137 /* In "p->S::~T", look in the scope given by "*p" as well. */
6138 else if (!done
&& object_scope
)
6140 cp_parser_parse_tentatively (parser
);
6141 parser
->scope
= object_scope
;
6142 parser
->object_scope
= NULL_TREE
;
6143 parser
->qualifying_scope
= NULL_TREE
;
6145 = cp_parser_class_name (parser
,
6146 /*typename_keyword_p=*/false,
6147 /*template_keyword_p=*/false,
6149 /*check_dependency=*/false,
6150 /*class_head_p=*/false,
6152 if (cp_parser_parse_definitely (parser
))
6155 /* Look in the surrounding context. */
6158 parser
->scope
= NULL_TREE
;
6159 parser
->object_scope
= NULL_TREE
;
6160 parser
->qualifying_scope
= NULL_TREE
;
6161 if (processing_template_decl
)
6162 cp_parser_parse_tentatively (parser
);
6164 = cp_parser_class_name (parser
,
6165 /*typename_keyword_p=*/false,
6166 /*template_keyword_p=*/false,
6168 /*check_dependency=*/false,
6169 /*class_head_p=*/false,
6171 if (processing_template_decl
6172 && ! cp_parser_parse_definitely (parser
))
6174 /* We couldn't find a type with this name. If we're parsing
6175 tentatively, fail and try something else. */
6176 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6178 cp_parser_simulate_error (parser
);
6179 return error_mark_node
;
6181 /* Otherwise, accept it and check for a match at instantiation
6183 type_decl
= cp_parser_identifier (parser
);
6184 if (type_decl
!= error_mark_node
)
6185 type_decl
= build_min_nt_loc (loc
, BIT_NOT_EXPR
, type_decl
);
6189 /* If an error occurred, assume that the name of the
6190 destructor is the same as the name of the qualifying
6191 class. That allows us to keep parsing after running
6192 into ill-formed destructor names. */
6193 if (type_decl
== error_mark_node
&& scope
)
6194 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6195 else if (type_decl
== error_mark_node
)
6196 return error_mark_node
;
6198 /* Check that destructor name and scope match. */
6199 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6201 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6203 "declaration of %<~%T%> as member of %qT",
6205 cp_parser_simulate_error (parser
);
6206 return error_mark_node
;
6211 A typedef-name that names a class shall not be used as the
6212 identifier in the declarator for a destructor declaration. */
6214 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6215 && !DECL_SELF_REFERENCE_P (type_decl
)
6216 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6218 "typedef-name %qD used as destructor declarator",
6221 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6225 if (token
->keyword
== RID_OPERATOR
)
6229 /* This could be a template-id, so we try that first. */
6230 cp_parser_parse_tentatively (parser
);
6231 /* Try a template-id. */
6232 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6233 /*check_dependency_p=*/true,
6235 /* If that worked, we're done. */
6236 if (cp_parser_parse_definitely (parser
))
6238 /* We still don't know whether we're looking at an
6239 operator-function-id or a conversion-function-id. */
6240 cp_parser_parse_tentatively (parser
);
6241 /* Try an operator-function-id. */
6242 id
= cp_parser_operator_function_id (parser
);
6243 /* If that didn't work, try a conversion-function-id. */
6244 if (!cp_parser_parse_definitely (parser
))
6245 id
= cp_parser_conversion_function_id (parser
);
6254 cp_parser_error (parser
, "expected unqualified-id");
6255 return error_mark_node
;
6259 /* Parse an (optional) nested-name-specifier.
6261 nested-name-specifier: [C++98]
6262 class-or-namespace-name :: nested-name-specifier [opt]
6263 class-or-namespace-name :: template nested-name-specifier [opt]
6265 nested-name-specifier: [C++0x]
6268 nested-name-specifier identifier ::
6269 nested-name-specifier template [opt] simple-template-id ::
6271 PARSER->SCOPE should be set appropriately before this function is
6272 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6273 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6276 Sets PARSER->SCOPE to the class (TYPE) or namespace
6277 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6278 it unchanged if there is no nested-name-specifier. Returns the new
6279 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6281 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6282 part of a declaration and/or decl-specifier. */
6285 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6286 bool typename_keyword_p
,
6287 bool check_dependency_p
,
6289 bool is_declaration
,
6290 bool template_keyword_p
/* = false */)
6292 bool success
= false;
6293 cp_token_position start
= 0;
6296 /* Remember where the nested-name-specifier starts. */
6297 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
6298 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
6300 start
= cp_lexer_token_position (parser
->lexer
, false);
6301 push_deferring_access_checks (dk_deferred
);
6308 tree saved_qualifying_scope
;
6310 /* Spot cases that cannot be the beginning of a
6311 nested-name-specifier. */
6312 token
= cp_lexer_peek_token (parser
->lexer
);
6314 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6315 the already parsed nested-name-specifier. */
6316 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6318 /* Grab the nested-name-specifier and continue the loop. */
6319 cp_parser_pre_parsed_nested_name_specifier (parser
);
6320 /* If we originally encountered this nested-name-specifier
6321 with IS_DECLARATION set to false, we will not have
6322 resolved TYPENAME_TYPEs, so we must do so here. */
6324 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6326 new_scope
= resolve_typename_type (parser
->scope
,
6327 /*only_current_p=*/false);
6328 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6329 parser
->scope
= new_scope
;
6335 /* Spot cases that cannot be the beginning of a
6336 nested-name-specifier. On the second and subsequent times
6337 through the loop, we look for the `template' keyword. */
6338 if (success
&& token
->keyword
== RID_TEMPLATE
)
6340 /* A template-id can start a nested-name-specifier. */
6341 else if (token
->type
== CPP_TEMPLATE_ID
)
6343 /* DR 743: decltype can be used in a nested-name-specifier. */
6344 else if (token_is_decltype (token
))
6348 /* If the next token is not an identifier, then it is
6349 definitely not a type-name or namespace-name. */
6350 if (token
->type
!= CPP_NAME
)
6352 /* If the following token is neither a `<' (to begin a
6353 template-id), nor a `::', then we are not looking at a
6354 nested-name-specifier. */
6355 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6357 if (token
->type
== CPP_COLON
6358 && parser
->colon_corrects_to_scope_p
6359 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6361 gcc_rich_location
richloc (token
->location
);
6362 richloc
.add_fixit_replace ("::");
6364 "found %<:%> in nested-name-specifier, "
6366 token
->type
= CPP_SCOPE
;
6369 if (token
->type
!= CPP_SCOPE
6370 && !cp_parser_nth_token_starts_template_argument_list_p
6375 /* The nested-name-specifier is optional, so we parse
6377 cp_parser_parse_tentatively (parser
);
6379 /* Look for the optional `template' keyword, if this isn't the
6380 first time through the loop. */
6382 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6384 /* Save the old scope since the name lookup we are about to do
6385 might destroy it. */
6386 old_scope
= parser
->scope
;
6387 saved_qualifying_scope
= parser
->qualifying_scope
;
6388 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6389 look up names in "X<T>::I" in order to determine that "Y" is
6390 a template. So, if we have a typename at this point, we make
6391 an effort to look through it. */
6393 && !typename_keyword_p
6395 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6396 parser
->scope
= resolve_typename_type (parser
->scope
,
6397 /*only_current_p=*/false);
6398 /* Parse the qualifying entity. */
6400 = cp_parser_qualifying_entity (parser
,
6406 /* Look for the `::' token. */
6407 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6409 /* If we found what we wanted, we keep going; otherwise, we're
6411 if (!cp_parser_parse_definitely (parser
))
6413 bool error_p
= false;
6415 /* Restore the OLD_SCOPE since it was valid before the
6416 failed attempt at finding the last
6417 class-or-namespace-name. */
6418 parser
->scope
= old_scope
;
6419 parser
->qualifying_scope
= saved_qualifying_scope
;
6421 /* If the next token is a decltype, and the one after that is a
6422 `::', then the decltype has failed to resolve to a class or
6423 enumeration type. Give this error even when parsing
6424 tentatively since it can't possibly be valid--and we're going
6425 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6426 won't get another chance.*/
6427 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6428 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6431 token
= cp_lexer_consume_token (parser
->lexer
);
6432 tree dtype
= token
->u
.tree_check_value
->value
;
6433 if (dtype
!= error_mark_node
)
6434 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6435 "which is not a class or enumeration type",
6437 parser
->scope
= error_mark_node
;
6441 cp_lexer_consume_token (parser
->lexer
);
6444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6445 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6447 /* If we have a non-type template-id followed by ::, it can't
6448 possibly be valid. */
6449 token
= cp_lexer_peek_token (parser
->lexer
);
6450 tree tid
= token
->u
.tree_check_value
->value
;
6451 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6452 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6454 tree tmpl
= NULL_TREE
;
6455 if (is_overloaded_fn (tid
))
6457 tree fns
= get_fns (tid
);
6458 if (OVL_SINGLE_P (fns
))
6459 tmpl
= OVL_FIRST (fns
);
6460 error_at (token
->location
, "function template-id %qD "
6461 "in nested-name-specifier", tid
);
6465 /* Variable template. */
6466 tmpl
= TREE_OPERAND (tid
, 0);
6467 gcc_assert (variable_template_p (tmpl
));
6468 error_at (token
->location
, "variable template-id %qD "
6469 "in nested-name-specifier", tid
);
6472 inform (DECL_SOURCE_LOCATION (tmpl
),
6473 "%qD declared here", tmpl
);
6475 parser
->scope
= error_mark_node
;
6479 cp_lexer_consume_token (parser
->lexer
);
6480 cp_lexer_consume_token (parser
->lexer
);
6484 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6486 /* If the next token is an identifier, and the one after
6487 that is a `::', then any valid interpretation would have
6488 found a class-or-namespace-name. */
6489 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6490 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6492 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6495 token
= cp_lexer_consume_token (parser
->lexer
);
6498 if (!token
->error_reported
)
6501 tree ambiguous_decls
;
6503 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6505 /*is_template=*/false,
6506 /*is_namespace=*/false,
6507 /*check_dependency=*/true,
6510 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6511 error_at (token
->location
,
6512 "%qD used without template arguments",
6514 else if (ambiguous_decls
)
6516 // cp_parser_lookup_name has the same diagnostic,
6517 // thus make sure to emit it at most once.
6518 if (cp_parser_uncommitted_to_tentative_parse_p
6521 error_at (token
->location
,
6522 "reference to %qD is ambiguous",
6524 print_candidates (ambiguous_decls
);
6526 decl
= error_mark_node
;
6530 if (cxx_dialect
!= cxx98
)
6531 cp_parser_name_lookup_error
6532 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6535 cp_parser_name_lookup_error
6536 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6540 parser
->scope
= error_mark_node
;
6542 /* Treat this as a successful nested-name-specifier
6547 If the name found is not a class-name (clause
6548 _class_) or namespace-name (_namespace.def_), the
6549 program is ill-formed. */
6552 cp_lexer_consume_token (parser
->lexer
);
6556 /* We've found one valid nested-name-specifier. */
6558 /* Name lookup always gives us a DECL. */
6559 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6560 new_scope
= TREE_TYPE (new_scope
);
6561 /* Uses of "template" must be followed by actual templates. */
6562 if (template_keyword_p
6563 && !(CLASS_TYPE_P (new_scope
)
6564 && ((CLASSTYPE_USE_TEMPLATE (new_scope
)
6565 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope
)))
6566 || CLASSTYPE_IS_TEMPLATE (new_scope
)))
6567 && !(TREE_CODE (new_scope
) == TYPENAME_TYPE
6568 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope
))
6569 == TEMPLATE_ID_EXPR
)))
6570 permerror (input_location
, TYPE_P (new_scope
)
6571 ? G_("%qT is not a template")
6572 : G_("%qD is not a template"),
6574 /* If it is a class scope, try to complete it; we are about to
6575 be looking up names inside the class. */
6576 if (TYPE_P (new_scope
)
6577 /* Since checking types for dependency can be expensive,
6578 avoid doing it if the type is already complete. */
6579 && !COMPLETE_TYPE_P (new_scope
)
6580 /* Do not try to complete dependent types. */
6581 && !dependent_type_p (new_scope
))
6583 new_scope
= complete_type (new_scope
);
6584 /* If it is a typedef to current class, use the current
6585 class instead, as the typedef won't have any names inside
6587 if (!COMPLETE_TYPE_P (new_scope
)
6588 && currently_open_class (new_scope
))
6589 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6591 /* Make sure we look in the right scope the next time through
6593 parser
->scope
= new_scope
;
6596 /* If parsing tentatively, replace the sequence of tokens that makes
6597 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6598 token. That way, should we re-parse the token stream, we will
6599 not have to repeat the effort required to do the parse, nor will
6600 we issue duplicate error messages. */
6601 if (success
&& start
)
6605 token
= cp_lexer_token_at (parser
->lexer
, start
);
6606 /* Reset the contents of the START token. */
6607 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6608 /* Retrieve any deferred checks. Do not pop this access checks yet
6609 so the memory will not be reclaimed during token replacing below. */
6610 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6611 token
->tree_check_p
= true;
6612 token
->u
.tree_check_value
->value
= parser
->scope
;
6613 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6614 token
->u
.tree_check_value
->qualifying_scope
=
6615 parser
->qualifying_scope
;
6616 token
->keyword
= RID_MAX
;
6618 /* Purge all subsequent tokens. */
6619 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6623 pop_to_parent_deferring_access_checks ();
6625 return success
? parser
->scope
: NULL_TREE
;
6628 /* Parse a nested-name-specifier. See
6629 cp_parser_nested_name_specifier_opt for details. This function
6630 behaves identically, except that it will an issue an error if no
6631 nested-name-specifier is present. */
6634 cp_parser_nested_name_specifier (cp_parser
*parser
,
6635 bool typename_keyword_p
,
6636 bool check_dependency_p
,
6638 bool is_declaration
)
6642 /* Look for the nested-name-specifier. */
6643 scope
= cp_parser_nested_name_specifier_opt (parser
,
6648 /* If it was not present, issue an error message. */
6651 cp_parser_error (parser
, "expected nested-name-specifier");
6652 parser
->scope
= NULL_TREE
;
6658 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6659 this is either a class-name or a namespace-name (which corresponds
6660 to the class-or-namespace-name production in the grammar). For
6661 C++0x, it can also be a type-name that refers to an enumeration
6662 type or a simple-template-id.
6664 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6665 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6666 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6667 TYPE_P is TRUE iff the next name should be taken as a class-name,
6668 even the same name is declared to be another entity in the same
6671 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6672 specified by the class-or-namespace-name. If neither is found the
6673 ERROR_MARK_NODE is returned. */
6676 cp_parser_qualifying_entity (cp_parser
*parser
,
6677 bool typename_keyword_p
,
6678 bool template_keyword_p
,
6679 bool check_dependency_p
,
6681 bool is_declaration
)
6684 tree saved_qualifying_scope
;
6685 tree saved_object_scope
;
6688 bool successful_parse_p
;
6690 /* DR 743: decltype can appear in a nested-name-specifier. */
6691 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6693 scope
= cp_parser_decltype (parser
);
6694 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6695 && !MAYBE_CLASS_TYPE_P (scope
))
6697 cp_parser_simulate_error (parser
);
6698 return error_mark_node
;
6700 if (TYPE_NAME (scope
))
6701 scope
= TYPE_NAME (scope
);
6705 /* Before we try to parse the class-name, we must save away the
6706 current PARSER->SCOPE since cp_parser_class_name will destroy
6708 saved_scope
= parser
->scope
;
6709 saved_qualifying_scope
= parser
->qualifying_scope
;
6710 saved_object_scope
= parser
->object_scope
;
6711 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6712 there is no need to look for a namespace-name. */
6713 only_class_p
= template_keyword_p
6714 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6716 cp_parser_parse_tentatively (parser
);
6717 scope
= cp_parser_class_name (parser
,
6720 type_p
? class_type
: none_type
,
6722 /*class_head_p=*/false,
6724 /*enum_ok=*/cxx_dialect
> cxx98
);
6725 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
6726 /* If that didn't work, try for a namespace-name. */
6727 if (!only_class_p
&& !successful_parse_p
)
6729 /* Restore the saved scope. */
6730 parser
->scope
= saved_scope
;
6731 parser
->qualifying_scope
= saved_qualifying_scope
;
6732 parser
->object_scope
= saved_object_scope
;
6733 /* If we are not looking at an identifier followed by the scope
6734 resolution operator, then this is not part of a
6735 nested-name-specifier. (Note that this function is only used
6736 to parse the components of a nested-name-specifier.) */
6737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
6738 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
6739 return error_mark_node
;
6740 scope
= cp_parser_namespace_name (parser
);
6746 /* Return true if we are looking at a compound-literal, false otherwise. */
6749 cp_parser_compound_literal_p (cp_parser
*parser
)
6751 cp_lexer_save_tokens (parser
->lexer
);
6753 /* Skip tokens until the next token is a closing parenthesis.
6754 If we find the closing `)', and the next token is a `{', then
6755 we are looking at a compound-literal. */
6756 bool compound_literal_p
6757 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
6758 /*consume_paren=*/true)
6759 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
6761 /* Roll back the tokens we skipped. */
6762 cp_lexer_rollback_tokens (parser
->lexer
);
6764 return compound_literal_p
;
6767 /* Return true if EXPR is the integer constant zero or a complex constant
6768 of zero, without any folding, but ignoring location wrappers. */
6771 literal_integer_zerop (const_tree expr
)
6773 return (location_wrapper_p (expr
)
6774 && integer_zerop (TREE_OPERAND (expr
, 0)));
6777 /* Parse a postfix-expression.
6781 postfix-expression [ expression ]
6782 postfix-expression ( expression-list [opt] )
6783 simple-type-specifier ( expression-list [opt] )
6784 typename :: [opt] nested-name-specifier identifier
6785 ( expression-list [opt] )
6786 typename :: [opt] nested-name-specifier template [opt] template-id
6787 ( expression-list [opt] )
6788 postfix-expression . template [opt] id-expression
6789 postfix-expression -> template [opt] id-expression
6790 postfix-expression . pseudo-destructor-name
6791 postfix-expression -> pseudo-destructor-name
6792 postfix-expression ++
6793 postfix-expression --
6794 dynamic_cast < type-id > ( expression )
6795 static_cast < type-id > ( expression )
6796 reinterpret_cast < type-id > ( expression )
6797 const_cast < type-id > ( expression )
6798 typeid ( expression )
6804 ( type-id ) { initializer-list , [opt] }
6806 This extension is a GNU version of the C99 compound-literal
6807 construct. (The C99 grammar uses `type-name' instead of `type-id',
6808 but they are essentially the same concept.)
6810 If ADDRESS_P is true, the postfix expression is the operand of the
6811 `&' operator. CAST_P is true if this expression is the target of a
6814 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6815 class member access expressions [expr.ref].
6817 Returns a representation of the expression. */
6820 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
6821 bool member_access_only_p
, bool decltype_p
,
6822 cp_id_kind
* pidk_return
)
6827 cp_id_kind idk
= CP_ID_KIND_NONE
;
6828 cp_expr postfix_expression
= NULL_TREE
;
6829 bool is_member_access
= false;
6831 /* Peek at the next token. */
6832 token
= cp_lexer_peek_token (parser
->lexer
);
6833 loc
= token
->location
;
6834 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
6836 /* Some of the productions are determined by keywords. */
6837 keyword
= token
->keyword
;
6847 const char *saved_message
;
6848 bool saved_in_type_id_in_expr_p
;
6850 /* All of these can be handled in the same way from the point
6851 of view of parsing. Begin by consuming the token
6852 identifying the cast. */
6853 cp_lexer_consume_token (parser
->lexer
);
6855 /* New types cannot be defined in the cast. */
6856 saved_message
= parser
->type_definition_forbidden_message
;
6857 parser
->type_definition_forbidden_message
6858 = G_("types may not be defined in casts");
6860 /* Look for the opening `<'. */
6861 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
6862 /* Parse the type to which we are casting. */
6863 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6864 parser
->in_type_id_in_expr_p
= true;
6865 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
6867 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6868 /* Look for the closing `>'. */
6869 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
6870 /* Restore the old message. */
6871 parser
->type_definition_forbidden_message
= saved_message
;
6873 bool saved_greater_than_is_operator_p
6874 = parser
->greater_than_is_operator_p
;
6875 parser
->greater_than_is_operator_p
= true;
6877 /* And the expression which is being cast. */
6878 matching_parens parens
;
6879 parens
.require_open (parser
);
6880 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
6881 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
6883 location_t end_loc
= close_paren
?
6884 close_paren
->location
: UNKNOWN_LOCATION
;
6886 parser
->greater_than_is_operator_p
6887 = saved_greater_than_is_operator_p
;
6889 /* Only type conversions to integral or enumeration types
6890 can be used in constant-expressions. */
6891 if (!cast_valid_in_integral_constant_expression_p (type
)
6892 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
6894 postfix_expression
= error_mark_node
;
6902 = build_dynamic_cast (type
, expression
, tf_warning_or_error
);
6906 = build_static_cast (type
, expression
, tf_warning_or_error
);
6910 = build_reinterpret_cast (type
, expression
,
6911 tf_warning_or_error
);
6915 = build_const_cast (type
, expression
, tf_warning_or_error
);
6921 /* Construct a location e.g. :
6922 reinterpret_cast <int *> (expr)
6923 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6924 ranging from the start of the "*_cast" token to the final closing
6925 paren, with the caret at the start. */
6926 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
6927 postfix_expression
.set_location (cp_cast_loc
);
6934 const char *saved_message
;
6935 bool saved_in_type_id_in_expr_p
;
6937 /* Consume the `typeid' token. */
6938 cp_lexer_consume_token (parser
->lexer
);
6939 /* Look for the `(' token. */
6940 matching_parens parens
;
6941 parens
.require_open (parser
);
6942 /* Types cannot be defined in a `typeid' expression. */
6943 saved_message
= parser
->type_definition_forbidden_message
;
6944 parser
->type_definition_forbidden_message
6945 = G_("types may not be defined in a %<typeid%> expression");
6946 /* We can't be sure yet whether we're looking at a type-id or an
6948 cp_parser_parse_tentatively (parser
);
6949 /* Try a type-id first. */
6950 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
6951 parser
->in_type_id_in_expr_p
= true;
6952 type
= cp_parser_type_id (parser
);
6953 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
6954 /* Look for the `)' token. Otherwise, we can't be sure that
6955 we're not looking at an expression: consider `typeid (int
6956 (3))', for example. */
6957 cp_token
*close_paren
= parens
.require_close (parser
);
6958 /* If all went well, simply lookup the type-id. */
6959 if (cp_parser_parse_definitely (parser
))
6960 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
6961 /* Otherwise, fall back to the expression variant. */
6966 /* Look for an expression. */
6967 expression
= cp_parser_expression (parser
, & idk
);
6968 /* Compute its typeid. */
6969 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
6970 /* Look for the `)' token. */
6971 close_paren
= parens
.require_close (parser
);
6973 /* Restore the saved message. */
6974 parser
->type_definition_forbidden_message
= saved_message
;
6975 /* `typeid' may not appear in an integral constant expression. */
6976 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
6977 postfix_expression
= error_mark_node
;
6979 /* Construct a location e.g. :
6982 ranging from the start of the "typeid" token to the final closing
6983 paren, with the caret at the start. */
6986 location_t typeid_loc
6987 = make_location (start_loc
, start_loc
, close_paren
->location
);
6988 postfix_expression
.set_location (typeid_loc
);
6989 postfix_expression
.maybe_add_location_wrapper ();
6997 /* The syntax permitted here is the same permitted for an
6998 elaborated-type-specifier. */
6999 ++parser
->prevent_constrained_type_specifiers
;
7000 type
= cp_parser_elaborated_type_specifier (parser
,
7001 /*is_friend=*/false,
7002 /*is_declaration=*/false);
7003 --parser
->prevent_constrained_type_specifiers
;
7004 postfix_expression
= cp_parser_functional_cast (parser
, type
);
7009 case RID_BUILTIN_SHUFFLE
:
7010 case RID_BUILTIN_LAUNDER
:
7012 vec
<tree
, va_gc
> *vec
;
7016 cp_lexer_consume_token (parser
->lexer
);
7017 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
7018 /*cast_p=*/false, /*allow_expansion_p=*/true,
7019 /*non_constant_p=*/NULL
);
7022 postfix_expression
= error_mark_node
;
7026 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7032 if (vec
->length () == 1)
7034 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7037 error_at (loc
, "wrong number of arguments to "
7038 "%<__builtin_addressof%>");
7039 postfix_expression
= error_mark_node
;
7043 case RID_BUILTIN_LAUNDER
:
7044 if (vec
->length () == 1)
7045 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7046 tf_warning_or_error
);
7049 error_at (loc
, "wrong number of arguments to "
7050 "%<__builtin_launder%>");
7051 postfix_expression
= error_mark_node
;
7055 case RID_BUILTIN_SHUFFLE
:
7056 if (vec
->length () == 2)
7058 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7059 (*vec
)[1], tf_warning_or_error
);
7060 else if (vec
->length () == 3)
7062 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7063 (*vec
)[2], tf_warning_or_error
);
7066 error_at (loc
, "wrong number of arguments to "
7067 "%<__builtin_shuffle%>");
7068 postfix_expression
= error_mark_node
;
7078 case RID_BUILTIN_CONVERTVECTOR
:
7082 /* Consume the `__builtin_convertvector' token. */
7083 cp_lexer_consume_token (parser
->lexer
);
7084 /* Look for the opening `('. */
7085 matching_parens parens
;
7086 parens
.require_open (parser
);
7087 /* Now, parse the assignment-expression. */
7088 expression
= cp_parser_assignment_expression (parser
);
7089 /* Look for the `,'. */
7090 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7091 location_t type_location
7092 = cp_lexer_peek_token (parser
->lexer
)->location
;
7093 /* Parse the type-id. */
7095 type_id_in_expr_sentinel
s (parser
);
7096 type
= cp_parser_type_id (parser
);
7098 /* Look for the closing `)'. */
7099 parens
.require_close (parser
);
7100 return cp_build_vec_convert (expression
, type_location
, type
,
7101 tf_warning_or_error
);
7108 /* If the next thing is a simple-type-specifier, we may be
7109 looking at a functional cast. We could also be looking at
7110 an id-expression. So, we try the functional cast, and if
7111 that doesn't work we fall back to the primary-expression. */
7112 cp_parser_parse_tentatively (parser
);
7113 /* Look for the simple-type-specifier. */
7114 ++parser
->prevent_constrained_type_specifiers
;
7115 type
= cp_parser_simple_type_specifier (parser
,
7116 /*decl_specs=*/NULL
,
7117 CP_PARSER_FLAGS_NONE
);
7118 --parser
->prevent_constrained_type_specifiers
;
7119 /* Parse the cast itself. */
7120 if (!cp_parser_error_occurred (parser
))
7122 = cp_parser_functional_cast (parser
, type
);
7123 /* If that worked, we're done. */
7124 if (cp_parser_parse_definitely (parser
))
7127 /* If the functional-cast didn't work out, try a
7128 compound-literal. */
7129 if (cp_parser_allow_gnu_extensions_p (parser
)
7130 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7132 cp_expr initializer
= NULL_TREE
;
7134 cp_parser_parse_tentatively (parser
);
7136 matching_parens parens
;
7137 parens
.consume_open (parser
);
7139 /* Avoid calling cp_parser_type_id pointlessly, see comment
7140 in cp_parser_cast_expression about c++/29234. */
7141 if (!cp_parser_compound_literal_p (parser
))
7142 cp_parser_simulate_error (parser
);
7145 /* Parse the type. */
7146 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7147 parser
->in_type_id_in_expr_p
= true;
7148 type
= cp_parser_type_id (parser
);
7149 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7150 parens
.require_close (parser
);
7153 /* If things aren't going well, there's no need to
7155 if (!cp_parser_error_occurred (parser
))
7157 bool non_constant_p
;
7158 /* Parse the brace-enclosed initializer list. */
7159 initializer
= cp_parser_braced_list (parser
,
7162 /* If that worked, we're definitely looking at a
7163 compound-literal expression. */
7164 if (cp_parser_parse_definitely (parser
))
7166 /* Warn the user that a compound literal is not
7167 allowed in standard C++. */
7168 pedwarn (input_location
, OPT_Wpedantic
,
7169 "ISO C++ forbids compound-literals");
7170 /* For simplicity, we disallow compound literals in
7171 constant-expressions. We could
7172 allow compound literals of integer type, whose
7173 initializer was a constant, in constant
7174 expressions. Permitting that usage, as a further
7175 extension, would not change the meaning of any
7176 currently accepted programs. (Of course, as
7177 compound literals are not part of ISO C++, the
7178 standard has nothing to say.) */
7179 if (cp_parser_non_integral_constant_expression (parser
,
7182 postfix_expression
= error_mark_node
;
7185 /* Form the representation of the compound-literal. */
7187 = finish_compound_literal (type
, initializer
,
7188 tf_warning_or_error
, fcl_c99
);
7189 postfix_expression
.set_location (initializer
.get_location ());
7194 /* It must be a primary-expression. */
7196 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7197 /*template_arg_p=*/false,
7204 /* Note that we don't need to worry about calling build_cplus_new on a
7205 class-valued CALL_EXPR in decltype when it isn't the end of the
7206 postfix-expression; unary_complex_lvalue will take care of that for
7209 /* Keep looping until the postfix-expression is complete. */
7212 if (idk
== CP_ID_KIND_UNQUALIFIED
7213 && identifier_p (postfix_expression
)
7214 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7215 /* It is not a Koenig lookup function call. */
7217 = unqualified_name_lookup_error (postfix_expression
);
7219 /* Peek at the next token. */
7220 token
= cp_lexer_peek_token (parser
->lexer
);
7222 switch (token
->type
)
7224 case CPP_OPEN_SQUARE
:
7225 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7227 cp_parser_error (parser
,
7228 "two consecutive %<[%> shall "
7229 "only introduce an attribute");
7230 return error_mark_node
;
7233 = cp_parser_postfix_open_square_expression (parser
,
7237 postfix_expression
.set_range (start_loc
,
7238 postfix_expression
.get_location ());
7240 idk
= CP_ID_KIND_NONE
;
7241 is_member_access
= false;
7244 case CPP_OPEN_PAREN
:
7245 /* postfix-expression ( expression-list [opt] ) */
7248 bool is_builtin_constant_p
;
7249 bool saved_integral_constant_expression_p
= false;
7250 bool saved_non_integral_constant_expression_p
= false;
7251 tsubst_flags_t complain
= complain_flags (decltype_p
);
7252 vec
<tree
, va_gc
> *args
;
7253 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7255 is_member_access
= false;
7257 tree stripped_expression
7258 = tree_strip_any_location_wrapper (postfix_expression
);
7259 is_builtin_constant_p
7260 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7261 if (is_builtin_constant_p
)
7263 /* The whole point of __builtin_constant_p is to allow
7264 non-constant expressions to appear as arguments. */
7265 saved_integral_constant_expression_p
7266 = parser
->integral_constant_expression_p
;
7267 saved_non_integral_constant_expression_p
7268 = parser
->non_integral_constant_expression_p
;
7269 parser
->integral_constant_expression_p
= false;
7271 args
= (cp_parser_parenthesized_expression_list
7273 /*cast_p=*/false, /*allow_expansion_p=*/true,
7274 /*non_constant_p=*/NULL
,
7275 /*close_paren_loc=*/&close_paren_loc
,
7276 /*wrap_locations_p=*/true));
7277 if (is_builtin_constant_p
)
7279 parser
->integral_constant_expression_p
7280 = saved_integral_constant_expression_p
;
7281 parser
->non_integral_constant_expression_p
7282 = saved_non_integral_constant_expression_p
;
7287 postfix_expression
= error_mark_node
;
7291 /* Function calls are not permitted in
7292 constant-expressions. */
7293 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7294 && cp_parser_non_integral_constant_expression (parser
,
7297 postfix_expression
= error_mark_node
;
7298 release_tree_vector (args
);
7303 if (idk
== CP_ID_KIND_UNQUALIFIED
7304 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7306 if (identifier_p (postfix_expression
)
7307 /* In C++2A, we may need to perform ADL for a template
7309 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7310 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7312 if (!args
->is_empty ())
7315 if (!any_type_dependent_arguments_p (args
))
7317 = perform_koenig_lookup (postfix_expression
, args
,
7322 = unqualified_fn_lookup_error (postfix_expression
);
7324 /* We do not perform argument-dependent lookup if
7325 normal lookup finds a non-function, in accordance
7326 with the expected resolution of DR 218. */
7327 else if (!args
->is_empty ()
7328 && is_overloaded_fn (postfix_expression
))
7330 /* We only need to look at the first function,
7331 because all the fns share the attribute we're
7332 concerned with (all member fns or all local
7334 tree fn
= get_first_fn (postfix_expression
);
7335 fn
= STRIP_TEMPLATE (fn
);
7337 /* Do not do argument dependent lookup if regular
7338 lookup finds a member function or a block-scope
7339 function declaration. [basic.lookup.argdep]/3 */
7340 if (!((TREE_CODE (fn
) == USING_DECL
&& DECL_DEPENDENT_P (fn
))
7341 || DECL_FUNCTION_MEMBER_P (fn
)
7342 || DECL_LOCAL_FUNCTION_P (fn
)))
7345 if (!any_type_dependent_arguments_p (args
))
7347 = perform_koenig_lookup (postfix_expression
, args
,
7353 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7355 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7356 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7358 if (processing_template_decl
7359 && (type_dependent_object_expression_p (instance
)
7360 || (!BASELINK_P (fn
)
7361 && TREE_CODE (fn
) != FIELD_DECL
)
7362 || type_dependent_expression_p (fn
)
7363 || any_type_dependent_arguments_p (args
)))
7365 maybe_generic_this_capture (instance
, fn
);
7367 = build_min_nt_call_vec (postfix_expression
, args
);
7369 else if (BASELINK_P (fn
))
7372 = (build_new_method_call
7373 (instance
, fn
, &args
, NULL_TREE
,
7374 (idk
== CP_ID_KIND_QUALIFIED
7375 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7382 = finish_call_expr (postfix_expression
, &args
,
7383 /*disallow_virtual=*/false,
7387 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7388 || TREE_CODE (postfix_expression
) == MEMBER_REF
7389 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7390 postfix_expression
= (build_offset_ref_call_from_tree
7391 (postfix_expression
, &args
,
7393 else if (idk
== CP_ID_KIND_QUALIFIED
)
7394 /* A call to a static class member, or a namespace-scope
7397 = finish_call_expr (postfix_expression
, &args
,
7398 /*disallow_virtual=*/true,
7402 /* All other function calls. */
7404 = finish_call_expr (postfix_expression
, &args
,
7405 /*disallow_virtual=*/false,
7409 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7411 location_t combined_loc
= make_location (token
->location
,
7414 postfix_expression
.set_location (combined_loc
);
7417 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7418 idk
= CP_ID_KIND_NONE
;
7420 release_tree_vector (args
);
7426 /* postfix-expression . template [opt] id-expression
7427 postfix-expression . pseudo-destructor-name
7428 postfix-expression -> template [opt] id-expression
7429 postfix-expression -> pseudo-destructor-name */
7431 /* Consume the `.' or `->' operator. */
7432 cp_lexer_consume_token (parser
->lexer
);
7435 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7439 is_member_access
= true;
7443 /* postfix-expression ++ */
7444 /* Consume the `++' token. */
7445 cp_lexer_consume_token (parser
->lexer
);
7446 /* Generate a representation for the complete expression. */
7448 = finish_increment_expr (postfix_expression
,
7449 POSTINCREMENT_EXPR
);
7450 /* Increments may not appear in constant-expressions. */
7451 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7452 postfix_expression
= error_mark_node
;
7453 idk
= CP_ID_KIND_NONE
;
7454 is_member_access
= false;
7457 case CPP_MINUS_MINUS
:
7458 /* postfix-expression -- */
7459 /* Consume the `--' token. */
7460 cp_lexer_consume_token (parser
->lexer
);
7461 /* Generate a representation for the complete expression. */
7463 = finish_increment_expr (postfix_expression
,
7464 POSTDECREMENT_EXPR
);
7465 /* Decrements may not appear in constant-expressions. */
7466 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7467 postfix_expression
= error_mark_node
;
7468 idk
= CP_ID_KIND_NONE
;
7469 is_member_access
= false;
7473 if (pidk_return
!= NULL
)
7474 * pidk_return
= idk
;
7475 if (member_access_only_p
)
7476 return is_member_access
7477 ? postfix_expression
7478 : cp_expr (error_mark_node
);
7480 return postfix_expression
;
7484 /* We should never get here. */
7486 return error_mark_node
;
7489 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7490 by cp_parser_builtin_offsetof. We're looking for
7492 postfix-expression [ expression ]
7493 postfix-expression [ braced-init-list ] (C++11)
7495 FOR_OFFSETOF is set if we're being called in that context, which
7496 changes how we deal with integer constant expressions. */
7499 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7500 tree postfix_expression
,
7504 tree index
= NULL_TREE
;
7505 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7506 bool saved_greater_than_is_operator_p
;
7508 /* Consume the `[' token. */
7509 cp_lexer_consume_token (parser
->lexer
);
7511 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7512 parser
->greater_than_is_operator_p
= true;
7514 /* Parse the index expression. */
7515 /* ??? For offsetof, there is a question of what to allow here. If
7516 offsetof is not being used in an integral constant expression context,
7517 then we *could* get the right answer by computing the value at runtime.
7518 If we are in an integral constant expression context, then we might
7519 could accept any constant expression; hard to say without analysis.
7520 Rather than open the barn door too wide right away, allow only integer
7521 constant expressions here. */
7523 index
= cp_parser_constant_expression (parser
);
7526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7528 bool expr_nonconst_p
;
7529 cp_lexer_set_source_position (parser
->lexer
);
7530 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7531 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7534 index
= cp_parser_expression (parser
, NULL
, /*cast_p=*/false,
7535 /*decltype_p=*/false,
7536 /*warn_comma_p=*/warn_comma_subscript
);
7539 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7541 /* Look for the closing `]'. */
7542 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7544 /* Build the ARRAY_REF. */
7545 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7548 /* When not doing offsetof, array references are not permitted in
7549 constant-expressions. */
7551 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7552 postfix_expression
= error_mark_node
;
7554 return postfix_expression
;
7557 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7558 dereference of incomplete type, returns true if error_mark_node should
7559 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7560 and *DEPENDENT_P. */
7563 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7566 /* In a template, be permissive by treating an object expression
7567 of incomplete type as dependent (after a pedwarn). */
7568 diagnostic_t kind
= (processing_template_decl
7569 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7571 switch (TREE_CODE (*postfix_expression
))
7574 case REINTERPRET_CAST_EXPR
:
7575 case CONST_CAST_EXPR
:
7576 case STATIC_CAST_EXPR
:
7577 case DYNAMIC_CAST_EXPR
:
7578 case IMPLICIT_CONV_EXPR
:
7579 case VIEW_CONVERT_EXPR
:
7580 case NON_LVALUE_EXPR
:
7584 /* Don't emit any diagnostic for OVERLOADs. */
7588 /* Avoid clobbering e.g. DECLs. */
7589 if (!EXPR_P (*postfix_expression
))
7594 if (kind
== DK_IGNORED
)
7597 location_t exploc
= location_of (*postfix_expression
);
7598 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7599 if (!MAYBE_CLASS_TYPE_P (*scope
))
7601 if (kind
== DK_ERROR
)
7602 *scope
= *postfix_expression
= error_mark_node
;
7603 else if (processing_template_decl
)
7605 *dependent_p
= true;
7606 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7611 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7612 by cp_parser_builtin_offsetof. We're looking for
7614 postfix-expression . template [opt] id-expression
7615 postfix-expression . pseudo-destructor-name
7616 postfix-expression -> template [opt] id-expression
7617 postfix-expression -> pseudo-destructor-name
7619 FOR_OFFSETOF is set if we're being called in that context. That sorta
7620 limits what of the above we'll actually accept, but nevermind.
7621 TOKEN_TYPE is the "." or "->" token, which will already have been
7622 removed from the stream. */
7625 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7626 enum cpp_ttype token_type
,
7627 cp_expr postfix_expression
,
7628 bool for_offsetof
, cp_id_kind
*idk
,
7629 location_t location
)
7633 bool pseudo_destructor_p
;
7634 tree scope
= NULL_TREE
;
7635 location_t start_loc
= postfix_expression
.get_start ();
7637 /* If this is a `->' operator, dereference the pointer. */
7638 if (token_type
== CPP_DEREF
)
7639 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7640 tf_warning_or_error
);
7641 /* Check to see whether or not the expression is type-dependent and
7642 not the current instantiation. */
7643 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7644 /* The identifier following the `->' or `.' is not qualified. */
7645 parser
->scope
= NULL_TREE
;
7646 parser
->qualifying_scope
= NULL_TREE
;
7647 parser
->object_scope
= NULL_TREE
;
7648 *idk
= CP_ID_KIND_NONE
;
7650 /* Enter the scope corresponding to the type of the object
7651 given by the POSTFIX_EXPRESSION. */
7654 scope
= TREE_TYPE (postfix_expression
);
7655 /* According to the standard, no expression should ever have
7656 reference type. Unfortunately, we do not currently match
7657 the standard in this respect in that our internal representation
7658 of an expression may have reference type even when the standard
7659 says it does not. Therefore, we have to manually obtain the
7660 underlying type here. */
7661 scope
= non_reference (scope
);
7662 /* The type of the POSTFIX_EXPRESSION must be complete. */
7663 /* Unlike the object expression in other contexts, *this is not
7664 required to be of complete type for purposes of class member
7665 access (5.2.5) outside the member function body. */
7666 if (postfix_expression
!= current_class_ref
7667 && scope
!= error_mark_node
7668 && !currently_open_class (scope
))
7670 scope
= complete_type (scope
);
7671 if (!COMPLETE_TYPE_P (scope
)
7672 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7674 return error_mark_node
;
7679 /* Let the name lookup machinery know that we are processing a
7680 class member access expression. */
7681 parser
->context
->object_type
= scope
;
7682 /* If something went wrong, we want to be able to discern that case,
7683 as opposed to the case where there was no SCOPE due to the type
7684 of expression being dependent. */
7686 scope
= error_mark_node
;
7687 /* If the SCOPE was erroneous, make the various semantic analysis
7688 functions exit quickly -- and without issuing additional error
7690 if (scope
== error_mark_node
)
7691 postfix_expression
= error_mark_node
;
7696 /* Tell cp_parser_lookup_name that there was an object, even though it's
7698 parser
->context
->object_type
= unknown_type_node
;
7700 /* Assume this expression is not a pseudo-destructor access. */
7701 pseudo_destructor_p
= false;
7703 /* If the SCOPE is a scalar type, then, if this is a valid program,
7704 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7705 is type dependent, it can be pseudo-destructor-name or something else.
7706 Try to parse it as pseudo-destructor-name first. */
7707 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
7712 cp_parser_parse_tentatively (parser
);
7713 /* Parse the pseudo-destructor-name. */
7715 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
7718 && (cp_parser_error_occurred (parser
)
7719 || !SCALAR_TYPE_P (type
)))
7720 cp_parser_abort_tentative_parse (parser
);
7721 else if (cp_parser_parse_definitely (parser
))
7723 pseudo_destructor_p
= true;
7725 = finish_pseudo_destructor_expr (postfix_expression
,
7730 if (!pseudo_destructor_p
)
7732 /* If the SCOPE is not a scalar type, we are looking at an
7733 ordinary class member access expression, rather than a
7734 pseudo-destructor-name. */
7736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
7737 /* Parse the id-expression. */
7738 name
= (cp_parser_id_expression
7740 cp_parser_optional_template_keyword (parser
),
7741 /*check_dependency_p=*/true,
7743 /*declarator_p=*/false,
7744 /*optional_p=*/false));
7745 /* In general, build a SCOPE_REF if the member name is qualified.
7746 However, if the name was not dependent and has already been
7747 resolved; there is no need to build the SCOPE_REF. For example;
7749 struct X { void f(); };
7750 template <typename T> void f(T* t) { t->X::f(); }
7752 Even though "t" is dependent, "X::f" is not and has been resolved
7753 to a BASELINK; there is no need to include scope information. */
7755 /* But we do need to remember that there was an explicit scope for
7756 virtual function calls. */
7758 *idk
= CP_ID_KIND_QUALIFIED
;
7760 /* If the name is a template-id that names a type, we will get a
7761 TYPE_DECL here. That is invalid code. */
7762 if (TREE_CODE (name
) == TYPE_DECL
)
7764 error_at (token
->location
, "invalid use of %qD", name
);
7765 postfix_expression
= error_mark_node
;
7769 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
7771 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
7773 error_at (token
->location
, "%<%D::%D%> is not a class member",
7774 parser
->scope
, name
);
7775 postfix_expression
= error_mark_node
;
7778 name
= build_qualified_name (/*type=*/NULL_TREE
,
7782 parser
->scope
= NULL_TREE
;
7783 parser
->qualifying_scope
= NULL_TREE
;
7784 parser
->object_scope
= NULL_TREE
;
7786 if (parser
->scope
&& name
&& BASELINK_P (name
))
7787 adjust_result_of_qualified_name_lookup
7788 (name
, parser
->scope
, scope
);
7790 = finish_class_member_access_expr (postfix_expression
, name
,
7792 tf_warning_or_error
);
7793 /* Build a location e.g.:
7796 where the caret is at the deref token, ranging from
7797 the start of postfix_expression to the end of the access expr. */
7798 location_t combined_loc
7799 = make_location (input_location
, start_loc
, parser
->lexer
);
7800 protected_set_expr_location (postfix_expression
, combined_loc
);
7804 /* We no longer need to look up names in the scope of the object on
7805 the left-hand side of the `.' or `->' operator. */
7806 parser
->context
->object_type
= NULL_TREE
;
7808 /* Outside of offsetof, these operators may not appear in
7809 constant-expressions. */
7811 && (cp_parser_non_integral_constant_expression
7812 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
7813 postfix_expression
= error_mark_node
;
7815 return postfix_expression
;
7818 /* Parse a parenthesized expression-list.
7821 assignment-expression
7822 expression-list, assignment-expression
7827 identifier, expression-list
7829 CAST_P is true if this expression is the target of a cast.
7831 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7834 WRAP_LOCATIONS_P is true if expressions within this list for which
7835 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
7836 their source locations.
7838 Returns a vector of trees. Each element is a representation of an
7839 assignment-expression. NULL is returned if the ( and or ) are
7840 missing. An empty, but allocated, vector is returned on no
7841 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7842 if we are parsing an attribute list for an attribute that wants a
7843 plain identifier argument, normal_attr for an attribute that wants
7844 an expression, or non_attr if we aren't parsing an attribute list. If
7845 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7846 not all of the expressions in the list were constant.
7847 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7848 will be written to with the location of the closing parenthesis. If
7849 an error occurs, it may or may not be written to. */
7851 static vec
<tree
, va_gc
> *
7852 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
7853 int is_attribute_list
,
7855 bool allow_expansion_p
,
7856 bool *non_constant_p
,
7857 location_t
*close_paren_loc
,
7858 bool wrap_locations_p
)
7860 vec
<tree
, va_gc
> *expression_list
;
7861 bool fold_expr_p
= is_attribute_list
!= non_attr
;
7862 tree identifier
= NULL_TREE
;
7863 bool saved_greater_than_is_operator_p
;
7865 /* Assume all the expressions will be constant. */
7867 *non_constant_p
= false;
7869 matching_parens parens
;
7870 if (!parens
.require_open (parser
))
7873 expression_list
= make_tree_vector ();
7875 /* Within a parenthesized expression, a `>' token is always
7876 the greater-than operator. */
7877 saved_greater_than_is_operator_p
7878 = parser
->greater_than_is_operator_p
;
7879 parser
->greater_than_is_operator_p
= true;
7881 cp_expr
expr (NULL_TREE
);
7883 /* Consume expressions until there are no more. */
7884 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
7887 /* At the beginning of attribute lists, check to see if the
7888 next token is an identifier. */
7889 if (is_attribute_list
== id_attr
7890 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
7894 /* Consume the identifier. */
7895 token
= cp_lexer_consume_token (parser
->lexer
);
7896 /* Save the identifier. */
7897 identifier
= token
->u
.value
;
7901 bool expr_non_constant_p
;
7903 /* Parse the next assignment-expression. */
7904 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7906 /* A braced-init-list. */
7907 cp_lexer_set_source_position (parser
->lexer
);
7908 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7909 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
7910 if (non_constant_p
&& expr_non_constant_p
)
7911 *non_constant_p
= true;
7913 else if (non_constant_p
)
7915 expr
= (cp_parser_constant_expression
7916 (parser
, /*allow_non_constant_p=*/true,
7917 &expr_non_constant_p
));
7918 if (expr_non_constant_p
)
7919 *non_constant_p
= true;
7922 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
7926 expr
= instantiate_non_dependent_expr (expr
);
7928 /* If we have an ellipsis, then this is an expression
7930 if (allow_expansion_p
7931 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
7933 /* Consume the `...'. */
7934 cp_lexer_consume_token (parser
->lexer
);
7936 /* Build the argument pack. */
7937 expr
= make_pack_expansion (expr
);
7940 if (wrap_locations_p
)
7941 expr
.maybe_add_location_wrapper ();
7943 /* Add it to the list. We add error_mark_node
7944 expressions to the list, so that we can still tell if
7945 the correct form for a parenthesized expression-list
7946 is found. That gives better errors. */
7947 vec_safe_push (expression_list
, expr
.get_value ());
7949 if (expr
== error_mark_node
)
7953 /* After the first item, attribute lists look the same as
7954 expression lists. */
7955 is_attribute_list
= non_attr
;
7958 /* If the next token isn't a `,', then we are done. */
7959 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
7962 /* Otherwise, consume the `,' and keep going. */
7963 cp_lexer_consume_token (parser
->lexer
);
7966 if (close_paren_loc
)
7967 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7969 if (!parens
.require_close (parser
))
7974 /* We try and resync to an unnested comma, as that will give the
7975 user better diagnostics. */
7976 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
7977 /*recovering=*/true,
7979 /*consume_paren=*/true);
7984 parser
->greater_than_is_operator_p
7985 = saved_greater_than_is_operator_p
;
7990 parser
->greater_than_is_operator_p
7991 = saved_greater_than_is_operator_p
;
7994 vec_safe_insert (expression_list
, 0, identifier
);
7996 return expression_list
;
7999 /* Parse a pseudo-destructor-name.
8001 pseudo-destructor-name:
8002 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8003 :: [opt] nested-name-specifier template template-id :: ~ type-name
8004 :: [opt] nested-name-specifier [opt] ~ type-name
8006 If either of the first two productions is used, sets *SCOPE to the
8007 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8008 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8009 or ERROR_MARK_NODE if the parse fails. */
8012 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
8017 bool nested_name_specifier_p
;
8020 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8021 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8022 && !type_dependent_expression_p (object
))
8024 if (cxx_dialect
< cxx14
)
8025 pedwarn (input_location
, 0,
8026 "%<~auto%> only available with "
8027 "%<-std=c++14%> or %<-std=gnu++14%>");
8028 cp_lexer_consume_token (parser
->lexer
);
8029 cp_lexer_consume_token (parser
->lexer
);
8031 *type
= TREE_TYPE (object
);
8035 /* Assume that things will not work out. */
8036 *type
= error_mark_node
;
8038 /* Look for the optional `::' operator. */
8039 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8040 /* Look for the optional nested-name-specifier. */
8041 nested_name_specifier_p
8042 = (cp_parser_nested_name_specifier_opt (parser
,
8043 /*typename_keyword_p=*/false,
8044 /*check_dependency_p=*/true,
8046 /*is_declaration=*/false)
8048 /* Now, if we saw a nested-name-specifier, we might be doing the
8049 second production. */
8050 if (nested_name_specifier_p
8051 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8053 /* Consume the `template' keyword. */
8054 cp_lexer_consume_token (parser
->lexer
);
8055 /* Parse the template-id. */
8056 cp_parser_template_id (parser
,
8057 /*template_keyword_p=*/true,
8058 /*check_dependency_p=*/false,
8060 /*is_declaration=*/true);
8061 /* Look for the `::' token. */
8062 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8064 /* If the next token is not a `~', then there might be some
8065 additional qualification. */
8066 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8068 /* At this point, we're looking for "type-name :: ~". The type-name
8069 must not be a class-name, since this is a pseudo-destructor. So,
8070 it must be either an enum-name, or a typedef-name -- both of which
8071 are just identifiers. So, we peek ahead to check that the "::"
8072 and "~" tokens are present; if they are not, then we can avoid
8073 calling type_name. */
8074 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8075 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8076 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8078 cp_parser_error (parser
, "non-scalar type");
8082 /* Look for the type-name. */
8083 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8084 if (*scope
== error_mark_node
)
8087 /* Look for the `::' token. */
8088 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8093 /* Look for the `~'. */
8094 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8096 /* Once we see the ~, this has to be a pseudo-destructor. */
8097 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8098 cp_parser_commit_to_topmost_tentative_parse (parser
);
8100 /* Look for the type-name again. We are not responsible for
8101 checking that it matches the first type-name. */
8102 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8105 /* Parse a unary-expression.
8111 unary-operator cast-expression
8112 sizeof unary-expression
8114 alignof ( type-id ) [C++0x]
8121 __extension__ cast-expression
8122 __alignof__ unary-expression
8123 __alignof__ ( type-id )
8124 alignof unary-expression [C++0x]
8125 __real__ cast-expression
8126 __imag__ cast-expression
8128 sizeof ( type-id ) { initializer-list , [opt] }
8129 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8130 __alignof__ ( type-id ) { initializer-list , [opt] }
8132 ADDRESS_P is true iff the unary-expression is appearing as the
8133 operand of the `&' operator. CAST_P is true if this expression is
8134 the target of a cast.
8136 Returns a representation of the expression. */
8139 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8140 bool address_p
, bool cast_p
, bool decltype_p
)
8143 enum tree_code unary_operator
;
8145 /* Peek at the next token. */
8146 token
= cp_lexer_peek_token (parser
->lexer
);
8147 /* Some keywords give away the kind of expression. */
8148 if (token
->type
== CPP_KEYWORD
)
8150 enum rid keyword
= token
->keyword
;
8159 location_t start_loc
= token
->location
;
8161 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8162 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8164 /* Consume the token. */
8165 cp_lexer_consume_token (parser
->lexer
);
8166 /* Parse the operand. */
8167 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8169 if (TYPE_P (operand
))
8170 ret
= cxx_sizeof_or_alignof_type (operand
, op
, std_alignof
,
8174 /* ISO C++ defines alignof only with types, not with
8175 expressions. So pedwarn if alignof is used with a non-
8176 type expression. However, __alignof__ is ok. */
8178 pedwarn (token
->location
, OPT_Wpedantic
,
8179 "ISO C++ does not allow %<alignof%> "
8182 ret
= cxx_sizeof_or_alignof_expr (operand
, op
, true);
8184 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8185 SIZEOF_EXPR with the original operand. */
8186 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8188 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8190 if (!processing_template_decl
&& TYPE_P (operand
))
8192 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8193 build1 (NOP_EXPR
, operand
,
8195 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8198 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8199 TREE_SIDE_EFFECTS (ret
) = 0;
8200 TREE_READONLY (ret
) = 1;
8204 /* Construct a location e.g. :
8207 with start == caret at the start of the "alignof"/"sizeof"
8208 token, with the endpoint at the final closing paren. */
8209 location_t compound_loc
8210 = make_location (start_loc
, start_loc
, parser
->lexer
);
8212 cp_expr
ret_expr (ret
);
8213 ret_expr
.set_location (compound_loc
);
8214 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8218 case RID_BUILTIN_HAS_ATTRIBUTE
:
8219 return cp_parser_has_attribute_expression (parser
);
8222 return cp_parser_new_expression (parser
);
8225 return cp_parser_delete_expression (parser
);
8229 /* The saved value of the PEDANTIC flag. */
8233 /* Save away the PEDANTIC flag. */
8234 cp_parser_extension_opt (parser
, &saved_pedantic
);
8235 /* Parse the cast-expression. */
8236 expr
= cp_parser_simple_cast_expression (parser
);
8237 /* Restore the PEDANTIC flag. */
8238 pedantic
= saved_pedantic
;
8248 /* Consume the `__real__' or `__imag__' token. */
8249 cp_lexer_consume_token (parser
->lexer
);
8250 /* Parse the cast-expression. */
8251 expression
= cp_parser_simple_cast_expression (parser
);
8252 /* Create the complete representation. */
8253 return build_x_unary_op (token
->location
,
8254 (keyword
== RID_REALPART
8255 ? REALPART_EXPR
: IMAGPART_EXPR
),
8257 tf_warning_or_error
);
8261 case RID_TRANSACTION_ATOMIC
:
8262 case RID_TRANSACTION_RELAXED
:
8263 return cp_parser_transaction_expression (parser
, keyword
);
8268 const char *saved_message
;
8269 bool saved_integral_constant_expression_p
;
8270 bool saved_non_integral_constant_expression_p
;
8271 bool saved_greater_than_is_operator_p
;
8273 location_t start_loc
= token
->location
;
8275 cp_lexer_consume_token (parser
->lexer
);
8276 matching_parens parens
;
8277 parens
.require_open (parser
);
8279 saved_message
= parser
->type_definition_forbidden_message
;
8280 parser
->type_definition_forbidden_message
8281 = G_("types may not be defined in %<noexcept%> expressions");
8283 saved_integral_constant_expression_p
8284 = parser
->integral_constant_expression_p
;
8285 saved_non_integral_constant_expression_p
8286 = parser
->non_integral_constant_expression_p
;
8287 parser
->integral_constant_expression_p
= false;
8289 saved_greater_than_is_operator_p
8290 = parser
->greater_than_is_operator_p
;
8291 parser
->greater_than_is_operator_p
= true;
8293 ++cp_unevaluated_operand
;
8294 ++c_inhibit_evaluation_warnings
;
8295 ++cp_noexcept_operand
;
8296 expr
= cp_parser_expression (parser
);
8297 --cp_noexcept_operand
;
8298 --c_inhibit_evaluation_warnings
;
8299 --cp_unevaluated_operand
;
8301 parser
->greater_than_is_operator_p
8302 = saved_greater_than_is_operator_p
;
8304 parser
->integral_constant_expression_p
8305 = saved_integral_constant_expression_p
;
8306 parser
->non_integral_constant_expression_p
8307 = saved_non_integral_constant_expression_p
;
8309 parser
->type_definition_forbidden_message
= saved_message
;
8311 parens
.require_close (parser
);
8313 /* Construct a location of the form:
8316 with start == caret, finishing at the close-paren. */
8317 location_t noexcept_loc
8318 = make_location (start_loc
, start_loc
, parser
->lexer
);
8320 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8329 /* Look for the `:: new' and `:: delete', which also signal the
8330 beginning of a new-expression, or delete-expression,
8331 respectively. If the next token is `::', then it might be one of
8333 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8337 /* See if the token after the `::' is one of the keywords in
8338 which we're interested. */
8339 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8340 /* If it's `new', we have a new-expression. */
8341 if (keyword
== RID_NEW
)
8342 return cp_parser_new_expression (parser
);
8343 /* Similarly, for `delete'. */
8344 else if (keyword
== RID_DELETE
)
8345 return cp_parser_delete_expression (parser
);
8348 /* Look for a unary operator. */
8349 unary_operator
= cp_parser_unary_operator (token
);
8350 /* The `++' and `--' operators can be handled similarly, even though
8351 they are not technically unary-operators in the grammar. */
8352 if (unary_operator
== ERROR_MARK
)
8354 if (token
->type
== CPP_PLUS_PLUS
)
8355 unary_operator
= PREINCREMENT_EXPR
;
8356 else if (token
->type
== CPP_MINUS_MINUS
)
8357 unary_operator
= PREDECREMENT_EXPR
;
8358 /* Handle the GNU address-of-label extension. */
8359 else if (cp_parser_allow_gnu_extensions_p (parser
)
8360 && token
->type
== CPP_AND_AND
)
8364 location_t start_loc
= token
->location
;
8366 /* Consume the '&&' token. */
8367 cp_lexer_consume_token (parser
->lexer
);
8368 /* Look for the identifier. */
8369 identifier
= cp_parser_identifier (parser
);
8370 /* Construct a location of the form:
8373 with caret==start at the "&&", finish at the end of the label. */
8374 location_t combined_loc
8375 = make_location (start_loc
, start_loc
, parser
->lexer
);
8376 /* Create an expression representing the address. */
8377 expression
= finish_label_address_expr (identifier
, combined_loc
);
8378 if (cp_parser_non_integral_constant_expression (parser
,
8380 expression
= error_mark_node
;
8384 if (unary_operator
!= ERROR_MARK
)
8386 cp_expr cast_expression
;
8387 cp_expr expression
= error_mark_node
;
8388 non_integral_constant non_constant_p
= NIC_NONE
;
8389 location_t loc
= token
->location
;
8390 tsubst_flags_t complain
= complain_flags (decltype_p
);
8392 /* Consume the operator token. */
8393 token
= cp_lexer_consume_token (parser
->lexer
);
8394 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8396 /* Parse the cast-expression. */
8398 = cp_parser_cast_expression (parser
,
8399 unary_operator
== ADDR_EXPR
,
8405 OP_TOKEN CAST_EXPRESSION
8406 ^~~~~~~~~~~~~~~~~~~~~~~~~
8407 with start==caret at the operator token, and
8408 extending to the end of the cast_expression. */
8409 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8411 /* Now, build an appropriate representation. */
8412 switch (unary_operator
)
8415 non_constant_p
= NIC_STAR
;
8416 expression
= build_x_indirect_ref (loc
, cast_expression
,
8419 /* TODO: build_x_indirect_ref does not always honor the
8420 location, so ensure it is set. */
8421 expression
.set_location (loc
);
8425 non_constant_p
= NIC_ADDR
;
8428 expression
= build_x_unary_op (loc
, unary_operator
,
8431 /* TODO: build_x_unary_op does not always honor the location,
8432 so ensure it is set. */
8433 expression
.set_location (loc
);
8436 case PREINCREMENT_EXPR
:
8437 case PREDECREMENT_EXPR
:
8438 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8439 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8442 /* Immediately fold negation of a constant, unless the constant is 0
8443 (since -0 == 0) or it would overflow. */
8444 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8447 = tree_strip_any_location_wrapper (cast_expression
);
8448 if (CONSTANT_CLASS_P (stripped_expr
)
8449 && !integer_zerop (stripped_expr
)
8450 && !TREE_OVERFLOW (stripped_expr
))
8452 tree folded
= fold_build1 (unary_operator
,
8453 TREE_TYPE (stripped_expr
),
8455 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8457 expression
= maybe_wrap_with_location (folded
, loc
);
8463 case UNARY_PLUS_EXPR
:
8464 case TRUTH_NOT_EXPR
:
8465 expression
= finish_unary_op_expr (loc
, unary_operator
,
8466 cast_expression
, complain
);
8473 if (non_constant_p
!= NIC_NONE
8474 && cp_parser_non_integral_constant_expression (parser
,
8476 expression
= error_mark_node
;
8481 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8482 /*member_access_only_p=*/false,
8487 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8488 unary-operator, the corresponding tree code is returned. */
8490 static enum tree_code
8491 cp_parser_unary_operator (cp_token
* token
)
8493 switch (token
->type
)
8496 return INDIRECT_REF
;
8502 return UNARY_PLUS_EXPR
;
8508 return TRUTH_NOT_EXPR
;
8511 return BIT_NOT_EXPR
;
8518 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8519 Returns a representation of the expression. */
8522 cp_parser_has_attribute_expression (cp_parser
*parser
)
8524 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8526 /* Consume the __builtin_has_attribute token. */
8527 cp_lexer_consume_token (parser
->lexer
);
8529 matching_parens parens
;
8530 if (!parens
.require_open (parser
))
8531 return error_mark_node
;
8533 /* Types cannot be defined in a `sizeof' expression. Save away the
8535 const char *saved_message
= parser
->type_definition_forbidden_message
;
8536 const char *saved_message_arg
8537 = parser
->type_definition_forbidden_message_arg
;
8538 parser
->type_definition_forbidden_message
8539 = G_("types may not be defined in %qs expressions");
8540 parser
->type_definition_forbidden_message_arg
8541 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
8543 /* The restrictions on constant-expressions do not apply inside
8544 sizeof expressions. */
8545 bool saved_integral_constant_expression_p
8546 = parser
->integral_constant_expression_p
;
8547 bool saved_non_integral_constant_expression_p
8548 = parser
->non_integral_constant_expression_p
;
8549 parser
->integral_constant_expression_p
= false;
8551 /* Do not actually evaluate the expression. */
8552 ++cp_unevaluated_operand
;
8553 ++c_inhibit_evaluation_warnings
;
8555 tree oper
= NULL_TREE
;
8557 /* We can't be sure yet whether we're looking at a type-id or an
8559 cp_parser_parse_tentatively (parser
);
8561 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8562 parser
->in_type_id_in_expr_p
= true;
8563 /* Look for the type-id. */
8564 oper
= cp_parser_type_id (parser
);
8565 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8567 cp_parser_parse_definitely (parser
);
8569 /* If the type-id production did not work out, then we must be
8570 looking at an expression. */
8571 if (!oper
|| oper
== error_mark_node
)
8572 oper
= cp_parser_assignment_expression (parser
);
8574 STRIP_ANY_LOCATION_WRAPPER (oper
);
8576 /* Go back to evaluating expressions. */
8577 --cp_unevaluated_operand
;
8578 --c_inhibit_evaluation_warnings
;
8580 /* And restore the old one. */
8581 parser
->type_definition_forbidden_message
= saved_message
;
8582 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
8583 parser
->integral_constant_expression_p
8584 = saved_integral_constant_expression_p
;
8585 parser
->non_integral_constant_expression_p
8586 = saved_non_integral_constant_expression_p
;
8588 /* Consume the comma if it's there. */
8589 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8591 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8592 /*consume_paren=*/true);
8593 return error_mark_node
;
8596 /* Parse the attribute specification. */
8598 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8599 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8601 if (oper
!= error_mark_node
)
8603 /* Fold constant expressions used in attributes first. */
8604 cp_check_const_attributes (attr
);
8606 /* Finally, see if OPER has been declared with ATTR. */
8607 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8610 parens
.require_close (parser
);
8614 error_at (atloc
, "expected identifier");
8615 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8618 /* Construct a location e.g. :
8619 __builtin_has_attribute (oper, attr)
8620 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8621 with start == caret at the start of the built-in token,
8622 and with the endpoint at the final closing paren. */
8623 location_t compound_loc
8624 = make_location (start_loc
, start_loc
, parser
->lexer
);
8626 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8627 ret_expr
.set_location (compound_loc
);
8628 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8632 /* Parse a new-expression.
8635 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8636 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8638 Returns a representation of the expression. */
8641 cp_parser_new_expression (cp_parser
* parser
)
8643 bool global_scope_p
;
8644 vec
<tree
, va_gc
> *placement
;
8646 vec
<tree
, va_gc
> *initializer
;
8647 tree nelts
= NULL_TREE
;
8650 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8652 /* Look for the optional `::' operator. */
8654 = (cp_parser_global_scope_opt (parser
,
8655 /*current_scope_valid_p=*/false)
8657 /* Look for the `new' operator. */
8658 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8659 /* There's no easy way to tell a new-placement from the
8660 `( type-id )' construct. */
8661 cp_parser_parse_tentatively (parser
);
8662 /* Look for a new-placement. */
8663 placement
= cp_parser_new_placement (parser
);
8664 /* If that didn't work out, there's no new-placement. */
8665 if (!cp_parser_parse_definitely (parser
))
8667 if (placement
!= NULL
)
8668 release_tree_vector (placement
);
8672 /* If the next token is a `(', then we have a parenthesized
8674 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
8677 const char *saved_message
= parser
->type_definition_forbidden_message
;
8679 /* Consume the `('. */
8680 matching_parens parens
;
8681 parens
.consume_open (parser
);
8683 /* Parse the type-id. */
8684 parser
->type_definition_forbidden_message
8685 = G_("types may not be defined in a new-expression");
8687 type_id_in_expr_sentinel
s (parser
);
8688 type
= cp_parser_type_id (parser
);
8690 parser
->type_definition_forbidden_message
= saved_message
;
8692 /* Look for the closing `)'. */
8693 parens
.require_close (parser
);
8694 token
= cp_lexer_peek_token (parser
->lexer
);
8695 /* There should not be a direct-new-declarator in this production,
8696 but GCC used to allowed this, so we check and emit a sensible error
8697 message for this case. */
8698 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8700 error_at (token
->location
,
8701 "array bound forbidden after parenthesized type-id");
8702 inform (token
->location
,
8703 "try removing the parentheses around the type-id");
8704 cp_parser_direct_new_declarator (parser
);
8707 /* Otherwise, there must be a new-type-id. */
8709 type
= cp_parser_new_type_id (parser
, &nelts
);
8711 /* If the next token is a `(' or '{', then we have a new-initializer. */
8712 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8713 if (token
->type
== CPP_OPEN_PAREN
8714 || token
->type
== CPP_OPEN_BRACE
)
8715 initializer
= cp_parser_new_initializer (parser
);
8719 /* A new-expression may not appear in an integral constant
8721 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
8722 ret
= error_mark_node
;
8723 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8724 of a new-type-id or type-id of a new-expression, the new-expression shall
8725 contain a new-initializer of the form ( assignment-expression )".
8726 Additionally, consistently with the spirit of DR 1467, we want to accept
8727 'new auto { 2 }' too. */
8728 else if ((ret
= type_uses_auto (type
))
8729 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
8730 && (vec_safe_length (initializer
) != 1
8731 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
8732 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
8734 error_at (token
->location
,
8735 "initialization of new-expression for type %<auto%> "
8736 "requires exactly one element");
8737 ret
= error_mark_node
;
8741 /* Construct a location e.g.:
8744 with caret == start at the start of the "new" token, and the end
8745 at the end of the final token we consumed. */
8746 location_t combined_loc
= make_location (start_loc
, start_loc
,
8749 /* Create a representation of the new-expression. */
8750 ret
= build_new (&placement
, type
, nelts
, &initializer
, global_scope_p
,
8751 tf_warning_or_error
);
8752 protected_set_expr_location (ret
, combined_loc
);
8755 if (placement
!= NULL
)
8756 release_tree_vector (placement
);
8757 if (initializer
!= NULL
)
8758 release_tree_vector (initializer
);
8763 /* Parse a new-placement.
8768 Returns the same representation as for an expression-list. */
8770 static vec
<tree
, va_gc
> *
8771 cp_parser_new_placement (cp_parser
* parser
)
8773 vec
<tree
, va_gc
> *expression_list
;
8775 /* Parse the expression-list. */
8776 expression_list
= (cp_parser_parenthesized_expression_list
8777 (parser
, non_attr
, /*cast_p=*/false,
8778 /*allow_expansion_p=*/true,
8779 /*non_constant_p=*/NULL
));
8781 if (expression_list
&& expression_list
->is_empty ())
8782 error ("expected expression-list or type-id");
8784 return expression_list
;
8787 /* Parse a new-type-id.
8790 type-specifier-seq new-declarator [opt]
8792 Returns the TYPE allocated. If the new-type-id indicates an array
8793 type, *NELTS is set to the number of elements in the last array
8794 bound; the TYPE will not include the last array bound. */
8797 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
8799 cp_decl_specifier_seq type_specifier_seq
;
8800 cp_declarator
*new_declarator
;
8801 cp_declarator
*declarator
;
8802 cp_declarator
*outer_declarator
;
8803 const char *saved_message
;
8805 /* The type-specifier sequence must not contain type definitions.
8806 (It cannot contain declarations of new types either, but if they
8807 are not definitions we will catch that because they are not
8809 saved_message
= parser
->type_definition_forbidden_message
;
8810 parser
->type_definition_forbidden_message
8811 = G_("types may not be defined in a new-type-id");
8812 /* Parse the type-specifier-seq. */
8813 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
8814 /*is_declaration=*/false,
8815 /*is_trailing_return=*/false,
8816 &type_specifier_seq
);
8817 /* Restore the old message. */
8818 parser
->type_definition_forbidden_message
= saved_message
;
8820 if (type_specifier_seq
.type
== error_mark_node
)
8821 return error_mark_node
;
8823 /* Parse the new-declarator. */
8824 new_declarator
= cp_parser_new_declarator_opt (parser
);
8826 /* Determine the number of elements in the last array dimension, if
8829 /* Skip down to the last array dimension. */
8830 declarator
= new_declarator
;
8831 outer_declarator
= NULL
;
8832 while (declarator
&& (declarator
->kind
== cdk_pointer
8833 || declarator
->kind
== cdk_ptrmem
))
8835 outer_declarator
= declarator
;
8836 declarator
= declarator
->declarator
;
8839 && declarator
->kind
== cdk_array
8840 && declarator
->declarator
8841 && declarator
->declarator
->kind
== cdk_array
)
8843 outer_declarator
= declarator
;
8844 declarator
= declarator
->declarator
;
8847 if (declarator
&& declarator
->kind
== cdk_array
)
8849 *nelts
= declarator
->u
.array
.bounds
;
8850 if (*nelts
== error_mark_node
)
8851 *nelts
= integer_one_node
;
8853 if (outer_declarator
)
8854 outer_declarator
->declarator
= declarator
->declarator
;
8856 new_declarator
= NULL
;
8859 return groktypename (&type_specifier_seq
, new_declarator
, false);
8862 /* Parse an (optional) new-declarator.
8865 ptr-operator new-declarator [opt]
8866 direct-new-declarator
8868 Returns the declarator. */
8870 static cp_declarator
*
8871 cp_parser_new_declarator_opt (cp_parser
* parser
)
8873 enum tree_code code
;
8874 tree type
, std_attributes
= NULL_TREE
;
8875 cp_cv_quals cv_quals
;
8877 /* We don't know if there's a ptr-operator next, or not. */
8878 cp_parser_parse_tentatively (parser
);
8879 /* Look for a ptr-operator. */
8880 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
8881 /* If that worked, look for more new-declarators. */
8882 if (cp_parser_parse_definitely (parser
))
8884 cp_declarator
*declarator
;
8886 /* Parse another optional declarator. */
8887 declarator
= cp_parser_new_declarator_opt (parser
);
8889 declarator
= cp_parser_make_indirect_declarator
8890 (code
, type
, cv_quals
, declarator
, std_attributes
);
8895 /* If the next token is a `[', there is a direct-new-declarator. */
8896 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
8897 return cp_parser_direct_new_declarator (parser
);
8902 /* Parse a direct-new-declarator.
8904 direct-new-declarator:
8906 direct-new-declarator [constant-expression]
8910 static cp_declarator
*
8911 cp_parser_direct_new_declarator (cp_parser
* parser
)
8913 cp_declarator
*declarator
= NULL
;
8920 /* Look for the opening `['. */
8921 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
8923 token
= cp_lexer_peek_token (parser
->lexer
);
8924 expression
= cp_parser_expression (parser
);
8925 /* The standard requires that the expression have integral
8926 type. DR 74 adds enumeration types. We believe that the
8927 real intent is that these expressions be handled like the
8928 expression in a `switch' condition, which also allows
8929 classes with a single conversion to integral or
8930 enumeration type. */
8931 if (!processing_template_decl
)
8934 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
8939 error_at (token
->location
,
8940 "expression in new-declarator must have integral "
8941 "or enumeration type");
8942 expression
= error_mark_node
;
8946 /* Look for the closing `]'. */
8947 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
8949 /* Add this bound to the declarator. */
8950 declarator
= make_array_declarator (declarator
, expression
);
8952 /* If the next token is not a `[', then there are no more
8954 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
8961 /* Parse a new-initializer.
8964 ( expression-list [opt] )
8967 Returns a representation of the expression-list. */
8969 static vec
<tree
, va_gc
> *
8970 cp_parser_new_initializer (cp_parser
* parser
)
8972 vec
<tree
, va_gc
> *expression_list
;
8974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8977 bool expr_non_constant_p
;
8978 cp_lexer_set_source_position (parser
->lexer
);
8979 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8980 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8981 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
8982 expression_list
= make_tree_vector_single (t
);
8985 expression_list
= (cp_parser_parenthesized_expression_list
8986 (parser
, non_attr
, /*cast_p=*/false,
8987 /*allow_expansion_p=*/true,
8988 /*non_constant_p=*/NULL
));
8990 return expression_list
;
8993 /* Parse a delete-expression.
8996 :: [opt] delete cast-expression
8997 :: [opt] delete [ ] cast-expression
8999 Returns a representation of the expression. */
9002 cp_parser_delete_expression (cp_parser
* parser
)
9004 bool global_scope_p
;
9007 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9009 /* Look for the optional `::' operator. */
9011 = (cp_parser_global_scope_opt (parser
,
9012 /*current_scope_valid_p=*/false)
9014 /* Look for the `delete' keyword. */
9015 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9016 /* See if the array syntax is in use. */
9017 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9019 /* Consume the `[' token. */
9020 cp_lexer_consume_token (parser
->lexer
);
9021 /* Look for the `]' token. */
9022 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9023 /* Remember that this is the `[]' construct. */
9029 /* Parse the cast-expression. */
9030 expression
= cp_parser_simple_cast_expression (parser
);
9032 /* A delete-expression may not appear in an integral constant
9034 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9035 return error_mark_node
;
9037 /* Construct a location e.g.:
9040 with caret == start at the start of the "delete" token, and
9041 the end at the end of the final token we consumed. */
9042 location_t combined_loc
= make_location (start_loc
, start_loc
,
9044 expression
= delete_sanity (expression
, NULL_TREE
, array_p
,
9045 global_scope_p
, tf_warning_or_error
);
9046 protected_set_expr_location (expression
, combined_loc
);
9051 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9052 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9056 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9058 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9059 switch (token
->type
)
9065 case CPP_CLOSE_SQUARE
:
9066 case CPP_CLOSE_PAREN
:
9067 case CPP_CLOSE_BRACE
:
9068 case CPP_OPEN_BRACE
:
9072 case CPP_DEREF_STAR
:
9080 case CPP_GREATER_EQ
:
9101 case CPP_OPEN_PAREN
:
9102 /* In ((type ()) () the last () isn't a valid cast-expression,
9103 so the whole must be parsed as postfix-expression. */
9104 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9107 case CPP_OPEN_SQUARE
:
9108 /* '[' may start a primary-expression in obj-c++ and in C++11,
9109 as a lambda-expression, eg, '(void)[]{}'. */
9110 if (cxx_dialect
>= cxx11
)
9112 return c_dialect_objc ();
9115 case CPP_MINUS_MINUS
:
9116 /* '++' and '--' may or may not start a cast-expression:
9118 struct T { void operator++(int); };
9119 void f() { (T())++; }
9132 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9133 in the order: const_cast, static_cast, reinterpret_cast.
9135 Don't suggest dynamic_cast.
9137 Return the first legal cast kind found, or NULL otherwise. */
9140 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9144 /* Reuse the parser logic by attempting to build the various kinds of
9145 cast, with "complain" disabled.
9146 Identify the first such cast that is valid. */
9148 /* Don't attempt to run such logic within template processing. */
9149 if (processing_template_decl
)
9152 /* First try const_cast. */
9153 trial
= build_const_cast (dst_type
, orig_expr
, tf_none
);
9154 if (trial
!= error_mark_node
)
9155 return "const_cast";
9157 /* If that fails, try static_cast. */
9158 trial
= build_static_cast (dst_type
, orig_expr
, tf_none
);
9159 if (trial
!= error_mark_node
)
9160 return "static_cast";
9162 /* Finally, try reinterpret_cast. */
9163 trial
= build_reinterpret_cast (dst_type
, orig_expr
, tf_none
);
9164 if (trial
!= error_mark_node
)
9165 return "reinterpret_cast";
9167 /* No such cast possible. */
9171 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9172 suggesting how to convert a C-style cast of the form:
9176 to a C++-style cast.
9178 The primary range of RICHLOC is asssumed to be that of the original
9179 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9180 of the parens in the C-style cast. */
9183 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9184 location_t close_paren_loc
, tree orig_expr
,
9187 /* This function is non-trivial, so bail out now if the warning isn't
9188 going to be emitted. */
9189 if (!warn_old_style_cast
)
9192 /* Try to find a legal C++ cast, trying them in order:
9193 const_cast, static_cast, reinterpret_cast. */
9194 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9195 if (!cast_suggestion
)
9198 /* Replace the open paren with "CAST_SUGGESTION<". */
9200 pp_printf (&pp
, "%s<", cast_suggestion
);
9201 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9203 /* Replace the close paren with "> (". */
9204 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9206 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9207 rich_loc
->add_fixit_insert_after (")");
9211 /* Parse a cast-expression.
9215 ( type-id ) cast-expression
9217 ADDRESS_P is true iff the unary-expression is appearing as the
9218 operand of the `&' operator. CAST_P is true if this expression is
9219 the target of a cast.
9221 Returns a representation of the expression. */
9224 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9225 bool decltype_p
, cp_id_kind
* pidk
)
9227 /* If it's a `(', then we might be looking at a cast. */
9228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9230 tree type
= NULL_TREE
;
9231 cp_expr
expr (NULL_TREE
);
9232 int cast_expression
= 0;
9233 const char *saved_message
;
9235 /* There's no way to know yet whether or not this is a cast.
9236 For example, `(int (3))' is a unary-expression, while `(int)
9237 3' is a cast. So, we resort to parsing tentatively. */
9238 cp_parser_parse_tentatively (parser
);
9239 /* Types may not be defined in a cast. */
9240 saved_message
= parser
->type_definition_forbidden_message
;
9241 parser
->type_definition_forbidden_message
9242 = G_("types may not be defined in casts");
9243 /* Consume the `('. */
9244 matching_parens parens
;
9245 cp_token
*open_paren
= parens
.consume_open (parser
);
9246 location_t open_paren_loc
= open_paren
->location
;
9247 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9249 /* A very tricky bit is that `(struct S) { 3 }' is a
9250 compound-literal (which we permit in C++ as an extension).
9251 But, that construct is not a cast-expression -- it is a
9252 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9253 is legal; if the compound-literal were a cast-expression,
9254 you'd need an extra set of parentheses.) But, if we parse
9255 the type-id, and it happens to be a class-specifier, then we
9256 will commit to the parse at that point, because we cannot
9257 undo the action that is done when creating a new class. So,
9258 then we cannot back up and do a postfix-expression.
9260 Another tricky case is the following (c++/29234):
9262 struct S { void operator () (); };
9269 As a type-id we parse the parenthesized S()() as a function
9270 returning a function, groktypename complains and we cannot
9271 back up in this case either.
9273 Therefore, we scan ahead to the closing `)', and check to see
9274 if the tokens after the `)' can start a cast-expression. Otherwise
9275 we are dealing with an unary-expression, a postfix-expression
9278 Yet another tricky case, in C++11, is the following (c++/54891):
9282 The issue is that usually, besides the case of lambda-expressions,
9283 the parenthesized type-id cannot be followed by '[', and, eg, we
9284 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9285 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9286 we don't commit, we try a cast-expression, then an unary-expression.
9288 Save tokens so that we can put them back. */
9289 cp_lexer_save_tokens (parser
->lexer
);
9291 /* We may be looking at a cast-expression. */
9292 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9293 /*consume_paren=*/true))
9295 = cp_parser_tokens_start_cast_expression (parser
);
9297 /* Roll back the tokens we skipped. */
9298 cp_lexer_rollback_tokens (parser
->lexer
);
9299 /* If we aren't looking at a cast-expression, simulate an error so
9300 that the call to cp_parser_error_occurred below returns true. */
9301 if (!cast_expression
)
9302 cp_parser_simulate_error (parser
);
9305 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9306 parser
->in_type_id_in_expr_p
= true;
9307 /* Look for the type-id. */
9308 type
= cp_parser_type_id (parser
);
9309 /* Look for the closing `)'. */
9310 cp_token
*close_paren
= parens
.require_close (parser
);
9312 close_paren_loc
= close_paren
->location
;
9313 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9316 /* Restore the saved message. */
9317 parser
->type_definition_forbidden_message
= saved_message
;
9319 /* At this point this can only be either a cast or a
9320 parenthesized ctor such as `(T ())' that looks like a cast to
9321 function returning T. */
9322 if (!cp_parser_error_occurred (parser
))
9324 /* Only commit if the cast-expression doesn't start with
9325 '++', '--', or '[' in C++11. */
9326 if (cast_expression
> 0)
9327 cp_parser_commit_to_topmost_tentative_parse (parser
);
9329 expr
= cp_parser_cast_expression (parser
,
9330 /*address_p=*/false,
9332 /*decltype_p=*/false,
9335 if (cp_parser_parse_definitely (parser
))
9337 /* Warn about old-style casts, if so requested. */
9338 if (warn_old_style_cast
9339 && !in_system_header_at (input_location
)
9340 && !VOID_TYPE_P (type
)
9341 && current_lang_name
!= lang_name_c
)
9343 gcc_rich_location
rich_loc (input_location
);
9344 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9346 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9347 "use of old-style cast to %q#T", type
);
9350 /* Only type conversions to integral or enumeration types
9351 can be used in constant-expressions. */
9352 if (!cast_valid_in_integral_constant_expression_p (type
)
9353 && cp_parser_non_integral_constant_expression (parser
,
9355 return error_mark_node
;
9357 /* Perform the cast. */
9361 with start==caret at the open paren, extending to the
9363 location_t cast_loc
= make_location (open_paren_loc
,
9365 expr
.get_finish ());
9366 expr
= build_c_cast (cast_loc
, type
, expr
);
9371 cp_parser_abort_tentative_parse (parser
);
9374 /* If we get here, then it's not a cast, so it must be a
9375 unary-expression. */
9376 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9377 cast_p
, decltype_p
);
9380 /* Parse a binary expression of the general form:
9384 pm-expression .* cast-expression
9385 pm-expression ->* cast-expression
9387 multiplicative-expression:
9389 multiplicative-expression * pm-expression
9390 multiplicative-expression / pm-expression
9391 multiplicative-expression % pm-expression
9393 additive-expression:
9394 multiplicative-expression
9395 additive-expression + multiplicative-expression
9396 additive-expression - multiplicative-expression
9400 shift-expression << additive-expression
9401 shift-expression >> additive-expression
9403 relational-expression:
9405 relational-expression < shift-expression
9406 relational-expression > shift-expression
9407 relational-expression <= shift-expression
9408 relational-expression >= shift-expression
9412 relational-expression:
9413 relational-expression <? shift-expression
9414 relational-expression >? shift-expression
9416 equality-expression:
9417 relational-expression
9418 equality-expression == relational-expression
9419 equality-expression != relational-expression
9423 and-expression & equality-expression
9425 exclusive-or-expression:
9427 exclusive-or-expression ^ and-expression
9429 inclusive-or-expression:
9430 exclusive-or-expression
9431 inclusive-or-expression | exclusive-or-expression
9433 logical-and-expression:
9434 inclusive-or-expression
9435 logical-and-expression && inclusive-or-expression
9437 logical-or-expression:
9438 logical-and-expression
9439 logical-or-expression || logical-and-expression
9441 All these are implemented with a single function like:
9444 simple-cast-expression
9445 binary-expression <token> binary-expression
9447 CAST_P is true if this expression is the target of a cast.
9449 The binops_by_token map is used to get the tree codes for each <token> type.
9450 binary-expressions are associated according to a precedence table. */
9452 #define TOKEN_PRECEDENCE(token) \
9453 (((token->type == CPP_GREATER \
9454 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9455 && !parser->greater_than_is_operator_p) \
9456 ? PREC_NOT_OPERATOR \
9457 : binops_by_token[token->type].prec)
9460 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9461 bool no_toplevel_fold_p
,
9463 enum cp_parser_prec prec
,
9466 cp_parser_expression_stack stack
;
9467 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9468 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9469 cp_parser_expression_stack_entry current
;
9472 enum tree_code rhs_type
;
9473 enum cp_parser_prec new_prec
, lookahead_prec
;
9476 /* Parse the first expression. */
9477 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9478 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9479 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9480 cast_p
, decltype_p
, pidk
);
9481 current
.prec
= prec
;
9483 if (cp_parser_error_occurred (parser
))
9484 return error_mark_node
;
9488 /* Get an operator token. */
9489 token
= cp_lexer_peek_token (parser
->lexer
);
9491 if (warn_cxx11_compat
9492 && token
->type
== CPP_RSHIFT
9493 && !parser
->greater_than_is_operator_p
)
9495 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9496 "%<>>%> operator is treated"
9497 " as two right angle brackets in C++11"))
9498 inform (token
->location
,
9499 "suggest parentheses around %<>>%> expression");
9502 new_prec
= TOKEN_PRECEDENCE (token
);
9503 if (new_prec
!= PREC_NOT_OPERATOR
9504 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9505 /* This is a fold-expression; handle it later. */
9506 new_prec
= PREC_NOT_OPERATOR
;
9508 /* Popping an entry off the stack means we completed a subexpression:
9509 - either we found a token which is not an operator (`>' where it is not
9510 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9511 will happen repeatedly;
9512 - or, we found an operator which has lower priority. This is the case
9513 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9515 if (new_prec
<= current
.prec
)
9524 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9525 current
.loc
= token
->location
;
9527 /* We used the operator token. */
9528 cp_lexer_consume_token (parser
->lexer
);
9530 /* For "false && x" or "true || x", x will never be executed;
9531 disable warnings while evaluating it. */
9532 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
9533 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
9534 || (current
.tree_type
== TRUTH_ORIF_EXPR
9535 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
9537 disable_warnings_sp
= sp
;
9538 ++c_inhibit_evaluation_warnings
;
9541 /* Extract another operand. It may be the RHS of this expression
9542 or the LHS of a new, higher priority expression. */
9543 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9544 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9545 rhs
= cp_parser_simple_cast_expression (parser
);
9547 /* Get another operator token. Look up its precedence to avoid
9548 building a useless (immediately popped) stack entry for common
9549 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9550 token
= cp_lexer_peek_token (parser
->lexer
);
9551 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9552 if (lookahead_prec
!= PREC_NOT_OPERATOR
9553 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9554 lookahead_prec
= PREC_NOT_OPERATOR
;
9555 if (lookahead_prec
> new_prec
)
9557 /* ... and prepare to parse the RHS of the new, higher priority
9558 expression. Since precedence levels on the stack are
9559 monotonically increasing, we do not have to care about
9564 current
.lhs_type
= rhs_type
;
9565 current
.prec
= new_prec
;
9566 new_prec
= lookahead_prec
;
9570 lookahead_prec
= new_prec
;
9571 /* If the stack is not empty, we have parsed into LHS the right side
9572 (`4' in the example above) of an expression we had suspended.
9573 We can use the information on the stack to recover the LHS (`3')
9574 from the stack together with the tree code (`MULT_EXPR'), and
9575 the precedence of the higher level subexpression
9576 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9577 which will be used to actually build the additive expression. */
9579 rhs_type
= current
.lhs_type
;
9584 /* Undo the disabling of warnings done above. */
9585 if (sp
== disable_warnings_sp
)
9587 disable_warnings_sp
= NULL
;
9588 --c_inhibit_evaluation_warnings
;
9591 if (warn_logical_not_paren
9592 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9593 && current
.lhs_type
== TRUTH_NOT_EXPR
9594 /* Avoid warning for !!x == y. */
9595 && (TREE_CODE (current
.lhs
) != NE_EXPR
9596 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9597 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9598 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9599 /* Avoid warning for !b == y where b is boolean. */
9600 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9601 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9603 /* Avoid warning for !!b == y where b is boolean. */
9604 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9605 || TREE_TYPE (current
.lhs
) == NULL_TREE
9606 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9607 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9608 current
.lhs
, maybe_constant_value (rhs
));
9612 location_t combined_loc
= make_location (current
.loc
,
9613 current
.lhs
.get_start (),
9616 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9617 ERROR_MARK for everything that is not a binary expression.
9618 This makes warn_about_parentheses miss some warnings that
9619 involve unary operators. For unary expressions we should
9620 pass the correct tree_code unless the unary expression was
9621 surrounded by parentheses.
9623 if (no_toplevel_fold_p
9624 && lookahead_prec
<= current
.prec
9627 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9628 current
.lhs
= error_mark_node
;
9631 current
.lhs
.maybe_add_location_wrapper ();
9632 rhs
.maybe_add_location_wrapper ();
9634 = build_min (current
.tree_type
,
9635 TREE_CODE_CLASS (current
.tree_type
)
9637 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9638 current
.lhs
.get_value (), rhs
.get_value ());
9639 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9644 op_location_t
op_loc (current
.loc
, combined_loc
);
9645 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9646 current
.lhs
, current
.lhs_type
,
9647 rhs
, rhs_type
, &overload
,
9648 complain_flags (decltype_p
));
9649 /* TODO: build_x_binary_op doesn't always honor the location. */
9650 current
.lhs
.set_location (combined_loc
);
9652 current
.lhs_type
= current
.tree_type
;
9654 /* If the binary operator required the use of an overloaded operator,
9655 then this expression cannot be an integral constant-expression.
9656 An overloaded operator can be used even if both operands are
9657 otherwise permissible in an integral constant-expression if at
9658 least one of the operands is of enumeration type. */
9661 && cp_parser_non_integral_constant_expression (parser
,
9663 return error_mark_node
;
9670 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9671 bool no_toplevel_fold_p
,
9672 enum cp_parser_prec prec
,
9675 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
9676 /*decltype*/false, prec
, pidk
);
9679 /* Parse the `? expression : assignment-expression' part of a
9680 conditional-expression. The LOGICAL_OR_EXPR is the
9681 logical-or-expression that started the conditional-expression.
9682 Returns a representation of the entire conditional-expression.
9684 This routine is used by cp_parser_assignment_expression.
9686 ? expression : assignment-expression
9690 ? : assignment-expression */
9693 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
9695 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
9696 cp_expr assignment_expr
;
9697 struct cp_token
*token
;
9698 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9700 /* Consume the `?' token. */
9701 cp_lexer_consume_token (parser
->lexer
);
9702 token
= cp_lexer_peek_token (parser
->lexer
);
9703 if (cp_parser_allow_gnu_extensions_p (parser
)
9704 && token
->type
== CPP_COLON
)
9706 pedwarn (token
->location
, OPT_Wpedantic
,
9707 "ISO C++ does not allow %<?:%> with omitted middle operand");
9708 /* Implicit true clause. */
9710 c_inhibit_evaluation_warnings
+=
9711 folded_logical_or_expr
== truthvalue_true_node
;
9712 warn_for_omitted_condop (token
->location
, logical_or_expr
);
9716 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
9717 parser
->colon_corrects_to_scope_p
= false;
9718 /* Parse the expression. */
9719 c_inhibit_evaluation_warnings
+=
9720 folded_logical_or_expr
== truthvalue_false_node
;
9721 expr
= cp_parser_expression (parser
);
9722 c_inhibit_evaluation_warnings
+=
9723 ((folded_logical_or_expr
== truthvalue_true_node
)
9724 - (folded_logical_or_expr
== truthvalue_false_node
));
9725 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
9728 /* The next token should be a `:'. */
9729 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
9730 /* Parse the assignment-expression. */
9731 assignment_expr
= cp_parser_assignment_expression (parser
);
9732 c_inhibit_evaluation_warnings
-=
9733 folded_logical_or_expr
== truthvalue_true_node
;
9736 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9737 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9738 with the caret at the "?", ranging from the start of
9739 the logical_or_expr to the end of the assignment_expr. */
9740 loc
= make_location (loc
,
9741 logical_or_expr
.get_start (),
9742 assignment_expr
.get_finish ());
9744 /* Build the conditional-expression. */
9745 return build_x_conditional_expr (loc
, logical_or_expr
,
9748 tf_warning_or_error
);
9751 /* Parse an assignment-expression.
9753 assignment-expression:
9754 conditional-expression
9755 logical-or-expression assignment-operator assignment_expression
9758 CAST_P is true if this expression is the target of a cast.
9759 DECLTYPE_P is true if this expression is the operand of decltype.
9761 Returns a representation for the expression. */
9764 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9765 bool cast_p
, bool decltype_p
)
9769 /* If the next token is the `throw' keyword, then we're looking at
9770 a throw-expression. */
9771 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
9772 expr
= cp_parser_throw_expression (parser
);
9773 /* Otherwise, it must be that we are looking at a
9774 logical-or-expression. */
9777 /* Parse the binary expressions (logical-or-expression). */
9778 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
9780 PREC_NOT_OPERATOR
, pidk
);
9781 /* If the next token is a `?' then we're actually looking at a
9782 conditional-expression. */
9783 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
9784 return cp_parser_question_colon_clause (parser
, expr
);
9787 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9789 /* If it's an assignment-operator, we're using the second
9791 enum tree_code assignment_operator
9792 = cp_parser_assignment_operator_opt (parser
);
9793 if (assignment_operator
!= ERROR_MARK
)
9795 bool non_constant_p
;
9797 /* Parse the right-hand side of the assignment. */
9798 cp_expr rhs
= cp_parser_initializer_clause (parser
,
9801 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
9802 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9804 /* An assignment may not appear in a
9805 constant-expression. */
9806 if (cp_parser_non_integral_constant_expression (parser
,
9808 return error_mark_node
;
9809 /* Build the assignment expression. Its default
9813 is the location of the '=' token as the
9814 caret, ranging from the start of the lhs to the
9816 loc
= make_location (loc
,
9819 expr
= build_x_modify_expr (loc
, expr
,
9820 assignment_operator
,
9822 complain_flags (decltype_p
));
9823 /* TODO: build_x_modify_expr doesn't honor the location,
9824 so we must set it here. */
9825 expr
.set_location (loc
);
9833 /* Parse an (optional) assignment-operator.
9835 assignment-operator: one of
9836 = *= /= %= += -= >>= <<= &= ^= |=
9840 assignment-operator: one of
9843 If the next token is an assignment operator, the corresponding tree
9844 code is returned, and the token is consumed. For example, for
9845 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9846 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9847 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9848 operator, ERROR_MARK is returned. */
9850 static enum tree_code
9851 cp_parser_assignment_operator_opt (cp_parser
* parser
)
9856 /* Peek at the next token. */
9857 token
= cp_lexer_peek_token (parser
->lexer
);
9859 switch (token
->type
)
9870 op
= TRUNC_DIV_EXPR
;
9874 op
= TRUNC_MOD_EXPR
;
9906 /* Nothing else is an assignment operator. */
9910 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9911 if (op
!= ERROR_MARK
9912 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9915 /* If it was an assignment operator, consume it. */
9916 if (op
!= ERROR_MARK
)
9917 cp_lexer_consume_token (parser
->lexer
);
9922 /* Parse an expression.
9925 assignment-expression
9926 expression , assignment-expression
9928 CAST_P is true if this expression is the target of a cast.
9929 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9930 except possibly parenthesized or on the RHS of a comma (N3276).
9931 WARN_COMMA_P is true if a comma should be diagnosed.
9933 Returns a representation of the expression. */
9936 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
9937 bool cast_p
, bool decltype_p
, bool warn_comma_p
)
9939 cp_expr expression
= NULL_TREE
;
9940 location_t loc
= UNKNOWN_LOCATION
;
9944 cp_expr assignment_expression
;
9946 /* Parse the next assignment-expression. */
9947 assignment_expression
9948 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
9950 /* We don't create a temporary for a call that is the immediate operand
9951 of decltype or on the RHS of a comma. But when we see a comma, we
9952 need to create a temporary for a call on the LHS. */
9953 if (decltype_p
&& !processing_template_decl
9954 && TREE_CODE (assignment_expression
) == CALL_EXPR
9955 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
9956 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
9957 assignment_expression
9958 = build_cplus_new (TREE_TYPE (assignment_expression
),
9959 assignment_expression
, tf_warning_or_error
);
9961 /* If this is the first assignment-expression, we can just
9964 expression
= assignment_expression
;
9967 /* Create a location with caret at the comma, ranging
9968 from the start of the LHS to the end of the RHS. */
9969 loc
= make_location (loc
,
9970 expression
.get_start (),
9971 assignment_expression
.get_finish ());
9972 expression
= build_x_compound_expr (loc
, expression
,
9973 assignment_expression
,
9974 complain_flags (decltype_p
));
9975 expression
.set_location (loc
);
9977 /* If the next token is not a comma, or we're in a fold-expression, then
9978 we are done with the expression. */
9979 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
9980 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9982 /* Consume the `,'. */
9983 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9986 /* [depr.comma.subscript]: A comma expression appearing as
9987 the expr-or-braced-init-list of a subscripting expression
9988 is deprecated. A parenthesized comma expression is not
9990 warning_at (loc
, OPT_Wcomma_subscript
,
9991 "top-level comma expression in array subscript "
9993 warn_comma_p
= false;
9995 cp_lexer_consume_token (parser
->lexer
);
9996 /* A comma operator cannot appear in a constant-expression. */
9997 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
9998 expression
= error_mark_node
;
10004 /* Parse a constant-expression.
10006 constant-expression:
10007 conditional-expression
10009 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10010 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10011 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10012 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10013 only parse a conditional-expression, otherwise parse an
10014 assignment-expression. See below for rationale. */
10017 cp_parser_constant_expression (cp_parser
* parser
,
10018 bool allow_non_constant_p
,
10019 bool *non_constant_p
,
10022 bool saved_integral_constant_expression_p
;
10023 bool saved_allow_non_integral_constant_expression_p
;
10024 bool saved_non_integral_constant_expression_p
;
10025 cp_expr expression
;
10027 /* It might seem that we could simply parse the
10028 conditional-expression, and then check to see if it were
10029 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10030 one that the compiler can figure out is constant, possibly after
10031 doing some simplifications or optimizations. The standard has a
10032 precise definition of constant-expression, and we must honor
10033 that, even though it is somewhat more restrictive.
10039 is not a legal declaration, because `(2, 3)' is not a
10040 constant-expression. The `,' operator is forbidden in a
10041 constant-expression. However, GCC's constant-folding machinery
10042 will fold this operation to an INTEGER_CST for `3'. */
10044 /* Save the old settings. */
10045 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10046 saved_allow_non_integral_constant_expression_p
10047 = parser
->allow_non_integral_constant_expression_p
;
10048 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10049 /* We are now parsing a constant-expression. */
10050 parser
->integral_constant_expression_p
= true;
10051 parser
->allow_non_integral_constant_expression_p
10052 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10053 parser
->non_integral_constant_expression_p
= false;
10054 /* Although the grammar says "conditional-expression", when not STRICT_P,
10055 we parse an "assignment-expression", which also permits
10056 "throw-expression" and the use of assignment operators. In the case
10057 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10058 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10059 actually essential that we look for an assignment-expression.
10060 For example, cp_parser_initializer_clauses uses this function to
10061 determine whether a particular assignment-expression is in fact
10065 /* Parse the binary expressions (logical-or-expression). */
10066 expression
= cp_parser_binary_expression (parser
, false, false, false,
10067 PREC_NOT_OPERATOR
, NULL
);
10068 /* If the next token is a `?' then we're actually looking at
10069 a conditional-expression; otherwise we're done. */
10070 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10071 expression
= cp_parser_question_colon_clause (parser
, expression
);
10074 expression
= cp_parser_assignment_expression (parser
);
10075 /* Restore the old settings. */
10076 parser
->integral_constant_expression_p
10077 = saved_integral_constant_expression_p
;
10078 parser
->allow_non_integral_constant_expression_p
10079 = saved_allow_non_integral_constant_expression_p
;
10080 if (cxx_dialect
>= cxx11
)
10082 /* Require an rvalue constant expression here; that's what our
10083 callers expect. Reference constant expressions are handled
10084 separately in e.g. cp_parser_template_argument. */
10085 tree decay
= expression
;
10086 if (TREE_TYPE (expression
)
10087 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10088 decay
= build_address (expression
);
10089 bool is_const
= potential_rvalue_constant_expression (decay
);
10090 parser
->non_integral_constant_expression_p
= !is_const
;
10091 if (!is_const
&& !allow_non_constant_p
)
10092 require_potential_rvalue_constant_expression (decay
);
10094 if (allow_non_constant_p
)
10095 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10096 parser
->non_integral_constant_expression_p
10097 = saved_non_integral_constant_expression_p
;
10102 /* Parse __builtin_offsetof.
10104 offsetof-expression:
10105 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10107 offsetof-member-designator:
10109 | offsetof-member-designator "." id-expression
10110 | offsetof-member-designator "[" expression "]"
10111 | offsetof-member-designator "->" id-expression */
10114 cp_parser_builtin_offsetof (cp_parser
*parser
)
10116 int save_ice_p
, save_non_ice_p
;
10121 location_t finish_loc
;
10123 /* We're about to accept non-integral-constant things, but will
10124 definitely yield an integral constant expression. Save and
10125 restore these values around our local parsing. */
10126 save_ice_p
= parser
->integral_constant_expression_p
;
10127 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10129 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10131 /* Consume the "__builtin_offsetof" token. */
10132 cp_lexer_consume_token (parser
->lexer
);
10133 /* Consume the opening `('. */
10134 matching_parens parens
;
10135 parens
.require_open (parser
);
10136 /* Parse the type-id. */
10137 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10139 const char *saved_message
= parser
->type_definition_forbidden_message
;
10140 parser
->type_definition_forbidden_message
10141 = G_("types may not be defined within %<__builtin_offsetof%>");
10142 type
= cp_parser_type_id (parser
);
10143 parser
->type_definition_forbidden_message
= saved_message
;
10145 /* Look for the `,'. */
10146 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10147 token
= cp_lexer_peek_token (parser
->lexer
);
10149 /* Build the (type *)null that begins the traditional offsetof macro. */
10151 = build_static_cast (build_pointer_type (type
), null_pointer_node
,
10152 tf_warning_or_error
);
10154 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10155 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10156 true, &dummy
, token
->location
);
10159 token
= cp_lexer_peek_token (parser
->lexer
);
10160 switch (token
->type
)
10162 case CPP_OPEN_SQUARE
:
10163 /* offsetof-member-designator "[" expression "]" */
10164 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10169 /* offsetof-member-designator "->" identifier */
10170 expr
= grok_array_decl (token
->location
, expr
,
10171 integer_zero_node
, false);
10175 /* offsetof-member-designator "." identifier */
10176 cp_lexer_consume_token (parser
->lexer
);
10177 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10178 expr
, true, &dummy
,
10182 case CPP_CLOSE_PAREN
:
10183 /* Consume the ")" token. */
10184 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10185 cp_lexer_consume_token (parser
->lexer
);
10189 /* Error. We know the following require will fail, but
10190 that gives the proper error message. */
10191 parens
.require_close (parser
);
10192 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10193 expr
= error_mark_node
;
10199 /* Make a location of the form:
10200 __builtin_offsetof (struct s, f)
10201 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10202 with caret at the type-id, ranging from the start of the
10203 "_builtin_offsetof" token to the close paren. */
10204 loc
= make_location (loc
, start_loc
, finish_loc
);
10205 /* The result will be an INTEGER_CST, so we need to explicitly
10206 preserve the location. */
10207 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10210 parser
->integral_constant_expression_p
= save_ice_p
;
10211 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10213 expr
= expr
.maybe_add_location_wrapper ();
10217 /* Parse a trait expression.
10219 Returns a representation of the expression, the underlying type
10220 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10223 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10225 cp_trait_kind kind
;
10226 tree type1
, type2
= NULL_TREE
;
10227 bool binary
= false;
10228 bool variadic
= false;
10232 case RID_HAS_NOTHROW_ASSIGN
:
10233 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10235 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10236 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10238 case RID_HAS_NOTHROW_COPY
:
10239 kind
= CPTK_HAS_NOTHROW_COPY
;
10241 case RID_HAS_TRIVIAL_ASSIGN
:
10242 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10244 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10245 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10247 case RID_HAS_TRIVIAL_COPY
:
10248 kind
= CPTK_HAS_TRIVIAL_COPY
;
10250 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10251 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10253 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10254 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10256 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10257 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10259 case RID_IS_ABSTRACT
:
10260 kind
= CPTK_IS_ABSTRACT
;
10262 case RID_IS_AGGREGATE
:
10263 kind
= CPTK_IS_AGGREGATE
;
10265 case RID_IS_BASE_OF
:
10266 kind
= CPTK_IS_BASE_OF
;
10270 kind
= CPTK_IS_CLASS
;
10273 kind
= CPTK_IS_EMPTY
;
10276 kind
= CPTK_IS_ENUM
;
10279 kind
= CPTK_IS_FINAL
;
10281 case RID_IS_LITERAL_TYPE
:
10282 kind
= CPTK_IS_LITERAL_TYPE
;
10285 kind
= CPTK_IS_POD
;
10287 case RID_IS_POLYMORPHIC
:
10288 kind
= CPTK_IS_POLYMORPHIC
;
10290 case RID_IS_SAME_AS
:
10291 kind
= CPTK_IS_SAME_AS
;
10294 case RID_IS_STD_LAYOUT
:
10295 kind
= CPTK_IS_STD_LAYOUT
;
10297 case RID_IS_TRIVIAL
:
10298 kind
= CPTK_IS_TRIVIAL
;
10300 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10301 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10304 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10305 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10308 case RID_IS_TRIVIALLY_COPYABLE
:
10309 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10312 kind
= CPTK_IS_UNION
;
10314 case RID_UNDERLYING_TYPE
:
10315 kind
= CPTK_UNDERLYING_TYPE
;
10320 case RID_DIRECT_BASES
:
10321 kind
= CPTK_DIRECT_BASES
;
10323 case RID_IS_ASSIGNABLE
:
10324 kind
= CPTK_IS_ASSIGNABLE
;
10327 case RID_IS_CONSTRUCTIBLE
:
10328 kind
= CPTK_IS_CONSTRUCTIBLE
;
10332 gcc_unreachable ();
10335 /* Get location of initial token. */
10336 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10338 /* Consume the token. */
10339 cp_lexer_consume_token (parser
->lexer
);
10341 matching_parens parens
;
10342 parens
.require_open (parser
);
10345 type_id_in_expr_sentinel
s (parser
);
10346 type1
= cp_parser_type_id (parser
);
10349 if (type1
== error_mark_node
)
10350 return error_mark_node
;
10354 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10357 type_id_in_expr_sentinel
s (parser
);
10358 type2
= cp_parser_type_id (parser
);
10361 if (type2
== error_mark_node
)
10362 return error_mark_node
;
10366 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10368 cp_lexer_consume_token (parser
->lexer
);
10369 tree elt
= cp_parser_type_id (parser
);
10370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10372 cp_lexer_consume_token (parser
->lexer
);
10373 elt
= make_pack_expansion (elt
);
10375 if (elt
== error_mark_node
)
10376 return error_mark_node
;
10377 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10381 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10382 parens
.require_close (parser
);
10384 /* Construct a location of the form:
10385 __is_trivially_copyable(_Tp)
10386 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10387 with start == caret, finishing at the close-paren. */
10388 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10390 /* Complete the trait expression, which may mean either processing
10391 the trait expr now or saving it for template instantiation. */
10394 case CPTK_UNDERLYING_TYPE
:
10395 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10397 return cp_expr (finish_bases (type1
, false), trait_loc
);
10398 case CPTK_DIRECT_BASES
:
10399 return cp_expr (finish_bases (type1
, true), trait_loc
);
10401 return finish_trait_expr (trait_loc
, kind
, type1
, type2
);
10405 /* Parse a lambda expression.
10408 lambda-introducer lambda-declarator [opt] compound-statement
10410 Returns a representation of the expression. */
10413 cp_parser_lambda_expression (cp_parser
* parser
)
10415 tree lambda_expr
= build_lambda_expr ();
10418 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10419 cp_token_position start
= 0;
10421 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10423 if (cxx_dialect
>= cxx2a
)
10424 /* C++20 allows lambdas in unevaluated context. */;
10425 else if (cp_unevaluated_operand
)
10427 if (!token
->error_reported
)
10429 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10430 "lambda-expression in unevaluated context"
10431 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10432 token
->error_reported
= true;
10436 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10438 if (!token
->error_reported
)
10440 error_at (token
->location
, "lambda-expression in template-argument"
10441 " only available with %<-std=c++2a%> or %<-std=gnu++2a%>");
10442 token
->error_reported
= true;
10447 /* We may be in the middle of deferred access check. Disable
10449 push_deferring_access_checks (dk_no_deferred
);
10451 cp_parser_lambda_introducer (parser
, lambda_expr
);
10452 if (cp_parser_error_occurred (parser
))
10453 return error_mark_node
;
10455 type
= begin_lambda_type (lambda_expr
);
10456 if (type
== error_mark_node
)
10457 return error_mark_node
;
10459 record_lambda_scope (lambda_expr
);
10461 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10462 determine_visibility (TYPE_NAME (type
));
10464 /* Now that we've started the type, add the capture fields for any
10465 explicit captures. */
10466 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10469 /* Inside the class, surrounding template-parameter-lists do not apply. */
10470 unsigned int saved_num_template_parameter_lists
10471 = parser
->num_template_parameter_lists
;
10472 unsigned char in_statement
= parser
->in_statement
;
10473 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10474 bool fully_implicit_function_template_p
10475 = parser
->fully_implicit_function_template_p
;
10476 tree implicit_template_parms
= parser
->implicit_template_parms
;
10477 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10478 bool auto_is_implicit_function_template_parm_p
10479 = parser
->auto_is_implicit_function_template_parm_p
;
10481 parser
->num_template_parameter_lists
= 0;
10482 parser
->in_statement
= 0;
10483 parser
->in_switch_statement_p
= false;
10484 parser
->fully_implicit_function_template_p
= false;
10485 parser
->implicit_template_parms
= 0;
10486 parser
->implicit_template_scope
= 0;
10487 parser
->auto_is_implicit_function_template_parm_p
= false;
10489 /* By virtue of defining a local class, a lambda expression has access to
10490 the private variables of enclosing classes. */
10492 if (cp_parser_start_tentative_firewall (parser
))
10495 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10497 if (ok
&& cp_parser_error_occurred (parser
))
10502 cp_parser_lambda_body (parser
, lambda_expr
);
10504 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10506 if (cp_parser_skip_to_closing_brace (parser
))
10507 cp_lexer_consume_token (parser
->lexer
);
10510 /* The capture list was built up in reverse order; fix that now. */
10511 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10512 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10515 maybe_add_lambda_conv_op (type
);
10517 finish_struct (type
, /*attributes=*/NULL_TREE
);
10519 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10520 parser
->in_statement
= in_statement
;
10521 parser
->in_switch_statement_p
= in_switch_statement_p
;
10522 parser
->fully_implicit_function_template_p
10523 = fully_implicit_function_template_p
;
10524 parser
->implicit_template_parms
= implicit_template_parms
;
10525 parser
->implicit_template_scope
= implicit_template_scope
;
10526 parser
->auto_is_implicit_function_template_parm_p
10527 = auto_is_implicit_function_template_parm_p
;
10530 /* This field is only used during parsing of the lambda. */
10531 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10533 /* This lambda shouldn't have any proxies left at this point. */
10534 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10535 /* And now that we're done, push proxies for an enclosing lambda. */
10536 insert_pending_capture_proxies ();
10538 /* Update the lambda expression to a range. */
10539 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10544 lambda_expr
= build_lambda_object (lambda_expr
);
10546 lambda_expr
= error_mark_node
;
10548 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10550 pop_deferring_access_checks ();
10552 return lambda_expr
;
10555 /* Parse the beginning of a lambda expression.
10558 [ lambda-capture [opt] ]
10560 LAMBDA_EXPR is the current representation of the lambda expression. */
10563 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10565 /* Need commas after the first capture. */
10568 /* Eat the leading `['. */
10569 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10571 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10572 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10573 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)
10574 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10575 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10576 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10577 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10579 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10581 cp_lexer_consume_token (parser
->lexer
);
10584 if (!(at_function_scope_p () || parsing_nsdmi ()))
10585 error ("non-local lambda expression cannot have a capture-default");
10588 hash_set
<tree
, true> ids
;
10589 tree first_capture_id
= NULL_TREE
;
10590 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10592 cp_token
* capture_token
;
10594 tree capture_init_expr
;
10595 cp_id_kind idk
= CP_ID_KIND_NONE
;
10596 bool explicit_init_p
= false;
10598 enum capture_kind_type
10603 enum capture_kind_type capture_kind
= BY_COPY
;
10605 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10607 error ("expected end of capture-list");
10614 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10616 /* Possibly capture `this'. */
10617 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10619 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10620 if (cxx_dialect
< cxx2a
10621 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10622 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10623 "with by-copy capture default");
10624 cp_lexer_consume_token (parser
->lexer
);
10625 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10626 pedwarn (input_location
, 0,
10627 "already captured %qD in lambda expression",
10630 add_capture (lambda_expr
, /*id=*/this_identifier
,
10631 /*initializer=*/finish_this_expr (),
10632 /*by_reference_p=*/true, explicit_init_p
);
10636 /* Possibly capture `*this'. */
10637 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
10638 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10640 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10641 if (cxx_dialect
< cxx17
)
10642 pedwarn (loc
, 0, "%<*this%> capture only available with "
10643 "%<-std=c++17%> or %<-std=gnu++17%>");
10644 cp_lexer_consume_token (parser
->lexer
);
10645 cp_lexer_consume_token (parser
->lexer
);
10646 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10647 pedwarn (input_location
, 0,
10648 "already captured %qD in lambda expression",
10651 add_capture (lambda_expr
, /*id=*/this_identifier
,
10652 /*initializer=*/finish_this_expr (),
10653 /*by_reference_p=*/false, explicit_init_p
);
10657 /* But reject `&this'. */
10658 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10659 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10661 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
10662 "%<this%> cannot be captured by reference");
10663 cp_lexer_consume_token (parser
->lexer
);
10664 cp_lexer_consume_token (parser
->lexer
);
10668 bool init_pack_expansion
= false;
10669 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
10670 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10672 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10673 if (cxx_dialect
< cxx2a
)
10674 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
10675 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10676 cp_lexer_consume_token (parser
->lexer
);
10677 init_pack_expansion
= true;
10680 /* Remember whether we want to capture as a reference or not. */
10681 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
10683 capture_kind
= BY_REFERENCE
;
10684 cp_lexer_consume_token (parser
->lexer
);
10687 /* Get the identifier. */
10688 capture_token
= cp_lexer_peek_token (parser
->lexer
);
10689 capture_id
= cp_parser_identifier (parser
);
10691 if (capture_id
== error_mark_node
)
10692 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10693 delimiters, but I modified this to stop on unnested ']' as well. It
10694 was already changed to stop on unnested '}', so the
10695 "closing_parenthesis" name is no more misleading with my change. */
10697 cp_parser_skip_to_closing_parenthesis (parser
,
10698 /*recovering=*/true,
10700 /*consume_paren=*/true);
10704 /* Find the initializer for this capture. */
10705 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10706 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10707 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10709 bool direct
, non_constant
;
10710 /* An explicit initializer exists. */
10711 if (cxx_dialect
< cxx14
)
10712 pedwarn (input_location
, 0,
10713 "lambda capture initializers "
10714 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
10715 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
10716 &non_constant
, true);
10717 explicit_init_p
= true;
10718 if (capture_init_expr
== NULL_TREE
)
10720 error ("empty initializer for lambda init-capture");
10721 capture_init_expr
= error_mark_node
;
10723 if (init_pack_expansion
)
10724 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10728 const char* error_msg
;
10730 /* Turn the identifier into an id-expression. */
10732 = cp_parser_lookup_name_simple (parser
, capture_id
,
10733 capture_token
->location
);
10735 if (capture_init_expr
== error_mark_node
)
10737 unqualified_name_lookup_error (capture_id
);
10740 else if (!VAR_P (capture_init_expr
)
10741 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
10743 error_at (capture_token
->location
,
10744 "capture of non-variable %qE",
10745 capture_init_expr
);
10746 if (DECL_P (capture_init_expr
))
10747 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10748 "%q#D declared here", capture_init_expr
);
10751 if (VAR_P (capture_init_expr
)
10752 && decl_storage_duration (capture_init_expr
) != dk_auto
)
10754 if (pedwarn (capture_token
->location
, 0, "capture of variable "
10755 "%qD with non-automatic storage duration",
10756 capture_init_expr
))
10757 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
10758 "%q#D declared here", capture_init_expr
);
10763 = finish_id_expression
10768 /*integral_constant_expression_p=*/false,
10769 /*allow_non_integral_constant_expression_p=*/false,
10770 /*non_integral_constant_expression_p=*/NULL
,
10771 /*template_p=*/false,
10773 /*address_p=*/false,
10774 /*template_arg_p=*/false,
10776 capture_token
->location
);
10778 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10780 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10781 cp_lexer_consume_token (parser
->lexer
);
10782 capture_init_expr
= make_pack_expansion (capture_init_expr
);
10783 if (init_pack_expansion
)
10785 /* If what follows is an initializer, the second '...' is
10786 invalid. But for cases like [...xs...], the first one
10788 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
10789 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
10790 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
10791 ellipsis_loc
= loc
;
10792 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
10798 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
10799 && !explicit_init_p
)
10801 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
10802 && capture_kind
== BY_COPY
)
10803 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
10804 "of %qD redundant with by-copy capture default",
10806 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
10807 && capture_kind
== BY_REFERENCE
)
10808 pedwarn (capture_token
->location
, 0, "explicit by-reference "
10809 "capture of %qD redundant with by-reference capture "
10810 "default", capture_id
);
10813 /* Check for duplicates.
10814 Optimize for the zero or one explicit captures cases and only create
10815 the hash_set after adding second capture. */
10816 bool found
= false;
10817 if (!ids
.is_empty ())
10818 found
= ids
.add (capture_id
);
10819 else if (first_capture_id
== NULL_TREE
)
10820 first_capture_id
= capture_id
;
10821 else if (capture_id
== first_capture_id
)
10825 ids
.add (first_capture_id
);
10826 ids
.add (capture_id
);
10829 pedwarn (input_location
, 0,
10830 "already captured %qD in lambda expression", capture_id
);
10832 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
10833 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
10836 /* If there is any qualification still in effect, clear it
10837 now; we will be starting fresh with the next capture. */
10838 parser
->scope
= NULL_TREE
;
10839 parser
->qualifying_scope
= NULL_TREE
;
10840 parser
->object_scope
= NULL_TREE
;
10843 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
10846 /* Parse the (optional) middle of a lambda expression.
10849 < template-parameter-list [opt] >
10850 requires-clause [opt]
10851 ( parameter-declaration-clause [opt] )
10852 attribute-specifier [opt]
10853 decl-specifier-seq [opt]
10854 exception-specification [opt]
10855 lambda-return-type-clause [opt]
10856 requires-clause [opt]
10858 LAMBDA_EXPR is the current representation of the lambda expression. */
10861 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
10863 /* 5.1.1.4 of the standard says:
10864 If a lambda-expression does not include a lambda-declarator, it is as if
10865 the lambda-declarator were ().
10866 This means an empty parameter list, no attributes, and no exception
10868 tree param_list
= void_list_node
;
10869 tree std_attrs
= NULL_TREE
;
10870 tree gnu_attrs
= NULL_TREE
;
10871 tree exception_spec
= NULL_TREE
;
10872 tree template_param_list
= NULL_TREE
;
10873 tree tx_qual
= NULL_TREE
;
10874 tree return_type
= NULL_TREE
;
10875 tree trailing_requires_clause
= NULL_TREE
;
10876 cp_decl_specifier_seq lambda_specs
;
10877 clear_decl_specs (&lambda_specs
);
10879 /* The template-parameter-list is optional, but must begin with
10880 an opening angle if present. */
10881 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
10883 if (cxx_dialect
< cxx14
)
10884 pedwarn (parser
->lexer
->next_token
->location
, 0,
10885 "lambda templates are only available with "
10886 "%<-std=c++14%> or %<-std=gnu++14%>");
10887 else if (cxx_dialect
< cxx2a
)
10888 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
10889 "lambda templates are only available with "
10890 "%<-std=c++2a%> or %<-std=gnu++2a%>");
10892 cp_lexer_consume_token (parser
->lexer
);
10894 template_param_list
= cp_parser_template_parameter_list (parser
);
10895 cp_parser_skip_to_end_of_template_parameter_list (parser
);
10897 /* We may have a constrained generic lambda; parse the requires-clause
10898 immediately after the template-parameter-list and combine with any
10899 shorthand constraints present. */
10900 tree dreqs
= cp_parser_requires_clause_opt (parser
, true);
10903 tree reqs
= get_shorthand_constraints (current_template_parms
);
10905 reqs
= combine_constraint_expressions (reqs
, dreqs
);
10906 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
10909 /* We just processed one more parameter list. */
10910 ++parser
->num_template_parameter_lists
;
10913 /* The parameter-declaration-clause is optional (unless
10914 template-parameter-list was given), but must begin with an
10915 opening parenthesis if present. */
10916 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
10918 bool is_consteval
= false;
10919 /* For C++20, before parsing the parameter list check if there is
10920 a consteval specifier in the corresponding decl-specifier-seq. */
10921 if (cxx_dialect
>= cxx2a
)
10923 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
10924 cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_KEYWORD
); n
++)
10926 if (cp_lexer_peek_nth_token (parser
->lexer
, n
)->keyword
10929 is_consteval
= true;
10935 matching_parens parens
;
10936 parens
.consume_open (parser
);
10938 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
10941 current_binding_level
->immediate_fn_ctx_p
= true;
10943 /* Parse parameters. */
10945 = cp_parser_parameter_declaration_clause
10946 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
10948 /* Default arguments shall not be specified in the
10949 parameter-declaration-clause of a lambda-declarator. */
10950 if (cxx_dialect
< cxx14
)
10951 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
10952 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
10953 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
10954 "default argument specified for lambda parameter");
10956 parens
.require_close (parser
);
10958 /* In the decl-specifier-seq of the lambda-declarator, each
10959 decl-specifier shall either be mutable or constexpr. */
10960 int declares_class_or_enum
;
10961 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
10962 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
10963 cp_parser_decl_specifier_seq (parser
,
10964 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
10965 &lambda_specs
, &declares_class_or_enum
);
10966 if (lambda_specs
.storage_class
== sc_mutable
)
10968 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
10969 if (lambda_specs
.conflicting_specifiers_p
)
10970 error_at (lambda_specs
.locations
[ds_storage_class
],
10971 "duplicate %<mutable%>");
10974 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
10976 /* Parse optional exception specification. */
10978 = cp_parser_exception_specification_opt (parser
, CP_PARSER_FLAGS_NONE
);
10980 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
10982 /* Parse optional trailing return type. */
10983 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
10985 cp_lexer_consume_token (parser
->lexer
);
10986 return_type
= cp_parser_trailing_type_id (parser
);
10989 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
10990 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
10992 /* Parse optional trailing requires clause. */
10993 trailing_requires_clause
= cp_parser_requires_clause_opt (parser
, false);
10995 /* The function parameters must be in scope all the way until after the
10996 trailing-return-type in case of decltype. */
10997 pop_bindings_and_leave_scope ();
10999 else if (template_param_list
!= NULL_TREE
) // generate diagnostic
11000 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
11002 /* Create the function call operator.
11004 Messing with declarators like this is no uglier than building up the
11005 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11008 cp_decl_specifier_seq return_type_specs
;
11009 cp_declarator
* declarator
;
11014 clear_decl_specs (&return_type_specs
);
11015 return_type_specs
.type
= make_auto ();
11017 if (lambda_specs
.locations
[ds_constexpr
])
11019 if (cxx_dialect
>= cxx17
)
11020 return_type_specs
.locations
[ds_constexpr
]
11021 = lambda_specs
.locations
[ds_constexpr
];
11023 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
11024 "lambda only available with %<-std=c++17%> or "
11025 "%<-std=gnu++17%>");
11027 if (lambda_specs
.locations
[ds_consteval
])
11028 return_type_specs
.locations
[ds_consteval
]
11029 = lambda_specs
.locations
[ds_consteval
];
11031 p
= obstack_alloc (&declarator_obstack
, 0);
11033 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
11034 LAMBDA_EXPR_LOCATION (lambda_expr
));
11036 quals
= (LAMBDA_EXPR_MUTABLE_P (lambda_expr
)
11037 ? TYPE_UNQUALIFIED
: TYPE_QUAL_CONST
);
11038 declarator
= make_call_declarator (declarator
, param_list
, quals
,
11039 VIRT_SPEC_UNSPECIFIED
,
11044 trailing_requires_clause
);
11045 declarator
->std_attributes
= std_attrs
;
11047 fco
= grokmethod (&return_type_specs
,
11050 if (fco
!= error_mark_node
)
11052 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
11053 DECL_ARTIFICIAL (fco
) = 1;
11054 /* Give the object parameter a different name. */
11055 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
11056 DECL_SET_LAMBDA_FUNCTION (fco
, true);
11058 if (template_param_list
)
11060 fco
= finish_member_template_decl (fco
);
11061 finish_template_decl (template_param_list
);
11062 --parser
->num_template_parameter_lists
;
11064 else if (parser
->fully_implicit_function_template_p
)
11065 fco
= finish_fully_implicit_template (parser
, fco
);
11067 finish_member_declaration (fco
);
11069 obstack_free (&declarator_obstack
, p
);
11071 return (fco
!= error_mark_node
);
11075 /* Parse the body of a lambda expression, which is simply
11079 but which requires special handling.
11080 LAMBDA_EXPR is the current representation of the lambda expression. */
11083 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
11085 bool nested
= (current_function_decl
!= NULL_TREE
);
11086 unsigned char local_variables_forbidden_p
11087 = parser
->local_variables_forbidden_p
;
11088 bool in_function_body
= parser
->in_function_body
;
11090 /* The body of a lambda-expression is not a subexpression of the enclosing
11095 push_function_context ();
11097 /* Still increment function_depth so that we don't GC in the
11098 middle of an expression. */
11101 vec
<tree
> omp_privatization_save
;
11102 save_omp_privatization_clauses (omp_privatization_save
);
11103 /* Clear this in case we're in the middle of a default argument. */
11104 parser
->local_variables_forbidden_p
= 0;
11105 parser
->in_function_body
= true;
11108 local_specialization_stack
s (lss_copy
);
11109 tree fco
= lambda_function (lambda_expr
);
11110 tree body
= start_lambda_function (fco
, lambda_expr
);
11111 matching_braces braces
;
11113 if (braces
.require_open (parser
))
11115 tree compound_stmt
= begin_compound_stmt (0);
11117 /* Originally C++11 required us to peek for 'return expr'; and
11118 process it specially here to deduce the return type. N3638
11119 removed the need for that. */
11121 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11122 cp_parser_label_declaration (parser
);
11123 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
11124 braces
.require_close (parser
);
11126 finish_compound_stmt (compound_stmt
);
11129 finish_lambda_function (body
);
11132 restore_omp_privatization_clauses (omp_privatization_save
);
11133 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11134 parser
->in_function_body
= in_function_body
;
11136 pop_function_context();
11141 /* Statements [gram.stmt.stmt] */
11143 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11146 add_debug_begin_stmt (location_t loc
)
11148 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11150 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11151 /* A concept is never expanded normally. */
11154 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11155 SET_EXPR_LOCATION (stmt
, loc
);
11159 /* Parse a statement.
11163 expression-statement
11165 selection-statement
11166 iteration-statement
11168 declaration-statement
11175 attribute-specifier-seq (opt) expression-statement
11176 attribute-specifier-seq (opt) compound-statement
11177 attribute-specifier-seq (opt) selection-statement
11178 attribute-specifier-seq (opt) iteration-statement
11179 attribute-specifier-seq (opt) jump-statement
11180 declaration-statement
11181 attribute-specifier-seq (opt) try-block
11184 expression-statement
11192 IN_COMPOUND is true when the statement is nested inside a
11193 cp_parser_compound_statement; this matters for certain pragmas.
11195 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11196 is a (possibly labeled) if statement which is not enclosed in braces
11197 and has an else clause. This is used to implement -Wparentheses.
11199 CHAIN is a vector of if-else-if conditions. */
11202 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11203 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11204 location_t
*loc_after_labels
)
11206 tree statement
, std_attrs
= NULL_TREE
;
11208 location_t statement_location
, attrs_loc
;
11213 /* There is no statement yet. */
11214 statement
= NULL_TREE
;
11216 saved_token_sentinel
saved_tokens (parser
->lexer
);
11217 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11218 if (c_dialect_objc ())
11219 /* In obj-c++, seeing '[[' might be the either the beginning of
11220 c++11 attributes, or a nested objc-message-expression. So
11221 let's parse the c++11 attributes tentatively. */
11222 cp_parser_parse_tentatively (parser
);
11223 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11225 attrs_loc
= make_location (attrs_loc
, attrs_loc
, parser
->lexer
);
11226 if (c_dialect_objc ())
11228 if (!cp_parser_parse_definitely (parser
))
11229 std_attrs
= NULL_TREE
;
11232 /* Peek at the next token. */
11233 token
= cp_lexer_peek_token (parser
->lexer
);
11234 /* Remember the location of the first token in the statement. */
11235 cp_token
*statement_token
= token
;
11236 statement_location
= token
->location
;
11237 add_debug_begin_stmt (statement_location
);
11238 /* If this is a keyword, then that will often determine what kind of
11239 statement we have. */
11240 if (token
->type
== CPP_KEYWORD
)
11242 enum rid keyword
= token
->keyword
;
11248 /* Looks like a labeled-statement with a case label.
11249 Parse the label, and then use tail recursion to parse
11251 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11252 in_compound
= false;
11257 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11258 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11264 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11265 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11272 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11273 statement
= cp_parser_jump_statement (parser
);
11276 /* Objective-C++ exception-handling constructs. */
11279 case RID_AT_FINALLY
:
11280 case RID_AT_SYNCHRONIZED
:
11282 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11283 statement
= cp_parser_objc_statement (parser
);
11287 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11288 statement
= cp_parser_try_block (parser
);
11291 case RID_NAMESPACE
:
11292 /* This must be a namespace alias definition. */
11293 if (std_attrs
!= NULL_TREE
)
11295 /* Attributes should be parsed as part of the the
11296 declaration, so let's un-parse them. */
11297 saved_tokens
.rollback();
11298 std_attrs
= NULL_TREE
;
11300 cp_parser_declaration_statement (parser
);
11303 case RID_TRANSACTION_ATOMIC
:
11304 case RID_TRANSACTION_RELAXED
:
11305 case RID_SYNCHRONIZED
:
11306 case RID_ATOMIC_NOEXCEPT
:
11307 case RID_ATOMIC_CANCEL
:
11308 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11309 statement
= cp_parser_transaction (parser
, token
);
11311 case RID_TRANSACTION_CANCEL
:
11312 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11313 statement
= cp_parser_transaction_cancel (parser
);
11317 /* It might be a keyword like `int' that can start a
11318 declaration-statement. */
11322 else if (token
->type
== CPP_NAME
)
11324 /* If the next token is a `:', then we are looking at a
11325 labeled-statement. */
11326 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11327 if (token
->type
== CPP_COLON
)
11329 /* Looks like a labeled-statement with an ordinary label.
11330 Parse the label, and then use tail recursion to parse
11333 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11334 in_compound
= false;
11338 /* Anything that starts with a `{' must be a compound-statement. */
11339 else if (token
->type
== CPP_OPEN_BRACE
)
11341 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11342 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11344 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11345 a statement all its own. */
11346 else if (token
->type
== CPP_PRAGMA
)
11348 /* Only certain OpenMP pragmas are attached to statements, and thus
11349 are considered statements themselves. All others are not. In
11350 the context of a compound, accept the pragma as a "statement" and
11351 return so that we can check for a close brace. Otherwise we
11352 require a real statement and must go back and read one. */
11354 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11355 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11359 else if (token
->type
== CPP_EOF
)
11361 cp_parser_error (parser
, "expected statement");
11365 /* Everything else must be a declaration-statement or an
11366 expression-statement. Try for the declaration-statement
11367 first, unless we are looking at a `;', in which case we know that
11368 we have an expression-statement. */
11371 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11373 if (std_attrs
!= NULL_TREE
)
11374 /* Attributes should be parsed as part of the declaration,
11375 so let's un-parse them. */
11376 saved_tokens
.rollback();
11378 cp_parser_parse_tentatively (parser
);
11379 /* Try to parse the declaration-statement. */
11380 cp_parser_declaration_statement (parser
);
11381 /* If that worked, we're done. */
11382 if (cp_parser_parse_definitely (parser
))
11384 /* It didn't work, restore the post-attribute position. */
11386 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11388 /* All preceding labels have been parsed at this point. */
11389 if (loc_after_labels
!= NULL
)
11390 *loc_after_labels
= statement_location
;
11392 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11394 /* Look for an expression-statement instead. */
11395 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11397 /* Handle [[fallthrough]];. */
11398 if (attribute_fallthrough_p (std_attrs
))
11400 /* The next token after the fallthrough attribute is ';'. */
11401 if (statement
== NULL_TREE
)
11403 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11404 statement
= build_call_expr_internal_loc (statement_location
,
11406 void_type_node
, 0);
11407 finish_expr_stmt (statement
);
11410 warning_at (statement_location
, OPT_Wattributes
,
11411 "%<fallthrough%> attribute not followed by %<;%>");
11412 std_attrs
= NULL_TREE
;
11416 /* Set the line number for the statement. */
11417 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11418 SET_EXPR_LOCATION (statement
, statement_location
);
11420 /* Allow "[[fallthrough]];", but warn otherwise. */
11421 if (std_attrs
!= NULL_TREE
)
11422 warning_at (attrs_loc
,
11424 "attributes at the beginning of statement are ignored");
11427 /* Append ATTR to attribute list ATTRS. */
11430 attr_chainon (tree attrs
, tree attr
)
11432 if (attrs
== error_mark_node
)
11433 return error_mark_node
;
11434 if (attr
== error_mark_node
)
11435 return error_mark_node
;
11436 return chainon (attrs
, attr
);
11439 /* Parse the label for a labeled-statement, i.e.
11442 case constant-expression :
11446 case constant-expression ... constant-expression : statement
11448 When a label is parsed without errors, the label is added to the
11449 parse tree by the finish_* functions, so this function doesn't
11450 have to return the label. */
11453 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11456 tree label
= NULL_TREE
;
11457 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11459 /* The next token should be an identifier. */
11460 token
= cp_lexer_peek_token (parser
->lexer
);
11461 if (token
->type
!= CPP_NAME
11462 && token
->type
!= CPP_KEYWORD
)
11464 cp_parser_error (parser
, "expected labeled-statement");
11468 /* Remember whether this case or a user-defined label is allowed to fall
11470 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11472 parser
->colon_corrects_to_scope_p
= false;
11473 switch (token
->keyword
)
11477 tree expr
, expr_hi
;
11478 cp_token
*ellipsis
;
11480 /* Consume the `case' token. */
11481 cp_lexer_consume_token (parser
->lexer
);
11482 /* Parse the constant-expression. */
11483 expr
= cp_parser_constant_expression (parser
);
11484 if (check_for_bare_parameter_packs (expr
))
11485 expr
= error_mark_node
;
11487 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11488 if (ellipsis
->type
== CPP_ELLIPSIS
)
11490 /* Consume the `...' token. */
11491 cp_lexer_consume_token (parser
->lexer
);
11492 expr_hi
= cp_parser_constant_expression (parser
);
11493 if (check_for_bare_parameter_packs (expr_hi
))
11494 expr_hi
= error_mark_node
;
11496 /* We don't need to emit warnings here, as the common code
11497 will do this for us. */
11500 expr_hi
= NULL_TREE
;
11502 if (parser
->in_switch_statement_p
)
11504 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11505 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11507 label
= CASE_LABEL (l
);
11508 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11512 error_at (token
->location
,
11513 "case label %qE not within a switch statement",
11519 /* Consume the `default' token. */
11520 cp_lexer_consume_token (parser
->lexer
);
11522 if (parser
->in_switch_statement_p
)
11524 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11525 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11527 label
= CASE_LABEL (l
);
11528 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11532 error_at (token
->location
, "case label not within a switch statement");
11536 /* Anything else must be an ordinary label. */
11537 label
= finish_label_stmt (cp_parser_identifier (parser
));
11538 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11539 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11543 /* Require the `:' token. */
11544 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11546 /* An ordinary label may optionally be followed by attributes.
11547 However, this is only permitted if the attributes are then
11548 followed by a semicolon. This is because, for backward
11549 compatibility, when parsing
11550 lab: __attribute__ ((unused)) int i;
11551 we want the attribute to attach to "i", not "lab". */
11552 if (label
!= NULL_TREE
11553 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11556 cp_parser_parse_tentatively (parser
);
11557 attrs
= cp_parser_gnu_attributes_opt (parser
);
11558 if (attrs
== NULL_TREE
11559 /* And fallthrough always binds to the expression-statement. */
11560 || attribute_fallthrough_p (attrs
)
11561 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11562 cp_parser_abort_tentative_parse (parser
);
11563 else if (!cp_parser_parse_definitely (parser
))
11566 attributes
= attr_chainon (attributes
, attrs
);
11569 if (attributes
!= NULL_TREE
)
11570 cplus_decl_attributes (&label
, attributes
, 0);
11572 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11575 /* Parse an expression-statement.
11577 expression-statement:
11580 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11581 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11582 indicates whether this expression-statement is part of an
11583 expression statement. */
11586 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11588 tree statement
= NULL_TREE
;
11589 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11590 location_t loc
= token
->location
;
11592 /* There might be attribute fallthrough. */
11593 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11595 /* If the next token is a ';', then there is no expression
11597 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11599 statement
= cp_parser_expression (parser
);
11600 if (statement
== error_mark_node
11601 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11603 cp_parser_skip_to_end_of_block_or_statement (parser
);
11604 return error_mark_node
;
11608 /* Handle [[fallthrough]];. */
11609 if (attribute_fallthrough_p (attr
))
11611 /* The next token after the fallthrough attribute is ';'. */
11612 if (statement
== NULL_TREE
)
11613 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11614 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11615 void_type_node
, 0);
11617 warning_at (loc
, OPT_Wattributes
,
11618 "%<fallthrough%> attribute not followed by %<;%>");
11622 /* Allow "[[fallthrough]];", but warn otherwise. */
11623 if (attr
!= NULL_TREE
)
11624 warning_at (loc
, OPT_Wattributes
,
11625 "attributes at the beginning of statement are ignored");
11627 /* Give a helpful message for "A<T>::type t;" and the like. */
11628 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11629 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11631 if (TREE_CODE (statement
) == SCOPE_REF
)
11632 error_at (token
->location
, "need %<typename%> before %qE because "
11633 "%qT is a dependent scope",
11634 statement
, TREE_OPERAND (statement
, 0));
11635 else if (is_overloaded_fn (statement
)
11636 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
11639 tree fn
= get_first_fn (statement
);
11640 error_at (token
->location
,
11641 "%<%T::%D%> names the constructor, not the type",
11642 DECL_CONTEXT (fn
), DECL_NAME (fn
));
11646 /* Consume the final `;'. */
11647 cp_parser_consume_semicolon_at_end_of_statement (parser
);
11649 if (in_statement_expr
11650 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
11651 /* This is the final expression statement of a statement
11653 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
11654 else if (statement
)
11655 statement
= finish_expr_stmt (statement
);
11660 /* Parse a compound-statement.
11662 compound-statement:
11663 { statement-seq [opt] }
11667 compound-statement:
11668 { label-declaration-seq [opt] statement-seq [opt] }
11670 label-declaration-seq:
11672 label-declaration-seq label-declaration
11674 Returns a tree representing the statement. */
11677 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
11678 int bcs_flags
, bool function_body
)
11680 tree compound_stmt
;
11681 matching_braces braces
;
11683 /* Consume the `{'. */
11684 if (!braces
.require_open (parser
))
11685 return error_mark_node
;
11686 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
11687 && !function_body
&& cxx_dialect
< cxx14
)
11688 pedwarn (input_location
, OPT_Wpedantic
,
11689 "compound-statement in %<constexpr%> function");
11690 /* Begin the compound-statement. */
11691 compound_stmt
= begin_compound_stmt (bcs_flags
);
11692 /* If the next keyword is `__label__' we have a label declaration. */
11693 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
11694 cp_parser_label_declaration (parser
);
11695 /* Parse an (optional) statement-seq. */
11696 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
11697 /* Finish the compound-statement. */
11698 finish_compound_stmt (compound_stmt
);
11699 /* Consume the `}'. */
11700 braces
.require_close (parser
);
11702 return compound_stmt
;
11705 /* Parse an (optional) statement-seq.
11709 statement-seq [opt] statement */
11712 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
11714 /* Scan statements until there aren't any more. */
11717 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11719 /* If we are looking at a `}', then we have run out of
11720 statements; the same is true if we have reached the end
11721 of file, or have stumbled upon a stray '@end'. */
11722 if (token
->type
== CPP_CLOSE_BRACE
11723 || token
->type
== CPP_EOF
11724 || token
->type
== CPP_PRAGMA_EOL
11725 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
11728 /* If we are in a compound statement and find 'else' then
11729 something went wrong. */
11730 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
11732 if (parser
->in_statement
& IN_IF_STMT
)
11736 token
= cp_lexer_consume_token (parser
->lexer
);
11737 error_at (token
->location
, "%<else%> without a previous %<if%>");
11741 /* Parse the statement. */
11742 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
11746 /* Return true if this is the C++20 version of range-based-for with
11750 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
11754 /* Save tokens so that we can put them back. */
11755 cp_lexer_save_tokens (parser
->lexer
);
11757 /* There has to be an unnested ; followed by an unnested :. */
11758 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11759 /*recovering=*/false,
11761 /*consume_paren=*/false) != -1)
11764 /* We found the semicolon, eat it now. */
11765 cp_lexer_consume_token (parser
->lexer
);
11767 /* Now look for ':' that is not nested in () or {}. */
11768 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
11769 /*recovering=*/false,
11771 /*consume_paren=*/false) == -1);
11774 /* Roll back the tokens we skipped. */
11775 cp_lexer_rollback_tokens (parser
->lexer
);
11780 /* Return true if we're looking at (init; cond), false otherwise. */
11783 cp_parser_init_statement_p (cp_parser
*parser
)
11785 /* Save tokens so that we can put them back. */
11786 cp_lexer_save_tokens (parser
->lexer
);
11788 /* Look for ';' that is not nested in () or {}. */
11789 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
11790 /*recovering=*/false,
11792 /*consume_paren=*/false);
11794 /* Roll back the tokens we skipped. */
11795 cp_lexer_rollback_tokens (parser
->lexer
);
11800 /* Parse a selection-statement.
11802 selection-statement:
11803 if ( init-statement [opt] condition ) statement
11804 if ( init-statement [opt] condition ) statement else statement
11805 switch ( init-statement [opt] condition ) statement
11807 Returns the new IF_STMT or SWITCH_STMT.
11809 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11810 is a (possibly labeled) if statement which is not enclosed in
11811 braces and has an else clause. This is used to implement
11814 CHAIN is a vector of if-else-if conditions. This is used to implement
11815 -Wduplicated-cond. */
11818 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
11823 token_indent_info guard_tinfo
;
11828 /* Peek at the next token. */
11829 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
11830 guard_tinfo
= get_token_indent_info (token
);
11832 /* See what kind of keyword it is. */
11833 keyword
= token
->keyword
;
11843 if (keyword
== RID_IF
11844 && cp_lexer_next_token_is_keyword (parser
->lexer
,
11848 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
11849 if (cxx_dialect
< cxx17
)
11850 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
11851 "with %<-std=c++17%> or %<-std=gnu++17%>");
11854 /* Look for the `('. */
11855 matching_parens parens
;
11856 if (!parens
.require_open (parser
))
11858 cp_parser_skip_to_end_of_statement (parser
);
11859 return error_mark_node
;
11862 /* Begin the selection-statement. */
11863 if (keyword
== RID_IF
)
11865 statement
= begin_if_stmt ();
11866 IF_STMT_CONSTEXPR_P (statement
) = cx
;
11869 statement
= begin_switch_stmt ();
11871 /* Parse the optional init-statement. */
11872 if (cp_parser_init_statement_p (parser
))
11875 if (cxx_dialect
< cxx17
)
11876 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
11877 "init-statement in selection statements only available "
11878 "with %<-std=c++17%> or %<-std=gnu++17%>");
11879 cp_parser_init_statement (parser
, &decl
);
11882 /* Parse the condition. */
11883 condition
= cp_parser_condition (parser
);
11884 /* Look for the `)'. */
11885 if (!parens
.require_close (parser
))
11886 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
11887 /*consume_paren=*/true);
11889 if (keyword
== RID_IF
)
11892 unsigned char in_statement
;
11894 /* Add the condition. */
11895 condition
= finish_if_stmt_cond (condition
, statement
);
11897 if (warn_duplicated_cond
)
11898 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
11901 /* Parse the then-clause. */
11902 in_statement
= parser
->in_statement
;
11903 parser
->in_statement
|= IN_IF_STMT
;
11905 /* Outside a template, the non-selected branch of a constexpr
11906 if is a 'discarded statement', i.e. unevaluated. */
11907 bool was_discarded
= in_discarded_stmt
;
11908 bool discard_then
= (cx
&& !processing_template_decl
11909 && integer_zerop (condition
));
11912 in_discarded_stmt
= true;
11913 ++c_inhibit_evaluation_warnings
;
11916 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
11919 parser
->in_statement
= in_statement
;
11921 finish_then_clause (statement
);
11925 THEN_CLAUSE (statement
) = NULL_TREE
;
11926 in_discarded_stmt
= was_discarded
;
11927 --c_inhibit_evaluation_warnings
;
11930 /* If the next token is `else', parse the else-clause. */
11931 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11934 bool discard_else
= (cx
&& !processing_template_decl
11935 && integer_nonzerop (condition
));
11938 in_discarded_stmt
= true;
11939 ++c_inhibit_evaluation_warnings
;
11943 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
11944 /* Consume the `else' keyword. */
11945 cp_lexer_consume_token (parser
->lexer
);
11946 if (warn_duplicated_cond
)
11948 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
11952 /* We've got "if (COND) else if (COND2)". Start
11953 the condition chain and add COND as the first
11955 chain
= new vec
<tree
> ();
11956 if (!CONSTANT_CLASS_P (condition
)
11957 && !TREE_SIDE_EFFECTS (condition
))
11959 /* Wrap it in a NOP_EXPR so that we can set the
11960 location of the condition. */
11961 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
11963 SET_EXPR_LOCATION (e
, token
->location
);
11964 chain
->safe_push (e
);
11967 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
11970 /* This is if-else without subsequent if. Zap the
11971 condition chain; we would have already warned at
11977 begin_else_clause (statement
);
11978 /* Parse the else-clause. */
11979 cp_parser_implicitly_scoped_statement (parser
, NULL
,
11980 guard_tinfo
, chain
);
11982 finish_else_clause (statement
);
11984 /* If we are currently parsing a then-clause, then
11985 IF_P will not be NULL. We set it to true to
11986 indicate that this if statement has an else clause.
11987 This may trigger the Wparentheses warning below
11988 when we get back up to the parent if statement. */
11994 ELSE_CLAUSE (statement
) = NULL_TREE
;
11995 in_discarded_stmt
= was_discarded
;
11996 --c_inhibit_evaluation_warnings
;
12001 /* This if statement does not have an else clause. If
12002 NESTED_IF is true, then the then-clause has an if
12003 statement which does have an else clause. We warn
12004 about the potential ambiguity. */
12006 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
12007 "suggest explicit braces to avoid ambiguous"
12009 if (warn_duplicated_cond
)
12011 /* We don't need the condition chain anymore. */
12017 /* Now we're all done with the if-statement. */
12018 finish_if_stmt (statement
);
12022 bool in_switch_statement_p
;
12023 unsigned char in_statement
;
12025 /* Add the condition. */
12026 finish_switch_cond (condition
, statement
);
12028 /* Parse the body of the switch-statement. */
12029 in_switch_statement_p
= parser
->in_switch_statement_p
;
12030 in_statement
= parser
->in_statement
;
12031 parser
->in_switch_statement_p
= true;
12032 parser
->in_statement
|= IN_SWITCH_STMT
;
12033 cp_parser_implicitly_scoped_statement (parser
, if_p
,
12035 parser
->in_switch_statement_p
= in_switch_statement_p
;
12036 parser
->in_statement
= in_statement
;
12038 /* Now we're all done with the switch-statement. */
12039 finish_switch_stmt (statement
);
12047 cp_parser_error (parser
, "expected selection-statement");
12048 return error_mark_node
;
12052 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12053 If we have seen at least one decl-specifier, and the next token
12054 is not a parenthesis, then we must be looking at a declaration.
12055 (After "int (" we might be looking at a functional cast.) */
12058 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
12059 bool any_specifiers_p
)
12061 if (any_specifiers_p
12062 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12063 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12064 && !cp_parser_error_occurred (parser
))
12065 cp_parser_commit_to_tentative_parse (parser
);
12068 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12069 The declarator shall not specify a function or an array. Returns
12070 TRUE if the declarator is valid, FALSE otherwise. */
12073 cp_parser_check_condition_declarator (cp_parser
* parser
,
12074 cp_declarator
*declarator
,
12077 if (declarator
== cp_error_declarator
12078 || function_declarator_p (declarator
)
12079 || declarator
->kind
== cdk_array
)
12081 if (declarator
== cp_error_declarator
)
12082 /* Already complained. */;
12083 else if (declarator
->kind
== cdk_array
)
12084 error_at (loc
, "condition declares an array");
12086 error_at (loc
, "condition declares a function");
12087 if (parser
->fully_implicit_function_template_p
)
12088 abort_fully_implicit_template (parser
);
12089 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
12090 /*or_comma=*/false,
12091 /*consume_paren=*/false);
12098 /* Parse a condition.
12102 type-specifier-seq declarator = initializer-clause
12103 type-specifier-seq declarator braced-init-list
12108 type-specifier-seq declarator asm-specification [opt]
12109 attributes [opt] = assignment-expression
12111 Returns the expression that should be tested. */
12114 cp_parser_condition (cp_parser
* parser
)
12116 cp_decl_specifier_seq type_specifiers
;
12117 const char *saved_message
;
12118 int declares_class_or_enum
;
12120 /* Try the declaration first. */
12121 cp_parser_parse_tentatively (parser
);
12122 /* New types are not allowed in the type-specifier-seq for a
12124 saved_message
= parser
->type_definition_forbidden_message
;
12125 parser
->type_definition_forbidden_message
12126 = G_("types may not be defined in conditions");
12127 /* Parse the type-specifier-seq. */
12128 cp_parser_decl_specifier_seq (parser
,
12129 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12131 &declares_class_or_enum
);
12132 /* Restore the saved message. */
12133 parser
->type_definition_forbidden_message
= saved_message
;
12135 /* Gather the attributes that were provided with the
12136 decl-specifiers. */
12137 tree prefix_attributes
= type_specifiers
.attributes
;
12139 cp_parser_maybe_commit_to_declaration (parser
,
12140 type_specifiers
.any_specifiers_p
);
12142 /* If all is well, we might be looking at a declaration. */
12143 if (!cp_parser_error_occurred (parser
))
12146 tree asm_specification
;
12148 cp_declarator
*declarator
;
12149 tree initializer
= NULL_TREE
;
12150 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12152 /* Parse the declarator. */
12153 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12154 CP_PARSER_FLAGS_NONE
,
12155 /*ctor_dtor_or_conv_p=*/NULL
,
12156 /*parenthesized_p=*/NULL
,
12157 /*member_p=*/false,
12158 /*friend_p=*/false,
12159 /*static_p=*/false);
12160 /* Parse the attributes. */
12161 attributes
= cp_parser_attributes_opt (parser
);
12162 /* Parse the asm-specification. */
12163 asm_specification
= cp_parser_asm_specification_opt (parser
);
12164 /* If the next token is not an `=' or '{', then we might still be
12165 looking at an expression. For example:
12169 looks like a decl-specifier-seq and a declarator -- but then
12170 there is no `=', so this is an expression. */
12171 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12172 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12173 cp_parser_simulate_error (parser
);
12175 /* If we did see an `=' or '{', then we are looking at a declaration
12177 if (cp_parser_parse_definitely (parser
))
12180 bool non_constant_p
= false;
12181 int flags
= LOOKUP_ONLYCONVERTING
;
12183 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12184 return error_mark_node
;
12186 /* Create the declaration. */
12187 decl
= start_decl (declarator
, &type_specifiers
,
12188 /*initialized_p=*/true,
12189 attributes
, prefix_attributes
,
12192 /* Parse the initializer. */
12193 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12195 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12196 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12199 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12201 /* Consume the `='. */
12202 cp_lexer_consume_token (parser
->lexer
);
12203 initializer
= cp_parser_initializer_clause (parser
,
12208 cp_parser_error (parser
, "expected initializer");
12209 initializer
= error_mark_node
;
12211 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12212 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12214 /* Process the initializer. */
12215 cp_finish_decl (decl
,
12216 initializer
, !non_constant_p
,
12221 pop_scope (pushed_scope
);
12223 return convert_from_reference (decl
);
12226 /* If we didn't even get past the declarator successfully, we are
12227 definitely not looking at a declaration. */
12229 cp_parser_abort_tentative_parse (parser
);
12231 /* Otherwise, we are looking at an expression. */
12232 return cp_parser_expression (parser
);
12235 /* Parses a for-statement or range-for-statement until the closing ')',
12239 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12241 tree init
, scope
, decl
;
12244 /* Begin the for-statement. */
12245 scope
= begin_for_scope (&init
);
12247 /* Parse the initialization. */
12248 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12251 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12254 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12258 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12259 unsigned short unroll
)
12261 /* Normal for loop */
12262 tree condition
= NULL_TREE
;
12263 tree expression
= NULL_TREE
;
12266 stmt
= begin_for_stmt (scope
, init
);
12267 /* The init-statement has already been parsed in
12268 cp_parser_init_statement, so no work is needed here. */
12269 finish_init_stmt (stmt
);
12271 /* If there's a condition, process it. */
12272 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12273 condition
= cp_parser_condition (parser
);
12276 cp_parser_error (parser
, "missing loop condition in loop with "
12277 "%<GCC ivdep%> pragma");
12278 condition
= error_mark_node
;
12282 cp_parser_error (parser
, "missing loop condition in loop with "
12283 "%<GCC unroll%> pragma");
12284 condition
= error_mark_node
;
12286 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12287 /* Look for the `;'. */
12288 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12290 /* If there's an expression, process it. */
12291 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12292 expression
= cp_parser_expression (parser
);
12293 finish_for_expr (expression
, stmt
);
12298 /* Tries to parse a range-based for-statement:
12301 decl-specifier-seq declarator : expression
12303 The decl-specifier-seq declarator and the `:' are already parsed by
12304 cp_parser_init_statement. If processing_template_decl it returns a
12305 newly created RANGE_FOR_STMT; if not, it is converted to a
12306 regular FOR_STMT. */
12309 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12310 bool ivdep
, unsigned short unroll
, bool is_omp
)
12312 tree stmt
, range_expr
;
12313 auto_vec
<cxx_binding
*, 16> bindings
;
12314 auto_vec
<tree
, 16> names
;
12315 tree decomp_first_name
= NULL_TREE
;
12316 unsigned int decomp_cnt
= 0;
12318 /* Get the range declaration momentarily out of the way so that
12319 the range expression doesn't clash with it. */
12320 if (range_decl
!= error_mark_node
)
12322 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12324 tree v
= DECL_VALUE_EXPR (range_decl
);
12325 /* For decomposition declaration get all of the corresponding
12326 declarations out of the way. */
12327 if (TREE_CODE (v
) == ARRAY_REF
12328 && VAR_P (TREE_OPERAND (v
, 0))
12329 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12331 tree d
= range_decl
;
12332 range_decl
= TREE_OPERAND (v
, 0);
12333 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12334 decomp_first_name
= d
;
12335 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12337 tree name
= DECL_NAME (d
);
12338 names
.safe_push (name
);
12339 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12340 IDENTIFIER_BINDING (name
)
12341 = IDENTIFIER_BINDING (name
)->previous
;
12345 if (names
.is_empty ())
12347 tree name
= DECL_NAME (range_decl
);
12348 names
.safe_push (name
);
12349 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12350 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12354 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12356 bool expr_non_constant_p
;
12357 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12360 range_expr
= cp_parser_expression (parser
);
12362 /* Put the range declaration(s) back into scope. */
12363 for (unsigned int i
= 0; i
< names
.length (); i
++)
12365 cxx_binding
*binding
= bindings
[i
];
12366 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12367 IDENTIFIER_BINDING (names
[i
]) = binding
;
12370 /* finish_omp_for has its own code for the following, so just
12371 return the range_expr instead. */
12375 /* If in template, STMT is converted to a normal for-statement
12376 at instantiation. If not, it is done just ahead. */
12377 if (processing_template_decl
)
12379 if (check_for_bare_parameter_packs (range_expr
))
12380 range_expr
= error_mark_node
;
12381 stmt
= begin_range_for_stmt (scope
, init
);
12383 RANGE_FOR_IVDEP (stmt
) = 1;
12385 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12386 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12387 if (!type_dependent_expression_p (range_expr
)
12388 /* do_auto_deduction doesn't mess with template init-lists. */
12389 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12390 do_range_for_auto_deduction (range_decl
, range_expr
);
12394 stmt
= begin_for_stmt (scope
, init
);
12395 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12396 decomp_first_name
, decomp_cnt
, ivdep
,
12402 /* Subroutine of cp_convert_range_for: given the initializer expression,
12403 builds up the range temporary. */
12406 build_range_temp (tree range_expr
)
12408 tree range_type
, range_temp
;
12410 /* Find out the type deduced by the declaration
12411 `auto &&__range = range_expr'. */
12412 range_type
= cp_build_reference_type (make_auto (), true);
12413 range_type
= do_auto_deduction (range_type
, range_expr
,
12414 type_uses_auto (range_type
));
12416 /* Create the __range variable. */
12417 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12419 TREE_USED (range_temp
) = 1;
12420 DECL_ARTIFICIAL (range_temp
) = 1;
12425 /* Used by cp_parser_range_for in template context: we aren't going to
12426 do a full conversion yet, but we still need to resolve auto in the
12427 type of the for-range-declaration if present. This is basically
12428 a shortcut version of cp_convert_range_for. */
12431 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12433 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12436 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12437 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12438 iter_type
= (cp_parser_perform_range_for_lookup
12439 (range_temp
, &begin_dummy
, &end_dummy
));
12442 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12444 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12446 tf_warning_or_error
);
12447 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12448 iter_decl
, auto_node
);
12453 /* Converts a range-based for-statement into a normal
12454 for-statement, as per the definition.
12456 for (RANGE_DECL : RANGE_EXPR)
12459 should be equivalent to:
12462 auto &&__range = RANGE_EXPR;
12463 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
12467 RANGE_DECL = *__begin;
12472 If RANGE_EXPR is an array:
12473 BEGIN_EXPR = __range
12474 END_EXPR = __range + ARRAY_SIZE(__range)
12475 Else if RANGE_EXPR has a member 'begin' or 'end':
12476 BEGIN_EXPR = __range.begin()
12477 END_EXPR = __range.end()
12479 BEGIN_EXPR = begin(__range)
12480 END_EXPR = end(__range);
12482 If __range has a member 'begin' but not 'end', or vice versa, we must
12483 still use the second alternative (it will surely fail, however).
12484 When calling begin()/end() in the third alternative we must use
12485 argument dependent lookup, but always considering 'std' as an associated
12489 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12490 tree decomp_first_name
, unsigned int decomp_cnt
,
12491 bool ivdep
, unsigned short unroll
)
12494 tree iter_type
, begin_expr
, end_expr
;
12495 tree condition
, expression
;
12497 range_expr
= mark_lvalue_use (range_expr
);
12499 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12500 /* If an error happened previously do nothing or else a lot of
12501 unhelpful errors would be issued. */
12502 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12507 if (VAR_P (range_expr
)
12508 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12509 /* Can't bind a reference to an array of runtime bound. */
12510 range_temp
= range_expr
;
12513 range_temp
= build_range_temp (range_expr
);
12514 pushdecl (range_temp
);
12515 cp_finish_decl (range_temp
, range_expr
,
12516 /*is_constant_init*/false, NULL_TREE
,
12517 LOOKUP_ONLYCONVERTING
);
12518 range_temp
= convert_from_reference (range_temp
);
12520 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12521 &begin_expr
, &end_expr
);
12524 /* The new for initialization statement. */
12525 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12527 TREE_USED (begin
) = 1;
12528 DECL_ARTIFICIAL (begin
) = 1;
12530 cp_finish_decl (begin
, begin_expr
,
12531 /*is_constant_init*/false, NULL_TREE
,
12532 LOOKUP_ONLYCONVERTING
);
12534 if (cxx_dialect
>= cxx17
)
12535 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12536 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12537 TREE_USED (end
) = 1;
12538 DECL_ARTIFICIAL (end
) = 1;
12540 cp_finish_decl (end
, end_expr
,
12541 /*is_constant_init*/false, NULL_TREE
,
12542 LOOKUP_ONLYCONVERTING
);
12544 finish_init_stmt (statement
);
12546 /* The new for condition. */
12547 condition
= build_x_binary_op (input_location
, NE_EXPR
,
12550 NULL
, tf_warning_or_error
);
12551 finish_for_cond (condition
, statement
, ivdep
, unroll
);
12553 /* The new increment expression. */
12554 expression
= finish_unary_op_expr (input_location
,
12555 PREINCREMENT_EXPR
, begin
,
12556 tf_warning_or_error
);
12557 finish_for_expr (expression
, statement
);
12559 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12560 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12562 /* The declaration is initialized with *__begin inside the loop body. */
12563 cp_finish_decl (range_decl
,
12564 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
12565 tf_warning_or_error
),
12566 /*is_constant_init*/false, NULL_TREE
,
12567 LOOKUP_ONLYCONVERTING
);
12568 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
12569 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
12574 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
12575 We need to solve both at the same time because the method used
12576 depends on the existence of members begin or end.
12577 Returns the type deduced for the iterator expression. */
12580 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
12582 if (error_operand_p (range
))
12584 *begin
= *end
= error_mark_node
;
12585 return error_mark_node
;
12588 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
12590 error ("range-based %<for%> expression of type %qT "
12591 "has incomplete type", TREE_TYPE (range
));
12592 *begin
= *end
= error_mark_node
;
12593 return error_mark_node
;
12595 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
12597 /* If RANGE is an array, we will use pointer arithmetic. */
12598 *begin
= decay_conversion (range
, tf_warning_or_error
);
12599 *end
= build_binary_op (input_location
, PLUS_EXPR
,
12601 array_type_nelts_top (TREE_TYPE (range
)),
12603 return TREE_TYPE (*begin
);
12607 /* If it is not an array, we must do a bit of magic. */
12608 tree id_begin
, id_end
;
12609 tree member_begin
, member_end
;
12611 *begin
= *end
= error_mark_node
;
12613 id_begin
= get_identifier ("begin");
12614 id_end
= get_identifier ("end");
12615 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
12616 /*protect=*/2, /*want_type=*/false,
12617 tf_warning_or_error
);
12618 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
12619 /*protect=*/2, /*want_type=*/false,
12620 tf_warning_or_error
);
12622 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
12624 /* Use the member functions. */
12625 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
12626 *end
= cp_parser_range_for_member_function (range
, id_end
);
12630 /* Use global functions with ADL. */
12633 vec_safe_push (vec
, range
);
12635 member_begin
= perform_koenig_lookup (id_begin
, vec
,
12636 tf_warning_or_error
);
12637 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
12638 tf_warning_or_error
);
12639 member_end
= perform_koenig_lookup (id_end
, vec
,
12640 tf_warning_or_error
);
12641 *end
= finish_call_expr (member_end
, &vec
, false, true,
12642 tf_warning_or_error
);
12645 /* Last common checks. */
12646 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
12648 /* If one of the expressions is an error do no more checks. */
12649 *begin
= *end
= error_mark_node
;
12650 return error_mark_node
;
12652 else if (type_dependent_expression_p (*begin
)
12653 || type_dependent_expression_p (*end
))
12654 /* Can happen, when, eg, in a template context, Koenig lookup
12655 can't resolve begin/end (c++/58503). */
12659 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
12660 /* The unqualified type of the __begin and __end temporaries should
12661 be the same, as required by the multiple auto declaration. */
12662 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
12664 if (cxx_dialect
>= cxx17
12665 && (build_x_binary_op (input_location
, NE_EXPR
,
12666 *begin
, ERROR_MARK
,
12669 != error_mark_node
))
12670 /* P0184R0 allows __begin and __end to have different types,
12671 but make sure they are comparable so we can give a better
12674 error ("inconsistent begin/end types in range-based %<for%> "
12675 "statement: %qT and %qT",
12676 TREE_TYPE (*begin
), TREE_TYPE (*end
));
12683 /* Helper function for cp_parser_perform_range_for_lookup.
12684 Builds a tree for RANGE.IDENTIFIER(). */
12687 cp_parser_range_for_member_function (tree range
, tree identifier
)
12691 member
= finish_class_member_access_expr (range
, identifier
,
12692 false, tf_warning_or_error
);
12693 if (member
== error_mark_node
)
12694 return error_mark_node
;
12697 res
= finish_call_expr (member
, &vec
,
12698 /*disallow_virtual=*/false,
12699 /*koenig_p=*/false,
12700 tf_warning_or_error
);
12704 /* Parse an iteration-statement.
12706 iteration-statement:
12707 while ( condition ) statement
12708 do statement while ( expression ) ;
12709 for ( init-statement condition [opt] ; expression [opt] )
12712 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12715 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
12716 unsigned short unroll
)
12721 unsigned char in_statement
;
12722 token_indent_info guard_tinfo
;
12724 /* Peek at the next token. */
12725 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
12727 return error_mark_node
;
12729 guard_tinfo
= get_token_indent_info (token
);
12731 /* Remember whether or not we are already within an iteration
12733 in_statement
= parser
->in_statement
;
12735 /* See what kind of keyword it is. */
12736 keyword
= token
->keyword
;
12743 /* Begin the while-statement. */
12744 statement
= begin_while_stmt ();
12745 /* Look for the `('. */
12746 matching_parens parens
;
12747 parens
.require_open (parser
);
12748 /* Parse the condition. */
12749 condition
= cp_parser_condition (parser
);
12750 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
12751 /* Look for the `)'. */
12752 parens
.require_close (parser
);
12753 /* Parse the dependent statement. */
12754 parser
->in_statement
= IN_ITERATION_STMT
;
12755 bool prev
= note_iteration_stmt_body_start ();
12756 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12757 note_iteration_stmt_body_end (prev
);
12758 parser
->in_statement
= in_statement
;
12759 /* We're done with the while-statement. */
12760 finish_while_stmt (statement
);
12768 /* Begin the do-statement. */
12769 statement
= begin_do_stmt ();
12770 /* Parse the body of the do-statement. */
12771 parser
->in_statement
= IN_ITERATION_STMT
;
12772 bool prev
= note_iteration_stmt_body_start ();
12773 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
12774 note_iteration_stmt_body_end (prev
);
12775 parser
->in_statement
= in_statement
;
12776 finish_do_body (statement
);
12777 /* Look for the `while' keyword. */
12778 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
12779 /* Look for the `('. */
12780 matching_parens parens
;
12781 parens
.require_open (parser
);
12782 /* Parse the expression. */
12783 expression
= cp_parser_expression (parser
);
12784 /* We're done with the do-statement. */
12785 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
12786 /* Look for the `)'. */
12787 parens
.require_close (parser
);
12788 /* Look for the `;'. */
12789 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12795 /* Look for the `('. */
12796 matching_parens parens
;
12797 parens
.require_open (parser
);
12799 statement
= cp_parser_for (parser
, ivdep
, unroll
);
12801 /* Look for the `)'. */
12802 parens
.require_close (parser
);
12804 /* Parse the body of the for-statement. */
12805 parser
->in_statement
= IN_ITERATION_STMT
;
12806 bool prev
= note_iteration_stmt_body_start ();
12807 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
12808 note_iteration_stmt_body_end (prev
);
12809 parser
->in_statement
= in_statement
;
12811 /* We're done with the for-statement. */
12812 finish_for_stmt (statement
);
12817 cp_parser_error (parser
, "expected iteration-statement");
12818 statement
= error_mark_node
;
12825 /* Parse a init-statement or the declarator of a range-based-for.
12826 Returns true if a range-based-for declaration is seen.
12829 expression-statement
12830 simple-declaration */
12833 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
12835 /* If the next token is a `;', then we have an empty
12836 expression-statement. Grammatically, this is also a
12837 simple-declaration, but an invalid one, because it does not
12838 declare anything. Therefore, if we did not handle this case
12839 specially, we would issue an error message about an invalid
12841 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12843 bool is_range_for
= false;
12844 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
12846 /* Try to parse the init-statement. */
12847 if (cp_parser_range_based_for_with_init_p (parser
))
12850 cp_parser_parse_tentatively (parser
);
12851 /* Parse the declaration. */
12852 cp_parser_simple_declaration (parser
,
12853 /*function_definition_allowed_p=*/false,
12855 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12856 if (!cp_parser_parse_definitely (parser
))
12857 /* That didn't work, try to parse it as an expression-statement. */
12858 cp_parser_expression_statement (parser
, NULL_TREE
);
12860 if (cxx_dialect
< cxx2a
)
12862 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12863 "range-based %<for%> loops with initializer only "
12864 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
12865 *decl
= error_mark_node
;
12869 /* A colon is used in range-based for. */
12870 parser
->colon_corrects_to_scope_p
= false;
12872 /* We're going to speculatively look for a declaration, falling back
12873 to an expression, if necessary. */
12874 cp_parser_parse_tentatively (parser
);
12875 /* Parse the declaration. */
12876 cp_parser_simple_declaration (parser
,
12877 /*function_definition_allowed_p=*/false,
12879 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
12880 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
12882 /* It is a range-for, consume the ':'. */
12883 cp_lexer_consume_token (parser
->lexer
);
12884 is_range_for
= true;
12885 if (cxx_dialect
< cxx11
)
12886 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12887 "range-based %<for%> loops only available with "
12888 "%<-std=c++11%> or %<-std=gnu++11%>");
12891 /* The ';' is not consumed yet because we told
12892 cp_parser_simple_declaration not to. */
12893 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12895 if (cp_parser_parse_definitely (parser
))
12896 return is_range_for
;
12897 /* If the tentative parse failed, then we shall need to look for an
12898 expression-statement. */
12900 /* If we are here, it is an expression-statement. */
12901 cp_parser_expression_statement (parser
, NULL_TREE
);
12905 /* Parse a jump-statement.
12910 return expression [opt] ;
12911 return braced-init-list ;
12917 goto * expression ;
12919 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12922 cp_parser_jump_statement (cp_parser
* parser
)
12924 tree statement
= error_mark_node
;
12927 unsigned char in_statement
;
12929 /* Peek at the next token. */
12930 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
12932 return error_mark_node
;
12934 /* See what kind of keyword it is. */
12935 keyword
= token
->keyword
;
12939 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
12940 switch (in_statement
)
12943 error_at (token
->location
, "break statement not within loop or switch");
12946 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
12947 || in_statement
== IN_ITERATION_STMT
);
12948 statement
= finish_break_stmt ();
12949 if (in_statement
== IN_ITERATION_STMT
)
12950 break_maybe_infinite_loop ();
12953 error_at (token
->location
, "invalid exit from OpenMP structured block");
12956 error_at (token
->location
, "break statement used with OpenMP for loop");
12959 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12963 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
12966 error_at (token
->location
, "continue statement not within a loop");
12968 /* Fall through. */
12969 case IN_ITERATION_STMT
:
12971 statement
= finish_continue_stmt ();
12974 error_at (token
->location
, "invalid exit from OpenMP structured block");
12977 gcc_unreachable ();
12979 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12985 bool expr_non_constant_p
;
12987 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12989 cp_lexer_set_source_position (parser
->lexer
);
12990 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12991 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12993 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12994 expr
= cp_parser_expression (parser
);
12996 /* If the next token is a `;', then there is no
12999 /* Build the return-statement. */
13000 if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
13001 /* Don't deduce from a discarded return statement. */;
13003 statement
= finish_return_stmt (expr
);
13004 /* Look for the final `;'. */
13005 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13010 if (parser
->in_function_body
13011 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
13013 error ("%<goto%> in %<constexpr%> function");
13014 cp_function_chain
->invalid_constexpr
= true;
13017 /* Create the goto-statement. */
13018 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
13020 /* Issue a warning about this use of a GNU extension. */
13021 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
13022 /* Consume the '*' token. */
13023 cp_lexer_consume_token (parser
->lexer
);
13024 /* Parse the dependent expression. */
13025 finish_goto_stmt (cp_parser_expression (parser
));
13028 finish_goto_stmt (cp_parser_identifier (parser
));
13029 /* Look for the final `;'. */
13030 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13034 cp_parser_error (parser
, "expected jump-statement");
13041 /* Parse a declaration-statement.
13043 declaration-statement:
13044 block-declaration */
13047 cp_parser_declaration_statement (cp_parser
* parser
)
13051 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13052 p
= obstack_alloc (&declarator_obstack
, 0);
13054 /* Parse the block-declaration. */
13055 cp_parser_block_declaration (parser
, /*statement_p=*/true);
13057 /* Free any declarators allocated. */
13058 obstack_free (&declarator_obstack
, p
);
13061 /* Some dependent statements (like `if (cond) statement'), are
13062 implicitly in their own scope. In other words, if the statement is
13063 a single statement (as opposed to a compound-statement), it is
13064 none-the-less treated as if it were enclosed in braces. Any
13065 declarations appearing in the dependent statement are out of scope
13066 after control passes that point. This function parses a statement,
13067 but ensures that is in its own scope, even if it is not a
13068 compound-statement.
13070 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13071 is a (possibly labeled) if statement which is not enclosed in
13072 braces and has an else clause. This is used to implement
13075 CHAIN is a vector of if-else-if conditions. This is used to implement
13078 Returns the new statement. */
13081 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
13082 const token_indent_info
&guard_tinfo
,
13086 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13087 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
13088 token_indent_info body_tinfo
13089 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13094 /* Mark if () ; with a special NOP_EXPR. */
13095 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13097 cp_lexer_consume_token (parser
->lexer
);
13098 statement
= add_stmt (build_empty_stmt (body_loc
));
13100 if (guard_tinfo
.keyword
== RID_IF
13101 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13102 warning_at (body_loc
, OPT_Wempty_body
,
13103 "suggest braces around empty body in an %<if%> statement");
13104 else if (guard_tinfo
.keyword
== RID_ELSE
)
13105 warning_at (body_loc
, OPT_Wempty_body
,
13106 "suggest braces around empty body in an %<else%> statement");
13108 /* if a compound is opened, we simply parse the statement directly. */
13109 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13110 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13111 /* If the token is not a `{', then we must take special action. */
13114 /* Create a compound-statement. */
13115 statement
= begin_compound_stmt (0);
13116 /* Parse the dependent-statement. */
13117 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13118 &body_loc_after_labels
);
13119 /* Finish the dummy compound-statement. */
13120 finish_compound_stmt (statement
);
13123 token_indent_info next_tinfo
13124 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13125 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13127 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13128 && next_tinfo
.type
!= CPP_SEMICOLON
)
13129 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13130 guard_tinfo
.location
, guard_tinfo
.keyword
);
13132 /* Return the statement. */
13136 /* For some dependent statements (like `while (cond) statement'), we
13137 have already created a scope. Therefore, even if the dependent
13138 statement is a compound-statement, we do not want to create another
13142 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13143 const token_indent_info
&guard_tinfo
)
13145 /* If the token is a `{', then we must take special action. */
13146 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13148 token_indent_info body_tinfo
13149 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13150 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13152 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13153 &loc_after_labels
);
13154 token_indent_info next_tinfo
13155 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13156 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13158 if (loc_after_labels
!= UNKNOWN_LOCATION
13159 && next_tinfo
.type
!= CPP_SEMICOLON
)
13160 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13161 guard_tinfo
.location
,
13162 guard_tinfo
.keyword
);
13166 /* Avoid calling cp_parser_compound_statement, so that we
13167 don't create a new scope. Do everything else by hand. */
13168 matching_braces braces
;
13169 braces
.require_open (parser
);
13170 /* If the next keyword is `__label__' we have a label declaration. */
13171 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13172 cp_parser_label_declaration (parser
);
13173 /* Parse an (optional) statement-seq. */
13174 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13175 braces
.require_close (parser
);
13179 /* Declarations [gram.dcl.dcl] */
13181 /* Parse an optional declaration-sequence.
13185 declaration-seq declaration */
13188 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13192 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13194 if (token
->type
== CPP_CLOSE_BRACE
13195 || token
->type
== CPP_EOF
)
13198 cp_parser_toplevel_declaration (parser
);
13202 /* Parse a declaration.
13206 function-definition
13207 template-declaration
13208 explicit-instantiation
13209 explicit-specialization
13210 linkage-specification
13211 namespace-definition
13219 __extension__ declaration */
13222 cp_parser_declaration (cp_parser
* parser
)
13226 int saved_pedantic
;
13228 tree attributes
= NULL_TREE
;
13230 /* Check for the `__extension__' keyword. */
13231 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13233 /* Parse the qualified declaration. */
13234 cp_parser_declaration (parser
);
13235 /* Restore the PEDANTIC flag. */
13236 pedantic
= saved_pedantic
;
13241 /* Try to figure out what kind of declaration is present. */
13242 token1
= *cp_lexer_peek_token (parser
->lexer
);
13244 if (token1
.type
!= CPP_EOF
)
13245 token2
= *cp_lexer_peek_nth_token (parser
->lexer
, 2);
13248 token2
.type
= CPP_EOF
;
13249 token2
.keyword
= RID_MAX
;
13252 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13253 p
= obstack_alloc (&declarator_obstack
, 0);
13255 /* If the next token is `extern' and the following token is a string
13256 literal, then we have a linkage specification. */
13257 if (token1
.keyword
== RID_EXTERN
13258 && cp_parser_is_pure_string_literal (&token2
))
13259 cp_parser_linkage_specification (parser
);
13260 /* If the next token is `template', then we have either a template
13261 declaration, an explicit instantiation, or an explicit
13263 else if (token1
.keyword
== RID_TEMPLATE
)
13265 /* `template <>' indicates a template specialization. */
13266 if (token2
.type
== CPP_LESS
13267 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
13268 cp_parser_explicit_specialization (parser
);
13269 /* `template <' indicates a template declaration. */
13270 else if (token2
.type
== CPP_LESS
)
13271 cp_parser_template_declaration (parser
, /*member_p=*/false);
13272 /* Anything else must be an explicit instantiation. */
13274 cp_parser_explicit_instantiation (parser
);
13276 /* If the next token is `export', then we have a template
13278 else if (token1
.keyword
== RID_EXPORT
)
13279 cp_parser_template_declaration (parser
, /*member_p=*/false);
13280 /* If the next token is `extern', 'static' or 'inline' and the one
13281 after that is `template', we have a GNU extended explicit
13282 instantiation directive. */
13283 else if (cp_parser_allow_gnu_extensions_p (parser
)
13284 && (token1
.keyword
== RID_EXTERN
13285 || token1
.keyword
== RID_STATIC
13286 || token1
.keyword
== RID_INLINE
)
13287 && token2
.keyword
== RID_TEMPLATE
)
13288 cp_parser_explicit_instantiation (parser
);
13289 /* If the next token is `namespace', check for a named or unnamed
13290 namespace definition. */
13291 else if (token1
.keyword
== RID_NAMESPACE
13292 && (/* A named namespace definition. */
13293 (token2
.type
== CPP_NAME
13294 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13296 || (token2
.type
== CPP_OPEN_SQUARE
13297 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
13298 == CPP_OPEN_SQUARE
)
13299 /* An unnamed namespace definition. */
13300 || token2
.type
== CPP_OPEN_BRACE
13301 || token2
.keyword
== RID_ATTRIBUTE
))
13302 cp_parser_namespace_definition (parser
);
13303 /* An inline (associated) namespace definition. */
13304 else if (token1
.keyword
== RID_INLINE
13305 && token2
.keyword
== RID_NAMESPACE
)
13306 cp_parser_namespace_definition (parser
);
13307 /* Objective-C++ declaration/definition. */
13308 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
.keyword
))
13309 cp_parser_objc_declaration (parser
, NULL_TREE
);
13310 else if (c_dialect_objc ()
13311 && token1
.keyword
== RID_ATTRIBUTE
13312 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
13313 cp_parser_objc_declaration (parser
, attributes
);
13314 /* At this point we may have a template declared by a concept
13316 else if (flag_concepts
13317 && cp_parser_template_declaration_after_export (parser
,
13318 /*member_p=*/false))
13321 /* Try to parse a block-declaration, or a function-definition. */
13322 cp_parser_block_declaration (parser
, /*statement_p=*/false);
13324 /* Free any declarators allocated. */
13325 obstack_free (&declarator_obstack
, p
);
13328 /* Parse a namespace-scope declaration. */
13331 cp_parser_toplevel_declaration (cp_parser
* parser
)
13333 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13335 if (token
->type
== CPP_PRAGMA
)
13336 /* A top-level declaration can consist solely of a #pragma. A
13337 nested declaration cannot, so this is done here and not in
13338 cp_parser_declaration. (A #pragma at block scope is
13339 handled in cp_parser_statement.) */
13340 cp_parser_pragma (parser
, pragma_external
, NULL
);
13341 else if (token
->type
== CPP_SEMICOLON
)
13343 /* A declaration consisting of a single semicolon is
13344 invalid. Allow it unless we're being pedantic. */
13345 cp_lexer_consume_token (parser
->lexer
);
13346 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
13349 /* Parse the declaration itself. */
13350 cp_parser_declaration (parser
);
13353 /* Parse a block-declaration.
13358 namespace-alias-definition
13365 __extension__ block-declaration
13370 static_assert-declaration
13372 If STATEMENT_P is TRUE, then this block-declaration is occurring as
13373 part of a declaration-statement. */
13376 cp_parser_block_declaration (cp_parser
*parser
,
13380 int saved_pedantic
;
13382 /* Check for the `__extension__' keyword. */
13383 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13385 /* Parse the qualified declaration. */
13386 cp_parser_block_declaration (parser
, statement_p
);
13387 /* Restore the PEDANTIC flag. */
13388 pedantic
= saved_pedantic
;
13393 /* Peek at the next token to figure out which kind of declaration is
13395 token1
= cp_lexer_peek_token (parser
->lexer
);
13397 /* If the next keyword is `asm', we have an asm-definition. */
13398 if (token1
->keyword
== RID_ASM
)
13401 cp_parser_commit_to_tentative_parse (parser
);
13402 cp_parser_asm_definition (parser
);
13404 /* If the next keyword is `namespace', we have a
13405 namespace-alias-definition. */
13406 else if (token1
->keyword
== RID_NAMESPACE
)
13407 cp_parser_namespace_alias_definition (parser
);
13408 /* If the next keyword is `using', we have a
13409 using-declaration, a using-directive, or an alias-declaration. */
13410 else if (token1
->keyword
== RID_USING
)
13415 cp_parser_commit_to_tentative_parse (parser
);
13416 /* If the token after `using' is `namespace', then we have a
13417 using-directive. */
13418 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
13419 if (token2
->keyword
== RID_NAMESPACE
)
13420 cp_parser_using_directive (parser
);
13421 /* If the second token after 'using' is '=', then we have an
13422 alias-declaration. */
13423 else if (cxx_dialect
>= cxx11
13424 && token2
->type
== CPP_NAME
13425 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
13426 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
13427 cp_parser_alias_declaration (parser
);
13428 /* Otherwise, it's a using-declaration. */
13430 cp_parser_using_declaration (parser
,
13431 /*access_declaration_p=*/false);
13433 /* If the next keyword is `__label__' we have a misplaced label
13435 else if (token1
->keyword
== RID_LABEL
)
13437 cp_lexer_consume_token (parser
->lexer
);
13438 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
13439 cp_parser_skip_to_end_of_statement (parser
);
13440 /* If the next token is now a `;', consume it. */
13441 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13442 cp_lexer_consume_token (parser
->lexer
);
13444 /* If the next token is `static_assert' we have a static assertion. */
13445 else if (token1
->keyword
== RID_STATIC_ASSERT
)
13446 cp_parser_static_assert (parser
, /*member_p=*/false);
13447 /* Anything else must be a simple-declaration. */
13449 cp_parser_simple_declaration (parser
, !statement_p
,
13450 /*maybe_range_for_decl*/NULL
);
13453 /* Parse a simple-declaration.
13455 simple-declaration:
13456 decl-specifier-seq [opt] init-declarator-list [opt] ;
13457 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13458 brace-or-equal-initializer ;
13460 init-declarator-list:
13462 init-declarator-list , init-declarator
13464 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
13465 function-definition as a simple-declaration.
13467 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
13468 parsed declaration if it is an uninitialized single declarator not followed
13469 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
13470 if present, will not be consumed. */
13473 cp_parser_simple_declaration (cp_parser
* parser
,
13474 bool function_definition_allowed_p
,
13475 tree
*maybe_range_for_decl
)
13477 cp_decl_specifier_seq decl_specifiers
;
13478 int declares_class_or_enum
;
13479 bool saw_declarator
;
13480 location_t comma_loc
= UNKNOWN_LOCATION
;
13481 location_t init_loc
= UNKNOWN_LOCATION
;
13483 if (maybe_range_for_decl
)
13484 *maybe_range_for_decl
= NULL_TREE
;
13486 /* Defer access checks until we know what is being declared; the
13487 checks for names appearing in the decl-specifier-seq should be
13488 done as if we were in the scope of the thing being declared. */
13489 push_deferring_access_checks (dk_deferred
);
13491 /* Parse the decl-specifier-seq. We have to keep track of whether
13492 or not the decl-specifier-seq declares a named class or
13493 enumeration type, since that is the only case in which the
13494 init-declarator-list is allowed to be empty.
13498 In a simple-declaration, the optional init-declarator-list can be
13499 omitted only when declaring a class or enumeration, that is when
13500 the decl-specifier-seq contains either a class-specifier, an
13501 elaborated-type-specifier, or an enum-specifier. */
13502 cp_parser_decl_specifier_seq (parser
,
13503 CP_PARSER_FLAGS_OPTIONAL
,
13505 &declares_class_or_enum
);
13506 /* We no longer need to defer access checks. */
13507 stop_deferring_access_checks ();
13509 /* In a block scope, a valid declaration must always have a
13510 decl-specifier-seq. By not trying to parse declarators, we can
13511 resolve the declaration/expression ambiguity more quickly. */
13512 if (!function_definition_allowed_p
13513 && !decl_specifiers
.any_specifiers_p
)
13515 cp_parser_error (parser
, "expected declaration");
13519 /* If the next two tokens are both identifiers, the code is
13520 erroneous. The usual cause of this situation is code like:
13524 where "T" should name a type -- but does not. */
13525 if (!decl_specifiers
.any_type_specifiers_p
13526 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
13528 /* If parsing tentatively, we should commit; we really are
13529 looking at a declaration. */
13530 cp_parser_commit_to_tentative_parse (parser
);
13535 cp_parser_maybe_commit_to_declaration (parser
,
13536 decl_specifiers
.any_specifiers_p
);
13538 /* Look for C++17 decomposition declaration. */
13539 for (size_t n
= 1; ; n
++)
13540 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
13541 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
13543 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
13544 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
13545 && decl_specifiers
.any_specifiers_p
)
13548 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
13549 maybe_range_for_decl
,
13552 /* The next token should be either a `,' or a `;'. */
13553 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13554 /* If it's a `;', we are done. */
13555 if (token
->type
== CPP_SEMICOLON
)
13557 else if (maybe_range_for_decl
)
13559 if (*maybe_range_for_decl
== NULL_TREE
)
13560 *maybe_range_for_decl
= error_mark_node
;
13563 /* Anything else is an error. */
13566 /* If we have already issued an error message we don't need
13567 to issue another one. */
13568 if ((decl
!= error_mark_node
13569 && DECL_INITIAL (decl
) != error_mark_node
)
13570 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13571 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13572 /* Skip tokens until we reach the end of the statement. */
13573 cp_parser_skip_to_end_of_statement (parser
);
13574 /* If the next token is now a `;', consume it. */
13575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13576 cp_lexer_consume_token (parser
->lexer
);
13584 bool auto_specifier_p
;
13585 /* NULL_TREE if both variable and function declaration are allowed,
13586 error_mark_node if function declaration are not allowed and
13587 a FUNCTION_DECL that should be diagnosed if it is followed by
13588 variable declarations. */
13589 tree auto_function_declaration
;
13591 last_type
= NULL_TREE
;
13593 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
13594 auto_function_declaration
= NULL_TREE
;
13596 /* Keep going until we hit the `;' at the end of the simple
13598 saw_declarator
= false;
13599 while (cp_lexer_next_token_is_not (parser
->lexer
,
13603 bool function_definition_p
;
13605 tree auto_result
= NULL_TREE
;
13607 if (saw_declarator
)
13609 /* If we are processing next declarator, comma is expected */
13610 token
= cp_lexer_peek_token (parser
->lexer
);
13611 gcc_assert (token
->type
== CPP_COMMA
);
13612 cp_lexer_consume_token (parser
->lexer
);
13613 if (maybe_range_for_decl
)
13615 *maybe_range_for_decl
= error_mark_node
;
13616 if (comma_loc
== UNKNOWN_LOCATION
)
13617 comma_loc
= token
->location
;
13621 saw_declarator
= true;
13623 /* Parse the init-declarator. */
13624 decl
= cp_parser_init_declarator (parser
,
13625 CP_PARSER_FLAGS_NONE
,
13628 function_definition_allowed_p
,
13629 /*member_p=*/false,
13630 declares_class_or_enum
,
13631 &function_definition_p
,
13632 maybe_range_for_decl
,
13635 /* If an error occurred while parsing tentatively, exit quickly.
13636 (That usually happens when in the body of a function; each
13637 statement is treated as a declaration-statement until proven
13639 if (cp_parser_error_occurred (parser
))
13642 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
13644 /* If the init-declarator-list contains more than one
13645 init-declarator, they shall all form declarations of
13647 if (auto_function_declaration
== NULL_TREE
)
13648 auto_function_declaration
13649 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
13650 else if (TREE_CODE (decl
) == FUNCTION_DECL
13651 || auto_function_declaration
!= error_mark_node
)
13653 error_at (decl_specifiers
.locations
[ds_type_spec
],
13654 "non-variable %qD in declaration with more than one "
13655 "declarator with placeholder type",
13656 TREE_CODE (decl
) == FUNCTION_DECL
13657 ? decl
: auto_function_declaration
);
13658 auto_function_declaration
= error_mark_node
;
13663 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
13666 && last_type
!= error_mark_node
13667 && !same_type_p (auto_result
, last_type
))
13669 /* If the list of declarators contains more than one declarator,
13670 the type of each declared variable is determined as described
13671 above. If the type deduced for the template parameter U is not
13672 the same in each deduction, the program is ill-formed. */
13673 error_at (decl_specifiers
.locations
[ds_type_spec
],
13674 "inconsistent deduction for %qT: %qT and then %qT",
13675 decl_specifiers
.type
, last_type
, auto_result
);
13676 last_type
= error_mark_node
;
13679 last_type
= auto_result
;
13682 /* Handle function definitions specially. */
13683 if (function_definition_p
)
13685 /* If the next token is a `,', then we are probably
13686 processing something like:
13690 which is erroneous. */
13691 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13693 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13694 error_at (token
->location
,
13696 " declarations and function-definitions is forbidden");
13698 /* Otherwise, we're done with the list of declarators. */
13701 pop_deferring_access_checks ();
13705 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
13706 *maybe_range_for_decl
= decl
;
13707 /* The next token should be either a `,' or a `;'. */
13708 token
= cp_lexer_peek_token (parser
->lexer
);
13709 /* If it's a `,', there are more declarators to come. */
13710 if (token
->type
== CPP_COMMA
)
13711 /* will be consumed next time around */;
13712 /* If it's a `;', we are done. */
13713 else if (token
->type
== CPP_SEMICOLON
)
13715 else if (maybe_range_for_decl
)
13717 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
13718 permerror (decl_specifiers
.locations
[ds_type_spec
],
13719 "types may not be defined in a for-range-declaration");
13722 /* Anything else is an error. */
13725 /* If we have already issued an error message we don't need
13726 to issue another one. */
13727 if ((decl
!= error_mark_node
13728 && DECL_INITIAL (decl
) != error_mark_node
)
13729 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
13730 cp_parser_error (parser
, "expected %<,%> or %<;%>");
13731 /* Skip tokens until we reach the end of the statement. */
13732 cp_parser_skip_to_end_of_statement (parser
);
13733 /* If the next token is now a `;', consume it. */
13734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13735 cp_lexer_consume_token (parser
->lexer
);
13738 /* After the first time around, a function-definition is not
13739 allowed -- even if it was OK at first. For example:
13744 function_definition_allowed_p
= false;
13747 /* Issue an error message if no declarators are present, and the
13748 decl-specifier-seq does not itself declare a class or
13749 enumeration: [dcl.dcl]/3. */
13750 if (!saw_declarator
)
13752 if (cp_parser_declares_only_class_p (parser
))
13754 if (!declares_class_or_enum
13755 && decl_specifiers
.type
13756 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
13757 /* Ensure an error is issued anyway when finish_decltype_type,
13758 called via cp_parser_decl_specifier_seq, returns a class or
13759 an enumeration (c++/51786). */
13760 decl_specifiers
.type
= NULL_TREE
;
13761 shadow_tag (&decl_specifiers
);
13763 /* Perform any deferred access checks. */
13764 perform_deferred_access_checks (tf_warning_or_error
);
13767 /* Consume the `;'. */
13769 if (!maybe_range_for_decl
)
13770 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13771 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13773 if (init_loc
!= UNKNOWN_LOCATION
)
13774 error_at (init_loc
, "initializer in range-based %<for%> loop");
13775 if (comma_loc
!= UNKNOWN_LOCATION
)
13776 error_at (comma_loc
,
13777 "multiple declarations in range-based %<for%> loop");
13781 pop_deferring_access_checks ();
13784 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13785 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13789 cp_parser_decomposition_declaration (cp_parser
*parser
,
13790 cp_decl_specifier_seq
*decl_specifiers
,
13791 tree
*maybe_range_for_decl
,
13792 location_t
*init_loc
)
13794 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
13795 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13796 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
13798 /* Parse the identifier-list. */
13799 auto_vec
<cp_expr
, 10> v
;
13800 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13803 cp_expr e
= cp_parser_identifier (parser
);
13804 if (e
.get_value () == error_mark_node
)
13807 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
13809 cp_lexer_consume_token (parser
->lexer
);
13812 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13813 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
13815 end_loc
= UNKNOWN_LOCATION
;
13816 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
13818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
13819 cp_lexer_consume_token (parser
->lexer
);
13822 cp_parser_skip_to_end_of_statement (parser
);
13823 return error_mark_node
;
13827 if (cxx_dialect
< cxx17
)
13828 pedwarn (loc
, 0, "structured bindings only available with "
13829 "%<-std=c++17%> or %<-std=gnu++17%>");
13832 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
13833 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
13834 declarator
->id_loc
= loc
;
13835 if (ref_qual
!= REF_QUAL_NONE
)
13836 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
13837 ref_qual
== REF_QUAL_RVALUE
,
13839 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
13840 NULL_TREE
, decl_specifiers
->attributes
,
13842 tree orig_decl
= decl
;
13846 cp_decl_specifier_seq decl_specs
;
13847 clear_decl_specs (&decl_specs
);
13848 decl_specs
.type
= make_auto ();
13850 FOR_EACH_VEC_ELT (v
, i
, e
)
13853 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
13854 sfk_none
, e
.get_location ());
13857 declarator
->u
.id
.unqualified_name
= e
.get_value ();
13858 declarator
->id_loc
= e
.get_location ();
13860 tree elt_pushed_scope
;
13861 tree decl2
= start_decl (declarator
, &decl_specs
, SD_INITIALIZED
,
13862 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
13863 if (decl2
== error_mark_node
)
13864 decl
= error_mark_node
;
13865 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
13867 /* Ensure we've diagnosed redeclaration if we aren't creating
13869 gcc_assert (errorcount
);
13870 decl
= error_mark_node
;
13874 if (elt_pushed_scope
)
13875 pop_scope (elt_pushed_scope
);
13880 error_at (loc
, "empty structured binding declaration");
13881 decl
= error_mark_node
;
13884 if (maybe_range_for_decl
== NULL
13885 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
13887 bool non_constant_p
= false, is_direct_init
= false;
13888 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13889 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
13891 if (initializer
== NULL_TREE
13892 || (TREE_CODE (initializer
) == TREE_LIST
13893 && TREE_CHAIN (initializer
))
13895 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
13896 && CONSTRUCTOR_NELTS (initializer
) != 1))
13898 error_at (loc
, "invalid initializer for structured binding "
13900 initializer
= error_mark_node
;
13903 if (decl
!= error_mark_node
)
13905 int flags
= (decl_spec_seq_has_spec_p (decl_specifiers
, ds_constinit
)
13906 ? LOOKUP_CONSTINIT
: 0);
13907 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
13908 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
13909 (is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
)
13911 cp_finish_decomp (decl
, prev
, v
.length ());
13914 else if (decl
!= error_mark_node
)
13916 *maybe_range_for_decl
= prev
;
13917 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13918 the underlying DECL. */
13919 cp_finish_decomp (decl
, prev
, v
.length ());
13923 pop_scope (pushed_scope
);
13925 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
13927 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
13928 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
13934 /* Parse a decl-specifier-seq.
13936 decl-specifier-seq:
13937 decl-specifier-seq [opt] decl-specifier
13938 decl-specifier attribute-specifier-seq [opt] (C++11)
13941 storage-class-specifier
13952 Concepts Extension:
13957 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13959 The parser flags FLAGS is used to control type-specifier parsing.
13961 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13964 1: one of the decl-specifiers is an elaborated-type-specifier
13965 (i.e., a type declaration)
13966 2: one of the decl-specifiers is an enum-specifier or a
13967 class-specifier (i.e., a type definition)
13972 cp_parser_decl_specifier_seq (cp_parser
* parser
,
13973 cp_parser_flags flags
,
13974 cp_decl_specifier_seq
*decl_specs
,
13975 int* declares_class_or_enum
)
13977 bool constructor_possible_p
= !parser
->in_declarator_p
;
13978 bool found_decl_spec
= false;
13979 cp_token
*start_token
= NULL
;
13982 /* Clear DECL_SPECS. */
13983 clear_decl_specs (decl_specs
);
13985 /* Assume no class or enumeration type is declared. */
13986 *declares_class_or_enum
= 0;
13988 /* Keep reading specifiers until there are no more to read. */
13991 bool constructor_p
;
13995 /* Peek at the next token. */
13996 token
= cp_lexer_peek_token (parser
->lexer
);
13998 /* Save the first token of the decl spec list for error
14001 start_token
= token
;
14002 /* Handle attributes. */
14003 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) == 0
14004 && cp_next_tokens_can_be_attribute_p (parser
))
14006 /* Parse the attributes. */
14007 tree attrs
= cp_parser_attributes_opt (parser
);
14009 /* In a sequence of declaration specifiers, c++11 attributes
14010 appertain to the type that precede them. In that case
14013 The attribute-specifier-seq affects the type only for
14014 the declaration it appears in, not other declarations
14015 involving the same type.
14017 But for now let's force the user to position the
14018 attribute either at the beginning of the declaration or
14019 after the declarator-id, which would clearly mean that it
14020 applies to the declarator. */
14021 if (cxx11_attribute_p (attrs
))
14023 if (!found_decl_spec
)
14024 /* The c++11 attribute is at the beginning of the
14025 declaration. It appertains to the entity being
14029 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
14031 /* This is an attribute following a
14032 class-specifier. */
14033 if (decl_specs
->type_definition_p
)
14034 warn_misplaced_attr_for_class_type (token
->location
,
14040 decl_specs
->std_attributes
14041 = attr_chainon (decl_specs
->std_attributes
, attrs
);
14042 if (decl_specs
->locations
[ds_std_attribute
] == 0)
14043 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
14049 decl_specs
->attributes
14050 = attr_chainon (decl_specs
->attributes
, attrs
);
14051 if (decl_specs
->locations
[ds_attribute
] == 0)
14052 decl_specs
->locations
[ds_attribute
] = token
->location
;
14055 /* Assume we will find a decl-specifier keyword. */
14056 found_decl_spec
= true;
14057 /* If the next token is an appropriate keyword, we can simply
14058 add it to the list. */
14059 switch (token
->keyword
)
14066 if (!at_class_scope_p ())
14068 gcc_rich_location
richloc (token
->location
);
14069 richloc
.add_fixit_remove ();
14070 error_at (&richloc
, "%<friend%> used outside of class");
14071 cp_lexer_purge_token (parser
->lexer
);
14076 /* Consume the token. */
14077 cp_lexer_consume_token (parser
->lexer
);
14081 case RID_CONSTEXPR
:
14083 cp_lexer_consume_token (parser
->lexer
);
14086 case RID_CONSTINIT
:
14088 cp_lexer_consume_token (parser
->lexer
);
14091 case RID_CONSTEVAL
:
14093 cp_lexer_consume_token (parser
->lexer
);
14098 cp_lexer_consume_token (parser
->lexer
);
14100 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14103 /* Warn for concept as a decl-specifier. We'll rewrite these as
14104 concept declarations later. */
14105 if (!flag_concepts_ts
)
14107 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
14108 if (next
->keyword
== RID_BOOL
)
14109 pedwarn (next
->location
, 0, "the %<bool%> keyword is not "
14110 "allowed in a C++20 concept definition");
14112 pedwarn (token
->location
, 0, "C++20 concept definition syntax "
14113 "is %<concept <name> = <expr>%> ");
14116 /* In C++20 a concept definition is just 'concept name = expr;'
14117 Support that syntax as a TS extension by pretending we've seen
14118 the 'bool' specifier. */
14119 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14120 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
)
14121 && !decl_specs
->any_type_specifiers_p
)
14123 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
14124 token
, /*type_definition*/false);
14125 decl_specs
->any_type_specifiers_p
= true;
14129 /* function-specifier:
14136 cp_parser_function_specifier_opt (parser
, decl_specs
);
14143 /* Consume the token. */
14144 cp_lexer_consume_token (parser
->lexer
);
14146 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14149 /* A constructor declarator cannot appear in a typedef. */
14150 constructor_possible_p
= false;
14151 /* The "typedef" keyword can only occur in a declaration; we
14152 may as well commit at this point. */
14153 cp_parser_commit_to_tentative_parse (parser
);
14155 if (decl_specs
->storage_class
!= sc_none
)
14156 decl_specs
->conflicting_specifiers_p
= true;
14159 /* storage-class-specifier:
14169 if (cxx_dialect
== cxx98
)
14171 /* Consume the token. */
14172 cp_lexer_consume_token (parser
->lexer
);
14174 /* Complain about `auto' as a storage specifier, if
14175 we're complaining about C++0x compatibility. */
14176 gcc_rich_location
richloc (token
->location
);
14177 richloc
.add_fixit_remove ();
14178 warning_at (&richloc
, OPT_Wc__11_compat
,
14179 "%<auto%> changes meaning in C++11; "
14180 "please remove it");
14182 /* Set the storage class anyway. */
14183 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14187 /* C++0x auto type-specifier. */
14188 found_decl_spec
= false;
14195 /* Consume the token. */
14196 cp_lexer_consume_token (parser
->lexer
);
14197 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14201 /* Consume the token. */
14203 cp_lexer_consume_token (parser
->lexer
);
14207 /* We did not yet find a decl-specifier yet. */
14208 found_decl_spec
= false;
14212 if (found_decl_spec
14213 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14214 && token
->keyword
!= RID_CONSTEXPR
)
14215 error ("%<decl-specifier%> invalid in condition");
14217 if (found_decl_spec
14218 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14219 && token
->keyword
!= RID_MUTABLE
14220 && token
->keyword
!= RID_CONSTEXPR
14221 && token
->keyword
!= RID_CONSTEVAL
)
14222 error_at (token
->location
, "%qD invalid in lambda",
14223 ridpointers
[token
->keyword
]);
14226 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14228 /* Constructors are a special case. The `S' in `S()' is not a
14229 decl-specifier; it is the beginning of the declarator. */
14231 = (!found_decl_spec
14232 && constructor_possible_p
14233 && (cp_parser_constructor_declarator_p
14234 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14237 /* If we don't have a DECL_SPEC yet, then we must be looking at
14238 a type-specifier. */
14239 if (!found_decl_spec
&& !constructor_p
)
14241 int decl_spec_declares_class_or_enum
;
14242 bool is_cv_qualifier
;
14245 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14246 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
14249 = cp_parser_type_specifier (parser
, flags
,
14251 /*is_declaration=*/true,
14252 &decl_spec_declares_class_or_enum
,
14254 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
14256 /* If this type-specifier referenced a user-defined type
14257 (a typedef, class-name, etc.), then we can't allow any
14258 more such type-specifiers henceforth.
14262 The longest sequence of decl-specifiers that could
14263 possibly be a type name is taken as the
14264 decl-specifier-seq of a declaration. The sequence shall
14265 be self-consistent as described below.
14269 As a general rule, at most one type-specifier is allowed
14270 in the complete decl-specifier-seq of a declaration. The
14271 only exceptions are the following:
14273 -- const or volatile can be combined with any other
14276 -- signed or unsigned can be combined with char, long,
14284 void g (const int Pc);
14286 Here, Pc is *not* part of the decl-specifier seq; it's
14287 the declarator. Therefore, once we see a type-specifier
14288 (other than a cv-qualifier), we forbid any additional
14289 user-defined types. We *do* still allow things like `int
14290 int' to be considered a decl-specifier-seq, and issue the
14291 error message later. */
14292 if (type_spec
&& !is_cv_qualifier
)
14293 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
14294 /* A constructor declarator cannot follow a type-specifier. */
14297 constructor_possible_p
= false;
14298 found_decl_spec
= true;
14299 if (!is_cv_qualifier
)
14300 decl_specs
->any_type_specifiers_p
= true;
14302 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
14303 error_at (token
->location
, "type-specifier invalid in lambda");
14307 /* If we still do not have a DECL_SPEC, then there are no more
14308 decl-specifiers. */
14309 if (!found_decl_spec
)
14312 decl_specs
->any_specifiers_p
= true;
14313 /* After we see one decl-specifier, further decl-specifiers are
14314 always optional. */
14315 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
14318 /* Don't allow a friend specifier with a class definition. */
14319 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
14320 && (*declares_class_or_enum
& 2))
14321 error_at (decl_specs
->locations
[ds_friend
],
14322 "class definition may not be declared a friend");
14325 /* Parse an (optional) storage-class-specifier.
14327 storage-class-specifier:
14336 storage-class-specifier:
14339 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
14342 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
14344 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
14347 if (cxx_dialect
!= cxx98
)
14349 /* Fall through for C++98. */
14350 gcc_fallthrough ();
14357 /* Consume the token. */
14358 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14365 /* Parse an (optional) function-specifier.
14367 function-specifier:
14373 explicit(constant-expression)
14375 Returns an IDENTIFIER_NODE corresponding to the keyword used.
14376 Updates DECL_SPECS, if it is non-NULL. */
14379 cp_parser_function_specifier_opt (cp_parser
* parser
,
14380 cp_decl_specifier_seq
*decl_specs
)
14382 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14383 switch (token
->keyword
)
14386 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
14390 /* 14.5.2.3 [temp.mem]
14392 A member function template shall not be virtual. */
14393 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14394 && current_class_type
)
14395 error_at (token
->location
, "templates may not be %<virtual%>");
14397 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
14402 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14403 /* If we see '(', it's C++20 explicit(bool). */
14405 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
14407 matching_parens parens
;
14408 parens
.consume_open (parser
);
14410 /* New types are not allowed in an explicit-specifier. */
14411 const char *saved_message
14412 = parser
->type_definition_forbidden_message
;
14413 parser
->type_definition_forbidden_message
14414 = G_("types may not be defined in explicit-specifier");
14416 if (cxx_dialect
< cxx2a
)
14417 pedwarn (token
->location
, 0,
14418 "%<explicit(bool)%> only available with %<-std=c++2a%> "
14419 "or %<-std=gnu++2a%>");
14421 /* Parse the constant-expression. */
14422 expr
= cp_parser_constant_expression (parser
);
14424 /* Restore the saved message. */
14425 parser
->type_definition_forbidden_message
= saved_message
;
14426 parens
.require_close (parser
);
14429 /* The explicit-specifier explicit without a constant-expression is
14430 equivalent to the explicit-specifier explicit(true). */
14431 expr
= boolean_true_node
;
14434 "the constant-expression, if supplied, shall be a contextually
14435 converted constant expression of type bool." */
14436 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
14437 /* We could evaluate it -- mark the decl as appropriate. */
14438 if (expr
== boolean_true_node
)
14439 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
14440 else if (expr
== boolean_false_node
)
14441 /* Don't mark the decl as explicit. */;
14442 else if (decl_specs
)
14443 /* The expression was value-dependent. Remember it so that we can
14444 substitute it later. */
14445 decl_specs
->explicit_specifier
= expr
;
14453 /* Consume the token. */
14454 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
14457 /* Parse a linkage-specification.
14459 linkage-specification:
14460 extern string-literal { declaration-seq [opt] }
14461 extern string-literal declaration */
14464 cp_parser_linkage_specification (cp_parser
* parser
)
14468 /* Look for the `extern' keyword. */
14469 cp_token
*extern_token
14470 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
14472 /* Look for the string-literal. */
14473 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
14474 linkage
= cp_parser_string_literal (parser
, false, false);
14476 /* Transform the literal into an identifier. If the literal is a
14477 wide-character string, or contains embedded NULs, then we can't
14478 handle it as the user wants. */
14479 if (strlen (TREE_STRING_POINTER (linkage
))
14480 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
14482 cp_parser_error (parser
, "invalid linkage-specification");
14483 /* Assume C++ linkage. */
14484 linkage
= lang_name_cplusplus
;
14487 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
14489 /* We're now using the new linkage. */
14490 push_lang_context (linkage
);
14492 /* Preserve the location of the the innermost linkage specification,
14493 tracking the locations of nested specifications via a local. */
14494 location_t saved_location
14495 = parser
->innermost_linkage_specification_location
;
14496 /* Construct a location ranging from the start of the "extern" to
14497 the end of the string-literal, with the caret at the start, e.g.:
14501 parser
->innermost_linkage_specification_location
14502 = make_location (extern_token
->location
,
14503 extern_token
->location
,
14504 get_finish (string_token
->location
));
14506 /* If the next token is a `{', then we're using the first
14508 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
14510 cp_ensure_no_omp_declare_simd (parser
);
14511 cp_ensure_no_oacc_routine (parser
);
14513 /* Consume the `{' token. */
14514 matching_braces braces
;
14515 braces
.consume_open (parser
);
14516 /* Parse the declarations. */
14517 cp_parser_declaration_seq_opt (parser
);
14518 /* Look for the closing `}'. */
14519 braces
.require_close (parser
);
14521 /* Otherwise, there's just one declaration. */
14524 bool saved_in_unbraced_linkage_specification_p
;
14526 saved_in_unbraced_linkage_specification_p
14527 = parser
->in_unbraced_linkage_specification_p
;
14528 parser
->in_unbraced_linkage_specification_p
= true;
14529 cp_parser_declaration (parser
);
14530 parser
->in_unbraced_linkage_specification_p
14531 = saved_in_unbraced_linkage_specification_p
;
14534 /* We're done with the linkage-specification. */
14535 pop_lang_context ();
14537 /* Restore location of parent linkage specification, if any. */
14538 parser
->innermost_linkage_specification_location
= saved_location
;
14541 /* Parse a static_assert-declaration.
14543 static_assert-declaration:
14544 static_assert ( constant-expression , string-literal ) ;
14545 static_assert ( constant-expression ) ; (C++17)
14547 If MEMBER_P, this static_assert is a class member. */
14550 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
14553 location_t token_loc
;
14557 /* Peek at the `static_assert' token so we can keep track of exactly
14558 where the static assertion started. */
14559 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14561 /* Look for the `static_assert' keyword. */
14562 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
14566 /* We know we are in a static assertion; commit to any tentative
14568 if (cp_parser_parsing_tentatively (parser
))
14569 cp_parser_commit_to_tentative_parse (parser
);
14571 /* Parse the `(' starting the static assertion condition. */
14572 matching_parens parens
;
14573 parens
.require_open (parser
);
14575 /* Parse the constant-expression. Allow a non-constant expression
14576 here in order to give better diagnostics in finish_static_assert. */
14578 cp_parser_constant_expression (parser
,
14579 /*allow_non_constant_p=*/true,
14580 /*non_constant_p=*/&dummy
);
14582 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14584 if (cxx_dialect
< cxx17
)
14585 pedwarn (input_location
, OPT_Wpedantic
,
14586 "%<static_assert%> without a message "
14587 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
14589 cp_lexer_consume_token (parser
->lexer
);
14590 message
= build_string (1, "");
14591 TREE_TYPE (message
) = char_array_type_node
;
14592 fix_string_type (message
);
14596 /* Parse the separating `,'. */
14597 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
14599 /* Parse the string-literal message. */
14600 message
= cp_parser_string_literal (parser
,
14601 /*translate=*/false,
14604 /* A `)' completes the static assertion. */
14605 if (!parens
.require_close (parser
))
14606 cp_parser_skip_to_closing_parenthesis (parser
,
14607 /*recovering=*/true,
14608 /*or_comma=*/false,
14609 /*consume_paren=*/true);
14612 /* A semicolon terminates the declaration. */
14613 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14615 /* Get the location for the static assertion. Use that of the
14616 condition if available, otherwise, use that of the "static_assert"
14618 location_t assert_loc
= condition
.get_location ();
14619 if (assert_loc
== UNKNOWN_LOCATION
)
14620 assert_loc
= token_loc
;
14622 /* Complete the static assertion, which may mean either processing
14623 the static assert now or saving it for template instantiation. */
14624 finish_static_assert (condition
, message
, assert_loc
, member_p
);
14627 /* Parse the expression in decltype ( expression ). */
14630 cp_parser_decltype_expr (cp_parser
*parser
,
14631 bool &id_expression_or_member_access_p
)
14633 cp_token
*id_expr_start_token
;
14636 /* Since we're going to preserve any side-effects from this parse, set up a
14637 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14638 in the expression. */
14639 tentative_firewall
firewall (parser
);
14641 /* First, try parsing an id-expression. */
14642 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
14643 cp_parser_parse_tentatively (parser
);
14644 expr
= cp_parser_id_expression (parser
,
14645 /*template_keyword_p=*/false,
14646 /*check_dependency_p=*/true,
14647 /*template_p=*/NULL
,
14648 /*declarator_p=*/false,
14649 /*optional_p=*/false);
14651 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
14653 bool non_integral_constant_expression_p
= false;
14654 tree id_expression
= expr
;
14656 const char *error_msg
;
14658 if (identifier_p (expr
))
14659 /* Lookup the name we got back from the id-expression. */
14660 expr
= cp_parser_lookup_name_simple (parser
, expr
,
14661 id_expr_start_token
->location
);
14663 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
14664 /* A template without args is not a complete id-expression. */
14665 expr
= error_mark_node
;
14668 && expr
!= error_mark_node
14669 && TREE_CODE (expr
) != TYPE_DECL
14670 && (TREE_CODE (expr
) != BIT_NOT_EXPR
14671 || !TYPE_P (TREE_OPERAND (expr
, 0)))
14672 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14674 /* Complete lookup of the id-expression. */
14675 expr
= (finish_id_expression
14676 (id_expression
, expr
, parser
->scope
, &idk
,
14677 /*integral_constant_expression_p=*/false,
14678 /*allow_non_integral_constant_expression_p=*/true,
14679 &non_integral_constant_expression_p
,
14680 /*template_p=*/false,
14682 /*address_p=*/false,
14683 /*template_arg_p=*/false,
14685 id_expr_start_token
->location
));
14687 if (expr
== error_mark_node
)
14688 /* We found an id-expression, but it was something that we
14689 should not have found. This is an error, not something
14690 we can recover from, so note that we found an
14691 id-expression and we'll recover as gracefully as
14693 id_expression_or_member_access_p
= true;
14697 && expr
!= error_mark_node
14698 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14699 /* We have an id-expression. */
14700 id_expression_or_member_access_p
= true;
14703 if (!id_expression_or_member_access_p
)
14705 /* Abort the id-expression parse. */
14706 cp_parser_abort_tentative_parse (parser
);
14708 /* Parsing tentatively, again. */
14709 cp_parser_parse_tentatively (parser
);
14711 /* Parse a class member access. */
14712 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
14713 /*cast_p=*/false, /*decltype*/true,
14714 /*member_access_only_p=*/true, NULL
);
14717 && expr
!= error_mark_node
14718 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
14719 /* We have an id-expression. */
14720 id_expression_or_member_access_p
= true;
14723 if (id_expression_or_member_access_p
)
14724 /* We have parsed the complete id-expression or member access. */
14725 cp_parser_parse_definitely (parser
);
14728 /* Abort our attempt to parse an id-expression or member access
14730 cp_parser_abort_tentative_parse (parser
);
14732 /* Commit to the tentative_firewall so we get syntax errors. */
14733 cp_parser_commit_to_tentative_parse (parser
);
14735 /* Parse a full expression. */
14736 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
14737 /*decltype_p=*/true);
14743 /* Parse a `decltype' type. Returns the type.
14745 simple-type-specifier:
14746 decltype ( expression )
14748 decltype ( auto ) */
14751 cp_parser_decltype (cp_parser
*parser
)
14753 bool id_expression_or_member_access_p
= false;
14754 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
14756 if (start_token
->type
== CPP_DECLTYPE
)
14758 /* Already parsed. */
14759 cp_lexer_consume_token (parser
->lexer
);
14760 return saved_checks_value (start_token
->u
.tree_check_value
);
14763 /* Look for the `decltype' token. */
14764 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
14765 return error_mark_node
;
14767 /* Parse the opening `('. */
14768 matching_parens parens
;
14769 if (!parens
.require_open (parser
))
14770 return error_mark_node
;
14772 push_deferring_access_checks (dk_deferred
);
14774 tree expr
= NULL_TREE
;
14776 if (cxx_dialect
>= cxx14
14777 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
14778 /* decltype (auto) */
14779 cp_lexer_consume_token (parser
->lexer
);
14782 /* decltype (expression) */
14784 /* Types cannot be defined in a `decltype' expression. Save away the
14785 old message and set the new one. */
14786 const char *saved_message
= parser
->type_definition_forbidden_message
;
14787 parser
->type_definition_forbidden_message
14788 = G_("types may not be defined in %<decltype%> expressions");
14790 /* The restrictions on constant-expressions do not apply inside
14791 decltype expressions. */
14792 bool saved_integral_constant_expression_p
14793 = parser
->integral_constant_expression_p
;
14794 bool saved_non_integral_constant_expression_p
14795 = parser
->non_integral_constant_expression_p
;
14796 parser
->integral_constant_expression_p
= false;
14798 /* Within a parenthesized expression, a `>' token is always
14799 the greater-than operator. */
14800 bool saved_greater_than_is_operator_p
14801 = parser
->greater_than_is_operator_p
;
14802 parser
->greater_than_is_operator_p
= true;
14804 /* Do not actually evaluate the expression. */
14805 ++cp_unevaluated_operand
;
14807 /* Do not warn about problems with the expression. */
14808 ++c_inhibit_evaluation_warnings
;
14810 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
14811 STRIP_ANY_LOCATION_WRAPPER (expr
);
14813 /* Go back to evaluating expressions. */
14814 --cp_unevaluated_operand
;
14815 --c_inhibit_evaluation_warnings
;
14817 /* The `>' token might be the end of a template-id or
14818 template-parameter-list now. */
14819 parser
->greater_than_is_operator_p
14820 = saved_greater_than_is_operator_p
;
14822 /* Restore the old message and the integral constant expression
14824 parser
->type_definition_forbidden_message
= saved_message
;
14825 parser
->integral_constant_expression_p
14826 = saved_integral_constant_expression_p
;
14827 parser
->non_integral_constant_expression_p
14828 = saved_non_integral_constant_expression_p
;
14831 /* Parse to the closing `)'. */
14832 if (!parens
.require_close (parser
))
14834 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
14835 /*consume_paren=*/true);
14836 pop_deferring_access_checks ();
14837 return error_mark_node
;
14843 expr
= make_decltype_auto ();
14844 AUTO_IS_DECLTYPE (expr
) = true;
14847 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
14848 tf_warning_or_error
);
14850 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14852 start_token
->type
= CPP_DECLTYPE
;
14853 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
14854 start_token
->tree_check_p
= true;
14855 start_token
->u
.tree_check_value
->value
= expr
;
14856 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
14857 start_token
->keyword
= RID_MAX
;
14858 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
14860 pop_to_parent_deferring_access_checks ();
14865 /* Special member functions [gram.special] */
14867 /* Parse a conversion-function-id.
14869 conversion-function-id:
14870 operator conversion-type-id
14872 Returns an IDENTIFIER_NODE representing the operator. */
14875 cp_parser_conversion_function_id (cp_parser
* parser
)
14879 tree saved_qualifying_scope
;
14880 tree saved_object_scope
;
14881 tree pushed_scope
= NULL_TREE
;
14883 /* Look for the `operator' token. */
14884 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
14885 return error_mark_node
;
14886 /* When we parse the conversion-type-id, the current scope will be
14887 reset. However, we need that information in able to look up the
14888 conversion function later, so we save it here. */
14889 saved_scope
= parser
->scope
;
14890 saved_qualifying_scope
= parser
->qualifying_scope
;
14891 saved_object_scope
= parser
->object_scope
;
14892 /* We must enter the scope of the class so that the names of
14893 entities declared within the class are available in the
14894 conversion-type-id. For example, consider:
14901 S::operator I() { ... }
14903 In order to see that `I' is a type-name in the definition, we
14904 must be in the scope of `S'. */
14906 pushed_scope
= push_scope (saved_scope
);
14907 /* Parse the conversion-type-id. */
14908 type
= cp_parser_conversion_type_id (parser
);
14909 /* Leave the scope of the class, if any. */
14911 pop_scope (pushed_scope
);
14912 /* Restore the saved scope. */
14913 parser
->scope
= saved_scope
;
14914 parser
->qualifying_scope
= saved_qualifying_scope
;
14915 parser
->object_scope
= saved_object_scope
;
14916 /* If the TYPE is invalid, indicate failure. */
14917 if (type
== error_mark_node
)
14918 return error_mark_node
;
14919 return make_conv_op_name (type
);
14922 /* Parse a conversion-type-id:
14924 conversion-type-id:
14925 type-specifier-seq conversion-declarator [opt]
14927 Returns the TYPE specified. */
14930 cp_parser_conversion_type_id (cp_parser
* parser
)
14933 cp_decl_specifier_seq type_specifiers
;
14934 cp_declarator
*declarator
;
14935 tree type_specified
;
14936 const char *saved_message
;
14938 /* Parse the attributes. */
14939 attributes
= cp_parser_attributes_opt (parser
);
14941 saved_message
= parser
->type_definition_forbidden_message
;
14942 parser
->type_definition_forbidden_message
14943 = G_("types may not be defined in a conversion-type-id");
14945 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
14946 optional in conversion-type-id. */
14947 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
14948 /*is_declaration=*/false,
14949 /*is_trailing_return=*/false,
14952 parser
->type_definition_forbidden_message
= saved_message
;
14954 /* If that didn't work, stop. */
14955 if (type_specifiers
.type
== error_mark_node
)
14956 return error_mark_node
;
14957 /* Parse the conversion-declarator. */
14958 declarator
= cp_parser_conversion_declarator_opt (parser
);
14960 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
14961 /*initialized=*/0, &attributes
);
14963 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
14965 /* Don't give this error when parsing tentatively. This happens to
14966 work because we always parse this definitively once. */
14967 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
14968 && type_uses_auto (type_specified
))
14970 if (cxx_dialect
< cxx14
)
14972 error ("invalid use of %<auto%> in conversion operator");
14973 return error_mark_node
;
14975 else if (template_parm_scope_p ())
14976 warning (0, "use of %<auto%> in member template "
14977 "conversion operator can never be deduced");
14980 return type_specified
;
14983 /* Parse an (optional) conversion-declarator.
14985 conversion-declarator:
14986 ptr-operator conversion-declarator [opt]
14990 static cp_declarator
*
14991 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
14993 enum tree_code code
;
14994 tree class_type
, std_attributes
= NULL_TREE
;
14995 cp_cv_quals cv_quals
;
14997 /* We don't know if there's a ptr-operator next, or not. */
14998 cp_parser_parse_tentatively (parser
);
14999 /* Try the ptr-operator. */
15000 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
15002 /* If it worked, look for more conversion-declarators. */
15003 if (cp_parser_parse_definitely (parser
))
15005 cp_declarator
*declarator
;
15007 /* Parse another optional declarator. */
15008 declarator
= cp_parser_conversion_declarator_opt (parser
);
15010 declarator
= cp_parser_make_indirect_declarator
15011 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
15019 /* Parse an (optional) ctor-initializer.
15022 : mem-initializer-list */
15025 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
15027 /* If the next token is not a `:', then there is no
15028 ctor-initializer. */
15029 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
15031 /* Do default initialization of any bases and members. */
15032 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15033 finish_mem_initializers (NULL_TREE
);
15037 /* Consume the `:' token. */
15038 cp_lexer_consume_token (parser
->lexer
);
15039 /* And the mem-initializer-list. */
15040 cp_parser_mem_initializer_list (parser
);
15043 /* Parse a mem-initializer-list.
15045 mem-initializer-list:
15046 mem-initializer ... [opt]
15047 mem-initializer ... [opt] , mem-initializer-list */
15050 cp_parser_mem_initializer_list (cp_parser
* parser
)
15052 tree mem_initializer_list
= NULL_TREE
;
15053 tree target_ctor
= error_mark_node
;
15054 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15056 /* Let the semantic analysis code know that we are starting the
15057 mem-initializer-list. */
15058 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
15059 error_at (token
->location
,
15060 "only constructors take member initializers");
15062 /* Loop through the list. */
15065 tree mem_initializer
;
15067 token
= cp_lexer_peek_token (parser
->lexer
);
15068 /* Parse the mem-initializer. */
15069 mem_initializer
= cp_parser_mem_initializer (parser
);
15070 /* If the next token is a `...', we're expanding member initializers. */
15071 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15073 || (mem_initializer
!= error_mark_node
15074 && check_for_bare_parameter_packs (TREE_PURPOSE
15075 (mem_initializer
))))
15077 /* Consume the `...'. */
15079 cp_lexer_consume_token (parser
->lexer
);
15081 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15082 can be expanded but members cannot. */
15083 if (mem_initializer
!= error_mark_node
15084 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
15086 error_at (token
->location
,
15087 "cannot expand initializer for member %qD",
15088 TREE_PURPOSE (mem_initializer
));
15089 mem_initializer
= error_mark_node
;
15092 /* Construct the pack expansion type. */
15093 if (mem_initializer
!= error_mark_node
)
15094 mem_initializer
= make_pack_expansion (mem_initializer
);
15096 if (target_ctor
!= error_mark_node
15097 && mem_initializer
!= error_mark_node
)
15099 error ("mem-initializer for %qD follows constructor delegation",
15100 TREE_PURPOSE (mem_initializer
));
15101 mem_initializer
= error_mark_node
;
15103 /* Look for a target constructor. */
15104 if (mem_initializer
!= error_mark_node
15105 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
15106 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
15108 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
15109 if (mem_initializer_list
)
15111 error ("constructor delegation follows mem-initializer for %qD",
15112 TREE_PURPOSE (mem_initializer_list
));
15113 mem_initializer
= error_mark_node
;
15115 target_ctor
= mem_initializer
;
15117 /* Add it to the list, unless it was erroneous. */
15118 if (mem_initializer
!= error_mark_node
)
15120 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
15121 mem_initializer_list
= mem_initializer
;
15123 /* If the next token is not a `,', we're done. */
15124 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15126 /* Consume the `,' token. */
15127 cp_lexer_consume_token (parser
->lexer
);
15130 /* Perform semantic analysis. */
15131 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15132 finish_mem_initializers (mem_initializer_list
);
15135 /* Parse a mem-initializer.
15138 mem-initializer-id ( expression-list [opt] )
15139 mem-initializer-id braced-init-list
15144 ( expression-list [opt] )
15146 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15147 class) or FIELD_DECL (for a non-static data member) to initialize;
15148 the TREE_VALUE is the expression-list. An empty initialization
15149 list is represented by void_list_node. */
15152 cp_parser_mem_initializer (cp_parser
* parser
)
15154 tree mem_initializer_id
;
15155 tree expression_list
;
15157 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15159 /* Find out what is being initialized. */
15160 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15162 permerror (token
->location
,
15163 "anachronistic old-style base class initializer");
15164 mem_initializer_id
= NULL_TREE
;
15168 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15169 if (mem_initializer_id
== error_mark_node
)
15170 return mem_initializer_id
;
15172 member
= expand_member_init (mem_initializer_id
);
15173 if (member
&& !DECL_P (member
))
15174 in_base_initializer
= 1;
15176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15178 bool expr_non_constant_p
;
15179 cp_lexer_set_source_position (parser
->lexer
);
15180 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15181 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15182 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15183 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15187 vec
<tree
, va_gc
> *vec
;
15188 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15190 /*allow_expansion_p=*/true,
15191 /*non_constant_p=*/NULL
,
15192 /*close_paren_loc=*/NULL
,
15193 /*wrap_locations_p=*/true);
15195 return error_mark_node
;
15196 expression_list
= build_tree_list_vec (vec
);
15197 release_tree_vector (vec
);
15200 if (expression_list
== error_mark_node
)
15201 return error_mark_node
;
15202 if (!expression_list
)
15203 expression_list
= void_type_node
;
15205 in_base_initializer
= 0;
15207 return member
? build_tree_list (member
, expression_list
) : error_mark_node
;
15210 /* Parse a mem-initializer-id.
15212 mem-initializer-id:
15213 :: [opt] nested-name-specifier [opt] class-name
15214 decltype-specifier (C++11)
15217 Returns a TYPE indicating the class to be initialized for the first
15218 production (and the second in C++11). Returns an IDENTIFIER_NODE
15219 indicating the data member to be initialized for the last production. */
15222 cp_parser_mem_initializer_id (cp_parser
* parser
)
15224 bool global_scope_p
;
15225 bool nested_name_specifier_p
;
15226 bool template_p
= false;
15229 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15231 /* `typename' is not allowed in this context ([temp.res]). */
15232 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
15234 error_at (token
->location
,
15235 "keyword %<typename%> not allowed in this context (a qualified "
15236 "member initializer is implicitly a type)");
15237 cp_lexer_consume_token (parser
->lexer
);
15239 /* Look for the optional `::' operator. */
15241 = (cp_parser_global_scope_opt (parser
,
15242 /*current_scope_valid_p=*/false)
15244 /* Look for the optional nested-name-specifier. The simplest way to
15249 The keyword `typename' is not permitted in a base-specifier or
15250 mem-initializer; in these contexts a qualified name that
15251 depends on a template-parameter is implicitly assumed to be a
15254 is to assume that we have seen the `typename' keyword at this
15256 nested_name_specifier_p
15257 = (cp_parser_nested_name_specifier_opt (parser
,
15258 /*typename_keyword_p=*/true,
15259 /*check_dependency_p=*/true,
15261 /*is_declaration=*/true)
15263 if (nested_name_specifier_p
)
15264 template_p
= cp_parser_optional_template_keyword (parser
);
15265 /* If there is a `::' operator or a nested-name-specifier, then we
15266 are definitely looking for a class-name. */
15267 if (global_scope_p
|| nested_name_specifier_p
)
15268 return cp_parser_class_name (parser
,
15269 /*typename_keyword_p=*/true,
15270 /*template_keyword_p=*/template_p
,
15272 /*check_dependency_p=*/true,
15273 /*class_head_p=*/false,
15274 /*is_declaration=*/true);
15275 /* Otherwise, we could also be looking for an ordinary identifier. */
15276 cp_parser_parse_tentatively (parser
);
15277 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
15278 /* Try a decltype-specifier. */
15279 id
= cp_parser_decltype (parser
);
15281 /* Otherwise, try a class-name. */
15282 id
= cp_parser_class_name (parser
,
15283 /*typename_keyword_p=*/true,
15284 /*template_keyword_p=*/false,
15286 /*check_dependency_p=*/true,
15287 /*class_head_p=*/false,
15288 /*is_declaration=*/true);
15289 /* If we found one, we're done. */
15290 if (cp_parser_parse_definitely (parser
))
15292 /* Otherwise, look for an ordinary identifier. */
15293 return cp_parser_identifier (parser
);
15296 /* Overloading [gram.over] */
15298 /* Parse an operator-function-id.
15300 operator-function-id:
15303 Returns an IDENTIFIER_NODE for the operator which is a
15304 human-readable spelling of the identifier, e.g., `operator +'. */
15307 cp_parser_operator_function_id (cp_parser
* parser
)
15309 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15310 /* Look for the `operator' keyword. */
15311 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15312 return error_mark_node
;
15313 /* And then the name of the operator itself. */
15314 return cp_parser_operator (parser
, start_loc
);
15317 /* Return an identifier node for a user-defined literal operator.
15318 The suffix identifier is chained to the operator name identifier. */
15321 cp_literal_operator_id (const char* name
)
15324 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
15325 + strlen (name
) + 10);
15326 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
15327 identifier
= get_identifier (buffer
);
15332 /* Parse an operator.
15335 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
15336 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
15337 || ++ -- , ->* -> () []
15344 Returns an IDENTIFIER_NODE for the operator which is a
15345 human-readable spelling of the identifier, e.g., `operator +'. */
15348 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
15350 tree id
= NULL_TREE
;
15354 /* Peek at the next token. */
15355 token
= cp_lexer_peek_token (parser
->lexer
);
15357 location_t end_loc
= token
->location
;
15359 /* Figure out which operator we have. */
15360 enum tree_code op
= ERROR_MARK
;
15361 bool assop
= false;
15362 bool consumed
= false;
15363 switch (token
->type
)
15367 /* The keyword should be either `new' or `delete'. */
15368 if (token
->keyword
== RID_NEW
)
15370 else if (token
->keyword
== RID_DELETE
)
15375 /* Consume the `new' or `delete' token. */
15376 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
15378 /* Peek at the next token. */
15379 token
= cp_lexer_peek_token (parser
->lexer
);
15380 /* If it's a `[' token then this is the array variant of the
15382 if (token
->type
== CPP_OPEN_SQUARE
)
15384 /* Consume the `[' token. */
15385 cp_lexer_consume_token (parser
->lexer
);
15386 /* Look for the `]' token. */
15387 if (cp_token
*close_token
15388 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
15389 end_loc
= close_token
->location
;
15390 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
15409 op
= TRUNC_DIV_EXPR
;
15413 op
= TRUNC_MOD_EXPR
;
15433 op
= TRUTH_NOT_EXPR
;
15466 op
= TRUNC_DIV_EXPR
;
15471 op
= TRUNC_MOD_EXPR
;
15497 case CPP_LSHIFT_EQ
:
15502 case CPP_RSHIFT_EQ
:
15519 case CPP_GREATER_EQ
:
15523 case CPP_SPACESHIP
:
15524 op
= SPACESHIP_EXPR
;
15528 op
= TRUTH_ANDIF_EXPR
;
15532 op
= TRUTH_ORIF_EXPR
;
15535 case CPP_PLUS_PLUS
:
15536 op
= POSTINCREMENT_EXPR
;
15539 case CPP_MINUS_MINUS
:
15540 op
= PREDECREMENT_EXPR
;
15544 op
= COMPOUND_EXPR
;
15547 case CPP_DEREF_STAR
:
15552 op
= COMPONENT_REF
;
15557 /* Consume the `?'. */
15558 cp_lexer_consume_token (parser
->lexer
);
15559 /* Look for the matching `:'. */
15560 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
15564 case CPP_OPEN_PAREN
:
15566 /* Consume the `('. */
15567 matching_parens parens
;
15568 parens
.consume_open (parser
);
15569 /* Look for the matching `)'. */
15570 token
= parens
.require_close (parser
);
15572 end_loc
= token
->location
;
15578 case CPP_OPEN_SQUARE
:
15579 /* Consume the `['. */
15580 cp_lexer_consume_token (parser
->lexer
);
15581 /* Look for the matching `]'. */
15582 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
15584 end_loc
= token
->location
;
15589 case CPP_UTF8STRING
:
15590 case CPP_UTF8STRING_USERDEF
:
15597 case CPP_STRING_USERDEF
:
15598 case CPP_WSTRING_USERDEF
:
15599 case CPP_STRING16_USERDEF
:
15600 case CPP_STRING32_USERDEF
:
15606 if (cxx_dialect
== cxx98
)
15607 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
15609 /* Consume the string. */
15610 str
= cp_parser_string_literal (parser
, /*translate=*/true,
15611 /*wide_ok=*/true, /*lookup_udlit=*/false);
15612 if (str
== error_mark_node
)
15613 return error_mark_node
;
15614 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
15616 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
15617 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
15618 end_loc
= str
.get_location ();
15623 /* Look for the suffix identifier. */
15624 token
= cp_lexer_peek_token (parser
->lexer
);
15625 if (token
->type
== CPP_NAME
)
15627 id
= cp_parser_identifier (parser
);
15628 end_loc
= token
->location
;
15630 else if (token
->type
== CPP_KEYWORD
)
15632 error ("unexpected keyword;"
15633 " remove space between quotes and suffix identifier");
15634 return error_mark_node
;
15638 error ("expected suffix identifier");
15639 return error_mark_node
;
15642 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
15643 (TREE_TYPE (TREE_TYPE (string_tree
))));
15644 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
15647 error ("expected empty string after %<operator%> keyword");
15648 return error_mark_node
;
15650 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
15653 error ("invalid encoding prefix in literal operator");
15654 return error_mark_node
;
15656 if (id
!= error_mark_node
)
15658 const char *name
= IDENTIFIER_POINTER (id
);
15659 id
= cp_literal_operator_id (name
);
15661 /* Generate a location of the form:
15662 "" _suffix_identifier
15663 ^~~~~~~~~~~~~~~~~~~~~
15664 with caret == start at the start token, finish at the end of the
15665 suffix identifier. */
15666 location_t combined_loc
15667 = make_location (start_loc
, start_loc
, parser
->lexer
);
15668 return cp_expr (id
, combined_loc
);
15672 /* Anything else is an error. */
15676 /* If we have selected an identifier, we need to consume the
15678 if (op
!= ERROR_MARK
)
15680 id
= ovl_op_identifier (assop
, op
);
15682 cp_lexer_consume_token (parser
->lexer
);
15684 /* Otherwise, no valid operator name was present. */
15687 cp_parser_error (parser
, "expected operator");
15688 id
= error_mark_node
;
15691 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
15692 return cp_expr (id
, start_loc
);
15695 /* Parse a template-declaration.
15697 template-declaration:
15698 export [opt] template < template-parameter-list > declaration
15700 If MEMBER_P is TRUE, this template-declaration occurs within a
15703 The grammar rule given by the standard isn't correct. What
15704 is really meant is:
15706 template-declaration:
15707 export [opt] template-parameter-list-seq
15708 decl-specifier-seq [opt] init-declarator [opt] ;
15709 export [opt] template-parameter-list-seq
15710 function-definition
15712 template-parameter-list-seq:
15713 template-parameter-list-seq [opt]
15714 template < template-parameter-list >
15716 Concept Extensions:
15718 template-parameter-list-seq:
15719 template < template-parameter-list > requires-clause [opt]
15722 requires logical-or-expression */
15725 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
15727 /* Check for `export'. */
15728 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
15730 /* Consume the `export' token. */
15731 cp_lexer_consume_token (parser
->lexer
);
15732 /* Warn that we do not support `export'. */
15733 warning (0, "keyword %<export%> not implemented, and will be ignored");
15736 cp_parser_template_declaration_after_export (parser
, member_p
);
15739 /* Parse a template-parameter-list.
15741 template-parameter-list:
15743 template-parameter-list , template-parameter
15745 Returns a TREE_LIST. Each node represents a template parameter.
15746 The nodes are connected via their TREE_CHAINs. */
15749 cp_parser_template_parameter_list (cp_parser
* parser
)
15751 tree parameter_list
= NULL_TREE
;
15753 /* Don't create wrapper nodes within a template-parameter-list,
15754 since we don't want to have different types based on the
15755 spelling location of constants and decls within them. */
15756 auto_suppress_location_wrappers sentinel
;
15758 begin_template_parm_list ();
15760 /* The loop below parses the template parms. We first need to know
15761 the total number of template parms to be able to compute proper
15762 canonical types of each dependent type. So after the loop, when
15763 we know the total number of template parms,
15764 end_template_parm_list computes the proper canonical types and
15765 fixes up the dependent types accordingly. */
15770 bool is_parameter_pack
;
15771 location_t parm_loc
;
15773 /* Parse the template-parameter. */
15774 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15775 parameter
= cp_parser_template_parameter (parser
,
15777 &is_parameter_pack
);
15778 /* Add it to the list. */
15779 if (parameter
!= error_mark_node
)
15780 parameter_list
= process_template_parm (parameter_list
,
15784 is_parameter_pack
);
15787 tree err_parm
= build_tree_list (parameter
, parameter
);
15788 parameter_list
= chainon (parameter_list
, err_parm
);
15791 /* If the next token is not a `,', we're done. */
15792 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15794 /* Otherwise, consume the `,' token. */
15795 cp_lexer_consume_token (parser
->lexer
);
15798 return end_template_parm_list (parameter_list
);
15801 /* Parse a introduction-list.
15804 introduced-parameter
15805 introduction-list , introduced-parameter
15807 introduced-parameter:
15808 ...[opt] identifier
15810 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15811 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15812 WILDCARD_DECL will also have DECL_NAME set and token location in
15813 DECL_SOURCE_LOCATION. */
15816 cp_parser_introduction_list (cp_parser
*parser
)
15818 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
15822 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15824 cp_lexer_consume_token (parser
->lexer
);
15826 tree identifier
= cp_parser_identifier (parser
);
15827 if (identifier
== error_mark_node
)
15830 /* Build placeholder. */
15831 tree parm
= build_nt (WILDCARD_DECL
);
15832 DECL_SOURCE_LOCATION (parm
)
15833 = cp_lexer_peek_token (parser
->lexer
)->location
;
15834 DECL_NAME (parm
) = identifier
;
15835 WILDCARD_PACK_P (parm
) = is_pack
;
15836 vec_safe_push (introduction_vec
, parm
);
15838 /* If the next token is not a `,', we're done. */
15839 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15841 /* Otherwise, consume the `,' token. */
15842 cp_lexer_consume_token (parser
->lexer
);
15845 /* Convert the vec into a TREE_VEC. */
15846 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
15849 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
15850 TREE_VEC_ELT (introduction_list
, n
) = parm
;
15852 release_tree_vector (introduction_vec
);
15853 return introduction_list
;
15856 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15857 is an abstract declarator. */
15859 static inline cp_declarator
*
15860 get_id_declarator (cp_declarator
*declarator
)
15862 cp_declarator
*d
= declarator
;
15863 while (d
&& d
->kind
!= cdk_id
)
15868 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15869 is an abstract declarator. */
15872 get_unqualified_id (cp_declarator
*declarator
)
15874 declarator
= get_id_declarator (declarator
);
15876 return declarator
->u
.id
.unqualified_name
;
15881 /* Returns true if TYPE would declare a constrained constrained-parameter. */
15884 is_constrained_parameter (tree type
)
15887 && TREE_CODE (type
) == TYPE_DECL
15888 && CONSTRAINED_PARM_CONCEPT (type
)
15889 && DECL_P (CONSTRAINED_PARM_CONCEPT (type
)));
15892 /* Returns true if PARM declares a constrained-parameter. */
15895 is_constrained_parameter (cp_parameter_declarator
*parm
)
15897 return is_constrained_parameter (parm
->decl_specifiers
.type
);
15900 /* Check that the type parameter is only a declarator-id, and that its
15901 type is not cv-qualified. */
15904 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
15905 cp_parameter_declarator
*parm
)
15907 if (!parm
->declarator
)
15910 if (parm
->declarator
->kind
!= cdk_id
)
15912 cp_parser_error (parser
, "invalid constrained type parameter");
15916 /* Don't allow cv-qualified type parameters. */
15917 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
15918 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
15920 cp_parser_error (parser
, "cv-qualified type parameter");
15927 /* Finish parsing/processing a template type parameter and checking
15928 various restrictions. */
15931 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
15933 cp_parameter_declarator
* parmdecl
)
15935 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15936 return finish_template_type_parm (class_type_node
, id
);
15938 return error_mark_node
;
15942 finish_constrained_template_template_parm (tree proto
, tree id
)
15944 /* FIXME: This should probably be copied, and we may need to adjust
15945 the template parameter depths. */
15946 tree saved_parms
= current_template_parms
;
15947 begin_template_parm_list ();
15948 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
15949 end_template_parm_list ();
15951 tree parm
= finish_template_template_parm (class_type_node
, id
);
15952 current_template_parms
= saved_parms
;
15957 /* Finish parsing/processing a template template parameter by borrowing
15958 the template parameter list from the prototype parameter. */
15961 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
15964 cp_parameter_declarator
*parmdecl
)
15966 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
15967 return error_mark_node
;
15968 return finish_constrained_template_template_parm (proto
, id
);
15971 /* Create a new non-type template parameter from the given PARM
15975 cp_parser_constrained_non_type_template_parm (bool *is_non_type
,
15976 cp_parameter_declarator
*parm
)
15978 *is_non_type
= true;
15979 cp_declarator
*decl
= parm
->declarator
;
15980 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
15981 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
15982 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
15985 /* Build a constrained template parameter based on the PARMDECL
15986 declarator. The type of PARMDECL is the constrained type, which
15987 refers to the prototype template parameter that ultimately
15988 specifies the type of the declared parameter. */
15991 finish_constrained_parameter (cp_parser
*parser
,
15992 cp_parameter_declarator
*parmdecl
,
15995 tree decl
= parmdecl
->decl_specifiers
.type
;
15996 tree id
= get_unqualified_id (parmdecl
->declarator
);
15997 tree def
= parmdecl
->default_argument
;
15998 tree proto
= DECL_INITIAL (decl
);
16000 /* Build the parameter. Return an error if the declarator was invalid. */
16002 if (TREE_CODE (proto
) == TYPE_DECL
)
16003 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
16004 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
16005 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
16008 parm
= cp_parser_constrained_non_type_template_parm (is_non_type
, parmdecl
);
16009 if (parm
== error_mark_node
)
16010 return error_mark_node
;
16012 /* Finish the parameter decl and create a node attaching the
16013 default argument and constraint. */
16014 parm
= build_tree_list (def
, parm
);
16015 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
16020 /* Returns true if the parsed type actually represents the declaration
16021 of a type template-parameter. */
16024 declares_constrained_type_template_parameter (tree type
)
16026 return (is_constrained_parameter (type
)
16027 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
16030 /* Returns true if the parsed type actually represents the declaration of
16031 a template template-parameter. */
16034 declares_constrained_template_template_parameter (tree type
)
16036 return (is_constrained_parameter (type
)
16037 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
16040 /* Parse a default argument for a type template-parameter.
16041 Note that diagnostics are handled in cp_parser_template_parameter. */
16044 cp_parser_default_type_template_argument (cp_parser
*parser
)
16046 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16048 /* Consume the `=' token. */
16049 cp_lexer_consume_token (parser
->lexer
);
16051 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16053 /* Parse the default-argument. */
16054 push_deferring_access_checks (dk_no_deferred
);
16055 tree default_argument
= cp_parser_type_id (parser
,
16056 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16058 pop_deferring_access_checks ();
16060 if (flag_concepts
&& type_uses_auto (default_argument
))
16062 error_at (token
->location
,
16063 "invalid use of %<auto%> in default template argument");
16064 return error_mark_node
;
16067 return default_argument
;
16070 /* Parse a default argument for a template template-parameter. */
16073 cp_parser_default_template_template_argument (cp_parser
*parser
)
16075 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16079 /* Consume the `='. */
16080 cp_lexer_consume_token (parser
->lexer
);
16081 /* Parse the id-expression. */
16082 push_deferring_access_checks (dk_no_deferred
);
16083 /* save token before parsing the id-expression, for error
16085 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
16086 tree default_argument
16087 = cp_parser_id_expression (parser
,
16088 /*template_keyword_p=*/false,
16089 /*check_dependency_p=*/true,
16090 /*template_p=*/&is_template
,
16091 /*declarator_p=*/false,
16092 /*optional_p=*/false);
16093 if (TREE_CODE (default_argument
) == TYPE_DECL
)
16094 /* If the id-expression was a template-id that refers to
16095 a template-class, we already have the declaration here,
16096 so no further lookup is needed. */
16099 /* Look up the name. */
16101 = cp_parser_lookup_name (parser
, default_argument
,
16103 /*is_template=*/is_template
,
16104 /*is_namespace=*/false,
16105 /*check_dependency=*/true,
16106 /*ambiguous_decls=*/NULL
,
16108 /* See if the default argument is valid. */
16109 default_argument
= check_template_template_default_arg (default_argument
);
16110 pop_deferring_access_checks ();
16111 return default_argument
;
16114 /* Parse a template-parameter.
16116 template-parameter:
16118 parameter-declaration
16120 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16121 the parameter. The TREE_PURPOSE is the default value, if any.
16122 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16123 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16124 set to true iff this parameter is a parameter pack. */
16127 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
16128 bool *is_parameter_pack
)
16131 cp_parameter_declarator
*parameter_declarator
;
16134 /* Assume it is a type parameter or a template parameter. */
16135 *is_non_type
= false;
16136 /* Assume it not a parameter pack. */
16137 *is_parameter_pack
= false;
16138 /* Peek at the next token. */
16139 token
= cp_lexer_peek_token (parser
->lexer
);
16140 /* If it is `template', we have a type-parameter. */
16141 if (token
->keyword
== RID_TEMPLATE
)
16142 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16143 /* If it is `class' or `typename' we do not know yet whether it is a
16144 type parameter or a non-type parameter. Consider:
16146 template <typename T, typename T::X X> ...
16150 template <class C, class D*> ...
16152 Here, the first parameter is a type parameter, and the second is
16153 a non-type parameter. We can tell by looking at the token after
16154 the identifier -- if it is a `,', `=', or `>' then we have a type
16156 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16158 /* Peek at the token after `class' or `typename'. */
16159 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16160 /* If it's an ellipsis, we have a template type parameter
16162 if (token
->type
== CPP_ELLIPSIS
)
16163 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16164 /* If it's an identifier, skip it. */
16165 if (token
->type
== CPP_NAME
)
16166 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16167 /* Now, see if the token looks like the end of a template
16169 if (token
->type
== CPP_COMMA
16170 || token
->type
== CPP_EQ
16171 || token
->type
== CPP_GREATER
)
16172 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16175 /* Otherwise, it is a non-type parameter or a constrained parameter.
16179 When parsing a default template-argument for a non-type
16180 template-parameter, the first non-nested `>' is taken as the end
16181 of the template parameter-list rather than a greater-than
16183 parameter_declarator
16184 = cp_parser_parameter_declaration (parser
,
16185 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16186 /*template_parm_p=*/true,
16187 /*parenthesized_p=*/NULL
);
16189 if (!parameter_declarator
)
16190 return error_mark_node
;
16192 /* If the parameter declaration is marked as a parameter pack, set
16193 *IS_PARAMETER_PACK to notify the caller. */
16194 if (parameter_declarator
->template_parameter_pack_p
)
16195 *is_parameter_pack
= true;
16197 if (parameter_declarator
->default_argument
)
16199 /* Can happen in some cases of erroneous input (c++/34892). */
16200 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16201 /* Consume the `...' for better error recovery. */
16202 cp_lexer_consume_token (parser
->lexer
);
16205 /* The parameter may have been constrained type parameter. */
16206 if (is_constrained_parameter (parameter_declarator
))
16207 return finish_constrained_parameter (parser
,
16208 parameter_declarator
,
16211 // Now we're sure that the parameter is a non-type parameter.
16212 *is_non_type
= true;
16214 parm
= grokdeclarator (parameter_declarator
->declarator
,
16215 ¶meter_declarator
->decl_specifiers
,
16216 TPARM
, /*initialized=*/0,
16217 /*attrlist=*/NULL
);
16218 if (parm
== error_mark_node
)
16219 return error_mark_node
;
16221 return build_tree_list (parameter_declarator
->default_argument
, parm
);
16224 /* Parse a type-parameter.
16227 class identifier [opt]
16228 class identifier [opt] = type-id
16229 typename identifier [opt]
16230 typename identifier [opt] = type-id
16231 template < template-parameter-list > class identifier [opt]
16232 template < template-parameter-list > class identifier [opt]
16235 GNU Extension (variadic templates):
16238 class ... identifier [opt]
16239 typename ... identifier [opt]
16241 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
16242 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
16243 the declaration of the parameter.
16245 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
16248 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
16253 /* Look for a keyword to tell us what kind of parameter this is. */
16254 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
16256 return error_mark_node
;
16258 switch (token
->keyword
)
16264 tree default_argument
;
16266 /* If the next token is an ellipsis, we have a template
16268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16270 /* Consume the `...' token. */
16271 cp_lexer_consume_token (parser
->lexer
);
16272 maybe_warn_variadic_templates ();
16274 *is_parameter_pack
= true;
16277 /* If the next token is an identifier, then it names the
16279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
16280 identifier
= cp_parser_identifier (parser
);
16282 identifier
= NULL_TREE
;
16284 /* Create the parameter. */
16285 parameter
= finish_template_type_parm (class_type_node
, identifier
);
16287 /* If the next token is an `=', we have a default argument. */
16288 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16291 = cp_parser_default_type_template_argument (parser
);
16293 /* Template parameter packs cannot have default
16295 if (*is_parameter_pack
)
16298 error_at (token
->location
,
16299 "template parameter pack %qD cannot have a "
16300 "default argument", identifier
);
16302 error_at (token
->location
,
16303 "template parameter packs cannot have "
16304 "default arguments");
16305 default_argument
= NULL_TREE
;
16307 else if (check_for_bare_parameter_packs (default_argument
))
16308 default_argument
= error_mark_node
;
16311 default_argument
= NULL_TREE
;
16313 /* Create the combined representation of the parameter and the
16314 default argument. */
16315 parameter
= build_tree_list (default_argument
, parameter
);
16322 tree default_argument
;
16324 /* Look for the `<'. */
16325 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
16326 /* Parse the template-parameter-list. */
16327 cp_parser_template_parameter_list (parser
);
16328 /* Look for the `>'. */
16329 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
16331 /* If template requirements are present, parse them. */
16334 tree reqs
= get_shorthand_constraints (current_template_parms
);
16335 if (tree dreqs
= cp_parser_requires_clause_opt (parser
, false))
16336 reqs
= combine_constraint_expressions (reqs
, dreqs
);
16337 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
16340 /* Look for the `class' or 'typename' keywords. */
16341 cp_parser_type_parameter_key (parser
);
16342 /* If the next token is an ellipsis, we have a template
16344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16346 /* Consume the `...' token. */
16347 cp_lexer_consume_token (parser
->lexer
);
16348 maybe_warn_variadic_templates ();
16350 *is_parameter_pack
= true;
16352 /* If the next token is an `=', then there is a
16353 default-argument. If the next token is a `>', we are at
16354 the end of the parameter-list. If the next token is a `,',
16355 then we are at the end of this parameter. */
16356 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
16357 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
16358 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16360 identifier
= cp_parser_identifier (parser
);
16361 /* Treat invalid names as if the parameter were nameless. */
16362 if (identifier
== error_mark_node
)
16363 identifier
= NULL_TREE
;
16366 identifier
= NULL_TREE
;
16368 /* Create the template parameter. */
16369 parameter
= finish_template_template_parm (class_type_node
,
16372 /* If the next token is an `=', then there is a
16373 default-argument. */
16374 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
16377 = cp_parser_default_template_template_argument (parser
);
16379 /* Template parameter packs cannot have default
16381 if (*is_parameter_pack
)
16384 error_at (token
->location
,
16385 "template parameter pack %qD cannot "
16386 "have a default argument",
16389 error_at (token
->location
, "template parameter packs cannot "
16390 "have default arguments");
16391 default_argument
= NULL_TREE
;
16395 default_argument
= NULL_TREE
;
16397 /* Create the combined representation of the parameter and the
16398 default argument. */
16399 parameter
= build_tree_list (default_argument
, parameter
);
16404 gcc_unreachable ();
16411 /* Parse a template-id.
16414 template-name < template-argument-list [opt] >
16416 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
16417 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
16418 returned. Otherwise, if the template-name names a function, or set
16419 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
16420 names a class, returns a TYPE_DECL for the specialization.
16422 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
16423 uninstantiated templates. */
16426 cp_parser_template_id (cp_parser
*parser
,
16427 bool template_keyword_p
,
16428 bool check_dependency_p
,
16429 enum tag_types tag_type
,
16430 bool is_declaration
)
16435 cp_token_position start_of_id
= 0;
16436 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
16437 bool is_identifier
;
16439 /* If the next token corresponds to a template-id, there is no need
16441 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16443 if (token
->type
== CPP_TEMPLATE_ID
)
16445 cp_lexer_consume_token (parser
->lexer
);
16446 return saved_checks_value (token
->u
.tree_check_value
);
16449 /* Avoid performing name lookup if there is no possibility of
16450 finding a template-id. */
16451 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
16452 || (token
->type
== CPP_NAME
16453 && !cp_parser_nth_token_starts_template_argument_list_p
16456 cp_parser_error (parser
, "expected template-id");
16457 return error_mark_node
;
16460 /* Remember where the template-id starts. */
16461 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
16462 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
16464 push_deferring_access_checks (dk_deferred
);
16466 /* Parse the template-name. */
16467 is_identifier
= false;
16468 templ
= cp_parser_template_name (parser
, template_keyword_p
,
16469 check_dependency_p
,
16474 /* Push any access checks inside the firewall we're about to create. */
16475 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
16476 pop_deferring_access_checks ();
16477 if (templ
== error_mark_node
|| is_identifier
)
16480 /* Since we're going to preserve any side-effects from this parse, set up a
16481 firewall to protect our callers from cp_parser_commit_to_tentative_parse
16482 in the template arguments. */
16483 tentative_firewall
firewall (parser
);
16484 reopen_deferring_access_checks (checks
);
16486 /* If we find the sequence `[:' after a template-name, it's probably
16487 a digraph-typo for `< ::'. Substitute the tokens and check if we can
16488 parse correctly the argument list. */
16489 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
16490 == CPP_OPEN_SQUARE
)
16491 && next_token
->flags
& DIGRAPH
16492 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
16494 && !(next_token_2
->flags
& PREV_WHITE
))
16496 cp_parser_parse_tentatively (parser
);
16497 /* Change `:' into `::'. */
16498 next_token_2
->type
= CPP_SCOPE
;
16499 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
16501 cp_lexer_consume_token (parser
->lexer
);
16503 /* Parse the arguments. */
16504 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16505 if (!cp_parser_parse_definitely (parser
))
16507 /* If we couldn't parse an argument list, then we revert our changes
16508 and return simply an error. Maybe this is not a template-id
16510 next_token_2
->type
= CPP_COLON
;
16511 cp_parser_error (parser
, "expected %<<%>");
16512 pop_deferring_access_checks ();
16513 return error_mark_node
;
16515 /* Otherwise, emit an error about the invalid digraph, but continue
16516 parsing because we got our argument list. */
16517 if (permerror (next_token
->location
,
16518 "%<<::%> cannot begin a template-argument list"))
16520 static bool hint
= false;
16521 inform (next_token
->location
,
16522 "%<<:%> is an alternate spelling for %<[%>."
16523 " Insert whitespace between %<<%> and %<::%>");
16524 if (!hint
&& !flag_permissive
)
16526 inform (next_token
->location
, "(if you use %<-fpermissive%> "
16527 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
16528 "accept your code)");
16535 /* Look for the `<' that starts the template-argument-list. */
16536 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
16538 pop_deferring_access_checks ();
16539 return error_mark_node
;
16541 /* Parse the arguments. */
16542 arguments
= cp_parser_enclosed_template_argument_list (parser
);
16544 if ((cxx_dialect
> cxx17
)
16545 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
16546 && !template_keyword_p
16547 && (cp_parser_error_occurred (parser
)
16548 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
16550 /* This didn't go well. */
16551 if (TREE_CODE (templ
) == FUNCTION_DECL
)
16553 /* C++2A says that "function-name < a;" is now ill-formed. */
16554 if (cp_parser_error_occurred (parser
))
16556 error_at (token
->location
, "invalid template-argument-list");
16557 inform (token
->location
, "function name as the left hand "
16558 "operand of %<<%> is ill-formed in C++2a; wrap the "
16559 "function name in %<()%>");
16562 /* We expect "f<targs>" to be followed by "(args)". */
16563 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
16564 "expected %<(%> after template-argument-list");
16566 /* Purge all subsequent tokens. */
16567 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16570 cp_parser_simulate_error (parser
);
16571 pop_deferring_access_checks ();
16572 return error_mark_node
;
16576 /* Set the location to be of the form:
16577 template-name < template-argument-list [opt] >
16578 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
16579 with caret == start at the start of the template-name,
16580 ranging until the closing '>'. */
16581 location_t combined_loc
16582 = make_location (token
->location
, token
->location
, parser
->lexer
);
16584 /* Check for concepts autos where they don't belong. We could
16585 identify types in some cases of identifier TEMPL, looking ahead
16586 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
16587 types. We reject them in functions, but if what we have is an
16588 identifier, even with none_type we can't conclude it's NOT a
16589 type, we have to wait for template substitution. */
16590 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
16591 template_id
= error_mark_node
;
16592 /* Build a representation of the specialization. */
16593 else if (identifier_p (templ
))
16594 template_id
= build_min_nt_loc (combined_loc
,
16597 else if (DECL_TYPE_TEMPLATE_P (templ
)
16598 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
16600 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
16601 template (rather than some instantiation thereof) only if
16602 is not nested within some other construct. For example, in
16603 "template <typename T> void f(T) { A<T>::", A<T> is just an
16604 instantiation of A. */
16605 bool entering_scope
16606 = (template_parm_scope_p ()
16607 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
16609 = finish_template_type (templ
, arguments
, entering_scope
);
16611 else if (concept_definition_p (templ
))
16613 /* The caller will decide whether this is a concept check or type
16615 template_id
= build2_loc (combined_loc
, TEMPLATE_ID_EXPR
,
16616 boolean_type_node
, templ
, arguments
);
16618 else if (variable_template_p (templ
))
16620 template_id
= lookup_template_variable (templ
, arguments
);
16621 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16622 SET_EXPR_LOCATION (template_id
, combined_loc
);
16626 /* If it's not a class-template or a template-template, it should be
16627 a function-template. */
16628 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
16630 template_id
= lookup_template_function (templ
, arguments
);
16631 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
16632 SET_EXPR_LOCATION (template_id
, combined_loc
);
16635 /* If parsing tentatively, replace the sequence of tokens that makes
16636 up the template-id with a CPP_TEMPLATE_ID token. That way,
16637 should we re-parse the token stream, we will not have to repeat
16638 the effort required to do the parse, nor will we issue duplicate
16639 error messages about problems during instantiation of the
16642 /* Don't do this if we had a parse error in a declarator; re-parsing
16643 might succeed if a name changes meaning (60361). */
16644 && !(cp_parser_error_occurred (parser
)
16645 && cp_parser_parsing_tentatively (parser
)
16646 && parser
->in_declarator_p
))
16648 /* Reset the contents of the START_OF_ID token. */
16649 token
->type
= CPP_TEMPLATE_ID
;
16650 token
->location
= combined_loc
;
16652 /* Retrieve any deferred checks. Do not pop this access checks yet
16653 so the memory will not be reclaimed during token replacing below. */
16654 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
16655 token
->tree_check_p
= true;
16656 token
->u
.tree_check_value
->value
= template_id
;
16657 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
16658 token
->keyword
= RID_MAX
;
16660 /* Purge all subsequent tokens. */
16661 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
16663 /* ??? Can we actually assume that, if template_id ==
16664 error_mark_node, we will have issued a diagnostic to the
16665 user, as opposed to simply marking the tentative parse as
16667 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
16668 error_at (token
->location
, "parse error in template argument list");
16671 pop_to_parent_deferring_access_checks ();
16672 return template_id
;
16675 /* Like cp_parser_template_id, called in non-type context. */
16678 cp_parser_template_id_expr (cp_parser
*parser
,
16679 bool template_keyword_p
,
16680 bool check_dependency_p
,
16681 bool is_declaration
)
16683 tree x
= cp_parser_template_id (parser
, template_keyword_p
, check_dependency_p
,
16684 none_type
, is_declaration
);
16685 if (TREE_CODE (x
) == TEMPLATE_ID_EXPR
16686 && concept_check_p (x
))
16687 /* We didn't check the arguments in cp_parser_template_id; do that now. */
16688 return build_concept_id (x
);
16692 /* Parse a template-name.
16697 The standard should actually say:
16701 operator-function-id
16703 A defect report has been filed about this issue.
16705 A conversion-function-id cannot be a template name because they cannot
16706 be part of a template-id. In fact, looking at this code:
16708 a.operator K<int>()
16710 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
16711 It is impossible to call a templated conversion-function-id with an
16712 explicit argument list, since the only allowed template parameter is
16713 the type to which it is converting.
16715 If TEMPLATE_KEYWORD_P is true, then we have just seen the
16716 `template' keyword, in a construction like:
16720 In that case `f' is taken to be a template-name, even though there
16721 is no way of knowing for sure.
16723 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
16724 name refers to a set of overloaded functions, at least one of which
16725 is a template, or an IDENTIFIER_NODE with the name of the template,
16726 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
16727 names are looked up inside uninstantiated templates. */
16730 cp_parser_template_name (cp_parser
* parser
,
16731 bool template_keyword_p
,
16732 bool check_dependency_p
,
16733 bool is_declaration
,
16734 enum tag_types tag_type
,
16735 bool *is_identifier
)
16739 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16741 /* If the next token is `operator', then we have either an
16742 operator-function-id or a conversion-function-id. */
16743 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
16745 /* We don't know whether we're looking at an
16746 operator-function-id or a conversion-function-id. */
16747 cp_parser_parse_tentatively (parser
);
16748 /* Try an operator-function-id. */
16749 identifier
= cp_parser_operator_function_id (parser
);
16750 /* If that didn't work, try a conversion-function-id. */
16751 if (!cp_parser_parse_definitely (parser
))
16753 cp_parser_error (parser
, "expected template-name");
16754 return error_mark_node
;
16757 /* Look for the identifier. */
16759 identifier
= cp_parser_identifier (parser
);
16761 /* If we didn't find an identifier, we don't have a template-id. */
16762 if (identifier
== error_mark_node
)
16763 return error_mark_node
;
16765 /* If the name immediately followed the `template' keyword, then it
16766 is a template-name. However, if the next token is not `<', then
16767 we do not treat it as a template-name, since it is not being used
16768 as part of a template-id. This enables us to handle constructs
16771 template <typename T> struct S { S(); };
16772 template <typename T> S<T>::S();
16774 correctly. We would treat `S' as a template -- if it were `S<T>'
16775 -- but we do not if there is no `<'. */
16777 if (processing_template_decl
16778 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
16780 /* In a declaration, in a dependent context, we pretend that the
16781 "template" keyword was present in order to improve error
16782 recovery. For example, given:
16784 template <typename T> void f(T::X<int>);
16786 we want to treat "X<int>" as a template-id. */
16788 && !template_keyword_p
16789 && parser
->scope
&& TYPE_P (parser
->scope
)
16790 && check_dependency_p
16791 && dependent_scope_p (parser
->scope
)
16792 /* Do not do this for dtors (or ctors), since they never
16793 need the template keyword before their name. */
16794 && !constructor_name_p (identifier
, parser
->scope
))
16796 cp_token_position start
= 0;
16798 /* Explain what went wrong. */
16799 error_at (token
->location
, "non-template %qD used as template",
16801 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
16802 parser
->scope
, identifier
);
16803 /* If parsing tentatively, find the location of the "<" token. */
16804 if (cp_parser_simulate_error (parser
))
16805 start
= cp_lexer_token_position (parser
->lexer
, true);
16806 /* Parse the template arguments so that we can issue error
16807 messages about them. */
16808 cp_lexer_consume_token (parser
->lexer
);
16809 cp_parser_enclosed_template_argument_list (parser
);
16810 /* Skip tokens until we find a good place from which to
16811 continue parsing. */
16812 cp_parser_skip_to_closing_parenthesis (parser
,
16813 /*recovering=*/true,
16815 /*consume_paren=*/false);
16816 /* If parsing tentatively, permanently remove the
16817 template argument list. That will prevent duplicate
16818 error messages from being issued about the missing
16819 "template" keyword. */
16821 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
16823 *is_identifier
= true;
16824 parser
->context
->object_type
= NULL_TREE
;
16828 /* If the "template" keyword is present, then there is generally
16829 no point in doing name-lookup, so we just return IDENTIFIER.
16830 But, if the qualifying scope is non-dependent then we can
16831 (and must) do name-lookup normally. */
16832 if (template_keyword_p
)
16834 tree scope
= (parser
->scope
? parser
->scope
16835 : parser
->context
->object_type
);
16836 if (scope
&& TYPE_P (scope
)
16837 && (!CLASS_TYPE_P (scope
)
16838 || (check_dependency_p
&& dependent_type_p (scope
))))
16840 /* We're optimizing away the call to cp_parser_lookup_name, but
16841 we still need to do this. */
16842 parser
->context
->object_type
= NULL_TREE
;
16848 /* cp_parser_lookup_name clears OBJECT_TYPE. */
16849 const bool scoped_p
= ((parser
->scope
? parser
->scope
16850 : parser
->context
->object_type
) != NULL_TREE
);
16852 /* Look up the name. */
16853 decl
= cp_parser_lookup_name (parser
, identifier
,
16855 /*is_template=*/true,
16856 /*is_namespace=*/false,
16857 check_dependency_p
,
16858 /*ambiguous_decls=*/NULL
,
16861 decl
= strip_using_decl (decl
);
16863 /* If DECL is a template, then the name was a template-name. */
16864 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
16866 if (TREE_DEPRECATED (decl
)
16867 && deprecated_state
!= DEPRECATED_SUPPRESS
)
16868 warn_deprecated_use (decl
, NULL_TREE
);
16872 /* The standard does not explicitly indicate whether a name that
16873 names a set of overloaded declarations, some of which are
16874 templates, is a template-name. However, such a name should
16875 be a template-name; otherwise, there is no way to form a
16876 template-id for the overloaded templates. */
16877 bool found
= false;
16879 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
16880 !found
&& iter
; ++iter
)
16881 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
16885 && (cxx_dialect
> cxx17
)
16887 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
16888 && tag_type
== none_type
)
16890 /* [temp.names] says "A name is also considered to refer to a template
16891 if it is an unqualified-id followed by a < and name lookup finds
16892 either one or more functions or finds nothing." */
16894 /* The "more functions" case. Just use the OVERLOAD as normally.
16895 We don't use is_overloaded_fn here to avoid considering
16897 if (TREE_CODE (decl
) == OVERLOAD
16898 /* Name lookup found one function. */
16899 || TREE_CODE (decl
) == FUNCTION_DECL
)
16901 /* Name lookup found nothing. */
16902 else if (decl
== error_mark_node
)
16908 /* The name does not name a template. */
16909 cp_parser_error (parser
, "expected template-name");
16910 return error_mark_node
;
16917 /* Parse a template-argument-list.
16919 template-argument-list:
16920 template-argument ... [opt]
16921 template-argument-list , template-argument ... [opt]
16923 Returns a TREE_VEC containing the arguments. */
16926 cp_parser_template_argument_list (cp_parser
* parser
)
16928 tree fixed_args
[10];
16929 unsigned n_args
= 0;
16930 unsigned alloced
= 10;
16931 tree
*arg_ary
= fixed_args
;
16933 bool saved_in_template_argument_list_p
;
16935 bool saved_non_ice_p
;
16937 /* Don't create location wrapper nodes within a template-argument-list. */
16938 auto_suppress_location_wrappers sentinel
;
16940 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
16941 parser
->in_template_argument_list_p
= true;
16942 /* Even if the template-id appears in an integral
16943 constant-expression, the contents of the argument list do
16945 saved_ice_p
= parser
->integral_constant_expression_p
;
16946 parser
->integral_constant_expression_p
= false;
16947 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
16948 parser
->non_integral_constant_expression_p
= false;
16950 /* Parse the arguments. */
16956 /* Consume the comma. */
16957 cp_lexer_consume_token (parser
->lexer
);
16959 /* Parse the template-argument. */
16960 argument
= cp_parser_template_argument (parser
);
16962 /* If the next token is an ellipsis, we're expanding a template
16964 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16966 if (argument
== error_mark_node
)
16968 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16969 error_at (token
->location
,
16970 "expected parameter pack before %<...%>");
16972 /* Consume the `...' token. */
16973 cp_lexer_consume_token (parser
->lexer
);
16975 /* Make the argument into a TYPE_PACK_EXPANSION or
16976 EXPR_PACK_EXPANSION. */
16977 argument
= make_pack_expansion (argument
);
16980 if (n_args
== alloced
)
16984 if (arg_ary
== fixed_args
)
16986 arg_ary
= XNEWVEC (tree
, alloced
);
16987 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
16990 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
16992 arg_ary
[n_args
++] = argument
;
16994 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
16996 vec
= make_tree_vec (n_args
);
16999 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
17001 if (arg_ary
!= fixed_args
)
17003 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
17004 parser
->integral_constant_expression_p
= saved_ice_p
;
17005 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
17007 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
17011 /* Parse a template-argument.
17014 assignment-expression
17018 The representation is that of an assignment-expression, type-id, or
17019 id-expression -- except that the qualified id-expression is
17020 evaluated, so that the value returned is either a DECL or an
17023 Although the standard says "assignment-expression", it forbids
17024 throw-expressions or assignments in the template argument.
17025 Therefore, we use "conditional-expression" instead. */
17028 cp_parser_template_argument (cp_parser
* parser
)
17033 bool maybe_type_id
= false;
17034 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
17035 location_t loc
= 0;
17038 /* There's really no way to know what we're looking at, so we just
17039 try each alternative in order.
17043 In a template-argument, an ambiguity between a type-id and an
17044 expression is resolved to a type-id, regardless of the form of
17045 the corresponding template-parameter.
17047 Therefore, we try a type-id first. */
17048 cp_parser_parse_tentatively (parser
);
17049 argument
= cp_parser_template_type_arg (parser
);
17050 /* If there was no error parsing the type-id but the next token is a
17051 '>>', our behavior depends on which dialect of C++ we're
17052 parsing. In C++98, we probably found a typo for '> >'. But there
17053 are type-id which are also valid expressions. For instance:
17055 struct X { int operator >> (int); };
17056 template <int V> struct Foo {};
17059 Here 'X()' is a valid type-id of a function type, but the user just
17060 wanted to write the expression "X() >> 5". Thus, we remember that we
17061 found a valid type-id, but we still try to parse the argument as an
17062 expression to see what happens.
17064 In C++0x, the '>>' will be considered two separate '>'
17066 if (!cp_parser_error_occurred (parser
)
17067 && cxx_dialect
== cxx98
17068 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17070 maybe_type_id
= true;
17071 cp_parser_abort_tentative_parse (parser
);
17075 /* If the next token isn't a `,' or a `>', then this argument wasn't
17076 really finished. This means that the argument is not a valid
17078 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17079 cp_parser_error (parser
, "expected template-argument");
17080 /* If that worked, we're done. */
17081 if (cp_parser_parse_definitely (parser
))
17084 /* We're still not sure what the argument will be. */
17085 cp_parser_parse_tentatively (parser
);
17086 /* Try a template. */
17087 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
17088 argument
= cp_parser_id_expression (parser
,
17089 /*template_keyword_p=*/false,
17090 /*check_dependency_p=*/true,
17092 /*declarator_p=*/false,
17093 /*optional_p=*/false);
17094 /* If the next token isn't a `,' or a `>', then this argument wasn't
17095 really finished. */
17096 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17097 cp_parser_error (parser
, "expected template-argument");
17098 if (!cp_parser_error_occurred (parser
))
17100 /* Figure out what is being referred to. If the id-expression
17101 was for a class template specialization, then we will have a
17102 TYPE_DECL at this point. There is no need to do name lookup
17103 at this point in that case. */
17104 if (TREE_CODE (argument
) != TYPE_DECL
)
17105 argument
= cp_parser_lookup_name (parser
, argument
,
17107 /*is_template=*/template_p
,
17108 /*is_namespace=*/false,
17109 /*check_dependency=*/true,
17110 /*ambiguous_decls=*/NULL
,
17111 argument_start_token
->location
);
17112 if (TREE_CODE (argument
) != TEMPLATE_DECL
17113 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
17114 cp_parser_error (parser
, "expected template-name");
17116 if (cp_parser_parse_definitely (parser
))
17118 if (TREE_DEPRECATED (argument
))
17119 warn_deprecated_use (argument
, NULL_TREE
);
17122 /* It must be a non-type argument. In C++17 any constant-expression is
17124 if (cxx_dialect
> cxx14
)
17127 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17129 -- an integral constant-expression of integral or enumeration
17132 -- the name of a non-type template-parameter; or
17134 -- the name of an object or function with external linkage...
17136 -- the address of an object or function with external linkage...
17138 -- a pointer to member... */
17139 /* Look for a non-type template parameter. */
17140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17142 cp_parser_parse_tentatively (parser
);
17143 argument
= cp_parser_primary_expression (parser
,
17144 /*address_p=*/false,
17146 /*template_arg_p=*/true,
17148 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
17149 || !cp_parser_next_token_ends_template_argument_p (parser
))
17150 cp_parser_simulate_error (parser
);
17151 if (cp_parser_parse_definitely (parser
))
17155 /* If the next token is "&", the argument must be the address of an
17156 object or function with external linkage. */
17157 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17160 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17161 cp_lexer_consume_token (parser
->lexer
);
17163 /* See if we might have an id-expression. */
17164 token
= cp_lexer_peek_token (parser
->lexer
);
17165 if (token
->type
== CPP_NAME
17166 || token
->keyword
== RID_OPERATOR
17167 || token
->type
== CPP_SCOPE
17168 || token
->type
== CPP_TEMPLATE_ID
17169 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17171 cp_parser_parse_tentatively (parser
);
17172 argument
= cp_parser_primary_expression (parser
,
17175 /*template_arg_p=*/true,
17177 if (cp_parser_error_occurred (parser
)
17178 || !cp_parser_next_token_ends_template_argument_p (parser
))
17179 cp_parser_abort_tentative_parse (parser
);
17184 if (INDIRECT_REF_P (argument
))
17186 /* Strip the dereference temporarily. */
17187 gcc_assert (REFERENCE_REF_P (argument
));
17188 argument
= TREE_OPERAND (argument
, 0);
17191 /* If we're in a template, we represent a qualified-id referring
17192 to a static data member as a SCOPE_REF even if the scope isn't
17193 dependent so that we can check access control later. */
17195 if (TREE_CODE (probe
) == SCOPE_REF
)
17196 probe
= TREE_OPERAND (probe
, 1);
17199 /* A variable without external linkage might still be a
17200 valid constant-expression, so no error is issued here
17201 if the external-linkage check fails. */
17202 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
17203 cp_parser_simulate_error (parser
);
17205 else if (is_overloaded_fn (argument
))
17206 /* All overloaded functions are allowed; if the external
17207 linkage test does not pass, an error will be issued
17211 && (TREE_CODE (argument
) == OFFSET_REF
17212 || TREE_CODE (argument
) == SCOPE_REF
))
17213 /* A pointer-to-member. */
17215 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
17218 cp_parser_simulate_error (parser
);
17220 if (cp_parser_parse_definitely (parser
))
17223 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
17224 tf_warning_or_error
);
17226 argument
= convert_from_reference (argument
);
17231 /* If the argument started with "&", there are no other valid
17232 alternatives at this point. */
17235 cp_parser_error (parser
, "invalid non-type template argument");
17236 return error_mark_node
;
17240 /* If the argument wasn't successfully parsed as a type-id followed
17241 by '>>', the argument can only be a constant expression now.
17242 Otherwise, we try parsing the constant-expression tentatively,
17243 because the argument could really be a type-id. */
17245 cp_parser_parse_tentatively (parser
);
17247 if (cxx_dialect
<= cxx14
)
17248 argument
= cp_parser_constant_expression (parser
);
17251 /* In C++20, we can encounter a braced-init-list. */
17252 if (cxx_dialect
>= cxx2a
17253 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
17255 bool expr_non_constant_p
;
17256 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
17259 /* With C++17 generalized non-type template arguments we need to handle
17260 lvalue constant expressions, too. */
17261 argument
= cp_parser_assignment_expression (parser
);
17262 require_potential_constant_expression (argument
);
17265 if (!maybe_type_id
)
17267 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17268 cp_parser_error (parser
, "expected template-argument");
17269 if (cp_parser_parse_definitely (parser
))
17271 /* We did our best to parse the argument as a non type-id, but that
17272 was the only alternative that matched (albeit with a '>' after
17273 it). We can assume it's just a typo from the user, and a
17274 diagnostic will then be issued. */
17275 return cp_parser_template_type_arg (parser
);
17278 /* Parse an explicit-instantiation.
17280 explicit-instantiation:
17281 template declaration
17283 Although the standard says `declaration', what it really means is:
17285 explicit-instantiation:
17286 template decl-specifier-seq [opt] declarator [opt] ;
17288 Things like `template int S<int>::i = 5, int S<double>::j;' are not
17289 supposed to be allowed. A defect report has been filed about this
17294 explicit-instantiation:
17295 storage-class-specifier template
17296 decl-specifier-seq [opt] declarator [opt] ;
17297 function-specifier template
17298 decl-specifier-seq [opt] declarator [opt] ; */
17301 cp_parser_explicit_instantiation (cp_parser
* parser
)
17303 int declares_class_or_enum
;
17304 cp_decl_specifier_seq decl_specifiers
;
17305 tree extension_specifier
= NULL_TREE
;
17307 timevar_push (TV_TEMPLATE_INST
);
17309 /* Look for an (optional) storage-class-specifier or
17310 function-specifier. */
17311 if (cp_parser_allow_gnu_extensions_p (parser
))
17313 extension_specifier
17314 = cp_parser_storage_class_specifier_opt (parser
);
17315 if (!extension_specifier
)
17316 extension_specifier
17317 = cp_parser_function_specifier_opt (parser
,
17318 /*decl_specs=*/NULL
);
17321 /* Look for the `template' keyword. */
17322 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17323 /* Let the front end know that we are processing an explicit
17325 begin_explicit_instantiation ();
17326 /* [temp.explicit] says that we are supposed to ignore access
17327 control while processing explicit instantiation directives. */
17328 push_deferring_access_checks (dk_no_check
);
17329 /* Parse a decl-specifier-seq. */
17330 cp_parser_decl_specifier_seq (parser
,
17331 CP_PARSER_FLAGS_OPTIONAL
,
17333 &declares_class_or_enum
);
17334 /* If there was exactly one decl-specifier, and it declared a class,
17335 and there's no declarator, then we have an explicit type
17337 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
17341 type
= check_tag_decl (&decl_specifiers
,
17342 /*explicit_type_instantiation_p=*/true);
17343 /* Turn access control back on for names used during
17344 template instantiation. */
17345 pop_deferring_access_checks ();
17347 do_type_instantiation (type
, extension_specifier
,
17348 /*complain=*/tf_error
);
17352 cp_declarator
*declarator
;
17355 /* Parse the declarator. */
17357 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
17358 CP_PARSER_FLAGS_NONE
,
17359 /*ctor_dtor_or_conv_p=*/NULL
,
17360 /*parenthesized_p=*/NULL
,
17361 /*member_p=*/false,
17362 /*friend_p=*/false,
17363 /*static_p=*/false);
17364 if (declares_class_or_enum
& 2)
17365 cp_parser_check_for_definition_in_return_type (declarator
,
17366 decl_specifiers
.type
,
17367 decl_specifiers
.locations
[ds_type_spec
]);
17368 if (declarator
!= cp_error_declarator
)
17370 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
17371 permerror (decl_specifiers
.locations
[ds_inline
],
17372 "explicit instantiation shall not use"
17373 " %<inline%> specifier");
17374 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
17375 permerror (decl_specifiers
.locations
[ds_constexpr
],
17376 "explicit instantiation shall not use"
17377 " %<constexpr%> specifier");
17378 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_consteval
))
17379 permerror (decl_specifiers
.locations
[ds_consteval
],
17380 "explicit instantiation shall not use"
17381 " %<consteval%> specifier");
17383 decl
= grokdeclarator (declarator
, &decl_specifiers
,
17384 NORMAL
, 0, &decl_specifiers
.attributes
);
17385 /* Turn access control back on for names used during
17386 template instantiation. */
17387 pop_deferring_access_checks ();
17388 /* Do the explicit instantiation. */
17389 do_decl_instantiation (decl
, extension_specifier
);
17393 pop_deferring_access_checks ();
17394 /* Skip the body of the explicit instantiation. */
17395 cp_parser_skip_to_end_of_statement (parser
);
17398 /* We're done with the instantiation. */
17399 end_explicit_instantiation ();
17401 cp_parser_consume_semicolon_at_end_of_statement (parser
);
17403 timevar_pop (TV_TEMPLATE_INST
);
17406 /* Parse an explicit-specialization.
17408 explicit-specialization:
17409 template < > declaration
17411 Although the standard says `declaration', what it really means is:
17413 explicit-specialization:
17414 template <> decl-specifier [opt] init-declarator [opt] ;
17415 template <> function-definition
17416 template <> explicit-specialization
17417 template <> template-declaration */
17420 cp_parser_explicit_specialization (cp_parser
* parser
)
17422 bool need_lang_pop
;
17423 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17425 /* Look for the `template' keyword. */
17426 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
17427 /* Look for the `<'. */
17428 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17429 /* Look for the `>'. */
17430 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17431 /* We have processed another parameter list. */
17432 ++parser
->num_template_parameter_lists
;
17435 A template ... explicit specialization ... shall not have C
17437 if (current_lang_name
== lang_name_c
)
17439 error_at (token
->location
, "template specialization with C linkage");
17440 maybe_show_extern_c_location ();
17441 /* Give it C++ linkage to avoid confusing other parts of the
17443 push_lang_context (lang_name_cplusplus
);
17444 need_lang_pop
= true;
17447 need_lang_pop
= false;
17448 /* Let the front end know that we are beginning a specialization. */
17449 if (!begin_specialization ())
17451 end_specialization ();
17455 /* If the next keyword is `template', we need to figure out whether
17456 or not we're looking a template-declaration. */
17457 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
17459 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
17460 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
17461 cp_parser_template_declaration_after_export (parser
,
17462 /*member_p=*/false);
17464 cp_parser_explicit_specialization (parser
);
17467 /* Parse the dependent declaration. */
17468 cp_parser_single_declaration (parser
,
17470 /*member_p=*/false,
17471 /*explicit_specialization_p=*/true,
17472 /*friend_p=*/NULL
);
17473 /* We're done with the specialization. */
17474 end_specialization ();
17475 /* For the erroneous case of a template with C linkage, we pushed an
17476 implicit C++ linkage scope; exit that scope now. */
17478 pop_lang_context ();
17479 /* We're done with this parameter list. */
17480 --parser
->num_template_parameter_lists
;
17483 /* Parse a type-specifier.
17486 simple-type-specifier
17489 elaborated-type-specifier
17497 Returns a representation of the type-specifier. For a
17498 class-specifier, enum-specifier, or elaborated-type-specifier, a
17499 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
17501 The parser flags FLAGS is used to control type-specifier parsing.
17503 If IS_DECLARATION is TRUE, then this type-specifier is appearing
17504 in a decl-specifier-seq.
17506 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
17507 class-specifier, enum-specifier, or elaborated-type-specifier, then
17508 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
17509 if a type is declared; 2 if it is defined. Otherwise, it is set to
17512 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
17513 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
17514 is set to FALSE. */
17517 cp_parser_type_specifier (cp_parser
* parser
,
17518 cp_parser_flags flags
,
17519 cp_decl_specifier_seq
*decl_specs
,
17520 bool is_declaration
,
17521 int* declares_class_or_enum
,
17522 bool* is_cv_qualifier
)
17524 tree type_spec
= NULL_TREE
;
17527 cp_decl_spec ds
= ds_last
;
17529 /* Assume this type-specifier does not declare a new type. */
17530 if (declares_class_or_enum
)
17531 *declares_class_or_enum
= 0;
17532 /* And that it does not specify a cv-qualifier. */
17533 if (is_cv_qualifier
)
17534 *is_cv_qualifier
= false;
17535 /* Peek at the next token. */
17536 token
= cp_lexer_peek_token (parser
->lexer
);
17538 /* If we're looking at a keyword, we can use that to guide the
17539 production we choose. */
17540 keyword
= token
->keyword
;
17544 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17545 goto elaborated_type_specifier
;
17547 /* Look for the enum-specifier. */
17548 type_spec
= cp_parser_enum_specifier (parser
);
17549 /* If that worked, we're done. */
17552 if (declares_class_or_enum
)
17553 *declares_class_or_enum
= 2;
17555 cp_parser_set_decl_spec_type (decl_specs
,
17558 /*type_definition_p=*/true);
17562 goto elaborated_type_specifier
;
17564 /* Any of these indicate either a class-specifier, or an
17565 elaborated-type-specifier. */
17569 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
17570 goto elaborated_type_specifier
;
17572 /* Parse tentatively so that we can back up if we don't find a
17573 class-specifier. */
17574 cp_parser_parse_tentatively (parser
);
17575 /* Look for the class-specifier. */
17576 type_spec
= cp_parser_class_specifier (parser
);
17577 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
17578 /* If that worked, we're done. */
17579 if (cp_parser_parse_definitely (parser
))
17581 if (declares_class_or_enum
)
17582 *declares_class_or_enum
= 2;
17584 cp_parser_set_decl_spec_type (decl_specs
,
17587 /*type_definition_p=*/true);
17591 /* Fall through. */
17592 elaborated_type_specifier
:
17593 /* We're declaring (not defining) a class or enum. */
17594 if (declares_class_or_enum
)
17595 *declares_class_or_enum
= 1;
17597 /* Fall through. */
17599 /* Look for an elaborated-type-specifier. */
17601 = (cp_parser_elaborated_type_specifier
17603 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
17606 cp_parser_set_decl_spec_type (decl_specs
,
17609 /*type_definition_p=*/false);
17614 if (is_cv_qualifier
)
17615 *is_cv_qualifier
= true;
17620 if (is_cv_qualifier
)
17621 *is_cv_qualifier
= true;
17626 if (is_cv_qualifier
)
17627 *is_cv_qualifier
= true;
17631 /* The `__complex__' keyword is a GNU extension. */
17639 /* Handle simple keywords. */
17644 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
17645 decl_specs
->any_specifiers_p
= true;
17647 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
17650 /* If we do not already have a type-specifier, assume we are looking
17651 at a simple-type-specifier. */
17652 type_spec
= cp_parser_simple_type_specifier (parser
,
17656 /* If we didn't find a type-specifier, and a type-specifier was not
17657 optional in this context, issue an error message. */
17658 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
17660 cp_parser_error (parser
, "expected type specifier");
17661 return error_mark_node
;
17667 /* Parse a simple-type-specifier.
17669 simple-type-specifier:
17670 :: [opt] nested-name-specifier [opt] type-name
17671 :: [opt] nested-name-specifier template template-id
17686 simple-type-specifier:
17688 decltype ( expression )
17691 __underlying_type ( type-id )
17695 nested-name-specifier(opt) template-name
17699 simple-type-specifier:
17701 __typeof__ unary-expression
17702 __typeof__ ( type-id )
17703 __typeof__ ( type-id ) { initializer-list , [opt] }
17705 Concepts Extension:
17707 simple-type-specifier:
17708 constrained-type-specifier
17710 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
17711 appropriately updated. */
17714 cp_parser_simple_type_specifier (cp_parser
* parser
,
17715 cp_decl_specifier_seq
*decl_specs
,
17716 cp_parser_flags flags
)
17718 tree type
= NULL_TREE
;
17722 /* Peek at the next token. */
17723 token
= cp_lexer_peek_token (parser
->lexer
);
17725 /* If we're looking at a keyword, things are easy. */
17726 switch (token
->keyword
)
17730 decl_specs
->explicit_char_p
= true;
17731 type
= char_type_node
;
17734 type
= char8_type_node
;
17737 type
= char16_type_node
;
17740 type
= char32_type_node
;
17743 type
= wchar_type_node
;
17746 type
= boolean_type_node
;
17749 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
17750 type
= short_integer_type_node
;
17754 decl_specs
->explicit_int_p
= true;
17755 type
= integer_type_node
;
17761 idx
= token
->keyword
- RID_INT_N_0
;
17762 if (! int_n_enabled_p
[idx
])
17766 decl_specs
->explicit_intN_p
= true;
17767 decl_specs
->int_n_idx
= idx
;
17768 /* Check if the alternate "__intN__" form has been used instead of
17770 if (strncmp (IDENTIFIER_POINTER (token
->u
.value
)
17771 + (IDENTIFIER_LENGTH (token
->u
.value
) - 2),
17773 decl_specs
->int_n_alt
= true;
17775 type
= int_n_trees
[idx
].signed_type
;
17779 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
17780 type
= long_integer_type_node
;
17783 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
17784 type
= integer_type_node
;
17787 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
17788 type
= unsigned_type_node
;
17791 type
= float_type_node
;
17794 type
= double_type_node
;
17797 type
= void_type_node
;
17801 maybe_warn_cpp0x (CPP0X_AUTO
);
17802 if (parser
->auto_is_implicit_function_template_parm_p
)
17804 /* The 'auto' might be the placeholder return type for a function decl
17805 with trailing return type. */
17806 bool have_trailing_return_fn_decl
= false;
17808 cp_parser_parse_tentatively (parser
);
17809 cp_lexer_consume_token (parser
->lexer
);
17810 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17811 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
17812 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
17813 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
17815 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
17817 cp_lexer_consume_token (parser
->lexer
);
17818 cp_parser_skip_to_closing_parenthesis (parser
,
17819 /*recovering*/false,
17821 /*consume_paren*/true);
17825 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
17827 have_trailing_return_fn_decl
= true;
17831 cp_lexer_consume_token (parser
->lexer
);
17833 cp_parser_abort_tentative_parse (parser
);
17835 if (have_trailing_return_fn_decl
)
17837 type
= make_auto ();
17841 if (cxx_dialect
>= cxx14
)
17843 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
17844 type
= TREE_TYPE (type
);
17847 type
= error_mark_node
;
17849 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
17851 if (cxx_dialect
< cxx14
)
17852 error_at (token
->location
,
17853 "use of %<auto%> in lambda parameter declaration "
17854 "only available with "
17855 "%<-std=c++14%> or %<-std=gnu++14%>");
17857 else if (cxx_dialect
< cxx14
)
17858 error_at (token
->location
,
17859 "use of %<auto%> in parameter declaration "
17860 "only available with "
17861 "%<-std=c++14%> or %<-std=gnu++14%>");
17862 else if (!flag_concepts
)
17863 pedwarn (token
->location
, 0,
17864 "use of %<auto%> in parameter declaration "
17865 "only available with %<-fconcepts-ts%>");
17868 type
= make_auto ();
17872 /* Since DR 743, decltype can either be a simple-type-specifier by
17873 itself or begin a nested-name-specifier. Parsing it will replace
17874 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17875 handling below decide what to do. */
17876 cp_parser_decltype (parser
);
17877 cp_lexer_set_token_position (parser
->lexer
, token
);
17881 /* Consume the `typeof' token. */
17882 cp_lexer_consume_token (parser
->lexer
);
17883 /* Parse the operand to `typeof'. */
17884 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
17885 /* If it is not already a TYPE, take its type. */
17886 if (!TYPE_P (type
))
17887 type
= finish_typeof (type
);
17890 cp_parser_set_decl_spec_type (decl_specs
, type
,
17892 /*type_definition_p=*/false);
17896 case RID_UNDERLYING_TYPE
:
17897 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
17899 cp_parser_set_decl_spec_type (decl_specs
, type
,
17901 /*type_definition_p=*/false);
17906 case RID_DIRECT_BASES
:
17907 type
= cp_parser_trait_expr (parser
, token
->keyword
);
17909 cp_parser_set_decl_spec_type (decl_specs
, type
,
17911 /*type_definition_p=*/false);
17917 /* If token is an already-parsed decltype not followed by ::,
17918 it's a simple-type-specifier. */
17919 if (token
->type
== CPP_DECLTYPE
17920 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
17922 type
= saved_checks_value (token
->u
.tree_check_value
);
17925 cp_parser_set_decl_spec_type (decl_specs
, type
,
17927 /*type_definition_p=*/false);
17928 /* Remember that we are handling a decltype in order to
17929 implement the resolution of DR 1510 when the argument
17930 isn't instantiation dependent. */
17931 decl_specs
->decltype_p
= true;
17933 cp_lexer_consume_token (parser
->lexer
);
17937 /* If the type-specifier was for a built-in type, we're done. */
17940 /* Record the type. */
17942 && (token
->keyword
!= RID_SIGNED
17943 && token
->keyword
!= RID_UNSIGNED
17944 && token
->keyword
!= RID_SHORT
17945 && token
->keyword
!= RID_LONG
))
17946 cp_parser_set_decl_spec_type (decl_specs
,
17949 /*type_definition_p=*/false);
17951 decl_specs
->any_specifiers_p
= true;
17953 /* Consume the token. */
17954 cp_lexer_consume_token (parser
->lexer
);
17956 if (type
== error_mark_node
)
17957 return error_mark_node
;
17959 /* There is no valid C++ program where a non-template type is
17960 followed by a "<". That usually indicates that the user thought
17961 that the type was a template. */
17962 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
17965 return TYPE_NAME (type
);
17968 /* The type-specifier must be a user-defined type. */
17969 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
17973 const bool typename_p
= (cxx_dialect
>= cxx2a
17974 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
17976 /* Don't gobble tokens or issue error messages if this is an
17977 optional type-specifier. */
17978 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
17979 cp_parser_parse_tentatively (parser
);
17981 /* Remember current tentative parsing state -- if we know we need
17982 a type, we can give better diagnostics here. */
17983 bool tent
= cp_parser_parsing_tentatively (parser
);
17985 token
= cp_lexer_peek_token (parser
->lexer
);
17987 /* Look for the optional `::' operator. */
17989 = (cp_parser_global_scope_opt (parser
,
17990 /*current_scope_valid_p=*/false)
17992 /* Look for the nested-name specifier. */
17994 = (cp_parser_nested_name_specifier_opt (parser
,
17995 /*typename_keyword_p=*/false,
17996 /*check_dependency_p=*/true,
17998 /*is_declaration=*/false)
18000 /* If we have seen a nested-name-specifier, and the next token
18001 is `template', then we are using the template-id production. */
18003 && cp_parser_optional_template_keyword (parser
))
18005 /* Look for the template-id. */
18006 type
= cp_parser_template_id (parser
,
18007 /*template_keyword_p=*/true,
18008 /*check_dependency_p=*/true,
18010 /*is_declaration=*/false);
18011 /* If the template-id did not name a type, we are out of
18013 if (TREE_CODE (type
) != TYPE_DECL
)
18015 /* ...unless we pretend we have seen 'typename'. */
18017 type
= cp_parser_make_typename_type (parser
, type
,
18021 cp_parser_error (parser
, "expected template-id for type");
18022 type
= error_mark_node
;
18027 /* Otherwise, look for a type-name. */
18030 if (cxx_dialect
>= cxx17
)
18031 cp_parser_parse_tentatively (parser
);
18033 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
18035 if (cxx_dialect
>= cxx17
&& !cp_parser_parse_definitely (parser
))
18039 if (!type
&& flag_concepts
&& decl_specs
)
18041 /* Try for a type-constraint with template arguments. We check
18042 decl_specs here to avoid trying this for a functional cast. */
18044 cp_parser_parse_tentatively (parser
);
18046 type
= cp_parser_template_id (parser
,
18047 /*template_keyword_p=*/false,
18048 /*check_dependency_p=*/true,
18050 /*is_declaration=*/false);
18051 if (type
&& concept_check_p (type
))
18053 location_t loc
= EXPR_LOCATION (type
);
18054 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18056 if (tent
&& type
== error_mark_node
)
18057 /* Perhaps it's a concept-check expression. */
18058 cp_parser_simulate_error (parser
);
18061 cp_parser_simulate_error (parser
);
18063 if (!cp_parser_parse_definitely (parser
))
18067 if (!type
&& cxx_dialect
>= cxx17
)
18069 /* Try class template argument deduction or type-constraint without
18070 template arguments. */
18071 tree name
= cp_parser_identifier (parser
);
18072 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
18073 && parser
->scope
!= error_mark_node
)
18076 = cp_lexer_previous_token (parser
->lexer
)->location
;
18077 tree tmpl
= cp_parser_lookup_name (parser
, name
,
18079 /*is_template=*/false,
18080 /*is_namespace=*/false,
18081 /*check_dependency=*/true,
18082 /*ambiguous_decls=*/NULL
,
18084 if (tmpl
&& tmpl
!= error_mark_node
18085 && ctad_template_p (tmpl
))
18086 type
= make_template_placeholder (tmpl
);
18087 else if (flag_concepts
&& tmpl
&& concept_definition_p (tmpl
))
18088 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18092 type
= error_mark_node
;
18093 if (!cp_parser_simulate_error (parser
))
18094 cp_parser_name_lookup_error (parser
, name
, tmpl
,
18095 NLE_TYPE
, token
->location
);
18099 type
= error_mark_node
;
18102 /* If it didn't work out, we don't have a TYPE. */
18103 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
18104 && !cp_parser_parse_definitely (parser
))
18107 /* Keep track of all name-lookups performed in class scopes. */
18111 && TREE_CODE (type
) == TYPE_DECL
18112 && identifier_p (DECL_NAME (type
)))
18113 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
18115 if (type
&& decl_specs
)
18116 cp_parser_set_decl_spec_type (decl_specs
, type
,
18118 /*type_definition_p=*/false);
18121 /* If we didn't get a type-name, issue an error message. */
18122 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
18124 cp_parser_error (parser
, "expected type-name");
18125 return error_mark_node
;
18128 if (type
&& type
!= error_mark_node
)
18130 /* See if TYPE is an Objective-C type, and if so, parse and
18131 accept any protocol references following it. Do this before
18132 the cp_parser_check_for_invalid_template_id() call, because
18133 Objective-C types can be followed by '<...>' which would
18134 enclose protocol names rather than template arguments, and so
18135 everything is fine. */
18136 if (c_dialect_objc () && !parser
->scope
18137 && (objc_is_id (type
) || objc_is_class_name (type
)))
18139 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18140 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
18142 /* Clobber the "unqualified" type previously entered into
18143 DECL_SPECS with the new, improved protocol-qualified version. */
18145 decl_specs
->type
= qual_type
;
18150 /* There is no valid C++ program where a non-template type is
18151 followed by a "<". That usually indicates that the user
18152 thought that the type was a template. */
18153 cp_parser_check_for_invalid_template_id (parser
, type
,
18161 /* Parse the remainder of a placholder-type-specifier.
18163 placeholder-type-specifier:
18164 type-constraint_opt auto
18165 type-constraint_opt decltype(auto)
18167 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
18168 and passed as TMPL. This function converts TMPL to an actual type-constraint,
18169 parses the placeholder type, and performs some contextual syntactic analysis.
18171 LOC provides the location of the template name.
18173 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
18174 don't give an error if TMPL isn't a valid type-constraint, as the template-id
18175 might actually be a concept-check,
18177 Note that the Concepts TS allows the auto or decltype(auto) to be
18178 omitted in a constrained-type-specifier. */
18181 cp_parser_placeholder_type_specifier (cp_parser
*parser
, location_t loc
,
18182 tree tmpl
, bool tentative
)
18184 if (tmpl
== error_mark_node
)
18185 return error_mark_node
;
18187 tree orig_tmpl
= tmpl
;
18189 /* Get the arguments as written for subsequent analysis. */
18190 tree args
= NULL_TREE
;
18191 if (TREE_CODE (tmpl
) == TEMPLATE_ID_EXPR
)
18193 args
= TREE_OPERAND (tmpl
, 1);
18194 tmpl
= TREE_OPERAND (tmpl
, 0);
18196 if (args
== NULL_TREE
)
18197 /* A concept-name with no arguments can't be an expression. */
18200 tsubst_flags_t complain
= tentative
? tf_none
: tf_warning_or_error
;
18202 /* Get the concept and prototype parameter for the constraint. */
18203 tree_pair info
= finish_type_constraints (tmpl
, args
, complain
);
18204 tree con
= info
.first
;
18205 tree proto
= info
.second
;
18206 if (con
== error_mark_node
)
18207 return error_mark_node
;
18209 /* As per the standard, require auto or decltype(auto), except in some
18210 cases (template parameter lists, -fconcepts-ts enabled). */
18211 cp_token
*placeholder
= NULL
, *open_paren
= NULL
, *close_paren
= NULL
;
18212 if (cxx_dialect
>= cxx2a
)
18214 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
18215 placeholder
= cp_lexer_consume_token (parser
->lexer
);
18216 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DECLTYPE
))
18218 placeholder
= cp_lexer_consume_token (parser
->lexer
);
18219 open_paren
= cp_parser_require (parser
, CPP_OPEN_PAREN
,
18221 cp_parser_require_keyword (parser
, RID_AUTO
, RT_AUTO
);
18222 close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
18224 open_paren
->location
);
18228 /* A type constraint constrains a contextually determined type or type
18229 parameter pack. However, the the Concepts TS does allow concepts
18230 to introduce non-type and template template parameters. */
18231 if (TREE_CODE (proto
) != TYPE_DECL
)
18233 if (!flag_concepts_ts
18234 || !processing_template_parmlist
)
18236 error_at (loc
, "%qE does not constrain a type", DECL_NAME (con
));
18237 inform (DECL_SOURCE_LOCATION (con
), "concept defined here");
18238 return error_mark_node
;
18242 /* In a template parameter list, a type-parameter can be introduced
18243 by type-constraints alone. */
18244 if (processing_template_parmlist
&& !placeholder
)
18245 return build_constrained_parameter (con
, proto
, args
);
18247 /* Diagnose issues placeholder issues. */
18248 if (!flag_concepts_ts
18249 && !parser
->in_result_type_constraint_p
18252 tree id
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, args
);
18253 tree expr
= DECL_P (orig_tmpl
) ? DECL_NAME (con
) : id
;
18254 error_at (input_location
,
18255 "expected %<auto%> or %<decltype(auto)%> after %qE", expr
);
18256 /* Fall through. This is an error of omission. */
18258 else if (parser
->in_result_type_constraint_p
&& placeholder
)
18260 /* A trailing return type only allows type-constraints. */
18261 error_at (input_location
,
18262 "unexpected placeholder in constrained result type");
18265 /* In a parameter-declaration-clause, a placeholder-type-specifier
18266 results in an invented template parameter. */
18267 if (parser
->auto_is_implicit_function_template_parm_p
)
18269 if (placeholder
&& token_is_decltype (placeholder
))
18271 location_t loc
= make_location (placeholder
->location
,
18272 placeholder
->location
,
18273 close_paren
->location
);
18274 error_at (loc
, "cannot declare a parameter with %<decltype(auto)%>");
18275 return error_mark_node
;
18277 tree parm
= build_constrained_parameter (con
, proto
, args
);
18278 return synthesize_implicit_template_parm (parser
, parm
);
18281 /* Determine if the type should be deduced using template argument
18282 deduction or decltype deduction. Note that the latter is always
18283 used for type-constraints in trailing return types. */
18284 bool decltype_p
= placeholder
18285 ? placeholder
->keyword
== RID_DECLTYPE
18286 : parser
->in_result_type_constraint_p
;
18288 /* Otherwise, this is the type of a variable or return type. */
18290 return make_constrained_decltype_auto (con
, args
);
18292 return make_constrained_auto (con
, args
);
18295 /* Parse a type-name.
18301 simple-template-id [in c++0x]
18318 Returns a TYPE_DECL for the type. */
18321 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
18325 /* We can't know yet whether it is a class-name or not. */
18326 cp_parser_parse_tentatively (parser
);
18327 /* Try a class-name. */
18328 type_decl
= cp_parser_class_name (parser
,
18329 typename_keyword_p
,
18330 /*template_keyword_p=*/false,
18332 /*check_dependency_p=*/true,
18333 /*class_head_p=*/false,
18334 /*is_declaration=*/false);
18335 /* If it's not a class-name, keep looking. */
18336 if (!cp_parser_parse_definitely (parser
))
18338 if (cxx_dialect
< cxx11
)
18339 /* It must be a typedef-name or an enum-name. */
18340 return cp_parser_nonclass_name (parser
);
18342 cp_parser_parse_tentatively (parser
);
18343 /* It is either a simple-template-id representing an
18344 instantiation of an alias template... */
18345 type_decl
= cp_parser_template_id (parser
,
18346 /*template_keyword_p=*/false,
18347 /*check_dependency_p=*/true,
18349 /*is_declaration=*/false);
18350 /* Note that this must be an instantiation of an alias template
18351 because [temp.names]/6 says:
18353 A template-id that names an alias template specialization
18356 Whereas [temp.names]/7 says:
18358 A simple-template-id that names a class template
18359 specialization is a class-name.
18361 With concepts, this could also be a partial-concept-id that
18362 declares a non-type template parameter. */
18363 if (type_decl
!= NULL_TREE
18364 && TREE_CODE (type_decl
) == TYPE_DECL
18365 && TYPE_DECL_ALIAS_P (type_decl
))
18366 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
18368 cp_parser_simulate_error (parser
);
18370 if (!cp_parser_parse_definitely (parser
))
18371 /* ... Or a typedef-name or an enum-name. */
18372 return cp_parser_nonclass_name (parser
);
18378 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
18390 Returns a TYPE_DECL for the type. */
18393 cp_parser_nonclass_name (cp_parser
* parser
)
18398 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18399 identifier
= cp_parser_identifier (parser
);
18400 if (identifier
== error_mark_node
)
18401 return error_mark_node
;
18403 /* Look up the type-name. */
18404 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
18406 type_decl
= strip_using_decl (type_decl
);
18408 if (TREE_CODE (type_decl
) != TYPE_DECL
18409 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
18411 /* See if this is an Objective-C type. */
18412 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18413 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
18415 type_decl
= TYPE_NAME (type
);
18418 /* Issue an error if we did not find a type-name. */
18419 if (TREE_CODE (type_decl
) != TYPE_DECL
18420 /* In Objective-C, we have the complication that class names are
18421 normally type names and start declarations (eg, the
18422 "NSObject" in "NSObject *object;"), but can be used in an
18423 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
18424 is an expression. So, a classname followed by a dot is not a
18425 valid type-name. */
18426 || (objc_is_class_name (TREE_TYPE (type_decl
))
18427 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
18429 if (!cp_parser_simulate_error (parser
))
18430 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
18431 NLE_TYPE
, token
->location
);
18432 return error_mark_node
;
18434 /* Remember that the name was used in the definition of the
18435 current class so that we can check later to see if the
18436 meaning would have been different after the class was
18437 entirely defined. */
18438 else if (type_decl
!= error_mark_node
18440 maybe_note_name_used_in_class (identifier
, type_decl
);
18445 /* Parse an elaborated-type-specifier. Note that the grammar given
18446 here incorporates the resolution to DR68.
18448 elaborated-type-specifier:
18449 class-key :: [opt] nested-name-specifier [opt] identifier
18450 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
18451 enum-key :: [opt] nested-name-specifier [opt] identifier
18452 typename :: [opt] nested-name-specifier identifier
18453 typename :: [opt] nested-name-specifier template [opt]
18458 elaborated-type-specifier:
18459 class-key attributes :: [opt] nested-name-specifier [opt] identifier
18460 class-key attributes :: [opt] nested-name-specifier [opt]
18461 template [opt] template-id
18462 enum attributes :: [opt] nested-name-specifier [opt] identifier
18464 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
18465 declared `friend'. If IS_DECLARATION is TRUE, then this
18466 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
18467 something is being declared.
18469 Returns the TYPE specified. */
18472 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
18474 bool is_declaration
)
18476 enum tag_types tag_type
;
18478 tree type
= NULL_TREE
;
18479 tree attributes
= NULL_TREE
;
18481 cp_token
*token
= NULL
;
18483 /* See if we're looking at the `enum' keyword. */
18484 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
18486 /* Consume the `enum' token. */
18487 cp_lexer_consume_token (parser
->lexer
);
18488 /* Remember that it's an enumeration type. */
18489 tag_type
= enum_type
;
18490 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
18491 enums) is used here. */
18492 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18493 if (cp_parser_is_keyword (token
, RID_CLASS
)
18494 || cp_parser_is_keyword (token
, RID_STRUCT
))
18496 gcc_rich_location
richloc (token
->location
);
18497 richloc
.add_range (input_location
);
18498 richloc
.add_fixit_remove ();
18499 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
18500 "a scoped enum must not use the %qD keyword",
18502 /* Consume the `struct' or `class' and parse it anyway. */
18503 cp_lexer_consume_token (parser
->lexer
);
18505 /* Parse the attributes. */
18506 attributes
= cp_parser_attributes_opt (parser
);
18508 /* Or, it might be `typename'. */
18509 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
18512 /* Consume the `typename' token. */
18513 cp_lexer_consume_token (parser
->lexer
);
18514 /* Remember that it's a `typename' type. */
18515 tag_type
= typename_type
;
18517 /* Otherwise it must be a class-key. */
18520 tag_type
= cp_parser_class_key (parser
);
18521 if (tag_type
== none_type
)
18522 return error_mark_node
;
18523 /* Parse the attributes. */
18524 attributes
= cp_parser_attributes_opt (parser
);
18527 /* Look for the `::' operator. */
18528 globalscope
= cp_parser_global_scope_opt (parser
,
18529 /*current_scope_valid_p=*/false);
18530 /* Look for the nested-name-specifier. */
18531 tree nested_name_specifier
;
18532 if (tag_type
== typename_type
&& !globalscope
)
18534 nested_name_specifier
18535 = cp_parser_nested_name_specifier (parser
,
18536 /*typename_keyword_p=*/true,
18537 /*check_dependency_p=*/true,
18540 if (!nested_name_specifier
)
18541 return error_mark_node
;
18544 /* Even though `typename' is not present, the proposed resolution
18545 to Core Issue 180 says that in `class A<T>::B', `B' should be
18546 considered a type-name, even if `A<T>' is dependent. */
18547 nested_name_specifier
18548 = cp_parser_nested_name_specifier_opt (parser
,
18549 /*typename_keyword_p=*/true,
18550 /*check_dependency_p=*/true,
18553 /* For everything but enumeration types, consider a template-id.
18554 For an enumeration type, consider only a plain identifier. */
18555 if (tag_type
!= enum_type
)
18557 bool template_p
= false;
18560 /* Allow the `template' keyword. */
18561 template_p
= cp_parser_optional_template_keyword (parser
);
18562 /* If we didn't see `template', we don't know if there's a
18563 template-id or not. */
18565 cp_parser_parse_tentatively (parser
);
18566 /* The `template' keyword must follow a nested-name-specifier. */
18567 else if (!nested_name_specifier
)
18569 cp_parser_error (parser
, "%<template%> must follow a nested-"
18571 return error_mark_node
;
18574 /* Parse the template-id. */
18575 token
= cp_lexer_peek_token (parser
->lexer
);
18576 decl
= cp_parser_template_id (parser
, template_p
,
18577 /*check_dependency_p=*/true,
18580 /* If we didn't find a template-id, look for an ordinary
18582 if (!template_p
&& !cp_parser_parse_definitely (parser
))
18584 /* We can get here when cp_parser_template_id, called by
18585 cp_parser_class_name with tag_type == none_type, succeeds
18586 and caches a BASELINK. Then, when called again here,
18587 instead of failing and returning an error_mark_node
18588 returns it (see template/typename17.C in C++11).
18589 ??? Could we diagnose this earlier? */
18590 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
18592 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
18593 type
= error_mark_node
;
18595 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
18596 in effect, then we must assume that, upon instantiation, the
18597 template will correspond to a class. */
18598 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
18599 && tag_type
== typename_type
)
18600 type
= make_typename_type (parser
->scope
, decl
,
18602 /*complain=*/tf_error
);
18603 /* If the `typename' keyword is in effect and DECL is not a type
18604 decl, then type is non existent. */
18605 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
18607 else if (TREE_CODE (decl
) == TYPE_DECL
)
18609 type
= check_elaborated_type_specifier (tag_type
, decl
,
18610 /*allow_template_p=*/true);
18612 /* If the next token is a semicolon, this must be a specialization,
18613 instantiation, or friend declaration. Check the scope while we
18614 still know whether or not we had a nested-name-specifier. */
18615 if (type
!= error_mark_node
18616 && !nested_name_specifier
&& !is_friend
18617 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
18618 check_unqualified_spec_or_inst (type
, token
->location
);
18620 else if (decl
== error_mark_node
)
18621 type
= error_mark_node
;
18626 token
= cp_lexer_peek_token (parser
->lexer
);
18627 identifier
= cp_parser_identifier (parser
);
18629 if (identifier
== error_mark_node
)
18631 parser
->scope
= NULL_TREE
;
18632 return error_mark_node
;
18635 /* For a `typename', we needn't call xref_tag. */
18636 if (tag_type
== typename_type
18637 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
18638 return cp_parser_make_typename_type (parser
, identifier
,
18641 /* Template parameter lists apply only if we are not within a
18642 function parameter list. */
18643 bool template_parm_lists_apply
18644 = parser
->num_template_parameter_lists
;
18645 if (template_parm_lists_apply
)
18646 for (cp_binding_level
*s
= current_binding_level
;
18647 s
&& s
->kind
!= sk_template_parms
;
18648 s
= s
->level_chain
)
18649 if (s
->kind
== sk_function_parms
)
18650 template_parm_lists_apply
= false;
18652 /* Look up a qualified name in the usual way. */
18656 tree ambiguous_decls
;
18658 decl
= cp_parser_lookup_name (parser
, identifier
,
18660 /*is_template=*/false,
18661 /*is_namespace=*/false,
18662 /*check_dependency=*/true,
18666 /* If the lookup was ambiguous, an error will already have been
18668 if (ambiguous_decls
)
18669 return error_mark_node
;
18671 /* If we are parsing friend declaration, DECL may be a
18672 TEMPLATE_DECL tree node here. However, we need to check
18673 whether this TEMPLATE_DECL results in valid code. Consider
18674 the following example:
18677 template <class T> class C {};
18680 template <class T> friend class N::C; // #1, valid code
18682 template <class T> class Y {
18683 friend class N::C; // #2, invalid code
18686 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
18687 name lookup of `N::C'. We see that friend declaration must
18688 be template for the code to be valid. Note that
18689 processing_template_decl does not work here since it is
18690 always 1 for the above two cases. */
18692 decl
= (cp_parser_maybe_treat_template_as_class
18693 (decl
, /*tag_name_p=*/is_friend
18694 && template_parm_lists_apply
));
18696 if (TREE_CODE (decl
) != TYPE_DECL
)
18698 cp_parser_diagnose_invalid_type_name (parser
,
18701 return error_mark_node
;
18704 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
18706 bool allow_template
= (template_parm_lists_apply
18707 || DECL_SELF_REFERENCE_P (decl
));
18708 type
= check_elaborated_type_specifier (tag_type
, decl
,
18711 if (type
== error_mark_node
)
18712 return error_mark_node
;
18715 /* Forward declarations of nested types, such as
18720 are invalid unless all components preceding the final '::'
18721 are complete. If all enclosing types are complete, these
18722 declarations become merely pointless.
18724 Invalid forward declarations of nested types are errors
18725 caught elsewhere in parsing. Those that are pointless arrive
18728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
18729 && !is_friend
&& !processing_explicit_instantiation
)
18730 warning (0, "declaration %qD does not declare anything", decl
);
18732 type
= TREE_TYPE (decl
);
18736 /* An elaborated-type-specifier sometimes introduces a new type and
18737 sometimes names an existing type. Normally, the rule is that it
18738 introduces a new type only if there is not an existing type of
18739 the same name already in scope. For example, given:
18742 void f() { struct S s; }
18744 the `struct S' in the body of `f' is the same `struct S' as in
18745 the global scope; the existing definition is used. However, if
18746 there were no global declaration, this would introduce a new
18747 local class named `S'.
18749 An exception to this rule applies to the following code:
18751 namespace N { struct S; }
18753 Here, the elaborated-type-specifier names a new type
18754 unconditionally; even if there is already an `S' in the
18755 containing scope this declaration names a new type.
18756 This exception only applies if the elaborated-type-specifier
18757 forms the complete declaration:
18761 A declaration consisting solely of `class-key identifier ;' is
18762 either a redeclaration of the name in the current scope or a
18763 forward declaration of the identifier as a class name. It
18764 introduces the name into the current scope.
18766 We are in this situation precisely when the next token is a `;'.
18768 An exception to the exception is that a `friend' declaration does
18769 *not* name a new type; i.e., given:
18771 struct S { friend struct T; };
18773 `T' is not a new type in the scope of `S'.
18775 Also, `new struct S' or `sizeof (struct S)' never results in the
18776 definition of a new type; a new type can only be declared in a
18777 declaration context. */
18783 /* Friends have special name lookup rules. */
18784 ts
= ts_within_enclosing_non_class
;
18785 else if (is_declaration
18786 && cp_lexer_next_token_is (parser
->lexer
,
18788 /* This is a `class-key identifier ;' */
18794 (template_parm_lists_apply
18795 && (cp_parser_next_token_starts_class_definition_p (parser
)
18796 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
18797 /* An unqualified name was used to reference this type, so
18798 there were no qualifying templates. */
18799 if (template_parm_lists_apply
18800 && !cp_parser_check_template_parameters (parser
,
18801 /*num_templates=*/0,
18802 /*template_id*/false,
18804 /*declarator=*/NULL
))
18805 return error_mark_node
;
18806 type
= xref_tag (tag_type
, identifier
, ts
, template_p
);
18810 if (type
== error_mark_node
)
18811 return error_mark_node
;
18813 /* Allow attributes on forward declarations of classes. */
18816 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18817 warning (OPT_Wattributes
,
18818 "attributes ignored on uninstantiated type");
18819 else if (tag_type
!= enum_type
&& CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
18820 && ! processing_explicit_instantiation
)
18821 warning (OPT_Wattributes
,
18822 "attributes ignored on template instantiation");
18823 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
18824 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
18826 warning (OPT_Wattributes
,
18827 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
18830 if (tag_type
!= enum_type
)
18832 /* Indicate whether this class was declared as a `class' or as a
18834 if (CLASS_TYPE_P (type
))
18835 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
18836 cp_parser_check_class_key (tag_type
, type
);
18839 /* A "<" cannot follow an elaborated type specifier. If that
18840 happens, the user was probably trying to form a template-id. */
18841 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
18847 /* Parse an enum-specifier.
18850 enum-head { enumerator-list [opt] }
18851 enum-head { enumerator-list , } [C++0x]
18854 enum-key identifier [opt] enum-base [opt]
18855 enum-key nested-name-specifier identifier enum-base [opt]
18860 enum struct [C++0x]
18863 : type-specifier-seq
18865 opaque-enum-specifier:
18866 enum-key identifier enum-base [opt] ;
18869 enum-key attributes[opt] identifier [opt] enum-base [opt]
18870 { enumerator-list [opt] }attributes[opt]
18871 enum-key attributes[opt] identifier [opt] enum-base [opt]
18872 { enumerator-list, }attributes[opt] [C++0x]
18874 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
18875 if the token stream isn't an enum-specifier after all. */
18878 cp_parser_enum_specifier (cp_parser
* parser
)
18881 tree type
= NULL_TREE
;
18883 tree nested_name_specifier
= NULL_TREE
;
18885 bool scoped_enum_p
= false;
18886 bool has_underlying_type
= false;
18887 bool nested_being_defined
= false;
18888 bool new_value_list
= false;
18889 bool is_new_type
= false;
18890 bool is_unnamed
= false;
18891 tree underlying_type
= NULL_TREE
;
18892 cp_token
*type_start_token
= NULL
;
18893 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
18895 parser
->colon_corrects_to_scope_p
= false;
18897 /* Parse tentatively so that we can back up if we don't find a
18899 cp_parser_parse_tentatively (parser
);
18901 /* Caller guarantees that the current token is 'enum', an identifier
18902 possibly follows, and the token after that is an opening brace.
18903 If we don't have an identifier, fabricate an anonymous name for
18904 the enumeration being defined. */
18905 cp_lexer_consume_token (parser
->lexer
);
18907 /* Parse the "class" or "struct", which indicates a scoped
18908 enumeration type in C++0x. */
18909 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
18910 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
18912 if (cxx_dialect
< cxx11
)
18913 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
18915 /* Consume the `struct' or `class' token. */
18916 cp_lexer_consume_token (parser
->lexer
);
18918 scoped_enum_p
= true;
18921 attributes
= cp_parser_attributes_opt (parser
);
18923 /* Clear the qualification. */
18924 parser
->scope
= NULL_TREE
;
18925 parser
->qualifying_scope
= NULL_TREE
;
18926 parser
->object_scope
= NULL_TREE
;
18928 /* Figure out in what scope the declaration is being placed. */
18929 prev_scope
= current_scope ();
18931 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
18933 push_deferring_access_checks (dk_no_check
);
18934 nested_name_specifier
18935 = cp_parser_nested_name_specifier_opt (parser
,
18936 /*typename_keyword_p=*/true,
18937 /*check_dependency_p=*/false,
18939 /*is_declaration=*/false);
18941 if (nested_name_specifier
)
18945 identifier
= cp_parser_identifier (parser
);
18946 name
= cp_parser_lookup_name (parser
, identifier
,
18948 /*is_template=*/false,
18949 /*is_namespace=*/false,
18950 /*check_dependency=*/true,
18951 /*ambiguous_decls=*/NULL
,
18953 if (name
&& name
!= error_mark_node
)
18955 type
= TREE_TYPE (name
);
18956 if (TREE_CODE (type
) == TYPENAME_TYPE
)
18958 /* Are template enums allowed in ISO? */
18959 if (template_parm_scope_p ())
18960 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
18961 "%qD is an enumeration template", name
);
18962 /* ignore a typename reference, for it will be solved by name
18967 else if (nested_name_specifier
== error_mark_node
)
18968 /* We already issued an error. */;
18971 error_at (type_start_token
->location
,
18972 "%qD does not name an enumeration in %qT",
18973 identifier
, nested_name_specifier
);
18974 nested_name_specifier
= error_mark_node
;
18979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
18980 identifier
= cp_parser_identifier (parser
);
18983 identifier
= make_anon_name ();
18986 error_at (type_start_token
->location
,
18987 "unnamed scoped enum is not allowed");
18990 pop_deferring_access_checks ();
18992 /* Check for the `:' that denotes a specified underlying type in C++0x.
18993 Note that a ':' could also indicate a bitfield width, however. */
18994 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
18996 cp_decl_specifier_seq type_specifiers
;
18998 /* Consume the `:'. */
18999 cp_lexer_consume_token (parser
->lexer
);
19001 /* Parse the type-specifier-seq. */
19002 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
19003 /*is_declaration=*/false,
19004 /*is_trailing_return=*/false,
19007 /* At this point this is surely not elaborated type specifier. */
19008 if (!cp_parser_parse_definitely (parser
))
19011 if (cxx_dialect
< cxx11
)
19012 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19014 has_underlying_type
= true;
19016 /* If that didn't work, stop. */
19017 if (type_specifiers
.type
!= error_mark_node
)
19019 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
19020 /*initialized=*/0, NULL
);
19021 if (underlying_type
== error_mark_node
19022 || check_for_bare_parameter_packs (underlying_type
))
19023 underlying_type
= NULL_TREE
;
19027 /* Look for the `{' but don't consume it yet. */
19028 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19030 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
19032 cp_parser_error (parser
, "expected %<{%>");
19033 if (has_underlying_type
)
19039 /* An opaque-enum-specifier must have a ';' here. */
19040 if ((scoped_enum_p
|| underlying_type
)
19041 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19043 cp_parser_error (parser
, "expected %<;%> or %<{%>");
19044 if (has_underlying_type
)
19052 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
19055 if (nested_name_specifier
)
19057 if (CLASS_TYPE_P (nested_name_specifier
))
19059 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
19060 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
19061 push_scope (nested_name_specifier
);
19063 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19065 push_nested_namespace (nested_name_specifier
);
19069 /* Issue an error message if type-definitions are forbidden here. */
19070 if (!cp_parser_check_type_definition (parser
))
19071 type
= error_mark_node
;
19073 /* Create the new type. We do this before consuming the opening
19074 brace so the enum will be recorded as being on the line of its
19075 tag (or the 'enum' keyword, if there is no tag). */
19076 type
= start_enum (identifier
, type
, underlying_type
,
19077 attributes
, scoped_enum_p
, &is_new_type
);
19079 /* If the next token is not '{' it is an opaque-enum-specifier or an
19080 elaborated-type-specifier. */
19081 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19083 timevar_push (TV_PARSE_ENUM
);
19084 if (nested_name_specifier
19085 && nested_name_specifier
!= error_mark_node
)
19087 /* The following catches invalid code such as:
19088 enum class S<int>::E { A, B, C }; */
19089 if (!processing_specialization
19090 && CLASS_TYPE_P (nested_name_specifier
)
19091 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
19092 error_at (type_start_token
->location
, "cannot add an enumerator "
19093 "list to a template instantiation");
19095 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
19097 error_at (type_start_token
->location
,
19098 "%<%T::%E%> has not been declared",
19099 TYPE_CONTEXT (nested_name_specifier
),
19100 nested_name_specifier
);
19101 type
= error_mark_node
;
19103 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
19104 && !CLASS_TYPE_P (nested_name_specifier
))
19106 error_at (type_start_token
->location
, "nested name specifier "
19107 "%qT for enum declaration does not name a class "
19108 "or namespace", nested_name_specifier
);
19109 type
= error_mark_node
;
19111 /* If that scope does not contain the scope in which the
19112 class was originally declared, the program is invalid. */
19113 else if (prev_scope
&& !is_ancestor (prev_scope
,
19114 nested_name_specifier
))
19116 if (at_namespace_scope_p ())
19117 error_at (type_start_token
->location
,
19118 "declaration of %qD in namespace %qD which does not "
19120 type
, prev_scope
, nested_name_specifier
);
19122 error_at (type_start_token
->location
,
19123 "declaration of %qD in %qD which does not "
19125 type
, prev_scope
, nested_name_specifier
);
19126 type
= error_mark_node
;
19128 /* If that scope is the scope where the declaration is being placed
19129 the program is invalid. */
19130 else if (CLASS_TYPE_P (nested_name_specifier
)
19131 && CLASS_TYPE_P (prev_scope
)
19132 && same_type_p (nested_name_specifier
, prev_scope
))
19134 permerror (type_start_token
->location
,
19135 "extra qualification not allowed");
19136 nested_name_specifier
= NULL_TREE
;
19141 begin_scope (sk_scoped_enum
, type
);
19143 /* Consume the opening brace. */
19144 matching_braces braces
;
19145 braces
.consume_open (parser
);
19147 if (type
== error_mark_node
)
19148 ; /* Nothing to add */
19149 else if (OPAQUE_ENUM_P (type
)
19150 || (cxx_dialect
> cxx98
&& processing_specialization
))
19152 new_value_list
= true;
19153 SET_OPAQUE_ENUM_P (type
, false);
19154 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
19158 error_at (type_start_token
->location
,
19159 "multiple definition of %q#T", type
);
19160 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
19161 "previous definition here");
19162 type
= error_mark_node
;
19165 if (type
== error_mark_node
)
19166 cp_parser_skip_to_end_of_block_or_statement (parser
);
19167 /* If the next token is not '}', then there are some enumerators. */
19168 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19170 if (is_unnamed
&& !scoped_enum_p
)
19171 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
19172 "ISO C++ forbids empty unnamed enum");
19175 cp_parser_enumerator_list (parser
, type
);
19177 /* Consume the final '}'. */
19178 braces
.require_close (parser
);
19182 timevar_pop (TV_PARSE_ENUM
);
19186 /* If a ';' follows, then it is an opaque-enum-specifier
19187 and additional restrictions apply. */
19188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19191 error_at (type_start_token
->location
,
19192 "opaque-enum-specifier without name");
19193 else if (nested_name_specifier
)
19194 error_at (type_start_token
->location
,
19195 "opaque-enum-specifier must use a simple identifier");
19199 /* Look for trailing attributes to apply to this enumeration, and
19200 apply them if appropriate. */
19201 if (cp_parser_allow_gnu_extensions_p (parser
))
19203 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
19204 cplus_decl_attributes (&type
,
19206 (int) ATTR_FLAG_TYPE_IN_PLACE
);
19209 /* Finish up the enumeration. */
19210 if (type
!= error_mark_node
)
19212 if (new_value_list
)
19213 finish_enum_value_list (type
);
19215 finish_enum (type
);
19218 if (nested_name_specifier
)
19220 if (CLASS_TYPE_P (nested_name_specifier
))
19222 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
19223 pop_scope (nested_name_specifier
);
19225 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19227 pop_nested_namespace (nested_name_specifier
);
19231 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
19235 /* Parse an enumerator-list. The enumerators all have the indicated
19239 enumerator-definition
19240 enumerator-list , enumerator-definition */
19243 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
19247 /* Parse an enumerator-definition. */
19248 cp_parser_enumerator_definition (parser
, type
);
19250 /* If the next token is not a ',', we've reached the end of
19252 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
19254 /* Otherwise, consume the `,' and keep going. */
19255 cp_lexer_consume_token (parser
->lexer
);
19256 /* If the next token is a `}', there is a trailing comma. */
19257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
19259 if (cxx_dialect
< cxx11
)
19260 pedwarn (input_location
, OPT_Wpedantic
,
19261 "comma at end of enumerator list");
19267 /* Parse an enumerator-definition. The enumerator has the indicated
19270 enumerator-definition:
19272 enumerator = constant-expression
19279 enumerator-definition:
19280 enumerator attributes [opt]
19281 enumerator attributes [opt] = constant-expression */
19284 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
19290 /* Save the input location because we are interested in the location
19291 of the identifier and not the location of the explicit value. */
19292 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19294 /* Look for the identifier. */
19295 identifier
= cp_parser_identifier (parser
);
19296 if (identifier
== error_mark_node
)
19299 /* Parse any specified attributes. */
19300 tree attrs
= cp_parser_attributes_opt (parser
);
19302 /* If the next token is an '=', then there is an explicit value. */
19303 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
19305 /* Consume the `=' token. */
19306 cp_lexer_consume_token (parser
->lexer
);
19307 /* Parse the value. */
19308 value
= cp_parser_constant_expression (parser
);
19313 /* If we are processing a template, make sure the initializer of the
19314 enumerator doesn't contain any bare template parameter pack. */
19315 if (check_for_bare_parameter_packs (value
))
19316 value
= error_mark_node
;
19318 /* Create the enumerator. */
19319 build_enumerator (identifier
, value
, type
, attrs
, loc
);
19322 /* Parse a namespace-name.
19325 original-namespace-name
19328 Returns the NAMESPACE_DECL for the namespace. */
19331 cp_parser_namespace_name (cp_parser
* parser
)
19334 tree namespace_decl
;
19336 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19338 /* Get the name of the namespace. */
19339 identifier
= cp_parser_identifier (parser
);
19340 if (identifier
== error_mark_node
)
19341 return error_mark_node
;
19343 /* Look up the identifier in the currently active scope. Look only
19344 for namespaces, due to:
19346 [basic.lookup.udir]
19348 When looking up a namespace-name in a using-directive or alias
19349 definition, only namespace names are considered.
19353 [basic.lookup.qual]
19355 During the lookup of a name preceding the :: scope resolution
19356 operator, object, function, and enumerator names are ignored.
19358 (Note that cp_parser_qualifying_entity only calls this
19359 function if the token after the name is the scope resolution
19361 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
19363 /*is_template=*/false,
19364 /*is_namespace=*/true,
19365 /*check_dependency=*/true,
19366 /*ambiguous_decls=*/NULL
,
19368 /* If it's not a namespace, issue an error. */
19369 if (namespace_decl
== error_mark_node
19370 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
19372 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
19374 auto_diagnostic_group d
;
19376 if (namespace_decl
== error_mark_node
19377 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
19378 hint
= suggest_alternative_in_explicit_scope (token
->location
,
19381 if (const char *suggestion
= hint
.suggestion ())
19383 gcc_rich_location
richloc (token
->location
);
19384 richloc
.add_fixit_replace (suggestion
);
19385 error_at (&richloc
,
19386 "%qD is not a namespace-name; did you mean %qs?",
19387 identifier
, suggestion
);
19390 error_at (token
->location
, "%qD is not a namespace-name",
19394 cp_parser_error (parser
, "expected namespace-name");
19395 namespace_decl
= error_mark_node
;
19398 return namespace_decl
;
19401 /* Parse a namespace-definition.
19403 namespace-definition:
19404 named-namespace-definition
19405 unnamed-namespace-definition
19407 named-namespace-definition:
19408 original-namespace-definition
19409 extension-namespace-definition
19411 original-namespace-definition:
19412 namespace identifier { namespace-body }
19414 extension-namespace-definition:
19415 namespace original-namespace-name { namespace-body }
19417 unnamed-namespace-definition:
19418 namespace { namespace-body } */
19421 cp_parser_namespace_definition (cp_parser
* parser
)
19424 int nested_definition_count
= 0;
19426 cp_ensure_no_omp_declare_simd (parser
);
19427 cp_ensure_no_oacc_routine (parser
);
19429 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
19430 const bool topmost_inline_p
= is_inline
;
19434 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
19435 cp_lexer_consume_token (parser
->lexer
);
19438 /* Look for the `namespace' keyword. */
19440 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19442 /* Parse any specified attributes before the identifier. */
19443 tree attribs
= cp_parser_attributes_opt (parser
);
19447 identifier
= NULL_TREE
;
19449 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
19451 if (nested_inline_p
&& nested_definition_count
!= 0)
19453 if (cxx_dialect
< cxx2a
)
19454 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
19455 OPT_Wpedantic
, "nested inline namespace definitions only "
19456 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
19457 cp_lexer_consume_token (parser
->lexer
);
19460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19462 identifier
= cp_parser_identifier (parser
);
19464 if (cp_next_tokens_can_be_std_attribute_p (parser
))
19465 pedwarn (input_location
, OPT_Wpedantic
,
19466 "standard attributes on namespaces must precede "
19467 "the namespace name");
19469 /* Parse any attributes specified after the identifier. */
19470 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
19473 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
19475 /* Don't forget that the innermost namespace might have been
19476 marked as inline. Use |= because we cannot overwrite
19477 IS_INLINE in case the outermost namespace is inline, but
19478 there are no nested inlines. */
19479 is_inline
|= nested_inline_p
;
19483 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
19484 pedwarn (input_location
, OPT_Wpedantic
,
19485 "nested namespace definitions only available with "
19486 "%<-std=c++17%> or %<-std=gnu++17%>");
19488 /* Nested namespace names can create new namespaces (unlike
19489 other qualified-ids). */
19490 if (int count
= (identifier
19491 ? push_namespace (identifier
, nested_inline_p
)
19493 nested_definition_count
+= count
;
19495 cp_parser_error (parser
, "nested namespace name required");
19496 cp_lexer_consume_token (parser
->lexer
);
19499 if (nested_definition_count
&& !identifier
)
19500 cp_parser_error (parser
, "namespace name required");
19502 if (nested_definition_count
&& attribs
)
19503 error_at (token
->location
,
19504 "a nested namespace definition cannot have attributes");
19505 if (nested_definition_count
&& topmost_inline_p
)
19506 error_at (token
->location
,
19507 "a nested namespace definition cannot be inline");
19509 /* Start the namespace. */
19510 nested_definition_count
+= push_namespace (identifier
, is_inline
);
19512 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
19514 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
19516 /* Look for the `{' to validate starting the namespace. */
19517 matching_braces braces
;
19518 if (braces
.require_open (parser
))
19520 /* Parse the body of the namespace. */
19521 cp_parser_namespace_body (parser
);
19523 /* Look for the final `}'. */
19524 braces
.require_close (parser
);
19527 if (has_visibility
)
19528 pop_visibility (1);
19530 /* Pop the nested namespace definitions. */
19531 while (nested_definition_count
--)
19535 /* Parse a namespace-body.
19538 declaration-seq [opt] */
19541 cp_parser_namespace_body (cp_parser
* parser
)
19543 cp_parser_declaration_seq_opt (parser
);
19546 /* Parse a namespace-alias-definition.
19548 namespace-alias-definition:
19549 namespace identifier = qualified-namespace-specifier ; */
19552 cp_parser_namespace_alias_definition (cp_parser
* parser
)
19555 tree namespace_specifier
;
19557 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19559 /* Look for the `namespace' keyword. */
19560 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19561 /* Look for the identifier. */
19562 identifier
= cp_parser_identifier (parser
);
19563 if (identifier
== error_mark_node
)
19565 /* Look for the `=' token. */
19566 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
19567 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19569 error_at (token
->location
, "%<namespace%> definition is not allowed here");
19570 /* Skip the definition. */
19571 cp_lexer_consume_token (parser
->lexer
);
19572 if (cp_parser_skip_to_closing_brace (parser
))
19573 cp_lexer_consume_token (parser
->lexer
);
19576 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19577 /* Look for the qualified-namespace-specifier. */
19578 namespace_specifier
19579 = cp_parser_qualified_namespace_specifier (parser
);
19580 cp_warn_deprecated_use_scopes (namespace_specifier
);
19581 /* Look for the `;' token. */
19582 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19584 /* Register the alias in the symbol table. */
19585 do_namespace_alias (identifier
, namespace_specifier
);
19588 /* Parse a qualified-namespace-specifier.
19590 qualified-namespace-specifier:
19591 :: [opt] nested-name-specifier [opt] namespace-name
19593 Returns a NAMESPACE_DECL corresponding to the specified
19597 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
19599 /* Look for the optional `::'. */
19600 cp_parser_global_scope_opt (parser
,
19601 /*current_scope_valid_p=*/false);
19603 /* Look for the optional nested-name-specifier. */
19604 cp_parser_nested_name_specifier_opt (parser
,
19605 /*typename_keyword_p=*/false,
19606 /*check_dependency_p=*/true,
19608 /*is_declaration=*/true);
19610 return cp_parser_namespace_name (parser
);
19613 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
19614 access declaration.
19617 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
19618 using :: unqualified-id ;
19620 access-declaration:
19626 cp_parser_using_declaration (cp_parser
* parser
,
19627 bool access_declaration_p
)
19630 bool typename_p
= false;
19631 bool global_scope_p
;
19635 int oldcount
= errorcount
;
19636 cp_token
*diag_token
= NULL
;
19638 if (access_declaration_p
)
19640 diag_token
= cp_lexer_peek_token (parser
->lexer
);
19641 cp_parser_parse_tentatively (parser
);
19645 /* Look for the `using' keyword. */
19646 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19649 /* Peek at the next token. */
19650 token
= cp_lexer_peek_token (parser
->lexer
);
19651 /* See if it's `typename'. */
19652 if (token
->keyword
== RID_TYPENAME
)
19654 /* Remember that we've seen it. */
19656 /* Consume the `typename' token. */
19657 cp_lexer_consume_token (parser
->lexer
);
19661 /* Look for the optional global scope qualification. */
19663 = (cp_parser_global_scope_opt (parser
,
19664 /*current_scope_valid_p=*/false)
19667 /* If we saw `typename', or didn't see `::', then there must be a
19668 nested-name-specifier present. */
19669 if (typename_p
|| !global_scope_p
)
19671 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
19672 /*check_dependency_p=*/true,
19674 /*is_declaration=*/true);
19675 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
19677 cp_parser_skip_to_end_of_block_or_statement (parser
);
19681 /* Otherwise, we could be in either of the two productions. In that
19682 case, treat the nested-name-specifier as optional. */
19684 qscope
= cp_parser_nested_name_specifier_opt (parser
,
19685 /*typename_keyword_p=*/false,
19686 /*check_dependency_p=*/true,
19688 /*is_declaration=*/true);
19690 qscope
= global_namespace
;
19691 else if (UNSCOPED_ENUM_P (qscope
)
19692 && !TYPE_FUNCTION_SCOPE_P (qscope
))
19693 qscope
= CP_TYPE_CONTEXT (qscope
);
19695 cp_warn_deprecated_use_scopes (qscope
);
19697 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
19698 /* Something has already gone wrong; there's no need to parse
19699 further. Since an error has occurred, the return value of
19700 cp_parser_parse_definitely will be false, as required. */
19701 return cp_parser_parse_definitely (parser
);
19703 token
= cp_lexer_peek_token (parser
->lexer
);
19704 /* Parse the unqualified-id. */
19705 identifier
= cp_parser_unqualified_id (parser
,
19706 /*template_keyword_p=*/false,
19707 /*check_dependency_p=*/true,
19708 /*declarator_p=*/true,
19709 /*optional_p=*/false);
19711 if (access_declaration_p
)
19713 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19714 cp_parser_simulate_error (parser
);
19715 if (!cp_parser_parse_definitely (parser
))
19718 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
19720 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
19721 if (cxx_dialect
< cxx17
)
19722 pedwarn (ell
->location
, 0,
19723 "pack expansion in using-declaration only available "
19724 "with %<-std=c++17%> or %<-std=gnu++17%>");
19725 qscope
= make_pack_expansion (qscope
);
19728 /* The function we call to handle a using-declaration is different
19729 depending on what scope we are in. */
19730 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
19732 else if (!identifier_p (identifier
)
19733 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
19734 /* [namespace.udecl]
19736 A using declaration shall not name a template-id. */
19737 error_at (token
->location
,
19738 "a template-id may not appear in a using-declaration");
19741 if (at_class_scope_p ())
19743 /* Create the USING_DECL. */
19744 decl
= do_class_using_decl (qscope
, identifier
);
19746 if (decl
&& typename_p
)
19747 USING_DECL_TYPENAME_P (decl
) = 1;
19749 if (check_for_bare_parameter_packs (decl
))
19751 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19755 /* Add it to the list of members in this class. */
19756 finish_member_declaration (decl
);
19759 finish_nonmember_using_decl (qscope
, identifier
);
19762 if (!access_declaration_p
19763 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
19765 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
19766 if (cxx_dialect
< cxx17
)
19767 pedwarn (comma
->location
, 0,
19768 "comma-separated list in using-declaration only available "
19769 "with %<-std=c++17%> or %<-std=gnu++17%>");
19773 /* Look for the final `;'. */
19774 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19776 if (access_declaration_p
&& errorcount
== oldcount
)
19777 warning_at (diag_token
->location
, OPT_Wdeprecated
,
19778 "access declarations are deprecated "
19779 "in favour of using-declarations; "
19780 "suggestion: add the %<using%> keyword");
19785 /* Parse an alias-declaration.
19788 using identifier attribute-specifier-seq [opt] = type-id */
19791 cp_parser_alias_declaration (cp_parser
* parser
)
19793 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
19794 location_t id_location
, type_location
;
19795 cp_declarator
*declarator
;
19796 cp_decl_specifier_seq decl_specs
;
19798 const char *saved_message
= NULL
;
19800 /* Look for the `using' keyword. */
19801 cp_token
*using_token
19802 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19803 if (using_token
== NULL
)
19804 return error_mark_node
;
19806 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
19807 id
= cp_parser_identifier (parser
);
19808 if (id
== error_mark_node
)
19809 return error_mark_node
;
19811 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
19812 attributes
= cp_parser_attributes_opt (parser
);
19813 if (attributes
== error_mark_node
)
19814 return error_mark_node
;
19816 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
19818 if (cp_parser_error_occurred (parser
))
19819 return error_mark_node
;
19821 cp_parser_commit_to_tentative_parse (parser
);
19823 /* Now we are going to parse the type-id of the declaration. */
19828 "A type-specifier-seq shall not define a class or enumeration
19829 unless it appears in the type-id of an alias-declaration (7.1.3) that
19830 is not the declaration of a template-declaration."
19832 In other words, if we currently are in an alias template, the
19833 type-id should not define a type.
19835 So let's set parser->type_definition_forbidden_message in that
19836 case; cp_parser_check_type_definition (called by
19837 cp_parser_class_specifier) will then emit an error if a type is
19838 defined in the type-id. */
19839 if (parser
->num_template_parameter_lists
)
19841 saved_message
= parser
->type_definition_forbidden_message
;
19842 parser
->type_definition_forbidden_message
=
19843 G_("types may not be defined in alias template declarations");
19846 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
19849 /* Restore the error message if need be. */
19850 if (parser
->num_template_parameter_lists
)
19851 parser
->type_definition_forbidden_message
= saved_message
;
19853 if (type
== error_mark_node
19854 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
19856 cp_parser_skip_to_end_of_block_or_statement (parser
);
19857 return error_mark_node
;
19860 /* A typedef-name can also be introduced by an alias-declaration. The
19861 identifier following the using keyword becomes a typedef-name. It has
19862 the same semantics as if it were introduced by the typedef
19863 specifier. In particular, it does not define a new type and it shall
19864 not appear in the type-id. */
19866 clear_decl_specs (&decl_specs
);
19867 decl_specs
.type
= type
;
19868 if (attributes
!= NULL_TREE
)
19870 decl_specs
.attributes
= attributes
;
19871 set_and_check_decl_spec_loc (&decl_specs
,
19875 set_and_check_decl_spec_loc (&decl_specs
,
19878 set_and_check_decl_spec_loc (&decl_specs
,
19881 decl_specs
.locations
[ds_type_spec
] = type_location
;
19883 if (parser
->num_template_parameter_lists
19884 && !cp_parser_check_template_parameters (parser
,
19885 /*num_templates=*/0,
19886 /*template_id*/false,
19888 /*declarator=*/NULL
))
19889 return error_mark_node
;
19891 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
19893 member_p
= at_class_scope_p ();
19895 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
19896 NULL_TREE
, attributes
);
19898 decl
= start_decl (declarator
, &decl_specs
, 0,
19899 attributes
, NULL_TREE
, &pushed_scope
);
19900 if (decl
== error_mark_node
)
19903 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
19906 pop_scope (pushed_scope
);
19908 /* If decl is a template, return its TEMPLATE_DECL so that it gets
19909 added into the symbol table; otherwise, return the TYPE_DECL. */
19910 if (DECL_LANG_SPECIFIC (decl
)
19911 && DECL_TEMPLATE_INFO (decl
)
19912 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
19914 decl
= DECL_TI_TEMPLATE (decl
);
19916 check_member_template (decl
);
19922 /* Parse a using-directive.
19925 using namespace :: [opt] nested-name-specifier [opt]
19926 namespace-name ; */
19929 cp_parser_using_directive (cp_parser
* parser
)
19931 tree namespace_decl
;
19934 /* Look for the `using' keyword. */
19935 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
19936 /* And the `namespace' keyword. */
19937 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
19938 /* Look for the optional `::' operator. */
19939 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
19940 /* And the optional nested-name-specifier. */
19941 cp_parser_nested_name_specifier_opt (parser
,
19942 /*typename_keyword_p=*/false,
19943 /*check_dependency_p=*/true,
19945 /*is_declaration=*/true);
19946 /* Get the namespace being used. */
19947 namespace_decl
= cp_parser_namespace_name (parser
);
19948 cp_warn_deprecated_use_scopes (namespace_decl
);
19949 /* And any specified attributes. */
19950 attribs
= cp_parser_attributes_opt (parser
);
19952 /* Update the symbol table. */
19953 finish_using_directive (namespace_decl
, attribs
);
19955 /* Look for the final `;'. */
19956 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
19959 /* Parse an asm-definition.
19966 asm-qualifier-list:
19968 asm-qualifier-list asm-qualifier
19971 asm ( string-literal ) ;
19976 asm asm-qualifier-list [opt] ( string-literal ) ;
19977 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
19978 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19979 : asm-operand-list [opt] ) ;
19980 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
19981 : asm-operand-list [opt]
19982 : asm-clobber-list [opt] ) ;
19983 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
19984 : asm-clobber-list [opt]
19985 : asm-goto-list ) ;
19987 The form with asm-goto-list is valid if and only if the asm-qualifier-list
19988 contains goto, and is the only allowed form in that case. No duplicates are
19989 allowed in an asm-qualifier-list. */
19992 cp_parser_asm_definition (cp_parser
* parser
)
19995 tree outputs
= NULL_TREE
;
19996 tree inputs
= NULL_TREE
;
19997 tree clobbers
= NULL_TREE
;
19998 tree labels
= NULL_TREE
;
20000 bool extended_p
= false;
20001 bool invalid_inputs_p
= false;
20002 bool invalid_outputs_p
= false;
20003 required_token missing
= RT_NONE
;
20004 location_t asm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20006 /* Look for the `asm' keyword. */
20007 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
20009 /* In C++2a, unevaluated inline assembly is permitted in constexpr
20011 if (parser
->in_function_body
20012 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
20013 && (cxx_dialect
< cxx2a
))
20014 pedwarn (asm_loc
, 0, "%<asm%> in %<constexpr%> function only available "
20015 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
20017 /* Handle the asm-qualifier-list. */
20018 location_t volatile_loc
= UNKNOWN_LOCATION
;
20019 location_t inline_loc
= UNKNOWN_LOCATION
;
20020 location_t goto_loc
= UNKNOWN_LOCATION
;
20021 location_t first_loc
= UNKNOWN_LOCATION
;
20023 if (cp_parser_allow_gnu_extensions_p (parser
))
20026 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20027 location_t loc
= token
->location
;
20028 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
20033 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20035 inform (volatile_loc
, "first seen here");
20039 if (!parser
->in_function_body
)
20040 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
20041 "outside of function body", token
->u
.value
);
20042 volatile_loc
= loc
;
20044 cp_lexer_consume_token (parser
->lexer
);
20050 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20052 inform (inline_loc
, "first seen here");
20058 cp_lexer_consume_token (parser
->lexer
);
20064 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20066 inform (goto_loc
, "first seen here");
20072 cp_lexer_consume_token (parser
->lexer
);
20077 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
20078 cp_lexer_consume_token (parser
->lexer
);
20087 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
20088 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
20089 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
20091 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
20093 error_at (first_loc
, "%<asm%> qualifier outside of function body");
20094 inline_p
= goto_p
= false;
20097 /* Look for the opening `('. */
20098 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
20100 /* Look for the string. */
20101 string
= cp_parser_string_literal (parser
, false, false);
20102 if (string
== error_mark_node
)
20104 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20105 /*consume_paren=*/true);
20109 /* If we're allowing GNU extensions, check for the extended assembly
20110 syntax. Unfortunately, the `:' tokens need not be separated by
20111 a space in C, and so, for compatibility, we tolerate that here
20112 too. Doing that means that we have to treat the `::' operator as
20114 if (cp_parser_allow_gnu_extensions_p (parser
)
20115 && parser
->in_function_body
20116 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
20117 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
20119 bool inputs_p
= false;
20120 bool clobbers_p
= false;
20121 bool labels_p
= false;
20123 /* The extended syntax was used. */
20126 /* Look for outputs. */
20127 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20129 /* Consume the `:'. */
20130 cp_lexer_consume_token (parser
->lexer
);
20131 /* Parse the output-operands. */
20132 if (cp_lexer_next_token_is_not (parser
->lexer
,
20134 && cp_lexer_next_token_is_not (parser
->lexer
,
20136 && cp_lexer_next_token_is_not (parser
->lexer
,
20140 outputs
= cp_parser_asm_operand_list (parser
);
20141 if (outputs
== error_mark_node
)
20142 invalid_outputs_p
= true;
20145 /* If the next token is `::', there are no outputs, and the
20146 next token is the beginning of the inputs. */
20147 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20148 /* The inputs are coming next. */
20151 /* Look for inputs. */
20153 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20155 /* Consume the `:' or `::'. */
20156 cp_lexer_consume_token (parser
->lexer
);
20157 /* Parse the output-operands. */
20158 if (cp_lexer_next_token_is_not (parser
->lexer
,
20160 && cp_lexer_next_token_is_not (parser
->lexer
,
20162 && cp_lexer_next_token_is_not (parser
->lexer
,
20165 inputs
= cp_parser_asm_operand_list (parser
);
20166 if (inputs
== error_mark_node
)
20167 invalid_inputs_p
= true;
20170 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20171 /* The clobbers are coming next. */
20174 /* Look for clobbers. */
20176 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
20179 /* Consume the `:' or `::'. */
20180 cp_lexer_consume_token (parser
->lexer
);
20181 /* Parse the clobbers. */
20182 if (cp_lexer_next_token_is_not (parser
->lexer
,
20184 && cp_lexer_next_token_is_not (parser
->lexer
,
20186 clobbers
= cp_parser_asm_clobber_list (parser
);
20188 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
20189 /* The labels are coming next. */
20192 /* Look for labels. */
20194 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
20197 /* Consume the `:' or `::'. */
20198 cp_lexer_consume_token (parser
->lexer
);
20199 /* Parse the labels. */
20200 labels
= cp_parser_asm_label_list (parser
);
20203 if (goto_p
&& !labels_p
)
20204 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
20207 missing
= RT_COLON_SCOPE
;
20209 /* Look for the closing `)'. */
20210 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
20211 missing
? missing
: RT_CLOSE_PAREN
))
20212 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
20213 /*consume_paren=*/true);
20214 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20216 if (!invalid_inputs_p
&& !invalid_outputs_p
)
20218 /* Create the ASM_EXPR. */
20219 if (parser
->in_function_body
)
20221 asm_stmt
= finish_asm_stmt (asm_loc
, volatile_p
, string
, outputs
,
20222 inputs
, clobbers
, labels
, inline_p
);
20223 /* If the extended syntax was not used, mark the ASM_EXPR. */
20226 tree temp
= asm_stmt
;
20227 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
20228 temp
= TREE_OPERAND (temp
, 0);
20230 ASM_INPUT_P (temp
) = 1;
20234 symtab
->finalize_toplevel_asm (string
);
20238 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
20239 type that comes from the decl-specifier-seq. */
20242 strip_declarator_types (tree type
, cp_declarator
*declarator
)
20244 for (cp_declarator
*d
= declarator
; d
;)
20254 if (TYPE_PTRMEMFUNC_P (type
))
20255 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
20256 type
= TREE_TYPE (type
);
20264 /* Declarators [gram.dcl.decl] */
20266 /* Parse an init-declarator.
20269 declarator initializer [opt]
20274 declarator asm-specification [opt] attributes [opt] initializer [opt]
20276 function-definition:
20277 decl-specifier-seq [opt] declarator ctor-initializer [opt]
20279 decl-specifier-seq [opt] declarator function-try-block
20283 function-definition:
20284 __extension__ function-definition
20288 function-definition:
20289 decl-specifier-seq [opt] declarator function-transaction-block
20291 The parser flags FLAGS is used to control type-specifier parsing.
20293 The DECL_SPECIFIERS apply to this declarator. Returns a
20294 representation of the entity declared. If MEMBER_P is TRUE, then
20295 this declarator appears in a class scope. The new DECL created by
20296 this declarator is returned.
20298 The CHECKS are access checks that should be performed once we know
20299 what entity is being declared (and, therefore, what classes have
20302 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
20303 for a function-definition here as well. If the declarator is a
20304 declarator for a function-definition, *FUNCTION_DEFINITION_P will
20305 be TRUE upon return. By that point, the function-definition will
20306 have been completely parsed.
20308 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
20311 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
20312 parsed declaration if it is an uninitialized single declarator not followed
20313 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
20314 if present, will not be consumed. If returned, this declarator will be
20315 created with SD_INITIALIZED but will not call cp_finish_decl.
20317 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
20318 and there is an initializer, the pointed location_t is set to the
20319 location of the '=' or `(', or '{' in C++11 token introducing the
20323 cp_parser_init_declarator (cp_parser
* parser
,
20324 cp_parser_flags flags
,
20325 cp_decl_specifier_seq
*decl_specifiers
,
20326 vec
<deferred_access_check
, va_gc
> *checks
,
20327 bool function_definition_allowed_p
,
20329 int declares_class_or_enum
,
20330 bool* function_definition_p
,
20331 tree
* maybe_range_for_decl
,
20332 location_t
* init_loc
,
20335 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
20336 *attributes_start_token
= NULL
;
20337 cp_declarator
*declarator
;
20338 tree prefix_attributes
;
20339 tree attributes
= NULL
;
20340 tree asm_specification
;
20342 tree decl
= NULL_TREE
;
20344 int is_initialized
;
20345 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
20346 initialized with "= ..", CPP_OPEN_PAREN if initialized with
20348 enum cpp_ttype initialization_kind
;
20349 bool is_direct_init
= false;
20350 bool is_non_constant_init
;
20351 int ctor_dtor_or_conv_p
;
20352 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
20353 tree pushed_scope
= NULL_TREE
;
20354 bool range_for_decl_p
= false;
20355 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20356 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
20358 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_consteval
))
20359 flags
|= CP_PARSER_FLAGS_CONSTEVAL
;
20361 /* Gather the attributes that were provided with the
20362 decl-specifiers. */
20363 prefix_attributes
= decl_specifiers
->attributes
;
20365 /* Assume that this is not the declarator for a function
20367 if (function_definition_p
)
20368 *function_definition_p
= false;
20370 /* Default arguments are only permitted for function parameters. */
20371 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
20372 parser
->default_arg_ok_p
= false;
20374 /* Defer access checks while parsing the declarator; we cannot know
20375 what names are accessible until we know what is being
20377 resume_deferring_access_checks ();
20379 token
= cp_lexer_peek_token (parser
->lexer
);
20381 /* Parse the declarator. */
20383 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
20384 flags
, &ctor_dtor_or_conv_p
,
20385 /*parenthesized_p=*/NULL
,
20386 member_p
, friend_p
, /*static_p=*/false);
20387 /* Gather up the deferred checks. */
20388 stop_deferring_access_checks ();
20390 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
20392 /* If the DECLARATOR was erroneous, there's no need to go
20394 if (declarator
== cp_error_declarator
)
20395 return error_mark_node
;
20397 /* Check that the number of template-parameter-lists is OK. */
20398 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
20400 return error_mark_node
;
20402 if (declares_class_or_enum
& 2)
20403 cp_parser_check_for_definition_in_return_type (declarator
,
20404 decl_specifiers
->type
,
20405 decl_specifiers
->locations
[ds_type_spec
]);
20407 /* Figure out what scope the entity declared by the DECLARATOR is
20408 located in. `grokdeclarator' sometimes changes the scope, so
20409 we compute it now. */
20410 scope
= get_scope_of_declarator (declarator
);
20412 /* Perform any lookups in the declared type which were thought to be
20413 dependent, but are not in the scope of the declarator. */
20414 decl_specifiers
->type
20415 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
20417 /* If we're allowing GNU extensions, look for an
20418 asm-specification. */
20419 if (cp_parser_allow_gnu_extensions_p (parser
))
20421 /* Look for an asm-specification. */
20422 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
20423 asm_specification
= cp_parser_asm_specification_opt (parser
);
20426 asm_specification
= NULL_TREE
;
20428 /* Look for attributes. */
20429 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
20430 attributes
= cp_parser_attributes_opt (parser
);
20432 /* Peek at the next token. */
20433 token
= cp_lexer_peek_token (parser
->lexer
);
20435 bool bogus_implicit_tmpl
= false;
20437 if (function_declarator_p (declarator
))
20439 /* Handle C++17 deduction guides. */
20440 if (!decl_specifiers
->type
20441 && ctor_dtor_or_conv_p
<= 0
20442 && cxx_dialect
>= cxx17
)
20444 cp_declarator
*id
= get_id_declarator (declarator
);
20445 tree name
= id
->u
.id
.unqualified_name
;
20446 parser
->scope
= id
->u
.id
.qualifying_scope
;
20447 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
20449 && (DECL_CLASS_TEMPLATE_P (tmpl
)
20450 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
20452 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
20453 id
->u
.id
.sfk
= sfk_deduction_guide
;
20454 ctor_dtor_or_conv_p
= 1;
20458 /* Check to see if the token indicates the start of a
20459 function-definition. */
20460 if (cp_parser_token_starts_function_definition_p (token
))
20462 if (!function_definition_allowed_p
)
20464 /* If a function-definition should not appear here, issue an
20466 cp_parser_error (parser
,
20467 "a function-definition is not allowed here");
20468 return error_mark_node
;
20471 location_t func_brace_location
20472 = cp_lexer_peek_token (parser
->lexer
)->location
;
20474 /* Neither attributes nor an asm-specification are allowed
20475 on a function-definition. */
20476 if (asm_specification
)
20477 error_at (asm_spec_start_token
->location
,
20478 "an %<asm%> specification is not allowed "
20479 "on a function-definition");
20481 error_at (attributes_start_token
->location
,
20482 "attributes are not allowed "
20483 "on a function-definition");
20484 /* This is a function-definition. */
20485 *function_definition_p
= true;
20487 /* Parse the function definition. */
20489 decl
= cp_parser_save_member_function_body (parser
,
20492 prefix_attributes
);
20495 (cp_parser_function_definition_from_specifiers_and_declarator
20496 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
20498 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
20500 /* This is where the prologue starts... */
20501 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
20502 = func_brace_location
;
20508 else if (parser
->fully_implicit_function_template_p
)
20510 /* A non-template declaration involving a function parameter list
20511 containing an implicit template parameter will be made into a
20512 template. If the resulting declaration is not going to be an
20513 actual function then finish the template scope here to prevent it.
20514 An error message will be issued once we have a decl to talk about.
20516 FIXME probably we should do type deduction rather than create an
20517 implicit template, but the standard currently doesn't allow it. */
20518 bogus_implicit_tmpl
= true;
20519 finish_fully_implicit_template (parser
, NULL_TREE
);
20524 Only in function declarations for constructors, destructors, type
20525 conversions, and deduction guides can the decl-specifier-seq be omitted.
20527 We explicitly postpone this check past the point where we handle
20528 function-definitions because we tolerate function-definitions
20529 that are missing their return types in some modes. */
20530 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
20532 cp_parser_error (parser
,
20533 "expected constructor, destructor, or type conversion");
20534 return error_mark_node
;
20537 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
20538 if (token
->type
== CPP_EQ
20539 || token
->type
== CPP_OPEN_PAREN
20540 || token
->type
== CPP_OPEN_BRACE
)
20542 is_initialized
= SD_INITIALIZED
;
20543 initialization_kind
= token
->type
;
20544 if (maybe_range_for_decl
)
20545 *maybe_range_for_decl
= error_mark_node
;
20546 tmp_init_loc
= token
->location
;
20547 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
20548 *init_loc
= tmp_init_loc
;
20550 if (token
->type
== CPP_EQ
20551 && function_declarator_p (declarator
))
20553 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
20554 if (t2
->keyword
== RID_DEFAULT
)
20555 is_initialized
= SD_DEFAULTED
;
20556 else if (t2
->keyword
== RID_DELETE
)
20557 is_initialized
= SD_DELETED
;
20562 /* If the init-declarator isn't initialized and isn't followed by a
20563 `,' or `;', it's not a valid init-declarator. */
20564 if (token
->type
!= CPP_COMMA
20565 && token
->type
!= CPP_SEMICOLON
)
20567 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
20568 range_for_decl_p
= true;
20571 if (!maybe_range_for_decl
)
20572 cp_parser_error (parser
, "expected initializer");
20573 return error_mark_node
;
20576 is_initialized
= SD_UNINITIALIZED
;
20577 initialization_kind
= CPP_EOF
;
20580 /* Because start_decl has side-effects, we should only call it if we
20581 know we're going ahead. By this point, we know that we cannot
20582 possibly be looking at any other construct. */
20583 cp_parser_commit_to_tentative_parse (parser
);
20585 /* Enter the newly declared entry in the symbol table. If we're
20586 processing a declaration in a class-specifier, we wait until
20587 after processing the initializer. */
20590 if (parser
->in_unbraced_linkage_specification_p
)
20591 decl_specifiers
->storage_class
= sc_extern
;
20592 decl
= start_decl (declarator
, decl_specifiers
,
20593 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
20594 attributes
, prefix_attributes
, &pushed_scope
);
20595 cp_finalize_omp_declare_simd (parser
, decl
);
20596 cp_finalize_oacc_routine (parser
, decl
, false);
20597 /* Adjust location of decl if declarator->id_loc is more appropriate:
20598 set, and decl wasn't merged with another decl, in which case its
20599 location would be different from input_location, and more accurate. */
20601 && declarator
->id_loc
!= UNKNOWN_LOCATION
20602 && DECL_SOURCE_LOCATION (decl
) == input_location
)
20603 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
20606 /* Enter the SCOPE. That way unqualified names appearing in the
20607 initializer will be looked up in SCOPE. */
20608 pushed_scope
= push_scope (scope
);
20610 /* Perform deferred access control checks, now that we know in which
20611 SCOPE the declared entity resides. */
20612 if (!member_p
&& decl
)
20614 tree saved_current_function_decl
= NULL_TREE
;
20616 /* If the entity being declared is a function, pretend that we
20617 are in its scope. If it is a `friend', it may have access to
20618 things that would not otherwise be accessible. */
20619 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20621 saved_current_function_decl
= current_function_decl
;
20622 current_function_decl
= decl
;
20625 /* Perform access checks for template parameters. */
20626 cp_parser_perform_template_parameter_access_checks (checks
);
20628 /* Perform the access control checks for the declarator and the
20629 decl-specifiers. */
20630 perform_deferred_access_checks (tf_warning_or_error
);
20632 /* Restore the saved value. */
20633 if (TREE_CODE (decl
) == FUNCTION_DECL
)
20634 current_function_decl
= saved_current_function_decl
;
20637 /* Parse the initializer. */
20638 initializer
= NULL_TREE
;
20639 is_direct_init
= false;
20640 is_non_constant_init
= true;
20641 if (is_initialized
)
20643 if (function_declarator_p (declarator
))
20645 if (initialization_kind
== CPP_EQ
)
20646 initializer
= cp_parser_pure_specifier (parser
);
20649 /* If the declaration was erroneous, we don't really
20650 know what the user intended, so just silently
20651 consume the initializer. */
20652 if (decl
!= error_mark_node
)
20653 error_at (tmp_init_loc
, "initializer provided for function");
20654 cp_parser_skip_to_closing_parenthesis (parser
,
20655 /*recovering=*/true,
20656 /*or_comma=*/false,
20657 /*consume_paren=*/true);
20662 /* We want to record the extra mangling scope for in-class
20663 initializers of class members and initializers of static data
20664 member templates. The former involves deferring
20665 parsing of the initializer until end of class as with default
20666 arguments. So right here we only handle the latter. */
20667 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20668 start_lambda_scope (decl
);
20669 initializer
= cp_parser_initializer (parser
,
20671 &is_non_constant_init
);
20672 if (!member_p
&& processing_template_decl
&& decl
!= error_mark_node
)
20673 finish_lambda_scope ();
20674 if (initializer
== error_mark_node
)
20675 cp_parser_skip_to_end_of_statement (parser
);
20679 /* The old parser allows attributes to appear after a parenthesized
20680 initializer. Mark Mitchell proposed removing this functionality
20681 on the GCC mailing lists on 2002-08-13. This parser accepts the
20682 attributes -- but ignores them. Made a permerror in GCC 8. */
20683 if (cp_parser_allow_gnu_extensions_p (parser
)
20684 && initialization_kind
== CPP_OPEN_PAREN
20685 && cp_parser_attributes_opt (parser
)
20686 && permerror (input_location
,
20687 "attributes after parenthesized initializer ignored"))
20690 if (flag_permissive
&& !hint
)
20693 inform (input_location
,
20694 "this flexibility is deprecated and will be removed");
20698 /* And now complain about a non-function implicit template. */
20699 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
20700 error_at (DECL_SOURCE_LOCATION (decl
),
20701 "non-function %qD declared as implicit template", decl
);
20703 /* For an in-class declaration, use `grokfield' to create the
20709 pop_scope (pushed_scope
);
20710 pushed_scope
= NULL_TREE
;
20712 decl
= grokfield (declarator
, decl_specifiers
,
20713 initializer
, !is_non_constant_init
,
20714 /*asmspec=*/NULL_TREE
,
20715 attr_chainon (attributes
, prefix_attributes
));
20716 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20717 cp_parser_save_default_args (parser
, decl
);
20718 cp_finalize_omp_declare_simd (parser
, decl
);
20719 cp_finalize_oacc_routine (parser
, decl
, false);
20722 /* Finish processing the declaration. But, skip member
20724 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
20726 int cf
= (decl_spec_seq_has_spec_p (decl_specifiers
, ds_constinit
)
20727 ? LOOKUP_CONSTINIT
: 0);
20728 cp_finish_decl (decl
,
20729 initializer
, !is_non_constant_init
,
20731 /* If the initializer is in parentheses, then this is
20732 a direct-initialization, which means that an
20733 `explicit' constructor is OK. Otherwise, an
20734 `explicit' constructor cannot be used. */
20735 ((is_direct_init
|| !is_initialized
)
20736 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
) | cf
);
20738 else if ((cxx_dialect
!= cxx98
) && friend_p
20739 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
20740 /* Core issue #226 (C++0x only): A default template-argument
20741 shall not be specified in a friend class template
20743 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
20744 /*is_partial=*/false, /*is_friend_decl=*/1);
20746 if (!friend_p
&& pushed_scope
)
20747 pop_scope (pushed_scope
);
20749 if (function_declarator_p (declarator
)
20750 && parser
->fully_implicit_function_template_p
)
20753 decl
= finish_fully_implicit_template (parser
, decl
);
20755 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
20758 if (auto_result
&& is_initialized
&& decl_specifiers
->type
20759 && type_uses_auto (decl_specifiers
->type
))
20760 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
20765 /* Parse a declarator.
20769 ptr-operator declarator
20771 abstract-declarator:
20772 ptr-operator abstract-declarator [opt]
20773 direct-abstract-declarator
20778 attributes [opt] direct-declarator
20779 attributes [opt] ptr-operator declarator
20781 abstract-declarator:
20782 attributes [opt] ptr-operator abstract-declarator [opt]
20783 attributes [opt] direct-abstract-declarator
20785 The parser flags FLAGS is used to control type-specifier parsing.
20787 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
20788 detect constructors, destructors, deduction guides, or conversion operators.
20789 It is set to -1 if the declarator is a name, and +1 if it is a
20790 function. Otherwise it is set to zero. Usually you just want to
20791 test for >0, but internally the negative value is used.
20793 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
20794 a decl-specifier-seq unless it declares a constructor, destructor,
20795 or conversion. It might seem that we could check this condition in
20796 semantic analysis, rather than parsing, but that makes it difficult
20797 to handle something like `f()'. We want to notice that there are
20798 no decl-specifiers, and therefore realize that this is an
20799 expression, not a declaration.)
20801 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
20802 the declarator is a direct-declarator of the form "(...)".
20804 MEMBER_P is true iff this declarator is a member-declarator.
20806 FRIEND_P is true iff this declarator is a friend.
20808 STATIC_P is true iff the keyword static was seen. */
20810 static cp_declarator
*
20811 cp_parser_declarator (cp_parser
* parser
,
20812 cp_parser_declarator_kind dcl_kind
,
20813 cp_parser_flags flags
,
20814 int* ctor_dtor_or_conv_p
,
20815 bool* parenthesized_p
,
20816 bool member_p
, bool friend_p
, bool static_p
)
20818 cp_declarator
*declarator
;
20819 enum tree_code code
;
20820 cp_cv_quals cv_quals
;
20822 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
20824 /* Assume this is not a constructor, destructor, or type-conversion
20826 if (ctor_dtor_or_conv_p
)
20827 *ctor_dtor_or_conv_p
= 0;
20829 if (cp_parser_allow_gnu_extensions_p (parser
))
20830 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
20832 /* Check for the ptr-operator production. */
20833 cp_parser_parse_tentatively (parser
);
20834 /* Parse the ptr-operator. */
20835 code
= cp_parser_ptr_operator (parser
,
20840 /* If that worked, then we have a ptr-operator. */
20841 if (cp_parser_parse_definitely (parser
))
20843 /* If a ptr-operator was found, then this declarator was not
20845 if (parenthesized_p
)
20846 *parenthesized_p
= true;
20847 /* The dependent declarator is optional if we are parsing an
20848 abstract-declarator. */
20849 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20850 cp_parser_parse_tentatively (parser
);
20852 /* Parse the dependent declarator. */
20853 declarator
= cp_parser_declarator (parser
, dcl_kind
,
20854 CP_PARSER_FLAGS_NONE
,
20855 /*ctor_dtor_or_conv_p=*/NULL
,
20856 /*parenthesized_p=*/NULL
,
20857 /*member_p=*/false,
20858 friend_p
, /*static_p=*/false);
20860 /* If we are parsing an abstract-declarator, we must handle the
20861 case where the dependent declarator is absent. */
20862 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
20863 && !cp_parser_parse_definitely (parser
))
20866 declarator
= cp_parser_make_indirect_declarator
20867 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
20869 /* Everything else is a direct-declarator. */
20872 if (parenthesized_p
)
20873 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
20875 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
20876 flags
, ctor_dtor_or_conv_p
,
20877 member_p
, friend_p
, static_p
);
20880 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
20881 declarator
->attributes
= gnu_attributes
;
20885 /* Parse a direct-declarator or direct-abstract-declarator.
20889 direct-declarator ( parameter-declaration-clause )
20890 cv-qualifier-seq [opt]
20891 ref-qualifier [opt]
20892 exception-specification [opt]
20893 direct-declarator [ constant-expression [opt] ]
20896 direct-abstract-declarator:
20897 direct-abstract-declarator [opt]
20898 ( parameter-declaration-clause )
20899 cv-qualifier-seq [opt]
20900 ref-qualifier [opt]
20901 exception-specification [opt]
20902 direct-abstract-declarator [opt] [ constant-expression [opt] ]
20903 ( abstract-declarator )
20905 Returns a representation of the declarator. DCL_KIND is
20906 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
20907 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
20908 we are parsing a direct-declarator. It is
20909 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
20910 of ambiguity we prefer an abstract declarator, as per
20912 The parser flags FLAGS is used to control type-specifier parsing.
20913 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
20914 as for cp_parser_declarator. */
20916 static cp_declarator
*
20917 cp_parser_direct_declarator (cp_parser
* parser
,
20918 cp_parser_declarator_kind dcl_kind
,
20919 cp_parser_flags flags
,
20920 int* ctor_dtor_or_conv_p
,
20921 bool member_p
, bool friend_p
, bool static_p
)
20924 cp_declarator
*declarator
= NULL
;
20925 tree scope
= NULL_TREE
;
20926 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
20927 bool saved_in_declarator_p
= parser
->in_declarator_p
;
20929 tree pushed_scope
= NULL_TREE
;
20930 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
20934 /* Peek at the next token. */
20935 token
= cp_lexer_peek_token (parser
->lexer
);
20936 if (token
->type
== CPP_OPEN_PAREN
)
20938 /* This is either a parameter-declaration-clause, or a
20939 parenthesized declarator. When we know we are parsing a
20940 named declarator, it must be a parenthesized declarator
20941 if FIRST is true. For instance, `(int)' is a
20942 parameter-declaration-clause, with an omitted
20943 direct-abstract-declarator. But `((*))', is a
20944 parenthesized abstract declarator. Finally, when T is a
20945 template parameter `(T)' is a
20946 parameter-declaration-clause, and not a parenthesized
20949 We first try and parse a parameter-declaration-clause,
20950 and then try a nested declarator (if FIRST is true).
20952 It is not an error for it not to be a
20953 parameter-declaration-clause, even when FIRST is
20959 The first is the declaration of a function while the
20960 second is the definition of a variable, including its
20963 Having seen only the parenthesis, we cannot know which of
20964 these two alternatives should be selected. Even more
20965 complex are examples like:
20970 The former is a function-declaration; the latter is a
20971 variable initialization.
20973 Thus again, we try a parameter-declaration-clause, and if
20974 that fails, we back out and return. */
20976 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
20979 bool is_declarator
= false;
20983 /* In a member-declarator, the only valid interpretation
20984 of a parenthesis is the start of a
20985 parameter-declaration-clause. (It is invalid to
20986 initialize a static data member with a parenthesized
20987 initializer; only the "=" form of initialization is
20990 cp_parser_parse_tentatively (parser
);
20992 /* Consume the `('. */
20993 matching_parens parens
;
20994 parens
.consume_open (parser
);
20997 /* If this is going to be an abstract declarator, we're
20998 in a declarator and we can't have default args. */
20999 parser
->default_arg_ok_p
= false;
21000 parser
->in_declarator_p
= true;
21003 begin_scope (sk_function_parms
, NULL_TREE
);
21005 /* Signal we are in the immediate function context. */
21006 if (flags
& CP_PARSER_FLAGS_CONSTEVAL
)
21007 current_binding_level
->immediate_fn_ctx_p
= true;
21009 /* Parse the parameter-declaration-clause. */
21011 = cp_parser_parameter_declaration_clause (parser
, flags
);
21013 /* Consume the `)'. */
21014 parens
.require_close (parser
);
21016 /* If all went well, parse the cv-qualifier-seq,
21017 ref-qualifier and the exception-specification. */
21018 if (member_p
|| cp_parser_parse_definitely (parser
))
21020 cp_cv_quals cv_quals
;
21021 cp_virt_specifiers virt_specifiers
;
21022 cp_ref_qualifier ref_qual
;
21023 tree exception_specification
;
21026 bool memfn
= (member_p
|| (pushed_scope
21027 && CLASS_TYPE_P (pushed_scope
)));
21028 unsigned char local_variables_forbidden_p
21029 = parser
->local_variables_forbidden_p
;
21030 /* 'this' is not allowed in static member functions. */
21031 if (static_p
|| friend_p
)
21032 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
21034 is_declarator
= true;
21036 if (ctor_dtor_or_conv_p
)
21037 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
21040 /* Parse the cv-qualifier-seq. */
21041 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21042 /* Parse the ref-qualifier. */
21043 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
21044 /* Parse the tx-qualifier. */
21045 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
21046 /* And the exception-specification. */
21047 exception_specification
21048 = cp_parser_exception_specification_opt (parser
, flags
);
21050 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21052 /* In here, we handle cases where attribute is used after
21053 the function declaration. For example:
21054 void func (int x) __attribute__((vector(..))); */
21055 tree gnu_attrs
= NULL_TREE
;
21056 tree requires_clause
= NULL_TREE
;
21057 late_return
= (cp_parser_late_return_type_opt
21058 (parser
, declarator
, requires_clause
,
21059 memfn
? cv_quals
: -1));
21061 /* Parse the virt-specifier-seq. */
21062 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
21064 /* Create the function-declarator. */
21065 declarator
= make_call_declarator (declarator
,
21071 exception_specification
,
21074 declarator
->std_attributes
= attrs
;
21075 declarator
->attributes
= gnu_attrs
;
21076 /* Any subsequent parameter lists are to do with
21077 return type, so are not those of the declared
21079 parser
->default_arg_ok_p
= false;
21081 /* Restore the state of local_variables_forbidden_p. */
21082 parser
->local_variables_forbidden_p
21083 = local_variables_forbidden_p
;
21086 /* Remove the function parms from scope. */
21087 pop_bindings_and_leave_scope ();
21090 /* Repeat the main loop. */
21094 /* If this is the first, we can try a parenthesized
21098 bool saved_in_type_id_in_expr_p
;
21100 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21101 parser
->in_declarator_p
= saved_in_declarator_p
;
21103 open_paren
= token
;
21104 /* Consume the `('. */
21105 matching_parens parens
;
21106 parens
.consume_open (parser
);
21107 /* Parse the nested declarator. */
21108 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
21109 parser
->in_type_id_in_expr_p
= true;
21111 = cp_parser_declarator (parser
, dcl_kind
, flags
,
21112 ctor_dtor_or_conv_p
,
21113 /*parenthesized_p=*/NULL
,
21114 member_p
, friend_p
,
21115 /*static_p=*/false);
21116 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
21118 /* Expect a `)'. */
21119 close_paren
= cp_lexer_peek_token (parser
->lexer
);
21120 if (!parens
.require_close (parser
))
21121 declarator
= cp_error_declarator
;
21122 if (declarator
== cp_error_declarator
)
21125 goto handle_declarator
;
21127 /* Otherwise, we must be done. */
21131 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21132 && token
->type
== CPP_OPEN_SQUARE
21133 && !cp_next_tokens_can_be_attribute_p (parser
))
21135 /* Parse an array-declarator. */
21136 tree bounds
, attrs
;
21138 if (ctor_dtor_or_conv_p
)
21139 *ctor_dtor_or_conv_p
= 0;
21143 parser
->default_arg_ok_p
= false;
21144 parser
->in_declarator_p
= true;
21145 /* Consume the `['. */
21146 cp_lexer_consume_token (parser
->lexer
);
21147 /* Peek at the next token. */
21148 token
= cp_lexer_peek_token (parser
->lexer
);
21149 /* If the next token is `]', then there is no
21150 constant-expression. */
21151 if (token
->type
!= CPP_CLOSE_SQUARE
)
21153 bool non_constant_p
;
21155 = cp_parser_constant_expression (parser
,
21156 /*allow_non_constant=*/true,
21158 if (!non_constant_p
)
21160 else if (error_operand_p (bounds
))
21161 /* Already gave an error. */;
21162 else if (!parser
->in_function_body
21163 || current_binding_level
->kind
== sk_function_parms
)
21165 /* Normally, the array bound must be an integral constant
21166 expression. However, as an extension, we allow VLAs
21167 in function scopes as long as they aren't part of a
21168 parameter declaration. */
21169 cp_parser_error (parser
,
21170 "array bound is not an integer constant");
21171 bounds
= error_mark_node
;
21173 else if (processing_template_decl
21174 && !type_dependent_expression_p (bounds
))
21176 /* Remember this wasn't a constant-expression. */
21177 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
21178 TREE_SIDE_EFFECTS (bounds
) = 1;
21182 bounds
= NULL_TREE
;
21183 /* Look for the closing `]'. */
21184 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
21186 declarator
= cp_error_declarator
;
21190 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21191 declarator
= make_array_declarator (declarator
, bounds
);
21192 declarator
->std_attributes
= attrs
;
21194 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
21197 tree qualifying_scope
;
21198 tree unqualified_name
;
21200 special_function_kind sfk
;
21202 bool pack_expansion_p
= false;
21203 cp_token
*declarator_id_start_token
;
21205 /* Parse a declarator-id */
21206 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
21209 cp_parser_parse_tentatively (parser
);
21211 /* If we see an ellipsis, we should be looking at a
21213 if (token
->type
== CPP_ELLIPSIS
)
21215 /* Consume the `...' */
21216 cp_lexer_consume_token (parser
->lexer
);
21218 pack_expansion_p
= true;
21222 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
21224 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
21225 qualifying_scope
= parser
->scope
;
21230 if (!unqualified_name
&& pack_expansion_p
)
21232 /* Check whether an error occurred. */
21233 okay
= !cp_parser_error_occurred (parser
);
21235 /* We already consumed the ellipsis to mark a
21236 parameter pack, but we have no way to report it,
21237 so abort the tentative parse. We will be exiting
21238 immediately anyway. */
21239 cp_parser_abort_tentative_parse (parser
);
21242 okay
= cp_parser_parse_definitely (parser
);
21245 unqualified_name
= error_mark_node
;
21246 else if (unqualified_name
21247 && (qualifying_scope
21248 || (!identifier_p (unqualified_name
))))
21250 cp_parser_error (parser
, "expected unqualified-id");
21251 unqualified_name
= error_mark_node
;
21255 if (!unqualified_name
)
21257 if (unqualified_name
== error_mark_node
)
21259 declarator
= cp_error_declarator
;
21260 pack_expansion_p
= false;
21261 declarator
->parameter_pack_p
= false;
21265 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21267 if (qualifying_scope
&& at_namespace_scope_p ()
21268 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
21270 /* In the declaration of a member of a template class
21271 outside of the class itself, the SCOPE will sometimes
21272 be a TYPENAME_TYPE. For example, given:
21274 template <typename T>
21275 int S<T>::R::i = 3;
21277 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
21278 this context, we must resolve S<T>::R to an ordinary
21279 type, rather than a typename type.
21281 The reason we normally avoid resolving TYPENAME_TYPEs
21282 is that a specialization of `S' might render
21283 `S<T>::R' not a type. However, if `S' is
21284 specialized, then this `i' will not be used, so there
21285 is no harm in resolving the types here. */
21288 /* Resolve the TYPENAME_TYPE. */
21289 type
= resolve_typename_type (qualifying_scope
,
21290 /*only_current_p=*/false);
21291 /* If that failed, the declarator is invalid. */
21292 if (TREE_CODE (type
) == TYPENAME_TYPE
)
21294 if (typedef_variant_p (type
))
21295 error_at (declarator_id_start_token
->location
,
21296 "cannot define member of dependent typedef "
21299 error_at (declarator_id_start_token
->location
,
21300 "%<%T::%E%> is not a type",
21301 TYPE_CONTEXT (qualifying_scope
),
21302 TYPE_IDENTIFIER (qualifying_scope
));
21304 qualifying_scope
= type
;
21309 if (unqualified_name
)
21313 if (qualifying_scope
21314 && CLASS_TYPE_P (qualifying_scope
))
21315 class_type
= qualifying_scope
;
21317 class_type
= current_class_type
;
21319 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
21321 tree name_type
= TREE_TYPE (unqualified_name
);
21323 if (!class_type
|| !same_type_p (name_type
, class_type
))
21325 /* We do not attempt to print the declarator
21326 here because we do not have enough
21327 information about its original syntactic
21329 cp_parser_error (parser
, "invalid declarator");
21330 declarator
= cp_error_declarator
;
21333 else if (qualifying_scope
21334 && CLASSTYPE_USE_TEMPLATE (name_type
))
21336 error_at (declarator_id_start_token
->location
,
21337 "invalid use of constructor as a template");
21338 inform (declarator_id_start_token
->location
,
21339 "use %<%T::%D%> instead of %<%T::%D%> to "
21340 "name the constructor in a qualified name",
21342 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
21343 class_type
, name_type
);
21344 declarator
= cp_error_declarator
;
21347 unqualified_name
= constructor_name (class_type
);
21352 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
21353 sfk
= sfk_destructor
;
21354 else if (identifier_p (unqualified_name
)
21355 && IDENTIFIER_CONV_OP_P (unqualified_name
))
21356 sfk
= sfk_conversion
;
21357 else if (/* There's no way to declare a constructor
21358 for an unnamed type, even if the type
21359 got a name for linkage purposes. */
21360 !TYPE_WAS_UNNAMED (class_type
)
21361 /* Handle correctly (c++/19200):
21375 friend void N::S();
21377 && (!friend_p
|| class_type
== qualifying_scope
)
21378 && constructor_name_p (unqualified_name
,
21380 sfk
= sfk_constructor
;
21381 else if (is_overloaded_fn (unqualified_name
)
21382 && DECL_CONSTRUCTOR_P (get_first_fn
21383 (unqualified_name
)))
21384 sfk
= sfk_constructor
;
21386 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
21387 *ctor_dtor_or_conv_p
= -1;
21390 declarator
= make_id_declarator (qualifying_scope
,
21392 sfk
, token
->location
);
21393 declarator
->std_attributes
= attrs
;
21394 declarator
->parameter_pack_p
= pack_expansion_p
;
21396 if (pack_expansion_p
)
21397 maybe_warn_variadic_templates ();
21399 /* We're looking for this case in [temp.res]:
21400 A qualified-id is assumed to name a type if [...]
21401 - it is a decl-specifier of the decl-specifier-seq of a
21402 parameter-declaration in a declarator of a function or
21403 function template declaration, ... */
21404 if (cxx_dialect
>= cxx2a
21405 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
21406 && declarator
->kind
== cdk_id
21407 && !at_class_scope_p ()
21408 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
21410 /* ...whose declarator-id is qualified. If it isn't, never
21411 assume the parameters to refer to types. */
21412 if (qualifying_scope
== NULL_TREE
)
21413 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21416 /* Now we have something like
21417 template <typename T> int C::x(S::p);
21418 which can be a function template declaration or a
21419 variable template definition. If name lookup for
21420 the declarator-id C::x finds one or more function
21421 templates, assume S::p to name a type. Otherwise,
21424 = cp_parser_lookup_name_simple (parser
, unqualified_name
,
21426 if (!is_overloaded_fn (decl
)
21428 template<typename T>
21429 A<T>::A(T::type) { } */
21430 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
21431 && constructor_name_p (unqualified_name
,
21432 qualifying_scope
)))
21433 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
21438 handle_declarator
:;
21439 scope
= get_scope_of_declarator (declarator
);
21442 /* Any names that appear after the declarator-id for a
21443 member are looked up in the containing scope. */
21444 if (at_function_scope_p ())
21446 /* But declarations with qualified-ids can't appear in a
21448 cp_parser_error (parser
, "qualified-id in declaration");
21449 declarator
= cp_error_declarator
;
21452 pushed_scope
= push_scope (scope
);
21454 parser
->in_declarator_p
= true;
21455 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
21456 || (declarator
&& declarator
->kind
== cdk_id
))
21457 /* Default args are only allowed on function
21459 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21461 parser
->default_arg_ok_p
= false;
21470 /* For an abstract declarator, we might wind up with nothing at this
21471 point. That's an error; the declarator is not optional. */
21473 cp_parser_error (parser
, "expected declarator");
21474 else if (open_paren
)
21476 /* Record overly parenthesized declarator so we can give a
21477 diagnostic about confusing decl/expr disambiguation. */
21478 if (declarator
->kind
== cdk_array
)
21480 /* If the open and close parens are on different lines, this
21481 is probably a formatting thing, so ignore. */
21482 expanded_location open
= expand_location (open_paren
->location
);
21483 expanded_location close
= expand_location (close_paren
->location
);
21484 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
21488 declarator
->parenthesized
= open_paren
->location
;
21491 /* If we entered a scope, we must exit it now. */
21493 pop_scope (pushed_scope
);
21495 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21496 parser
->in_declarator_p
= saved_in_declarator_p
;
21501 /* Parse a ptr-operator.
21504 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21505 * cv-qualifier-seq [opt]
21507 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
21508 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
21513 & cv-qualifier-seq [opt]
21515 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
21516 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
21517 an rvalue reference. In the case of a pointer-to-member, *TYPE is
21518 filled in with the TYPE containing the member. *CV_QUALS is
21519 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
21520 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
21521 Note that the tree codes returned by this function have nothing
21522 to do with the types of trees that will be eventually be created
21523 to represent the pointer or reference type being parsed. They are
21524 just constants with suggestive names. */
21525 static enum tree_code
21526 cp_parser_ptr_operator (cp_parser
* parser
,
21528 cp_cv_quals
*cv_quals
,
21531 enum tree_code code
= ERROR_MARK
;
21533 tree attrs
= NULL_TREE
;
21535 /* Assume that it's not a pointer-to-member. */
21537 /* And that there are no cv-qualifiers. */
21538 *cv_quals
= TYPE_UNQUALIFIED
;
21540 /* Peek at the next token. */
21541 token
= cp_lexer_peek_token (parser
->lexer
);
21543 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
21544 if (token
->type
== CPP_MULT
)
21545 code
= INDIRECT_REF
;
21546 else if (token
->type
== CPP_AND
)
21548 else if ((cxx_dialect
!= cxx98
) &&
21549 token
->type
== CPP_AND_AND
) /* C++0x only */
21550 code
= NON_LVALUE_EXPR
;
21552 if (code
!= ERROR_MARK
)
21554 /* Consume the `*', `&' or `&&'. */
21555 cp_lexer_consume_token (parser
->lexer
);
21557 /* A `*' can be followed by a cv-qualifier-seq, and so can a
21558 `&', if we are allowing GNU extensions. (The only qualifier
21559 that can legally appear after `&' is `restrict', but that is
21560 enforced during semantic analysis. */
21561 if (code
== INDIRECT_REF
21562 || cp_parser_allow_gnu_extensions_p (parser
))
21563 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21565 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21566 if (attributes
!= NULL
)
21567 *attributes
= attrs
;
21571 /* Try the pointer-to-member case. */
21572 cp_parser_parse_tentatively (parser
);
21573 /* Look for the optional `::' operator. */
21574 cp_parser_global_scope_opt (parser
,
21575 /*current_scope_valid_p=*/false);
21576 /* Look for the nested-name specifier. */
21577 token
= cp_lexer_peek_token (parser
->lexer
);
21578 cp_parser_nested_name_specifier (parser
,
21579 /*typename_keyword_p=*/false,
21580 /*check_dependency_p=*/true,
21582 /*is_declaration=*/false);
21583 /* If we found it, and the next token is a `*', then we are
21584 indeed looking at a pointer-to-member operator. */
21585 if (!cp_parser_error_occurred (parser
)
21586 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
21588 /* Indicate that the `*' operator was used. */
21589 code
= INDIRECT_REF
;
21591 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
21592 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
21593 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
21594 error_at (token
->location
, "cannot form pointer to member of "
21595 "non-class %q#T", parser
->scope
);
21598 /* The type of which the member is a member is given by the
21600 *type
= parser
->scope
;
21601 /* The next name will not be qualified. */
21602 parser
->scope
= NULL_TREE
;
21603 parser
->qualifying_scope
= NULL_TREE
;
21604 parser
->object_scope
= NULL_TREE
;
21605 /* Look for optional c++11 attributes. */
21606 attrs
= cp_parser_std_attribute_spec_seq (parser
);
21607 if (attributes
!= NULL
)
21608 *attributes
= attrs
;
21609 /* Look for the optional cv-qualifier-seq. */
21610 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
21613 /* If that didn't work we don't have a ptr-operator. */
21614 if (!cp_parser_parse_definitely (parser
))
21615 cp_parser_error (parser
, "expected ptr-operator");
21621 /* Parse an (optional) cv-qualifier-seq.
21624 cv-qualifier cv-qualifier-seq [opt]
21635 Returns a bitmask representing the cv-qualifiers. */
21638 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
21640 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
21645 cp_cv_quals cv_qualifier
;
21647 /* Peek at the next token. */
21648 token
= cp_lexer_peek_token (parser
->lexer
);
21649 /* See if it's a cv-qualifier. */
21650 switch (token
->keyword
)
21653 cv_qualifier
= TYPE_QUAL_CONST
;
21657 cv_qualifier
= TYPE_QUAL_VOLATILE
;
21661 cv_qualifier
= TYPE_QUAL_RESTRICT
;
21665 cv_qualifier
= TYPE_UNQUALIFIED
;
21672 if (cv_quals
& cv_qualifier
)
21674 gcc_rich_location
richloc (token
->location
);
21675 richloc
.add_fixit_remove ();
21676 error_at (&richloc
, "duplicate cv-qualifier");
21677 cp_lexer_purge_token (parser
->lexer
);
21681 cp_lexer_consume_token (parser
->lexer
);
21682 cv_quals
|= cv_qualifier
;
21689 /* Parse an (optional) ref-qualifier
21695 Returns cp_ref_qualifier representing ref-qualifier. */
21697 static cp_ref_qualifier
21698 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
21700 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
21702 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
21703 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
21708 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
21709 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21711 switch (token
->type
)
21714 curr_ref_qual
= REF_QUAL_LVALUE
;
21718 curr_ref_qual
= REF_QUAL_RVALUE
;
21722 curr_ref_qual
= REF_QUAL_NONE
;
21726 if (!curr_ref_qual
)
21730 error_at (token
->location
, "multiple ref-qualifiers");
21731 cp_lexer_purge_token (parser
->lexer
);
21735 ref_qual
= curr_ref_qual
;
21736 cp_lexer_consume_token (parser
->lexer
);
21743 /* Parse an optional tx-qualifier.
21747 transaction_safe_dynamic */
21750 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
21752 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
21753 if (token
->type
== CPP_NAME
)
21755 tree name
= token
->u
.value
;
21756 const char *p
= IDENTIFIER_POINTER (name
);
21757 const int len
= strlen ("transaction_safe");
21758 if (!strncmp (p
, "transaction_safe", len
))
21762 || !strcmp (p
, "_dynamic"))
21764 cp_lexer_consume_token (parser
->lexer
);
21767 error ("%qE requires %<-fgnu-tm%>", name
);
21778 /* Parse an (optional) virt-specifier-seq.
21780 virt-specifier-seq:
21781 virt-specifier virt-specifier-seq [opt]
21787 Returns a bitmask representing the virt-specifiers. */
21789 static cp_virt_specifiers
21790 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
21792 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
21797 cp_virt_specifiers virt_specifier
;
21799 /* Peek at the next token. */
21800 token
= cp_lexer_peek_token (parser
->lexer
);
21801 /* See if it's a virt-specifier-qualifier. */
21802 if (token
->type
!= CPP_NAME
)
21804 if (id_equal (token
->u
.value
, "override"))
21806 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21807 virt_specifier
= VIRT_SPEC_OVERRIDE
;
21809 else if (id_equal (token
->u
.value
, "final"))
21811 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
21812 virt_specifier
= VIRT_SPEC_FINAL
;
21814 else if (id_equal (token
->u
.value
, "__final"))
21816 virt_specifier
= VIRT_SPEC_FINAL
;
21821 if (virt_specifiers
& virt_specifier
)
21823 gcc_rich_location
richloc (token
->location
);
21824 richloc
.add_fixit_remove ();
21825 error_at (&richloc
, "duplicate virt-specifier");
21826 cp_lexer_purge_token (parser
->lexer
);
21830 cp_lexer_consume_token (parser
->lexer
);
21831 virt_specifiers
|= virt_specifier
;
21834 return virt_specifiers
;
21837 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
21838 is in scope even though it isn't real. */
21841 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
21845 if (current_class_ptr
)
21847 /* We don't clear this between NSDMIs. Is it already what we want? */
21848 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
21849 if (DECL_P (current_class_ptr
)
21850 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
21851 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
21852 && cp_type_quals (type
) == quals
)
21856 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
21857 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
21858 current_class_ptr
= NULL_TREE
;
21860 = cp_build_fold_indirect_ref (this_parm
);
21861 current_class_ptr
= this_parm
;
21864 /* Return true iff our current scope is a non-static data member
21868 parsing_nsdmi (void)
21870 /* We recognize NSDMI context by the context-less 'this' pointer set up
21871 by the function above. */
21872 if (current_class_ptr
21873 && TREE_CODE (current_class_ptr
) == PARM_DECL
21874 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
21879 /* Parse a late-specified return type, if any. This is not a separate
21880 non-terminal, but part of a function declarator, which looks like
21882 -> trailing-type-specifier-seq abstract-declarator(opt)
21884 Returns the type indicated by the type-id.
21886 In addition to this, parse any queued up #pragma omp declare simd
21887 clauses, and #pragma acc routine clauses.
21889 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
21893 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
21894 tree
& requires_clause
, cp_cv_quals quals
)
21897 tree type
= NULL_TREE
;
21898 bool declare_simd_p
= (parser
->omp_declare_simd
21900 && declarator
->kind
== cdk_id
);
21902 bool oacc_routine_p
= (parser
->oacc_routine
21904 && declarator
->kind
== cdk_id
);
21906 /* Peek at the next token. */
21907 token
= cp_lexer_peek_token (parser
->lexer
);
21908 /* A late-specified return type is indicated by an initial '->'. */
21909 if (token
->type
!= CPP_DEREF
21910 && token
->keyword
!= RID_REQUIRES
21911 && !(token
->type
== CPP_NAME
21912 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
21913 && !(declare_simd_p
|| oacc_routine_p
))
21916 tree save_ccp
= current_class_ptr
;
21917 tree save_ccr
= current_class_ref
;
21920 /* DR 1207: 'this' is in scope in the trailing return type. */
21921 inject_this_parameter (current_class_type
, quals
);
21924 if (token
->type
== CPP_DEREF
)
21926 /* Consume the ->. */
21927 cp_lexer_consume_token (parser
->lexer
);
21929 type
= cp_parser_trailing_type_id (parser
);
21932 /* Function declarations may be followed by a trailing
21933 requires-clause. */
21934 requires_clause
= cp_parser_requires_clause_opt (parser
, false);
21936 if (declare_simd_p
)
21937 declarator
->attributes
21938 = cp_parser_late_parsing_omp_declare_simd (parser
,
21939 declarator
->attributes
);
21940 if (oacc_routine_p
)
21941 declarator
->attributes
21942 = cp_parser_late_parsing_oacc_routine (parser
,
21943 declarator
->attributes
);
21947 current_class_ptr
= save_ccp
;
21948 current_class_ref
= save_ccr
;
21954 /* Parse a declarator-id.
21958 :: [opt] nested-name-specifier [opt] type-name
21960 In the `id-expression' case, the value returned is as for
21961 cp_parser_id_expression if the id-expression was an unqualified-id.
21962 If the id-expression was a qualified-id, then a SCOPE_REF is
21963 returned. The first operand is the scope (either a NAMESPACE_DECL
21964 or TREE_TYPE), but the second is still just a representation of an
21968 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
21971 /* The expression must be an id-expression. Assume that qualified
21972 names are the names of types so that:
21975 int S<T>::R::i = 3;
21977 will work; we must treat `S<T>::R' as the name of a type.
21978 Similarly, assume that qualified names are templates, where
21982 int S<T>::R<T>::i = 3;
21985 id
= cp_parser_id_expression (parser
,
21986 /*template_keyword_p=*/false,
21987 /*check_dependency_p=*/false,
21988 /*template_p=*/NULL
,
21989 /*declarator_p=*/true,
21991 if (id
&& BASELINK_P (id
))
21992 id
= BASELINK_FUNCTIONS (id
);
21996 /* Parse a type-id.
21999 type-specifier-seq abstract-declarator [opt]
22001 The parser flags FLAGS is used to control type-specifier parsing.
22003 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
22005 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22006 i.e. we've just seen "->".
22008 Returns the TYPE specified. */
22011 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
22012 bool is_template_arg
, bool is_trailing_return
,
22013 location_t
*type_location
)
22015 cp_decl_specifier_seq type_specifier_seq
;
22016 cp_declarator
*abstract_declarator
;
22018 /* Parse the type-specifier-seq. */
22019 cp_parser_type_specifier_seq (parser
, flags
,
22020 /*is_declaration=*/false,
22021 is_trailing_return
,
22022 &type_specifier_seq
);
22024 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
22026 if (is_template_arg
&& type_specifier_seq
.type
22027 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
22028 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
22029 /* A bare template name as a template argument is a template template
22030 argument, not a placeholder, so fail parsing it as a type argument. */
22032 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
22033 cp_parser_simulate_error (parser
);
22034 return error_mark_node
;
22036 if (type_specifier_seq
.type
== error_mark_node
)
22037 return error_mark_node
;
22039 /* There might or might not be an abstract declarator. */
22040 cp_parser_parse_tentatively (parser
);
22041 /* Look for the declarator. */
22042 abstract_declarator
22043 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
22044 CP_PARSER_FLAGS_NONE
, NULL
,
22045 /*parenthesized_p=*/NULL
,
22046 /*member_p=*/false,
22047 /*friend_p=*/false,
22048 /*static_p=*/false);
22049 /* Check to see if there really was a declarator. */
22050 if (!cp_parser_parse_definitely (parser
))
22051 abstract_declarator
= NULL
;
22053 if (type_specifier_seq
.type
22054 /* The concepts TS allows 'auto' as a type-id. */
22055 && (!flag_concepts
|| parser
->in_type_id_in_expr_p
)
22056 /* None of the valid uses of 'auto' in C++14 involve the type-id
22057 nonterminal, but it is valid in a trailing-return-type. */
22058 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
22059 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
22061 /* A type-id with type 'auto' is only ok if the abstract declarator
22062 is a function declarator with a late-specified return type.
22064 A type-id with 'auto' is also valid in a trailing-return-type
22065 in a compound-requirement. */
22066 if (abstract_declarator
22067 && abstract_declarator
->kind
== cdk_function
22068 && abstract_declarator
->u
.function
.late_return_type
)
22070 else if (parser
->in_result_type_constraint_p
)
22074 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
22075 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
22077 error_at (loc
, "missing template arguments after %qT",
22079 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
22083 error_at (loc
, "invalid use of %qT", auto_node
);
22084 return error_mark_node
;
22088 return groktypename (&type_specifier_seq
, abstract_declarator
,
22092 /* Wrapper for cp_parser_type_id_1. */
22095 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
22096 location_t
*type_location
)
22098 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
22101 /* Wrapper for cp_parser_type_id_1. */
22104 cp_parser_template_type_arg (cp_parser
*parser
)
22107 const char *saved_message
= parser
->type_definition_forbidden_message
;
22108 parser
->type_definition_forbidden_message
22109 = G_("types may not be defined in template arguments");
22110 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
22111 parser
->type_definition_forbidden_message
= saved_message
;
22112 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
22114 error ("invalid use of %<auto%> in template argument");
22115 r
= error_mark_node
;
22120 /* Wrapper for cp_parser_type_id_1. */
22123 cp_parser_trailing_type_id (cp_parser
*parser
)
22125 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
22126 false, true, NULL
);
22129 /* Parse a type-specifier-seq.
22131 type-specifier-seq:
22132 type-specifier type-specifier-seq [opt]
22136 type-specifier-seq:
22137 attributes type-specifier-seq [opt]
22139 The parser flags FLAGS is used to control type-specifier parsing.
22141 If IS_DECLARATION is true, we are at the start of a "condition" or
22142 exception-declaration, so we might be followed by a declarator-id.
22144 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
22145 i.e. we've just seen "->".
22147 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
22150 cp_parser_type_specifier_seq (cp_parser
* parser
,
22151 cp_parser_flags flags
,
22152 bool is_declaration
,
22153 bool is_trailing_return
,
22154 cp_decl_specifier_seq
*type_specifier_seq
)
22156 bool seen_type_specifier
= false;
22157 cp_token
*start_token
= NULL
;
22159 /* Clear the TYPE_SPECIFIER_SEQ. */
22160 clear_decl_specs (type_specifier_seq
);
22162 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
22163 /* In the context of a trailing return type, enum E { } is an
22164 elaborated-type-specifier followed by a function-body, not an
22166 if (is_trailing_return
)
22167 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
22169 /* Parse the type-specifiers and attributes. */
22172 tree type_specifier
;
22173 bool is_cv_qualifier
;
22175 /* Check for attributes first. */
22176 if (cp_next_tokens_can_be_attribute_p (parser
))
22178 type_specifier_seq
->attributes
22179 = attr_chainon (type_specifier_seq
->attributes
,
22180 cp_parser_attributes_opt (parser
));
22184 /* record the token of the beginning of the type specifier seq,
22185 for error reporting purposes*/
22187 start_token
= cp_lexer_peek_token (parser
->lexer
);
22189 /* Look for the type-specifier. */
22190 type_specifier
= cp_parser_type_specifier (parser
,
22192 type_specifier_seq
,
22193 /*is_declaration=*/false,
22196 if (!type_specifier
)
22198 /* If the first type-specifier could not be found, this is not a
22199 type-specifier-seq at all. */
22200 if (!seen_type_specifier
)
22202 /* Set in_declarator_p to avoid skipping to the semicolon. */
22203 int in_decl
= parser
->in_declarator_p
;
22204 parser
->in_declarator_p
= true;
22206 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
22207 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22208 cp_parser_error (parser
, "expected type-specifier");
22210 parser
->in_declarator_p
= in_decl
;
22212 type_specifier_seq
->type
= error_mark_node
;
22215 /* If subsequent type-specifiers could not be found, the
22216 type-specifier-seq is complete. */
22220 seen_type_specifier
= true;
22221 /* The standard says that a condition can be:
22223 type-specifier-seq declarator = assignment-expression
22230 we should treat the "S" as a declarator, not as a
22231 type-specifier. The standard doesn't say that explicitly for
22232 type-specifier-seq, but it does say that for
22233 decl-specifier-seq in an ordinary declaration. Perhaps it
22234 would be clearer just to allow a decl-specifier-seq here, and
22235 then add a semantic restriction that if any decl-specifiers
22236 that are not type-specifiers appear, the program is invalid. */
22237 if (is_declaration
&& !is_cv_qualifier
)
22238 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
22242 /* Return whether the function currently being declared has an associated
22243 template parameter list. */
22246 function_being_declared_is_template_p (cp_parser
* parser
)
22248 if (!current_template_parms
|| processing_template_parmlist
)
22251 if (parser
->implicit_template_scope
)
22254 if (at_class_scope_p ()
22255 && TYPE_BEING_DEFINED (current_class_type
))
22256 return parser
->num_template_parameter_lists
!= 0;
22258 return ((int) parser
->num_template_parameter_lists
> template_class_depth
22259 (current_class_type
));
22262 /* Parse a parameter-declaration-clause.
22264 parameter-declaration-clause:
22265 parameter-declaration-list [opt] ... [opt]
22266 parameter-declaration-list , ...
22268 The parser flags FLAGS is used to control type-specifier parsing.
22270 Returns a representation for the parameter declarations. A return
22271 value of NULL indicates a parameter-declaration-clause consisting
22272 only of an ellipsis. */
22275 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
22276 cp_parser_flags flags
)
22282 temp_override
<bool> cleanup
22283 (parser
->auto_is_implicit_function_template_parm_p
);
22285 if (!processing_specialization
22286 && !processing_template_parmlist
22287 && !processing_explicit_instantiation
22288 /* default_arg_ok_p tracks whether this is a parameter-clause for an
22289 actual function or a random abstract declarator. */
22290 && parser
->default_arg_ok_p
)
22291 if (!current_function_decl
22292 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
22293 parser
->auto_is_implicit_function_template_parm_p
= true;
22295 /* Peek at the next token. */
22296 token
= cp_lexer_peek_token (parser
->lexer
);
22297 /* Check for trivial parameter-declaration-clauses. */
22298 if (token
->type
== CPP_ELLIPSIS
)
22300 /* Consume the `...' token. */
22301 cp_lexer_consume_token (parser
->lexer
);
22304 else if (token
->type
== CPP_CLOSE_PAREN
)
22305 /* There are no parameters. */
22306 return void_list_node
;
22307 /* Check for `(void)', too, which is a special case. */
22308 else if (token
->keyword
== RID_VOID
22309 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
22310 == CPP_CLOSE_PAREN
))
22312 /* Consume the `void' token. */
22313 cp_lexer_consume_token (parser
->lexer
);
22314 /* There are no parameters. */
22315 return void_list_node
;
22318 /* Parse the parameter-declaration-list. */
22319 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
22320 /* If a parse error occurred while parsing the
22321 parameter-declaration-list, then the entire
22322 parameter-declaration-clause is erroneous. */
22323 if (parameters
== error_mark_node
)
22326 /* Peek at the next token. */
22327 token
= cp_lexer_peek_token (parser
->lexer
);
22328 /* If it's a `,', the clause should terminate with an ellipsis. */
22329 if (token
->type
== CPP_COMMA
)
22331 /* Consume the `,'. */
22332 cp_lexer_consume_token (parser
->lexer
);
22333 /* Expect an ellipsis. */
22335 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
22337 /* It might also be `...' if the optional trailing `,' was
22339 else if (token
->type
== CPP_ELLIPSIS
)
22341 /* Consume the `...' token. */
22342 cp_lexer_consume_token (parser
->lexer
);
22343 /* And remember that we saw it. */
22347 ellipsis_p
= false;
22349 /* Finish the parameter list. */
22351 parameters
= chainon (parameters
, void_list_node
);
22356 /* Parse a parameter-declaration-list.
22358 parameter-declaration-list:
22359 parameter-declaration
22360 parameter-declaration-list , parameter-declaration
22362 The parser flags FLAGS is used to control type-specifier parsing.
22364 Returns a representation of the parameter-declaration-list, as for
22365 cp_parser_parameter_declaration_clause. However, the
22366 `void_list_node' is never appended to the list. */
22369 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
22371 tree parameters
= NULL_TREE
;
22372 tree
*tail
= ¶meters
;
22373 bool saved_in_unbraced_linkage_specification_p
;
22376 /* The special considerations that apply to a function within an
22377 unbraced linkage specifications do not apply to the parameters
22378 to the function. */
22379 saved_in_unbraced_linkage_specification_p
22380 = parser
->in_unbraced_linkage_specification_p
;
22381 parser
->in_unbraced_linkage_specification_p
= false;
22383 /* Look for more parameters. */
22386 cp_parameter_declarator
*parameter
;
22387 tree decl
= error_mark_node
;
22388 bool parenthesized_p
= false;
22390 /* Parse the parameter. */
22392 = cp_parser_parameter_declaration (parser
, flags
,
22393 /*template_parm_p=*/false,
22396 /* We don't know yet if the enclosing context is deprecated, so wait
22397 and warn in grokparms if appropriate. */
22398 deprecated_state
= DEPRECATED_SUPPRESS
;
22402 decl
= grokdeclarator (parameter
->declarator
,
22403 ¶meter
->decl_specifiers
,
22405 parameter
->default_argument
!= NULL_TREE
,
22406 ¶meter
->decl_specifiers
.attributes
);
22407 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
22408 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
22411 deprecated_state
= DEPRECATED_NORMAL
;
22413 /* If a parse error occurred parsing the parameter declaration,
22414 then the entire parameter-declaration-list is erroneous. */
22415 if (decl
== error_mark_node
)
22417 parameters
= error_mark_node
;
22421 if (parameter
->decl_specifiers
.attributes
)
22422 cplus_decl_attributes (&decl
,
22423 parameter
->decl_specifiers
.attributes
,
22425 if (DECL_NAME (decl
))
22426 decl
= pushdecl (decl
);
22428 if (decl
!= error_mark_node
)
22430 retrofit_lang_decl (decl
);
22431 DECL_PARM_INDEX (decl
) = ++index
;
22432 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
22435 /* Add the new parameter to the list. */
22436 *tail
= build_tree_list (parameter
->default_argument
, decl
);
22437 tail
= &TREE_CHAIN (*tail
);
22439 /* Peek at the next token. */
22440 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
22441 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
22442 /* These are for Objective-C++ */
22443 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
22444 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
22445 /* The parameter-declaration-list is complete. */
22447 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
22451 /* Peek at the next token. */
22452 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
22453 /* If it's an ellipsis, then the list is complete. */
22454 if (token
->type
== CPP_ELLIPSIS
)
22456 /* Otherwise, there must be more parameters. Consume the
22458 cp_lexer_consume_token (parser
->lexer
);
22459 /* When parsing something like:
22461 int i(float f, double d)
22463 we can tell after seeing the declaration for "f" that we
22464 are not looking at an initialization of a variable "i",
22465 but rather at the declaration of a function "i".
22467 Due to the fact that the parsing of template arguments
22468 (as specified to a template-id) requires backtracking we
22469 cannot use this technique when inside a template argument
22471 if (!parser
->in_template_argument_list_p
22472 && !parser
->in_type_id_in_expr_p
22473 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22474 /* However, a parameter-declaration of the form
22475 "float(f)" (which is a valid declaration of a
22476 parameter "f") can also be interpreted as an
22477 expression (the conversion of "f" to "float"). */
22478 && !parenthesized_p
)
22479 cp_parser_commit_to_tentative_parse (parser
);
22483 cp_parser_error (parser
, "expected %<,%> or %<...%>");
22484 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22485 cp_parser_skip_to_closing_parenthesis (parser
,
22486 /*recovering=*/true,
22487 /*or_comma=*/false,
22488 /*consume_paren=*/false);
22493 parser
->in_unbraced_linkage_specification_p
22494 = saved_in_unbraced_linkage_specification_p
;
22496 /* Reset implicit_template_scope if we are about to leave the function
22497 parameter list that introduced it. Note that for out-of-line member
22498 definitions, there will be one or more class scopes before we get to
22499 the template parameter scope. */
22501 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
22502 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
22504 while (maybe_its
->kind
== sk_class
)
22505 maybe_its
= maybe_its
->level_chain
;
22506 if (maybe_its
== its
)
22508 parser
->implicit_template_parms
= 0;
22509 parser
->implicit_template_scope
= 0;
22516 /* Parse a parameter declaration.
22518 parameter-declaration:
22519 decl-specifier-seq ... [opt] declarator
22520 decl-specifier-seq declarator = assignment-expression
22521 decl-specifier-seq ... [opt] abstract-declarator [opt]
22522 decl-specifier-seq abstract-declarator [opt] = assignment-expression
22524 The parser flags FLAGS is used to control type-specifier parsing.
22526 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
22527 declares a template parameter. (In that case, a non-nested `>'
22528 token encountered during the parsing of the assignment-expression
22529 is not interpreted as a greater-than operator.)
22531 Returns a representation of the parameter, or NULL if an error
22532 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
22533 true iff the declarator is of the form "(p)". */
22535 static cp_parameter_declarator
*
22536 cp_parser_parameter_declaration (cp_parser
*parser
,
22537 cp_parser_flags flags
,
22538 bool template_parm_p
,
22539 bool *parenthesized_p
)
22541 int declares_class_or_enum
;
22542 cp_decl_specifier_seq decl_specifiers
;
22543 cp_declarator
*declarator
;
22544 tree default_argument
;
22545 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
22546 const char *saved_message
;
22547 bool template_parameter_pack_p
= false;
22549 /* In a template parameter, `>' is not an operator.
22553 When parsing a default template-argument for a non-type
22554 template-parameter, the first non-nested `>' is taken as the end
22555 of the template parameter-list rather than a greater-than
22558 /* Type definitions may not appear in parameter types. */
22559 saved_message
= parser
->type_definition_forbidden_message
;
22560 parser
->type_definition_forbidden_message
22561 = G_("types may not be defined in parameter types");
22563 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
22564 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
22565 (current_template_parms
)) : 0);
22567 /* Parse the declaration-specifiers. */
22568 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
22569 cp_parser_decl_specifier_seq (parser
,
22572 &declares_class_or_enum
);
22574 /* Complain about missing 'typename' or other invalid type names. */
22575 if (!decl_specifiers
.any_type_specifiers_p
22576 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
22577 decl_specifiers
.type
= error_mark_node
;
22579 /* If an error occurred, there's no reason to attempt to parse the
22580 rest of the declaration. */
22581 if (cp_parser_error_occurred (parser
))
22583 parser
->type_definition_forbidden_message
= saved_message
;
22587 /* Peek at the next token. */
22588 token
= cp_lexer_peek_token (parser
->lexer
);
22590 /* If the next token is a `)', `,', `=', `>', or `...', then there
22591 is no declarator. However, when variadic templates are enabled,
22592 there may be a declarator following `...'. */
22593 if (token
->type
== CPP_CLOSE_PAREN
22594 || token
->type
== CPP_COMMA
22595 || token
->type
== CPP_EQ
22596 || token
->type
== CPP_GREATER
)
22599 if (parenthesized_p
)
22600 *parenthesized_p
= false;
22602 /* Otherwise, there should be a declarator. */
22605 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22606 parser
->default_arg_ok_p
= false;
22608 /* After seeing a decl-specifier-seq, if the next token is not a
22609 "(", there is no possibility that the code is a valid
22610 expression. Therefore, if parsing tentatively, we commit at
22612 if (!parser
->in_template_argument_list_p
22613 /* In an expression context, having seen:
22617 we cannot be sure whether we are looking at a
22618 function-type (taking a "char" as a parameter) or a cast
22619 of some object of type "char" to "int". */
22620 && !parser
->in_type_id_in_expr_p
22621 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
22622 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
22623 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
22624 cp_parser_commit_to_tentative_parse (parser
);
22625 /* Parse the declarator. */
22626 declarator_token_start
= token
;
22627 declarator
= cp_parser_declarator (parser
,
22628 CP_PARSER_DECLARATOR_EITHER
,
22629 CP_PARSER_FLAGS_NONE
,
22630 /*ctor_dtor_or_conv_p=*/NULL
,
22632 /*member_p=*/false,
22633 /*friend_p=*/false,
22634 /*static_p=*/false);
22635 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22636 /* After the declarator, allow more attributes. */
22637 decl_specifiers
.attributes
22638 = attr_chainon (decl_specifiers
.attributes
,
22639 cp_parser_attributes_opt (parser
));
22641 /* If the declarator is a template parameter pack, remember that and
22642 clear the flag in the declarator itself so we don't get errors
22643 from grokdeclarator. */
22644 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
22646 declarator
->parameter_pack_p
= false;
22647 template_parameter_pack_p
= true;
22651 /* If the next token is an ellipsis, and we have not seen a declarator
22652 name, and if either the type of the declarator contains parameter
22653 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
22654 for, eg, abbreviated integral type names), then we actually have a
22655 parameter pack expansion expression. Otherwise, leave the ellipsis
22656 for a C-style variadic function. */
22657 token
= cp_lexer_peek_token (parser
->lexer
);
22659 /* If a function parameter pack was specified and an implicit template
22660 parameter was introduced during cp_parser_parameter_declaration,
22661 change any implicit parameters introduced into packs. */
22662 if (parser
->implicit_template_parms
22663 && ((token
->type
== CPP_ELLIPSIS
22664 && declarator_can_be_parameter_pack (declarator
))
22665 || (declarator
&& declarator
->parameter_pack_p
)))
22667 int latest_template_parm_idx
= TREE_VEC_LENGTH
22668 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
22670 if (latest_template_parm_idx
!= template_parm_idx
)
22671 decl_specifiers
.type
= convert_generic_types_to_packs
22672 (decl_specifiers
.type
,
22673 template_parm_idx
, latest_template_parm_idx
);
22676 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
22678 tree type
= decl_specifiers
.type
;
22680 if (type
&& DECL_P (type
))
22681 type
= TREE_TYPE (type
);
22684 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
22685 && (template_parm_p
|| uses_parameter_packs (type
)))
22686 || (!type
&& template_parm_p
))
22687 && declarator_can_be_parameter_pack (declarator
))
22689 /* Consume the `...'. */
22690 cp_lexer_consume_token (parser
->lexer
);
22691 maybe_warn_variadic_templates ();
22693 /* Build a pack expansion type */
22694 if (template_parm_p
)
22695 template_parameter_pack_p
= true;
22696 else if (declarator
)
22697 declarator
->parameter_pack_p
= true;
22699 decl_specifiers
.type
= make_pack_expansion (type
);
22703 /* The restriction on defining new types applies only to the type
22704 of the parameter, not to the default argument. */
22705 parser
->type_definition_forbidden_message
= saved_message
;
22707 /* If the next token is `=', then process a default argument. */
22708 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
22710 tree type
= decl_specifiers
.type
;
22711 token
= cp_lexer_peek_token (parser
->lexer
);
22712 /* If we are defining a class, then the tokens that make up the
22713 default argument must be saved and processed later. */
22714 if (!template_parm_p
&& at_class_scope_p ()
22715 && TYPE_BEING_DEFINED (current_class_type
)
22716 && !LAMBDA_TYPE_P (current_class_type
))
22717 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
22719 /* A constrained-type-specifier may declare a type
22720 template-parameter. */
22721 else if (declares_constrained_type_template_parameter (type
))
22723 = cp_parser_default_type_template_argument (parser
);
22725 /* A constrained-type-specifier may declare a
22726 template-template-parameter. */
22727 else if (declares_constrained_template_template_parameter (type
))
22729 = cp_parser_default_template_template_argument (parser
);
22731 /* Outside of a class definition, we can just parse the
22732 assignment-expression. */
22735 = cp_parser_default_argument (parser
, template_parm_p
);
22737 if (!parser
->default_arg_ok_p
)
22739 permerror (token
->location
,
22740 "default arguments are only "
22741 "permitted for function parameters");
22743 else if ((declarator
&& declarator
->parameter_pack_p
)
22744 || template_parameter_pack_p
22745 || (decl_specifiers
.type
22746 && PACK_EXPANSION_P (decl_specifiers
.type
)))
22748 /* Find the name of the parameter pack. */
22749 cp_declarator
*id_declarator
= declarator
;
22750 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
22751 id_declarator
= id_declarator
->declarator
;
22753 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
22754 error_at (declarator_token_start
->location
,
22756 ? G_("template parameter pack %qD "
22757 "cannot have a default argument")
22758 : G_("parameter pack %qD cannot have "
22759 "a default argument"),
22760 id_declarator
->u
.id
.unqualified_name
);
22762 error_at (declarator_token_start
->location
,
22764 ? G_("template parameter pack cannot have "
22765 "a default argument")
22766 : G_("parameter pack cannot have a "
22767 "default argument"));
22769 default_argument
= NULL_TREE
;
22773 default_argument
= NULL_TREE
;
22775 if (default_argument
)
22776 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
22778 /* Generate a location for the parameter, ranging from the start of the
22779 initial token to the end of the final token (using input_location for
22780 the latter, set up by cp_lexer_set_source_position_from_token when
22783 If we have a identifier, then use it for the caret location, e.g.
22785 extern int callee (int one, int (*two)(int, int), float three);
22786 ~~~~~~^~~~~~~~~~~~~~
22788 otherwise, reuse the start location for the caret location e.g.:
22790 extern int callee (int one, int (*)(int, int), float three);
22794 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
22795 ? declarator
->id_loc
22796 : decl_spec_token_start
->location
);
22797 location_t param_loc
= make_location (caret_loc
,
22798 decl_spec_token_start
->location
,
22801 return make_parameter_declarator (&decl_specifiers
,
22805 template_parameter_pack_p
);
22808 /* Parse a default argument and return it.
22810 TEMPLATE_PARM_P is true if this is a default argument for a
22811 non-type template parameter. */
22813 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
22815 tree default_argument
= NULL_TREE
;
22816 bool saved_greater_than_is_operator_p
;
22817 unsigned char saved_local_variables_forbidden_p
;
22818 bool non_constant_p
, is_direct_init
;
22820 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
22822 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
22823 parser
->greater_than_is_operator_p
= !template_parm_p
;
22824 /* Local variable names (and the `this' keyword) may not
22825 appear in a default argument. */
22826 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
22827 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
22828 /* Parse the assignment-expression. */
22829 if (template_parm_p
)
22830 push_deferring_access_checks (dk_no_deferred
);
22831 tree saved_class_ptr
= NULL_TREE
;
22832 tree saved_class_ref
= NULL_TREE
;
22833 /* The "this" pointer is not valid in a default argument. */
22836 saved_class_ptr
= current_class_ptr
;
22837 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
22838 saved_class_ref
= current_class_ref
;
22839 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
22842 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
22843 /* Restore the "this" pointer. */
22846 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
22847 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
22849 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
22850 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22851 if (template_parm_p
)
22852 pop_deferring_access_checks ();
22853 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
22854 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
22856 return default_argument
;
22859 /* Parse a function-body.
22862 compound_statement */
22865 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
22867 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
22868 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
22872 /* Parse a ctor-initializer-opt followed by a function-body. Return
22873 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
22874 is true we are parsing a function-try-block. */
22877 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
22878 bool in_function_try_block
)
22881 const bool check_body_p
22882 = (DECL_CONSTRUCTOR_P (current_function_decl
)
22883 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
22886 if (in_function_try_block
22887 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
22888 && cxx_dialect
< cxx2a
)
22890 if (DECL_CONSTRUCTOR_P (current_function_decl
))
22891 pedwarn (input_location
, 0,
22892 "function-try-block body of %<constexpr%> constructor only "
22893 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22895 pedwarn (input_location
, 0,
22896 "function-try-block body of %<constexpr%> function only "
22897 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
22900 /* Begin the function body. */
22901 body
= begin_function_body ();
22902 /* Parse the optional ctor-initializer. */
22903 cp_parser_ctor_initializer_opt (parser
);
22905 /* If we're parsing a constexpr constructor definition, we need
22906 to check that the constructor body is indeed empty. However,
22907 before we get to cp_parser_function_body lot of junk has been
22908 generated, so we can't just check that we have an empty block.
22909 Rather we take a snapshot of the outermost block, and check whether
22910 cp_parser_function_body changed its state. */
22913 list
= cur_stmt_list
;
22914 if (STATEMENT_LIST_TAIL (list
))
22915 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
22917 /* Parse the function-body. */
22918 cp_parser_function_body (parser
, in_function_try_block
);
22920 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
22921 /* Finish the function body. */
22922 finish_function_body (body
);
22925 /* Parse an initializer.
22928 = initializer-clause
22929 ( expression-list )
22931 Returns an expression representing the initializer. If no
22932 initializer is present, NULL_TREE is returned.
22934 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
22935 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
22936 set to TRUE if there is no initializer present. If there is an
22937 initializer, and it is not a constant-expression, *NON_CONSTANT_P
22938 is set to true; otherwise it is set to false. */
22941 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
22942 bool* non_constant_p
, bool subexpression_p
)
22947 /* Peek at the next token. */
22948 token
= cp_lexer_peek_token (parser
->lexer
);
22950 /* Let our caller know whether or not this initializer was
22952 *is_direct_init
= (token
->type
!= CPP_EQ
);
22953 /* Assume that the initializer is constant. */
22954 *non_constant_p
= false;
22956 if (token
->type
== CPP_EQ
)
22958 /* Consume the `='. */
22959 cp_lexer_consume_token (parser
->lexer
);
22960 /* Parse the initializer-clause. */
22961 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
22963 else if (token
->type
== CPP_OPEN_PAREN
)
22965 vec
<tree
, va_gc
> *vec
;
22966 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
22968 /*allow_expansion_p=*/true,
22971 return error_mark_node
;
22972 init
= build_tree_list_vec (vec
);
22973 release_tree_vector (vec
);
22975 else if (token
->type
== CPP_OPEN_BRACE
)
22977 cp_lexer_set_source_position (parser
->lexer
);
22978 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
22979 init
= cp_parser_braced_list (parser
, non_constant_p
);
22980 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
22984 /* Anything else is an error. */
22985 cp_parser_error (parser
, "expected initializer");
22986 init
= error_mark_node
;
22989 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
22990 init
= error_mark_node
;
22995 /* Parse an initializer-clause.
22997 initializer-clause:
22998 assignment-expression
23001 Returns an expression representing the initializer.
23003 If the `assignment-expression' production is used the value
23004 returned is simply a representation for the expression.
23006 Otherwise, calls cp_parser_braced_list. */
23009 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
23011 cp_expr initializer
;
23013 /* Assume the expression is constant. */
23014 *non_constant_p
= false;
23016 /* If it is not a `{', then we are looking at an
23017 assignment-expression. */
23018 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
23021 = cp_parser_constant_expression (parser
,
23022 /*allow_non_constant_p=*/true,
23026 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
23028 return initializer
;
23031 /* Parse a brace-enclosed initializer list.
23034 { initializer-list , [opt] }
23035 { designated-initializer-list , [opt] }
23038 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
23039 the elements of the initializer-list (or NULL, if the last
23040 production is used). The TREE_TYPE for the CONSTRUCTOR will be
23041 NULL_TREE. There is no way to detect whether or not the optional
23042 trailing `,' was provided. NON_CONSTANT_P is as for
23043 cp_parser_initializer. */
23046 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
23049 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23051 /* Consume the `{' token. */
23052 matching_braces braces
;
23053 braces
.require_open (parser
);
23054 /* Create a CONSTRUCTOR to represent the braced-initializer. */
23055 initializer
= make_node (CONSTRUCTOR
);
23056 /* If it's not a `}', then there is a non-trivial initializer. */
23057 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
23060 /* Parse the initializer list. */
23061 CONSTRUCTOR_ELTS (initializer
)
23062 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
23063 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
23064 /* A trailing `,' token is allowed. */
23065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23066 cp_lexer_consume_token (parser
->lexer
);
23069 *non_constant_p
= false;
23070 /* Now, there should be a trailing `}'. */
23071 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23072 braces
.require_close (parser
);
23073 TREE_TYPE (initializer
) = init_list_type_node
;
23075 cp_expr
result (initializer
);
23076 /* Build a location of the form:
23079 with caret==start at the open brace, finish at the close brace. */
23080 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
23081 result
.set_location (combined_loc
);
23085 /* Consume tokens up to, and including, the next non-nested closing `]'.
23086 Returns true iff we found a closing `]'. */
23089 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
23091 unsigned square_depth
= 0;
23095 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
23097 switch (token
->type
)
23099 case CPP_PRAGMA_EOL
:
23100 if (!parser
->lexer
->in_pragma
)
23104 /* If we've run out of tokens, then there is no closing `]'. */
23107 case CPP_OPEN_SQUARE
:
23111 case CPP_CLOSE_SQUARE
:
23112 if (!square_depth
--)
23114 cp_lexer_consume_token (parser
->lexer
);
23123 /* Consume the token. */
23124 cp_lexer_consume_token (parser
->lexer
);
23128 /* Return true if we are looking at an array-designator, false otherwise. */
23131 cp_parser_array_designator_p (cp_parser
*parser
)
23133 /* Consume the `['. */
23134 cp_lexer_consume_token (parser
->lexer
);
23136 cp_lexer_save_tokens (parser
->lexer
);
23138 /* Skip tokens until the next token is a closing square bracket.
23139 If we find the closing `]', and the next token is a `=', then
23140 we are looking at an array designator. */
23141 bool array_designator_p
23142 = (cp_parser_skip_to_closing_square_bracket (parser
)
23143 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
23145 /* Roll back the tokens we skipped. */
23146 cp_lexer_rollback_tokens (parser
->lexer
);
23148 return array_designator_p
;
23151 /* Parse an initializer-list.
23154 initializer-clause ... [opt]
23155 initializer-list , initializer-clause ... [opt]
23159 designated-initializer-list:
23160 designated-initializer-clause
23161 designated-initializer-list , designated-initializer-clause
23163 designated-initializer-clause:
23164 designator brace-or-equal-initializer
23172 designation initializer-clause ...[opt]
23173 initializer-list , designation initializer-clause ...[opt]
23178 [ constant-expression ] =
23180 Returns a vec of constructor_elt. The VALUE of each elt is an expression
23181 for the initializer. If the INDEX of the elt is non-NULL, it is the
23182 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
23183 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
23184 are any designators. */
23186 static vec
<constructor_elt
, va_gc
> *
23187 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
23190 vec
<constructor_elt
, va_gc
> *v
= NULL
;
23191 bool first_p
= true;
23192 tree first_designator
= NULL_TREE
;
23194 /* Assume all of the expressions are constant. */
23195 *non_constant_p
= false;
23197 /* Parse the rest of the list. */
23203 bool clause_non_constant_p
;
23204 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23206 /* Handle the C++2A syntax, '. id ='. */
23207 if ((cxx_dialect
>= cxx2a
23208 || cp_parser_allow_gnu_extensions_p (parser
))
23209 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
23210 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
23211 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
23212 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
23213 == CPP_OPEN_BRACE
)))
23215 if (cxx_dialect
< cxx2a
)
23216 pedwarn (loc
, OPT_Wpedantic
,
23217 "C++ designated initializers only available with "
23218 "%<-std=c++2a%> or %<-std=gnu++2a%>");
23219 /* Consume the `.'. */
23220 cp_lexer_consume_token (parser
->lexer
);
23221 /* Consume the identifier. */
23222 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23223 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23224 /* Consume the `='. */
23225 cp_lexer_consume_token (parser
->lexer
);
23227 /* Also, if the next token is an identifier and the following one is a
23228 colon, we are looking at the GNU designated-initializer
23230 else if (cp_parser_allow_gnu_extensions_p (parser
)
23231 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
23232 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23235 /* Warn the user that they are using an extension. */
23236 pedwarn (loc
, OPT_Wpedantic
,
23237 "ISO C++ does not allow GNU designated initializers");
23238 /* Consume the identifier. */
23239 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
23240 /* Consume the `:'. */
23241 cp_lexer_consume_token (parser
->lexer
);
23243 /* Also handle C99 array designators, '[ const ] ='. */
23244 else if (cp_parser_allow_gnu_extensions_p (parser
)
23245 && !c_dialect_objc ()
23246 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
23248 /* In C++11, [ could start a lambda-introducer. */
23249 bool non_const
= false;
23251 cp_parser_parse_tentatively (parser
);
23253 if (!cp_parser_array_designator_p (parser
))
23255 cp_parser_simulate_error (parser
);
23256 designator
= NULL_TREE
;
23260 designator
= cp_parser_constant_expression (parser
, true,
23262 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
23263 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
23266 if (!cp_parser_parse_definitely (parser
))
23267 designator
= NULL_TREE
;
23269 && (!require_potential_rvalue_constant_expression
23271 designator
= NULL_TREE
;
23273 /* Warn the user that they are using an extension. */
23274 pedwarn (loc
, OPT_Wpedantic
,
23275 "ISO C++ does not allow C99 designated initializers");
23278 designator
= NULL_TREE
;
23282 first_designator
= designator
;
23285 else if (cxx_dialect
>= cxx2a
23286 && first_designator
!= error_mark_node
23287 && (!first_designator
!= !designator
))
23289 error_at (loc
, "either all initializer clauses should be designated "
23290 "or none of them should be");
23291 first_designator
= error_mark_node
;
23293 else if (cxx_dialect
< cxx2a
&& !first_designator
)
23294 first_designator
= designator
;
23296 /* Parse the initializer. */
23297 initializer
= cp_parser_initializer_clause (parser
,
23298 &clause_non_constant_p
);
23299 /* If any clause is non-constant, so is the entire initializer. */
23300 if (clause_non_constant_p
)
23301 *non_constant_p
= true;
23303 /* If we have an ellipsis, this is an initializer pack
23305 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23307 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
23309 /* Consume the `...'. */
23310 cp_lexer_consume_token (parser
->lexer
);
23312 if (designator
&& cxx_dialect
>= cxx2a
)
23314 "%<...%> not allowed in designated initializer list");
23316 /* Turn the initializer into an initializer expansion. */
23317 initializer
= make_pack_expansion (initializer
);
23320 /* Add it to the vector. */
23321 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
23323 /* If the next token is not a comma, we have reached the end of
23325 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
23328 /* Peek at the next token. */
23329 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23330 /* If the next token is a `}', then we're still done. An
23331 initializer-clause can have a trailing `,' after the
23332 initializer-list and before the closing `}'. */
23333 if (token
->type
== CPP_CLOSE_BRACE
)
23336 /* Consume the `,' token. */
23337 cp_lexer_consume_token (parser
->lexer
);
23340 /* The same identifier shall not appear in multiple designators
23341 of a designated-initializer-list. */
23342 if (first_designator
)
23345 tree designator
, val
;
23346 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23347 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23349 if (IDENTIFIER_MARKED (designator
))
23351 error_at (cp_expr_loc_or_input_loc (val
),
23352 "%<.%s%> designator used multiple times in "
23353 "the same initializer list",
23354 IDENTIFIER_POINTER (designator
));
23355 (*v
)[i
].index
= error_mark_node
;
23358 IDENTIFIER_MARKED (designator
) = 1;
23360 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
23361 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
23362 IDENTIFIER_MARKED (designator
) = 0;
23365 *designated
= first_designator
!= NULL_TREE
;
23369 /* Classes [gram.class] */
23371 /* Parse a class-name.
23377 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
23378 to indicate that names looked up in dependent types should be
23379 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
23380 keyword has been used to indicate that the name that appears next
23381 is a template. TAG_TYPE indicates the explicit tag given before
23382 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
23383 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
23384 is the class being defined in a class-head. If ENUM_OK is TRUE,
23385 enum-names are also accepted.
23387 Returns the TYPE_DECL representing the class. */
23390 cp_parser_class_name (cp_parser
*parser
,
23391 bool typename_keyword_p
,
23392 bool template_keyword_p
,
23393 enum tag_types tag_type
,
23394 bool check_dependency_p
,
23396 bool is_declaration
,
23403 tree identifier
= NULL_TREE
;
23405 /* All class-names start with an identifier. */
23406 token
= cp_lexer_peek_token (parser
->lexer
);
23407 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
23409 cp_parser_error (parser
, "expected class-name");
23410 return error_mark_node
;
23413 /* PARSER->SCOPE can be cleared when parsing the template-arguments
23414 to a template-id, so we save it here. */
23415 scope
= parser
->scope
;
23416 if (scope
== error_mark_node
)
23417 return error_mark_node
;
23419 /* Any name names a type if we're following the `typename' keyword
23420 in a qualified name where the enclosing scope is type-dependent. */
23421 typename_p
= (typename_keyword_p
&& scope
&& TYPE_P (scope
)
23422 && dependent_type_p (scope
));
23423 /* Handle the common case (an identifier, but not a template-id)
23425 if (token
->type
== CPP_NAME
23426 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
23428 cp_token
*identifier_token
;
23431 /* Look for the identifier. */
23432 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
23433 ambiguous_p
= identifier_token
->error_reported
;
23434 identifier
= cp_parser_identifier (parser
);
23435 /* If the next token isn't an identifier, we are certainly not
23436 looking at a class-name. */
23437 if (identifier
== error_mark_node
)
23438 decl
= error_mark_node
;
23439 /* If we know this is a type-name, there's no need to look it
23441 else if (typename_p
)
23445 tree ambiguous_decls
;
23446 /* If we already know that this lookup is ambiguous, then
23447 we've already issued an error message; there's no reason
23451 cp_parser_simulate_error (parser
);
23452 return error_mark_node
;
23454 /* If the next token is a `::', then the name must be a type
23457 [basic.lookup.qual]
23459 During the lookup for a name preceding the :: scope
23460 resolution operator, object, function, and enumerator
23461 names are ignored. */
23462 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23463 tag_type
= scope_type
;
23464 /* Look up the name. */
23465 decl
= cp_parser_lookup_name (parser
, identifier
,
23467 /*is_template=*/false,
23468 /*is_namespace=*/false,
23469 check_dependency_p
,
23471 identifier_token
->location
);
23472 if (ambiguous_decls
)
23474 if (cp_parser_parsing_tentatively (parser
))
23475 cp_parser_simulate_error (parser
);
23476 return error_mark_node
;
23482 /* Try a template-id. */
23483 decl
= cp_parser_template_id (parser
, template_keyword_p
,
23484 check_dependency_p
,
23487 if (decl
== error_mark_node
)
23488 return error_mark_node
;
23491 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
23493 /* If this is a typename, create a TYPENAME_TYPE. */
23495 && decl
!= error_mark_node
23496 && !is_overloaded_fn (decl
))
23498 decl
= make_typename_type (scope
, decl
, typename_type
,
23499 /*complain=*/tf_error
);
23500 if (decl
!= error_mark_node
)
23501 decl
= TYPE_NAME (decl
);
23504 decl
= strip_using_decl (decl
);
23506 /* Check to see that it is really the name of a class. */
23507 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
23508 && identifier_p (TREE_OPERAND (decl
, 0))
23509 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
23510 /* Situations like this:
23512 template <typename T> struct A {
23513 typename T::template X<int>::I i;
23516 are problematic. Is `T::template X<int>' a class-name? The
23517 standard does not seem to be definitive, but there is no other
23518 valid interpretation of the following `::'. Therefore, those
23519 names are considered class-names. */
23521 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
23522 if (decl
!= error_mark_node
)
23523 decl
= TYPE_NAME (decl
);
23525 else if (TREE_CODE (decl
) != TYPE_DECL
23526 || TREE_TYPE (decl
) == error_mark_node
23527 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
23528 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
23529 /* In Objective-C 2.0, a classname followed by '.' starts a
23530 dot-syntax expression, and it's not a type-name. */
23531 || (c_dialect_objc ()
23532 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
23533 && objc_is_class_name (decl
)))
23534 decl
= error_mark_node
;
23536 if (decl
== error_mark_node
)
23537 cp_parser_error (parser
, "expected class-name");
23538 else if (identifier
&& !parser
->scope
)
23539 maybe_note_name_used_in_class (identifier
, decl
);
23544 /* Make sure that any member-function parameters are in scope.
23545 For instance, a function's noexcept-specifier can use the function's
23549 void fn (int p) noexcept(noexcept(p));
23552 so we need to make sure name lookup can find them. This is used
23553 when we delay parsing of the noexcept-specifier. */
23556 inject_parm_decls (tree decl
)
23558 begin_scope (sk_function_parms
, decl
);
23559 tree args
= DECL_ARGUMENTS (decl
);
23561 do_push_parm_decls (decl
, args
, /*nonparms=*/NULL
);
23564 /* Undo the effects of inject_parm_decls. */
23567 pop_injected_parms (void)
23569 pop_bindings_and_leave_scope ();
23572 /* Parse a class-specifier.
23575 class-head { member-specification [opt] }
23577 Returns the TREE_TYPE representing the class. */
23580 cp_parser_class_specifier_1 (cp_parser
* parser
)
23583 tree attributes
= NULL_TREE
;
23584 bool nested_name_specifier_p
;
23585 unsigned saved_num_template_parameter_lists
;
23586 bool saved_in_function_body
;
23587 unsigned char in_statement
;
23588 bool in_switch_statement_p
;
23589 bool saved_in_unbraced_linkage_specification_p
;
23590 tree old_scope
= NULL_TREE
;
23591 tree scope
= NULL_TREE
;
23592 cp_token
*closing_brace
;
23594 push_deferring_access_checks (dk_no_deferred
);
23596 /* Parse the class-head. */
23597 type
= cp_parser_class_head (parser
,
23598 &nested_name_specifier_p
);
23599 /* If the class-head was a semantic disaster, skip the entire body
23603 cp_parser_skip_to_end_of_block_or_statement (parser
);
23604 pop_deferring_access_checks ();
23605 return error_mark_node
;
23608 /* Look for the `{'. */
23609 matching_braces braces
;
23610 if (!braces
.require_open (parser
))
23612 pop_deferring_access_checks ();
23613 return error_mark_node
;
23616 cp_ensure_no_omp_declare_simd (parser
);
23617 cp_ensure_no_oacc_routine (parser
);
23619 /* Issue an error message if type-definitions are forbidden here. */
23620 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
23621 /* Remember that we are defining one more class. */
23622 ++parser
->num_classes_being_defined
;
23623 /* Inside the class, surrounding template-parameter-lists do not
23625 saved_num_template_parameter_lists
23626 = parser
->num_template_parameter_lists
;
23627 parser
->num_template_parameter_lists
= 0;
23628 /* We are not in a function body. */
23629 saved_in_function_body
= parser
->in_function_body
;
23630 parser
->in_function_body
= false;
23631 /* Or in a loop. */
23632 in_statement
= parser
->in_statement
;
23633 parser
->in_statement
= 0;
23634 /* Or in a switch. */
23635 in_switch_statement_p
= parser
->in_switch_statement_p
;
23636 parser
->in_switch_statement_p
= false;
23637 /* We are not immediately inside an extern "lang" block. */
23638 saved_in_unbraced_linkage_specification_p
23639 = parser
->in_unbraced_linkage_specification_p
;
23640 parser
->in_unbraced_linkage_specification_p
= false;
23642 // Associate constraints with the type.
23644 type
= associate_classtype_constraints (type
);
23646 /* Start the class. */
23647 if (nested_name_specifier_p
)
23649 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
23650 old_scope
= push_inner_scope (scope
);
23652 type
= begin_class_definition (type
);
23654 if (type
== error_mark_node
)
23655 /* If the type is erroneous, skip the entire body of the class. */
23656 cp_parser_skip_to_closing_brace (parser
);
23658 /* Parse the member-specification. */
23659 cp_parser_member_specification_opt (parser
);
23661 /* Look for the trailing `}'. */
23662 closing_brace
= braces
.require_close (parser
);
23663 /* Look for trailing attributes to apply to this class. */
23664 if (cp_parser_allow_gnu_extensions_p (parser
))
23665 attributes
= cp_parser_gnu_attributes_opt (parser
);
23666 if (type
!= error_mark_node
)
23667 type
= finish_struct (type
, attributes
);
23668 if (nested_name_specifier_p
)
23669 pop_inner_scope (old_scope
, scope
);
23671 /* We've finished a type definition. Check for the common syntax
23672 error of forgetting a semicolon after the definition. We need to
23673 be careful, as we can't just check for not-a-semicolon and be done
23674 with it; the user might have typed:
23676 class X { } c = ...;
23677 class X { } *p = ...;
23679 and so forth. Instead, enumerate all the possible tokens that
23680 might follow this production; if we don't see one of them, then
23681 complain and silently insert the semicolon. */
23683 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
23684 bool want_semicolon
= true;
23686 if (cp_next_tokens_can_be_std_attribute_p (parser
))
23687 /* Don't try to parse c++11 attributes here. As per the
23688 grammar, that should be a task for
23689 cp_parser_decl_specifier_seq. */
23690 want_semicolon
= false;
23692 switch (token
->type
)
23695 case CPP_SEMICOLON
:
23698 case CPP_OPEN_PAREN
:
23699 case CPP_CLOSE_PAREN
:
23701 want_semicolon
= false;
23704 /* While it's legal for type qualifiers and storage class
23705 specifiers to follow type definitions in the grammar, only
23706 compiler testsuites contain code like that. Assume that if
23707 we see such code, then what we're really seeing is a case
23711 const <type> var = ...;
23716 static <type> func (...) ...
23718 i.e. the qualifier or specifier applies to the next
23719 declaration. To do so, however, we need to look ahead one
23720 more token to see if *that* token is a type specifier.
23722 This code could be improved to handle:
23725 static const <type> var = ...; */
23727 if (keyword_is_decl_specifier (token
->keyword
))
23729 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23731 /* Handling user-defined types here would be nice, but very
23734 = (lookahead
->type
== CPP_KEYWORD
23735 && keyword_begins_type_specifier (lookahead
->keyword
));
23742 /* If we don't have a type, then something is very wrong and we
23743 shouldn't try to do anything clever. Likewise for not seeing the
23745 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
23747 /* Locate the closing brace. */
23748 cp_token_position prev
23749 = cp_lexer_previous_token_position (parser
->lexer
);
23750 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
23751 location_t loc
= prev_token
->location
;
23753 /* We want to suggest insertion of a ';' immediately *after* the
23754 closing brace, so, if we can, offset the location by 1 column. */
23755 location_t next_loc
= loc
;
23756 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
23757 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
23759 rich_location
richloc (line_table
, next_loc
);
23761 /* If we successfully offset the location, suggest the fix-it. */
23762 if (next_loc
!= loc
)
23763 richloc
.add_fixit_insert_before (next_loc
, ";");
23765 if (CLASSTYPE_DECLARED_CLASS (type
))
23766 error_at (&richloc
,
23767 "expected %<;%> after class definition");
23768 else if (TREE_CODE (type
) == RECORD_TYPE
)
23769 error_at (&richloc
,
23770 "expected %<;%> after struct definition");
23771 else if (TREE_CODE (type
) == UNION_TYPE
)
23772 error_at (&richloc
,
23773 "expected %<;%> after union definition");
23775 gcc_unreachable ();
23777 /* Unget one token and smash it to look as though we encountered
23778 a semicolon in the input stream. */
23779 cp_lexer_set_token_position (parser
->lexer
, prev
);
23780 token
= cp_lexer_peek_token (parser
->lexer
);
23781 token
->type
= CPP_SEMICOLON
;
23782 token
->keyword
= RID_MAX
;
23786 /* If this class is not itself within the scope of another class,
23787 then we need to parse the bodies of all of the queued function
23788 definitions. Note that the queued functions defined in a class
23789 are not always processed immediately following the
23790 class-specifier for that class. Consider:
23793 struct B { void f() { sizeof (A); } };
23796 If `f' were processed before the processing of `A' were
23797 completed, there would be no way to compute the size of `A'.
23798 Note that the nesting we are interested in here is lexical --
23799 not the semantic nesting given by TYPE_CONTEXT. In particular,
23802 struct A { struct B; };
23803 struct A::B { void f() { } };
23805 there is no need to delay the parsing of `A::B::f'. */
23806 if (--parser
->num_classes_being_defined
== 0)
23809 tree class_type
= NULL_TREE
;
23810 tree pushed_scope
= NULL_TREE
;
23812 cp_default_arg_entry
*e
;
23813 tree save_ccp
, save_ccr
;
23815 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
23817 /* Skip default arguments, NSDMIs, etc, in order to improve
23818 error recovery (c++/71169, c++/71832). */
23819 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23820 vec_safe_truncate (unparsed_nsdmis
, 0);
23821 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23824 /* In a first pass, parse default arguments to the functions.
23825 Then, in a second pass, parse the bodies of the functions.
23826 This two-phased approach handles cases like:
23834 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
23837 /* If there are default arguments that have not yet been processed,
23838 take care of them now. */
23839 if (class_type
!= e
->class_type
)
23842 pop_scope (pushed_scope
);
23843 class_type
= e
->class_type
;
23844 pushed_scope
= push_scope (class_type
);
23846 /* Make sure that any template parameters are in scope. */
23847 maybe_begin_member_template_processing (decl
);
23848 /* Parse the default argument expressions. */
23849 cp_parser_late_parsing_default_args (parser
, decl
);
23850 /* Remove any template parameters from the symbol table. */
23851 maybe_end_member_template_processing ();
23853 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
23854 /* Now parse any NSDMIs. */
23855 save_ccp
= current_class_ptr
;
23856 save_ccr
= current_class_ref
;
23857 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
23859 if (class_type
!= DECL_CONTEXT (decl
))
23862 pop_scope (pushed_scope
);
23863 class_type
= DECL_CONTEXT (decl
);
23864 pushed_scope
= push_scope (class_type
);
23866 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
23867 cp_parser_late_parsing_nsdmi (parser
, decl
);
23869 vec_safe_truncate (unparsed_nsdmis
, 0);
23870 current_class_ptr
= save_ccp
;
23871 current_class_ref
= save_ccr
;
23873 pop_scope (pushed_scope
);
23875 /* If there are noexcept-specifiers that have not yet been processed,
23876 take care of them now. */
23877 class_type
= NULL_TREE
;
23878 pushed_scope
= NULL_TREE
;
23879 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts
, ix
, decl
)
23881 tree ctx
= DECL_CONTEXT (decl
);
23882 if (class_type
!= ctx
)
23885 pop_scope (pushed_scope
);
23887 pushed_scope
= push_scope (class_type
);
23890 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
23891 spec
= TREE_PURPOSE (spec
);
23893 /* Make sure that any template parameters are in scope. */
23894 maybe_begin_member_template_processing (decl
);
23896 /* Make sure that any member-function parameters are in scope. */
23897 inject_parm_decls (decl
);
23899 /* 'this' is not allowed in static member functions. */
23900 unsigned char local_variables_forbidden_p
23901 = parser
->local_variables_forbidden_p
;
23902 if (DECL_THIS_STATIC (decl
))
23903 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
23905 /* Now we can parse the noexcept-specifier. */
23906 spec
= cp_parser_late_noexcept_specifier (parser
, spec
);
23908 if (spec
!= error_mark_node
)
23909 TREE_TYPE (decl
) = build_exception_variant (TREE_TYPE (decl
), spec
);
23911 /* Restore the state of local_variables_forbidden_p. */
23912 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
23914 /* The finish_struct call above performed various override checking,
23915 but it skipped unparsed noexcept-specifier operands. Now that we
23916 have resolved them, check again. */
23917 noexcept_override_late_checks (type
, decl
);
23919 /* Remove any member-function parameters from the symbol table. */
23920 pop_injected_parms ();
23922 /* Remove any template parameters from the symbol table. */
23923 maybe_end_member_template_processing ();
23925 vec_safe_truncate (unparsed_noexcepts
, 0);
23927 pop_scope (pushed_scope
);
23929 /* Now parse the body of the functions. */
23932 /* OpenMP UDRs need to be parsed before all other functions. */
23933 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23934 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
23935 cp_parser_late_parsing_for_member (parser
, decl
);
23936 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23937 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
23938 cp_parser_late_parsing_for_member (parser
, decl
);
23941 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
23942 cp_parser_late_parsing_for_member (parser
, decl
);
23943 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
23946 /* Put back any saved access checks. */
23947 pop_deferring_access_checks ();
23949 /* Restore saved state. */
23950 parser
->in_switch_statement_p
= in_switch_statement_p
;
23951 parser
->in_statement
= in_statement
;
23952 parser
->in_function_body
= saved_in_function_body
;
23953 parser
->num_template_parameter_lists
23954 = saved_num_template_parameter_lists
;
23955 parser
->in_unbraced_linkage_specification_p
23956 = saved_in_unbraced_linkage_specification_p
;
23962 cp_parser_class_specifier (cp_parser
* parser
)
23965 timevar_push (TV_PARSE_STRUCT
);
23966 ret
= cp_parser_class_specifier_1 (parser
);
23967 timevar_pop (TV_PARSE_STRUCT
);
23971 /* Parse a class-head.
23974 class-key identifier [opt] base-clause [opt]
23975 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
23976 class-key nested-name-specifier [opt] template-id
23979 class-virt-specifier:
23983 class-key attributes identifier [opt] base-clause [opt]
23984 class-key attributes nested-name-specifier identifier base-clause [opt]
23985 class-key attributes nested-name-specifier [opt] template-id
23988 Upon return BASES is initialized to the list of base classes (or
23989 NULL, if there are none) in the same form returned by
23990 cp_parser_base_clause.
23992 Returns the TYPE of the indicated class. Sets
23993 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
23994 involving a nested-name-specifier was used, and FALSE otherwise.
23996 Returns error_mark_node if this is not a class-head.
23998 Returns NULL_TREE if the class-head is syntactically valid, but
23999 semantically invalid in a way that means we should skip the entire
24000 body of the class. */
24003 cp_parser_class_head (cp_parser
* parser
,
24004 bool* nested_name_specifier_p
)
24006 tree nested_name_specifier
;
24007 enum tag_types class_key
;
24008 tree id
= NULL_TREE
;
24009 tree type
= NULL_TREE
;
24012 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
24013 bool template_id_p
= false;
24014 bool qualified_p
= false;
24015 bool invalid_nested_name_p
= false;
24016 bool invalid_explicit_specialization_p
= false;
24017 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24018 tree pushed_scope
= NULL_TREE
;
24019 unsigned num_templates
;
24020 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
24021 /* Assume no nested-name-specifier will be present. */
24022 *nested_name_specifier_p
= false;
24023 /* Assume no template parameter lists will be used in defining the
24026 parser
->colon_corrects_to_scope_p
= false;
24028 /* Look for the class-key. */
24029 class_key
= cp_parser_class_key (parser
);
24030 if (class_key
== none_type
)
24031 return error_mark_node
;
24033 location_t class_head_start_location
= input_location
;
24035 /* Parse the attributes. */
24036 attributes
= cp_parser_attributes_opt (parser
);
24038 /* If the next token is `::', that is invalid -- but sometimes
24039 people do try to write:
24043 Handle this gracefully by accepting the extra qualifier, and then
24044 issuing an error about it later if this really is a
24045 class-head. If it turns out just to be an elaborated type
24046 specifier, remain silent. */
24047 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
24048 qualified_p
= true;
24050 push_deferring_access_checks (dk_no_check
);
24052 /* Determine the name of the class. Begin by looking for an
24053 optional nested-name-specifier. */
24054 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
24055 nested_name_specifier
24056 = cp_parser_nested_name_specifier_opt (parser
,
24057 /*typename_keyword_p=*/false,
24058 /*check_dependency_p=*/false,
24060 /*is_declaration=*/false);
24061 /* If there was a nested-name-specifier, then there *must* be an
24064 cp_token
*bad_template_keyword
= NULL
;
24066 if (nested_name_specifier
)
24068 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24069 /* Although the grammar says `identifier', it really means
24070 `class-name' or `template-name'. You are only allowed to
24071 define a class that has already been declared with this
24074 The proposed resolution for Core Issue 180 says that wherever
24075 you see `class T::X' you should treat `X' as a type-name.
24077 It is OK to define an inaccessible class; for example:
24079 class A { class B; };
24082 We do not know if we will see a class-name, or a
24083 template-name. We look for a class-name first, in case the
24084 class-name is a template-id; if we looked for the
24085 template-name first we would stop after the template-name. */
24086 cp_parser_parse_tentatively (parser
);
24087 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24088 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
24089 type
= cp_parser_class_name (parser
,
24090 /*typename_keyword_p=*/false,
24091 /*template_keyword_p=*/false,
24093 /*check_dependency_p=*/false,
24094 /*class_head_p=*/true,
24095 /*is_declaration=*/false);
24096 /* If that didn't work, ignore the nested-name-specifier. */
24097 if (!cp_parser_parse_definitely (parser
))
24099 invalid_nested_name_p
= true;
24100 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24101 id
= cp_parser_identifier (parser
);
24102 if (id
== error_mark_node
)
24105 /* If we could not find a corresponding TYPE, treat this
24106 declaration like an unqualified declaration. */
24107 if (type
== error_mark_node
)
24108 nested_name_specifier
= NULL_TREE
;
24109 /* Otherwise, count the number of templates used in TYPE and its
24110 containing scopes. */
24112 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
24114 /* Otherwise, the identifier is optional. */
24117 /* We don't know whether what comes next is a template-id,
24118 an identifier, or nothing at all. */
24119 cp_parser_parse_tentatively (parser
);
24120 /* Check for a template-id. */
24121 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24122 id
= cp_parser_template_id (parser
,
24123 /*template_keyword_p=*/false,
24124 /*check_dependency_p=*/true,
24126 /*is_declaration=*/true);
24127 /* If that didn't work, it could still be an identifier. */
24128 if (!cp_parser_parse_definitely (parser
))
24130 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
24132 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
24133 id
= cp_parser_identifier (parser
);
24140 template_id_p
= true;
24145 pop_deferring_access_checks ();
24149 cp_parser_check_for_invalid_template_id (parser
, id
,
24151 type_start_token
->location
);
24153 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
24155 /* If it's not a `:' or a `{' then we can't really be looking at a
24156 class-head, since a class-head only appears as part of a
24157 class-specifier. We have to detect this situation before calling
24158 xref_tag, since that has irreversible side-effects. */
24159 if (!cp_parser_next_token_starts_class_definition_p (parser
))
24161 cp_parser_error (parser
, "expected %<{%> or %<:%>");
24162 type
= error_mark_node
;
24166 /* At this point, we're going ahead with the class-specifier, even
24167 if some other problem occurs. */
24168 cp_parser_commit_to_tentative_parse (parser
);
24169 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
24171 cp_parser_error (parser
,
24172 "cannot specify %<override%> for a class");
24173 type
= error_mark_node
;
24176 /* Issue the error about the overly-qualified name now. */
24179 cp_parser_error (parser
,
24180 "global qualification of class name is invalid");
24181 type
= error_mark_node
;
24184 else if (invalid_nested_name_p
)
24186 cp_parser_error (parser
,
24187 "qualified name does not name a class");
24188 type
= error_mark_node
;
24191 else if (nested_name_specifier
)
24195 if (bad_template_keyword
)
24196 /* [temp.names]: in a qualified-id formed by a class-head-name, the
24197 keyword template shall not appear at the top level. */
24198 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
24199 "keyword %<template%> not allowed in class-head-name");
24201 /* Reject typedef-names in class heads. */
24202 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
24204 error_at (type_start_token
->location
,
24205 "invalid class name in declaration of %qD",
24211 /* Figure out in what scope the declaration is being placed. */
24212 scope
= current_scope ();
24213 /* If that scope does not contain the scope in which the
24214 class was originally declared, the program is invalid. */
24215 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
24217 if (at_namespace_scope_p ())
24218 error_at (type_start_token
->location
,
24219 "declaration of %qD in namespace %qD which does not "
24221 type
, scope
, nested_name_specifier
);
24223 error_at (type_start_token
->location
,
24224 "declaration of %qD in %qD which does not enclose %qD",
24225 type
, scope
, nested_name_specifier
);
24231 A declarator-id shall not be qualified except for the
24232 definition of a ... nested class outside of its class
24233 ... [or] the definition or explicit instantiation of a
24234 class member of a namespace outside of its namespace. */
24235 if (scope
== nested_name_specifier
)
24236 permerror (nested_name_specifier_token_start
->location
,
24237 "extra qualification not allowed");
24239 /* An explicit-specialization must be preceded by "template <>". If
24240 it is not, try to recover gracefully. */
24241 if (at_namespace_scope_p ()
24242 && parser
->num_template_parameter_lists
== 0
24243 && !processing_template_parmlist
24246 /* Build a location of this form:
24247 struct typename <ARGS>
24248 ^~~~~~~~~~~~~~~~~~~~~~
24249 with caret==start at the start token, and
24250 finishing at the end of the type. */
24251 location_t reported_loc
24252 = make_location (class_head_start_location
,
24253 class_head_start_location
,
24254 get_finish (type_start_token
->location
));
24255 rich_location
richloc (line_table
, reported_loc
);
24256 richloc
.add_fixit_insert_before (class_head_start_location
,
24258 error_at (&richloc
,
24259 "an explicit specialization must be preceded by"
24260 " %<template <>%>");
24261 invalid_explicit_specialization_p
= true;
24262 /* Take the same action that would have been taken by
24263 cp_parser_explicit_specialization. */
24264 ++parser
->num_template_parameter_lists
;
24265 begin_specialization ();
24267 /* There must be no "return" statements between this point and the
24268 end of this function; set "type "to the correct return value and
24269 use "goto done;" to return. */
24270 /* Make sure that the right number of template parameters were
24272 if (!cp_parser_check_template_parameters (parser
, num_templates
,
24274 type_start_token
->location
,
24275 /*declarator=*/NULL
))
24277 /* If something went wrong, there is no point in even trying to
24278 process the class-definition. */
24283 /* Look up the type. */
24286 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
24287 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
24288 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
24290 error_at (type_start_token
->location
,
24291 "function template %qD redeclared as a class template", id
);
24292 type
= error_mark_node
;
24296 type
= TREE_TYPE (id
);
24297 type
= maybe_process_partial_specialization (type
);
24299 /* Check the scope while we still know whether or not we had a
24300 nested-name-specifier. */
24301 if (type
!= error_mark_node
)
24302 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
24304 if (nested_name_specifier
)
24305 pushed_scope
= push_scope (nested_name_specifier
);
24307 else if (nested_name_specifier
)
24313 template <typename T> struct S { struct T };
24314 template <typename T> struct S<T>::T { };
24316 we will get a TYPENAME_TYPE when processing the definition of
24317 `S::T'. We need to resolve it to the actual type before we
24318 try to define it. */
24319 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
24321 class_type
= resolve_typename_type (TREE_TYPE (type
),
24322 /*only_current_p=*/false);
24323 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
24324 type
= TYPE_NAME (class_type
);
24327 cp_parser_error (parser
, "could not resolve typename type");
24328 type
= error_mark_node
;
24332 if (maybe_process_partial_specialization (TREE_TYPE (type
))
24333 == error_mark_node
)
24339 class_type
= current_class_type
;
24340 /* Enter the scope indicated by the nested-name-specifier. */
24341 pushed_scope
= push_scope (nested_name_specifier
);
24342 /* Get the canonical version of this type. */
24343 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
24344 /* Call push_template_decl if it seems like we should be defining a
24345 template either from the template headers or the type we're
24346 defining, so that we diagnose both extra and missing headers. */
24347 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
24348 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
24349 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
24351 type
= push_template_decl (type
);
24352 if (type
== error_mark_node
)
24359 type
= TREE_TYPE (type
);
24360 *nested_name_specifier_p
= true;
24362 else /* The name is not a nested name. */
24364 /* If the class was unnamed, create a dummy name. */
24366 id
= make_anon_name ();
24367 tag_scope tag_scope
= (parser
->in_type_id_in_expr_p
24368 ? ts_within_enclosing_non_class
24370 type
= xref_tag (class_key
, id
, tag_scope
,
24371 parser
->num_template_parameter_lists
);
24374 /* Indicate whether this class was declared as a `class' or as a
24376 if (TREE_CODE (type
) == RECORD_TYPE
)
24377 CLASSTYPE_DECLARED_CLASS (type
) = (class_key
== class_type
);
24378 cp_parser_check_class_key (class_key
, type
);
24380 /* If this type was already complete, and we see another definition,
24381 that's an error. Likewise if the type is already being defined:
24382 this can happen, eg, when it's defined from within an expression
24384 if (type
!= error_mark_node
24385 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
24387 error_at (type_start_token
->location
, "redefinition of %q#T",
24389 inform (location_of (type
), "previous definition of %q#T",
24394 else if (type
== error_mark_node
)
24399 /* Apply attributes now, before any use of the class as a template
24400 argument in its base list. */
24401 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
24402 fixup_attribute_variants (type
);
24405 /* We will have entered the scope containing the class; the names of
24406 base classes should be looked up in that context. For example:
24408 struct A { struct B {}; struct C; };
24409 struct A::C : B {};
24413 /* Get the list of base-classes, if there is one. */
24414 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
24416 /* PR59482: enter the class scope so that base-specifiers are looked
24420 bases
= cp_parser_base_clause (parser
);
24421 /* PR59482: get out of the previously pushed class scope so that the
24422 subsequent pops pop the right thing. */
24429 /* If we're really defining a class, process the base classes.
24430 If they're invalid, fail. */
24431 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
24432 xref_basetypes (type
, bases
);
24435 /* Leave the scope given by the nested-name-specifier. We will
24436 enter the class scope itself while processing the members. */
24438 pop_scope (pushed_scope
);
24440 if (invalid_explicit_specialization_p
)
24442 end_specialization ();
24443 --parser
->num_template_parameter_lists
;
24447 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
24448 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
24449 CLASSTYPE_FINAL (type
) = 1;
24451 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
24455 /* Parse a class-key.
24462 Returns the kind of class-key specified, or none_type to indicate
24465 static enum tag_types
24466 cp_parser_class_key (cp_parser
* parser
)
24469 enum tag_types tag_type
;
24471 /* Look for the class-key. */
24472 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
24476 /* Check to see if the TOKEN is a class-key. */
24477 tag_type
= cp_parser_token_is_class_key (token
);
24479 cp_parser_error (parser
, "expected class-key");
24483 /* Parse a type-parameter-key.
24485 type-parameter-key:
24491 cp_parser_type_parameter_key (cp_parser
* parser
)
24493 /* Look for the type-parameter-key. */
24494 enum tag_types tag_type
= none_type
;
24495 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24496 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
24498 cp_lexer_consume_token (parser
->lexer
);
24499 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
24500 /* typename is not allowed in a template template parameter
24501 by the standard until C++17. */
24502 pedwarn (token
->location
, OPT_Wpedantic
,
24503 "ISO C++ forbids typename key in template template parameter;"
24504 " use %<-std=c++17%> or %<-std=gnu++17%>");
24507 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
24512 /* Parse an (optional) member-specification.
24514 member-specification:
24515 member-declaration member-specification [opt]
24516 access-specifier : member-specification [opt] */
24519 cp_parser_member_specification_opt (cp_parser
* parser
)
24526 /* Peek at the next token. */
24527 token
= cp_lexer_peek_token (parser
->lexer
);
24528 /* If it's a `}', or EOF then we've seen all the members. */
24529 if (token
->type
== CPP_CLOSE_BRACE
24530 || token
->type
== CPP_EOF
24531 || token
->type
== CPP_PRAGMA_EOL
)
24534 /* See if this token is a keyword. */
24535 keyword
= token
->keyword
;
24539 case RID_PROTECTED
:
24541 /* Consume the access-specifier. */
24542 cp_lexer_consume_token (parser
->lexer
);
24543 /* Remember which access-specifier is active. */
24544 current_access_specifier
= token
->u
.value
;
24545 /* Look for the `:'. */
24546 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
24550 /* Accept #pragmas at class scope. */
24551 if (token
->type
== CPP_PRAGMA
)
24553 cp_parser_pragma (parser
, pragma_member
, NULL
);
24557 /* Otherwise, the next construction must be a
24558 member-declaration. */
24559 cp_parser_member_declaration (parser
);
24564 /* Parse a member-declaration.
24566 member-declaration:
24567 decl-specifier-seq [opt] member-declarator-list [opt] ;
24568 function-definition ; [opt]
24569 :: [opt] nested-name-specifier template [opt] unqualified-id ;
24571 template-declaration
24574 member-declarator-list:
24576 member-declarator-list , member-declarator
24579 declarator pure-specifier [opt]
24580 declarator constant-initializer [opt]
24581 identifier [opt] : constant-expression
24585 member-declaration:
24586 __extension__ member-declaration
24589 declarator attributes [opt] pure-specifier [opt]
24590 declarator attributes [opt] constant-initializer [opt]
24591 identifier [opt] attributes [opt] : constant-expression
24595 member-declaration:
24596 static_assert-declaration */
24599 cp_parser_member_declaration (cp_parser
* parser
)
24601 cp_decl_specifier_seq decl_specifiers
;
24602 tree prefix_attributes
;
24604 int declares_class_or_enum
;
24606 cp_token
*token
= NULL
;
24607 cp_token
*decl_spec_token_start
= NULL
;
24608 cp_token
*initializer_token_start
= NULL
;
24609 int saved_pedantic
;
24610 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
24612 /* Check for the `__extension__' keyword. */
24613 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
24616 cp_parser_member_declaration (parser
);
24617 /* Restore the old value of the PEDANTIC flag. */
24618 pedantic
= saved_pedantic
;
24623 /* Check for a template-declaration. */
24624 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
24626 /* An explicit specialization here is an error condition, and we
24627 expect the specialization handler to detect and report this. */
24628 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
24629 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
24630 cp_parser_explicit_specialization (parser
);
24632 cp_parser_template_declaration (parser
, /*member_p=*/true);
24636 /* Check for a template introduction. */
24637 else if (cp_parser_template_declaration_after_export (parser
, true))
24640 /* Check for a using-declaration. */
24641 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
24643 if (cxx_dialect
< cxx11
)
24645 /* Parse the using-declaration. */
24646 cp_parser_using_declaration (parser
,
24647 /*access_declaration_p=*/false);
24653 bool alias_decl_expected
;
24654 cp_parser_parse_tentatively (parser
);
24655 decl
= cp_parser_alias_declaration (parser
);
24656 /* Note that if we actually see the '=' token after the
24657 identifier, cp_parser_alias_declaration commits the
24658 tentative parse. In that case, we really expect an
24659 alias-declaration. Otherwise, we expect a using
24661 alias_decl_expected
=
24662 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
24663 cp_parser_parse_definitely (parser
);
24665 if (alias_decl_expected
)
24666 finish_member_declaration (decl
);
24668 cp_parser_using_declaration (parser
,
24669 /*access_declaration_p=*/false);
24674 /* Check for @defs. */
24675 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
24678 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
24679 ivar
= ivar_chains
;
24683 ivar
= TREE_CHAIN (member
);
24684 TREE_CHAIN (member
) = NULL_TREE
;
24685 finish_member_declaration (member
);
24690 /* If the next token is `static_assert' we have a static assertion. */
24691 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
24693 cp_parser_static_assert (parser
, /*member_p=*/true);
24697 parser
->colon_corrects_to_scope_p
= false;
24699 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
24702 /* Parse the decl-specifier-seq. */
24703 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
24704 cp_parser_decl_specifier_seq (parser
,
24705 (CP_PARSER_FLAGS_OPTIONAL
24706 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
24708 &declares_class_or_enum
);
24709 /* Check for an invalid type-name. */
24710 if (!decl_specifiers
.any_type_specifiers_p
24711 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
24713 /* If there is no declarator, then the decl-specifier-seq should
24715 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
24717 /* If there was no decl-specifier-seq, and the next token is a
24718 `;', then we have something like:
24724 Each member-declaration shall declare at least one member
24725 name of the class. */
24726 if (!decl_specifiers
.any_specifiers_p
)
24728 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24729 if (!in_system_header_at (token
->location
))
24731 gcc_rich_location
richloc (token
->location
);
24732 richloc
.add_fixit_remove ();
24733 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
24740 /* See if this declaration is a friend. */
24741 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24742 /* If there were decl-specifiers, check to see if there was
24743 a class-declaration. */
24744 type
= check_tag_decl (&decl_specifiers
,
24745 /*explicit_type_instantiation_p=*/false);
24746 /* Nested classes have already been added to the class, but
24747 a `friend' needs to be explicitly registered. */
24750 /* If the `friend' keyword was present, the friend must
24751 be introduced with a class-key. */
24752 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
24753 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
24754 "in C++03 a class-key must be used "
24755 "when declaring a friend");
24758 template <typename T> struct A {
24759 friend struct A<T>::B;
24762 A<T>::B will be represented by a TYPENAME_TYPE, and
24763 therefore not recognized by check_tag_decl. */
24766 type
= decl_specifiers
.type
;
24767 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
24768 type
= TREE_TYPE (type
);
24770 if (!type
|| !TYPE_P (type
))
24771 error_at (decl_spec_token_start
->location
,
24772 "friend declaration does not name a class or "
24775 make_friend_class (current_class_type
, type
,
24776 /*complain=*/true);
24778 /* If there is no TYPE, an error message will already have
24780 else if (!type
|| type
== error_mark_node
)
24782 /* An anonymous aggregate has to be handled specially; such
24783 a declaration really declares a data member (with a
24784 particular type), as opposed to a nested class. */
24785 else if (ANON_AGGR_TYPE_P (type
))
24788 if (decl_specifiers
.storage_class
!= sc_none
)
24789 error_at (decl_spec_token_start
->location
,
24790 "a storage class on an anonymous aggregate "
24791 "in class scope is not allowed");
24793 /* Remove constructors and such from TYPE, now that we
24794 know it is an anonymous aggregate. */
24795 fixup_anonymous_aggr (type
);
24796 /* And make the corresponding data member. */
24797 decl
= build_decl (decl_spec_token_start
->location
,
24798 FIELD_DECL
, NULL_TREE
, type
);
24799 /* Add it to the class. */
24800 finish_member_declaration (decl
);
24803 cp_parser_check_access_in_redeclaration
24805 decl_spec_token_start
->location
);
24810 bool assume_semicolon
= false;
24812 /* Clear attributes from the decl_specifiers but keep them
24813 around as prefix attributes that apply them to the entity
24815 prefix_attributes
= decl_specifiers
.attributes
;
24816 decl_specifiers
.attributes
= NULL_TREE
;
24818 /* See if these declarations will be friends. */
24819 friend_p
= cp_parser_friend_p (&decl_specifiers
);
24821 /* Keep going until we hit the `;' at the end of the
24823 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
24825 tree attributes
= NULL_TREE
;
24826 tree first_attribute
;
24828 bool named_bitfld
= false;
24830 /* Peek at the next token. */
24831 token
= cp_lexer_peek_token (parser
->lexer
);
24833 /* The following code wants to know early if it is a bit-field
24834 or some other declaration. Attributes can appear before
24835 the `:' token. Skip over them without consuming any tokens
24836 to peek if they are followed by `:'. */
24837 if (cp_next_tokens_can_be_attribute_p (parser
)
24838 || (token
->type
== CPP_NAME
24839 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
24840 && (named_bitfld
= true)))
24843 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
24844 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
24847 /* Check for a bitfield declaration. */
24848 if (token
->type
== CPP_COLON
24849 || (token
->type
== CPP_NAME
24850 && token
== cp_lexer_peek_token (parser
->lexer
)
24851 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
24852 && (named_bitfld
= true)))
24856 tree late_attributes
= NULL_TREE
;
24857 location_t id_location
24858 = cp_lexer_peek_token (parser
->lexer
)->location
;
24861 identifier
= cp_parser_identifier (parser
);
24863 identifier
= NULL_TREE
;
24865 /* Look for attributes that apply to the bitfield. */
24866 attributes
= cp_parser_attributes_opt (parser
);
24868 /* Consume the `:' token. */
24869 cp_lexer_consume_token (parser
->lexer
);
24871 /* Get the width of the bitfield. */
24872 width
= cp_parser_constant_expression (parser
, false, NULL
,
24873 cxx_dialect
>= cxx11
);
24875 /* In C++2A and as extension for C++11 and above we allow
24876 default member initializers for bit-fields. */
24877 initializer
= NULL_TREE
;
24878 if (cxx_dialect
>= cxx11
24879 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
24880 || cp_lexer_next_token_is (parser
->lexer
,
24884 = cp_lexer_peek_token (parser
->lexer
)->location
;
24885 if (cxx_dialect
< cxx2a
24886 && identifier
!= NULL_TREE
)
24888 "default member initializers for bit-fields "
24889 "only available with %<-std=c++2a%> or "
24890 "%<-std=gnu++2a%>");
24892 initializer
= cp_parser_save_nsdmi (parser
);
24893 if (identifier
== NULL_TREE
)
24895 error_at (loc
, "default member initializer for "
24896 "unnamed bit-field");
24897 initializer
= NULL_TREE
;
24902 /* Look for attributes that apply to the bitfield after
24903 the `:' token and width. This is where GCC used to
24904 parse attributes in the past, pedwarn if there is
24905 a std attribute. */
24906 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24907 pedwarn (input_location
, OPT_Wpedantic
,
24908 "ISO C++ allows bit-field attributes only "
24909 "before the %<:%> token");
24911 late_attributes
= cp_parser_attributes_opt (parser
);
24914 attributes
= attr_chainon (attributes
, late_attributes
);
24916 /* Remember which attributes are prefix attributes and
24918 first_attribute
= attributes
;
24919 /* Combine the attributes. */
24920 attributes
= attr_chainon (prefix_attributes
, attributes
);
24922 /* Create the bitfield declaration. */
24923 decl
= grokbitfield (identifier
24924 ? make_id_declarator (NULL_TREE
,
24930 width
, initializer
,
24935 cp_declarator
*declarator
;
24936 tree asm_specification
;
24937 int ctor_dtor_or_conv_p
;
24938 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
24939 cp_parser_flags flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
24941 && !decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
24942 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
24944 /* Parse the declarator. */
24946 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
24948 &ctor_dtor_or_conv_p
,
24949 /*parenthesized_p=*/NULL
,
24951 friend_p
, static_p
);
24953 /* If something went wrong parsing the declarator, make sure
24954 that we at least consume some tokens. */
24955 if (declarator
== cp_error_declarator
)
24957 /* Skip to the end of the statement. */
24958 cp_parser_skip_to_end_of_statement (parser
);
24959 /* If the next token is not a semicolon, that is
24960 probably because we just skipped over the body of
24961 a function. So, we consume a semicolon if
24962 present, but do not issue an error message if it
24964 if (cp_lexer_next_token_is (parser
->lexer
,
24966 cp_lexer_consume_token (parser
->lexer
);
24970 if (declares_class_or_enum
& 2)
24971 cp_parser_check_for_definition_in_return_type
24972 (declarator
, decl_specifiers
.type
,
24973 decl_specifiers
.locations
[ds_type_spec
]);
24975 /* Look for an asm-specification. */
24976 asm_specification
= cp_parser_asm_specification_opt (parser
);
24977 /* Look for attributes that apply to the declaration. */
24978 attributes
= cp_parser_attributes_opt (parser
);
24979 /* Remember which attributes are prefix attributes and
24981 first_attribute
= attributes
;
24982 /* Combine the attributes. */
24983 attributes
= attr_chainon (prefix_attributes
, attributes
);
24985 /* If it's an `=', then we have a constant-initializer or a
24986 pure-specifier. It is not correct to parse the
24987 initializer before registering the member declaration
24988 since the member declaration should be in scope while
24989 its initializer is processed. However, the rest of the
24990 front end does not yet provide an interface that allows
24991 us to handle this correctly. */
24992 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24996 A pure-specifier shall be used only in the declaration of
24997 a virtual function.
24999 A member-declarator can contain a constant-initializer
25000 only if it declares a static member of integral or
25003 Therefore, if the DECLARATOR is for a function, we look
25004 for a pure-specifier; otherwise, we look for a
25005 constant-initializer. When we call `grokfield', it will
25006 perform more stringent semantics checks. */
25007 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
25008 if (function_declarator_p (declarator
)
25009 || (decl_specifiers
.type
25010 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
25011 && declarator
->kind
== cdk_id
25012 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
25013 == FUNCTION_TYPE
)))
25014 initializer
= cp_parser_pure_specifier (parser
);
25015 else if (decl_specifiers
.storage_class
!= sc_static
)
25016 initializer
= cp_parser_save_nsdmi (parser
);
25017 else if (cxx_dialect
>= cxx11
)
25020 /* Don't require a constant rvalue in C++11, since we
25021 might want a reference constant. We'll enforce
25022 constancy later. */
25023 cp_lexer_consume_token (parser
->lexer
);
25024 /* Parse the initializer. */
25025 initializer
= cp_parser_initializer_clause (parser
,
25029 /* Parse the initializer. */
25030 initializer
= cp_parser_constant_initializer (parser
);
25032 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
25033 && !function_declarator_p (declarator
))
25036 if (decl_specifiers
.storage_class
!= sc_static
)
25037 initializer
= cp_parser_save_nsdmi (parser
);
25039 initializer
= cp_parser_initializer (parser
, &x
, &x
);
25041 /* Detect invalid bit-field cases such as
25047 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
25048 /* If there were no type specifiers, it was a
25050 && decl_specifiers
.any_type_specifiers_p
)
25052 /* This is called for a decent diagnostic only. */
25053 tree d
= grokdeclarator (declarator
, &decl_specifiers
,
25054 BITFIELD
, /*initialized=*/false,
25056 if (!error_operand_p (d
))
25057 error_at (DECL_SOURCE_LOCATION (d
),
25058 "bit-field %qD has non-integral type %qT",
25060 cp_parser_skip_to_end_of_statement (parser
);
25061 /* Avoid "extra ;" pedwarns. */
25062 if (cp_lexer_next_token_is (parser
->lexer
,
25064 cp_lexer_consume_token (parser
->lexer
);
25067 /* Otherwise, there is no initializer. */
25069 initializer
= NULL_TREE
;
25071 /* See if we are probably looking at a function
25072 definition. We are certainly not looking at a
25073 member-declarator. Calling `grokfield' has
25074 side-effects, so we must not do it unless we are sure
25075 that we are looking at a member-declarator. */
25076 if (cp_parser_token_starts_function_definition_p
25077 (cp_lexer_peek_token (parser
->lexer
)))
25079 /* The grammar does not allow a pure-specifier to be
25080 used when a member function is defined. (It is
25081 possible that this fact is an oversight in the
25082 standard, since a pure function may be defined
25083 outside of the class-specifier. */
25084 if (initializer
&& initializer_token_start
)
25085 error_at (initializer_token_start
->location
,
25086 "pure-specifier on function-definition");
25087 decl
= cp_parser_save_member_function_body (parser
,
25091 if (parser
->fully_implicit_function_template_p
)
25092 decl
= finish_fully_implicit_template (parser
, decl
);
25093 /* If the member was not a friend, declare it here. */
25095 finish_member_declaration (decl
);
25096 /* Peek at the next token. */
25097 token
= cp_lexer_peek_token (parser
->lexer
);
25098 /* If the next token is a semicolon, consume it. */
25099 if (token
->type
== CPP_SEMICOLON
)
25101 location_t semicolon_loc
25102 = cp_lexer_consume_token (parser
->lexer
)->location
;
25103 gcc_rich_location
richloc (semicolon_loc
);
25104 richloc
.add_fixit_remove ();
25105 warning_at (&richloc
, OPT_Wextra_semi
,
25106 "extra %<;%> after in-class "
25107 "function definition");
25112 if (declarator
->kind
== cdk_function
)
25113 declarator
->id_loc
= token
->location
;
25114 /* Create the declaration. */
25115 decl
= grokfield (declarator
, &decl_specifiers
,
25116 initializer
, /*init_const_expr_p=*/true,
25117 asm_specification
, attributes
);
25118 if (parser
->fully_implicit_function_template_p
)
25121 finish_fully_implicit_template (parser
, 0);
25123 decl
= finish_fully_implicit_template (parser
, decl
);
25127 cp_finalize_omp_declare_simd (parser
, decl
);
25128 cp_finalize_oacc_routine (parser
, decl
, false);
25130 /* Reset PREFIX_ATTRIBUTES. */
25131 if (attributes
!= error_mark_node
)
25133 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
25134 attributes
= TREE_CHAIN (attributes
);
25136 TREE_CHAIN (attributes
) = NULL_TREE
;
25139 /* If there is any qualification still in effect, clear it
25140 now; we will be starting fresh with the next declarator. */
25141 parser
->scope
= NULL_TREE
;
25142 parser
->qualifying_scope
= NULL_TREE
;
25143 parser
->object_scope
= NULL_TREE
;
25144 /* If it's a `,', then there are more declarators. */
25145 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
25147 cp_lexer_consume_token (parser
->lexer
);
25148 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25150 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
25151 gcc_rich_location
richloc (token
->location
);
25152 richloc
.add_fixit_remove ();
25153 error_at (&richloc
, "stray %<,%> at end of "
25154 "member declaration");
25157 /* If the next token isn't a `;', then we have a parse error. */
25158 else if (cp_lexer_next_token_is_not (parser
->lexer
,
25161 /* The next token might be a ways away from where the
25162 actual semicolon is missing. Find the previous token
25163 and use that for our error position. */
25164 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
25165 gcc_rich_location
richloc (token
->location
);
25166 richloc
.add_fixit_insert_after (";");
25167 error_at (&richloc
, "expected %<;%> at end of "
25168 "member declaration");
25170 /* Assume that the user meant to provide a semicolon. If
25171 we were to cp_parser_skip_to_end_of_statement, we might
25172 skip to a semicolon inside a member function definition
25173 and issue nonsensical error messages. */
25174 assume_semicolon
= true;
25179 /* Add DECL to the list of members. */
25181 /* Explicitly include, eg, NSDMIs, for better error
25182 recovery (c++/58650). */
25183 || !DECL_DECLARES_FUNCTION_P (decl
))
25184 finish_member_declaration (decl
);
25186 if (TREE_CODE (decl
) == FUNCTION_DECL
)
25187 cp_parser_save_default_args (parser
, decl
);
25188 else if (TREE_CODE (decl
) == FIELD_DECL
25189 && DECL_INITIAL (decl
))
25190 /* Add DECL to the queue of NSDMI to be parsed later. */
25191 vec_safe_push (unparsed_nsdmis
, decl
);
25194 if (assume_semicolon
)
25199 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
25201 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
25204 /* Parse a pure-specifier.
25209 Returns INTEGER_ZERO_NODE if a pure specifier is found.
25210 Otherwise, ERROR_MARK_NODE is returned. */
25213 cp_parser_pure_specifier (cp_parser
* parser
)
25217 /* Look for the `=' token. */
25218 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25219 return error_mark_node
;
25220 /* Look for the `0' token. */
25221 token
= cp_lexer_peek_token (parser
->lexer
);
25223 if (token
->type
== CPP_EOF
25224 || token
->type
== CPP_PRAGMA_EOL
)
25225 return error_mark_node
;
25227 cp_lexer_consume_token (parser
->lexer
);
25229 /* Accept = default or = delete in c++0x mode. */
25230 if (token
->keyword
== RID_DEFAULT
25231 || token
->keyword
== RID_DELETE
)
25233 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
25234 return token
->u
.value
;
25237 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
25238 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
25240 cp_parser_error (parser
,
25241 "invalid pure specifier (only %<= 0%> is allowed)");
25242 cp_parser_skip_to_end_of_statement (parser
);
25243 return error_mark_node
;
25245 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
25247 error_at (token
->location
, "templates may not be %<virtual%>");
25248 return error_mark_node
;
25251 return integer_zero_node
;
25254 /* Parse a constant-initializer.
25256 constant-initializer:
25257 = constant-expression
25259 Returns a representation of the constant-expression. */
25262 cp_parser_constant_initializer (cp_parser
* parser
)
25264 /* Look for the `=' token. */
25265 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
25266 return error_mark_node
;
25268 /* It is invalid to write:
25270 struct S { static const int i = { 7 }; };
25273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25275 cp_parser_error (parser
,
25276 "a brace-enclosed initializer is not allowed here");
25277 /* Consume the opening brace. */
25278 matching_braces braces
;
25279 braces
.consume_open (parser
);
25280 /* Skip the initializer. */
25281 cp_parser_skip_to_closing_brace (parser
);
25282 /* Look for the trailing `}'. */
25283 braces
.require_close (parser
);
25285 return error_mark_node
;
25288 return cp_parser_constant_expression (parser
);
25291 /* Derived classes [gram.class.derived] */
25293 /* Parse a base-clause.
25296 : base-specifier-list
25298 base-specifier-list:
25299 base-specifier ... [opt]
25300 base-specifier-list , base-specifier ... [opt]
25302 Returns a TREE_LIST representing the base-classes, in the order in
25303 which they were declared. The representation of each node is as
25304 described by cp_parser_base_specifier.
25306 In the case that no bases are specified, this function will return
25307 NULL_TREE, not ERROR_MARK_NODE. */
25310 cp_parser_base_clause (cp_parser
* parser
)
25312 tree bases
= NULL_TREE
;
25314 /* Look for the `:' that begins the list. */
25315 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25317 /* Scan the base-specifier-list. */
25322 bool pack_expansion_p
= false;
25324 /* Look for the base-specifier. */
25325 base
= cp_parser_base_specifier (parser
);
25326 /* Look for the (optional) ellipsis. */
25327 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25329 /* Consume the `...'. */
25330 cp_lexer_consume_token (parser
->lexer
);
25332 pack_expansion_p
= true;
25335 /* Add BASE to the front of the list. */
25336 if (base
&& base
!= error_mark_node
)
25338 if (pack_expansion_p
)
25339 /* Make this a pack expansion type. */
25340 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
25342 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
25344 TREE_CHAIN (base
) = bases
;
25348 /* Peek at the next token. */
25349 token
= cp_lexer_peek_token (parser
->lexer
);
25350 /* If it's not a comma, then the list is complete. */
25351 if (token
->type
!= CPP_COMMA
)
25353 /* Consume the `,'. */
25354 cp_lexer_consume_token (parser
->lexer
);
25357 /* PARSER->SCOPE may still be non-NULL at this point, if the last
25358 base class had a qualified name. However, the next name that
25359 appears is certainly not qualified. */
25360 parser
->scope
= NULL_TREE
;
25361 parser
->qualifying_scope
= NULL_TREE
;
25362 parser
->object_scope
= NULL_TREE
;
25364 return nreverse (bases
);
25367 /* Parse a base-specifier.
25370 :: [opt] nested-name-specifier [opt] class-name
25371 virtual access-specifier [opt] :: [opt] nested-name-specifier
25373 access-specifier virtual [opt] :: [opt] nested-name-specifier
25376 Returns a TREE_LIST. The TREE_PURPOSE will be one of
25377 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
25378 indicate the specifiers provided. The TREE_VALUE will be a TYPE
25379 (or the ERROR_MARK_NODE) indicating the type that was specified. */
25382 cp_parser_base_specifier (cp_parser
* parser
)
25386 bool virtual_p
= false;
25387 bool duplicate_virtual_error_issued_p
= false;
25388 bool duplicate_access_error_issued_p
= false;
25389 bool class_scope_p
, template_p
;
25390 tree access
= access_default_node
;
25393 /* Process the optional `virtual' and `access-specifier'. */
25396 /* Peek at the next token. */
25397 token
= cp_lexer_peek_token (parser
->lexer
);
25398 /* Process `virtual'. */
25399 switch (token
->keyword
)
25402 /* If `virtual' appears more than once, issue an error. */
25403 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
25405 cp_parser_error (parser
,
25406 "%<virtual%> specified more than once in base-specifier");
25407 duplicate_virtual_error_issued_p
= true;
25412 /* Consume the `virtual' token. */
25413 cp_lexer_consume_token (parser
->lexer
);
25418 case RID_PROTECTED
:
25420 /* If more than one access specifier appears, issue an
25422 if (access
!= access_default_node
25423 && !duplicate_access_error_issued_p
)
25425 cp_parser_error (parser
,
25426 "more than one access specifier in base-specifier");
25427 duplicate_access_error_issued_p
= true;
25430 access
= ridpointers
[(int) token
->keyword
];
25432 /* Consume the access-specifier. */
25433 cp_lexer_consume_token (parser
->lexer
);
25442 /* It is not uncommon to see programs mechanically, erroneously, use
25443 the 'typename' keyword to denote (dependent) qualified types
25444 as base classes. */
25445 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
25447 token
= cp_lexer_peek_token (parser
->lexer
);
25448 if (!processing_template_decl
)
25449 error_at (token
->location
,
25450 "keyword %<typename%> not allowed outside of templates");
25452 error_at (token
->location
,
25453 "keyword %<typename%> not allowed in this context "
25454 "(the base class is implicitly a type)");
25455 cp_lexer_consume_token (parser
->lexer
);
25458 /* Look for the optional `::' operator. */
25459 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
25460 /* Look for the nested-name-specifier. The simplest way to
25465 The keyword `typename' is not permitted in a base-specifier or
25466 mem-initializer; in these contexts a qualified name that
25467 depends on a template-parameter is implicitly assumed to be a
25470 is to pretend that we have seen the `typename' keyword at this
25472 cp_parser_nested_name_specifier_opt (parser
,
25473 /*typename_keyword_p=*/true,
25474 /*check_dependency_p=*/true,
25476 /*is_declaration=*/true);
25477 /* If the base class is given by a qualified name, assume that names
25478 we see are type names or templates, as appropriate. */
25479 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
25480 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
25483 && cp_lexer_next_token_is_decltype (parser
->lexer
))
25484 /* DR 950 allows decltype as a base-specifier. */
25485 type
= cp_parser_decltype (parser
);
25488 /* Otherwise, look for the class-name. */
25489 type
= cp_parser_class_name (parser
,
25493 /*check_dependency_p=*/true,
25494 /*class_head_p=*/false,
25495 /*is_declaration=*/true);
25496 type
= TREE_TYPE (type
);
25499 if (type
== error_mark_node
)
25500 return error_mark_node
;
25502 return finish_base_specifier (type
, access
, virtual_p
);
25505 /* Exception handling [gram.exception] */
25507 /* Save the tokens that make up the noexcept-specifier for a member-function.
25508 Returns a DEFERRED_PARSE. */
25511 cp_parser_save_noexcept (cp_parser
*parser
)
25513 cp_token
*first
= parser
->lexer
->next_token
;
25514 /* We want everything up to, including, the final ')'. */
25515 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0);
25516 cp_token
*last
= parser
->lexer
->next_token
;
25518 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
25519 to carry the information we will need. */
25520 tree expr
= make_node (DEFERRED_PARSE
);
25521 /* Save away the noexcept-specifier; we will process it when the
25522 class is complete. */
25523 DEFPARSE_TOKENS (expr
) = cp_token_cache_new (first
, last
);
25524 expr
= build_tree_list (expr
, NULL_TREE
);
25528 /* Used for late processing of noexcept-specifiers of member-functions.
25529 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
25530 we saved for later; parse it now. */
25533 cp_parser_late_noexcept_specifier (cp_parser
*parser
, tree default_arg
)
25535 /* Make sure we've gotten something that hasn't been parsed yet. */
25536 gcc_assert (TREE_CODE (default_arg
) == DEFERRED_PARSE
);
25538 push_unparsed_function_queues (parser
);
25540 /* Push the saved tokens for the noexcept-specifier onto the parser's
25542 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_arg
);
25543 cp_parser_push_lexer_for_tokens (parser
, tokens
);
25545 /* Parse the cached noexcept-specifier. */
25547 = cp_parser_noexcept_specification_opt (parser
,
25548 CP_PARSER_FLAGS_NONE
,
25549 /*require_constexpr=*/true,
25550 /*consumed_expr=*/NULL
,
25551 /*return_cond=*/false);
25553 /* Revert to the main lexer. */
25554 cp_parser_pop_lexer (parser
);
25556 /* Restore the queue. */
25557 pop_unparsed_function_queues (parser
);
25559 /* And we're done. */
25563 /* Perform late checking of overriding function with respect to their
25564 noexcept-specifiers. TYPE is the class and FNDECL is the function
25565 that potentially overrides some virtual function with the same
25569 noexcept_override_late_checks (tree type
, tree fndecl
)
25571 tree binfo
= TYPE_BINFO (type
);
25574 if (DECL_STATIC_FUNCTION_P (fndecl
))
25577 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
25579 tree basetype
= BINFO_TYPE (base_binfo
);
25581 if (!TYPE_POLYMORPHIC_P (basetype
))
25584 tree fn
= look_for_overrides_here (basetype
, fndecl
);
25586 maybe_check_overriding_exception_spec (fndecl
, fn
);
25590 /* Parse an (optional) noexcept-specification.
25592 noexcept-specification:
25593 noexcept ( constant-expression ) [opt]
25595 If no noexcept-specification is present, returns NULL_TREE.
25596 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
25597 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
25598 there are no parentheses. CONSUMED_EXPR will be set accordingly.
25599 Otherwise, returns a noexcept specification unless RETURN_COND is true,
25600 in which case a boolean condition is returned instead. The parser flags
25601 FLAGS is used to control parsing. */
25604 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
25605 cp_parser_flags flags
,
25606 bool require_constexpr
,
25607 bool* consumed_expr
,
25611 const char *saved_message
;
25613 /* Peek at the next token. */
25614 token
= cp_lexer_peek_token (parser
->lexer
);
25616 /* Is it a noexcept-specification? */
25617 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
25621 /* [class.mem]/6 says that a noexcept-specifer (within the
25622 member-specification of the class) is a complete-class context of
25623 a class. So, if the noexcept-specifier has the optional expression,
25624 just save the tokens, and reparse this after we're done with the
25626 const bool literal_p
25627 = ((cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
25628 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
25629 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
));
25631 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
)
25632 /* No need to delay parsing for a number literal or true/false. */
25634 && at_class_scope_p ()
25635 /* We don't delay parsing for friend member functions,
25636 alias-declarations, and typedefs, even though the standard seems
25638 && (flags
& CP_PARSER_FLAGS_DELAY_NOEXCEPT
)
25639 && TYPE_BEING_DEFINED (current_class_type
)
25640 && !LAMBDA_TYPE_P (current_class_type
))
25641 return cp_parser_save_noexcept (parser
);
25643 cp_lexer_consume_token (parser
->lexer
);
25645 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
25647 matching_parens parens
;
25648 parens
.consume_open (parser
);
25650 tree save_ccp
= current_class_ptr
;
25651 tree save_ccr
= current_class_ref
;
25653 if (current_class_type
)
25654 inject_this_parameter (current_class_type
, TYPE_UNQUALIFIED
);
25656 if (require_constexpr
)
25658 /* Types may not be defined in an exception-specification. */
25659 saved_message
= parser
->type_definition_forbidden_message
;
25660 parser
->type_definition_forbidden_message
25661 = G_("types may not be defined in an exception-specification");
25663 bool non_constant_p
;
25665 = cp_parser_constant_expression (parser
,
25666 /*allow_non_constant=*/true,
25669 && !require_potential_rvalue_constant_expression (expr
))
25672 return_cond
= true;
25675 /* Restore the saved message. */
25676 parser
->type_definition_forbidden_message
= saved_message
;
25680 expr
= cp_parser_expression (parser
);
25681 *consumed_expr
= true;
25684 parens
.require_close (parser
);
25686 current_class_ptr
= save_ccp
;
25687 current_class_ref
= save_ccr
;
25691 expr
= boolean_true_node
;
25692 if (!require_constexpr
)
25693 *consumed_expr
= false;
25696 /* We cannot build a noexcept-spec right away because this will check
25697 that expr is a constexpr. */
25699 return build_noexcept_spec (expr
, tf_warning_or_error
);
25707 /* Parse an (optional) exception-specification.
25709 exception-specification:
25710 throw ( type-id-list [opt] )
25712 Returns a TREE_LIST representing the exception-specification. The
25713 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
25714 control parsing. */
25717 cp_parser_exception_specification_opt (cp_parser
* parser
, cp_parser_flags flags
)
25721 const char *saved_message
;
25723 /* Peek at the next token. */
25724 token
= cp_lexer_peek_token (parser
->lexer
);
25726 /* Is it a noexcept-specification? */
25728 = cp_parser_noexcept_specification_opt (parser
, flags
,
25729 /*require_constexpr=*/true,
25730 /*consumed_expr=*/NULL
,
25731 /*return_cond=*/false);
25732 if (type_id_list
!= NULL_TREE
)
25733 return type_id_list
;
25735 /* If it's not `throw', then there's no exception-specification. */
25736 if (!cp_parser_is_keyword (token
, RID_THROW
))
25739 location_t loc
= token
->location
;
25741 /* Consume the `throw'. */
25742 cp_lexer_consume_token (parser
->lexer
);
25744 /* Look for the `('. */
25745 matching_parens parens
;
25746 parens
.require_open (parser
);
25748 /* Peek at the next token. */
25749 token
= cp_lexer_peek_token (parser
->lexer
);
25750 /* If it's not a `)', then there is a type-id-list. */
25751 if (token
->type
!= CPP_CLOSE_PAREN
)
25753 /* Types may not be defined in an exception-specification. */
25754 saved_message
= parser
->type_definition_forbidden_message
;
25755 parser
->type_definition_forbidden_message
25756 = G_("types may not be defined in an exception-specification");
25757 /* Parse the type-id-list. */
25758 type_id_list
= cp_parser_type_id_list (parser
);
25759 /* Restore the saved message. */
25760 parser
->type_definition_forbidden_message
= saved_message
;
25762 if (cxx_dialect
>= cxx17
)
25764 error_at (loc
, "ISO C++17 does not allow dynamic exception "
25766 type_id_list
= NULL_TREE
;
25768 else if (cxx_dialect
>= cxx11
)
25769 warning_at (loc
, OPT_Wdeprecated
,
25770 "dynamic exception specifications are deprecated in "
25773 /* In C++17, throw() is equivalent to noexcept (true). throw()
25774 is deprecated in C++11 and above as well, but is still widely used,
25775 so don't warn about it yet. */
25776 else if (cxx_dialect
>= cxx17
)
25777 type_id_list
= noexcept_true_spec
;
25779 type_id_list
= empty_except_spec
;
25781 /* Look for the `)'. */
25782 parens
.require_close (parser
);
25784 return type_id_list
;
25787 /* Parse an (optional) type-id-list.
25791 type-id-list , type-id ... [opt]
25793 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
25794 in the order that the types were presented. */
25797 cp_parser_type_id_list (cp_parser
* parser
)
25799 tree types
= NULL_TREE
;
25806 token
= cp_lexer_peek_token (parser
->lexer
);
25808 /* Get the next type-id. */
25809 type
= cp_parser_type_id (parser
);
25810 /* Check for invalid 'auto'. */
25811 if (flag_concepts
&& type_uses_auto (type
))
25813 error_at (token
->location
,
25814 "invalid use of %<auto%> in exception-specification");
25815 type
= error_mark_node
;
25817 /* Parse the optional ellipsis. */
25818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25820 /* Consume the `...'. */
25821 cp_lexer_consume_token (parser
->lexer
);
25823 /* Turn the type into a pack expansion expression. */
25824 type
= make_pack_expansion (type
);
25826 /* Add it to the list. */
25827 types
= add_exception_specifier (types
, type
, /*complain=*/1);
25828 /* Peek at the next token. */
25829 token
= cp_lexer_peek_token (parser
->lexer
);
25830 /* If it is not a `,', we are done. */
25831 if (token
->type
!= CPP_COMMA
)
25833 /* Consume the `,'. */
25834 cp_lexer_consume_token (parser
->lexer
);
25837 return nreverse (types
);
25840 /* Parse a try-block.
25843 try compound-statement handler-seq */
25846 cp_parser_try_block (cp_parser
* parser
)
25850 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
25851 if (parser
->in_function_body
25852 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
25853 && cxx_dialect
< cxx2a
)
25854 pedwarn (input_location
, 0,
25855 "%<try%> in %<constexpr%> function only "
25856 "available with %<-std=c++2a%> or %<-std=gnu++2a%>");
25858 try_block
= begin_try_block ();
25859 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
25860 finish_try_block (try_block
);
25861 cp_parser_handler_seq (parser
);
25862 finish_handler_sequence (try_block
);
25867 /* Parse a function-try-block.
25869 function-try-block:
25870 try ctor-initializer [opt] function-body handler-seq */
25873 cp_parser_function_try_block (cp_parser
* parser
)
25875 tree compound_stmt
;
25878 /* Look for the `try' keyword. */
25879 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
25881 /* Let the rest of the front end know where we are. */
25882 try_block
= begin_function_try_block (&compound_stmt
);
25883 /* Parse the function-body. */
25884 cp_parser_ctor_initializer_opt_and_function_body
25885 (parser
, /*in_function_try_block=*/true);
25886 /* We're done with the `try' part. */
25887 finish_function_try_block (try_block
);
25888 /* Parse the handlers. */
25889 cp_parser_handler_seq (parser
);
25890 /* We're done with the handlers. */
25891 finish_function_handler_sequence (try_block
, compound_stmt
);
25894 /* Parse a handler-seq.
25897 handler handler-seq [opt] */
25900 cp_parser_handler_seq (cp_parser
* parser
)
25906 /* Parse the handler. */
25907 cp_parser_handler (parser
);
25908 /* Peek at the next token. */
25909 token
= cp_lexer_peek_token (parser
->lexer
);
25910 /* If it's not `catch' then there are no more handlers. */
25911 if (!cp_parser_is_keyword (token
, RID_CATCH
))
25916 /* Parse a handler.
25919 catch ( exception-declaration ) compound-statement */
25922 cp_parser_handler (cp_parser
* parser
)
25927 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
25928 handler
= begin_handler ();
25929 matching_parens parens
;
25930 parens
.require_open (parser
);
25931 declaration
= cp_parser_exception_declaration (parser
);
25932 finish_handler_parms (declaration
, handler
);
25933 parens
.require_close (parser
);
25934 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
25935 finish_handler (handler
);
25938 /* Parse an exception-declaration.
25940 exception-declaration:
25941 type-specifier-seq declarator
25942 type-specifier-seq abstract-declarator
25946 Returns a VAR_DECL for the declaration, or NULL_TREE if the
25947 ellipsis variant is used. */
25950 cp_parser_exception_declaration (cp_parser
* parser
)
25952 cp_decl_specifier_seq type_specifiers
;
25953 cp_declarator
*declarator
;
25954 const char *saved_message
;
25956 /* If it's an ellipsis, it's easy to handle. */
25957 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
25959 /* Consume the `...' token. */
25960 cp_lexer_consume_token (parser
->lexer
);
25964 /* Types may not be defined in exception-declarations. */
25965 saved_message
= parser
->type_definition_forbidden_message
;
25966 parser
->type_definition_forbidden_message
25967 = G_("types may not be defined in exception-declarations");
25969 /* Parse the type-specifier-seq. */
25970 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
25971 /*is_declaration=*/true,
25972 /*is_trailing_return=*/false,
25974 /* If it's a `)', then there is no declarator. */
25975 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
25978 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
25979 CP_PARSER_FLAGS_NONE
,
25980 /*ctor_dtor_or_conv_p=*/NULL
,
25981 /*parenthesized_p=*/NULL
,
25982 /*member_p=*/false,
25983 /*friend_p=*/false,
25984 /*static_p=*/false);
25986 /* Restore the saved message. */
25987 parser
->type_definition_forbidden_message
= saved_message
;
25989 if (!type_specifiers
.any_specifiers_p
)
25990 return error_mark_node
;
25992 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
25995 /* Parse a throw-expression.
25998 throw assignment-expression [opt]
26000 Returns a THROW_EXPR representing the throw-expression. */
26003 cp_parser_throw_expression (cp_parser
* parser
)
26007 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
26009 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
26010 token
= cp_lexer_peek_token (parser
->lexer
);
26011 /* Figure out whether or not there is an assignment-expression
26012 following the "throw" keyword. */
26013 if (token
->type
== CPP_COMMA
26014 || token
->type
== CPP_SEMICOLON
26015 || token
->type
== CPP_CLOSE_PAREN
26016 || token
->type
== CPP_CLOSE_SQUARE
26017 || token
->type
== CPP_CLOSE_BRACE
26018 || token
->type
== CPP_COLON
)
26019 expression
= NULL_TREE
;
26021 expression
= cp_parser_assignment_expression (parser
);
26023 /* Construct a location e.g.:
26026 with caret == start at the start of the "throw" token, and
26027 the end at the end of the final token we consumed. */
26028 location_t combined_loc
= make_location (start_loc
, start_loc
,
26030 expression
= build_throw (expression
);
26031 protected_set_expr_location (expression
, combined_loc
);
26036 /* GNU Extensions */
26038 /* Parse an (optional) asm-specification.
26041 asm ( string-literal )
26043 If the asm-specification is present, returns a STRING_CST
26044 corresponding to the string-literal. Otherwise, returns
26048 cp_parser_asm_specification_opt (cp_parser
* parser
)
26051 tree asm_specification
;
26053 /* Peek at the next token. */
26054 token
= cp_lexer_peek_token (parser
->lexer
);
26055 /* If the next token isn't the `asm' keyword, then there's no
26056 asm-specification. */
26057 if (!cp_parser_is_keyword (token
, RID_ASM
))
26060 /* Consume the `asm' token. */
26061 cp_lexer_consume_token (parser
->lexer
);
26062 /* Look for the `('. */
26063 matching_parens parens
;
26064 parens
.require_open (parser
);
26066 /* Look for the string-literal. */
26067 asm_specification
= cp_parser_string_literal (parser
, false, false);
26069 /* Look for the `)'. */
26070 parens
.require_close (parser
);
26072 return asm_specification
;
26075 /* Parse an asm-operand-list.
26079 asm-operand-list , asm-operand
26082 string-literal ( expression )
26083 [ string-literal ] string-literal ( expression )
26085 Returns a TREE_LIST representing the operands. The TREE_VALUE of
26086 each node is the expression. The TREE_PURPOSE is itself a
26087 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
26088 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
26089 is a STRING_CST for the string literal before the parenthesis. Returns
26090 ERROR_MARK_NODE if any of the operands are invalid. */
26093 cp_parser_asm_operand_list (cp_parser
* parser
)
26095 tree asm_operands
= NULL_TREE
;
26096 bool invalid_operands
= false;
26100 tree string_literal
;
26104 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
26106 /* Consume the `[' token. */
26107 cp_lexer_consume_token (parser
->lexer
);
26108 /* Read the operand name. */
26109 name
= cp_parser_identifier (parser
);
26110 if (name
!= error_mark_node
)
26111 name
= build_string (IDENTIFIER_LENGTH (name
),
26112 IDENTIFIER_POINTER (name
));
26113 /* Look for the closing `]'. */
26114 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
26118 /* Look for the string-literal. */
26119 string_literal
= cp_parser_string_literal (parser
, false, false);
26121 /* Look for the `('. */
26122 matching_parens parens
;
26123 parens
.require_open (parser
);
26124 /* Parse the expression. */
26125 expression
= cp_parser_expression (parser
);
26126 /* Look for the `)'. */
26127 parens
.require_close (parser
);
26129 if (name
== error_mark_node
26130 || string_literal
== error_mark_node
26131 || expression
== error_mark_node
)
26132 invalid_operands
= true;
26134 /* Add this operand to the list. */
26135 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
26138 /* If the next token is not a `,', there are no more
26140 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26142 /* Consume the `,'. */
26143 cp_lexer_consume_token (parser
->lexer
);
26146 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
26149 /* Parse an asm-clobber-list.
26153 asm-clobber-list , string-literal
26155 Returns a TREE_LIST, indicating the clobbers in the order that they
26156 appeared. The TREE_VALUE of each node is a STRING_CST. */
26159 cp_parser_asm_clobber_list (cp_parser
* parser
)
26161 tree clobbers
= NULL_TREE
;
26165 tree string_literal
;
26167 /* Look for the string literal. */
26168 string_literal
= cp_parser_string_literal (parser
, false, false);
26169 /* Add it to the list. */
26170 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
26171 /* If the next token is not a `,', then the list is
26173 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26175 /* Consume the `,' token. */
26176 cp_lexer_consume_token (parser
->lexer
);
26182 /* Parse an asm-label-list.
26186 asm-label-list , identifier
26188 Returns a TREE_LIST, indicating the labels in the order that they
26189 appeared. The TREE_VALUE of each node is a label. */
26192 cp_parser_asm_label_list (cp_parser
* parser
)
26194 tree labels
= NULL_TREE
;
26198 tree identifier
, label
, name
;
26200 /* Look for the identifier. */
26201 identifier
= cp_parser_identifier (parser
);
26202 if (!error_operand_p (identifier
))
26204 label
= lookup_label (identifier
);
26205 if (TREE_CODE (label
) == LABEL_DECL
)
26207 TREE_USED (label
) = 1;
26208 check_goto (label
);
26209 name
= build_string (IDENTIFIER_LENGTH (identifier
),
26210 IDENTIFIER_POINTER (identifier
));
26211 labels
= tree_cons (name
, label
, labels
);
26214 /* If the next token is not a `,', then the list is
26216 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
26218 /* Consume the `,' token. */
26219 cp_lexer_consume_token (parser
->lexer
);
26222 return nreverse (labels
);
26225 /* Return TRUE iff the next tokens in the stream are possibly the
26226 beginning of a GNU extension attribute. */
26229 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
26231 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
26234 /* Return TRUE iff the next tokens in the stream are possibly the
26235 beginning of a standard C++-11 attribute specifier. */
26238 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
26240 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
26243 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26244 beginning of a standard C++-11 attribute specifier. */
26247 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
26249 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26251 return (cxx_dialect
>= cxx11
26252 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
26253 || (token
->type
== CPP_OPEN_SQUARE
26254 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
26255 && token
->type
== CPP_OPEN_SQUARE
)));
26258 /* Return TRUE iff the next Nth tokens in the stream are possibly the
26259 beginning of a GNU extension attribute. */
26262 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
26264 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26266 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
26269 /* Return true iff the next tokens can be the beginning of either a
26270 GNU attribute list, or a standard C++11 attribute sequence. */
26273 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
26275 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
26276 || cp_next_tokens_can_be_std_attribute_p (parser
));
26279 /* Return true iff the next Nth tokens can be the beginning of either
26280 a GNU attribute list, or a standard C++11 attribute sequence. */
26283 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
26285 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
26286 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
26289 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
26290 of GNU attributes, or return NULL. */
26293 cp_parser_attributes_opt (cp_parser
*parser
)
26295 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
26296 return cp_parser_gnu_attributes_opt (parser
);
26297 return cp_parser_std_attribute_spec_seq (parser
);
26300 /* Parse an (optional) series of attributes.
26303 attributes attribute
26306 __attribute__ (( attribute-list [opt] ))
26308 The return value is as for cp_parser_gnu_attribute_list. */
26311 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
26313 tree attributes
= NULL_TREE
;
26315 temp_override
<bool> cleanup
26316 (parser
->auto_is_implicit_function_template_parm_p
, false);
26321 tree attribute_list
;
26324 /* Peek at the next token. */
26325 token
= cp_lexer_peek_token (parser
->lexer
);
26326 /* If it's not `__attribute__', then we're done. */
26327 if (token
->keyword
!= RID_ATTRIBUTE
)
26330 /* Consume the `__attribute__' keyword. */
26331 cp_lexer_consume_token (parser
->lexer
);
26332 /* Look for the two `(' tokens. */
26333 matching_parens outer_parens
;
26334 if (!outer_parens
.require_open (parser
))
26336 matching_parens inner_parens
;
26337 if (!inner_parens
.require_open (parser
))
26340 /* Peek at the next token. */
26341 token
= cp_lexer_peek_token (parser
->lexer
);
26342 if (token
->type
!= CPP_CLOSE_PAREN
)
26343 /* Parse the attribute-list. */
26344 attribute_list
= cp_parser_gnu_attribute_list (parser
);
26346 /* If the next token is a `)', then there is no attribute
26348 attribute_list
= NULL
;
26350 /* Look for the two `)' tokens. */
26351 if (!inner_parens
.require_close (parser
))
26353 if (!outer_parens
.require_close (parser
))
26356 cp_parser_skip_to_end_of_statement (parser
);
26358 /* Add these new attributes to the list. */
26359 attributes
= attr_chainon (attributes
, attribute_list
);
26365 /* Parse a GNU attribute-list.
26369 attribute-list , attribute
26373 identifier ( identifier )
26374 identifier ( identifier , expression-list )
26375 identifier ( expression-list )
26377 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
26378 to an attribute. The TREE_PURPOSE of each node is the identifier
26379 indicating which attribute is in use. The TREE_VALUE represents
26380 the arguments, if any. */
26383 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
26385 tree attribute_list
= NULL_TREE
;
26386 bool save_translate_strings_p
= parser
->translate_strings_p
;
26388 /* Don't create wrapper nodes within attributes: the
26389 handlers don't know how to handle them. */
26390 auto_suppress_location_wrappers sentinel
;
26392 parser
->translate_strings_p
= false;
26399 /* Look for the identifier. We also allow keywords here; for
26400 example `__attribute__ ((const))' is legal. */
26401 token
= cp_lexer_peek_token (parser
->lexer
);
26402 if (token
->type
== CPP_NAME
26403 || token
->type
== CPP_KEYWORD
)
26405 tree arguments
= NULL_TREE
;
26407 /* Consume the token, but save it since we need it for the
26408 SIMD enabled function parsing. */
26409 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
26411 /* Save away the identifier that indicates which attribute
26413 identifier
= (token
->type
== CPP_KEYWORD
)
26414 /* For keywords, use the canonical spelling, not the
26415 parsed identifier. */
26416 ? ridpointers
[(int) token
->keyword
]
26417 : id_token
->u
.value
;
26419 identifier
= canonicalize_attr_name (identifier
);
26420 attribute
= build_tree_list (identifier
, NULL_TREE
);
26422 /* Peek at the next token. */
26423 token
= cp_lexer_peek_token (parser
->lexer
);
26424 /* If it's an `(', then parse the attribute arguments. */
26425 if (token
->type
== CPP_OPEN_PAREN
)
26427 vec
<tree
, va_gc
> *vec
;
26428 int attr_flag
= (attribute_takes_identifier_p (identifier
)
26429 ? id_attr
: normal_attr
);
26430 vec
= cp_parser_parenthesized_expression_list
26431 (parser
, attr_flag
, /*cast_p=*/false,
26432 /*allow_expansion_p=*/false,
26433 /*non_constant_p=*/NULL
);
26435 arguments
= error_mark_node
;
26438 arguments
= build_tree_list_vec (vec
);
26439 release_tree_vector (vec
);
26441 /* Save the arguments away. */
26442 TREE_VALUE (attribute
) = arguments
;
26445 if (arguments
!= error_mark_node
)
26447 /* Add this attribute to the list. */
26448 TREE_CHAIN (attribute
) = attribute_list
;
26449 attribute_list
= attribute
;
26452 token
= cp_lexer_peek_token (parser
->lexer
);
26454 /* Unless EXACTLY_ONE is set look for more attributes.
26455 If the next token isn't a `,', we're done. */
26456 if (exactly_one
|| token
->type
!= CPP_COMMA
)
26459 /* Consume the comma and keep going. */
26460 cp_lexer_consume_token (parser
->lexer
);
26462 parser
->translate_strings_p
= save_translate_strings_p
;
26464 /* We built up the list in reverse order. */
26465 return nreverse (attribute_list
);
26468 /* Parse a standard C++11 attribute.
26470 The returned representation is a TREE_LIST which TREE_PURPOSE is
26471 the scoped name of the attribute, and the TREE_VALUE is its
26474 Note that the scoped name of the attribute is itself a TREE_LIST
26475 which TREE_PURPOSE is the namespace of the attribute, and
26476 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
26477 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
26478 and which TREE_PURPOSE is directly the attribute name.
26480 Clients of the attribute code should use get_attribute_namespace
26481 and get_attribute_name to get the actual namespace and name of
26482 attributes, regardless of their being GNU or C++11 attributes.
26485 attribute-token attribute-argument-clause [opt]
26489 attribute-scoped-token
26491 attribute-scoped-token:
26492 attribute-namespace :: identifier
26494 attribute-namespace:
26497 attribute-argument-clause:
26498 ( balanced-token-seq )
26500 balanced-token-seq:
26501 balanced-token [opt]
26502 balanced-token-seq balanced-token
26505 ( balanced-token-seq )
26506 [ balanced-token-seq ]
26507 { balanced-token-seq }. */
26510 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
26512 tree attribute
, attr_id
= NULL_TREE
, arguments
;
26515 temp_override
<bool> cleanup
26516 (parser
->auto_is_implicit_function_template_parm_p
, false);
26518 /* First, parse name of the attribute, a.k.a attribute-token. */
26520 token
= cp_lexer_peek_token (parser
->lexer
);
26521 if (token
->type
== CPP_NAME
)
26522 attr_id
= token
->u
.value
;
26523 else if (token
->type
== CPP_KEYWORD
)
26524 attr_id
= ridpointers
[(int) token
->keyword
];
26525 else if (token
->flags
& NAMED_OP
)
26526 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26528 if (attr_id
== NULL_TREE
)
26531 cp_lexer_consume_token (parser
->lexer
);
26533 token
= cp_lexer_peek_token (parser
->lexer
);
26534 if (token
->type
== CPP_SCOPE
)
26536 /* We are seeing a scoped attribute token. */
26538 cp_lexer_consume_token (parser
->lexer
);
26540 error_at (token
->location
, "attribute using prefix used together "
26541 "with scoped attribute token");
26544 token
= cp_lexer_consume_token (parser
->lexer
);
26545 if (token
->type
== CPP_NAME
)
26546 attr_id
= token
->u
.value
;
26547 else if (token
->type
== CPP_KEYWORD
)
26548 attr_id
= ridpointers
[(int) token
->keyword
];
26549 else if (token
->flags
& NAMED_OP
)
26550 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
26553 error_at (token
->location
,
26554 "expected an identifier for the attribute name");
26555 return error_mark_node
;
26558 attr_ns
= canonicalize_attr_name (attr_ns
);
26559 attr_id
= canonicalize_attr_name (attr_id
);
26560 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26562 token
= cp_lexer_peek_token (parser
->lexer
);
26566 attr_ns
= canonicalize_attr_name (attr_ns
);
26567 attr_id
= canonicalize_attr_name (attr_id
);
26568 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
26573 attr_id
= canonicalize_attr_name (attr_id
);
26574 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
26576 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
26577 but no longer: we have to be able to tell [[noreturn]] and
26578 __attribute__((noreturn)) apart. */
26579 /* C++14 deprecated attribute is equivalent to GNU's. */
26580 if (is_attribute_p ("deprecated", attr_id
))
26581 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26582 /* C++17 fallthrough attribute is equivalent to GNU's. */
26583 else if (is_attribute_p ("fallthrough", attr_id
))
26584 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
26585 /* Transactional Memory TS optimize_for_synchronized attribute is
26586 equivalent to GNU transaction_callable. */
26587 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
26588 TREE_PURPOSE (attribute
)
26589 = get_identifier ("transaction_callable");
26590 /* Transactional Memory attributes are GNU attributes. */
26591 else if (tm_attr_to_mask (attr_id
))
26592 TREE_PURPOSE (attribute
) = attr_id
;
26595 /* Now parse the optional argument clause of the attribute. */
26597 if (token
->type
!= CPP_OPEN_PAREN
)
26601 vec
<tree
, va_gc
> *vec
;
26602 int attr_flag
= normal_attr
;
26604 /* Maybe we don't expect to see any arguments for this attribute. */
26605 const attribute_spec
*as
26606 = lookup_attribute_spec (TREE_PURPOSE (attribute
));
26607 if (as
&& as
->max_length
== 0)
26609 error_at (token
->location
, "%qE attribute does not take any arguments",
26611 cp_parser_skip_to_closing_parenthesis (parser
,
26612 /*recovering=*/true,
26613 /*or_comma=*/false,
26614 /*consume_paren=*/true);
26615 return error_mark_node
;
26618 if (attr_ns
== gnu_identifier
26619 && attribute_takes_identifier_p (attr_id
))
26620 /* A GNU attribute that takes an identifier in parameter. */
26621 attr_flag
= id_attr
;
26625 /* For unknown attributes, just skip balanced tokens instead of
26626 trying to parse the arguments. */
26627 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1) - 1; n
; --n
)
26628 cp_lexer_consume_token (parser
->lexer
);
26632 vec
= cp_parser_parenthesized_expression_list
26633 (parser
, attr_flag
, /*cast_p=*/false,
26634 /*allow_expansion_p=*/true,
26635 /*non_constant_p=*/NULL
);
26637 arguments
= error_mark_node
;
26640 if (vec
->is_empty ())
26641 /* e.g. [[attr()]]. */
26642 error_at (token
->location
, "parentheses must be omitted if "
26643 "%qE attribute argument list is empty",
26645 arguments
= build_tree_list_vec (vec
);
26646 release_tree_vector (vec
);
26649 if (arguments
== error_mark_node
)
26650 attribute
= error_mark_node
;
26652 TREE_VALUE (attribute
) = arguments
;
26658 /* Check that the attribute ATTRIBUTE appears at most once in the
26659 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3),
26660 nodiscard, and deprecated (7.6.5). Note that
26661 carries_dependency (7.6.4) isn't implemented yet in GCC. */
26664 cp_parser_check_std_attribute (tree attributes
, tree attribute
)
26668 tree name
= get_attribute_name (attribute
);
26669 if (is_attribute_p ("noreturn", name
)
26670 && lookup_attribute ("noreturn", attributes
))
26671 error ("attribute %<noreturn%> can appear at most once "
26672 "in an attribute-list");
26673 else if (is_attribute_p ("deprecated", name
)
26674 && lookup_attribute ("deprecated", attributes
))
26675 error ("attribute %<deprecated%> can appear at most once "
26676 "in an attribute-list");
26677 else if (is_attribute_p ("nodiscard", name
)
26678 && lookup_attribute ("nodiscard", attributes
))
26679 error ("attribute %<nodiscard%> can appear at most once "
26680 "in an attribute-list");
26684 /* Parse a list of standard C++-11 attributes.
26688 attribute-list , attribute[opt]
26690 attribute-list , attribute ...
26694 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
26696 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
26697 cp_token
*token
= NULL
;
26701 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
26702 if (attribute
== error_mark_node
)
26704 if (attribute
!= NULL_TREE
)
26706 cp_parser_check_std_attribute (attributes
, attribute
);
26707 TREE_CHAIN (attribute
) = attributes
;
26708 attributes
= attribute
;
26710 token
= cp_lexer_peek_token (parser
->lexer
);
26711 if (token
->type
== CPP_ELLIPSIS
)
26713 cp_lexer_consume_token (parser
->lexer
);
26714 if (attribute
== NULL_TREE
)
26715 error_at (token
->location
,
26716 "expected attribute before %<...%>");
26719 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
26720 if (pack
== error_mark_node
)
26721 return error_mark_node
;
26722 TREE_VALUE (attribute
) = pack
;
26724 token
= cp_lexer_peek_token (parser
->lexer
);
26726 if (token
->type
!= CPP_COMMA
)
26728 cp_lexer_consume_token (parser
->lexer
);
26730 attributes
= nreverse (attributes
);
26734 /* Parse a standard C++-11 attribute specifier.
26736 attribute-specifier:
26737 [ [ attribute-using-prefix [opt] attribute-list ] ]
26738 alignment-specifier
26740 attribute-using-prefix:
26741 using attribute-namespace :
26743 alignment-specifier:
26744 alignas ( type-id ... [opt] )
26745 alignas ( alignment-expression ... [opt] ). */
26748 cp_parser_std_attribute_spec (cp_parser
*parser
)
26750 tree attributes
= NULL_TREE
;
26751 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
26753 if (token
->type
== CPP_OPEN_SQUARE
26754 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
26756 tree attr_ns
= NULL_TREE
;
26758 cp_lexer_consume_token (parser
->lexer
);
26759 cp_lexer_consume_token (parser
->lexer
);
26761 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
26763 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
26764 if (token
->type
== CPP_NAME
)
26765 attr_ns
= token
->u
.value
;
26766 else if (token
->type
== CPP_KEYWORD
)
26767 attr_ns
= ridpointers
[(int) token
->keyword
];
26768 else if (token
->flags
& NAMED_OP
)
26769 attr_ns
= get_identifier (cpp_type2name (token
->type
,
26772 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
26774 if (cxx_dialect
< cxx17
)
26775 pedwarn (input_location
, 0,
26776 "attribute using prefix only available "
26777 "with %<-std=c++17%> or %<-std=gnu++17%>");
26779 cp_lexer_consume_token (parser
->lexer
);
26780 cp_lexer_consume_token (parser
->lexer
);
26781 cp_lexer_consume_token (parser
->lexer
);
26784 attr_ns
= NULL_TREE
;
26787 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
26789 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
26790 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
26791 cp_parser_skip_to_end_of_statement (parser
);
26793 /* Warn about parsing c++11 attribute in non-c++11 mode, only
26794 when we are sure that we have actually parsed them. */
26795 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26801 /* Look for an alignment-specifier. */
26803 token
= cp_lexer_peek_token (parser
->lexer
);
26805 if (token
->type
!= CPP_KEYWORD
26806 || token
->keyword
!= RID_ALIGNAS
)
26809 cp_lexer_consume_token (parser
->lexer
);
26810 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
26812 matching_parens parens
;
26813 if (!parens
.require_open (parser
))
26814 return error_mark_node
;
26816 cp_parser_parse_tentatively (parser
);
26817 alignas_expr
= cp_parser_type_id (parser
);
26819 if (!cp_parser_parse_definitely (parser
))
26821 alignas_expr
= cp_parser_assignment_expression (parser
);
26822 if (alignas_expr
== error_mark_node
)
26823 cp_parser_skip_to_end_of_statement (parser
);
26824 if (alignas_expr
== NULL_TREE
26825 || alignas_expr
== error_mark_node
)
26826 return alignas_expr
;
26829 alignas_expr
= cxx_alignas_expr (alignas_expr
);
26830 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
26832 /* Handle alignas (pack...). */
26833 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26835 cp_lexer_consume_token (parser
->lexer
);
26836 alignas_expr
= make_pack_expansion (alignas_expr
);
26839 /* Something went wrong, so don't build the attribute. */
26840 if (alignas_expr
== error_mark_node
)
26841 return error_mark_node
;
26843 /* Missing ')' means the code cannot possibly be valid; go ahead
26844 and commit to make sure we issue a hard error. */
26845 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
26846 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
26847 cp_parser_commit_to_tentative_parse (parser
);
26849 if (!parens
.require_close (parser
))
26850 return error_mark_node
;
26852 /* Build the C++-11 representation of an 'aligned'
26855 = build_tree_list (build_tree_list (gnu_identifier
,
26856 aligned_identifier
), alignas_expr
);
26862 /* Parse a standard C++-11 attribute-specifier-seq.
26864 attribute-specifier-seq:
26865 attribute-specifier-seq [opt] attribute-specifier
26869 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
26871 tree attr_specs
= NULL_TREE
;
26872 tree attr_last
= NULL_TREE
;
26874 /* Don't create wrapper nodes within attributes: the
26875 handlers don't know how to handle them. */
26876 auto_suppress_location_wrappers sentinel
;
26880 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
26881 if (attr_spec
== NULL_TREE
)
26883 if (attr_spec
== error_mark_node
)
26884 return error_mark_node
;
26887 TREE_CHAIN (attr_last
) = attr_spec
;
26889 attr_specs
= attr_last
= attr_spec
;
26890 attr_last
= tree_last (attr_last
);
26896 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
26897 return index of the first token after balanced-token, or N on failure. */
26900 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
26903 int nparens
= 0, nbraces
= 0, nsquares
= 0;
26905 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
26907 case CPP_PRAGMA_EOL
:
26908 if (!parser
->lexer
->in_pragma
)
26912 /* Ran out of tokens. */
26914 case CPP_OPEN_PAREN
:
26917 case CPP_OPEN_BRACE
:
26920 case CPP_OPEN_SQUARE
:
26923 case CPP_CLOSE_PAREN
:
26926 case CPP_CLOSE_BRACE
:
26929 case CPP_CLOSE_SQUARE
:
26935 while (nparens
|| nbraces
|| nsquares
);
26939 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
26940 return index of the first token after the GNU attribute tokens, or N on
26944 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
26948 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
26949 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
26950 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
26953 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
26956 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
26963 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
26964 next token), return index of the first token after the standard C++11
26965 attribute tokens, or N on failure. */
26968 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
26972 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
26973 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
26975 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26978 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
26982 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
26983 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
26985 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
26996 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
26997 as the next token), return index of the first token after the attribute
26998 tokens, or N on failure. */
27001 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
27003 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
27004 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
27005 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
27008 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
27009 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
27010 current value of the PEDANTIC flag, regardless of whether or not
27011 the `__extension__' keyword is present. The caller is responsible
27012 for restoring the value of the PEDANTIC flag. */
27015 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
27017 /* Save the old value of the PEDANTIC flag. */
27018 *saved_pedantic
= pedantic
;
27020 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
27022 /* Consume the `__extension__' token. */
27023 cp_lexer_consume_token (parser
->lexer
);
27024 /* We're not being pedantic while the `__extension__' keyword is
27034 /* Parse a label declaration.
27037 __label__ label-declarator-seq ;
27039 label-declarator-seq:
27040 identifier , label-declarator-seq
27044 cp_parser_label_declaration (cp_parser
* parser
)
27046 /* Look for the `__label__' keyword. */
27047 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
27053 /* Look for an identifier. */
27054 identifier
= cp_parser_identifier (parser
);
27055 /* If we failed, stop. */
27056 if (identifier
== error_mark_node
)
27058 /* Declare it as a label. */
27059 finish_label_decl (identifier
);
27060 /* If the next token is a `;', stop. */
27061 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
27063 /* Look for the `,' separating the label declarations. */
27064 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
27067 /* Look for the final `;'. */
27068 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
27071 // -------------------------------------------------------------------------- //
27072 // Concept definitions
27075 cp_parser_concept_definition (cp_parser
*parser
)
27077 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
));
27078 cp_lexer_consume_token (parser
->lexer
);
27080 cp_expr id
= cp_parser_identifier (parser
);
27081 if (id
== error_mark_node
)
27083 cp_parser_skip_to_end_of_statement (parser
);
27084 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27088 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
27090 cp_parser_skip_to_end_of_statement (parser
);
27091 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27092 return error_mark_node
;
27095 processing_constraint_expression_sentinel parsing_constraint
;
27096 tree init
= cp_parser_constraint_expression (parser
);
27097 if (init
== error_mark_node
)
27098 cp_parser_skip_to_end_of_statement (parser
);
27100 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
27101 but continue as if it were. */
27102 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27104 return finish_concept_definition (id
, init
);
27107 // -------------------------------------------------------------------------- //
27110 /* Diagnose an expression that should appear in ()'s within a requires-clause
27111 and suggest where to place those parentheses. */
27114 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc
)
27116 error_at (loc
, "expression must be enclosed in parentheses");
27120 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc
)
27122 gcc_rich_location
richloc (loc
);
27123 richloc
.add_fixit_insert_before ("(");
27124 richloc
.add_fixit_insert_after (")");
27125 error_at (&richloc
, "expression must be enclosed in parentheses");
27128 /* Characterizes the likely kind of expression intended by a mis-written
27129 primary constraint. */
27130 enum primary_constraint_error
27133 pce_maybe_operator
,
27137 /* Returns true if the token(s) following a primary-expression in a
27138 constraint-logical-* expression would require parentheses. */
27140 static primary_constraint_error
27141 cp_parser_constraint_requires_parens (cp_parser
*parser
, bool lambda_p
)
27143 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27144 switch (token
->type
)
27151 /* An equal sign may be part of the the definition of a function,
27152 and not an assignment operator, when parsing the expression
27153 for a trailing requires-clause. For example:
27155 template<typename T>
27157 S() requires C<T> = default;
27160 Don't try to reparse this a binary operator. */
27161 if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DELETE
)
27162 || cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DEFAULT
))
27165 gcc_fallthrough ();
27168 /* Arithmetic operators. */
27174 /* Bitwise operators. */
27180 /* Relational operators. */
27186 case CPP_GREATER_EQ
:
27187 case CPP_SPACESHIP
:
27188 /* Pointer-to-member. */
27190 case CPP_DEREF_STAR
:
27191 /* Assignment operators. */
27200 case CPP_RSHIFT_EQ
:
27201 case CPP_LSHIFT_EQ
:
27202 /* Conditional operator */
27204 /* Unenclosed binary or conditional operator. */
27205 return pce_maybe_operator
;
27207 case CPP_OPEN_PAREN
:
27209 /* A primary constraint that precedes the parameter-list of a
27210 lambda expression is followed by an open paren.
27212 []<typename T> requires C (T a, T b) { ... }
27214 Don't try to re-parse this as a postfix expression. */
27218 gcc_fallthrough ();
27220 case CPP_OPEN_SQUARE
:
27221 case CPP_PLUS_PLUS
:
27222 case CPP_MINUS_MINUS
:
27225 /* Unenclosed postfix operator. */
27226 return pce_maybe_postfix
;
27230 /* Returns true if the next token begins a unary expression, preceded by
27231 an operator or keyword. */
27234 cp_parser_unary_constraint_requires_parens (cp_parser
*parser
)
27236 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27237 switch (token
->type
)
27244 case CPP_PLUS_PLUS
:
27245 case CPP_MINUS_MINUS
:
27250 switch (token
->keyword
)
27254 case RID_REINTCAST
:
27255 case RID_CONSTCAST
:
27277 /* Parse a primary expression within a constraint. */
27280 cp_parser_constraint_primary_expression (cp_parser
*parser
, bool lambda_p
)
27282 /* If this looks like a unary expression, parse it as such, but diagnose
27283 it as ill-formed; it requires parens. */
27284 if (cp_parser_unary_constraint_requires_parens (parser
))
27286 cp_expr e
= cp_parser_assignment_expression (parser
, NULL
, false, false);
27287 cp_parser_diagnose_ungrouped_constraint_rich (e
.get_location());
27291 cp_parser_parse_tentatively (parser
);
27293 location_t loc
= input_location
;
27294 cp_expr expr
= cp_parser_primary_expression (parser
,
27295 /*address_p=*/false,
27297 /*template_arg_p=*/false,
27299 expr
.maybe_add_location_wrapper ();
27301 primary_constraint_error pce
= pce_ok
;
27302 if (expr
!= error_mark_node
)
27304 /* The primary-expression could be part of an unenclosed non-logical
27305 compound expression. */
27306 pce
= cp_parser_constraint_requires_parens (parser
, lambda_p
);
27308 cp_parser_simulate_error (parser
);
27310 expr
= finish_constraint_primary_expr (expr
);
27312 if (cp_parser_parse_definitely (parser
))
27314 if (expr
== error_mark_node
)
27315 return error_mark_node
;
27317 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
27318 error, but return the expression as if it were valid. */
27319 gcc_assert (pce
!= pce_ok
);
27320 cp_parser_parse_tentatively (parser
);
27321 if (pce
== pce_maybe_operator
)
27322 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
27324 expr
= cp_parser_simple_cast_expression (parser
);
27325 if (cp_parser_parse_definitely (parser
))
27327 cp_parser_diagnose_ungrouped_constraint_rich (expr
.get_location());
27331 /* Otherwise, something has gone very wrong, and we can't generate a more
27332 meaningful diagnostic or recover. */
27333 cp_parser_diagnose_ungrouped_constraint_plain (loc
);
27334 return error_mark_node
;
27337 /* Parse a constraint-logical-and-expression.
27339 constraint-logical-and-expression:
27341 constraint-logical-and-expression '&&' primary-expression */
27344 cp_parser_constraint_logical_and_expression (cp_parser
*parser
, bool lambda_p
)
27346 cp_expr lhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
27347 while (cp_lexer_next_token_is (parser
->lexer
, CPP_AND_AND
))
27349 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
27350 tree rhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
27351 lhs
= finish_constraint_and_expr (op
->location
, lhs
, rhs
);
27356 /* Parse a constraint-logical-or-expression.
27358 constraint-logical-or-expression:
27359 constraint-logical-and-expression
27360 constraint-logical-or-expression '||' constraint-logical-and-expression */
27363 cp_parser_constraint_logical_or_expression (cp_parser
*parser
, bool lambda_p
)
27365 cp_expr lhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
27366 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OR_OR
))
27368 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
27369 cp_expr rhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
27370 lhs
= finish_constraint_or_expr (op
->location
, lhs
, rhs
);
27375 /* Parse the expression after a requires-clause. This has a different grammar
27376 than that in the concepts TS. */
27379 cp_parser_requires_clause_expression (cp_parser
*parser
, bool lambda_p
)
27381 processing_constraint_expression_sentinel parsing_constraint
;
27382 ++processing_template_decl
;
27383 cp_expr expr
= cp_parser_constraint_logical_or_expression (parser
, lambda_p
);
27384 if (check_for_bare_parameter_packs (expr
))
27385 expr
= error_mark_node
;
27386 --processing_template_decl
;
27390 /* Parse a expression after a requires clause.
27392 constraint-expression:
27393 logical-or-expression
27395 The required logical-or-expression must be a constant expression. Note
27396 that we don't check that the expression is constepxr here. We defer until
27397 we analyze constraints and then, we only check atomic constraints. */
27400 cp_parser_constraint_expression (cp_parser
*parser
)
27402 processing_constraint_expression_sentinel parsing_constraint
;
27403 ++processing_template_decl
;
27404 cp_expr expr
= cp_parser_binary_expression (parser
, false, true,
27405 PREC_NOT_OPERATOR
, NULL
);
27406 if (check_for_bare_parameter_packs (expr
))
27407 expr
= error_mark_node
;
27408 --processing_template_decl
;
27409 expr
.maybe_add_location_wrapper ();
27413 /* Optionally parse a requires clause:
27416 `requires` constraint-logical-or-expression.
27418 `requires constraint-expression.
27420 LAMBDA_P is true when the requires-clause is parsed before the
27421 parameter-list of a lambda-declarator. */
27424 cp_parser_requires_clause_opt (cp_parser
*parser
, bool lambda_p
)
27426 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
27427 if (tok
->keyword
!= RID_REQUIRES
)
27429 if (!flag_concepts
&& tok
->type
== CPP_NAME
27430 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
27432 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
27433 "%<requires%> only available with "
27434 "%<-std=c++2a%> or %<-fconcepts%>");
27435 /* Parse and discard the requires-clause. */
27436 cp_lexer_consume_token (parser
->lexer
);
27437 cp_parser_constraint_expression (parser
);
27441 cp_lexer_consume_token (parser
->lexer
);
27443 if (!flag_concepts_ts
)
27444 return cp_parser_requires_clause_expression (parser
, lambda_p
);
27446 return cp_parser_constraint_expression (parser
);
27449 /*---------------------------------------------------------------------------
27450 Requires expressions
27451 ---------------------------------------------------------------------------*/
27453 /* Parse a requires expression
27455 requirement-expression:
27456 'requires' requirement-parameter-list [opt] requirement-body */
27459 cp_parser_requires_expression (cp_parser
*parser
)
27461 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
27462 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
27464 /* This is definitely a requires-expression. */
27465 cp_parser_commit_to_tentative_parse (parser
);
27469 /* Local parameters are delared as variables within the scope
27470 of the expression. They are not visible past the end of
27471 the expression. Expressions within the requires-expression
27472 are unevaluated. */
27473 struct scope_sentinel
27477 ++cp_unevaluated_operand
;
27478 begin_scope (sk_block
, NULL_TREE
);
27483 pop_bindings_and_leave_scope ();
27484 --cp_unevaluated_operand
;
27488 /* Parse the optional parameter list. */
27489 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
27491 parms
= cp_parser_requirement_parameter_list (parser
);
27492 if (parms
== error_mark_node
)
27493 return error_mark_node
;
27498 /* Parse the requirement body. */
27499 reqs
= cp_parser_requirement_body (parser
);
27500 if (reqs
== error_mark_node
)
27501 return error_mark_node
;
27504 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
27506 grokparms (parms
, &parms
);
27507 loc
= make_location (loc
, loc
, parser
->lexer
);
27508 return finish_requires_expr (loc
, parms
, reqs
);
27511 /* Parse a parameterized requirement.
27513 requirement-parameter-list:
27514 '(' parameter-declaration-clause ')' */
27517 cp_parser_requirement_parameter_list (cp_parser
*parser
)
27519 matching_parens parens
;
27520 if (!parens
.require_open (parser
))
27521 return error_mark_node
;
27524 = cp_parser_parameter_declaration_clause (parser
, CP_PARSER_FLAGS_NONE
);
27526 if (!parens
.require_close (parser
))
27527 return error_mark_node
;
27532 /* Parse the body of a requirement.
27535 '{' requirement-list '}' */
27537 cp_parser_requirement_body (cp_parser
*parser
)
27539 matching_braces braces
;
27540 if (!braces
.require_open (parser
))
27541 return error_mark_node
;
27543 tree reqs
= cp_parser_requirement_seq (parser
);
27545 if (!braces
.require_close (parser
))
27546 return error_mark_node
;
27551 /* Parse a sequence of requirements.
27555 requirement-seq requirement */
27558 cp_parser_requirement_seq (cp_parser
*parser
)
27560 tree result
= NULL_TREE
;
27563 tree req
= cp_parser_requirement (parser
);
27564 if (req
!= error_mark_node
)
27565 result
= tree_cons (NULL_TREE
, req
, result
);
27566 } while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
));
27568 /* If there are no valid requirements, this is not a valid expression. */
27570 return error_mark_node
;
27572 /* Reverse the order of requirements so they are analyzed in order. */
27573 return nreverse (result
);
27576 /* Parse a syntactic requirement or type requirement.
27580 compound-requirement
27582 nested-requirement */
27585 cp_parser_requirement (cp_parser
*parser
)
27587 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27588 return cp_parser_compound_requirement (parser
);
27589 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
27590 return cp_parser_type_requirement (parser
);
27591 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
27592 return cp_parser_nested_requirement (parser
);
27594 return cp_parser_simple_requirement (parser
);
27597 /* Parse a simple requirement.
27599 simple-requirement:
27603 cp_parser_simple_requirement (cp_parser
*parser
)
27605 location_t start
= cp_lexer_peek_token (parser
->lexer
)->location
;
27606 cp_expr expr
= cp_parser_expression (parser
, NULL
, false, false);
27607 if (expr
== error_mark_node
)
27608 cp_parser_skip_to_end_of_statement (parser
);
27610 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27612 if (!expr
|| expr
== error_mark_node
)
27613 return error_mark_node
;
27615 /* Sometimes we don't get locations, so use the cached token location
27616 as a reasonable approximation. */
27617 if (expr
.get_location() == UNKNOWN_LOCATION
)
27618 expr
.set_location (start
);
27620 return finish_simple_requirement (expr
.get_location (), expr
);
27623 /* Parse a type requirement
27626 nested-name-specifier [opt] required-type-name ';'
27628 required-type-name:
27630 'template' [opt] simple-template-id */
27633 cp_parser_type_requirement (cp_parser
*parser
)
27635 cp_token
*start_tok
= cp_lexer_consume_token (parser
->lexer
);
27636 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27638 // Save the scope before parsing name specifiers.
27639 tree saved_scope
= parser
->scope
;
27640 tree saved_object_scope
= parser
->object_scope
;
27641 tree saved_qualifying_scope
= parser
->qualifying_scope
;
27642 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
27643 cp_parser_nested_name_specifier_opt (parser
,
27644 /*typename_keyword_p=*/true,
27645 /*check_dependency_p=*/false,
27647 /*is_declaration=*/false);
27650 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
27652 cp_lexer_consume_token (parser
->lexer
);
27653 type
= cp_parser_template_id (parser
,
27654 /*template_keyword_p=*/true,
27655 /*check_dependency=*/false,
27656 /*tag_type=*/none_type
,
27657 /*is_declaration=*/false);
27658 type
= make_typename_type (parser
->scope
, type
, typename_type
,
27659 /*complain=*/tf_error
);
27662 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
27664 if (TREE_CODE (type
) == TYPE_DECL
)
27665 type
= TREE_TYPE (type
);
27667 parser
->scope
= saved_scope
;
27668 parser
->object_scope
= saved_object_scope
;
27669 parser
->qualifying_scope
= saved_qualifying_scope
;
27671 if (type
== error_mark_node
)
27672 cp_parser_skip_to_end_of_statement (parser
);
27674 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27676 if (type
== error_mark_node
)
27677 return error_mark_node
;
27679 loc
= make_location (loc
, start_tok
->location
, parser
->lexer
);
27680 return finish_type_requirement (loc
, type
);
27683 /* Parse a compound requirement
27685 compound-requirement:
27686 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
27689 cp_parser_compound_requirement (cp_parser
*parser
)
27691 /* Parse an expression enclosed in '{ }'s. */
27692 matching_braces braces
;
27693 if (!braces
.require_open (parser
))
27694 return error_mark_node
;
27696 cp_token
*expr_token
= cp_lexer_peek_token (parser
->lexer
);
27698 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
27699 if (expr
== error_mark_node
)
27700 cp_parser_skip_to_closing_brace (parser
);
27702 if (!braces
.require_close (parser
))
27704 cp_parser_skip_to_end_of_statement (parser
);
27705 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27706 return error_mark_node
;
27709 /* If the expression was invalid, skip the remainder of the requirement. */
27710 if (!expr
|| expr
== error_mark_node
)
27712 cp_parser_skip_to_end_of_statement (parser
);
27713 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27714 return error_mark_node
;
27717 /* Parse the optional noexcept. */
27718 bool noexcept_p
= false;
27719 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
27721 cp_lexer_consume_token (parser
->lexer
);
27725 /* Parse the optional trailing return type. */
27726 tree type
= NULL_TREE
;
27727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
27729 cp_lexer_consume_token (parser
->lexer
);
27730 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
27732 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
27733 parser
->in_result_type_constraint_p
= true;
27734 /* C++2a allows either a type-id or a type-constraint. Parsing
27735 a type-id will subsume the parsing for a type-constraint but
27736 allow for more syntactic forms (e.g., const C<T>*). */
27737 type
= cp_parser_trailing_type_id (parser
);
27738 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
27739 if (type
== error_mark_node
)
27740 return error_mark_node
;
27742 location_t type_loc
= make_location (tok
->location
, tok
->location
,
27745 /* Check that we haven't written something like 'const C<T>*'. */
27746 if (type_uses_auto (type
))
27748 if (!is_auto (type
))
27750 error_at (type_loc
,
27751 "result type is not a plain type-constraint");
27752 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27753 return error_mark_node
;
27756 else if (!flag_concepts_ts
)
27757 /* P1452R2 removed the trailing-return-type option. */
27758 error_at (type_loc
,
27759 "return-type-requirement is not a type-constraint");
27762 location_t loc
= make_location (expr_token
->location
,
27763 braces
.open_location (),
27766 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27768 if (expr
== error_mark_node
|| type
== error_mark_node
)
27769 return error_mark_node
;
27771 return finish_compound_requirement (loc
, expr
, type
, noexcept_p
);
27774 /* Parse a nested requirement. This is the same as a requires clause.
27776 nested-requirement:
27780 cp_parser_nested_requirement (cp_parser
*parser
)
27782 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
27783 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
27784 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27785 tree req
= cp_parser_constraint_expression (parser
);
27786 if (req
== error_mark_node
)
27787 cp_parser_skip_to_end_of_statement (parser
);
27788 loc
= make_location (loc
, tok
->location
, parser
->lexer
);
27789 cp_parser_consume_semicolon_at_end_of_statement (parser
);
27790 if (req
== error_mark_node
)
27791 return error_mark_node
;
27792 return finish_nested_requirement (loc
, req
);
27795 /* Support Functions */
27797 /* Return the appropriate prefer_type argument for lookup_name_real based on
27798 tag_type and template_mem_access. */
27801 prefer_type_arg (tag_types tag_type
, bool template_mem_access
= false)
27803 /* DR 141: When looking in the current enclosing context for a template-name
27804 after -> or ., only consider class templates. */
27805 if (template_mem_access
)
27809 case none_type
: return 0; // No preference.
27810 case scope_type
: return 1; // Type or namespace.
27811 default: return 2; // Type only.
27815 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
27816 NAME should have one of the representations used for an
27817 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
27818 is returned. If PARSER->SCOPE is a dependent type, then a
27819 SCOPE_REF is returned.
27821 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
27822 returned; the name was already resolved when the TEMPLATE_ID_EXPR
27823 was formed. Abstractly, such entities should not be passed to this
27824 function, because they do not need to be looked up, but it is
27825 simpler to check for this special case here, rather than at the
27828 In cases not explicitly covered above, this function returns a
27829 DECL, OVERLOAD, or baselink representing the result of the lookup.
27830 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
27833 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
27834 (e.g., "struct") that was used. In that case bindings that do not
27835 refer to types are ignored.
27837 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
27840 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
27843 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
27846 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
27847 TREE_LIST of candidates if name-lookup results in an ambiguity, and
27848 NULL_TREE otherwise. */
27851 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
27852 enum tag_types tag_type
,
27855 bool check_dependency
,
27856 tree
*ambiguous_decls
,
27857 location_t name_location
)
27860 tree object_type
= parser
->context
->object_type
;
27862 /* Assume that the lookup will be unambiguous. */
27863 if (ambiguous_decls
)
27864 *ambiguous_decls
= NULL_TREE
;
27866 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
27867 no longer valid. Note that if we are parsing tentatively, and
27868 the parse fails, OBJECT_TYPE will be automatically restored. */
27869 parser
->context
->object_type
= NULL_TREE
;
27871 if (name
== error_mark_node
)
27872 return error_mark_node
;
27874 /* A template-id has already been resolved; there is no lookup to
27876 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
27878 if (BASELINK_P (name
))
27880 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
27881 == TEMPLATE_ID_EXPR
);
27885 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
27886 it should already have been checked to make sure that the name
27887 used matches the type being destroyed. */
27888 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
27892 /* Figure out to which type this destructor applies. */
27894 type
= parser
->scope
;
27895 else if (object_type
)
27896 type
= object_type
;
27898 type
= current_class_type
;
27899 /* If that's not a class type, there is no destructor. */
27900 if (!type
|| !CLASS_TYPE_P (type
))
27901 return error_mark_node
;
27903 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
27904 lazily_declare_fn (sfk_destructor
, type
);
27906 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
27909 return error_mark_node
;
27912 /* By this point, the NAME should be an ordinary identifier. If
27913 the id-expression was a qualified name, the qualifying scope is
27914 stored in PARSER->SCOPE at this point. */
27915 gcc_assert (identifier_p (name
));
27917 /* Perform the lookup. */
27922 if (parser
->scope
== error_mark_node
)
27923 return error_mark_node
;
27925 /* If the SCOPE is dependent, the lookup must be deferred until
27926 the template is instantiated -- unless we are explicitly
27927 looking up names in uninstantiated templates. Even then, we
27928 cannot look up the name if the scope is not a class type; it
27929 might, for example, be a template type parameter. */
27930 dependent_p
= (TYPE_P (parser
->scope
)
27931 && dependent_scope_p (parser
->scope
));
27932 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
27934 /* Defer lookup. */
27935 decl
= error_mark_node
;
27938 tree pushed_scope
= NULL_TREE
;
27940 /* If PARSER->SCOPE is a dependent type, then it must be a
27941 class type, and we must not be checking dependencies;
27942 otherwise, we would have processed this lookup above. So
27943 that PARSER->SCOPE is not considered a dependent base by
27944 lookup_member, we must enter the scope here. */
27946 pushed_scope
= push_scope (parser
->scope
);
27948 /* If the PARSER->SCOPE is a template specialization, it
27949 may be instantiated during name lookup. In that case,
27950 errors may be issued. Even if we rollback the current
27951 tentative parse, those errors are valid. */
27952 decl
= lookup_qualified_name (parser
->scope
, name
,
27953 prefer_type_arg (tag_type
),
27954 /*complain=*/true);
27956 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
27957 lookup result and the nested-name-specifier nominates a class C:
27958 * if the name specified after the nested-name-specifier, when
27959 looked up in C, is the injected-class-name of C (Clause 9), or
27960 * if the name specified after the nested-name-specifier is the
27961 same as the identifier or the simple-template-id's template-
27962 name in the last component of the nested-name-specifier,
27963 the name is instead considered to name the constructor of
27964 class C. [ Note: for example, the constructor is not an
27965 acceptable lookup result in an elaborated-type-specifier so
27966 the constructor would not be used in place of the
27967 injected-class-name. --end note ] Such a constructor name
27968 shall be used only in the declarator-id of a declaration that
27969 names a constructor or in a using-declaration. */
27970 if (tag_type
== none_type
27971 && DECL_SELF_REFERENCE_P (decl
)
27972 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
27973 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
27974 prefer_type_arg (tag_type
),
27975 /*complain=*/true);
27977 /* If we have a single function from a using decl, pull it out. */
27978 if (TREE_CODE (decl
) == OVERLOAD
27979 && !really_overloaded_fn (decl
))
27980 decl
= OVL_FUNCTION (decl
);
27983 pop_scope (pushed_scope
);
27986 /* If the scope is a dependent type and either we deferred lookup or
27987 we did lookup but didn't find the name, rememeber the name. */
27988 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
27989 && dependent_type_p (parser
->scope
))
27995 /* The resolution to Core Issue 180 says that `struct
27996 A::B' should be considered a type-name, even if `A'
27998 type
= make_typename_type (parser
->scope
, name
, tag_type
,
27999 /*complain=*/tf_error
);
28000 if (type
!= error_mark_node
)
28001 decl
= TYPE_NAME (type
);
28003 else if (is_template
28004 && (cp_parser_next_token_ends_template_argument_p (parser
)
28005 || cp_lexer_next_token_is (parser
->lexer
,
28007 decl
= make_unbound_class_template (parser
->scope
,
28009 /*complain=*/tf_error
);
28011 decl
= build_qualified_name (/*type=*/NULL_TREE
,
28012 parser
->scope
, name
,
28015 parser
->qualifying_scope
= parser
->scope
;
28016 parser
->object_scope
= NULL_TREE
;
28018 else if (object_type
)
28020 /* Look up the name in the scope of the OBJECT_TYPE, unless the
28021 OBJECT_TYPE is not a class. */
28022 if (CLASS_TYPE_P (object_type
))
28023 /* If the OBJECT_TYPE is a template specialization, it may
28024 be instantiated during name lookup. In that case, errors
28025 may be issued. Even if we rollback the current tentative
28026 parse, those errors are valid. */
28027 decl
= lookup_member (object_type
,
28030 prefer_type_arg (tag_type
),
28031 tf_warning_or_error
);
28036 /* Look it up in the enclosing context. DR 141: When looking for a
28037 template-name after -> or ., only consider class templates. */
28038 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
, is_template
),
28040 /*block_p=*/true, is_namespace
, 0);
28041 if (object_type
== unknown_type_node
)
28042 /* The object is type-dependent, so we can't look anything up; we used
28043 this to get the DR 141 behavior. */
28044 object_type
= NULL_TREE
;
28045 parser
->object_scope
= object_type
;
28046 parser
->qualifying_scope
= NULL_TREE
;
28050 decl
= lookup_name_real (name
, prefer_type_arg (tag_type
),
28052 /*block_p=*/true, is_namespace
, 0);
28053 parser
->qualifying_scope
= NULL_TREE
;
28054 parser
->object_scope
= NULL_TREE
;
28057 /* If the lookup failed, let our caller know. */
28058 if (!decl
|| decl
== error_mark_node
)
28059 return error_mark_node
;
28061 /* Pull out the template from an injected-class-name (or multiple). */
28063 decl
= maybe_get_template_decl_from_type_decl (decl
);
28065 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
28066 if (TREE_CODE (decl
) == TREE_LIST
)
28068 if (ambiguous_decls
)
28069 *ambiguous_decls
= decl
;
28070 /* The error message we have to print is too complicated for
28071 cp_parser_error, so we incorporate its actions directly. */
28072 if (!cp_parser_simulate_error (parser
))
28074 error_at (name_location
, "reference to %qD is ambiguous",
28076 print_candidates (decl
);
28078 return error_mark_node
;
28081 gcc_assert (DECL_P (decl
)
28082 || TREE_CODE (decl
) == OVERLOAD
28083 || TREE_CODE (decl
) == SCOPE_REF
28084 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
28085 || BASELINK_P (decl
));
28087 /* If we have resolved the name of a member declaration, check to
28088 see if the declaration is accessible. When the name resolves to
28089 set of overloaded functions, accessibility is checked when
28090 overload resolution is done.
28092 During an explicit instantiation, access is not checked at all,
28093 as per [temp.explicit]. */
28095 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
);
28097 maybe_record_typedef_use (decl
);
28099 return cp_expr (decl
, name_location
);
28102 /* Like cp_parser_lookup_name, but for use in the typical case where
28103 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
28104 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
28107 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
28109 return cp_parser_lookup_name (parser
, name
,
28111 /*is_template=*/false,
28112 /*is_namespace=*/false,
28113 /*check_dependency=*/true,
28114 /*ambiguous_decls=*/NULL
,
28118 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
28119 the current context, return the TYPE_DECL. If TAG_NAME_P is
28120 true, the DECL indicates the class being defined in a class-head,
28121 or declared in an elaborated-type-specifier.
28123 Otherwise, return DECL. */
28126 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
28128 /* If the TEMPLATE_DECL is being declared as part of a class-head,
28129 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
28132 template <typename T> struct B;
28135 template <typename T> struct A::B {};
28137 Similarly, in an elaborated-type-specifier:
28139 namespace N { struct X{}; }
28142 template <typename T> friend struct N::X;
28145 However, if the DECL refers to a class type, and we are in
28146 the scope of the class, then the name lookup automatically
28147 finds the TYPE_DECL created by build_self_reference rather
28148 than a TEMPLATE_DECL. For example, in:
28150 template <class T> struct S {
28154 there is no need to handle such case. */
28156 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
28157 return DECL_TEMPLATE_RESULT (decl
);
28162 /* If too many, or too few, template-parameter lists apply to the
28163 declarator, issue an error message. Returns TRUE if all went well,
28164 and FALSE otherwise. */
28167 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
28168 cp_declarator
*declarator
,
28169 location_t declarator_location
)
28171 switch (declarator
->kind
)
28175 unsigned num_templates
= 0;
28176 tree scope
= declarator
->u
.id
.qualifying_scope
;
28177 bool template_id_p
= false;
28180 num_templates
= num_template_headers_for_class (scope
);
28181 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
28182 == TEMPLATE_ID_EXPR
)
28184 /* If the DECLARATOR has the form `X<y>' then it uses one
28185 additional level of template parameters. */
28187 template_id_p
= true;
28190 return cp_parser_check_template_parameters
28191 (parser
, num_templates
, template_id_p
, declarator_location
,
28198 case cdk_reference
:
28200 return (cp_parser_check_declarator_template_parameters
28201 (parser
, declarator
->declarator
, declarator_location
));
28208 gcc_unreachable ();
28213 /* NUM_TEMPLATES were used in the current declaration. If that is
28214 invalid, return FALSE and issue an error messages. Otherwise,
28215 return TRUE. If DECLARATOR is non-NULL, then we are checking a
28216 declarator and we can print more accurate diagnostics. */
28219 cp_parser_check_template_parameters (cp_parser
* parser
,
28220 unsigned num_templates
,
28221 bool template_id_p
,
28222 location_t location
,
28223 cp_declarator
*declarator
)
28225 /* If there are the same number of template classes and parameter
28226 lists, that's OK. */
28227 if (parser
->num_template_parameter_lists
== num_templates
)
28229 /* If there are more, but only one more, and the name ends in an identifier,
28230 then we are declaring a primary template. That's OK too. */
28232 && parser
->num_template_parameter_lists
== num_templates
+ 1)
28234 /* If there are more template classes than parameter lists, we have
28237 template <class T> void S<T>::R<T>::f (); */
28238 if (parser
->num_template_parameter_lists
< num_templates
)
28240 if (declarator
&& !current_function_decl
)
28241 error_at (location
, "specializing member %<%T::%E%> "
28242 "requires %<template<>%> syntax",
28243 declarator
->u
.id
.qualifying_scope
,
28244 declarator
->u
.id
.unqualified_name
);
28245 else if (declarator
)
28246 error_at (location
, "invalid declaration of %<%T::%E%>",
28247 declarator
->u
.id
.qualifying_scope
,
28248 declarator
->u
.id
.unqualified_name
);
28250 error_at (location
, "too few template-parameter-lists");
28253 /* Otherwise, there are too many template parameter lists. We have
28256 template <class T> template <class U> void S::f(); */
28257 error_at (location
, "too many template-parameter-lists");
28261 /* Parse an optional `::' token indicating that the following name is
28262 from the global namespace. If so, PARSER->SCOPE is set to the
28263 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
28264 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
28265 Returns the new value of PARSER->SCOPE, if the `::' token is
28266 present, and NULL_TREE otherwise. */
28269 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
28273 /* Peek at the next token. */
28274 token
= cp_lexer_peek_token (parser
->lexer
);
28275 /* If we're looking at a `::' token then we're starting from the
28276 global namespace, not our current location. */
28277 if (token
->type
== CPP_SCOPE
)
28279 /* Consume the `::' token. */
28280 cp_lexer_consume_token (parser
->lexer
);
28281 /* Set the SCOPE so that we know where to start the lookup. */
28282 parser
->scope
= global_namespace
;
28283 parser
->qualifying_scope
= global_namespace
;
28284 parser
->object_scope
= NULL_TREE
;
28286 return parser
->scope
;
28288 else if (!current_scope_valid_p
)
28290 parser
->scope
= NULL_TREE
;
28291 parser
->qualifying_scope
= NULL_TREE
;
28292 parser
->object_scope
= NULL_TREE
;
28298 /* Returns TRUE if the upcoming token sequence is the start of a
28299 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
28300 declarator is preceded by the `friend' specifier. The parser flags FLAGS
28301 is used to control type-specifier parsing. */
28304 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
28307 bool constructor_p
;
28308 bool outside_class_specifier_p
;
28309 tree nested_name_specifier
;
28310 cp_token
*next_token
;
28312 /* The common case is that this is not a constructor declarator, so
28313 try to avoid doing lots of work if at all possible. It's not
28314 valid declare a constructor at function scope. */
28315 if (parser
->in_function_body
)
28317 /* And only certain tokens can begin a constructor declarator. */
28318 next_token
= cp_lexer_peek_token (parser
->lexer
);
28319 if (next_token
->type
!= CPP_NAME
28320 && next_token
->type
!= CPP_SCOPE
28321 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
28322 && next_token
->type
!= CPP_TEMPLATE_ID
)
28325 /* Parse tentatively; we are going to roll back all of the tokens
28327 cp_parser_parse_tentatively (parser
);
28328 /* Assume that we are looking at a constructor declarator. */
28329 constructor_p
= true;
28331 /* Look for the optional `::' operator. */
28332 cp_parser_global_scope_opt (parser
,
28333 /*current_scope_valid_p=*/false);
28334 /* Look for the nested-name-specifier. */
28335 nested_name_specifier
28336 = (cp_parser_nested_name_specifier_opt (parser
,
28337 /*typename_keyword_p=*/false,
28338 /*check_dependency_p=*/false,
28340 /*is_declaration=*/false));
28342 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
28343 if (nested_name_specifier
28344 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
28346 tree s
= resolve_typename_type (nested_name_specifier
,
28347 /*only_current_p=*/false);
28348 if (TREE_CODE (s
) != TYPENAME_TYPE
)
28349 nested_name_specifier
= s
;
28352 outside_class_specifier_p
= (!at_class_scope_p ()
28353 || !TYPE_BEING_DEFINED (current_class_type
)
28356 /* Outside of a class-specifier, there must be a
28357 nested-name-specifier. Except in C++17 mode, where we
28358 might be declaring a guiding declaration. */
28359 if (!nested_name_specifier
&& outside_class_specifier_p
28360 && cxx_dialect
< cxx17
)
28361 constructor_p
= false;
28362 else if (nested_name_specifier
== error_mark_node
)
28363 constructor_p
= false;
28365 /* If we have a class scope, this is easy; DR 147 says that S::S always
28366 names the constructor, and no other qualified name could. */
28367 if (constructor_p
&& nested_name_specifier
28368 && CLASS_TYPE_P (nested_name_specifier
))
28370 tree id
= cp_parser_unqualified_id (parser
,
28371 /*template_keyword_p=*/false,
28372 /*check_dependency_p=*/false,
28373 /*declarator_p=*/true,
28374 /*optional_p=*/false);
28375 if (is_overloaded_fn (id
))
28376 id
= DECL_NAME (get_first_fn (id
));
28377 if (!constructor_name_p (id
, nested_name_specifier
))
28378 constructor_p
= false;
28380 /* If we still think that this might be a constructor-declarator,
28381 look for a class-name. */
28382 else if (constructor_p
)
28386 template <typename T> struct S {
28390 we must recognize that the nested `S' names a class. */
28391 if (cxx_dialect
>= cxx17
)
28392 cp_parser_parse_tentatively (parser
);
28395 type_decl
= cp_parser_class_name (parser
,
28396 /*typename_keyword_p=*/false,
28397 /*template_keyword_p=*/false,
28399 /*check_dependency_p=*/false,
28400 /*class_head_p=*/false,
28401 /*is_declaration=*/false);
28403 if (cxx_dialect
>= cxx17
28404 && !cp_parser_parse_definitely (parser
))
28406 type_decl
= NULL_TREE
;
28407 tree tmpl
= cp_parser_template_name (parser
,
28408 /*template_keyword*/false,
28409 /*check_dependency_p*/false,
28410 /*is_declaration*/false,
28412 /*is_identifier*/NULL
);
28413 if (DECL_CLASS_TEMPLATE_P (tmpl
)
28414 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
28415 /* It's a deduction guide, return true. */;
28417 cp_parser_simulate_error (parser
);
28420 /* If there was no class-name, then this is not a constructor.
28421 Otherwise, if we are in a class-specifier and we aren't
28422 handling a friend declaration, check that its type matches
28423 current_class_type (c++/38313). Note: error_mark_node
28424 is left alone for error recovery purposes. */
28425 constructor_p
= (!cp_parser_error_occurred (parser
)
28426 && (outside_class_specifier_p
28427 || type_decl
== NULL_TREE
28428 || type_decl
== error_mark_node
28429 || same_type_p (current_class_type
,
28430 TREE_TYPE (type_decl
))));
28432 /* If we're still considering a constructor, we have to see a `(',
28433 to begin the parameter-declaration-clause, followed by either a
28434 `)', an `...', or a decl-specifier. We need to check for a
28435 type-specifier to avoid being fooled into thinking that:
28439 is a constructor. (It is actually a function named `f' that
28440 takes one parameter (of type `int') and returns a value of type
28443 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
28444 constructor_p
= false;
28447 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
28448 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
28449 /* A parameter declaration begins with a decl-specifier,
28450 which is either the "attribute" keyword, a storage class
28451 specifier, or (usually) a type-specifier. */
28452 && !cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
28453 /* A parameter declaration can also begin with [[attribute]]. */
28454 && !cp_next_tokens_can_be_std_attribute_p (parser
))
28457 tree pushed_scope
= NULL_TREE
;
28458 unsigned saved_num_template_parameter_lists
;
28460 /* Names appearing in the type-specifier should be looked up
28461 in the scope of the class. */
28462 if (current_class_type
)
28464 else if (type_decl
)
28466 type
= TREE_TYPE (type_decl
);
28467 if (TREE_CODE (type
) == TYPENAME_TYPE
)
28469 type
= resolve_typename_type (type
,
28470 /*only_current_p=*/false);
28471 if (TREE_CODE (type
) == TYPENAME_TYPE
)
28473 cp_parser_abort_tentative_parse (parser
);
28477 pushed_scope
= push_scope (type
);
28480 /* Inside the constructor parameter list, surrounding
28481 template-parameter-lists do not apply. */
28482 saved_num_template_parameter_lists
28483 = parser
->num_template_parameter_lists
;
28484 parser
->num_template_parameter_lists
= 0;
28486 /* Look for the type-specifier. It's not optional, but its typename
28487 might be. Unless this is a friend declaration; we don't want to
28490 friend S (T::fn)(int);
28492 as a constructor, but with P0634, we might assume a type when
28493 looking for the type-specifier. It is actually a function named
28494 `T::fn' that takes one parameter (of type `int') and returns a
28495 value of type `S'. Constructors can be friends, but they must
28496 use a qualified name.
28498 Parse with an empty set of declaration specifiers since we're
28499 trying to match a decl-specifier-seq of the first parameter.
28500 This must be non-null so that cp_parser_simple_type_specifier
28501 will recognize a constrained placeholder type such as:
28502 'C<int> auto' where C is a type concept. */
28503 cp_decl_specifier_seq ctor_specs
;
28504 clear_decl_specs (&ctor_specs
);
28505 cp_parser_type_specifier (parser
,
28506 (friend_p
? CP_PARSER_FLAGS_NONE
28507 : (flags
& ~CP_PARSER_FLAGS_OPTIONAL
)),
28508 /*decl_specs=*/&ctor_specs
,
28509 /*is_declarator=*/true,
28510 /*declares_class_or_enum=*/NULL
,
28511 /*is_cv_qualifier=*/NULL
);
28513 parser
->num_template_parameter_lists
28514 = saved_num_template_parameter_lists
;
28516 /* Leave the scope of the class. */
28518 pop_scope (pushed_scope
);
28520 constructor_p
= !cp_parser_error_occurred (parser
);
28524 /* We did not really want to consume any tokens. */
28525 cp_parser_abort_tentative_parse (parser
);
28527 return constructor_p
;
28530 /* Parse the definition of the function given by the DECL_SPECIFIERS,
28531 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
28532 they must be performed once we are in the scope of the function.
28534 Returns the function defined. */
28537 cp_parser_function_definition_from_specifiers_and_declarator
28538 (cp_parser
* parser
,
28539 cp_decl_specifier_seq
*decl_specifiers
,
28541 const cp_declarator
*declarator
)
28546 /* Begin the function-definition. */
28547 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
28549 /* The things we're about to see are not directly qualified by any
28550 template headers we've seen thus far. */
28551 reset_specialization ();
28553 /* If there were names looked up in the decl-specifier-seq that we
28554 did not check, check them now. We must wait until we are in the
28555 scope of the function to perform the checks, since the function
28556 might be a friend. */
28557 perform_deferred_access_checks (tf_warning_or_error
);
28561 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
28562 parser
->omp_declare_simd
= NULL
;
28563 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
28564 parser
->oacc_routine
= NULL
;
28569 /* Skip the entire function. */
28570 cp_parser_skip_to_end_of_block_or_statement (parser
);
28571 fn
= error_mark_node
;
28573 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
28575 /* Seen already, skip it. An error message has already been output. */
28576 cp_parser_skip_to_end_of_block_or_statement (parser
);
28577 fn
= current_function_decl
;
28578 current_function_decl
= NULL_TREE
;
28579 /* If this is a function from a class, pop the nested class. */
28580 if (current_class_name
)
28581 pop_nested_class ();
28586 if (DECL_DECLARED_INLINE_P (current_function_decl
))
28587 tv
= TV_PARSE_INLINE
;
28589 tv
= TV_PARSE_FUNC
;
28591 fn
= cp_parser_function_definition_after_declarator (parser
,
28592 /*inline_p=*/false);
28599 /* Parse the part of a function-definition that follows the
28600 declarator. INLINE_P is TRUE iff this function is an inline
28601 function defined within a class-specifier.
28603 Returns the function defined. */
28606 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
28610 bool saved_in_unbraced_linkage_specification_p
;
28611 bool saved_in_function_body
;
28612 unsigned saved_num_template_parameter_lists
;
28614 bool fully_implicit_function_template_p
28615 = parser
->fully_implicit_function_template_p
;
28616 parser
->fully_implicit_function_template_p
= false;
28617 tree implicit_template_parms
28618 = parser
->implicit_template_parms
;
28619 parser
->implicit_template_parms
= 0;
28620 cp_binding_level
* implicit_template_scope
28621 = parser
->implicit_template_scope
;
28622 parser
->implicit_template_scope
= 0;
28624 saved_in_function_body
= parser
->in_function_body
;
28625 parser
->in_function_body
= true;
28626 /* If the next token is `return', then the code may be trying to
28627 make use of the "named return value" extension that G++ used to
28629 token
= cp_lexer_peek_token (parser
->lexer
);
28630 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
28632 /* Consume the `return' keyword. */
28633 cp_lexer_consume_token (parser
->lexer
);
28634 /* Look for the identifier that indicates what value is to be
28636 cp_parser_identifier (parser
);
28637 /* Issue an error message. */
28638 error_at (token
->location
,
28639 "named return values are no longer supported");
28640 /* Skip tokens until we reach the start of the function body. */
28643 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28644 if (token
->type
== CPP_OPEN_BRACE
28645 || token
->type
== CPP_EOF
28646 || token
->type
== CPP_PRAGMA_EOL
)
28648 cp_lexer_consume_token (parser
->lexer
);
28651 /* The `extern' in `extern "C" void f () { ... }' does not apply to
28652 anything declared inside `f'. */
28653 saved_in_unbraced_linkage_specification_p
28654 = parser
->in_unbraced_linkage_specification_p
;
28655 parser
->in_unbraced_linkage_specification_p
= false;
28656 /* Inside the function, surrounding template-parameter-lists do not
28658 saved_num_template_parameter_lists
28659 = parser
->num_template_parameter_lists
;
28660 parser
->num_template_parameter_lists
= 0;
28662 /* If the next token is `try', `__transaction_atomic', or
28663 `__transaction_relaxed`, then we are looking at either function-try-block
28664 or function-transaction-block. Note that all of these include the
28666 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
28667 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
28668 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
28669 RID_TRANSACTION_RELAXED
))
28670 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
28671 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
28672 cp_parser_function_try_block (parser
);
28674 cp_parser_ctor_initializer_opt_and_function_body
28675 (parser
, /*in_function_try_block=*/false);
28677 /* Finish the function. */
28678 fn
= finish_function (inline_p
);
28679 /* Generate code for it, if necessary. */
28680 expand_or_defer_fn (fn
);
28681 /* Restore the saved values. */
28682 parser
->in_unbraced_linkage_specification_p
28683 = saved_in_unbraced_linkage_specification_p
;
28684 parser
->num_template_parameter_lists
28685 = saved_num_template_parameter_lists
;
28686 parser
->in_function_body
= saved_in_function_body
;
28688 parser
->fully_implicit_function_template_p
28689 = fully_implicit_function_template_p
;
28690 parser
->implicit_template_parms
28691 = implicit_template_parms
;
28692 parser
->implicit_template_scope
28693 = implicit_template_scope
;
28695 if (parser
->fully_implicit_function_template_p
)
28696 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
28701 /* Parse a template-declaration body (following argument list). */
28704 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
28705 tree parameter_list
,
28708 tree decl
= NULL_TREE
;
28709 bool friend_p
= false;
28711 /* We just processed one more parameter list. */
28712 ++parser
->num_template_parameter_lists
;
28714 /* Get the deferred access checks from the parameter list. These
28715 will be checked once we know what is being declared, as for a
28716 member template the checks must be performed in the scope of the
28717 class containing the member. */
28718 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
28720 /* Tentatively parse for a new template parameter list, which can either be
28721 the template keyword or a template introduction. */
28722 if (cp_parser_template_declaration_after_export (parser
, member_p
))
28724 else if (cxx_dialect
>= cxx11
28725 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
28726 decl
= cp_parser_alias_declaration (parser
);
28727 else if (cxx_dialect
>= cxx2a
/* Implies flag_concept. */
28728 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
)
28729 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_BOOL
))
28730 /* Allow 'concept bool' to be handled as per the TS. */
28731 decl
= cp_parser_concept_definition (parser
);
28734 /* There are no access checks when parsing a template, as we do not
28735 know if a specialization will be a friend. */
28736 push_deferring_access_checks (dk_no_check
);
28737 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28738 decl
= cp_parser_single_declaration (parser
,
28741 /*explicit_specialization_p=*/false,
28743 pop_deferring_access_checks ();
28745 /* If this is a member template declaration, let the front
28747 if (member_p
&& !friend_p
&& decl
)
28749 if (TREE_CODE (decl
) == TYPE_DECL
)
28750 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
28752 decl
= finish_member_template_decl (decl
);
28754 else if (friend_p
&& decl
28755 && DECL_DECLARES_TYPE_P (decl
))
28756 make_friend_class (current_class_type
, TREE_TYPE (decl
),
28757 /*complain=*/true);
28759 /* We are done with the current parameter list. */
28760 --parser
->num_template_parameter_lists
;
28762 pop_deferring_access_checks ();
28765 finish_template_decl (parameter_list
);
28767 /* Check the template arguments for a literal operator template. */
28769 && DECL_DECLARES_FUNCTION_P (decl
)
28770 && UDLIT_OPER_P (DECL_NAME (decl
)))
28773 if (parameter_list
== NULL_TREE
)
28777 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
28778 if (num_parms
== 1)
28780 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
28781 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
28782 if (TREE_CODE (parm
) != PARM_DECL
)
28784 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
))
28785 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
28786 /* OK, C++20 string literal operator template. We don't need
28787 to warn in lower dialects here because we will have already
28788 warned about the template parameter. */;
28789 else if (TREE_TYPE (parm
) != char_type_node
28790 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
28793 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
28795 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
28796 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
28797 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
28798 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
28799 if (TREE_CODE (parm
) != PARM_DECL
28800 || TREE_TYPE (parm
) != TREE_TYPE (type
)
28801 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
28804 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
28805 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
28806 "ISO C++ did not adopt string literal operator templa"
28807 "tes taking an argument pack of characters");
28814 if (cxx_dialect
> cxx17
)
28815 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
28816 "template %qD has invalid parameter list; expected "
28817 "non-type template parameter pack %<<char...>%> or "
28818 "single non-type parameter of class type",
28821 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
28822 "template %qD has invalid parameter list; expected "
28823 "non-type template parameter pack %<<char...>%>",
28828 /* Register member declarations. */
28829 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
28830 finish_member_declaration (decl
);
28831 /* If DECL is a function template, we must return to parse it later.
28832 (Even though there is no definition, there might be default
28833 arguments that need handling.) */
28834 if (member_p
&& decl
28835 && DECL_DECLARES_FUNCTION_P (decl
))
28836 vec_safe_push (unparsed_funs_with_definitions
, decl
);
28839 /* Parse a template introduction header for a template-declaration. Returns
28840 false if tentative parse fails. */
28843 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
28845 cp_parser_parse_tentatively (parser
);
28847 tree saved_scope
= parser
->scope
;
28848 tree saved_object_scope
= parser
->object_scope
;
28849 tree saved_qualifying_scope
= parser
->qualifying_scope
;
28851 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
28853 /* Look for the optional `::' operator. */
28854 cp_parser_global_scope_opt (parser
,
28855 /*current_scope_valid_p=*/false);
28856 /* Look for the nested-name-specifier. */
28857 cp_parser_nested_name_specifier_opt (parser
,
28858 /*typename_keyword_p=*/false,
28859 /*check_dependency_p=*/true,
28861 /*is_declaration=*/false);
28863 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28864 tree concept_name
= cp_parser_identifier (parser
);
28866 /* Look up the concept for which we will be matching
28867 template parameters. */
28868 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
28870 parser
->scope
= saved_scope
;
28871 parser
->object_scope
= saved_object_scope
;
28872 parser
->qualifying_scope
= saved_qualifying_scope
;
28874 if (concept_name
== error_mark_node
28875 || (seen_error () && !concept_definition_p (tmpl_decl
)))
28876 cp_parser_simulate_error (parser
);
28878 /* Look for opening brace for introduction. */
28879 matching_braces braces
;
28880 braces
.require_open (parser
);
28881 location_t open_loc
= input_location
;
28883 if (!cp_parser_parse_definitely (parser
))
28886 push_deferring_access_checks (dk_deferred
);
28888 /* Build vector of placeholder parameters and grab
28889 matching identifiers. */
28890 tree introduction_list
= cp_parser_introduction_list (parser
);
28892 /* Look for closing brace for introduction. */
28893 if (!braces
.require_close (parser
))
28896 /* The introduction-list shall not be empty. */
28897 int nargs
= TREE_VEC_LENGTH (introduction_list
);
28900 /* In cp_parser_introduction_list we have already issued an error. */
28904 if (tmpl_decl
== error_mark_node
)
28906 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
28911 /* Build and associate the constraint. */
28912 location_t introduction_loc
= make_location (open_loc
,
28913 start_token
->location
,
28915 tree parms
= finish_template_introduction (tmpl_decl
,
28918 if (parms
&& parms
!= error_mark_node
)
28920 if (!flag_concepts_ts
)
28921 pedwarn (introduction_loc
, 0, "template-introductions"
28922 " are not part of C++20 concepts [-fconcepts-ts]");
28924 cp_parser_template_declaration_after_parameters (parser
, parms
,
28929 if (parms
== NULL_TREE
)
28930 error_at (token
->location
, "no matching concept for template-introduction");
28935 /* Parse a normal template-declaration following the template keyword. */
28938 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
28940 tree parameter_list
;
28941 bool need_lang_pop
;
28942 location_t location
= input_location
;
28944 /* Look for the `<' token. */
28945 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
28947 if (at_class_scope_p () && current_function_decl
)
28949 /* 14.5.2.2 [temp.mem]
28951 A local class shall not have member templates. */
28952 error_at (location
,
28953 "invalid declaration of member template in local class");
28954 cp_parser_skip_to_end_of_block_or_statement (parser
);
28959 A template ... shall not have C linkage. */
28960 if (current_lang_name
== lang_name_c
)
28962 error_at (location
, "template with C linkage");
28963 maybe_show_extern_c_location ();
28964 /* Give it C++ linkage to avoid confusing other parts of the
28966 push_lang_context (lang_name_cplusplus
);
28967 need_lang_pop
= true;
28970 need_lang_pop
= false;
28972 /* We cannot perform access checks on the template parameter
28973 declarations until we know what is being declared, just as we
28974 cannot check the decl-specifier list. */
28975 push_deferring_access_checks (dk_deferred
);
28977 /* If the next token is `>', then we have an invalid
28978 specialization. Rather than complain about an invalid template
28979 parameter, issue an error message here. */
28980 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
28982 cp_parser_error (parser
, "invalid explicit specialization");
28983 begin_specialization ();
28984 parameter_list
= NULL_TREE
;
28988 /* Parse the template parameters. */
28989 parameter_list
= cp_parser_template_parameter_list (parser
);
28992 /* Look for the `>'. */
28993 cp_parser_skip_to_end_of_template_parameter_list (parser
);
28995 /* Manage template requirements */
28998 tree reqs
= get_shorthand_constraints (current_template_parms
);
28999 if (tree treqs
= cp_parser_requires_clause_opt (parser
, false))
29000 reqs
= combine_constraint_expressions (reqs
, treqs
);
29001 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
29004 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
29007 /* For the erroneous case of a template with C linkage, we pushed an
29008 implicit C++ linkage scope; exit that scope now. */
29010 pop_lang_context ();
29013 /* Parse a template-declaration, assuming that the `export' (and
29014 `extern') keywords, if present, has already been scanned. MEMBER_P
29015 is as for cp_parser_template_declaration. */
29018 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
29020 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
29022 cp_lexer_consume_token (parser
->lexer
);
29023 cp_parser_explicit_template_declaration (parser
, member_p
);
29026 else if (flag_concepts
)
29027 return cp_parser_template_introduction (parser
, member_p
);
29032 /* Perform the deferred access checks from a template-parameter-list.
29033 CHECKS is a TREE_LIST of access checks, as returned by
29034 get_deferred_access_checks. */
29037 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
29039 ++processing_template_parmlist
;
29040 perform_access_checks (checks
, tf_warning_or_error
);
29041 --processing_template_parmlist
;
29044 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
29045 `function-definition' sequence that follows a template header.
29046 If MEMBER_P is true, this declaration appears in a class scope.
29048 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
29049 *FRIEND_P is set to TRUE iff the declaration is a friend. */
29052 cp_parser_single_declaration (cp_parser
* parser
,
29053 vec
<deferred_access_check
, va_gc
> *checks
,
29055 bool explicit_specialization_p
,
29058 int declares_class_or_enum
;
29059 tree decl
= NULL_TREE
;
29060 cp_decl_specifier_seq decl_specifiers
;
29061 bool function_definition_p
= false;
29062 cp_token
*decl_spec_token_start
;
29064 /* This function is only used when processing a template
29066 gcc_assert (innermost_scope_kind () == sk_template_parms
29067 || innermost_scope_kind () == sk_template_spec
);
29069 /* Defer access checks until we know what is being declared. */
29070 push_deferring_access_checks (dk_deferred
);
29072 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
29074 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
29075 cp_parser_decl_specifier_seq (parser
,
29076 (CP_PARSER_FLAGS_OPTIONAL
29077 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
29079 &declares_class_or_enum
);
29081 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
29083 /* There are no template typedefs. */
29084 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
29086 error_at (decl_spec_token_start
->location
,
29087 "template declaration of %<typedef%>");
29088 decl
= error_mark_node
;
29091 /* Gather up the access checks that occurred the
29092 decl-specifier-seq. */
29093 stop_deferring_access_checks ();
29095 /* Check for the declaration of a template class. */
29096 if (declares_class_or_enum
)
29098 if (cp_parser_declares_only_class_p (parser
)
29099 || (declares_class_or_enum
& 2))
29101 /* If this is a declaration, but not a definition, associate
29102 any constraints with the type declaration. Constraints
29103 are associated with definitions in cp_parser_class_specifier. */
29104 if (declares_class_or_enum
== 1)
29105 associate_classtype_constraints (decl_specifiers
.type
);
29107 decl
= shadow_tag (&decl_specifiers
);
29112 friend template <typename T> struct A<T>::B;
29115 A<T>::B will be represented by a TYPENAME_TYPE, and
29116 therefore not recognized by shadow_tag. */
29117 if (friend_p
&& *friend_p
29119 && decl_specifiers
.type
29120 && TYPE_P (decl_specifiers
.type
))
29121 decl
= decl_specifiers
.type
;
29123 if (decl
&& decl
!= error_mark_node
)
29124 decl
= TYPE_NAME (decl
);
29126 decl
= error_mark_node
;
29128 /* Perform access checks for template parameters. */
29129 cp_parser_perform_template_parameter_access_checks (checks
);
29131 /* Give a helpful diagnostic for
29132 template <class T> struct A { } a;
29133 if we aren't already recovering from an error. */
29134 if (!cp_parser_declares_only_class_p (parser
)
29137 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
29138 "a class template declaration must not declare "
29140 cp_parser_skip_to_end_of_block_or_statement (parser
);
29146 /* Complain about missing 'typename' or other invalid type names. */
29147 if (!decl_specifiers
.any_type_specifiers_p
29148 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
29150 /* cp_parser_parse_and_diagnose_invalid_type_name calls
29151 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
29152 the rest of this declaration. */
29153 decl
= error_mark_node
;
29157 /* If it's not a template class, try for a template function. If
29158 the next token is a `;', then this declaration does not declare
29159 anything. But, if there were errors in the decl-specifiers, then
29160 the error might well have come from an attempted class-specifier.
29161 In that case, there's no need to warn about a missing declarator. */
29163 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
29164 || decl_specifiers
.type
!= error_mark_node
))
29166 decl
= cp_parser_init_declarator (parser
,
29167 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
29170 /*function_definition_allowed_p=*/true,
29172 declares_class_or_enum
,
29173 &function_definition_p
,
29176 /* 7.1.1-1 [dcl.stc]
29178 A storage-class-specifier shall not be specified in an explicit
29179 specialization... */
29181 && explicit_specialization_p
29182 && decl_specifiers
.storage_class
!= sc_none
)
29184 error_at (decl_spec_token_start
->location
,
29185 "explicit template specialization cannot have a storage class");
29186 decl
= error_mark_node
;
29189 if (decl
&& VAR_P (decl
))
29190 check_template_variable (decl
);
29193 /* Look for a trailing `;' after the declaration. */
29194 if (!function_definition_p
29195 && (decl
== error_mark_node
29196 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
29197 cp_parser_skip_to_end_of_block_or_statement (parser
);
29200 pop_deferring_access_checks ();
29202 /* Clear any current qualification; whatever comes next is the start
29203 of something new. */
29204 parser
->scope
= NULL_TREE
;
29205 parser
->qualifying_scope
= NULL_TREE
;
29206 parser
->object_scope
= NULL_TREE
;
29211 /* Parse a cast-expression that is not the operand of a unary "&". */
29214 cp_parser_simple_cast_expression (cp_parser
*parser
)
29216 return cp_parser_cast_expression (parser
, /*address_p=*/false,
29217 /*cast_p=*/false, /*decltype*/false, NULL
);
29220 /* Parse a functional cast to TYPE. Returns an expression
29221 representing the cast. */
29224 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
29226 vec
<tree
, va_gc
> *vec
;
29227 tree expression_list
;
29231 location_t start_loc
= input_location
;
29234 type
= error_mark_node
;
29236 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
29238 cp_lexer_set_source_position (parser
->lexer
);
29239 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
29240 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
29241 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
29242 if (TREE_CODE (type
) == TYPE_DECL
)
29243 type
= TREE_TYPE (type
);
29245 cast
= finish_compound_literal (type
, expression_list
,
29246 tf_warning_or_error
, fcl_functional
);
29247 /* Create a location of the form:
29250 with caret == start at the start of the type name,
29251 finishing at the closing brace. */
29252 location_t combined_loc
= make_location (start_loc
, start_loc
,
29254 cast
.set_location (combined_loc
);
29259 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
29261 /*allow_expansion_p=*/true,
29262 /*non_constant_p=*/NULL
);
29264 expression_list
= error_mark_node
;
29267 expression_list
= build_tree_list_vec (vec
);
29268 release_tree_vector (vec
);
29271 /* Create a location of the form:
29274 with caret == start at the start of the type name,
29275 finishing at the closing paren. */
29276 location_t combined_loc
= make_location (start_loc
, start_loc
,
29278 cast
= build_functional_cast (combined_loc
, type
, expression_list
,
29279 tf_warning_or_error
);
29280 cast
.set_location (combined_loc
);
29282 /* [expr.const]/1: In an integral constant expression "only type
29283 conversions to integral or enumeration type can be used". */
29284 if (TREE_CODE (type
) == TYPE_DECL
)
29285 type
= TREE_TYPE (type
);
29286 if (cast
!= error_mark_node
29287 && !cast_valid_in_integral_constant_expression_p (type
)
29288 && cp_parser_non_integral_constant_expression (parser
,
29290 return error_mark_node
;
29295 /* Save the tokens that make up the body of a member function defined
29296 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
29297 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
29298 specifiers applied to the declaration. Returns the FUNCTION_DECL
29299 for the member function. */
29302 cp_parser_save_member_function_body (cp_parser
* parser
,
29303 cp_decl_specifier_seq
*decl_specifiers
,
29304 cp_declarator
*declarator
,
29310 bool function_try_block
= false;
29312 /* Create the FUNCTION_DECL. */
29313 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
29314 cp_finalize_omp_declare_simd (parser
, fn
);
29315 cp_finalize_oacc_routine (parser
, fn
, true);
29316 /* If something went badly wrong, bail out now. */
29317 if (fn
== error_mark_node
)
29319 /* If there's a function-body, skip it. */
29320 if (cp_parser_token_starts_function_definition_p
29321 (cp_lexer_peek_token (parser
->lexer
)))
29322 cp_parser_skip_to_end_of_block_or_statement (parser
);
29323 return error_mark_node
;
29326 /* Remember it, if there are default args to post process. */
29327 cp_parser_save_default_args (parser
, fn
);
29329 /* Save away the tokens that make up the body of the
29331 first
= parser
->lexer
->next_token
;
29333 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
29334 cp_lexer_consume_token (parser
->lexer
);
29335 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
29336 RID_TRANSACTION_ATOMIC
))
29338 cp_lexer_consume_token (parser
->lexer
);
29339 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
29340 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
29341 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
29342 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
29343 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
29344 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
29345 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
29347 cp_lexer_consume_token (parser
->lexer
);
29348 cp_lexer_consume_token (parser
->lexer
);
29349 cp_lexer_consume_token (parser
->lexer
);
29350 cp_lexer_consume_token (parser
->lexer
);
29351 cp_lexer_consume_token (parser
->lexer
);
29354 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
29355 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
29357 cp_lexer_consume_token (parser
->lexer
);
29358 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
29363 /* Handle function try blocks. */
29364 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
29366 cp_lexer_consume_token (parser
->lexer
);
29367 function_try_block
= true;
29369 /* We can have braced-init-list mem-initializers before the fn body. */
29370 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
29372 cp_lexer_consume_token (parser
->lexer
);
29373 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
29375 /* cache_group will stop after an un-nested { } pair, too. */
29376 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
29379 /* variadic mem-inits have ... after the ')'. */
29380 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29381 cp_lexer_consume_token (parser
->lexer
);
29384 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29385 /* Handle function try blocks. */
29386 if (function_try_block
)
29387 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
29388 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
29389 last
= parser
->lexer
->next_token
;
29391 /* Save away the inline definition; we will process it when the
29392 class is complete. */
29393 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
29394 DECL_PENDING_INLINE_P (fn
) = 1;
29396 /* We need to know that this was defined in the class, so that
29397 friend templates are handled correctly. */
29398 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
29400 /* Add FN to the queue of functions to be parsed later. */
29401 vec_safe_push (unparsed_funs_with_definitions
, fn
);
29406 /* Save the tokens that make up the in-class initializer for a non-static
29407 data member. Returns a DEFERRED_PARSE. */
29410 cp_parser_save_nsdmi (cp_parser
* parser
)
29412 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
29415 /* Parse a template-argument-list, as well as the trailing ">" (but
29416 not the opening "<"). See cp_parser_template_argument_list for the
29420 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
29424 tree saved_qualifying_scope
;
29425 tree saved_object_scope
;
29426 bool saved_greater_than_is_operator_p
;
29430 When parsing a template-id, the first non-nested `>' is taken as
29431 the end of the template-argument-list rather than a greater-than
29433 saved_greater_than_is_operator_p
29434 = parser
->greater_than_is_operator_p
;
29435 parser
->greater_than_is_operator_p
= false;
29436 /* Parsing the argument list may modify SCOPE, so we save it
29438 saved_scope
= parser
->scope
;
29439 saved_qualifying_scope
= parser
->qualifying_scope
;
29440 saved_object_scope
= parser
->object_scope
;
29441 /* We need to evaluate the template arguments, even though this
29442 template-id may be nested within a "sizeof". */
29444 /* Parse the template-argument-list itself. */
29445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
29446 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
29447 arguments
= NULL_TREE
;
29449 arguments
= cp_parser_template_argument_list (parser
);
29450 /* Look for the `>' that ends the template-argument-list. If we find
29451 a '>>' instead, it's probably just a typo. */
29452 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
29454 if (cxx_dialect
!= cxx98
)
29456 /* In C++0x, a `>>' in a template argument list or cast
29457 expression is considered to be two separate `>'
29458 tokens. So, change the current token to a `>', but don't
29459 consume it: it will be consumed later when the outer
29460 template argument list (or cast expression) is parsed.
29461 Note that this replacement of `>' for `>>' is necessary
29462 even if we are parsing tentatively: in the tentative
29463 case, after calling
29464 cp_parser_enclosed_template_argument_list we will always
29465 throw away all of the template arguments and the first
29466 closing `>', either because the template argument list
29467 was erroneous or because we are replacing those tokens
29468 with a CPP_TEMPLATE_ID token. The second `>' (which will
29469 not have been thrown away) is needed either to close an
29470 outer template argument list or to complete a new-style
29472 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29473 token
->type
= CPP_GREATER
;
29475 else if (!saved_greater_than_is_operator_p
)
29477 /* If we're in a nested template argument list, the '>>' has
29478 to be a typo for '> >'. We emit the error message, but we
29479 continue parsing and we push a '>' as next token, so that
29480 the argument list will be parsed correctly. Note that the
29481 global source location is still on the token before the
29482 '>>', so we need to say explicitly where we want it. */
29483 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29484 gcc_rich_location
richloc (token
->location
);
29485 richloc
.add_fixit_replace ("> >");
29486 error_at (&richloc
, "%<>>%> should be %<> >%> "
29487 "within a nested template argument list");
29489 token
->type
= CPP_GREATER
;
29493 /* If this is not a nested template argument list, the '>>'
29494 is a typo for '>'. Emit an error message and continue.
29495 Same deal about the token location, but here we can get it
29496 right by consuming the '>>' before issuing the diagnostic. */
29497 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
29498 error_at (token
->location
,
29499 "spurious %<>>%>, use %<>%> to terminate "
29500 "a template argument list");
29504 cp_parser_skip_to_end_of_template_parameter_list (parser
);
29505 /* The `>' token might be a greater-than operator again now. */
29506 parser
->greater_than_is_operator_p
29507 = saved_greater_than_is_operator_p
;
29508 /* Restore the SAVED_SCOPE. */
29509 parser
->scope
= saved_scope
;
29510 parser
->qualifying_scope
= saved_qualifying_scope
;
29511 parser
->object_scope
= saved_object_scope
;
29516 /* MEMBER_FUNCTION is a member function, or a friend. If default
29517 arguments, or the body of the function have not yet been parsed,
29521 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
29523 timevar_push (TV_PARSE_INMETH
);
29524 /* If this member is a template, get the underlying
29526 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
29527 member_function
= DECL_TEMPLATE_RESULT (member_function
);
29529 /* There should not be any class definitions in progress at this
29530 point; the bodies of members are only parsed outside of all class
29532 gcc_assert (parser
->num_classes_being_defined
== 0);
29533 /* While we're parsing the member functions we might encounter more
29534 classes. We want to handle them right away, but we don't want
29535 them getting mixed up with functions that are currently in the
29537 push_unparsed_function_queues (parser
);
29539 /* Make sure that any template parameters are in scope. */
29540 maybe_begin_member_template_processing (member_function
);
29542 /* If the body of the function has not yet been parsed, parse it
29544 if (DECL_PENDING_INLINE_P (member_function
))
29546 tree function_scope
;
29547 cp_token_cache
*tokens
;
29549 /* The function is no longer pending; we are processing it. */
29550 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
29551 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
29552 DECL_PENDING_INLINE_P (member_function
) = 0;
29554 /* If this is a local class, enter the scope of the containing
29556 function_scope
= current_function_decl
;
29557 if (function_scope
)
29558 push_function_context ();
29560 /* Push the body of the function onto the lexer stack. */
29561 cp_parser_push_lexer_for_tokens (parser
, tokens
);
29563 /* Let the front end know that we going to be defining this
29565 start_preparsed_function (member_function
, NULL_TREE
,
29566 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
29568 /* Don't do access checking if it is a templated function. */
29569 if (processing_template_decl
)
29570 push_deferring_access_checks (dk_no_check
);
29572 /* #pragma omp declare reduction needs special parsing. */
29573 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
29575 parser
->lexer
->in_pragma
= true;
29576 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
29577 finish_function (/*inline_p=*/true);
29578 cp_check_omp_declare_reduction (member_function
);
29581 /* Now, parse the body of the function. */
29582 cp_parser_function_definition_after_declarator (parser
,
29583 /*inline_p=*/true);
29585 if (processing_template_decl
)
29586 pop_deferring_access_checks ();
29588 /* Leave the scope of the containing function. */
29589 if (function_scope
)
29590 pop_function_context ();
29591 cp_parser_pop_lexer (parser
);
29594 /* Remove any template parameters from the symbol table. */
29595 maybe_end_member_template_processing ();
29597 /* Restore the queue. */
29598 pop_unparsed_function_queues (parser
);
29599 timevar_pop (TV_PARSE_INMETH
);
29602 /* If DECL contains any default args, remember it on the unparsed
29603 functions queue. */
29606 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
29610 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
29612 probe
= TREE_CHAIN (probe
))
29613 if (TREE_PURPOSE (probe
))
29615 cp_default_arg_entry entry
= {current_class_type
, decl
};
29616 vec_safe_push (unparsed_funs_with_default_args
, entry
);
29620 /* Remember if there is a noexcept-specifier to post process. */
29621 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
29622 if (UNPARSED_NOEXCEPT_SPEC_P (spec
))
29623 vec_safe_push (unparsed_noexcepts
, decl
);
29626 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
29627 which is either a FIELD_DECL or PARM_DECL. Parse it and return
29628 the result. For a PARM_DECL, PARMTYPE is the corresponding type
29629 from the parameter-type-list. */
29632 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
29633 tree default_arg
, tree parmtype
)
29635 cp_token_cache
*tokens
;
29639 if (default_arg
== error_mark_node
)
29640 return error_mark_node
;
29642 /* Push the saved tokens for the default argument onto the parser's
29644 tokens
= DEFPARSE_TOKENS (default_arg
);
29645 cp_parser_push_lexer_for_tokens (parser
, tokens
);
29647 start_lambda_scope (decl
);
29649 /* Parse the default argument. */
29650 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
29651 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
29652 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
29654 finish_lambda_scope ();
29656 if (parsed_arg
== error_mark_node
)
29657 cp_parser_skip_to_end_of_statement (parser
);
29659 if (!processing_template_decl
)
29661 /* In a non-template class, check conversions now. In a template,
29662 we'll wait and instantiate these as needed. */
29663 if (TREE_CODE (decl
) == PARM_DECL
)
29664 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
29665 tf_warning_or_error
);
29666 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
29667 parsed_arg
= error_mark_node
;
29669 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
29672 /* If the token stream has not been completely used up, then
29673 there was extra junk after the end of the default
29675 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
29677 if (TREE_CODE (decl
) == PARM_DECL
)
29678 cp_parser_error (parser
, "expected %<,%>");
29680 cp_parser_error (parser
, "expected %<;%>");
29683 /* Revert to the main lexer. */
29684 cp_parser_pop_lexer (parser
);
29689 /* FIELD is a non-static data member with an initializer which we saved for
29690 later; parse it now. */
29693 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
29697 maybe_begin_member_template_processing (field
);
29699 push_unparsed_function_queues (parser
);
29700 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
29701 DECL_INITIAL (field
),
29703 pop_unparsed_function_queues (parser
);
29705 maybe_end_member_template_processing ();
29707 DECL_INITIAL (field
) = def
;
29710 /* FN is a FUNCTION_DECL which may contains a parameter with an
29711 unparsed DEFERRED_PARSE. Parse the default args now. This function
29712 assumes that the current scope is the scope in which the default
29713 argument should be processed. */
29716 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
29718 unsigned char saved_local_variables_forbidden_p
;
29719 tree parm
, parmdecl
;
29721 /* While we're parsing the default args, we might (due to the
29722 statement expression extension) encounter more classes. We want
29723 to handle them right away, but we don't want them getting mixed
29724 up with default args that are currently in the queue. */
29725 push_unparsed_function_queues (parser
);
29727 /* Local variable names (and the `this' keyword) may not appear
29728 in a default argument. */
29729 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
29730 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
29732 push_defarg_context (fn
);
29734 for (parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
)),
29735 parmdecl
= DECL_ARGUMENTS (fn
);
29736 parm
&& parm
!= void_list_node
;
29737 parm
= TREE_CHAIN (parm
),
29738 parmdecl
= DECL_CHAIN (parmdecl
))
29740 tree default_arg
= TREE_PURPOSE (parm
);
29742 vec
<tree
, va_gc
> *insts
;
29749 if (TREE_CODE (default_arg
) != DEFERRED_PARSE
)
29750 /* This can happen for a friend declaration for a function
29751 already declared with default arguments. */
29755 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
29757 TREE_VALUE (parm
));
29758 TREE_PURPOSE (parm
) = parsed_arg
;
29760 /* Update any instantiations we've already created. */
29761 for (insts
= DEFPARSE_INSTANTIATIONS (default_arg
), ix
= 0;
29762 vec_safe_iterate (insts
, ix
, ©
); ix
++)
29763 TREE_PURPOSE (copy
) = parsed_arg
;
29766 pop_defarg_context ();
29768 /* Make sure no default arg is missing. */
29769 check_default_args (fn
);
29771 /* Restore the state of local_variables_forbidden_p. */
29772 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
29774 /* Restore the queue. */
29775 pop_unparsed_function_queues (parser
);
29778 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
29780 sizeof ... ( identifier )
29782 where the 'sizeof' token has already been consumed. */
29785 cp_parser_sizeof_pack (cp_parser
*parser
)
29787 /* Consume the `...'. */
29788 cp_lexer_consume_token (parser
->lexer
);
29789 maybe_warn_variadic_templates ();
29791 matching_parens parens
;
29792 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
29794 parens
.consume_open (parser
);
29796 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
29797 "%<sizeof...%> argument must be surrounded by parentheses");
29799 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29800 tree name
= cp_parser_identifier (parser
);
29801 if (name
== error_mark_node
)
29802 return error_mark_node
;
29803 /* The name is not qualified. */
29804 parser
->scope
= NULL_TREE
;
29805 parser
->qualifying_scope
= NULL_TREE
;
29806 parser
->object_scope
= NULL_TREE
;
29807 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
29808 if (expr
== error_mark_node
)
29809 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
29811 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
29812 expr
= TREE_TYPE (expr
);
29813 else if (TREE_CODE (expr
) == CONST_DECL
)
29814 expr
= DECL_INITIAL (expr
);
29815 expr
= make_pack_expansion (expr
);
29816 PACK_EXPANSION_SIZEOF_P (expr
) = true;
29819 parens
.require_close (parser
);
29824 /* Parse the operand of `sizeof' (or a similar operator). Returns
29825 either a TYPE or an expression, depending on the form of the
29826 input. The KEYWORD indicates which kind of expression we have
29830 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
29832 tree expr
= NULL_TREE
;
29833 const char *saved_message
;
29834 const char *saved_message_arg
;
29835 bool saved_integral_constant_expression_p
;
29836 bool saved_non_integral_constant_expression_p
;
29838 /* If it's a `...', then we are computing the length of a parameter
29840 if (keyword
== RID_SIZEOF
29841 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
29842 return cp_parser_sizeof_pack (parser
);
29844 /* Types cannot be defined in a `sizeof' expression. Save away the
29846 saved_message
= parser
->type_definition_forbidden_message
;
29847 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
29848 parser
->type_definition_forbidden_message
29849 = G_("types may not be defined in %qs expressions");
29850 parser
->type_definition_forbidden_message_arg
29851 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
29853 /* The restrictions on constant-expressions do not apply inside
29854 sizeof expressions. */
29855 saved_integral_constant_expression_p
29856 = parser
->integral_constant_expression_p
;
29857 saved_non_integral_constant_expression_p
29858 = parser
->non_integral_constant_expression_p
;
29859 parser
->integral_constant_expression_p
= false;
29861 /* Do not actually evaluate the expression. */
29862 ++cp_unevaluated_operand
;
29863 ++c_inhibit_evaluation_warnings
;
29864 /* If it's a `(', then we might be looking at the type-id
29866 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
29868 tree type
= NULL_TREE
;
29870 tentative_firewall
firewall (parser
);
29872 /* We can't be sure yet whether we're looking at a type-id or an
29874 cp_parser_parse_tentatively (parser
);
29876 matching_parens parens
;
29877 parens
.consume_open (parser
);
29879 /* Note: as a GNU Extension, compound literals are considered
29880 postfix-expressions as they are in C99, so they are valid
29881 arguments to sizeof. See comment in cp_parser_cast_expression
29883 if (cp_parser_compound_literal_p (parser
))
29884 cp_parser_simulate_error (parser
);
29887 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
29888 parser
->in_type_id_in_expr_p
= true;
29889 /* Look for the type-id. */
29890 type
= cp_parser_type_id (parser
);
29891 /* Look for the closing `)'. */
29892 parens
.require_close (parser
);
29893 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
29896 /* If all went well, then we're done. */
29897 if (cp_parser_parse_definitely (parser
))
29901 /* Commit to the tentative_firewall so we get syntax errors. */
29902 cp_parser_commit_to_tentative_parse (parser
);
29904 expr
= cp_parser_unary_expression (parser
);
29908 expr
= cp_parser_unary_expression (parser
);
29910 /* Go back to evaluating expressions. */
29911 --cp_unevaluated_operand
;
29912 --c_inhibit_evaluation_warnings
;
29914 /* And restore the old one. */
29915 parser
->type_definition_forbidden_message
= saved_message
;
29916 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
29917 parser
->integral_constant_expression_p
29918 = saved_integral_constant_expression_p
;
29919 parser
->non_integral_constant_expression_p
29920 = saved_non_integral_constant_expression_p
;
29925 /* If the current declaration has no declarator, return true. */
29928 cp_parser_declares_only_class_p (cp_parser
*parser
)
29930 /* If the next token is a `;' or a `,' then there is no
29932 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
29933 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
29936 /* Update the DECL_SPECS to reflect the storage class indicated by
29940 cp_parser_set_storage_class (cp_parser
*parser
,
29941 cp_decl_specifier_seq
*decl_specs
,
29945 cp_storage_class storage_class
;
29947 if (parser
->in_unbraced_linkage_specification_p
)
29949 error_at (token
->location
, "invalid use of %qD in linkage specification",
29950 ridpointers
[keyword
]);
29953 else if (decl_specs
->storage_class
!= sc_none
)
29955 decl_specs
->conflicting_specifiers_p
= true;
29959 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
29960 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
29961 && decl_specs
->gnu_thread_keyword_p
)
29963 pedwarn (decl_specs
->locations
[ds_thread
], 0,
29964 "%<__thread%> before %qD", ridpointers
[keyword
]);
29970 storage_class
= sc_auto
;
29973 storage_class
= sc_register
;
29976 storage_class
= sc_static
;
29979 storage_class
= sc_extern
;
29982 storage_class
= sc_mutable
;
29985 gcc_unreachable ();
29987 decl_specs
->storage_class
= storage_class
;
29988 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
29990 /* A storage class specifier cannot be applied alongside a typedef
29991 specifier. If there is a typedef specifier present then set
29992 conflicting_specifiers_p which will trigger an error later
29993 on in grokdeclarator. */
29994 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
29995 decl_specs
->conflicting_specifiers_p
= true;
29998 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
29999 is true, the type is a class or enum definition. */
30002 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
30005 bool type_definition_p
)
30007 decl_specs
->any_specifiers_p
= true;
30009 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
30010 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
30011 this is what happened. In system headers, we ignore these
30012 declarations so that G++ can work with system headers that are not
30014 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
30015 && !type_definition_p
30016 && (type_spec
== boolean_type_node
30017 || type_spec
== char8_type_node
30018 || type_spec
== char16_type_node
30019 || type_spec
== char32_type_node
30020 || type_spec
== wchar_type_node
)
30021 && (decl_specs
->type
30022 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
30023 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
30024 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
30025 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
30027 decl_specs
->redefined_builtin_type
= type_spec
;
30028 set_and_check_decl_spec_loc (decl_specs
,
30029 ds_redefined_builtin_type_spec
,
30031 if (!decl_specs
->type
)
30033 decl_specs
->type
= type_spec
;
30034 decl_specs
->type_definition_p
= false;
30035 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
30038 else if (decl_specs
->type
)
30039 decl_specs
->multiple_types_p
= true;
30042 decl_specs
->type
= type_spec
;
30043 decl_specs
->type_definition_p
= type_definition_p
;
30044 decl_specs
->redefined_builtin_type
= NULL_TREE
;
30045 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
30049 /* True iff TOKEN is the GNU keyword __thread. */
30052 token_is__thread (cp_token
*token
)
30054 gcc_assert (token
->keyword
== RID_THREAD
);
30055 return id_equal (token
->u
.value
, "__thread");
30058 /* Set the location for a declarator specifier and check if it is
30061 DECL_SPECS is the sequence of declarator specifiers onto which to
30064 DS is the single declarator specifier to set which location is to
30065 be set onto the existing sequence of declarators.
30067 LOCATION is the location for the declarator specifier to
30071 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
30072 cp_decl_spec ds
, cp_token
*token
)
30074 gcc_assert (ds
< ds_last
);
30076 if (decl_specs
== NULL
)
30079 location_t location
= token
->location
;
30081 if (decl_specs
->locations
[ds
] == 0)
30083 decl_specs
->locations
[ds
] = location
;
30084 if (ds
== ds_thread
)
30085 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
30091 if (decl_specs
->locations
[ds_long_long
] != 0)
30092 error_at (location
,
30093 "%<long long long%> is too long for GCC");
30096 decl_specs
->locations
[ds_long_long
] = location
;
30097 pedwarn_cxx98 (location
,
30099 "ISO C++ 1998 does not support %<long long%>");
30102 else if (ds
== ds_thread
)
30104 bool gnu
= token_is__thread (token
);
30105 gcc_rich_location
richloc (location
);
30106 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
30108 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
30109 error_at (&richloc
,
30110 "both %<__thread%> and %<thread_local%> specified");
30114 richloc
.add_fixit_remove ();
30115 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
30120 static const char *const decl_spec_names
[] = {
30139 gcc_rich_location
richloc (location
);
30140 richloc
.add_fixit_remove ();
30141 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
30146 /* Return true iff the declarator specifier DS is present in the
30147 sequence of declarator specifiers DECL_SPECS. */
30150 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
30153 gcc_assert (ds
< ds_last
);
30155 if (decl_specs
== NULL
)
30158 return decl_specs
->locations
[ds
] != 0;
30161 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
30162 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
30165 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
30167 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
30170 /* Issue an error message indicating that TOKEN_DESC was expected.
30171 If KEYWORD is true, it indicated this function is called by
30172 cp_parser_require_keword and the required token can only be
30173 a indicated keyword.
30175 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30176 within any error as the location of an "opening" token matching
30177 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30178 RT_CLOSE_PAREN). */
30181 cp_parser_required_error (cp_parser
*parser
,
30182 required_token token_desc
,
30184 location_t matching_location
)
30186 if (cp_parser_simulate_error (parser
))
30189 const char *gmsgid
= NULL
;
30190 switch (token_desc
)
30193 gmsgid
= G_("expected %<new%>");
30196 gmsgid
= G_("expected %<delete%>");
30199 gmsgid
= G_("expected %<return%>");
30202 gmsgid
= G_("expected %<while%>");
30205 gmsgid
= G_("expected %<extern%>");
30207 case RT_STATIC_ASSERT
:
30208 gmsgid
= G_("expected %<static_assert%>");
30211 gmsgid
= G_("expected %<decltype%>");
30214 gmsgid
= G_("expected %<operator%>");
30217 gmsgid
= G_("expected %<class%>");
30220 gmsgid
= G_("expected %<template%>");
30223 gmsgid
= G_("expected %<namespace%>");
30226 gmsgid
= G_("expected %<using%>");
30229 gmsgid
= G_("expected %<asm%>");
30232 gmsgid
= G_("expected %<try%>");
30235 gmsgid
= G_("expected %<catch%>");
30238 gmsgid
= G_("expected %<throw%>");
30241 gmsgid
= G_("expected %<auto%>");
30244 gmsgid
= G_("expected %<__label__%>");
30247 gmsgid
= G_("expected %<@try%>");
30249 case RT_AT_SYNCHRONIZED
:
30250 gmsgid
= G_("expected %<@synchronized%>");
30253 gmsgid
= G_("expected %<@throw%>");
30255 case RT_TRANSACTION_ATOMIC
:
30256 gmsgid
= G_("expected %<__transaction_atomic%>");
30258 case RT_TRANSACTION_RELAXED
:
30259 gmsgid
= G_("expected %<__transaction_relaxed%>");
30265 if (!gmsgid
&& !keyword
)
30267 switch (token_desc
)
30270 gmsgid
= G_("expected %<;%>");
30272 case RT_OPEN_PAREN
:
30273 gmsgid
= G_("expected %<(%>");
30275 case RT_CLOSE_BRACE
:
30276 gmsgid
= G_("expected %<}%>");
30278 case RT_OPEN_BRACE
:
30279 gmsgid
= G_("expected %<{%>");
30281 case RT_CLOSE_SQUARE
:
30282 gmsgid
= G_("expected %<]%>");
30284 case RT_OPEN_SQUARE
:
30285 gmsgid
= G_("expected %<[%>");
30288 gmsgid
= G_("expected %<,%>");
30291 gmsgid
= G_("expected %<::%>");
30294 gmsgid
= G_("expected %<<%>");
30297 gmsgid
= G_("expected %<>%>");
30300 gmsgid
= G_("expected %<=%>");
30303 gmsgid
= G_("expected %<...%>");
30306 gmsgid
= G_("expected %<*%>");
30309 gmsgid
= G_("expected %<~%>");
30312 gmsgid
= G_("expected %<:%>");
30314 case RT_COLON_SCOPE
:
30315 gmsgid
= G_("expected %<:%> or %<::%>");
30317 case RT_CLOSE_PAREN
:
30318 gmsgid
= G_("expected %<)%>");
30320 case RT_COMMA_CLOSE_PAREN
:
30321 gmsgid
= G_("expected %<,%> or %<)%>");
30323 case RT_PRAGMA_EOL
:
30324 gmsgid
= G_("expected end of line");
30327 gmsgid
= G_("expected identifier");
30330 gmsgid
= G_("expected selection-statement");
30333 gmsgid
= G_("expected iteration-statement");
30336 gmsgid
= G_("expected jump-statement");
30339 gmsgid
= G_("expected class-key");
30341 case RT_CLASS_TYPENAME_TEMPLATE
:
30342 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
30345 gcc_unreachable ();
30350 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
30354 /* If the next token is of the indicated TYPE, consume it. Otherwise,
30355 issue an error message indicating that TOKEN_DESC was expected.
30357 Returns the token consumed, if the token had the appropriate type.
30358 Otherwise, returns NULL.
30360 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
30361 within any error as the location of an "opening" token matching
30362 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
30363 RT_CLOSE_PAREN). */
30366 cp_parser_require (cp_parser
* parser
,
30367 enum cpp_ttype type
,
30368 required_token token_desc
,
30369 location_t matching_location
)
30371 if (cp_lexer_next_token_is (parser
->lexer
, type
))
30372 return cp_lexer_consume_token (parser
->lexer
);
30375 /* Output the MESSAGE -- unless we're parsing tentatively. */
30376 if (!cp_parser_simulate_error (parser
))
30377 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
30378 matching_location
);
30383 /* An error message is produced if the next token is not '>'.
30384 All further tokens are skipped until the desired token is
30385 found or '{', '}', ';' or an unbalanced ')' or ']'. */
30388 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
30390 /* Current level of '< ... >'. */
30391 unsigned level
= 0;
30392 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
30393 unsigned nesting_depth
= 0;
30395 /* Are we ready, yet? If not, issue error message. */
30396 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
30399 /* Skip tokens until the desired token is found. */
30402 /* Peek at the next token. */
30403 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
30406 if (!nesting_depth
)
30411 if (cxx_dialect
== cxx98
)
30412 /* C++0x views the `>>' operator as two `>' tokens, but
30415 else if (!nesting_depth
&& level
-- == 0)
30417 /* We've hit a `>>' where the first `>' closes the
30418 template argument list, and the second `>' is
30419 spurious. Just consume the `>>' and stop; we've
30420 already produced at least one error. */
30421 cp_lexer_consume_token (parser
->lexer
);
30424 /* Fall through for C++0x, so we handle the second `>' in
30426 gcc_fallthrough ();
30429 if (!nesting_depth
&& level
-- == 0)
30431 /* We've reached the token we want, consume it and stop. */
30432 cp_lexer_consume_token (parser
->lexer
);
30437 case CPP_OPEN_PAREN
:
30438 case CPP_OPEN_SQUARE
:
30442 case CPP_CLOSE_PAREN
:
30443 case CPP_CLOSE_SQUARE
:
30444 if (nesting_depth
-- == 0)
30449 case CPP_PRAGMA_EOL
:
30450 case CPP_SEMICOLON
:
30451 case CPP_OPEN_BRACE
:
30452 case CPP_CLOSE_BRACE
:
30453 /* The '>' was probably forgotten, don't look further. */
30460 /* Consume this token. */
30461 cp_lexer_consume_token (parser
->lexer
);
30465 /* If the next token is the indicated keyword, consume it. Otherwise,
30466 issue an error message indicating that TOKEN_DESC was expected.
30468 Returns the token consumed, if the token had the appropriate type.
30469 Otherwise, returns NULL. */
30472 cp_parser_require_keyword (cp_parser
* parser
,
30474 required_token token_desc
)
30476 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
30478 if (token
&& token
->keyword
!= keyword
)
30480 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
30488 /* Returns TRUE iff TOKEN is a token that can begin the body of a
30489 function-definition. */
30492 cp_parser_token_starts_function_definition_p (cp_token
* token
)
30494 return (/* An ordinary function-body begins with an `{'. */
30495 token
->type
== CPP_OPEN_BRACE
30496 /* A ctor-initializer begins with a `:'. */
30497 || token
->type
== CPP_COLON
30498 /* A function-try-block begins with `try'. */
30499 || token
->keyword
== RID_TRY
30500 /* A function-transaction-block begins with `__transaction_atomic'
30501 or `__transaction_relaxed'. */
30502 || token
->keyword
== RID_TRANSACTION_ATOMIC
30503 || token
->keyword
== RID_TRANSACTION_RELAXED
30504 /* The named return value extension begins with `return'. */
30505 || token
->keyword
== RID_RETURN
);
30508 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
30512 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
30516 token
= cp_lexer_peek_token (parser
->lexer
);
30517 return (token
->type
== CPP_OPEN_BRACE
30518 || (token
->type
== CPP_COLON
30519 && !parser
->colon_doesnt_start_class_def_p
));
30522 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
30523 C++0x) ending a template-argument. */
30526 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
30530 token
= cp_lexer_peek_token (parser
->lexer
);
30531 return (token
->type
== CPP_COMMA
30532 || token
->type
== CPP_GREATER
30533 || token
->type
== CPP_ELLIPSIS
30534 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
30537 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
30538 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
30541 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
30546 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
30547 if (token
->type
== CPP_LESS
)
30549 /* Check for the sequence `<::' in the original code. It would be lexed as
30550 `[:', where `[' is a digraph, and there is no whitespace before
30552 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
30555 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
30556 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
30562 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
30563 or none_type otherwise. */
30565 static enum tag_types
30566 cp_parser_token_is_class_key (cp_token
* token
)
30568 switch (token
->keyword
)
30573 return record_type
;
30582 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
30583 or none_type otherwise or if the token is null. */
30585 static enum tag_types
30586 cp_parser_token_is_type_parameter_key (cp_token
* token
)
30591 switch (token
->keyword
)
30596 return typename_type
;
30603 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
30606 cp_parser_check_class_key (enum tag_types class_key
, tree type
)
30608 if (type
== error_mark_node
)
30610 if ((TREE_CODE (type
) == UNION_TYPE
) != (class_key
== union_type
))
30612 if (permerror (input_location
, "%qs tag used in naming %q#T",
30613 class_key
== union_type
? "union"
30614 : class_key
== record_type
? "struct" : "class",
30616 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
30617 "%q#T was previously declared here", type
);
30621 /* Issue an error message if DECL is redeclared with different
30622 access than its original declaration [class.access.spec/3].
30623 This applies to nested classes, nested class templates and
30624 enumerations [class.mem/1]. */
30627 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
30630 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
30631 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
30634 if ((TREE_PRIVATE (decl
)
30635 != (current_access_specifier
== access_private_node
))
30636 || (TREE_PROTECTED (decl
)
30637 != (current_access_specifier
== access_protected_node
)))
30638 error_at (location
, "%qD redeclared with different access", decl
);
30641 /* Look for the `template' keyword, as a syntactic disambiguator.
30642 Return TRUE iff it is present, in which case it will be
30646 cp_parser_optional_template_keyword (cp_parser
*parser
)
30648 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
30650 /* In C++98 the `template' keyword can only be used within templates;
30651 outside templates the parser can always figure out what is a
30652 template and what is not. In C++11, per the resolution of DR 468,
30653 `template' is allowed in cases where it is not strictly necessary. */
30654 if (!processing_template_decl
30655 && pedantic
&& cxx_dialect
== cxx98
)
30657 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30658 pedwarn (token
->location
, OPT_Wpedantic
,
30659 "in C++98 %<template%> (as a disambiguator) is only "
30660 "allowed within templates");
30661 /* If this part of the token stream is rescanned, the same
30662 error message would be generated. So, we purge the token
30663 from the stream. */
30664 cp_lexer_purge_token (parser
->lexer
);
30669 /* Consume the `template' keyword. */
30670 cp_lexer_consume_token (parser
->lexer
);
30677 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
30678 set PARSER->SCOPE, and perform other related actions. */
30681 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
30683 struct tree_check
*check_value
;
30685 /* Get the stored value. */
30686 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
30687 /* Set the scope from the stored value. */
30688 parser
->scope
= saved_checks_value (check_value
);
30689 parser
->qualifying_scope
= check_value
->qualifying_scope
;
30690 parser
->object_scope
= NULL_TREE
;
30693 /* Consume tokens up through a non-nested END token. Returns TRUE if we
30694 encounter the end of a block before what we were looking for. */
30697 cp_parser_cache_group (cp_parser
*parser
,
30698 enum cpp_ttype end
,
30703 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30705 /* Abort a parenthesized expression if we encounter a semicolon. */
30706 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
30707 && token
->type
== CPP_SEMICOLON
)
30709 /* If we've reached the end of the file, stop. */
30710 if (token
->type
== CPP_EOF
30711 || (end
!= CPP_PRAGMA_EOL
30712 && token
->type
== CPP_PRAGMA_EOL
))
30714 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
30715 /* We've hit the end of an enclosing block, so there's been some
30716 kind of syntax error. */
30719 /* Consume the token. */
30720 cp_lexer_consume_token (parser
->lexer
);
30721 /* See if it starts a new group. */
30722 if (token
->type
== CPP_OPEN_BRACE
)
30724 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
30725 /* In theory this should probably check end == '}', but
30726 cp_parser_save_member_function_body needs it to exit
30727 after either '}' or ')' when called with ')'. */
30731 else if (token
->type
== CPP_OPEN_PAREN
)
30733 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
30734 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
30737 else if (token
->type
== CPP_PRAGMA
)
30738 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
30739 else if (token
->type
== end
)
30744 /* Like above, for caching a default argument or NSDMI. Both of these are
30745 terminated by a non-nested comma, but it can be unclear whether or not a
30746 comma is nested in a template argument list unless we do more parsing.
30747 In order to handle this ambiguity, when we encounter a ',' after a '<'
30748 we try to parse what follows as a parameter-declaration-list (in the
30749 case of a default argument) or a member-declarator (in the case of an
30750 NSDMI). If that succeeds, then we stop caching. */
30753 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
30755 unsigned depth
= 0;
30756 int maybe_template_id
= 0;
30757 cp_token
*first_token
;
30759 tree default_argument
;
30761 /* Add tokens until we have processed the entire default
30762 argument. We add the range [first_token, token). */
30763 first_token
= cp_lexer_peek_token (parser
->lexer
);
30764 if (first_token
->type
== CPP_OPEN_BRACE
)
30766 /* For list-initialization, this is straightforward. */
30767 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
30768 token
= cp_lexer_peek_token (parser
->lexer
);
30774 /* Peek at the next token. */
30775 token
= cp_lexer_peek_token (parser
->lexer
);
30776 /* What we do depends on what token we have. */
30777 switch (token
->type
)
30779 /* In valid code, a default argument must be
30780 immediately followed by a `,' `)', or `...'. */
30782 if (depth
== 0 && maybe_template_id
)
30784 /* If we've seen a '<', we might be in a
30785 template-argument-list. Until Core issue 325 is
30786 resolved, we don't know how this situation ought
30787 to be handled, so try to DTRT. We check whether
30788 what comes after the comma is a valid parameter
30789 declaration list. If it is, then the comma ends
30790 the default argument; otherwise the default
30791 argument continues. */
30792 bool error
= false;
30795 /* Set ITALP so cp_parser_parameter_declaration_list
30796 doesn't decide to commit to this parse. */
30797 bool saved_italp
= parser
->in_template_argument_list_p
;
30798 parser
->in_template_argument_list_p
= true;
30800 cp_parser_parse_tentatively (parser
);
30804 /* Parse declarators until we reach a non-comma or
30805 somthing that cannot be an initializer.
30806 Just checking whether we're looking at a single
30807 declarator is insufficient. Consider:
30808 int var = tuple<T,U>::x;
30809 The template parameter 'U' looks exactly like a
30813 int ctor_dtor_or_conv_p
;
30814 cp_lexer_consume_token (parser
->lexer
);
30815 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
30816 CP_PARSER_FLAGS_NONE
,
30817 &ctor_dtor_or_conv_p
,
30818 /*parenthesized_p=*/NULL
,
30820 /*friend_p=*/false,
30821 /*static_p=*/false);
30822 peek
= cp_lexer_peek_token (parser
->lexer
);
30823 if (cp_parser_error_occurred (parser
))
30826 while (peek
->type
== CPP_COMMA
);
30827 /* If we met an '=' or ';' then the original comma
30828 was the end of the NSDMI. Otherwise assume
30829 we're still in the NSDMI. */
30830 error
= (peek
->type
!= CPP_EQ
30831 && peek
->type
!= CPP_SEMICOLON
);
30835 cp_lexer_consume_token (parser
->lexer
);
30836 begin_scope (sk_function_parms
, NULL_TREE
);
30837 tree t
= cp_parser_parameter_declaration_list
30838 (parser
, CP_PARSER_FLAGS_NONE
);
30839 if (t
== error_mark_node
)
30841 pop_bindings_and_leave_scope ();
30843 if (!cp_parser_error_occurred (parser
) && !error
)
30845 cp_parser_abort_tentative_parse (parser
);
30847 parser
->in_template_argument_list_p
= saved_italp
;
30851 case CPP_CLOSE_PAREN
:
30853 /* If we run into a non-nested `;', `}', or `]',
30854 then the code is invalid -- but the default
30855 argument is certainly over. */
30856 case CPP_SEMICOLON
:
30857 case CPP_CLOSE_BRACE
:
30858 case CPP_CLOSE_SQUARE
:
30860 /* Handle correctly int n = sizeof ... ( p ); */
30861 && token
->type
!= CPP_ELLIPSIS
)
30863 /* Update DEPTH, if necessary. */
30864 else if (token
->type
== CPP_CLOSE_PAREN
30865 || token
->type
== CPP_CLOSE_BRACE
30866 || token
->type
== CPP_CLOSE_SQUARE
)
30870 case CPP_OPEN_PAREN
:
30871 case CPP_OPEN_SQUARE
:
30872 case CPP_OPEN_BRACE
:
30878 /* This might be the comparison operator, or it might
30879 start a template argument list. */
30880 ++maybe_template_id
;
30884 if (cxx_dialect
== cxx98
)
30886 /* Fall through for C++0x, which treats the `>>'
30887 operator like two `>' tokens in certain
30889 gcc_fallthrough ();
30894 /* This might be an operator, or it might close a
30895 template argument list. But if a previous '<'
30896 started a template argument list, this will have
30897 closed it, so we can't be in one anymore. */
30898 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
30899 if (maybe_template_id
< 0)
30900 maybe_template_id
= 0;
30904 /* If we run out of tokens, issue an error message. */
30906 case CPP_PRAGMA_EOL
:
30907 error_at (token
->location
, "file ends in default argument");
30908 return error_mark_node
;
30912 /* In these cases, we should look for template-ids.
30913 For example, if the default argument is
30914 `X<int, double>()', we need to do name lookup to
30915 figure out whether or not `X' is a template; if
30916 so, the `,' does not end the default argument.
30918 That is not yet done. */
30925 /* If we've reached the end, stop. */
30929 /* Add the token to the token block. */
30930 token
= cp_lexer_consume_token (parser
->lexer
);
30933 /* Create a DEFERRED_PARSE to represent the unparsed default
30935 default_argument
= make_node (DEFERRED_PARSE
);
30936 DEFPARSE_TOKENS (default_argument
)
30937 = cp_token_cache_new (first_token
, token
);
30938 DEFPARSE_INSTANTIATIONS (default_argument
) = NULL
;
30940 return default_argument
;
30943 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
30946 defparse_location (tree default_argument
)
30948 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_argument
);
30949 location_t start
= tokens
->first
->location
;
30950 location_t end
= tokens
->last
->location
;
30951 return make_location (start
, start
, end
);
30954 /* Begin parsing tentatively. We always save tokens while parsing
30955 tentatively so that if the tentative parsing fails we can restore the
30959 cp_parser_parse_tentatively (cp_parser
* parser
)
30961 /* Enter a new parsing context. */
30962 parser
->context
= cp_parser_context_new (parser
->context
);
30963 /* Begin saving tokens. */
30964 cp_lexer_save_tokens (parser
->lexer
);
30965 /* In order to avoid repetitive access control error messages,
30966 access checks are queued up until we are no longer parsing
30968 push_deferring_access_checks (dk_deferred
);
30971 /* Commit to the currently active tentative parse. */
30974 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
30976 cp_parser_context
*context
;
30979 /* Mark all of the levels as committed. */
30980 lexer
= parser
->lexer
;
30981 for (context
= parser
->context
; context
->next
; context
= context
->next
)
30983 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
30985 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
30986 while (!cp_lexer_saving_tokens (lexer
))
30987 lexer
= lexer
->next
;
30988 cp_lexer_commit_tokens (lexer
);
30992 /* Commit to the topmost currently active tentative parse.
30994 Note that this function shouldn't be called when there are
30995 irreversible side-effects while in a tentative state. For
30996 example, we shouldn't create a permanent entry in the symbol
30997 table, or issue an error message that might not apply if the
30998 tentative parse is aborted. */
31001 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
31003 cp_parser_context
*context
= parser
->context
;
31004 cp_lexer
*lexer
= parser
->lexer
;
31008 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
31010 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
31012 while (!cp_lexer_saving_tokens (lexer
))
31013 lexer
= lexer
->next
;
31014 cp_lexer_commit_tokens (lexer
);
31018 /* Abort the currently active tentative parse. All consumed tokens
31019 will be rolled back, and no diagnostics will be issued. */
31022 cp_parser_abort_tentative_parse (cp_parser
* parser
)
31024 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
31025 || errorcount
> 0);
31026 cp_parser_simulate_error (parser
);
31027 /* Now, pretend that we want to see if the construct was
31028 successfully parsed. */
31029 cp_parser_parse_definitely (parser
);
31032 /* Stop parsing tentatively. If a parse error has occurred, restore the
31033 token stream. Otherwise, commit to the tokens we have consumed.
31034 Returns true if no error occurred; false otherwise. */
31037 cp_parser_parse_definitely (cp_parser
* parser
)
31039 bool error_occurred
;
31040 cp_parser_context
*context
;
31042 /* Remember whether or not an error occurred, since we are about to
31043 destroy that information. */
31044 error_occurred
= cp_parser_error_occurred (parser
);
31045 /* Remove the topmost context from the stack. */
31046 context
= parser
->context
;
31047 parser
->context
= context
->next
;
31048 /* If no parse errors occurred, commit to the tentative parse. */
31049 if (!error_occurred
)
31051 /* Commit to the tokens read tentatively, unless that was
31053 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
31054 cp_lexer_commit_tokens (parser
->lexer
);
31056 pop_to_parent_deferring_access_checks ();
31058 /* Otherwise, if errors occurred, roll back our state so that things
31059 are just as they were before we began the tentative parse. */
31062 cp_lexer_rollback_tokens (parser
->lexer
);
31063 pop_deferring_access_checks ();
31065 /* Add the context to the front of the free list. */
31066 context
->next
= cp_parser_context_free_list
;
31067 cp_parser_context_free_list
= context
;
31069 return !error_occurred
;
31072 /* Returns true if we are parsing tentatively and are not committed to
31073 this tentative parse. */
31076 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
31078 return (cp_parser_parsing_tentatively (parser
)
31079 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
31082 /* Returns nonzero iff an error has occurred during the most recent
31083 tentative parse. */
31086 cp_parser_error_occurred (cp_parser
* parser
)
31088 return (cp_parser_parsing_tentatively (parser
)
31089 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
31092 /* Returns nonzero if GNU extensions are allowed. */
31095 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
31097 return parser
->allow_gnu_extensions_p
;
31100 /* Objective-C++ Productions */
31103 /* Parse an Objective-C expression, which feeds into a primary-expression
31107 objc-message-expression
31108 objc-string-literal
31109 objc-encode-expression
31110 objc-protocol-expression
31111 objc-selector-expression
31113 Returns a tree representation of the expression. */
31116 cp_parser_objc_expression (cp_parser
* parser
)
31118 /* Try to figure out what kind of declaration is present. */
31119 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
31123 case CPP_OPEN_SQUARE
:
31124 return cp_parser_objc_message_expression (parser
);
31126 case CPP_OBJC_STRING
:
31127 kwd
= cp_lexer_consume_token (parser
->lexer
);
31128 return objc_build_string_object (kwd
->u
.value
);
31131 switch (kwd
->keyword
)
31133 case RID_AT_ENCODE
:
31134 return cp_parser_objc_encode_expression (parser
);
31136 case RID_AT_PROTOCOL
:
31137 return cp_parser_objc_protocol_expression (parser
);
31139 case RID_AT_SELECTOR
:
31140 return cp_parser_objc_selector_expression (parser
);
31147 error_at (kwd
->location
,
31148 "misplaced %<@%D%> Objective-C++ construct",
31150 cp_parser_skip_to_end_of_block_or_statement (parser
);
31153 return error_mark_node
;
31156 /* Parse an Objective-C message expression.
31158 objc-message-expression:
31159 [ objc-message-receiver objc-message-args ]
31161 Returns a representation of an Objective-C message. */
31164 cp_parser_objc_message_expression (cp_parser
* parser
)
31166 tree receiver
, messageargs
;
31168 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31169 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
31170 receiver
= cp_parser_objc_message_receiver (parser
);
31171 messageargs
= cp_parser_objc_message_args (parser
);
31172 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31173 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
31175 tree result
= objc_build_message_expr (receiver
, messageargs
);
31177 /* Construct a location e.g.
31180 ranging from the '[' to the ']', with the caret at the start. */
31181 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
31182 protected_set_expr_location (result
, combined_loc
);
31187 /* Parse an objc-message-receiver.
31189 objc-message-receiver:
31191 simple-type-specifier
31193 Returns a representation of the type or expression. */
31196 cp_parser_objc_message_receiver (cp_parser
* parser
)
31200 /* An Objective-C message receiver may be either (1) a type
31201 or (2) an expression. */
31202 cp_parser_parse_tentatively (parser
);
31203 rcv
= cp_parser_expression (parser
);
31205 /* If that worked out, fine. */
31206 if (cp_parser_parse_definitely (parser
))
31209 cp_parser_parse_tentatively (parser
);
31210 rcv
= cp_parser_simple_type_specifier (parser
,
31211 /*decl_specs=*/NULL
,
31212 CP_PARSER_FLAGS_NONE
);
31214 if (cp_parser_parse_definitely (parser
))
31215 return objc_get_class_reference (rcv
);
31217 cp_parser_error (parser
, "objective-c++ message receiver expected");
31218 return error_mark_node
;
31221 /* Parse the arguments and selectors comprising an Objective-C message.
31226 objc-selector-args , objc-comma-args
31228 objc-selector-args:
31229 objc-selector [opt] : assignment-expression
31230 objc-selector-args objc-selector [opt] : assignment-expression
31233 assignment-expression
31234 objc-comma-args , assignment-expression
31236 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
31237 selector arguments and TREE_VALUE containing a list of comma
31241 cp_parser_objc_message_args (cp_parser
* parser
)
31243 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
31244 bool maybe_unary_selector_p
= true;
31245 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31247 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
31249 tree selector
= NULL_TREE
, arg
;
31251 if (token
->type
!= CPP_COLON
)
31252 selector
= cp_parser_objc_selector (parser
);
31254 /* Detect if we have a unary selector. */
31255 if (maybe_unary_selector_p
31256 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31257 return build_tree_list (selector
, NULL_TREE
);
31259 maybe_unary_selector_p
= false;
31260 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
31261 arg
= cp_parser_assignment_expression (parser
);
31264 = chainon (sel_args
,
31265 build_tree_list (selector
, arg
));
31267 token
= cp_lexer_peek_token (parser
->lexer
);
31270 /* Handle non-selector arguments, if any. */
31271 while (token
->type
== CPP_COMMA
)
31275 cp_lexer_consume_token (parser
->lexer
);
31276 arg
= cp_parser_assignment_expression (parser
);
31279 = chainon (addl_args
,
31280 build_tree_list (NULL_TREE
, arg
));
31282 token
= cp_lexer_peek_token (parser
->lexer
);
31285 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
31287 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
31288 return build_tree_list (error_mark_node
, error_mark_node
);
31291 return build_tree_list (sel_args
, addl_args
);
31294 /* Parse an Objective-C encode expression.
31296 objc-encode-expression:
31297 @encode objc-typename
31299 Returns an encoded representation of the type argument. */
31302 cp_parser_objc_encode_expression (cp_parser
* parser
)
31306 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31308 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
31309 matching_parens parens
;
31310 parens
.require_open (parser
);
31311 token
= cp_lexer_peek_token (parser
->lexer
);
31312 type
= complete_type (cp_parser_type_id (parser
));
31313 parens
.require_close (parser
);
31317 error_at (token
->location
,
31318 "%<@encode%> must specify a type as an argument");
31319 return error_mark_node
;
31322 /* This happens if we find @encode(T) (where T is a template
31323 typename or something dependent on a template typename) when
31324 parsing a template. In that case, we can't compile it
31325 immediately, but we rather create an AT_ENCODE_EXPR which will
31326 need to be instantiated when the template is used.
31328 if (dependent_type_p (type
))
31330 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
31331 TREE_READONLY (value
) = 1;
31336 /* Build a location of the form:
31339 with caret==start at the @ token, finishing at the close paren. */
31340 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
31342 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
31345 /* Parse an Objective-C @defs expression. */
31348 cp_parser_objc_defs_expression (cp_parser
*parser
)
31352 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
31353 matching_parens parens
;
31354 parens
.require_open (parser
);
31355 name
= cp_parser_identifier (parser
);
31356 parens
.require_close (parser
);
31358 return objc_get_class_ivars (name
);
31361 /* Parse an Objective-C protocol expression.
31363 objc-protocol-expression:
31364 @protocol ( identifier )
31366 Returns a representation of the protocol expression. */
31369 cp_parser_objc_protocol_expression (cp_parser
* parser
)
31372 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31374 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
31375 matching_parens parens
;
31376 parens
.require_open (parser
);
31377 proto
= cp_parser_identifier (parser
);
31378 parens
.require_close (parser
);
31380 /* Build a location of the form:
31383 with caret==start at the @ token, finishing at the close paren. */
31384 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
31385 tree result
= objc_build_protocol_expr (proto
);
31386 protected_set_expr_location (result
, combined_loc
);
31390 /* Parse an Objective-C selector expression.
31392 objc-selector-expression:
31393 @selector ( objc-method-signature )
31395 objc-method-signature:
31401 objc-selector-seq objc-selector :
31403 Returns a representation of the method selector. */
31406 cp_parser_objc_selector_expression (cp_parser
* parser
)
31408 tree sel_seq
= NULL_TREE
;
31409 bool maybe_unary_selector_p
= true;
31411 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
31413 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
31414 matching_parens parens
;
31415 parens
.require_open (parser
);
31416 token
= cp_lexer_peek_token (parser
->lexer
);
31418 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
31419 || token
->type
== CPP_SCOPE
)
31421 tree selector
= NULL_TREE
;
31423 if (token
->type
!= CPP_COLON
31424 || token
->type
== CPP_SCOPE
)
31425 selector
= cp_parser_objc_selector (parser
);
31427 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
31428 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
31430 /* Detect if we have a unary selector. */
31431 if (maybe_unary_selector_p
)
31433 sel_seq
= selector
;
31434 goto finish_selector
;
31438 cp_parser_error (parser
, "expected %<:%>");
31441 maybe_unary_selector_p
= false;
31442 token
= cp_lexer_consume_token (parser
->lexer
);
31444 if (token
->type
== CPP_SCOPE
)
31447 = chainon (sel_seq
,
31448 build_tree_list (selector
, NULL_TREE
));
31450 = chainon (sel_seq
,
31451 build_tree_list (NULL_TREE
, NULL_TREE
));
31455 = chainon (sel_seq
,
31456 build_tree_list (selector
, NULL_TREE
));
31458 token
= cp_lexer_peek_token (parser
->lexer
);
31462 parens
.require_close (parser
);
31465 /* Build a location of the form:
31468 with caret==start at the @ token, finishing at the close paren. */
31469 location_t combined_loc
= make_location (loc
, loc
, parser
->lexer
);
31470 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
31471 /* TODO: objc_build_selector_expr doesn't always honor the location. */
31472 protected_set_expr_location (result
, combined_loc
);
31476 /* Parse a list of identifiers.
31478 objc-identifier-list:
31480 objc-identifier-list , identifier
31482 Returns a TREE_LIST of identifier nodes. */
31485 cp_parser_objc_identifier_list (cp_parser
* parser
)
31491 identifier
= cp_parser_identifier (parser
);
31492 if (identifier
== error_mark_node
)
31493 return error_mark_node
;
31495 list
= build_tree_list (NULL_TREE
, identifier
);
31496 sep
= cp_lexer_peek_token (parser
->lexer
);
31498 while (sep
->type
== CPP_COMMA
)
31500 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31501 identifier
= cp_parser_identifier (parser
);
31502 if (identifier
== error_mark_node
)
31505 list
= chainon (list
, build_tree_list (NULL_TREE
,
31507 sep
= cp_lexer_peek_token (parser
->lexer
);
31513 /* Parse an Objective-C alias declaration.
31515 objc-alias-declaration:
31516 @compatibility_alias identifier identifier ;
31518 This function registers the alias mapping with the Objective-C front end.
31519 It returns nothing. */
31522 cp_parser_objc_alias_declaration (cp_parser
* parser
)
31526 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
31527 alias
= cp_parser_identifier (parser
);
31528 orig
= cp_parser_identifier (parser
);
31529 objc_declare_alias (alias
, orig
);
31530 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31533 /* Parse an Objective-C class forward-declaration.
31535 objc-class-declaration:
31536 @class objc-identifier-list ;
31538 The function registers the forward declarations with the Objective-C
31539 front end. It returns nothing. */
31542 cp_parser_objc_class_declaration (cp_parser
* parser
)
31544 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
31549 id
= cp_parser_identifier (parser
);
31550 if (id
== error_mark_node
)
31553 objc_declare_class (id
);
31555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
31556 cp_lexer_consume_token (parser
->lexer
);
31560 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31563 /* Parse a list of Objective-C protocol references.
31565 objc-protocol-refs-opt:
31566 objc-protocol-refs [opt]
31568 objc-protocol-refs:
31569 < objc-identifier-list >
31571 Returns a TREE_LIST of identifiers, if any. */
31574 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
31576 tree protorefs
= NULL_TREE
;
31578 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
31580 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
31581 protorefs
= cp_parser_objc_identifier_list (parser
);
31582 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
31588 /* Parse a Objective-C visibility specification. */
31591 cp_parser_objc_visibility_spec (cp_parser
* parser
)
31593 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
31595 switch (vis
->keyword
)
31597 case RID_AT_PRIVATE
:
31598 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
31600 case RID_AT_PROTECTED
:
31601 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
31603 case RID_AT_PUBLIC
:
31604 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
31606 case RID_AT_PACKAGE
:
31607 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
31613 /* Eat '@private'/'@protected'/'@public'. */
31614 cp_lexer_consume_token (parser
->lexer
);
31617 /* Parse an Objective-C method type. Return 'true' if it is a class
31618 (+) method, and 'false' if it is an instance (-) method. */
31621 cp_parser_objc_method_type (cp_parser
* parser
)
31623 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
31629 /* Parse an Objective-C protocol qualifier. */
31632 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
31634 tree quals
= NULL_TREE
, node
;
31635 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31637 node
= token
->u
.value
;
31639 while (node
&& identifier_p (node
)
31640 && (node
== ridpointers
[(int) RID_IN
]
31641 || node
== ridpointers
[(int) RID_OUT
]
31642 || node
== ridpointers
[(int) RID_INOUT
]
31643 || node
== ridpointers
[(int) RID_BYCOPY
]
31644 || node
== ridpointers
[(int) RID_BYREF
]
31645 || node
== ridpointers
[(int) RID_ONEWAY
]))
31647 quals
= tree_cons (NULL_TREE
, node
, quals
);
31648 cp_lexer_consume_token (parser
->lexer
);
31649 token
= cp_lexer_peek_token (parser
->lexer
);
31650 node
= token
->u
.value
;
31656 /* Parse an Objective-C typename. */
31659 cp_parser_objc_typename (cp_parser
* parser
)
31661 tree type_name
= NULL_TREE
;
31663 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31665 tree proto_quals
, cp_type
= NULL_TREE
;
31667 matching_parens parens
;
31668 parens
.consume_open (parser
); /* Eat '('. */
31669 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
31671 /* An ObjC type name may consist of just protocol qualifiers, in which
31672 case the type shall default to 'id'. */
31673 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
31675 cp_type
= cp_parser_type_id (parser
);
31677 /* If the type could not be parsed, an error has already
31678 been produced. For error recovery, behave as if it had
31679 not been specified, which will use the default type
31681 if (cp_type
== error_mark_node
)
31683 cp_type
= NULL_TREE
;
31684 /* We need to skip to the closing parenthesis as
31685 cp_parser_type_id() does not seem to do it for
31687 cp_parser_skip_to_closing_parenthesis (parser
,
31688 /*recovering=*/true,
31689 /*or_comma=*/false,
31690 /*consume_paren=*/false);
31694 parens
.require_close (parser
);
31695 type_name
= build_tree_list (proto_quals
, cp_type
);
31701 /* Check to see if TYPE refers to an Objective-C selector name. */
31704 cp_parser_objc_selector_p (enum cpp_ttype type
)
31706 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
31707 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
31708 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
31709 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
31710 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
31713 /* Parse an Objective-C selector. */
31716 cp_parser_objc_selector (cp_parser
* parser
)
31718 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
31720 if (!cp_parser_objc_selector_p (token
->type
))
31722 error_at (token
->location
, "invalid Objective-C++ selector name");
31723 return error_mark_node
;
31726 /* C++ operator names are allowed to appear in ObjC selectors. */
31727 switch (token
->type
)
31729 case CPP_AND_AND
: return get_identifier ("and");
31730 case CPP_AND_EQ
: return get_identifier ("and_eq");
31731 case CPP_AND
: return get_identifier ("bitand");
31732 case CPP_OR
: return get_identifier ("bitor");
31733 case CPP_COMPL
: return get_identifier ("compl");
31734 case CPP_NOT
: return get_identifier ("not");
31735 case CPP_NOT_EQ
: return get_identifier ("not_eq");
31736 case CPP_OR_OR
: return get_identifier ("or");
31737 case CPP_OR_EQ
: return get_identifier ("or_eq");
31738 case CPP_XOR
: return get_identifier ("xor");
31739 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
31740 default: return token
->u
.value
;
31744 /* Parse an Objective-C params list. */
31747 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
31749 tree params
= NULL_TREE
;
31750 bool maybe_unary_selector_p
= true;
31751 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31753 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
31755 tree selector
= NULL_TREE
, type_name
, identifier
;
31756 tree parm_attr
= NULL_TREE
;
31758 if (token
->keyword
== RID_ATTRIBUTE
)
31761 if (token
->type
!= CPP_COLON
)
31762 selector
= cp_parser_objc_selector (parser
);
31764 /* Detect if we have a unary selector. */
31765 if (maybe_unary_selector_p
31766 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
31768 params
= selector
; /* Might be followed by attributes. */
31772 maybe_unary_selector_p
= false;
31773 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
31775 /* Something went quite wrong. There should be a colon
31776 here, but there is not. Stop parsing parameters. */
31779 type_name
= cp_parser_objc_typename (parser
);
31780 /* New ObjC allows attributes on parameters too. */
31781 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
31782 parm_attr
= cp_parser_attributes_opt (parser
);
31783 identifier
= cp_parser_identifier (parser
);
31787 objc_build_keyword_decl (selector
,
31792 token
= cp_lexer_peek_token (parser
->lexer
);
31795 if (params
== NULL_TREE
)
31797 cp_parser_error (parser
, "objective-c++ method declaration is expected");
31798 return error_mark_node
;
31801 /* We allow tail attributes for the method. */
31802 if (token
->keyword
== RID_ATTRIBUTE
)
31804 *attributes
= cp_parser_attributes_opt (parser
);
31805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
31806 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
31808 cp_parser_error (parser
,
31809 "method attributes must be specified at the end");
31810 return error_mark_node
;
31813 if (params
== NULL_TREE
)
31815 cp_parser_error (parser
, "objective-c++ method declaration is expected");
31816 return error_mark_node
;
31821 /* Parse the non-keyword Objective-C params. */
31824 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
31827 tree params
= make_node (TREE_LIST
);
31828 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31829 *ellipsisp
= false; /* Initially, assume no ellipsis. */
31831 while (token
->type
== CPP_COMMA
)
31833 cp_parameter_declarator
*parmdecl
;
31836 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
31837 token
= cp_lexer_peek_token (parser
->lexer
);
31839 if (token
->type
== CPP_ELLIPSIS
)
31841 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
31843 token
= cp_lexer_peek_token (parser
->lexer
);
31847 /* TODO: parse attributes for tail parameters. */
31848 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
31850 parm
= grokdeclarator (parmdecl
->declarator
,
31851 &parmdecl
->decl_specifiers
,
31852 PARM
, /*initialized=*/0,
31853 /*attrlist=*/NULL
);
31855 chainon (params
, build_tree_list (NULL_TREE
, parm
));
31856 token
= cp_lexer_peek_token (parser
->lexer
);
31859 /* We allow tail attributes for the method. */
31860 if (token
->keyword
== RID_ATTRIBUTE
)
31862 if (*attributes
== NULL_TREE
)
31864 *attributes
= cp_parser_attributes_opt (parser
);
31865 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
31866 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
31870 /* We have an error, but parse the attributes, so that we can
31872 *attributes
= cp_parser_attributes_opt (parser
);
31874 cp_parser_error (parser
,
31875 "method attributes must be specified at the end");
31876 return error_mark_node
;
31882 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
31885 cp_parser_objc_interstitial_code (cp_parser
* parser
)
31887 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31889 /* If the next token is `extern' and the following token is a string
31890 literal, then we have a linkage specification. */
31891 if (token
->keyword
== RID_EXTERN
31892 && cp_parser_is_pure_string_literal
31893 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
31894 cp_parser_linkage_specification (parser
);
31895 /* Handle #pragma, if any. */
31896 else if (token
->type
== CPP_PRAGMA
)
31897 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
31898 /* Allow stray semicolons. */
31899 else if (token
->type
== CPP_SEMICOLON
)
31900 cp_lexer_consume_token (parser
->lexer
);
31901 /* Mark methods as optional or required, when building protocols. */
31902 else if (token
->keyword
== RID_AT_OPTIONAL
)
31904 cp_lexer_consume_token (parser
->lexer
);
31905 objc_set_method_opt (true);
31907 else if (token
->keyword
== RID_AT_REQUIRED
)
31909 cp_lexer_consume_token (parser
->lexer
);
31910 objc_set_method_opt (false);
31912 else if (token
->keyword
== RID_NAMESPACE
)
31913 cp_parser_namespace_definition (parser
);
31914 /* Other stray characters must generate errors. */
31915 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
31917 cp_lexer_consume_token (parser
->lexer
);
31918 error ("stray %qs between Objective-C++ methods",
31919 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
31921 /* Finally, try to parse a block-declaration, or a function-definition. */
31923 cp_parser_block_declaration (parser
, /*statement_p=*/false);
31926 /* Parse a method signature. */
31929 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
31931 tree rettype
, kwdparms
, optparms
;
31932 bool ellipsis
= false;
31933 bool is_class_method
;
31935 is_class_method
= cp_parser_objc_method_type (parser
);
31936 rettype
= cp_parser_objc_typename (parser
);
31937 *attributes
= NULL_TREE
;
31938 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
31939 if (kwdparms
== error_mark_node
)
31940 return error_mark_node
;
31941 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
31942 if (optparms
== error_mark_node
)
31943 return error_mark_node
;
31945 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
31949 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
31952 cp_lexer_save_tokens (parser
->lexer
);
31953 tattr
= cp_parser_attributes_opt (parser
);
31954 gcc_assert (tattr
) ;
31956 /* If the attributes are followed by a method introducer, this is not allowed.
31957 Dump the attributes and flag the situation. */
31958 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
31959 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
31962 /* Otherwise, the attributes introduce some interstitial code, possibly so
31963 rewind to allow that check. */
31964 cp_lexer_rollback_tokens (parser
->lexer
);
31968 /* Parse an Objective-C method prototype list. */
31971 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
31973 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31975 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
31977 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
31979 tree attributes
, sig
;
31980 bool is_class_method
;
31981 if (token
->type
== CPP_PLUS
)
31982 is_class_method
= true;
31984 is_class_method
= false;
31985 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
31986 if (sig
== error_mark_node
)
31988 cp_parser_skip_to_end_of_block_or_statement (parser
);
31989 token
= cp_lexer_peek_token (parser
->lexer
);
31992 objc_add_method_declaration (is_class_method
, sig
, attributes
);
31993 cp_parser_consume_semicolon_at_end_of_statement (parser
);
31995 else if (token
->keyword
== RID_AT_PROPERTY
)
31996 cp_parser_objc_at_property_declaration (parser
);
31997 else if (token
->keyword
== RID_ATTRIBUTE
31998 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
31999 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
32001 "prefix attributes are ignored for methods");
32003 /* Allow for interspersed non-ObjC++ code. */
32004 cp_parser_objc_interstitial_code (parser
);
32006 token
= cp_lexer_peek_token (parser
->lexer
);
32009 if (token
->type
!= CPP_EOF
)
32010 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
32012 cp_parser_error (parser
, "expected %<@end%>");
32014 objc_finish_interface ();
32017 /* Parse an Objective-C method definition list. */
32020 cp_parser_objc_method_definition_list (cp_parser
* parser
)
32022 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32024 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
32028 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
32031 tree sig
, attribute
;
32032 bool is_class_method
;
32033 if (token
->type
== CPP_PLUS
)
32034 is_class_method
= true;
32036 is_class_method
= false;
32037 push_deferring_access_checks (dk_deferred
);
32038 sig
= cp_parser_objc_method_signature (parser
, &attribute
);
32039 if (sig
== error_mark_node
)
32041 cp_parser_skip_to_end_of_block_or_statement (parser
);
32042 token
= cp_lexer_peek_token (parser
->lexer
);
32045 objc_start_method_definition (is_class_method
, sig
, attribute
,
32048 /* For historical reasons, we accept an optional semicolon. */
32049 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32050 cp_lexer_consume_token (parser
->lexer
);
32052 ptk
= cp_lexer_peek_token (parser
->lexer
);
32053 if (!(ptk
->type
== CPP_PLUS
|| ptk
->type
== CPP_MINUS
32054 || ptk
->type
== CPP_EOF
|| ptk
->keyword
== RID_AT_END
))
32056 perform_deferred_access_checks (tf_warning_or_error
);
32057 stop_deferring_access_checks ();
32058 meth
= cp_parser_function_definition_after_declarator (parser
,
32060 pop_deferring_access_checks ();
32061 objc_finish_method_definition (meth
);
32064 /* The following case will be removed once @synthesize is
32065 completely implemented. */
32066 else if (token
->keyword
== RID_AT_PROPERTY
)
32067 cp_parser_objc_at_property_declaration (parser
);
32068 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
32069 cp_parser_objc_at_synthesize_declaration (parser
);
32070 else if (token
->keyword
== RID_AT_DYNAMIC
)
32071 cp_parser_objc_at_dynamic_declaration (parser
);
32072 else if (token
->keyword
== RID_ATTRIBUTE
32073 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
32074 warning_at (token
->location
, OPT_Wattributes
,
32075 "prefix attributes are ignored for methods");
32077 /* Allow for interspersed non-ObjC++ code. */
32078 cp_parser_objc_interstitial_code (parser
);
32080 token
= cp_lexer_peek_token (parser
->lexer
);
32083 if (token
->type
!= CPP_EOF
)
32084 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
32086 cp_parser_error (parser
, "expected %<@end%>");
32088 objc_finish_implementation ();
32091 /* Parse Objective-C ivars. */
32094 cp_parser_objc_class_ivars (cp_parser
* parser
)
32096 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32098 if (token
->type
!= CPP_OPEN_BRACE
)
32099 return; /* No ivars specified. */
32101 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
32102 token
= cp_lexer_peek_token (parser
->lexer
);
32104 while (token
->type
!= CPP_CLOSE_BRACE
32105 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
32107 cp_decl_specifier_seq declspecs
;
32108 int decl_class_or_enum_p
;
32109 tree prefix_attributes
;
32111 cp_parser_objc_visibility_spec (parser
);
32113 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
32116 cp_parser_decl_specifier_seq (parser
,
32117 CP_PARSER_FLAGS_OPTIONAL
,
32119 &decl_class_or_enum_p
);
32121 /* auto, register, static, extern, mutable. */
32122 if (declspecs
.storage_class
!= sc_none
)
32124 cp_parser_error (parser
, "invalid type for instance variable");
32125 declspecs
.storage_class
= sc_none
;
32128 /* thread_local. */
32129 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
32131 cp_parser_error (parser
, "invalid type for instance variable");
32132 declspecs
.locations
[ds_thread
] = 0;
32136 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
32138 cp_parser_error (parser
, "invalid type for instance variable");
32139 declspecs
.locations
[ds_typedef
] = 0;
32142 prefix_attributes
= declspecs
.attributes
;
32143 declspecs
.attributes
= NULL_TREE
;
32145 /* Keep going until we hit the `;' at the end of the
32147 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
32149 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
32150 cp_declarator
*declarator
= NULL
;
32151 int ctor_dtor_or_conv_p
;
32153 /* Check for a (possibly unnamed) bitfield declaration. */
32154 token
= cp_lexer_peek_token (parser
->lexer
);
32155 if (token
->type
== CPP_COLON
)
32158 if (token
->type
== CPP_NAME
32159 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
32162 /* Get the name of the bitfield. */
32163 declarator
= make_id_declarator (NULL_TREE
,
32164 cp_parser_identifier (parser
),
32165 sfk_none
, token
->location
);
32168 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
32169 /* Get the width of the bitfield. */
32171 = cp_parser_constant_expression (parser
);
32175 /* Parse the declarator. */
32177 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
32178 CP_PARSER_FLAGS_NONE
,
32179 &ctor_dtor_or_conv_p
,
32180 /*parenthesized_p=*/NULL
,
32181 /*member_p=*/false,
32182 /*friend_p=*/false,
32183 /*static_p=*/false);
32186 /* Look for attributes that apply to the ivar. */
32187 attributes
= cp_parser_attributes_opt (parser
);
32188 /* Remember which attributes are prefix attributes and
32190 first_attribute
= attributes
;
32191 /* Combine the attributes. */
32192 attributes
= attr_chainon (prefix_attributes
, attributes
);
32195 /* Create the bitfield declaration. */
32196 decl
= grokbitfield (declarator
, &declspecs
,
32197 width
, NULL_TREE
, attributes
);
32199 decl
= grokfield (declarator
, &declspecs
,
32200 NULL_TREE
, /*init_const_expr_p=*/false,
32201 NULL_TREE
, attributes
);
32203 /* Add the instance variable. */
32204 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
32205 objc_add_instance_variable (decl
);
32207 /* Reset PREFIX_ATTRIBUTES. */
32208 if (attributes
!= error_mark_node
)
32210 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
32211 attributes
= TREE_CHAIN (attributes
);
32213 TREE_CHAIN (attributes
) = NULL_TREE
;
32216 token
= cp_lexer_peek_token (parser
->lexer
);
32218 if (token
->type
== CPP_COMMA
)
32220 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
32226 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32227 token
= cp_lexer_peek_token (parser
->lexer
);
32230 if (token
->keyword
== RID_AT_END
)
32231 cp_parser_error (parser
, "expected %<}%>");
32233 /* Do not consume the RID_AT_END, so it will be read again as terminating
32234 the @interface of @implementation. */
32235 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
32236 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
32238 /* For historical reasons, we accept an optional semicolon. */
32239 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32240 cp_lexer_consume_token (parser
->lexer
);
32243 /* Parse an Objective-C protocol declaration. */
32246 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
32248 tree proto
, protorefs
;
32251 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
32252 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
32254 tok
= cp_lexer_peek_token (parser
->lexer
);
32255 error_at (tok
->location
, "identifier expected after %<@protocol%>");
32256 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32260 /* See if we have a forward declaration or a definition. */
32261 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
32263 /* Try a forward declaration first. */
32264 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
32270 id
= cp_parser_identifier (parser
);
32271 if (id
== error_mark_node
)
32274 objc_declare_protocol (id
, attributes
);
32276 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32277 cp_lexer_consume_token (parser
->lexer
);
32281 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32284 /* Ok, we got a full-fledged definition (or at least should). */
32287 proto
= cp_parser_identifier (parser
);
32288 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
32289 objc_start_protocol (proto
, protorefs
, attributes
);
32290 cp_parser_objc_method_prototype_list (parser
);
32294 /* Parse an Objective-C superclass or category. */
32297 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
32300 tree
*categ
, bool *is_class_extension
)
32302 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
32304 *super
= *categ
= NULL_TREE
;
32305 *is_class_extension
= false;
32306 if (next
->type
== CPP_COLON
)
32308 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
32309 *super
= cp_parser_identifier (parser
);
32311 else if (next
->type
== CPP_OPEN_PAREN
)
32313 matching_parens parens
;
32314 parens
.consume_open (parser
); /* Eat '('. */
32316 /* If there is no category name, and this is an @interface, we
32317 have a class extension. */
32318 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
32320 *categ
= NULL_TREE
;
32321 *is_class_extension
= true;
32324 *categ
= cp_parser_identifier (parser
);
32326 parens
.require_close (parser
);
32330 /* Parse an Objective-C class interface. */
32333 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
32335 tree name
, super
, categ
, protos
;
32336 bool is_class_extension
;
32338 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
32339 name
= cp_parser_identifier (parser
);
32340 if (name
== error_mark_node
)
32342 /* It's hard to recover because even if valid @interface stuff
32343 is to follow, we can't compile it (or validate it) if we
32344 don't even know which class it refers to. Let's assume this
32345 was a stray '@interface' token in the stream and skip it.
32349 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
32350 &is_class_extension
);
32351 protos
= cp_parser_objc_protocol_refs_opt (parser
);
32353 /* We have either a class or a category on our hands. */
32354 if (categ
|| is_class_extension
)
32355 objc_start_category_interface (name
, categ
, protos
, attributes
);
32358 objc_start_class_interface (name
, super
, protos
, attributes
);
32359 /* Handle instance variable declarations, if any. */
32360 cp_parser_objc_class_ivars (parser
);
32361 objc_continue_interface ();
32364 cp_parser_objc_method_prototype_list (parser
);
32367 /* Parse an Objective-C class implementation. */
32370 cp_parser_objc_class_implementation (cp_parser
* parser
)
32372 tree name
, super
, categ
;
32373 bool is_class_extension
;
32375 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
32376 name
= cp_parser_identifier (parser
);
32377 if (name
== error_mark_node
)
32379 /* It's hard to recover because even if valid @implementation
32380 stuff is to follow, we can't compile it (or validate it) if
32381 we don't even know which class it refers to. Let's assume
32382 this was a stray '@implementation' token in the stream and
32387 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
32388 &is_class_extension
);
32390 /* We have either a class or a category on our hands. */
32392 objc_start_category_implementation (name
, categ
);
32395 objc_start_class_implementation (name
, super
);
32396 /* Handle instance variable declarations, if any. */
32397 cp_parser_objc_class_ivars (parser
);
32398 objc_continue_implementation ();
32401 cp_parser_objc_method_definition_list (parser
);
32404 /* Consume the @end token and finish off the implementation. */
32407 cp_parser_objc_end_implementation (cp_parser
* parser
)
32409 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
32410 objc_finish_implementation ();
32413 /* Parse an Objective-C declaration. */
32416 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
32418 /* Try to figure out what kind of declaration is present. */
32419 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
32422 switch (kwd
->keyword
)
32427 error_at (kwd
->location
, "attributes may not be specified before"
32428 " the %<@%D%> Objective-C++ keyword",
32432 case RID_AT_IMPLEMENTATION
:
32433 warning_at (kwd
->location
, OPT_Wattributes
,
32434 "prefix attributes are ignored before %<@%D%>",
32441 switch (kwd
->keyword
)
32444 cp_parser_objc_alias_declaration (parser
);
32447 cp_parser_objc_class_declaration (parser
);
32449 case RID_AT_PROTOCOL
:
32450 cp_parser_objc_protocol_declaration (parser
, attributes
);
32452 case RID_AT_INTERFACE
:
32453 cp_parser_objc_class_interface (parser
, attributes
);
32455 case RID_AT_IMPLEMENTATION
:
32456 cp_parser_objc_class_implementation (parser
);
32459 cp_parser_objc_end_implementation (parser
);
32462 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
32464 cp_parser_skip_to_end_of_block_or_statement (parser
);
32468 /* Parse an Objective-C try-catch-finally statement.
32470 objc-try-catch-finally-stmt:
32471 @try compound-statement objc-catch-clause-seq [opt]
32472 objc-finally-clause [opt]
32474 objc-catch-clause-seq:
32475 objc-catch-clause objc-catch-clause-seq [opt]
32478 @catch ( objc-exception-declaration ) compound-statement
32480 objc-finally-clause:
32481 @finally compound-statement
32483 objc-exception-declaration:
32484 parameter-declaration
32487 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
32491 PS: This function is identical to c_parser_objc_try_catch_finally_statement
32492 for C. Keep them in sync. */
32495 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
32497 location_t location
;
32500 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
32501 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32502 objc_maybe_warn_exceptions (location
);
32503 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
32504 node, lest it get absorbed into the surrounding block. */
32505 stmt
= push_stmt_list ();
32506 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
32507 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
32509 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
32511 cp_parameter_declarator
*parm
;
32512 tree parameter_declaration
= error_mark_node
;
32513 bool seen_open_paren
= false;
32514 matching_parens parens
;
32516 cp_lexer_consume_token (parser
->lexer
);
32517 if (parens
.require_open (parser
))
32518 seen_open_paren
= true;
32519 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
32521 /* We have "@catch (...)" (where the '...' are literally
32522 what is in the code). Skip the '...'.
32523 parameter_declaration is set to NULL_TREE, and
32524 objc_being_catch_clauses() knows that that means
32526 cp_lexer_consume_token (parser
->lexer
);
32527 parameter_declaration
= NULL_TREE
;
32531 /* We have "@catch (NSException *exception)" or something
32532 like that. Parse the parameter declaration. */
32533 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
32536 parameter_declaration
= error_mark_node
;
32538 parameter_declaration
= grokdeclarator (parm
->declarator
,
32539 &parm
->decl_specifiers
,
32540 PARM
, /*initialized=*/0,
32541 /*attrlist=*/NULL
);
32543 if (seen_open_paren
)
32544 parens
.require_close (parser
);
32547 /* If there was no open parenthesis, we are recovering from
32548 an error, and we are trying to figure out what mistake
32549 the user has made. */
32551 /* If there is an immediate closing parenthesis, the user
32552 probably forgot the opening one (ie, they typed "@catch
32553 NSException *e)". Parse the closing parenthesis and keep
32555 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
32556 cp_lexer_consume_token (parser
->lexer
);
32558 /* If these is no immediate closing parenthesis, the user
32559 probably doesn't know that parenthesis are required at
32560 all (ie, they typed "@catch NSException *e"). So, just
32561 forget about the closing parenthesis and keep going. */
32563 objc_begin_catch_clause (parameter_declaration
);
32564 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
32565 objc_finish_catch_clause ();
32567 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
32569 cp_lexer_consume_token (parser
->lexer
);
32570 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32571 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
32572 node, lest it get absorbed into the surrounding block. */
32573 stmt
= push_stmt_list ();
32574 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
32575 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
32578 return objc_finish_try_stmt ();
32581 /* Parse an Objective-C synchronized statement.
32583 objc-synchronized-stmt:
32584 @synchronized ( expression ) compound-statement
32586 Returns NULL_TREE. */
32589 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
32591 location_t location
;
32594 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
32596 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
32597 objc_maybe_warn_exceptions (location
);
32598 matching_parens parens
;
32599 parens
.require_open (parser
);
32600 lock
= cp_parser_expression (parser
);
32601 parens
.require_close (parser
);
32603 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
32604 node, lest it get absorbed into the surrounding block. */
32605 stmt
= push_stmt_list ();
32606 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
32608 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
32611 /* Parse an Objective-C throw statement.
32614 @throw assignment-expression [opt] ;
32616 Returns a constructed '@throw' statement. */
32619 cp_parser_objc_throw_statement (cp_parser
*parser
)
32621 tree expr
= NULL_TREE
;
32622 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32624 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
32626 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
32627 expr
= cp_parser_expression (parser
);
32629 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32631 return objc_build_throw_stmt (loc
, expr
);
32634 /* Parse an Objective-C statement. */
32637 cp_parser_objc_statement (cp_parser
* parser
)
32639 /* Try to figure out what kind of declaration is present. */
32640 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
32642 switch (kwd
->keyword
)
32645 return cp_parser_objc_try_catch_finally_statement (parser
);
32646 case RID_AT_SYNCHRONIZED
:
32647 return cp_parser_objc_synchronized_statement (parser
);
32649 return cp_parser_objc_throw_statement (parser
);
32651 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
32653 cp_parser_skip_to_end_of_block_or_statement (parser
);
32656 return error_mark_node
;
32659 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
32660 look ahead to see if an objc keyword follows the attributes. This
32661 is to detect the use of prefix attributes on ObjC @interface and
32665 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
32667 cp_lexer_save_tokens (parser
->lexer
);
32668 *attrib
= cp_parser_attributes_opt (parser
);
32669 gcc_assert (*attrib
);
32670 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
32672 cp_lexer_commit_tokens (parser
->lexer
);
32675 cp_lexer_rollback_tokens (parser
->lexer
);
32679 /* This routine is a minimal replacement for
32680 c_parser_struct_declaration () used when parsing the list of
32681 types/names or ObjC++ properties. For example, when parsing the
32684 @property (readonly) int a, b, c;
32686 this function is responsible for parsing "int a, int b, int c" and
32687 returning the declarations as CHAIN of DECLs.
32689 TODO: Share this code with cp_parser_objc_class_ivars. It's very
32690 similar parsing. */
32692 cp_parser_objc_struct_declaration (cp_parser
*parser
)
32694 tree decls
= NULL_TREE
;
32695 cp_decl_specifier_seq declspecs
;
32696 int decl_class_or_enum_p
;
32697 tree prefix_attributes
;
32699 cp_parser_decl_specifier_seq (parser
,
32700 CP_PARSER_FLAGS_NONE
,
32702 &decl_class_or_enum_p
);
32704 if (declspecs
.type
== error_mark_node
)
32705 return error_mark_node
;
32707 /* auto, register, static, extern, mutable. */
32708 if (declspecs
.storage_class
!= sc_none
)
32710 cp_parser_error (parser
, "invalid type for property");
32711 declspecs
.storage_class
= sc_none
;
32714 /* thread_local. */
32715 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
32717 cp_parser_error (parser
, "invalid type for property");
32718 declspecs
.locations
[ds_thread
] = 0;
32722 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
32724 cp_parser_error (parser
, "invalid type for property");
32725 declspecs
.locations
[ds_typedef
] = 0;
32728 prefix_attributes
= declspecs
.attributes
;
32729 declspecs
.attributes
= NULL_TREE
;
32731 /* Keep going until we hit the `;' at the end of the declaration. */
32732 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
32734 tree attributes
, first_attribute
, decl
;
32735 cp_declarator
*declarator
;
32738 /* Parse the declarator. */
32739 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
32740 CP_PARSER_FLAGS_NONE
,
32741 NULL
, NULL
, false, false, false);
32743 /* Look for attributes that apply to the ivar. */
32744 attributes
= cp_parser_attributes_opt (parser
);
32745 /* Remember which attributes are prefix attributes and
32747 first_attribute
= attributes
;
32748 /* Combine the attributes. */
32749 attributes
= attr_chainon (prefix_attributes
, attributes
);
32751 decl
= grokfield (declarator
, &declspecs
,
32752 NULL_TREE
, /*init_const_expr_p=*/false,
32753 NULL_TREE
, attributes
);
32755 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
32756 return error_mark_node
;
32758 /* Reset PREFIX_ATTRIBUTES. */
32759 if (attributes
!= error_mark_node
)
32761 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
32762 attributes
= TREE_CHAIN (attributes
);
32764 TREE_CHAIN (attributes
) = NULL_TREE
;
32767 DECL_CHAIN (decl
) = decls
;
32770 token
= cp_lexer_peek_token (parser
->lexer
);
32771 if (token
->type
== CPP_COMMA
)
32773 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
32782 /* Parse an Objective-C @property declaration. The syntax is:
32784 objc-property-declaration:
32785 '@property' objc-property-attributes[opt] struct-declaration ;
32787 objc-property-attributes:
32788 '(' objc-property-attribute-list ')'
32790 objc-property-attribute-list:
32791 objc-property-attribute
32792 objc-property-attribute-list, objc-property-attribute
32794 objc-property-attribute
32795 'getter' = identifier
32796 'setter' = identifier
32805 @property NSString *name;
32806 @property (readonly) id object;
32807 @property (retain, nonatomic, getter=getTheName) id name;
32808 @property int a, b, c;
32810 PS: This function is identical to
32811 c_parser_objc_at_property_declaration for C. Keep them in sync. */
32813 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
32815 /* The following variables hold the attributes of the properties as
32816 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
32817 seen. When we see an attribute, we set them to 'true' (if they
32818 are boolean properties) or to the identifier (if they have an
32819 argument, ie, for getter and setter). Note that here we only
32820 parse the list of attributes, check the syntax and accumulate the
32821 attributes that we find. objc_add_property_declaration() will
32822 then process the information. */
32823 bool property_assign
= false;
32824 bool property_copy
= false;
32825 tree property_getter_ident
= NULL_TREE
;
32826 bool property_nonatomic
= false;
32827 bool property_readonly
= false;
32828 bool property_readwrite
= false;
32829 bool property_retain
= false;
32830 tree property_setter_ident
= NULL_TREE
;
32832 /* 'properties' is the list of properties that we read. Usually a
32833 single one, but maybe more (eg, in "@property int a, b, c;" there
32838 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32840 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
32842 /* Parse the optional attribute list... */
32843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
32846 matching_parens parens
;
32847 parens
.consume_open (parser
);
32851 bool syntax_error
= false;
32852 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32855 if (token
->type
!= CPP_NAME
)
32857 cp_parser_error (parser
, "expected identifier");
32860 keyword
= C_RID_CODE (token
->u
.value
);
32861 cp_lexer_consume_token (parser
->lexer
);
32864 case RID_ASSIGN
: property_assign
= true; break;
32865 case RID_COPY
: property_copy
= true; break;
32866 case RID_NONATOMIC
: property_nonatomic
= true; break;
32867 case RID_READONLY
: property_readonly
= true; break;
32868 case RID_READWRITE
: property_readwrite
= true; break;
32869 case RID_RETAIN
: property_retain
= true; break;
32873 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
32875 if (keyword
== RID_GETTER
)
32876 cp_parser_error (parser
,
32877 "missing %<=%> (after %<getter%> attribute)");
32879 cp_parser_error (parser
,
32880 "missing %<=%> (after %<setter%> attribute)");
32881 syntax_error
= true;
32884 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
32885 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser
->lexer
)->type
))
32887 cp_parser_error (parser
, "expected identifier");
32888 syntax_error
= true;
32891 if (keyword
== RID_SETTER
)
32893 if (property_setter_ident
!= NULL_TREE
)
32895 cp_parser_error (parser
, "the %<setter%> attribute may only be specified once");
32896 cp_lexer_consume_token (parser
->lexer
);
32899 property_setter_ident
= cp_parser_objc_selector (parser
);
32900 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
32901 cp_parser_error (parser
, "setter name must terminate with %<:%>");
32903 cp_lexer_consume_token (parser
->lexer
);
32907 if (property_getter_ident
!= NULL_TREE
)
32909 cp_parser_error (parser
, "the %<getter%> attribute may only be specified once");
32910 cp_lexer_consume_token (parser
->lexer
);
32913 property_getter_ident
= cp_parser_objc_selector (parser
);
32917 cp_parser_error (parser
, "unknown property attribute");
32918 syntax_error
= true;
32925 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
32926 cp_lexer_consume_token (parser
->lexer
);
32931 /* FIXME: "@property (setter, assign);" will generate a spurious
32932 "error: expected ‘)’ before ‘,’ token". This is because
32933 cp_parser_require, unlike the C counterpart, will produce an
32934 error even if we are in error recovery. */
32935 if (!parens
.require_close (parser
))
32937 cp_parser_skip_to_closing_parenthesis (parser
,
32938 /*recovering=*/true,
32939 /*or_comma=*/false,
32940 /*consume_paren=*/true);
32944 /* ... and the property declaration(s). */
32945 properties
= cp_parser_objc_struct_declaration (parser
);
32947 if (properties
== error_mark_node
)
32949 cp_parser_skip_to_end_of_statement (parser
);
32950 /* If the next token is now a `;', consume it. */
32951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
32952 cp_lexer_consume_token (parser
->lexer
);
32956 if (properties
== NULL_TREE
)
32957 cp_parser_error (parser
, "expected identifier");
32960 /* Comma-separated properties are chained together in
32961 reverse order; add them one by one. */
32962 properties
= nreverse (properties
);
32964 for (; properties
; properties
= TREE_CHAIN (properties
))
32965 objc_add_property_declaration (loc
, copy_node (properties
),
32966 property_readonly
, property_readwrite
,
32967 property_assign
, property_retain
,
32968 property_copy
, property_nonatomic
,
32969 property_getter_ident
, property_setter_ident
);
32972 cp_parser_consume_semicolon_at_end_of_statement (parser
);
32975 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
32977 objc-synthesize-declaration:
32978 @synthesize objc-synthesize-identifier-list ;
32980 objc-synthesize-identifier-list:
32981 objc-synthesize-identifier
32982 objc-synthesize-identifier-list, objc-synthesize-identifier
32984 objc-synthesize-identifier
32986 identifier = identifier
32989 @synthesize MyProperty;
32990 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
32992 PS: This function is identical to c_parser_objc_at_synthesize_declaration
32993 for C. Keep them in sync.
32996 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
32998 tree list
= NULL_TREE
;
33000 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33002 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
33005 tree property
, ivar
;
33006 property
= cp_parser_identifier (parser
);
33007 if (property
== error_mark_node
)
33009 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33012 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
33014 cp_lexer_consume_token (parser
->lexer
);
33015 ivar
= cp_parser_identifier (parser
);
33016 if (ivar
== error_mark_node
)
33018 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33024 list
= chainon (list
, build_tree_list (ivar
, property
));
33025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33026 cp_lexer_consume_token (parser
->lexer
);
33030 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33031 objc_add_synthesize_declaration (loc
, list
);
33034 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
33036 objc-dynamic-declaration:
33037 @dynamic identifier-list ;
33040 @dynamic MyProperty;
33041 @dynamic MyProperty, AnotherProperty;
33043 PS: This function is identical to c_parser_objc_at_dynamic_declaration
33044 for C. Keep them in sync.
33047 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
33049 tree list
= NULL_TREE
;
33051 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33053 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
33057 property
= cp_parser_identifier (parser
);
33058 if (property
== error_mark_node
)
33060 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33063 list
= chainon (list
, build_tree_list (NULL
, property
));
33064 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33065 cp_lexer_consume_token (parser
->lexer
);
33069 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33070 objc_add_dynamic_declaration (loc
, list
);
33074 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
33076 /* Returns name of the next clause.
33077 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
33078 the token is not consumed. Otherwise appropriate pragma_omp_clause is
33079 returned and the token is consumed. */
33081 static pragma_omp_clause
33082 cp_parser_omp_clause_name (cp_parser
*parser
)
33084 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
33086 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
33087 result
= PRAGMA_OACC_CLAUSE_AUTO
;
33088 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
33089 result
= PRAGMA_OMP_CLAUSE_IF
;
33090 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
33091 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
33092 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
33093 result
= PRAGMA_OACC_CLAUSE_DELETE
;
33094 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
33095 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
33096 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
33097 result
= PRAGMA_OMP_CLAUSE_FOR
;
33098 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
33100 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
33101 const char *p
= IDENTIFIER_POINTER (id
);
33106 if (!strcmp ("aligned", p
))
33107 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
33108 else if (!strcmp ("async", p
))
33109 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
33112 if (!strcmp ("bind", p
))
33113 result
= PRAGMA_OMP_CLAUSE_BIND
;
33116 if (!strcmp ("collapse", p
))
33117 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
33118 else if (!strcmp ("copy", p
))
33119 result
= PRAGMA_OACC_CLAUSE_COPY
;
33120 else if (!strcmp ("copyin", p
))
33121 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
33122 else if (!strcmp ("copyout", p
))
33123 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
33124 else if (!strcmp ("copyprivate", p
))
33125 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
33126 else if (!strcmp ("create", p
))
33127 result
= PRAGMA_OACC_CLAUSE_CREATE
;
33130 if (!strcmp ("defaultmap", p
))
33131 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
33132 else if (!strcmp ("depend", p
))
33133 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
33134 else if (!strcmp ("device", p
))
33135 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
33136 else if (!strcmp ("deviceptr", p
))
33137 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
33138 else if (!strcmp ("device_resident", p
))
33139 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
33140 else if (!strcmp ("device_type", p
))
33141 result
= PRAGMA_OMP_CLAUSE_DEVICE_TYPE
;
33142 else if (!strcmp ("dist_schedule", p
))
33143 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
33146 if (!strcmp ("final", p
))
33147 result
= PRAGMA_OMP_CLAUSE_FINAL
;
33148 else if (!strcmp ("finalize", p
))
33149 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
33150 else if (!strcmp ("firstprivate", p
))
33151 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
33152 else if (!strcmp ("from", p
))
33153 result
= PRAGMA_OMP_CLAUSE_FROM
;
33156 if (!strcmp ("gang", p
))
33157 result
= PRAGMA_OACC_CLAUSE_GANG
;
33158 else if (!strcmp ("grainsize", p
))
33159 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
33162 if (!strcmp ("hint", p
))
33163 result
= PRAGMA_OMP_CLAUSE_HINT
;
33164 else if (!strcmp ("host", p
))
33165 result
= PRAGMA_OACC_CLAUSE_HOST
;
33168 if (!strcmp ("if_present", p
))
33169 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
33170 else if (!strcmp ("in_reduction", p
))
33171 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
33172 else if (!strcmp ("inbranch", p
))
33173 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
33174 else if (!strcmp ("independent", p
))
33175 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
33176 else if (!strcmp ("is_device_ptr", p
))
33177 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
33180 if (!strcmp ("lastprivate", p
))
33181 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
33182 else if (!strcmp ("linear", p
))
33183 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
33184 else if (!strcmp ("link", p
))
33185 result
= PRAGMA_OMP_CLAUSE_LINK
;
33188 if (!strcmp ("map", p
))
33189 result
= PRAGMA_OMP_CLAUSE_MAP
;
33190 else if (!strcmp ("mergeable", p
))
33191 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
33194 if (!strcmp ("nogroup", p
))
33195 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
33196 else if (!strcmp ("nontemporal", p
))
33197 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
33198 else if (!strcmp ("notinbranch", p
))
33199 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
33200 else if (!strcmp ("nowait", p
))
33201 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
33202 else if (!strcmp ("num_gangs", p
))
33203 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
33204 else if (!strcmp ("num_tasks", p
))
33205 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
33206 else if (!strcmp ("num_teams", p
))
33207 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
33208 else if (!strcmp ("num_threads", p
))
33209 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
33210 else if (!strcmp ("num_workers", p
))
33211 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
33214 if (!strcmp ("ordered", p
))
33215 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
33216 else if (!strcmp ("order", p
))
33217 result
= PRAGMA_OMP_CLAUSE_ORDER
;
33220 if (!strcmp ("parallel", p
))
33221 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
33222 else if (!strcmp ("present", p
))
33223 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
33224 else if (!strcmp ("present_or_copy", p
)
33225 || !strcmp ("pcopy", p
))
33226 result
= PRAGMA_OACC_CLAUSE_COPY
;
33227 else if (!strcmp ("present_or_copyin", p
)
33228 || !strcmp ("pcopyin", p
))
33229 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
33230 else if (!strcmp ("present_or_copyout", p
)
33231 || !strcmp ("pcopyout", p
))
33232 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
33233 else if (!strcmp ("present_or_create", p
)
33234 || !strcmp ("pcreate", p
))
33235 result
= PRAGMA_OACC_CLAUSE_CREATE
;
33236 else if (!strcmp ("priority", p
))
33237 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
33238 else if (!strcmp ("proc_bind", p
))
33239 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
33242 if (!strcmp ("reduction", p
))
33243 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
33246 if (!strcmp ("safelen", p
))
33247 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
33248 else if (!strcmp ("schedule", p
))
33249 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
33250 else if (!strcmp ("sections", p
))
33251 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
33252 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
33253 result
= PRAGMA_OACC_CLAUSE_HOST
;
33254 else if (!strcmp ("seq", p
))
33255 result
= PRAGMA_OACC_CLAUSE_SEQ
;
33256 else if (!strcmp ("shared", p
))
33257 result
= PRAGMA_OMP_CLAUSE_SHARED
;
33258 else if (!strcmp ("simd", p
))
33259 result
= PRAGMA_OMP_CLAUSE_SIMD
;
33260 else if (!strcmp ("simdlen", p
))
33261 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
33264 if (!strcmp ("task_reduction", p
))
33265 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
33266 else if (!strcmp ("taskgroup", p
))
33267 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
33268 else if (!strcmp ("thread_limit", p
))
33269 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
33270 else if (!strcmp ("threads", p
))
33271 result
= PRAGMA_OMP_CLAUSE_THREADS
;
33272 else if (!strcmp ("tile", p
))
33273 result
= PRAGMA_OACC_CLAUSE_TILE
;
33274 else if (!strcmp ("to", p
))
33275 result
= PRAGMA_OMP_CLAUSE_TO
;
33278 if (!strcmp ("uniform", p
))
33279 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
33280 else if (!strcmp ("untied", p
))
33281 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
33282 else if (!strcmp ("use_device", p
))
33283 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
33284 else if (!strcmp ("use_device_addr", p
))
33285 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
;
33286 else if (!strcmp ("use_device_ptr", p
))
33287 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
33290 if (!strcmp ("vector", p
))
33291 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
33292 else if (!strcmp ("vector_length", p
))
33293 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
33296 if (!strcmp ("wait", p
))
33297 result
= PRAGMA_OACC_CLAUSE_WAIT
;
33298 else if (!strcmp ("worker", p
))
33299 result
= PRAGMA_OACC_CLAUSE_WORKER
;
33304 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
33305 cp_lexer_consume_token (parser
->lexer
);
33310 /* Validate that a clause of the given type does not already exist. */
33313 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
33314 const char *name
, location_t location
)
33316 if (omp_find_clause (clauses
, code
))
33317 error_at (location
, "too many %qs clauses", name
);
33323 variable-list , identifier
33325 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
33326 colon). An opening parenthesis will have been consumed by the caller.
33328 If KIND is nonzero, create the appropriate node and install the decl
33329 in OMP_CLAUSE_DECL and add the node to the head of the list.
33331 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
33332 return the list created.
33334 COLON can be NULL if only closing parenthesis should end the list,
33335 or pointer to bool which will receive false if the list is terminated
33336 by closing parenthesis or true if the list is terminated by colon. */
33339 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
33340 tree list
, bool *colon
)
33343 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
33346 parser
->colon_corrects_to_scope_p
= false;
33353 if (kind
== OMP_CLAUSE_DEPEND
)
33354 cp_parser_parse_tentatively (parser
);
33355 token
= cp_lexer_peek_token (parser
->lexer
);
33357 && current_class_ptr
33358 && cp_parser_is_keyword (token
, RID_THIS
))
33360 decl
= finish_this_expr ();
33361 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
33362 || CONVERT_EXPR_P (decl
))
33363 decl
= TREE_OPERAND (decl
, 0);
33364 cp_lexer_consume_token (parser
->lexer
);
33366 else if (cp_parser_is_keyword (token
, RID_FUNCTION_NAME
)
33367 || cp_parser_is_keyword (token
, RID_PRETTY_FUNCTION_NAME
)
33368 || cp_parser_is_keyword (token
, RID_C99_FUNCTION_NAME
))
33371 decl
= cp_parser_primary_expression (parser
, false, false, false,
33376 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
33377 /*check_dependency_p=*/true,
33378 /*template_p=*/NULL
,
33379 /*declarator_p=*/false,
33380 /*optional_p=*/false);
33381 if (name
== error_mark_node
)
33383 if (kind
== OMP_CLAUSE_DEPEND
33384 && cp_parser_simulate_error (parser
))
33385 goto depend_lvalue
;
33389 if (identifier_p (name
))
33390 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
33393 if (decl
== error_mark_node
)
33395 if (kind
== OMP_CLAUSE_DEPEND
33396 && cp_parser_simulate_error (parser
))
33397 goto depend_lvalue
;
33398 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
33402 if (decl
== error_mark_node
)
33404 else if (kind
!= 0)
33408 case OMP_CLAUSE__CACHE_
:
33409 /* The OpenACC cache directive explicitly only allows "array
33410 elements or subarrays". */
33411 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
33413 error_at (token
->location
, "expected %<[%>");
33414 decl
= error_mark_node
;
33418 case OMP_CLAUSE_MAP
:
33419 case OMP_CLAUSE_FROM
:
33420 case OMP_CLAUSE_TO
:
33421 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
))
33424 = cp_lexer_peek_token (parser
->lexer
)->location
;
33425 cp_id_kind idk
= CP_ID_KIND_NONE
;
33426 cp_lexer_consume_token (parser
->lexer
);
33427 decl
= convert_from_reference (decl
);
33429 = cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
33434 case OMP_CLAUSE_DEPEND
:
33435 case OMP_CLAUSE_REDUCTION
:
33436 case OMP_CLAUSE_IN_REDUCTION
:
33437 case OMP_CLAUSE_TASK_REDUCTION
:
33438 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
33440 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
33442 parser
->colon_corrects_to_scope_p
= false;
33443 cp_lexer_consume_token (parser
->lexer
);
33444 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
33445 low_bound
= cp_parser_expression (parser
);
33447 parser
->colon_corrects_to_scope_p
33448 = saved_colon_corrects_to_scope_p
;
33449 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
33450 length
= integer_one_node
;
33453 /* Look for `:'. */
33454 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33456 if (kind
== OMP_CLAUSE_DEPEND
33457 && cp_parser_simulate_error (parser
))
33458 goto depend_lvalue
;
33461 if (kind
== OMP_CLAUSE_DEPEND
)
33462 cp_parser_commit_to_tentative_parse (parser
);
33463 if (!cp_lexer_next_token_is (parser
->lexer
,
33465 length
= cp_parser_expression (parser
);
33467 /* Look for the closing `]'. */
33468 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
33471 if (kind
== OMP_CLAUSE_DEPEND
33472 && cp_parser_simulate_error (parser
))
33473 goto depend_lvalue
;
33477 decl
= tree_cons (low_bound
, length
, decl
);
33484 if (kind
== OMP_CLAUSE_DEPEND
)
33486 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
33487 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
33488 && cp_parser_simulate_error (parser
))
33491 cp_parser_abort_tentative_parse (parser
);
33492 decl
= cp_parser_assignment_expression (parser
, NULL
,
33496 cp_parser_parse_definitely (parser
);
33499 tree u
= build_omp_clause (token
->location
, kind
);
33500 OMP_CLAUSE_DECL (u
) = decl
;
33501 OMP_CLAUSE_CHAIN (u
) = list
;
33505 list
= tree_cons (decl
, NULL_TREE
, list
);
33508 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
33510 cp_lexer_consume_token (parser
->lexer
);
33514 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33516 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
33519 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33523 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
33527 /* Try to resync to an unnested comma. Copied from
33528 cp_parser_parenthesized_expression_list. */
33531 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
33532 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
33533 /*recovering=*/true,
33535 /*consume_paren=*/true);
33543 /* Similarly, but expect leading and trailing parenthesis. This is a very
33544 common case for omp clauses. */
33547 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
)
33549 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33550 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
);
33555 copy ( variable-list )
33556 copyin ( variable-list )
33557 copyout ( variable-list )
33558 create ( variable-list )
33559 delete ( variable-list )
33560 present ( variable-list ) */
33563 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
33566 enum gomp_map_kind kind
;
33569 case PRAGMA_OACC_CLAUSE_COPY
:
33570 kind
= GOMP_MAP_TOFROM
;
33572 case PRAGMA_OACC_CLAUSE_COPYIN
:
33573 kind
= GOMP_MAP_TO
;
33575 case PRAGMA_OACC_CLAUSE_COPYOUT
:
33576 kind
= GOMP_MAP_FROM
;
33578 case PRAGMA_OACC_CLAUSE_CREATE
:
33579 kind
= GOMP_MAP_ALLOC
;
33581 case PRAGMA_OACC_CLAUSE_DELETE
:
33582 kind
= GOMP_MAP_RELEASE
;
33584 case PRAGMA_OACC_CLAUSE_DEVICE
:
33585 kind
= GOMP_MAP_FORCE_TO
;
33587 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
33588 kind
= GOMP_MAP_DEVICE_RESIDENT
;
33590 case PRAGMA_OACC_CLAUSE_HOST
:
33591 kind
= GOMP_MAP_FORCE_FROM
;
33593 case PRAGMA_OACC_CLAUSE_LINK
:
33594 kind
= GOMP_MAP_LINK
;
33596 case PRAGMA_OACC_CLAUSE_PRESENT
:
33597 kind
= GOMP_MAP_FORCE_PRESENT
;
33600 gcc_unreachable ();
33603 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
);
33605 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
33606 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
33612 deviceptr ( variable-list ) */
33615 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
33617 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33620 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
33621 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
33622 variable-list must only allow for pointer variables. */
33623 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
33624 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
33626 tree v
= TREE_PURPOSE (t
);
33627 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
33628 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
33629 OMP_CLAUSE_DECL (u
) = v
;
33630 OMP_CLAUSE_CHAIN (u
) = list
;
33645 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
33648 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
33650 tree c
= build_omp_clause (loc
, code
);
33651 OMP_CLAUSE_CHAIN (c
) = list
;
33657 num_gangs ( expression )
33658 num_workers ( expression )
33659 vector_length ( expression ) */
33662 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
33663 const char *str
, tree list
)
33665 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33667 matching_parens parens
;
33668 if (!parens
.require_open (parser
))
33671 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
33673 if (t
== error_mark_node
33674 || !parens
.require_close (parser
))
33676 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33677 /*or_comma=*/false,
33678 /*consume_paren=*/true);
33682 check_no_duplicate_clause (list
, code
, str
, loc
);
33684 tree c
= build_omp_clause (loc
, code
);
33685 OMP_CLAUSE_OPERAND (c
, 0) = t
;
33686 OMP_CLAUSE_CHAIN (c
) = list
;
33692 gang [( gang-arg-list )]
33693 worker [( [num:] int-expr )]
33694 vector [( [length:] int-expr )]
33696 where gang-arg is one of:
33701 and size-expr may be:
33708 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
33709 omp_clause_code kind
,
33710 const char *str
, tree list
)
33712 const char *id
= "num";
33713 cp_lexer
*lexer
= parser
->lexer
;
33714 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
33716 if (kind
== OMP_CLAUSE_VECTOR
)
33719 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
33721 matching_parens parens
;
33722 parens
.consume_open (parser
);
33726 cp_token
*next
= cp_lexer_peek_token (lexer
);
33729 /* Gang static argument. */
33730 if (kind
== OMP_CLAUSE_GANG
33731 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
33733 cp_lexer_consume_token (lexer
);
33735 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33736 goto cleanup_error
;
33739 if (ops
[idx
] != NULL
)
33741 cp_parser_error (parser
, "too many %<static%> arguments");
33742 goto cleanup_error
;
33745 /* Check for the '*' argument. */
33746 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
33747 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
33748 || cp_lexer_nth_token_is (parser
->lexer
, 2,
33751 cp_lexer_consume_token (lexer
);
33752 ops
[idx
] = integer_minus_one_node
;
33754 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
33756 cp_lexer_consume_token (lexer
);
33762 /* Worker num: argument and vector length: arguments. */
33763 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
33764 && id_equal (next
->u
.value
, id
)
33765 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
33767 cp_lexer_consume_token (lexer
); /* id */
33768 cp_lexer_consume_token (lexer
); /* ':' */
33771 /* Now collect the actual argument. */
33772 if (ops
[idx
] != NULL_TREE
)
33774 cp_parser_error (parser
, "unexpected argument");
33775 goto cleanup_error
;
33778 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
33780 if (expr
== error_mark_node
)
33781 goto cleanup_error
;
33783 mark_exp_read (expr
);
33786 if (kind
== OMP_CLAUSE_GANG
33787 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
33789 cp_lexer_consume_token (lexer
);
33796 if (!parens
.require_close (parser
))
33797 goto cleanup_error
;
33800 check_no_duplicate_clause (list
, kind
, str
, loc
);
33802 c
= build_omp_clause (loc
, kind
);
33805 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
33807 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
33808 OMP_CLAUSE_CHAIN (c
) = list
;
33813 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
33818 tile ( size-expr-list ) */
33821 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
33823 tree c
, expr
= error_mark_node
;
33824 tree tile
= NULL_TREE
;
33826 /* Collapse and tile are mutually exclusive. (The spec doesn't say
33827 so, but the spec authors never considered such a case and have
33828 differing opinions on what it might mean, including 'not
33830 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
33831 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
33834 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
33839 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
33842 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
33843 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
33844 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
33846 cp_lexer_consume_token (parser
->lexer
);
33847 expr
= integer_zero_node
;
33850 expr
= cp_parser_constant_expression (parser
);
33852 tile
= tree_cons (NULL_TREE
, expr
, tile
);
33854 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
33856 /* Consume the trailing ')'. */
33857 cp_lexer_consume_token (parser
->lexer
);
33859 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
33860 tile
= nreverse (tile
);
33861 OMP_CLAUSE_TILE_LIST (c
) = tile
;
33862 OMP_CLAUSE_CHAIN (c
) = list
;
33867 Parse wait clause or directive parameters. */
33870 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
33872 vec
<tree
, va_gc
> *args
;
33875 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
33877 /*allow_expansion_p=*/true,
33878 /*non_constant_p=*/NULL
);
33880 if (args
== NULL
|| args
->length () == 0)
33884 cp_parser_error (parser
, "expected integer expression list");
33885 release_tree_vector (args
);
33890 args_tree
= build_tree_list_vec (args
);
33892 release_tree_vector (args
);
33894 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
33896 tree targ
= TREE_VALUE (t
);
33898 if (targ
!= error_mark_node
)
33900 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
33901 error ("%<wait%> expression must be integral");
33904 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
33906 targ
= mark_rvalue_use (targ
);
33907 OMP_CLAUSE_DECL (c
) = targ
;
33908 OMP_CLAUSE_CHAIN (c
) = list
;
33918 wait [( int-expr-list )] */
33921 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
33923 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
33925 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
33926 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
33929 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
33931 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
33932 OMP_CLAUSE_CHAIN (c
) = list
;
33940 collapse ( constant-expression ) */
33943 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
33949 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33950 matching_parens parens
;
33951 if (!parens
.require_open (parser
))
33954 num
= cp_parser_constant_expression (parser
);
33956 if (!parens
.require_close (parser
))
33957 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
33958 /*or_comma=*/false,
33959 /*consume_paren=*/true);
33961 if (num
== error_mark_node
)
33963 num
= fold_non_dependent_expr (num
);
33964 if (!tree_fits_shwi_p (num
)
33965 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
33966 || (n
= tree_to_shwi (num
)) <= 0
33969 error_at (loc
, "collapse argument needs positive constant integer expression");
33973 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
33974 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
33975 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
33976 OMP_CLAUSE_CHAIN (c
) = list
;
33977 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
33983 default ( none | shared )
33986 default ( none | present ) */
33989 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
33990 location_t location
, bool is_oacc
)
33992 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
33995 matching_parens parens
;
33996 if (!parens
.require_open (parser
))
33998 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34000 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34001 const char *p
= IDENTIFIER_POINTER (id
);
34006 if (strcmp ("none", p
) != 0)
34008 kind
= OMP_CLAUSE_DEFAULT_NONE
;
34012 if (strcmp ("present", p
) != 0 || !is_oacc
)
34014 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
34018 if (strcmp ("shared", p
) != 0 || is_oacc
)
34020 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
34027 cp_lexer_consume_token (parser
->lexer
);
34033 cp_parser_error (parser
, "expected %<none%> or %<present%>");
34035 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
34038 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
34039 || !parens
.require_close (parser
))
34040 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34041 /*or_comma=*/false,
34042 /*consume_paren=*/true);
34044 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
34047 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
34048 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
34049 OMP_CLAUSE_CHAIN (c
) = list
;
34050 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
34056 final ( expression ) */
34059 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
34063 matching_parens parens
;
34064 if (!parens
.require_open (parser
))
34067 t
= cp_parser_assignment_expression (parser
);
34069 if (t
== error_mark_node
34070 || !parens
.require_close (parser
))
34071 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34072 /*or_comma=*/false,
34073 /*consume_paren=*/true);
34075 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
34077 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
34078 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
34079 OMP_CLAUSE_CHAIN (c
) = list
;
34088 if ( directive-name-modifier : expression )
34090 directive-name-modifier:
34091 parallel | task | taskloop | target data | target | target update
34092 | target enter data | target exit data
34095 directive-name-modifier:
34096 ... | simd | cancel */
34099 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
34103 enum tree_code if_modifier
= ERROR_MARK
;
34105 matching_parens parens
;
34106 if (!parens
.require_open (parser
))
34109 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34111 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34112 const char *p
= IDENTIFIER_POINTER (id
);
34115 if (strcmp ("cancel", p
) == 0)
34116 if_modifier
= VOID_CST
;
34117 else if (strcmp ("parallel", p
) == 0)
34118 if_modifier
= OMP_PARALLEL
;
34119 else if (strcmp ("simd", p
) == 0)
34120 if_modifier
= OMP_SIMD
;
34121 else if (strcmp ("task", p
) == 0)
34122 if_modifier
= OMP_TASK
;
34123 else if (strcmp ("taskloop", p
) == 0)
34124 if_modifier
= OMP_TASKLOOP
;
34125 else if (strcmp ("target", p
) == 0)
34127 if_modifier
= OMP_TARGET
;
34128 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
34130 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
34131 p
= IDENTIFIER_POINTER (id
);
34132 if (strcmp ("data", p
) == 0)
34133 if_modifier
= OMP_TARGET_DATA
;
34134 else if (strcmp ("update", p
) == 0)
34135 if_modifier
= OMP_TARGET_UPDATE
;
34136 else if (strcmp ("enter", p
) == 0)
34137 if_modifier
= OMP_TARGET_ENTER_DATA
;
34138 else if (strcmp ("exit", p
) == 0)
34139 if_modifier
= OMP_TARGET_EXIT_DATA
;
34140 if (if_modifier
!= OMP_TARGET
)
34145 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
34146 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
34148 if_modifier
= ERROR_MARK
;
34150 if (if_modifier
== OMP_TARGET_ENTER_DATA
34151 || if_modifier
== OMP_TARGET_EXIT_DATA
)
34153 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
34155 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
34156 p
= IDENTIFIER_POINTER (id
);
34157 if (strcmp ("data", p
) == 0)
34163 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
34164 error_at (loc
, "expected %<data%>");
34165 if_modifier
= ERROR_MARK
;
34170 if (if_modifier
!= ERROR_MARK
)
34172 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
34175 cp_lexer_consume_token (parser
->lexer
);
34182 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
34183 error_at (loc
, "expected %<:%>");
34185 if_modifier
= ERROR_MARK
;
34190 t
= cp_parser_assignment_expression (parser
);
34192 if (t
== error_mark_node
34193 || !parens
.require_close (parser
))
34194 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34195 /*or_comma=*/false,
34196 /*consume_paren=*/true);
34198 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34199 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
34201 if (if_modifier
!= ERROR_MARK
34202 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
34204 const char *p
= NULL
;
34205 switch (if_modifier
)
34207 case VOID_CST
: p
= "cancel"; break;
34208 case OMP_PARALLEL
: p
= "parallel"; break;
34209 case OMP_SIMD
: p
= "simd"; break;
34210 case OMP_TASK
: p
= "task"; break;
34211 case OMP_TASKLOOP
: p
= "taskloop"; break;
34212 case OMP_TARGET_DATA
: p
= "target data"; break;
34213 case OMP_TARGET
: p
= "target"; break;
34214 case OMP_TARGET_UPDATE
: p
= "target update"; break;
34215 case OMP_TARGET_ENTER_DATA
: p
= "target enter data"; break;
34216 case OMP_TARGET_EXIT_DATA
: p
= "target exit data"; break;
34217 default: gcc_unreachable ();
34219 error_at (location
, "too many %<if%> clauses with %qs modifier",
34223 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
34226 error_at (location
, "too many %<if%> clauses");
34228 error_at (location
, "too many %<if%> clauses without modifier");
34231 else if (if_modifier
== ERROR_MARK
34232 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
34234 error_at (location
, "if any %<if%> clause has modifier, then all "
34235 "%<if%> clauses have to use modifier");
34240 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
34241 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
34242 OMP_CLAUSE_IF_EXPR (c
) = t
;
34243 OMP_CLAUSE_CHAIN (c
) = list
;
34252 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
34253 tree list
, location_t location
)
34257 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
34260 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
34261 OMP_CLAUSE_CHAIN (c
) = list
;
34269 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
34270 tree list
, location_t location
)
34274 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
34276 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
34277 OMP_CLAUSE_CHAIN (c
) = list
;
34282 num_threads ( expression ) */
34285 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
34286 location_t location
)
34290 matching_parens parens
;
34291 if (!parens
.require_open (parser
))
34294 t
= cp_parser_assignment_expression (parser
);
34296 if (t
== error_mark_node
34297 || !parens
.require_close (parser
))
34298 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34299 /*or_comma=*/false,
34300 /*consume_paren=*/true);
34302 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
34303 "num_threads", location
);
34305 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
34306 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
34307 OMP_CLAUSE_CHAIN (c
) = list
;
34313 num_tasks ( expression ) */
34316 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
34317 location_t location
)
34321 matching_parens parens
;
34322 if (!parens
.require_open (parser
))
34325 t
= cp_parser_assignment_expression (parser
);
34327 if (t
== error_mark_node
34328 || !parens
.require_close (parser
))
34329 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34330 /*or_comma=*/false,
34331 /*consume_paren=*/true);
34333 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
34334 "num_tasks", location
);
34336 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
34337 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
34338 OMP_CLAUSE_CHAIN (c
) = list
;
34344 grainsize ( expression ) */
34347 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
34348 location_t location
)
34352 matching_parens parens
;
34353 if (!parens
.require_open (parser
))
34356 t
= cp_parser_assignment_expression (parser
);
34358 if (t
== error_mark_node
34359 || !parens
.require_close (parser
))
34360 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34361 /*or_comma=*/false,
34362 /*consume_paren=*/true);
34364 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
34365 "grainsize", location
);
34367 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
34368 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
34369 OMP_CLAUSE_CHAIN (c
) = list
;
34375 priority ( expression ) */
34378 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
34379 location_t location
)
34383 matching_parens parens
;
34384 if (!parens
.require_open (parser
))
34387 t
= cp_parser_assignment_expression (parser
);
34389 if (t
== error_mark_node
34390 || !parens
.require_close (parser
))
34391 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34392 /*or_comma=*/false,
34393 /*consume_paren=*/true);
34395 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
34396 "priority", location
);
34398 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
34399 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
34400 OMP_CLAUSE_CHAIN (c
) = list
;
34406 hint ( expression ) */
34409 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
34413 matching_parens parens
;
34414 if (!parens
.require_open (parser
))
34417 t
= cp_parser_assignment_expression (parser
);
34419 if (t
== error_mark_node
34420 || !parens
.require_close (parser
))
34421 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34422 /*or_comma=*/false,
34423 /*consume_paren=*/true);
34425 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
34427 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
34428 OMP_CLAUSE_HINT_EXPR (c
) = t
;
34429 OMP_CLAUSE_CHAIN (c
) = list
;
34435 defaultmap ( tofrom : scalar )
34438 defaultmap ( implicit-behavior [ : variable-category ] ) */
34441 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
34442 location_t location
)
34446 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
34447 enum omp_clause_defaultmap_kind category
34448 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
34450 matching_parens parens
;
34451 if (!parens
.require_open (parser
))
34454 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
34456 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34459 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
34460 "%<tofrom%>, %<firstprivate%>, %<none%> "
34466 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34467 p
= IDENTIFIER_POINTER (id
);
34473 if (strcmp ("alloc", p
) == 0)
34474 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
34476 goto invalid_behavior
;
34480 if (strcmp ("default", p
) == 0)
34481 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
34483 goto invalid_behavior
;
34487 if (strcmp ("firstprivate", p
) == 0)
34488 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
34489 else if (strcmp ("from", p
) == 0)
34490 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
34492 goto invalid_behavior
;
34496 if (strcmp ("none", p
) == 0)
34497 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
34499 goto invalid_behavior
;
34503 if (strcmp ("tofrom", p
) == 0)
34504 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
34505 else if (strcmp ("to", p
) == 0)
34506 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
34508 goto invalid_behavior
;
34512 goto invalid_behavior
;
34514 cp_lexer_consume_token (parser
->lexer
);
34516 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
34518 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34521 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34524 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
34528 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34529 p
= IDENTIFIER_POINTER (id
);
34534 if (strcmp ("aggregate", p
) == 0)
34535 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
34537 goto invalid_category
;
34541 if (strcmp ("pointer", p
) == 0)
34542 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
34544 goto invalid_category
;
34548 if (strcmp ("scalar", p
) == 0)
34549 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
34551 goto invalid_category
;
34555 goto invalid_category
;
34558 cp_lexer_consume_token (parser
->lexer
);
34560 if (!parens
.require_close (parser
))
34563 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
34564 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
34565 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
34566 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
34567 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
34568 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
34570 enum omp_clause_defaultmap_kind cat
= category
;
34571 location_t loc
= OMP_CLAUSE_LOCATION (c
);
34572 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
34573 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
34577 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
34580 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
34583 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
34586 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
34590 gcc_unreachable ();
34593 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
34596 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
34601 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
34602 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
34603 OMP_CLAUSE_CHAIN (c
) = list
;
34607 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34608 /*or_comma=*/false,
34609 /*consume_paren=*/true);
34614 order ( concurrent ) */
34617 cp_parser_omp_clause_order (cp_parser
*parser
, tree list
, location_t location
)
34622 matching_parens parens
;
34623 if (!parens
.require_open (parser
))
34626 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34628 cp_parser_error (parser
, "expected %<concurrent%>");
34633 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34634 p
= IDENTIFIER_POINTER (id
);
34636 if (strcmp (p
, "concurrent") != 0)
34638 cp_parser_error (parser
, "expected %<concurrent%>");
34641 cp_lexer_consume_token (parser
->lexer
);
34642 if (!parens
.require_close (parser
))
34645 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
34646 c
= build_omp_clause (location
, OMP_CLAUSE_ORDER
);
34647 OMP_CLAUSE_CHAIN (c
) = list
;
34651 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34652 /*or_comma=*/false,
34653 /*consume_paren=*/true);
34658 bind ( teams | parallel | thread ) */
34661 cp_parser_omp_clause_bind (cp_parser
*parser
, tree list
,
34662 location_t location
)
34666 enum omp_clause_bind_kind kind
= OMP_CLAUSE_BIND_THREAD
;
34668 matching_parens parens
;
34669 if (!parens
.require_open (parser
))
34672 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34675 cp_parser_error (parser
,
34676 "expected %<teams%>, %<parallel%> or %<thread%>");
34681 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34682 p
= IDENTIFIER_POINTER (id
);
34684 if (strcmp (p
, "teams") == 0)
34685 kind
= OMP_CLAUSE_BIND_TEAMS
;
34686 else if (strcmp (p
, "parallel") == 0)
34687 kind
= OMP_CLAUSE_BIND_PARALLEL
;
34688 else if (strcmp (p
, "thread") != 0)
34690 cp_lexer_consume_token (parser
->lexer
);
34691 if (!parens
.require_close (parser
))
34694 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
34695 c
= build_omp_clause (location
, OMP_CLAUSE_BIND
);
34696 OMP_CLAUSE_BIND_KIND (c
) = kind
;
34697 OMP_CLAUSE_CHAIN (c
) = list
;
34701 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34702 /*or_comma=*/false,
34703 /*consume_paren=*/true);
34711 ordered ( constant-expression ) */
34714 cp_parser_omp_clause_ordered (cp_parser
*parser
,
34715 tree list
, location_t location
)
34717 tree c
, num
= NULL_TREE
;
34720 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
34721 "ordered", location
);
34723 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34725 matching_parens parens
;
34726 parens
.consume_open (parser
);
34728 num
= cp_parser_constant_expression (parser
);
34730 if (!parens
.require_close (parser
))
34731 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34732 /*or_comma=*/false,
34733 /*consume_paren=*/true);
34735 if (num
== error_mark_node
)
34737 num
= fold_non_dependent_expr (num
);
34738 if (!tree_fits_shwi_p (num
)
34739 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
34740 || (n
= tree_to_shwi (num
)) <= 0
34743 error_at (location
,
34744 "ordered argument needs positive constant integer "
34750 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
34751 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
34752 OMP_CLAUSE_CHAIN (c
) = list
;
34757 reduction ( reduction-operator : variable-list )
34759 reduction-operator:
34760 One of: + * - & ^ | && ||
34764 reduction-operator:
34765 One of: + * - & ^ | && || min max
34769 reduction-operator:
34770 One of: + * - & ^ | && ||
34774 reduction ( reduction-modifier, reduction-operator : variable-list )
34775 in_reduction ( reduction-operator : variable-list )
34776 task_reduction ( reduction-operator : variable-list ) */
34779 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
34780 bool is_omp
, tree list
)
34782 enum tree_code code
= ERROR_MARK
;
34783 tree nlist
, c
, id
= NULL_TREE
;
34785 bool inscan
= false;
34787 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
34790 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
34792 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
34793 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
34795 cp_lexer_consume_token (parser
->lexer
);
34796 cp_lexer_consume_token (parser
->lexer
);
34798 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
34799 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
34801 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34802 const char *p
= IDENTIFIER_POINTER (id
);
34803 if (strcmp (p
, "task") == 0)
34805 else if (strcmp (p
, "inscan") == 0)
34807 if (task
|| inscan
)
34809 cp_lexer_consume_token (parser
->lexer
);
34810 cp_lexer_consume_token (parser
->lexer
);
34815 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
34817 case CPP_PLUS
: code
= PLUS_EXPR
; break;
34818 case CPP_MULT
: code
= MULT_EXPR
; break;
34819 case CPP_MINUS
: code
= MINUS_EXPR
; break;
34820 case CPP_AND
: code
= BIT_AND_EXPR
; break;
34821 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
34822 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
34823 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
34824 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
34828 if (code
!= ERROR_MARK
)
34829 cp_lexer_consume_token (parser
->lexer
);
34832 bool saved_colon_corrects_to_scope_p
;
34833 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
34834 parser
->colon_corrects_to_scope_p
= false;
34835 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
34836 /*check_dependency_p=*/true,
34837 /*template_p=*/NULL
,
34838 /*declarator_p=*/false,
34839 /*optional_p=*/false);
34840 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
34841 if (identifier_p (id
))
34843 const char *p
= IDENTIFIER_POINTER (id
);
34845 if (strcmp (p
, "min") == 0)
34847 else if (strcmp (p
, "max") == 0)
34849 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
34851 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
34853 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
34855 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
34856 code
= BIT_AND_EXPR
;
34857 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
34858 code
= BIT_IOR_EXPR
;
34859 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
34860 code
= BIT_XOR_EXPR
;
34861 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
34862 code
= TRUTH_ANDIF_EXPR
;
34863 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
34864 code
= TRUTH_ORIF_EXPR
;
34865 id
= omp_reduction_id (code
, id
, NULL_TREE
);
34866 tree scope
= parser
->scope
;
34868 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
34869 parser
->scope
= NULL_TREE
;
34870 parser
->qualifying_scope
= NULL_TREE
;
34871 parser
->object_scope
= NULL_TREE
;
34875 error ("invalid reduction-identifier");
34877 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34878 /*or_comma=*/false,
34879 /*consume_paren=*/true);
34884 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
34887 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
34889 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
34891 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
34893 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
34895 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
34896 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
34903 schedule ( schedule-kind )
34904 schedule ( schedule-kind , expression )
34907 static | dynamic | guided | runtime | auto
34910 schedule ( schedule-modifier : schedule-kind )
34911 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
34919 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
34922 int modifiers
= 0, nmodifiers
= 0;
34924 matching_parens parens
;
34925 if (!parens
.require_open (parser
))
34928 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
34930 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34932 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34933 const char *p
= IDENTIFIER_POINTER (id
);
34934 if (strcmp ("simd", p
) == 0)
34935 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
34936 else if (strcmp ("monotonic", p
) == 0)
34937 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
34938 else if (strcmp ("nonmonotonic", p
) == 0)
34939 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
34942 cp_lexer_consume_token (parser
->lexer
);
34943 if (nmodifiers
++ == 0
34944 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34945 cp_lexer_consume_token (parser
->lexer
);
34948 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
34953 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34955 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34956 const char *p
= IDENTIFIER_POINTER (id
);
34961 if (strcmp ("dynamic", p
) != 0)
34963 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
34967 if (strcmp ("guided", p
) != 0)
34969 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
34973 if (strcmp ("runtime", p
) != 0)
34975 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
34982 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
34983 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
34984 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
34985 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
34988 cp_lexer_consume_token (parser
->lexer
);
34990 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
34991 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
34992 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
34993 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
34995 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
35000 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35003 cp_lexer_consume_token (parser
->lexer
);
35005 token
= cp_lexer_peek_token (parser
->lexer
);
35006 t
= cp_parser_assignment_expression (parser
);
35008 if (t
== error_mark_node
)
35010 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
35011 error_at (token
->location
, "schedule %<runtime%> does not take "
35012 "a %<chunk_size%> parameter");
35013 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
35014 error_at (token
->location
, "schedule %<auto%> does not take "
35015 "a %<chunk_size%> parameter");
35017 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
35019 if (!parens
.require_close (parser
))
35022 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
35025 OMP_CLAUSE_SCHEDULE_KIND (c
)
35026 = (enum omp_clause_schedule_kind
)
35027 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
35029 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
35030 OMP_CLAUSE_CHAIN (c
) = list
;
35034 cp_parser_error (parser
, "invalid schedule kind");
35036 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35037 /*or_comma=*/false,
35038 /*consume_paren=*/true);
35046 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
35047 tree list
, location_t location
)
35051 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
35053 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
35054 OMP_CLAUSE_CHAIN (c
) = list
;
35063 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
35064 tree list
, location_t location
)
35066 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
35067 tree c
= build_omp_clause (location
, code
);
35068 OMP_CLAUSE_CHAIN (c
) = list
;
35079 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
35080 enum omp_clause_code code
,
35081 tree list
, location_t location
)
35083 tree c
= build_omp_clause (location
, code
);
35084 OMP_CLAUSE_CHAIN (c
) = list
;
35092 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
35093 tree list
, location_t location
)
35095 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
35096 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
35097 OMP_CLAUSE_CHAIN (c
) = list
;
35106 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
35107 enum omp_clause_code code
,
35108 tree list
, location_t location
)
35110 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
35111 tree c
= build_omp_clause (location
, code
);
35112 OMP_CLAUSE_CHAIN (c
) = list
;
35117 num_teams ( expression ) */
35120 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
35121 location_t location
)
35125 matching_parens parens
;
35126 if (!parens
.require_open (parser
))
35129 t
= cp_parser_assignment_expression (parser
);
35131 if (t
== error_mark_node
35132 || !parens
.require_close (parser
))
35133 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35134 /*or_comma=*/false,
35135 /*consume_paren=*/true);
35137 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
35138 "num_teams", location
);
35140 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
35141 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
35142 OMP_CLAUSE_CHAIN (c
) = list
;
35148 thread_limit ( expression ) */
35151 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
35152 location_t location
)
35156 matching_parens parens
;
35157 if (!parens
.require_open (parser
))
35160 t
= cp_parser_assignment_expression (parser
);
35162 if (t
== error_mark_node
35163 || !parens
.require_close (parser
))
35164 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35165 /*or_comma=*/false,
35166 /*consume_paren=*/true);
35168 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
35169 "thread_limit", location
);
35171 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
35172 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
35173 OMP_CLAUSE_CHAIN (c
) = list
;
35179 aligned ( variable-list )
35180 aligned ( variable-list : constant-expression ) */
35183 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
35185 tree nlist
, c
, alignment
= NULL_TREE
;
35188 matching_parens parens
;
35189 if (!parens
.require_open (parser
))
35192 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
35197 alignment
= cp_parser_constant_expression (parser
);
35199 if (!parens
.require_close (parser
))
35200 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35201 /*or_comma=*/false,
35202 /*consume_paren=*/true);
35204 if (alignment
== error_mark_node
)
35205 alignment
= NULL_TREE
;
35208 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35209 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
35215 lastprivate ( variable-list )
35218 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
35221 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
35223 bool conditional
= false;
35225 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35228 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
35229 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
35231 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35232 const char *p
= IDENTIFIER_POINTER (id
);
35234 if (strcmp ("conditional", p
) == 0)
35236 conditional
= true;
35237 cp_lexer_consume_token (parser
->lexer
);
35238 cp_lexer_consume_token (parser
->lexer
);
35242 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
35246 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35247 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
35252 linear ( variable-list )
35253 linear ( variable-list : expression )
35256 linear ( modifier ( variable-list ) )
35257 linear ( modifier ( variable-list ) : expression ) */
35260 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
35263 tree nlist
, c
, step
= integer_one_node
;
35265 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
35267 matching_parens parens
;
35268 if (!parens
.require_open (parser
))
35271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35273 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35274 const char *p
= IDENTIFIER_POINTER (id
);
35276 if (strcmp ("ref", p
) == 0)
35277 kind
= OMP_CLAUSE_LINEAR_REF
;
35278 else if (strcmp ("val", p
) == 0)
35279 kind
= OMP_CLAUSE_LINEAR_VAL
;
35280 else if (strcmp ("uval", p
) == 0)
35281 kind
= OMP_CLAUSE_LINEAR_UVAL
;
35282 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
35283 cp_lexer_consume_token (parser
->lexer
);
35285 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
35288 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
35289 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
35293 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
35294 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
35296 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
35297 else if (!parens
.require_close (parser
))
35298 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35299 /*or_comma=*/false,
35300 /*consume_paren=*/true);
35307 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
35308 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
35310 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
35311 cp_parser_parse_tentatively (parser
);
35312 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
35313 /*check_dependency_p=*/true,
35314 /*template_p=*/NULL
,
35315 /*declarator_p=*/false,
35316 /*optional_p=*/false);
35317 if (step
!= error_mark_node
)
35318 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
35319 if (step
== error_mark_node
)
35322 cp_parser_abort_tentative_parse (parser
);
35324 else if (!cp_parser_parse_definitely (parser
))
35328 step
= cp_parser_assignment_expression (parser
);
35330 if (!parens
.require_close (parser
))
35331 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35332 /*or_comma=*/false,
35333 /*consume_paren=*/true);
35335 if (step
== error_mark_node
)
35339 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35341 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
35342 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
35349 safelen ( constant-expression ) */
35352 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
35353 location_t location
)
35357 matching_parens parens
;
35358 if (!parens
.require_open (parser
))
35361 t
= cp_parser_constant_expression (parser
);
35363 if (t
== error_mark_node
35364 || !parens
.require_close (parser
))
35365 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35366 /*or_comma=*/false,
35367 /*consume_paren=*/true);
35369 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
35371 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
35372 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
35373 OMP_CLAUSE_CHAIN (c
) = list
;
35379 simdlen ( constant-expression ) */
35382 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
35383 location_t location
)
35387 matching_parens parens
;
35388 if (!parens
.require_open (parser
))
35391 t
= cp_parser_constant_expression (parser
);
35393 if (t
== error_mark_node
35394 || !parens
.require_close (parser
))
35395 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35396 /*or_comma=*/false,
35397 /*consume_paren=*/true);
35399 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
35401 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
35402 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
35403 OMP_CLAUSE_CHAIN (c
) = list
;
35410 identifier [+/- integer]
35411 vec , identifier [+/- integer]
35415 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
35420 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
35422 cp_parser_error (parser
, "expected identifier");
35426 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35428 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35429 tree t
, identifier
= cp_parser_identifier (parser
);
35430 tree addend
= NULL
;
35432 if (identifier
== error_mark_node
)
35433 t
= error_mark_node
;
35436 t
= cp_parser_lookup_name_simple
35437 (parser
, identifier
,
35438 cp_lexer_peek_token (parser
->lexer
)->location
);
35439 if (t
== error_mark_node
)
35440 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
35445 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
35447 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
35449 addend
= integer_zero_node
;
35450 goto add_to_vector
;
35452 cp_lexer_consume_token (parser
->lexer
);
35454 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
35456 cp_parser_error (parser
, "expected integer");
35460 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35461 if (TREE_CODE (addend
) != INTEGER_CST
)
35463 cp_parser_error (parser
, "expected integer");
35466 cp_lexer_consume_token (parser
->lexer
);
35469 if (t
!= error_mark_node
)
35471 vec
= tree_cons (addend
, t
, vec
);
35473 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
35476 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
35479 cp_lexer_consume_token (parser
->lexer
);
35482 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
35484 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
35485 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
35486 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
35487 OMP_CLAUSE_CHAIN (u
) = list
;
35494 iterators ( iterators-definition )
35496 iterators-definition:
35498 iterator-specifier , iterators-definition
35500 iterator-specifier:
35501 identifier = range-specification
35502 iterator-type identifier = range-specification
35504 range-specification:
35506 begin : end : step */
35509 cp_parser_omp_iterators (cp_parser
*parser
)
35511 tree ret
= NULL_TREE
, *last
= &ret
;
35512 cp_lexer_consume_token (parser
->lexer
);
35514 matching_parens parens
;
35515 if (!parens
.require_open (parser
))
35516 return error_mark_node
;
35518 bool saved_colon_corrects_to_scope_p
35519 = parser
->colon_corrects_to_scope_p
;
35520 bool saved_colon_doesnt_start_class_def_p
35521 = parser
->colon_doesnt_start_class_def_p
;
35526 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
35527 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
35528 iter_type
= integer_type_node
;
35531 const char *saved_message
35532 = parser
->type_definition_forbidden_message
;
35533 parser
->type_definition_forbidden_message
35534 = G_("types may not be defined in iterator type");
35536 iter_type
= cp_parser_type_id (parser
);
35538 parser
->type_definition_forbidden_message
= saved_message
;
35541 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35542 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
35544 cp_parser_error (parser
, "expected identifier");
35548 tree id
= cp_parser_identifier (parser
);
35549 if (id
== error_mark_node
)
35552 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
35555 parser
->colon_corrects_to_scope_p
= false;
35556 parser
->colon_doesnt_start_class_def_p
= true;
35557 tree begin
= cp_parser_assignment_expression (parser
);
35559 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35562 tree end
= cp_parser_assignment_expression (parser
);
35564 tree step
= integer_one_node
;
35565 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
35567 cp_lexer_consume_token (parser
->lexer
);
35568 step
= cp_parser_assignment_expression (parser
);
35571 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
35572 DECL_ARTIFICIAL (iter_var
) = 1;
35573 DECL_CONTEXT (iter_var
) = current_function_decl
;
35574 pushdecl (iter_var
);
35576 *last
= make_tree_vec (6);
35577 TREE_VEC_ELT (*last
, 0) = iter_var
;
35578 TREE_VEC_ELT (*last
, 1) = begin
;
35579 TREE_VEC_ELT (*last
, 2) = end
;
35580 TREE_VEC_ELT (*last
, 3) = step
;
35581 last
= &TREE_CHAIN (*last
);
35583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35585 cp_lexer_consume_token (parser
->lexer
);
35592 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
35593 parser
->colon_doesnt_start_class_def_p
35594 = saved_colon_doesnt_start_class_def_p
;
35596 if (!parens
.require_close (parser
))
35597 cp_parser_skip_to_closing_parenthesis (parser
,
35598 /*recovering=*/true,
35599 /*or_comma=*/false,
35600 /*consume_paren=*/true);
35602 return ret
? ret
: error_mark_node
;
35606 depend ( depend-kind : variable-list )
35614 depend ( sink : vec )
35617 depend ( depend-modifier , depend-kind: variable-list )
35620 in | out | inout | mutexinoutset | depobj
35623 iterator ( iterators-definition ) */
35626 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
35628 tree nlist
, c
, iterators
= NULL_TREE
;
35629 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
35631 matching_parens parens
;
35632 if (!parens
.require_open (parser
))
35637 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
35640 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35641 const char *p
= IDENTIFIER_POINTER (id
);
35643 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
35645 begin_scope (sk_omp
, NULL
);
35646 iterators
= cp_parser_omp_iterators (parser
);
35647 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
35650 if (strcmp ("in", p
) == 0)
35651 kind
= OMP_CLAUSE_DEPEND_IN
;
35652 else if (strcmp ("inout", p
) == 0)
35653 kind
= OMP_CLAUSE_DEPEND_INOUT
;
35654 else if (strcmp ("mutexinoutset", p
) == 0)
35655 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
35656 else if (strcmp ("out", p
) == 0)
35657 kind
= OMP_CLAUSE_DEPEND_OUT
;
35658 else if (strcmp ("depobj", p
) == 0)
35659 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
35660 else if (strcmp ("sink", p
) == 0)
35661 kind
= OMP_CLAUSE_DEPEND_SINK
;
35662 else if (strcmp ("source", p
) == 0)
35663 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
35670 cp_lexer_consume_token (parser
->lexer
);
35673 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
35675 poplevel (0, 1, 0);
35676 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
35677 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
35678 iterators
= NULL_TREE
;
35681 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
35683 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
35684 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
35685 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
35686 OMP_CLAUSE_CHAIN (c
) = list
;
35687 if (!parens
.require_close (parser
))
35688 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35689 /*or_comma=*/false,
35690 /*consume_paren=*/true);
35694 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35697 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
35698 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
35701 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
35706 tree block
= poplevel (1, 1, 0);
35707 if (iterators
== error_mark_node
)
35708 iterators
= NULL_TREE
;
35710 TREE_VEC_ELT (iterators
, 5) = block
;
35713 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35715 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
35717 OMP_CLAUSE_DECL (c
)
35718 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
35724 cp_parser_error (parser
, "invalid depend kind");
35727 poplevel (0, 1, 0);
35728 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35729 /*or_comma=*/false,
35730 /*consume_paren=*/true);
35735 map ( map-kind : variable-list )
35736 map ( variable-list )
35739 alloc | to | from | tofrom
35743 alloc | to | from | tofrom | release | delete
35745 map ( always [,] map-kind: variable-list ) */
35748 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
35751 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
35752 bool always
= false;
35754 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35757 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35759 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35760 const char *p
= IDENTIFIER_POINTER (id
);
35762 if (strcmp ("always", p
) == 0)
35765 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
35767 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
35768 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
35770 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
35774 cp_lexer_consume_token (parser
->lexer
);
35776 cp_lexer_consume_token (parser
->lexer
);
35781 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
35782 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
35784 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35785 const char *p
= IDENTIFIER_POINTER (id
);
35787 if (strcmp ("alloc", p
) == 0)
35788 kind
= GOMP_MAP_ALLOC
;
35789 else if (strcmp ("to", p
) == 0)
35790 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
35791 else if (strcmp ("from", p
) == 0)
35792 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
35793 else if (strcmp ("tofrom", p
) == 0)
35794 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
35795 else if (strcmp ("release", p
) == 0)
35796 kind
= GOMP_MAP_RELEASE
;
35799 cp_parser_error (parser
, "invalid map kind");
35800 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35801 /*or_comma=*/false,
35802 /*consume_paren=*/true);
35805 cp_lexer_consume_token (parser
->lexer
);
35806 cp_lexer_consume_token (parser
->lexer
);
35808 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
35809 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
35811 kind
= GOMP_MAP_DELETE
;
35812 cp_lexer_consume_token (parser
->lexer
);
35813 cp_lexer_consume_token (parser
->lexer
);
35816 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
35819 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35820 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
35826 device ( expression ) */
35829 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
35830 location_t location
)
35834 matching_parens parens
;
35835 if (!parens
.require_open (parser
))
35838 t
= cp_parser_assignment_expression (parser
);
35840 if (t
== error_mark_node
35841 || !parens
.require_close (parser
))
35842 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35843 /*or_comma=*/false,
35844 /*consume_paren=*/true);
35846 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
35847 "device", location
);
35849 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
35850 OMP_CLAUSE_DEVICE_ID (c
) = t
;
35851 OMP_CLAUSE_CHAIN (c
) = list
;
35857 dist_schedule ( static )
35858 dist_schedule ( static , expression ) */
35861 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
35862 location_t location
)
35866 matching_parens parens
;
35867 if (!parens
.require_open (parser
))
35870 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
35872 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
35874 cp_lexer_consume_token (parser
->lexer
);
35876 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
35878 cp_lexer_consume_token (parser
->lexer
);
35880 t
= cp_parser_assignment_expression (parser
);
35882 if (t
== error_mark_node
)
35884 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
35886 if (!parens
.require_close (parser
))
35889 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
35892 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
35893 "dist_schedule", location); */
35894 if (omp_find_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
))
35895 warning_at (location
, 0, "too many %qs clauses", "dist_schedule");
35896 OMP_CLAUSE_CHAIN (c
) = list
;
35900 cp_parser_error (parser
, "invalid dist_schedule kind");
35902 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35903 /*or_comma=*/false,
35904 /*consume_paren=*/true);
35909 proc_bind ( proc-bind-kind )
35912 master | close | spread */
35915 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
35916 location_t location
)
35919 enum omp_clause_proc_bind_kind kind
;
35921 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35924 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35926 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35927 const char *p
= IDENTIFIER_POINTER (id
);
35929 if (strcmp ("master", p
) == 0)
35930 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
35931 else if (strcmp ("close", p
) == 0)
35932 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
35933 else if (strcmp ("spread", p
) == 0)
35934 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
35941 cp_lexer_consume_token (parser
->lexer
);
35942 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
35945 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
35946 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
35948 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
35949 OMP_CLAUSE_CHAIN (c
) = list
;
35953 cp_parser_error (parser
, "invalid depend kind");
35955 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35956 /*or_comma=*/false,
35957 /*consume_paren=*/true);
35962 device_type ( host | nohost | any ) */
35965 cp_parser_omp_clause_device_type (cp_parser
*parser
, tree list
,
35966 location_t location
)
35969 enum omp_clause_device_type_kind kind
;
35971 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35974 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35976 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35977 const char *p
= IDENTIFIER_POINTER (id
);
35979 if (strcmp ("host", p
) == 0)
35980 kind
= OMP_CLAUSE_DEVICE_TYPE_HOST
;
35981 else if (strcmp ("nohost", p
) == 0)
35982 kind
= OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
35983 else if (strcmp ("any", p
) == 0)
35984 kind
= OMP_CLAUSE_DEVICE_TYPE_ANY
;
35991 cp_lexer_consume_token (parser
->lexer
);
35992 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
35995 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE_TYPE
);
35996 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
35998 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = kind
;
35999 OMP_CLAUSE_CHAIN (c
) = list
;
36003 cp_parser_error (parser
, "invalid depend kind");
36005 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36006 /*or_comma=*/false,
36007 /*consume_paren=*/true);
36012 async [( int-expr )] */
36015 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
36018 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36020 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
36022 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
36024 matching_parens parens
;
36025 parens
.consume_open (parser
);
36027 t
= cp_parser_expression (parser
);
36028 if (t
== error_mark_node
36029 || !parens
.require_close (parser
))
36030 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36031 /*or_comma=*/false,
36032 /*consume_paren=*/true);
36035 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
36037 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
36038 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
36039 OMP_CLAUSE_CHAIN (c
) = list
;
36045 /* Parse all OpenACC clauses. The set clauses allowed by the directive
36046 is a bitmask in MASK. Return the list of clauses found. */
36049 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
36050 const char *where
, cp_token
*pragma_tok
,
36051 bool finish_p
= true)
36053 tree clauses
= NULL
;
36056 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36059 pragma_omp_clause c_kind
;
36060 omp_clause_code code
;
36061 const char *c_name
;
36062 tree prev
= clauses
;
36064 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36065 cp_lexer_consume_token (parser
->lexer
);
36067 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
36068 c_kind
= cp_parser_omp_clause_name (parser
);
36072 case PRAGMA_OACC_CLAUSE_ASYNC
:
36073 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
36076 case PRAGMA_OACC_CLAUSE_AUTO
:
36077 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
36081 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
36082 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
36083 c_name
= "collapse";
36085 case PRAGMA_OACC_CLAUSE_COPY
:
36086 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36089 case PRAGMA_OACC_CLAUSE_COPYIN
:
36090 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36093 case PRAGMA_OACC_CLAUSE_COPYOUT
:
36094 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36095 c_name
= "copyout";
36097 case PRAGMA_OACC_CLAUSE_CREATE
:
36098 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36101 case PRAGMA_OACC_CLAUSE_DELETE
:
36102 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36105 case PRAGMA_OMP_CLAUSE_DEFAULT
:
36106 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
36107 c_name
= "default";
36109 case PRAGMA_OACC_CLAUSE_DEVICE
:
36110 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36113 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
36114 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
36115 c_name
= "deviceptr";
36117 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
36118 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36119 c_name
= "device_resident";
36121 case PRAGMA_OACC_CLAUSE_FINALIZE
:
36122 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
36124 c_name
= "finalize";
36126 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
36127 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
36129 c_name
= "firstprivate";
36131 case PRAGMA_OACC_CLAUSE_GANG
:
36133 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
36136 case PRAGMA_OACC_CLAUSE_HOST
:
36137 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36140 case PRAGMA_OACC_CLAUSE_IF
:
36141 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
36144 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
36145 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
36147 c_name
= "if_present";
36149 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
36150 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
36152 c_name
= "independent";
36154 case PRAGMA_OACC_CLAUSE_LINK
:
36155 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36158 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
36159 code
= OMP_CLAUSE_NUM_GANGS
;
36160 c_name
= "num_gangs";
36161 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
36164 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
36165 c_name
= "num_workers";
36166 code
= OMP_CLAUSE_NUM_WORKERS
;
36167 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
36170 case PRAGMA_OACC_CLAUSE_PRESENT
:
36171 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
36172 c_name
= "present";
36174 case PRAGMA_OACC_CLAUSE_PRIVATE
:
36175 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
36177 c_name
= "private";
36179 case PRAGMA_OACC_CLAUSE_REDUCTION
:
36181 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
36183 c_name
= "reduction";
36185 case PRAGMA_OACC_CLAUSE_SEQ
:
36186 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
36190 case PRAGMA_OACC_CLAUSE_TILE
:
36191 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
36194 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
36195 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
36197 c_name
= "use_device";
36199 case PRAGMA_OACC_CLAUSE_VECTOR
:
36201 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
36205 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
36206 c_name
= "vector_length";
36207 code
= OMP_CLAUSE_VECTOR_LENGTH
;
36208 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
36211 case PRAGMA_OACC_CLAUSE_WAIT
:
36212 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
36215 case PRAGMA_OACC_CLAUSE_WORKER
:
36217 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
36222 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
36228 if (((mask
>> c_kind
) & 1) == 0)
36230 /* Remove the invalid clause(s) from the list to avoid
36231 confusing the rest of the compiler. */
36233 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
36238 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36241 return finish_omp_clauses (clauses
, C_ORT_ACC
);
36246 /* Parse all OpenMP clauses. The set clauses allowed by the directive
36247 is a bitmask in MASK. Return the list of clauses found.
36248 FINISH_P set if finish_omp_clauses should be called.
36249 NESTED non-zero if clauses should be terminated by closing paren instead
36250 of end of pragma. If it is 2, additionally commas are required in between
36254 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
36255 const char *where
, cp_token
*pragma_tok
,
36256 bool finish_p
= true, int nested
= 0)
36258 tree clauses
= NULL
;
36260 cp_token
*token
= NULL
;
36262 /* Don't create location wrapper nodes within OpenMP clauses. */
36263 auto_suppress_location_wrappers sentinel
;
36265 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36267 pragma_omp_clause c_kind
;
36268 const char *c_name
;
36269 tree prev
= clauses
;
36271 if (nested
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
36276 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36277 cp_lexer_consume_token (parser
->lexer
);
36278 else if (nested
== 2)
36279 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
36280 "clauses in %<simd%> trait should be separated "
36284 token
= cp_lexer_peek_token (parser
->lexer
);
36285 c_kind
= cp_parser_omp_clause_name (parser
);
36289 case PRAGMA_OMP_CLAUSE_BIND
:
36290 clauses
= cp_parser_omp_clause_bind (parser
, clauses
,
36294 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
36295 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
36297 c_name
= "collapse";
36299 case PRAGMA_OMP_CLAUSE_COPYIN
:
36300 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
36303 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
36304 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
36306 c_name
= "copyprivate";
36308 case PRAGMA_OMP_CLAUSE_DEFAULT
:
36309 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
36310 token
->location
, false);
36311 c_name
= "default";
36313 case PRAGMA_OMP_CLAUSE_FINAL
:
36314 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
36317 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
36318 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
36320 c_name
= "firstprivate";
36322 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
36323 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
36325 c_name
= "grainsize";
36327 case PRAGMA_OMP_CLAUSE_HINT
:
36328 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
36332 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
36333 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
36335 c_name
= "defaultmap";
36337 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
36338 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
36340 c_name
= "use_device_ptr";
36342 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
:
36343 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_ADDR
,
36345 c_name
= "use_device_addr";
36347 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
36348 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
36350 c_name
= "is_device_ptr";
36352 case PRAGMA_OMP_CLAUSE_IF
:
36353 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
36357 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
36359 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
36361 c_name
= "in_reduction";
36363 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
36364 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
36365 c_name
= "lastprivate";
36367 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
36368 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
36370 c_name
= "mergeable";
36372 case PRAGMA_OMP_CLAUSE_NOWAIT
:
36373 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
,
36377 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
36378 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
36380 c_name
= "num_tasks";
36382 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
36383 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
36385 c_name
= "num_threads";
36387 case PRAGMA_OMP_CLAUSE_ORDER
:
36388 clauses
= cp_parser_omp_clause_order (parser
, clauses
,
36392 case PRAGMA_OMP_CLAUSE_ORDERED
:
36393 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
36395 c_name
= "ordered";
36397 case PRAGMA_OMP_CLAUSE_PRIORITY
:
36398 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
36400 c_name
= "priority";
36402 case PRAGMA_OMP_CLAUSE_PRIVATE
:
36403 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
36405 c_name
= "private";
36407 case PRAGMA_OMP_CLAUSE_REDUCTION
:
36409 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
36411 c_name
= "reduction";
36413 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
36414 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
36416 c_name
= "schedule";
36418 case PRAGMA_OMP_CLAUSE_SHARED
:
36419 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
36423 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
36425 = cp_parser_omp_clause_reduction (parser
,
36426 OMP_CLAUSE_TASK_REDUCTION
,
36428 c_name
= "task_reduction";
36430 case PRAGMA_OMP_CLAUSE_UNTIED
:
36431 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
36435 case PRAGMA_OMP_CLAUSE_INBRANCH
:
36436 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
36437 clauses
, token
->location
);
36438 c_name
= "inbranch";
36440 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
36441 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
36443 c_name
= "nontemporal";
36445 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
36446 clauses
= cp_parser_omp_clause_branch (parser
,
36447 OMP_CLAUSE_NOTINBRANCH
,
36448 clauses
, token
->location
);
36449 c_name
= "notinbranch";
36451 case PRAGMA_OMP_CLAUSE_PARALLEL
:
36452 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
36453 clauses
, token
->location
);
36454 c_name
= "parallel";
36458 error_at (token
->location
, "%qs must be the first clause of %qs",
36463 case PRAGMA_OMP_CLAUSE_FOR
:
36464 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
36465 clauses
, token
->location
);
36468 goto clause_not_first
;
36470 case PRAGMA_OMP_CLAUSE_SECTIONS
:
36471 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
36472 clauses
, token
->location
);
36473 c_name
= "sections";
36475 goto clause_not_first
;
36477 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
36478 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
36479 clauses
, token
->location
);
36480 c_name
= "taskgroup";
36482 goto clause_not_first
;
36484 case PRAGMA_OMP_CLAUSE_LINK
:
36485 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
36488 case PRAGMA_OMP_CLAUSE_TO
:
36489 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
36490 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
36493 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
36496 case PRAGMA_OMP_CLAUSE_FROM
:
36497 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
36500 case PRAGMA_OMP_CLAUSE_UNIFORM
:
36501 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
36503 c_name
= "uniform";
36505 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
36506 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
36508 c_name
= "num_teams";
36510 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
36511 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
36513 c_name
= "thread_limit";
36515 case PRAGMA_OMP_CLAUSE_ALIGNED
:
36516 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
36517 c_name
= "aligned";
36519 case PRAGMA_OMP_CLAUSE_LINEAR
:
36521 bool declare_simd
= false;
36522 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
36523 declare_simd
= true;
36524 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
36528 case PRAGMA_OMP_CLAUSE_DEPEND
:
36529 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
36533 case PRAGMA_OMP_CLAUSE_MAP
:
36534 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
36537 case PRAGMA_OMP_CLAUSE_DEVICE
:
36538 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
36542 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
36543 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
36545 c_name
= "dist_schedule";
36547 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
36548 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
36550 c_name
= "proc_bind";
36552 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE
:
36553 clauses
= cp_parser_omp_clause_device_type (parser
, clauses
,
36555 c_name
= "device_type";
36557 case PRAGMA_OMP_CLAUSE_SAFELEN
:
36558 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
36560 c_name
= "safelen";
36562 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
36563 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
36565 c_name
= "simdlen";
36567 case PRAGMA_OMP_CLAUSE_NOGROUP
:
36568 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
36570 c_name
= "nogroup";
36572 case PRAGMA_OMP_CLAUSE_THREADS
:
36574 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
36575 clauses
, token
->location
);
36576 c_name
= "threads";
36578 case PRAGMA_OMP_CLAUSE_SIMD
:
36580 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
36581 clauses
, token
->location
);
36585 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
36591 if (((mask
>> c_kind
) & 1) == 0)
36593 /* Remove the invalid clause(s) from the list to avoid
36594 confusing the rest of the compiler. */
36596 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
36601 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
36604 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
36605 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
36607 return finish_omp_clauses (clauses
, C_ORT_OMP
);
36616 In practice, we're also interested in adding the statement to an
36617 outer node. So it is convenient if we work around the fact that
36618 cp_parser_statement calls add_stmt. */
36621 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
36623 unsigned save
= parser
->in_statement
;
36625 /* Only move the values to IN_OMP_BLOCK if they weren't false.
36626 This preserves the "not within loop or switch" style error messages
36627 for nonsense cases like
36633 if (parser
->in_statement
)
36634 parser
->in_statement
= IN_OMP_BLOCK
;
36640 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
36642 parser
->in_statement
= save
;
36646 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
36648 tree stmt
= begin_omp_structured_block ();
36649 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
36651 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
36653 cp_parser_end_omp_structured_block (parser
, save
);
36654 return finish_omp_structured_block (stmt
);
36658 # pragma omp atomic new-line
36662 x binop= expr | x++ | ++x | x-- | --x
36664 +, *, -, /, &, ^, |, <<, >>
36666 where x is an lvalue expression with scalar type.
36669 # pragma omp atomic new-line
36672 # pragma omp atomic read new-line
36675 # pragma omp atomic write new-line
36678 # pragma omp atomic update new-line
36681 # pragma omp atomic capture new-line
36684 # pragma omp atomic capture new-line
36692 expression-stmt | x = x binop expr
36694 v = expression-stmt
36696 { v = x; update-stmt; } | { update-stmt; v = x; }
36700 expression-stmt | x = x binop expr | x = expr binop x
36704 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
36706 where x and v are lvalue expressions with scalar type. */
36709 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
)
36711 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
36712 tree rhs1
= NULL_TREE
, orig_lhs
;
36713 location_t loc
= pragma_tok
->location
;
36714 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
36715 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
36716 bool structured_block
= false;
36718 tree clauses
= NULL_TREE
;
36720 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
36722 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36723 cp_lexer_consume_token (parser
->lexer
);
36727 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36729 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36730 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
36731 const char *p
= IDENTIFIER_POINTER (id
);
36732 enum tree_code new_code
= ERROR_MARK
;
36733 enum omp_memory_order new_memory_order
36734 = OMP_MEMORY_ORDER_UNSPECIFIED
;
36736 if (!strcmp (p
, "read"))
36737 new_code
= OMP_ATOMIC_READ
;
36738 else if (!strcmp (p
, "write"))
36739 new_code
= NOP_EXPR
;
36740 else if (!strcmp (p
, "update"))
36741 new_code
= OMP_ATOMIC
;
36742 else if (!strcmp (p
, "capture"))
36743 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
36744 else if (!strcmp (p
, "seq_cst"))
36745 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
36746 else if (!strcmp (p
, "acq_rel"))
36747 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
36748 else if (!strcmp (p
, "release"))
36749 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
36750 else if (!strcmp (p
, "acquire"))
36751 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
36752 else if (!strcmp (p
, "relaxed"))
36753 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
36754 else if (!strcmp (p
, "hint"))
36756 cp_lexer_consume_token (parser
->lexer
);
36757 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
36763 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
36764 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
36765 "%<release%>, %<relaxed%> or %<hint%> clause");
36769 if (new_code
!= ERROR_MARK
)
36771 if (code
!= ERROR_MARK
)
36772 error_at (cloc
, "too many atomic clauses");
36776 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
36778 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
36779 error_at (cloc
, "too many memory order clauses");
36781 memory_order
= new_memory_order
;
36783 cp_lexer_consume_token (parser
->lexer
);
36789 cp_parser_require_pragma_eol (parser
, pragma_tok
);
36791 if (code
== ERROR_MARK
)
36793 if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
36796 = (enum omp_requires
) (omp_requires_mask
36797 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
36798 switch ((enum omp_memory_order
)
36799 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
36801 case OMP_MEMORY_ORDER_UNSPECIFIED
:
36802 case OMP_MEMORY_ORDER_RELAXED
:
36803 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
36805 case OMP_MEMORY_ORDER_SEQ_CST
:
36806 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
36808 case OMP_MEMORY_ORDER_ACQ_REL
:
36811 case OMP_ATOMIC_READ
:
36812 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
36814 case NOP_EXPR
: /* atomic write */
36816 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
36819 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
36824 gcc_unreachable ();
36830 case OMP_ATOMIC_READ
:
36831 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
36832 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
36834 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
36835 "%<acq_rel%> or %<release%> clauses");
36836 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
36839 case NOP_EXPR
: /* atomic write */
36840 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
36841 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
36843 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
36844 "%<acq_rel%> or %<acquire%> clauses");
36845 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
36849 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
36850 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
36852 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
36853 "%<acq_rel%> or %<acquire%> clauses");
36854 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
36863 case OMP_ATOMIC_READ
:
36864 case NOP_EXPR
: /* atomic write */
36865 v
= cp_parser_unary_expression (parser
);
36866 if (v
== error_mark_node
)
36868 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36870 if (code
== NOP_EXPR
)
36871 lhs
= cp_parser_expression (parser
);
36873 lhs
= cp_parser_unary_expression (parser
);
36874 if (lhs
== error_mark_node
)
36876 if (code
== NOP_EXPR
)
36878 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
36886 case OMP_ATOMIC_CAPTURE_NEW
:
36887 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
36889 cp_lexer_consume_token (parser
->lexer
);
36890 structured_block
= true;
36894 v
= cp_parser_unary_expression (parser
);
36895 if (v
== error_mark_node
)
36897 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
36905 lhs
= cp_parser_unary_expression (parser
);
36907 switch (TREE_CODE (lhs
))
36912 case POSTINCREMENT_EXPR
:
36913 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
36914 code
= OMP_ATOMIC_CAPTURE_OLD
;
36916 case PREINCREMENT_EXPR
:
36917 lhs
= TREE_OPERAND (lhs
, 0);
36918 opcode
= PLUS_EXPR
;
36919 rhs
= integer_one_node
;
36922 case POSTDECREMENT_EXPR
:
36923 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
36924 code
= OMP_ATOMIC_CAPTURE_OLD
;
36926 case PREDECREMENT_EXPR
:
36927 lhs
= TREE_OPERAND (lhs
, 0);
36928 opcode
= MINUS_EXPR
;
36929 rhs
= integer_one_node
;
36932 case COMPOUND_EXPR
:
36933 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
36934 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
36935 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
36936 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
36937 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
36938 (TREE_OPERAND (lhs
, 1), 0), 0)))
36940 /* Undo effects of boolean_increment for post {in,de}crement. */
36941 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
36944 if (TREE_CODE (lhs
) == MODIFY_EXPR
36945 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
36947 /* Undo effects of boolean_increment. */
36948 if (integer_onep (TREE_OPERAND (lhs
, 1)))
36950 /* This is pre or post increment. */
36951 rhs
= TREE_OPERAND (lhs
, 1);
36952 lhs
= TREE_OPERAND (lhs
, 0);
36954 if (code
== OMP_ATOMIC_CAPTURE_NEW
36955 && !structured_block
36956 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
36957 code
= OMP_ATOMIC_CAPTURE_OLD
;
36963 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
36966 opcode
= MULT_EXPR
;
36969 opcode
= TRUNC_DIV_EXPR
;
36972 opcode
= PLUS_EXPR
;
36975 opcode
= MINUS_EXPR
;
36977 case CPP_LSHIFT_EQ
:
36978 opcode
= LSHIFT_EXPR
;
36980 case CPP_RSHIFT_EQ
:
36981 opcode
= RSHIFT_EXPR
;
36984 opcode
= BIT_AND_EXPR
;
36987 opcode
= BIT_IOR_EXPR
;
36990 opcode
= BIT_XOR_EXPR
;
36993 enum cp_parser_prec oprec
;
36995 cp_lexer_consume_token (parser
->lexer
);
36996 cp_parser_parse_tentatively (parser
);
36997 rhs1
= cp_parser_simple_cast_expression (parser
);
36998 if (rhs1
== error_mark_node
)
37000 cp_parser_abort_tentative_parse (parser
);
37001 cp_parser_simple_cast_expression (parser
);
37004 token
= cp_lexer_peek_token (parser
->lexer
);
37005 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
37007 cp_parser_abort_tentative_parse (parser
);
37008 cp_parser_parse_tentatively (parser
);
37009 rhs
= cp_parser_binary_expression (parser
, false, true,
37010 PREC_NOT_OPERATOR
, NULL
);
37011 if (rhs
== error_mark_node
)
37013 cp_parser_abort_tentative_parse (parser
);
37014 cp_parser_binary_expression (parser
, false, true,
37015 PREC_NOT_OPERATOR
, NULL
);
37018 switch (TREE_CODE (rhs
))
37021 case TRUNC_DIV_EXPR
:
37030 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
37032 if (cp_parser_parse_definitely (parser
))
37034 opcode
= TREE_CODE (rhs
);
37035 rhs1
= TREE_OPERAND (rhs
, 0);
37036 rhs
= TREE_OPERAND (rhs
, 1);
37046 cp_parser_abort_tentative_parse (parser
);
37047 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
37049 rhs
= cp_parser_expression (parser
);
37050 if (rhs
== error_mark_node
)
37056 cp_parser_error (parser
,
37057 "invalid form of %<#pragma omp atomic%>");
37060 if (!cp_parser_parse_definitely (parser
))
37062 switch (token
->type
)
37064 case CPP_SEMICOLON
:
37065 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
37067 code
= OMP_ATOMIC_CAPTURE_OLD
;
37072 cp_lexer_consume_token (parser
->lexer
);
37075 else if (structured_block
)
37082 cp_parser_error (parser
,
37083 "invalid form of %<#pragma omp atomic%>");
37086 opcode
= MULT_EXPR
;
37089 opcode
= TRUNC_DIV_EXPR
;
37092 opcode
= PLUS_EXPR
;
37095 opcode
= MINUS_EXPR
;
37098 opcode
= LSHIFT_EXPR
;
37101 opcode
= RSHIFT_EXPR
;
37104 opcode
= BIT_AND_EXPR
;
37107 opcode
= BIT_IOR_EXPR
;
37110 opcode
= BIT_XOR_EXPR
;
37113 cp_parser_error (parser
,
37114 "invalid operator for %<#pragma omp atomic%>");
37117 oprec
= TOKEN_PRECEDENCE (token
);
37118 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
37119 if (commutative_tree_code (opcode
))
37120 oprec
= (enum cp_parser_prec
) (oprec
- 1);
37121 cp_lexer_consume_token (parser
->lexer
);
37122 rhs
= cp_parser_binary_expression (parser
, false, false,
37124 if (rhs
== error_mark_node
)
37129 cp_parser_error (parser
,
37130 "invalid operator for %<#pragma omp atomic%>");
37133 cp_lexer_consume_token (parser
->lexer
);
37135 rhs
= cp_parser_expression (parser
);
37136 if (rhs
== error_mark_node
)
37141 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
37143 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
37145 v
= cp_parser_unary_expression (parser
);
37146 if (v
== error_mark_node
)
37148 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
37150 lhs1
= cp_parser_unary_expression (parser
);
37151 if (lhs1
== error_mark_node
)
37154 if (structured_block
)
37156 cp_parser_consume_semicolon_at_end_of_statement (parser
);
37157 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
37160 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37161 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
37162 rhs1
, clauses
, memory_order
);
37163 if (!structured_block
)
37164 cp_parser_consume_semicolon_at_end_of_statement (parser
);
37168 cp_parser_skip_to_end_of_block_or_statement (parser
);
37169 if (structured_block
)
37171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37172 cp_lexer_consume_token (parser
->lexer
);
37173 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
37175 cp_parser_skip_to_end_of_block_or_statement (parser
);
37176 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
37177 cp_lexer_consume_token (parser
->lexer
);
37184 # pragma omp barrier new-line */
37187 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
37189 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37190 finish_omp_barrier ();
37194 # pragma omp critical [(name)] new-line
37198 # pragma omp critical [(name) [hint(expression)]] new-line
37199 structured-block */
37201 #define OMP_CRITICAL_CLAUSE_MASK \
37202 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
37205 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
37207 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
37209 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37211 matching_parens parens
;
37212 parens
.consume_open (parser
);
37214 name
= cp_parser_identifier (parser
);
37216 if (name
== error_mark_node
37217 || !parens
.require_close (parser
))
37218 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37219 /*or_comma=*/false,
37220 /*consume_paren=*/true);
37221 if (name
== error_mark_node
)
37224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
37225 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
37226 cp_lexer_consume_token (parser
->lexer
);
37228 clauses
= cp_parser_omp_all_clauses (parser
,
37229 OMP_CRITICAL_CLAUSE_MASK
,
37230 "#pragma omp critical", pragma_tok
);
37233 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37235 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
37236 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
37240 # pragma omp depobj ( depobj ) depobj-clause new-line
37243 depend (dependence-type : locator)
37245 update (dependence-type)
37254 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
37256 location_t loc
= pragma_tok
->location
;
37257 matching_parens parens
;
37258 if (!parens
.require_open (parser
))
37260 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
37264 tree depobj
= cp_parser_assignment_expression (parser
);
37266 if (!parens
.require_close (parser
))
37267 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37268 /*or_comma=*/false,
37269 /*consume_paren=*/true);
37271 tree clause
= NULL_TREE
;
37272 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
37273 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37274 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37276 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37277 const char *p
= IDENTIFIER_POINTER (id
);
37279 cp_lexer_consume_token (parser
->lexer
);
37280 if (!strcmp ("depend", p
))
37282 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
37284 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
37286 clause
= error_mark_node
;
37288 else if (!strcmp ("destroy", p
))
37289 kind
= OMP_CLAUSE_DEPEND_LAST
;
37290 else if (!strcmp ("update", p
))
37292 matching_parens c_parens
;
37293 if (c_parens
.require_open (parser
))
37296 = cp_lexer_peek_token (parser
->lexer
)->location
;
37297 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37299 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37300 const char *p2
= IDENTIFIER_POINTER (id2
);
37302 cp_lexer_consume_token (parser
->lexer
);
37303 if (!strcmp ("in", p2
))
37304 kind
= OMP_CLAUSE_DEPEND_IN
;
37305 else if (!strcmp ("out", p2
))
37306 kind
= OMP_CLAUSE_DEPEND_OUT
;
37307 else if (!strcmp ("inout", p2
))
37308 kind
= OMP_CLAUSE_DEPEND_INOUT
;
37309 else if (!strcmp ("mutexinoutset", p2
))
37310 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
37312 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
37314 clause
= error_mark_node
;
37315 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
37316 "%<mutexinoutset%>");
37318 if (!c_parens
.require_close (parser
))
37319 cp_parser_skip_to_closing_parenthesis (parser
,
37320 /*recovering=*/true,
37321 /*or_comma=*/false,
37322 /*consume_paren=*/true);
37325 clause
= error_mark_node
;
37328 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
37330 clause
= error_mark_node
;
37331 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
37333 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37335 finish_omp_depobj (loc
, depobj
, kind
, clause
);
37340 # pragma omp flush flush-vars[opt] new-line
37346 # pragma omp flush memory-order-clause new-line */
37349 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
37351 enum memmodel mo
= MEMMODEL_LAST
;
37352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37354 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37355 const char *p
= IDENTIFIER_POINTER (id
);
37356 if (!strcmp (p
, "acq_rel"))
37357 mo
= MEMMODEL_ACQ_REL
;
37358 else if (!strcmp (p
, "release"))
37359 mo
= MEMMODEL_RELEASE
;
37360 else if (!strcmp (p
, "acquire"))
37361 mo
= MEMMODEL_ACQUIRE
;
37363 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37364 "expected %<acq_rel%>, %<release%> or %<acquire%>");
37365 cp_lexer_consume_token (parser
->lexer
);
37367 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
37369 if (mo
!= MEMMODEL_LAST
)
37370 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
37371 "%<flush%> list specified together with memory order "
37373 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
37375 cp_parser_require_pragma_eol (parser
, pragma_tok
);
37377 finish_omp_flush (mo
);
37380 /* Helper function, to parse omp for increment expression. */
37383 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
37385 tree cond
= cp_parser_binary_expression (parser
, false, true,
37386 PREC_NOT_OPERATOR
, NULL
);
37387 if (cond
== error_mark_node
37388 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
37390 cp_parser_skip_to_end_of_statement (parser
);
37391 return error_mark_node
;
37394 switch (TREE_CODE (cond
))
37402 if (code
!= OACC_LOOP
)
37404 gcc_fallthrough ();
37406 return error_mark_node
;
37409 /* If decl is an iterator, preserve LHS and RHS of the relational
37410 expr until finish_omp_for. */
37412 && (type_dependent_expression_p (decl
)
37413 || CLASS_TYPE_P (TREE_TYPE (decl
))))
37416 return build_x_binary_op (cp_expr_loc_or_input_loc (cond
),
37418 TREE_OPERAND (cond
, 0), ERROR_MARK
,
37419 TREE_OPERAND (cond
, 1), ERROR_MARK
,
37420 /*overload=*/NULL
, tf_warning_or_error
);
37423 /* Helper function, to parse omp for increment expression. */
37426 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
37428 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
37434 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
37436 op
= (token
->type
== CPP_PLUS_PLUS
37437 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
37438 cp_lexer_consume_token (parser
->lexer
);
37439 lhs
= cp_parser_simple_cast_expression (parser
);
37441 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
37442 return error_mark_node
;
37443 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
37446 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
37448 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
37449 return error_mark_node
;
37451 token
= cp_lexer_peek_token (parser
->lexer
);
37452 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
37454 op
= (token
->type
== CPP_PLUS_PLUS
37455 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
37456 cp_lexer_consume_token (parser
->lexer
);
37457 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
37460 op
= cp_parser_assignment_operator_opt (parser
);
37461 if (op
== ERROR_MARK
)
37462 return error_mark_node
;
37464 if (op
!= NOP_EXPR
)
37466 rhs
= cp_parser_assignment_expression (parser
);
37467 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
37468 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
37471 lhs
= cp_parser_binary_expression (parser
, false, false,
37472 PREC_ADDITIVE_EXPRESSION
, NULL
);
37473 token
= cp_lexer_peek_token (parser
->lexer
);
37474 decl_first
= (lhs
== decl
37475 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
37478 if (token
->type
!= CPP_PLUS
37479 && token
->type
!= CPP_MINUS
)
37480 return error_mark_node
;
37484 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
37485 cp_lexer_consume_token (parser
->lexer
);
37486 rhs
= cp_parser_binary_expression (parser
, false, false,
37487 PREC_ADDITIVE_EXPRESSION
, NULL
);
37488 token
= cp_lexer_peek_token (parser
->lexer
);
37489 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
37491 if (lhs
== NULL_TREE
)
37493 if (op
== PLUS_EXPR
)
37496 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
37497 tf_warning_or_error
);
37500 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
37501 ERROR_MARK
, NULL
, tf_warning_or_error
);
37504 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
37509 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
37510 || op
== MINUS_EXPR
)
37511 return error_mark_node
;
37512 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
37515 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
37517 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
37520 /* Parse the initialization statement of an OpenMP for loop.
37522 Return true if the resulting construct should have an
37523 OMP_CLAUSE_PRIVATE added to it. */
37526 cp_parser_omp_for_loop_init (cp_parser
*parser
,
37527 tree
&this_pre_body
,
37528 releasing_vec
&for_block
,
37534 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
37537 tree add_private_clause
= NULL_TREE
;
37539 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
37543 integer-type var = lb
37544 random-access-iterator-type var = lb
37545 pointer-type var = lb
37547 cp_decl_specifier_seq type_specifiers
;
37549 /* First, try to parse as an initialized declaration. See
37550 cp_parser_condition, from whence the bulk of this is copied. */
37552 cp_parser_parse_tentatively (parser
);
37553 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
37554 /*is_declaration=*/true,
37555 /*is_trailing_return=*/false,
37557 if (cp_parser_parse_definitely (parser
))
37559 /* If parsing a type specifier seq succeeded, then this
37560 MUST be a initialized declaration. */
37561 tree asm_specification
, attributes
;
37562 cp_declarator
*declarator
;
37564 declarator
= cp_parser_declarator (parser
,
37565 CP_PARSER_DECLARATOR_NAMED
,
37566 CP_PARSER_FLAGS_NONE
,
37567 /*ctor_dtor_or_conv_p=*/NULL
,
37568 /*parenthesized_p=*/NULL
,
37569 /*member_p=*/false,
37570 /*friend_p=*/false,
37571 /*static_p=*/false);
37572 attributes
= cp_parser_attributes_opt (parser
);
37573 asm_specification
= cp_parser_asm_specification_opt (parser
);
37575 if (declarator
== cp_error_declarator
)
37576 cp_parser_skip_to_end_of_statement (parser
);
37580 tree pushed_scope
, auto_node
;
37582 decl
= start_decl (declarator
, &type_specifiers
,
37583 SD_INITIALIZED
, attributes
,
37584 /*prefix_attributes=*/NULL_TREE
,
37587 auto_node
= type_uses_auto (TREE_TYPE (decl
));
37588 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
37590 if (cp_lexer_next_token_is (parser
->lexer
,
37592 error ("parenthesized initialization is not allowed in "
37593 "OpenMP %<for%> loop");
37595 /* Trigger an error. */
37596 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
37598 init
= error_mark_node
;
37599 cp_parser_skip_to_end_of_statement (parser
);
37601 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
37602 || type_dependent_expression_p (decl
)
37605 bool is_direct_init
, is_non_constant_init
;
37607 init
= cp_parser_initializer (parser
,
37609 &is_non_constant_init
);
37614 = do_auto_deduction (TREE_TYPE (decl
), init
,
37617 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
37618 && !type_dependent_expression_p (decl
))
37622 cp_finish_decl (decl
, init
, !is_non_constant_init
,
37624 LOOKUP_ONLYCONVERTING
);
37626 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
37628 vec_safe_push (for_block
, this_pre_body
);
37633 init
= pop_stmt_list (this_pre_body
);
37634 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
37636 tree_stmt_iterator i
= tsi_start (init
);
37637 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
37638 while (!tsi_end_p (i
))
37640 tree t
= tsi_stmt (i
);
37641 if (TREE_CODE (t
) == DECL_EXPR
37642 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
37645 vec_safe_push (for_block
, t
);
37650 if (tsi_one_before_end_p (i
))
37652 tree t
= tsi_stmt (i
);
37654 free_stmt_list (init
);
37659 this_pre_body
= NULL_TREE
;
37664 cp_lexer_consume_token (parser
->lexer
);
37665 init
= cp_parser_assignment_expression (parser
);
37668 if (TYPE_REF_P (TREE_TYPE (decl
)))
37669 init
= error_mark_node
;
37671 cp_finish_decl (decl
, NULL_TREE
,
37672 /*init_const_expr_p=*/false,
37674 LOOKUP_ONLYCONVERTING
);
37678 pop_scope (pushed_scope
);
37684 /* If parsing a type specifier sequence failed, then
37685 this MUST be a simple expression. */
37686 cp_parser_parse_tentatively (parser
);
37687 decl
= cp_parser_primary_expression (parser
, false, false,
37689 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
37690 if (!cp_parser_error_occurred (parser
)
37692 && (TREE_CODE (decl
) == COMPONENT_REF
37693 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
37695 cp_parser_abort_tentative_parse (parser
);
37696 cp_parser_parse_tentatively (parser
);
37697 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
37698 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37699 /*check_dependency_p=*/true,
37700 /*template_p=*/NULL
,
37701 /*declarator_p=*/false,
37702 /*optional_p=*/false);
37703 if (name
!= error_mark_node
37704 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
37706 decl
= cp_parser_lookup_name_simple (parser
, name
,
37708 if (TREE_CODE (decl
) == FIELD_DECL
)
37709 add_private_clause
= omp_privatize_field (decl
, false);
37711 cp_parser_abort_tentative_parse (parser
);
37712 cp_parser_parse_tentatively (parser
);
37713 decl
= cp_parser_primary_expression (parser
, false, false,
37716 if (!cp_parser_error_occurred (parser
)
37719 && CLASS_TYPE_P (TREE_TYPE (decl
)))
37723 cp_parser_parse_definitely (parser
);
37724 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
37725 rhs
= cp_parser_assignment_expression (parser
);
37727 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
37730 tf_warning_or_error
));
37731 if (!add_private_clause
)
37732 add_private_clause
= decl
;
37737 cp_parser_abort_tentative_parse (parser
);
37738 init
= cp_parser_expression (parser
);
37741 if (TREE_CODE (init
) == MODIFY_EXPR
37742 || TREE_CODE (init
) == MODOP_EXPR
)
37743 real_decl
= TREE_OPERAND (init
, 0);
37747 return add_private_clause
;
37750 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
37753 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
37754 tree
&decl
, tree
&orig_decl
, tree
&init
,
37755 tree
&orig_init
, tree
&cond
, tree
&incr
)
37757 tree begin
, end
, range_temp_decl
= NULL_TREE
;
37758 tree iter_type
, begin_expr
, end_expr
;
37760 if (processing_template_decl
)
37762 if (check_for_bare_parameter_packs (init
))
37763 init
= error_mark_node
;
37764 if (!type_dependent_expression_p (init
)
37765 /* do_auto_deduction doesn't mess with template init-lists. */
37766 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
37769 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
37771 tree v
= DECL_VALUE_EXPR (decl
);
37772 if (TREE_CODE (v
) == ARRAY_REF
37773 && VAR_P (TREE_OPERAND (v
, 0))
37774 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
37775 d
= TREE_OPERAND (v
, 0);
37777 do_range_for_auto_deduction (d
, init
);
37779 cond
= global_namespace
;
37783 this_pre_body
= pop_stmt_list (this_pre_body
);
37787 init
= mark_lvalue_use (init
);
37789 if (decl
== error_mark_node
|| init
== error_mark_node
)
37790 /* If an error happened previously do nothing or else a lot of
37791 unhelpful errors would be issued. */
37792 begin_expr
= end_expr
= iter_type
= error_mark_node
;
37798 && array_of_runtime_bound_p (TREE_TYPE (init
)))
37799 /* Can't bind a reference to an array of runtime bound. */
37803 range_temp
= build_range_temp (init
);
37804 DECL_NAME (range_temp
) = NULL_TREE
;
37805 pushdecl (range_temp
);
37806 cp_finish_decl (range_temp
, init
,
37807 /*is_constant_init*/false, NULL_TREE
,
37808 LOOKUP_ONLYCONVERTING
);
37809 range_temp_decl
= range_temp
;
37810 range_temp
= convert_from_reference (range_temp
);
37812 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
37813 &begin_expr
, &end_expr
);
37816 tree end_iter_type
= iter_type
;
37817 if (cxx_dialect
>= cxx17
)
37818 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
37819 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
37820 TREE_USED (end
) = 1;
37821 DECL_ARTIFICIAL (end
) = 1;
37823 cp_finish_decl (end
, end_expr
,
37824 /*is_constant_init*/false, NULL_TREE
,
37825 LOOKUP_ONLYCONVERTING
);
37827 /* The new for initialization statement. */
37828 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
37829 TREE_USED (begin
) = 1;
37830 DECL_ARTIFICIAL (begin
) = 1;
37833 if (CLASS_TYPE_P (iter_type
))
37838 begin_expr
= NULL_TREE
;
37840 cp_finish_decl (begin
, begin_expr
,
37841 /*is_constant_init*/false, NULL_TREE
,
37842 LOOKUP_ONLYCONVERTING
);
37844 /* The new for condition. */
37845 if (CLASS_TYPE_P (iter_type
))
37846 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
37848 cond
= build_x_binary_op (input_location
, NE_EXPR
,
37851 NULL
, tf_warning_or_error
);
37853 /* The new increment expression. */
37854 if (CLASS_TYPE_P (iter_type
))
37855 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
37857 incr
= finish_unary_op_expr (input_location
,
37858 PREINCREMENT_EXPR
, begin
,
37859 tf_warning_or_error
);
37865 vec_safe_push (for_block
, this_pre_body
);
37866 this_pre_body
= NULL_TREE
;
37869 tree decomp_first_name
= NULL_TREE
;
37870 unsigned decomp_cnt
= 0;
37871 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
37873 tree v
= DECL_VALUE_EXPR (orig_decl
);
37874 if (TREE_CODE (v
) == ARRAY_REF
37875 && VAR_P (TREE_OPERAND (v
, 0))
37876 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
37878 tree d
= orig_decl
;
37879 orig_decl
= TREE_OPERAND (v
, 0);
37880 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
37881 decomp_first_name
= d
;
37885 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
37888 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
37890 if (!error_operand_p (t
))
37891 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
37895 tree v
= make_tree_vec (decomp_cnt
+ 3);
37896 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
37897 TREE_VEC_ELT (v
, 1) = end
;
37898 TREE_VEC_ELT (v
, 2) = orig_decl
;
37899 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
37901 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
37902 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
37904 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
37907 /* Helper for cp_parser_omp_for_loop, finalize part of range for
37908 inside of the collapsed body. */
37911 cp_finish_omp_range_for (tree orig
, tree begin
)
37913 gcc_assert (TREE_CODE (orig
) == TREE_LIST
37914 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
37915 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
37916 tree decomp_first_name
= NULL_TREE
;
37917 unsigned int decomp_cnt
= 0;
37919 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
37921 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
37922 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
37923 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
37926 /* The declaration is initialized with *__begin inside the loop body. */
37927 cp_finish_decl (decl
,
37928 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
37929 tf_warning_or_error
),
37930 /*is_constant_init*/false, NULL_TREE
,
37931 LOOKUP_ONLYCONVERTING
);
37932 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
37933 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
37939 { structured-block scan-directive structured-block } */
37942 cp_parser_omp_scan_loop_body (cp_parser
*parser
)
37944 tree substmt
, clauses
= NULL_TREE
;
37946 matching_braces braces
;
37947 if (!braces
.require_open (parser
))
37950 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37951 substmt
= build2 (OMP_SCAN
, void_type_node
, substmt
, NULL_TREE
);
37952 add_stmt (substmt
);
37954 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
37955 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SCAN
)
37957 enum omp_clause_code clause
= OMP_CLAUSE_ERROR
;
37959 cp_lexer_consume_token (parser
->lexer
);
37961 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37963 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37964 const char *p
= IDENTIFIER_POINTER (id
);
37965 if (strcmp (p
, "inclusive") == 0)
37966 clause
= OMP_CLAUSE_INCLUSIVE
;
37967 else if (strcmp (p
, "exclusive") == 0)
37968 clause
= OMP_CLAUSE_EXCLUSIVE
;
37970 if (clause
!= OMP_CLAUSE_ERROR
)
37972 cp_lexer_consume_token (parser
->lexer
);
37973 clauses
= cp_parser_omp_var_list (parser
, clause
, NULL_TREE
);
37976 cp_parser_error (parser
, "expected %<inclusive%> or "
37977 "%<exclusive%> clause");
37979 cp_parser_require_pragma_eol (parser
, tok
);
37982 error ("expected %<#pragma omp scan%>");
37984 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
37985 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
37986 substmt
= build2_loc (tok
->location
, OMP_SCAN
, void_type_node
, substmt
,
37988 add_stmt (substmt
);
37990 braces
.require_close (parser
);
37993 /* Parse the restricted form of the for statement allowed by OpenMP. */
37996 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
37997 tree
*cclauses
, bool *if_p
)
37999 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
38001 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
38002 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
38003 location_t loc_first
;
38004 bool collapse_err
= false;
38005 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
38006 releasing_vec for_block
;
38007 auto_vec
<tree
, 4> orig_inits
;
38008 bool tiling
= false;
38009 bool inscan
= false;
38011 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
38012 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
38013 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
38014 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
38017 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
38019 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
38020 && OMP_CLAUSE_ORDERED_EXPR (cl
))
38023 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
38025 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_REDUCTION
38026 && OMP_CLAUSE_REDUCTION_INSCAN (cl
)
38027 && (code
== OMP_SIMD
|| code
== OMP_FOR
))
38030 if (ordered
&& ordered
< collapse
)
38032 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
38033 "%<ordered%> clause parameter is less than %<collapse%>");
38034 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
38035 = build_int_cst (NULL_TREE
, collapse
);
38036 ordered
= collapse
;
38040 for (tree
*pc
= &clauses
; *pc
; )
38041 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
38043 error_at (OMP_CLAUSE_LOCATION (*pc
),
38044 "%<linear%> clause may not be specified together "
38045 "with %<ordered%> clause with a parameter");
38046 *pc
= OMP_CLAUSE_CHAIN (*pc
);
38049 pc
= &OMP_CLAUSE_CHAIN (*pc
);
38052 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
38053 count
= ordered
? ordered
: collapse
;
38055 declv
= make_tree_vec (count
);
38056 initv
= make_tree_vec (count
);
38057 condv
= make_tree_vec (count
);
38058 incrv
= make_tree_vec (count
);
38059 orig_declv
= NULL_TREE
;
38061 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
38063 for (i
= 0; i
< count
; i
++)
38065 int bracecount
= 0;
38066 tree add_private_clause
= NULL_TREE
;
38069 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
38072 cp_parser_error (parser
, "for statement expected");
38075 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
38077 /* Don't create location wrapper nodes within an OpenMP "for"
38079 auto_suppress_location_wrappers sentinel
;
38081 matching_parens parens
;
38082 if (!parens
.require_open (parser
))
38085 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
38086 this_pre_body
= push_stmt_list ();
38088 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
38090 /* Save tokens so that we can put them back. */
38091 cp_lexer_save_tokens (parser
->lexer
);
38093 /* Look for ':' that is not nested in () or {}. */
38095 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
38096 /*recovering=*/false,
38101 /* Roll back the tokens we skipped. */
38102 cp_lexer_rollback_tokens (parser
->lexer
);
38106 bool saved_colon_corrects_to_scope_p
38107 = parser
->colon_corrects_to_scope_p
;
38109 /* A colon is used in range-based for. */
38110 parser
->colon_corrects_to_scope_p
= false;
38112 /* Parse the declaration. */
38113 cp_parser_simple_declaration (parser
,
38114 /*function_definition_allowed_p=*/
38116 parser
->colon_corrects_to_scope_p
38117 = saved_colon_corrects_to_scope_p
;
38119 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
38121 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
38124 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
38125 orig_decl
, init
, orig_init
,
38132 pre_body
= push_stmt_list ();
38134 add_stmt (this_pre_body
);
38135 pre_body
= pop_stmt_list (pre_body
);
38138 pre_body
= this_pre_body
;
38142 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
38143 "%<ordered%> clause with parameter on "
38144 "range-based %<for%> loop");
38146 goto parse_close_paren
;
38151 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
38152 init
, orig_init
, decl
, real_decl
);
38154 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38157 this_pre_body
= pop_stmt_list (this_pre_body
);
38161 pre_body
= push_stmt_list ();
38163 add_stmt (this_pre_body
);
38164 pre_body
= pop_stmt_list (pre_body
);
38167 pre_body
= this_pre_body
;
38172 if (cclauses
!= NULL
38173 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
38174 && real_decl
!= NULL_TREE
38175 && code
!= OMP_LOOP
)
38178 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
38179 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
38180 && OMP_CLAUSE_DECL (*c
) == real_decl
)
38182 error_at (loc
, "iteration variable %qD"
38183 " should not be firstprivate", real_decl
);
38184 *c
= OMP_CLAUSE_CHAIN (*c
);
38186 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
38187 && OMP_CLAUSE_DECL (*c
) == real_decl
)
38189 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
38191 *c
= OMP_CLAUSE_CHAIN (*c
);
38192 if (code
== OMP_SIMD
)
38194 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
38195 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
38199 OMP_CLAUSE_CHAIN (l
) = clauses
;
38202 add_private_clause
= NULL_TREE
;
38206 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
38207 && OMP_CLAUSE_DECL (*c
) == real_decl
)
38208 add_private_clause
= NULL_TREE
;
38209 c
= &OMP_CLAUSE_CHAIN (*c
);
38213 if (add_private_clause
)
38216 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
38218 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
38219 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
38220 && OMP_CLAUSE_DECL (c
) == decl
)
38222 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
38223 && OMP_CLAUSE_DECL (c
) == decl
)
38224 error_at (loc
, "iteration variable %qD "
38225 "should not be firstprivate",
38227 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
38228 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
38229 && OMP_CLAUSE_DECL (c
) == decl
)
38230 error_at (loc
, "iteration variable %qD should not be reduction",
38235 if ((code
== OMP_SIMD
&& collapse
!= 1) || code
== OMP_LOOP
)
38236 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
38237 else if (code
!= OMP_SIMD
)
38238 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
38240 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
38241 OMP_CLAUSE_DECL (c
) = add_private_clause
;
38242 c
= finish_omp_clauses (c
, C_ORT_OMP
);
38245 OMP_CLAUSE_CHAIN (c
) = clauses
;
38247 /* For linear, signal that we need to fill up
38248 the so far unknown linear step. */
38249 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
38250 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
38256 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
38257 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
38258 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
38261 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
38263 /* If decl is an iterator, preserve the operator on decl
38264 until finish_omp_for. */
38266 && ((processing_template_decl
38267 && (TREE_TYPE (real_decl
) == NULL_TREE
38268 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
38269 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
38270 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
38272 incr
= cp_parser_expression (parser
);
38273 if (!EXPR_HAS_LOCATION (incr
))
38274 protected_set_expr_location (incr
, input_location
);
38278 if (!parens
.require_close (parser
))
38279 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
38280 /*or_comma=*/false,
38281 /*consume_paren=*/true);
38283 TREE_VEC_ELT (declv
, i
) = decl
;
38284 TREE_VEC_ELT (initv
, i
) = init
;
38285 TREE_VEC_ELT (condv
, i
) = cond
;
38286 TREE_VEC_ELT (incrv
, i
) = incr
;
38289 orig_inits
.safe_grow_cleared (i
+ 1);
38290 orig_inits
[i
] = orig_init
;
38295 orig_declv
= copy_node (declv
);
38296 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
38298 else if (orig_declv
)
38299 TREE_VEC_ELT (orig_declv
, i
) = decl
;
38301 if (i
== count
- 1)
38304 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
38305 in between the collapsed for loops to be still considered perfectly
38306 nested. Hopefully the final version clarifies this.
38307 For now handle (multiple) {'s and empty statements. */
38308 cp_parser_parse_tentatively (parser
);
38311 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
38313 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
38315 cp_lexer_consume_token (parser
->lexer
);
38318 else if (bracecount
38319 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
38320 cp_lexer_consume_token (parser
->lexer
);
38323 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38324 error_at (loc
, "not enough for loops to collapse");
38325 collapse_err
= true;
38326 cp_parser_abort_tentative_parse (parser
);
38334 cp_parser_parse_definitely (parser
);
38335 nbraces
+= bracecount
;
38342 /* Note that we saved the original contents of this flag when we entered
38343 the structured block, and so we don't need to re-save it here. */
38344 parser
->in_statement
= IN_OMP_FOR
;
38346 /* Note that the grammar doesn't call for a structured block here,
38347 though the loop as a whole is a structured block. */
38350 body
= begin_omp_structured_block ();
38351 for (i
= 0; i
< count
; i
++)
38352 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
38353 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
38354 TREE_VEC_ELT (declv
, i
));
38357 body
= push_stmt_list ();
38359 cp_parser_omp_scan_loop_body (parser
);
38361 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38363 body
= finish_omp_structured_block (body
);
38365 body
= pop_stmt_list (body
);
38367 if (declv
== NULL_TREE
)
38370 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
38371 incrv
, body
, pre_body
, &orig_inits
, clauses
);
38375 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
38377 cp_lexer_consume_token (parser
->lexer
);
38380 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
38381 cp_lexer_consume_token (parser
->lexer
);
38386 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38387 "collapsed loops not perfectly nested");
38389 collapse_err
= true;
38390 cp_parser_statement_seq_opt (parser
, NULL
);
38391 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
38396 while (!for_block
->is_empty ())
38398 tree t
= for_block
->pop ();
38399 if (TREE_CODE (t
) == STATEMENT_LIST
)
38400 add_stmt (pop_stmt_list (t
));
38408 /* Helper function for OpenMP parsing, split clauses and call
38409 finish_omp_clauses on each of the set of clauses afterwards. */
38412 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
38413 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
38416 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
38417 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
38419 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
38423 #pragma omp loop loop-clause[optseq] new-line
38426 #define OMP_LOOP_CLAUSE_MASK \
38427 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38428 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
38432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38435 cp_parser_omp_loop (cp_parser
*parser
, cp_token
*pragma_tok
,
38436 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38439 tree clauses
, sb
, ret
;
38441 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38443 strcat (p_name
, " loop");
38444 mask
|= OMP_LOOP_CLAUSE_MASK
;
38446 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38450 cp_omp_split_clauses (loc
, OMP_LOOP
, mask
, clauses
, cclauses
);
38451 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_LOOP
];
38454 keep_next_level (true);
38455 sb
= begin_omp_structured_block ();
38456 save
= cp_parser_begin_omp_structured_block (parser
);
38458 ret
= cp_parser_omp_for_loop (parser
, OMP_LOOP
, clauses
, cclauses
, if_p
);
38460 cp_parser_end_omp_structured_block (parser
, save
);
38461 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
38467 #pragma omp simd simd-clause[optseq] new-line
38470 #define OMP_SIMD_CLAUSE_MASK \
38471 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
38472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
38473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
38475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
38481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38484 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
38485 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38488 tree clauses
, sb
, ret
;
38490 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38492 strcat (p_name
, " simd");
38493 mask
|= OMP_SIMD_CLAUSE_MASK
;
38495 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38499 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
38500 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
38501 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
38502 OMP_CLAUSE_ORDERED
);
38503 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
38505 error_at (OMP_CLAUSE_LOCATION (c
),
38506 "%<ordered%> clause with parameter may not be specified "
38507 "on %qs construct", p_name
);
38508 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
38512 keep_next_level (true);
38513 sb
= begin_omp_structured_block ();
38514 save
= cp_parser_begin_omp_structured_block (parser
);
38516 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
38518 cp_parser_end_omp_structured_block (parser
, save
);
38519 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
38525 #pragma omp for for-clause[optseq] new-line
38529 #pragma omp for simd for-simd-clause[optseq] new-line
38532 #define OMP_FOR_CLAUSE_MASK \
38533 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
38537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
38539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
38540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
38541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
38542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
38545 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
38546 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38549 tree clauses
, sb
, ret
;
38551 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38553 strcat (p_name
, " for");
38554 mask
|= OMP_FOR_CLAUSE_MASK
;
38555 /* parallel for{, simd} disallows nowait clause, but for
38556 target {teams distribute ,}parallel for{, simd} it should be accepted. */
38557 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
38558 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
38559 /* Composite distribute parallel for{, simd} disallows ordered clause. */
38560 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
38561 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
38563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38565 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38566 const char *p
= IDENTIFIER_POINTER (id
);
38568 if (strcmp (p
, "simd") == 0)
38570 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38571 if (cclauses
== NULL
)
38572 cclauses
= cclauses_buf
;
38574 cp_lexer_consume_token (parser
->lexer
);
38575 if (!flag_openmp
) /* flag_openmp_simd */
38576 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38578 sb
= begin_omp_structured_block ();
38579 save
= cp_parser_begin_omp_structured_block (parser
);
38580 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
38582 cp_parser_end_omp_structured_block (parser
, save
);
38583 tree body
= finish_omp_structured_block (sb
);
38586 ret
= make_node (OMP_FOR
);
38587 TREE_TYPE (ret
) = void_type_node
;
38588 OMP_FOR_BODY (ret
) = body
;
38589 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
38590 SET_EXPR_LOCATION (ret
, loc
);
38595 if (!flag_openmp
) /* flag_openmp_simd */
38597 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38601 /* Composite distribute parallel for disallows linear clause. */
38602 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
38603 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
38605 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38609 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
38610 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
38613 keep_next_level (true);
38614 sb
= begin_omp_structured_block ();
38615 save
= cp_parser_begin_omp_structured_block (parser
);
38617 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
38619 cp_parser_end_omp_structured_block (parser
, save
);
38620 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
38625 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
38626 omp_clause_mask
, tree
*, bool *);
38629 # pragma omp master new-line
38630 structured-block */
38633 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
38634 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38637 tree clauses
, sb
, ret
;
38639 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38641 strcat (p_name
, " master");
38643 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38645 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38646 const char *p
= IDENTIFIER_POINTER (id
);
38648 if (strcmp (p
, "taskloop") == 0)
38650 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38651 if (cclauses
== NULL
)
38652 cclauses
= cclauses_buf
;
38654 cp_lexer_consume_token (parser
->lexer
);
38655 if (!flag_openmp
) /* flag_openmp_simd */
38656 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
38658 sb
= begin_omp_structured_block ();
38659 save
= cp_parser_begin_omp_structured_block (parser
);
38660 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
38662 cp_parser_end_omp_structured_block (parser
, save
);
38663 tree body
= finish_omp_structured_block (sb
);
38666 return c_finish_omp_master (loc
, body
);
38669 if (!flag_openmp
) /* flag_openmp_simd */
38671 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38677 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38679 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
38682 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38684 return c_finish_omp_master (loc
,
38685 cp_parser_omp_structured_block (parser
, if_p
));
38689 # pragma omp ordered new-line
38693 # pragma omp ordered ordered-clauses new-line
38694 structured-block */
38696 #define OMP_ORDERED_CLAUSE_MASK \
38697 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
38698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
38700 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
38701 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
38704 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
38705 enum pragma_context context
, bool *if_p
)
38707 location_t loc
= pragma_tok
->location
;
38709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38711 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38712 const char *p
= IDENTIFIER_POINTER (id
);
38714 if (strcmp (p
, "depend") == 0)
38716 if (!flag_openmp
) /* flag_openmp_simd */
38718 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38721 if (context
== pragma_stmt
)
38723 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
38724 "%<depend%> clause may only be used in compound "
38726 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38730 = cp_parser_omp_all_clauses (parser
,
38731 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
38732 "#pragma omp ordered", pragma_tok
);
38733 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
38739 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
38740 "#pragma omp ordered", pragma_tok
);
38742 if (!flag_openmp
/* flag_openmp_simd */
38743 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
38746 c_finish_omp_ordered (loc
, clauses
,
38747 cp_parser_omp_structured_block (parser
, if_p
));
38754 { section-sequence }
38757 section-directive[opt] structured-block
38758 section-sequence section-directive structured-block */
38761 cp_parser_omp_sections_scope (cp_parser
*parser
)
38763 tree stmt
, substmt
;
38764 bool error_suppress
= false;
38767 matching_braces braces
;
38768 if (!braces
.require_open (parser
))
38771 stmt
= push_stmt_list ();
38773 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
38774 != PRAGMA_OMP_SECTION
)
38776 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
38777 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
38778 add_stmt (substmt
);
38783 tok
= cp_lexer_peek_token (parser
->lexer
);
38784 if (tok
->type
== CPP_CLOSE_BRACE
)
38786 if (tok
->type
== CPP_EOF
)
38789 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
38791 cp_lexer_consume_token (parser
->lexer
);
38792 cp_parser_require_pragma_eol (parser
, tok
);
38793 error_suppress
= false;
38795 else if (!error_suppress
)
38797 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
38798 error_suppress
= true;
38801 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
38802 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
38803 add_stmt (substmt
);
38805 braces
.require_close (parser
);
38807 substmt
= pop_stmt_list (stmt
);
38809 stmt
= make_node (OMP_SECTIONS
);
38810 TREE_TYPE (stmt
) = void_type_node
;
38811 OMP_SECTIONS_BODY (stmt
) = substmt
;
38818 # pragma omp sections sections-clause[optseq] newline
38821 #define OMP_SECTIONS_CLAUSE_MASK \
38822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
38825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
38829 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
38830 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
38833 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38835 strcat (p_name
, " sections");
38836 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
38838 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
38840 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
38844 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
38845 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
38848 ret
= cp_parser_omp_sections_scope (parser
);
38850 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
38856 # pragma omp parallel parallel-clause[optseq] new-line
38858 # pragma omp parallel for parallel-for-clause[optseq] new-line
38860 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
38864 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
38865 structured-block */
38867 #define OMP_PARALLEL_CLAUSE_MASK \
38868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
38869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
38870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
38871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
38872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
38873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
38874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
38875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
38876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
38879 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
38880 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
38883 tree stmt
, clauses
, block
;
38885 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38887 strcat (p_name
, " parallel");
38888 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
38889 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
38890 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
38891 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
38892 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
38894 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
38896 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38897 if (cclauses
== NULL
)
38898 cclauses
= cclauses_buf
;
38900 cp_lexer_consume_token (parser
->lexer
);
38901 if (!flag_openmp
) /* flag_openmp_simd */
38902 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
38904 block
= begin_omp_parallel ();
38905 save
= cp_parser_begin_omp_structured_block (parser
);
38906 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
38908 cp_parser_end_omp_structured_block (parser
, save
);
38909 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
38911 if (ret
== NULL_TREE
)
38913 OMP_PARALLEL_COMBINED (stmt
) = 1;
38916 /* When combined with distribute, parallel has to be followed by for.
38917 #pragma omp target parallel is allowed though. */
38919 && (mask
& (OMP_CLAUSE_MASK_1
38920 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
38922 error_at (loc
, "expected %<for%> after %qs", p_name
);
38923 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38926 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38928 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38929 const char *p
= IDENTIFIER_POINTER (id
);
38930 if (cclauses
== NULL
&& strcmp (p
, "master") == 0)
38932 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38933 cclauses
= cclauses_buf
;
38935 cp_lexer_consume_token (parser
->lexer
);
38936 block
= begin_omp_parallel ();
38937 save
= cp_parser_begin_omp_structured_block (parser
);
38938 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
38940 cp_parser_end_omp_structured_block (parser
, save
);
38941 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
38943 OMP_PARALLEL_COMBINED (stmt
) = 1;
38944 if (ret
== NULL_TREE
)
38948 else if (strcmp (p
, "loop") == 0)
38950 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38951 if (cclauses
== NULL
)
38952 cclauses
= cclauses_buf
;
38954 cp_lexer_consume_token (parser
->lexer
);
38955 if (!flag_openmp
) /* flag_openmp_simd */
38956 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
38958 block
= begin_omp_parallel ();
38959 save
= cp_parser_begin_omp_structured_block (parser
);
38960 tree ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
38962 cp_parser_end_omp_structured_block (parser
, save
);
38963 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
38965 if (ret
== NULL_TREE
)
38967 OMP_PARALLEL_COMBINED (stmt
) = 1;
38970 else if (!flag_openmp
) /* flag_openmp_simd */
38972 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38975 else if (cclauses
== NULL
&& strcmp (p
, "sections") == 0)
38977 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
38978 cclauses
= cclauses_buf
;
38980 cp_lexer_consume_token (parser
->lexer
);
38981 block
= begin_omp_parallel ();
38982 save
= cp_parser_begin_omp_structured_block (parser
);
38983 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
38984 cp_parser_end_omp_structured_block (parser
, save
);
38985 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
38987 OMP_PARALLEL_COMBINED (stmt
) = 1;
38991 else if (!flag_openmp
) /* flag_openmp_simd */
38993 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38997 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39001 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
39002 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
39005 block
= begin_omp_parallel ();
39006 save
= cp_parser_begin_omp_structured_block (parser
);
39007 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39008 cp_parser_end_omp_structured_block (parser
, save
);
39009 stmt
= finish_omp_parallel (clauses
, block
);
39014 # pragma omp single single-clause[optseq] new-line
39015 structured-block */
39017 #define OMP_SINGLE_CLAUSE_MASK \
39018 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
39021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39024 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39026 tree stmt
= make_node (OMP_SINGLE
);
39027 TREE_TYPE (stmt
) = void_type_node
;
39028 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39030 OMP_SINGLE_CLAUSES (stmt
)
39031 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
39032 "#pragma omp single", pragma_tok
);
39033 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
39035 return add_stmt (stmt
);
39039 # pragma omp task task-clause[optseq] new-line
39040 structured-block */
39042 #define OMP_TASK_CLAUSE_MASK \
39043 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
39045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
39046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39049 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
39050 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
39051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
39053 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
39056 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39058 tree clauses
, block
;
39061 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
39062 "#pragma omp task", pragma_tok
);
39063 block
= begin_omp_task ();
39064 save
= cp_parser_begin_omp_structured_block (parser
);
39065 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39066 cp_parser_end_omp_structured_block (parser
, save
);
39067 return finish_omp_task (clauses
, block
);
39071 # pragma omp taskwait new-line
39074 # pragma omp taskwait taskwait-clause[opt] new-line */
39076 #define OMP_TASKWAIT_CLAUSE_MASK \
39077 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
39080 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
39083 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
39084 "#pragma omp taskwait", pragma_tok
);
39088 tree stmt
= make_node (OMP_TASK
);
39089 TREE_TYPE (stmt
) = void_node
;
39090 OMP_TASK_CLAUSES (stmt
) = clauses
;
39091 OMP_TASK_BODY (stmt
) = NULL_TREE
;
39092 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39096 finish_omp_taskwait ();
39100 # pragma omp taskyield new-line */
39103 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
39105 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39106 finish_omp_taskyield ();
39110 # pragma omp taskgroup new-line
39114 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
39116 #define OMP_TASKGROUP_CLAUSE_MASK \
39117 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
39120 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39123 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
39124 "#pragma omp taskgroup", pragma_tok
);
39125 return c_finish_omp_taskgroup (input_location
,
39126 cp_parser_omp_structured_block (parser
,
39133 # pragma omp threadprivate (variable-list) */
39136 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
39140 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
39141 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39143 finish_omp_threadprivate (vars
);
39147 # pragma omp cancel cancel-clause[optseq] new-line */
39149 #define OMP_CANCEL_CLAUSE_MASK \
39150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
39151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
39152 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
39153 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
39154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
39157 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
39159 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
39160 "#pragma omp cancel", pragma_tok
);
39161 finish_omp_cancel (clauses
);
39165 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
39167 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
39168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
39169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
39170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
39171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
39174 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
39175 enum pragma_context context
)
39178 bool point_seen
= false;
39180 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39182 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39183 const char *p
= IDENTIFIER_POINTER (id
);
39185 if (strcmp (p
, "point") == 0)
39187 cp_lexer_consume_token (parser
->lexer
);
39193 cp_parser_error (parser
, "expected %<point%>");
39194 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39198 if (context
!= pragma_compound
)
39200 if (context
== pragma_stmt
)
39201 error_at (pragma_tok
->location
,
39202 "%<#pragma %s%> may only be used in compound statements",
39203 "omp cancellation point");
39205 cp_parser_error (parser
, "expected declaration specifiers");
39206 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39210 clauses
= cp_parser_omp_all_clauses (parser
,
39211 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
39212 "#pragma omp cancellation point",
39214 finish_omp_cancellation_point (clauses
);
39218 #pragma omp distribute distribute-clause[optseq] new-line
39221 #define OMP_DISTRIBUTE_CLAUSE_MASK \
39222 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
39225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
39226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
39229 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
39230 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39233 tree clauses
, sb
, ret
;
39235 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39237 strcat (p_name
, " distribute");
39238 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
39240 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39242 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39243 const char *p
= IDENTIFIER_POINTER (id
);
39245 bool parallel
= false;
39247 if (strcmp (p
, "simd") == 0)
39250 parallel
= strcmp (p
, "parallel") == 0;
39251 if (parallel
|| simd
)
39253 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39254 if (cclauses
== NULL
)
39255 cclauses
= cclauses_buf
;
39256 cp_lexer_consume_token (parser
->lexer
);
39257 if (!flag_openmp
) /* flag_openmp_simd */
39260 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39263 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
39266 sb
= begin_omp_structured_block ();
39267 save
= cp_parser_begin_omp_structured_block (parser
);
39269 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
39272 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
39274 cp_parser_end_omp_structured_block (parser
, save
);
39275 tree body
= finish_omp_structured_block (sb
);
39278 ret
= make_node (OMP_DISTRIBUTE
);
39279 TREE_TYPE (ret
) = void_type_node
;
39280 OMP_FOR_BODY (ret
) = body
;
39281 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
39282 SET_EXPR_LOCATION (ret
, loc
);
39287 if (!flag_openmp
) /* flag_openmp_simd */
39289 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39293 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39297 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
39298 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
39301 keep_next_level (true);
39302 sb
= begin_omp_structured_block ();
39303 save
= cp_parser_begin_omp_structured_block (parser
);
39305 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
39307 cp_parser_end_omp_structured_block (parser
, save
);
39308 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
39314 # pragma omp teams teams-clause[optseq] new-line
39315 structured-block */
39317 #define OMP_TEAMS_CLAUSE_MASK \
39318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
39321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
39322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
39323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
39324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
39327 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
39328 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
39331 tree clauses
, sb
, ret
;
39333 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39335 strcat (p_name
, " teams");
39336 mask
|= OMP_TEAMS_CLAUSE_MASK
;
39338 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39340 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39341 const char *p
= IDENTIFIER_POINTER (id
);
39342 if (strcmp (p
, "distribute") == 0)
39344 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39345 if (cclauses
== NULL
)
39346 cclauses
= cclauses_buf
;
39348 cp_lexer_consume_token (parser
->lexer
);
39349 if (!flag_openmp
) /* flag_openmp_simd */
39350 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
39352 keep_next_level (true);
39353 sb
= begin_omp_structured_block ();
39354 save
= cp_parser_begin_omp_structured_block (parser
);
39355 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
39357 cp_parser_end_omp_structured_block (parser
, save
);
39358 tree body
= finish_omp_structured_block (sb
);
39361 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
39362 ret
= make_node (OMP_TEAMS
);
39363 TREE_TYPE (ret
) = void_type_node
;
39364 OMP_TEAMS_CLAUSES (ret
) = clauses
;
39365 OMP_TEAMS_BODY (ret
) = body
;
39366 OMP_TEAMS_COMBINED (ret
) = 1;
39367 SET_EXPR_LOCATION (ret
, loc
);
39368 return add_stmt (ret
);
39370 else if (strcmp (p
, "loop") == 0)
39372 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
39373 if (cclauses
== NULL
)
39374 cclauses
= cclauses_buf
;
39376 cp_lexer_consume_token (parser
->lexer
);
39377 if (!flag_openmp
) /* flag_openmp_simd */
39378 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
39380 keep_next_level (true);
39381 sb
= begin_omp_structured_block ();
39382 save
= cp_parser_begin_omp_structured_block (parser
);
39383 ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
39385 cp_parser_end_omp_structured_block (parser
, save
);
39386 tree body
= finish_omp_structured_block (sb
);
39389 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
39390 ret
= make_node (OMP_TEAMS
);
39391 TREE_TYPE (ret
) = void_type_node
;
39392 OMP_TEAMS_CLAUSES (ret
) = clauses
;
39393 OMP_TEAMS_BODY (ret
) = body
;
39394 OMP_TEAMS_COMBINED (ret
) = 1;
39395 SET_EXPR_LOCATION (ret
, loc
);
39396 return add_stmt (ret
);
39399 if (!flag_openmp
) /* flag_openmp_simd */
39401 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39405 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
39409 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
39410 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
39413 tree stmt
= make_node (OMP_TEAMS
);
39414 TREE_TYPE (stmt
) = void_type_node
;
39415 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
39416 keep_next_level (true);
39417 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
39418 SET_EXPR_LOCATION (stmt
, loc
);
39420 return add_stmt (stmt
);
39424 # pragma omp target data target-data-clause[optseq] new-line
39425 structured-block */
39427 #define OMP_TARGET_DATA_CLAUSE_MASK \
39428 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39429 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
39432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
39435 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39438 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
39439 "#pragma omp target data", pragma_tok
);
39441 for (tree
*pc
= &clauses
; *pc
;)
39443 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
39444 switch (OMP_CLAUSE_MAP_KIND (*pc
))
39447 case GOMP_MAP_ALWAYS_TO
:
39448 case GOMP_MAP_FROM
:
39449 case GOMP_MAP_ALWAYS_FROM
:
39450 case GOMP_MAP_TOFROM
:
39451 case GOMP_MAP_ALWAYS_TOFROM
:
39452 case GOMP_MAP_ALLOC
:
39455 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
39456 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
39457 case GOMP_MAP_ALWAYS_POINTER
:
39461 error_at (OMP_CLAUSE_LOCATION (*pc
),
39462 "%<#pragma omp target data%> with map-type other "
39463 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39464 "on %<map%> clause");
39465 *pc
= OMP_CLAUSE_CHAIN (*pc
);
39468 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
39469 || OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_ADDR
)
39471 pc
= &OMP_CLAUSE_CHAIN (*pc
);
39477 error_at (pragma_tok
->location
,
39478 "%<#pragma omp target data%> must contain at least "
39479 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
39484 tree stmt
= make_node (OMP_TARGET_DATA
);
39485 TREE_TYPE (stmt
) = void_type_node
;
39486 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
39488 keep_next_level (true);
39489 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
39491 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39492 return add_stmt (stmt
);
39496 # pragma omp target enter data target-enter-data-clause[optseq] new-line
39497 structured-block */
39499 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
39500 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39502 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39503 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39507 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
39508 enum pragma_context context
)
39510 bool data_seen
= false;
39511 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39513 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39514 const char *p
= IDENTIFIER_POINTER (id
);
39516 if (strcmp (p
, "data") == 0)
39518 cp_lexer_consume_token (parser
->lexer
);
39524 cp_parser_error (parser
, "expected %<data%>");
39525 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39529 if (context
== pragma_stmt
)
39531 error_at (pragma_tok
->location
,
39532 "%<#pragma %s%> may only be used in compound statements",
39533 "omp target enter data");
39534 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39539 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
39540 "#pragma omp target enter data", pragma_tok
);
39542 for (tree
*pc
= &clauses
; *pc
;)
39544 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
39545 switch (OMP_CLAUSE_MAP_KIND (*pc
))
39548 case GOMP_MAP_ALWAYS_TO
:
39549 case GOMP_MAP_ALLOC
:
39552 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
39553 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
39554 case GOMP_MAP_ALWAYS_POINTER
:
39558 error_at (OMP_CLAUSE_LOCATION (*pc
),
39559 "%<#pragma omp target enter data%> with map-type other "
39560 "than %<to%> or %<alloc%> on %<map%> clause");
39561 *pc
= OMP_CLAUSE_CHAIN (*pc
);
39564 pc
= &OMP_CLAUSE_CHAIN (*pc
);
39570 error_at (pragma_tok
->location
,
39571 "%<#pragma omp target enter data%> must contain at least "
39572 "one %<map%> clause");
39576 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
39577 TREE_TYPE (stmt
) = void_type_node
;
39578 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
39579 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39580 return add_stmt (stmt
);
39584 # pragma omp target exit data target-enter-data-clause[optseq] new-line
39585 structured-block */
39587 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
39588 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39595 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
39596 enum pragma_context context
)
39598 bool data_seen
= false;
39599 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39601 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39602 const char *p
= IDENTIFIER_POINTER (id
);
39604 if (strcmp (p
, "data") == 0)
39606 cp_lexer_consume_token (parser
->lexer
);
39612 cp_parser_error (parser
, "expected %<data%>");
39613 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39617 if (context
== pragma_stmt
)
39619 error_at (pragma_tok
->location
,
39620 "%<#pragma %s%> may only be used in compound statements",
39621 "omp target exit data");
39622 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39627 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
39628 "#pragma omp target exit data", pragma_tok
);
39630 for (tree
*pc
= &clauses
; *pc
;)
39632 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
39633 switch (OMP_CLAUSE_MAP_KIND (*pc
))
39635 case GOMP_MAP_FROM
:
39636 case GOMP_MAP_ALWAYS_FROM
:
39637 case GOMP_MAP_RELEASE
:
39638 case GOMP_MAP_DELETE
:
39641 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
39642 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
39643 case GOMP_MAP_ALWAYS_POINTER
:
39647 error_at (OMP_CLAUSE_LOCATION (*pc
),
39648 "%<#pragma omp target exit data%> with map-type other "
39649 "than %<from%>, %<release%> or %<delete%> on %<map%>"
39651 *pc
= OMP_CLAUSE_CHAIN (*pc
);
39654 pc
= &OMP_CLAUSE_CHAIN (*pc
);
39660 error_at (pragma_tok
->location
,
39661 "%<#pragma omp target exit data%> must contain at least "
39662 "one %<map%> clause");
39666 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
39667 TREE_TYPE (stmt
) = void_type_node
;
39668 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
39669 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39670 return add_stmt (stmt
);
39674 # pragma omp target update target-update-clause[optseq] new-line */
39676 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
39677 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
39678 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
39679 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39680 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
39685 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
39686 enum pragma_context context
)
39688 if (context
== pragma_stmt
)
39690 error_at (pragma_tok
->location
,
39691 "%<#pragma %s%> may only be used in compound statements",
39692 "omp target update");
39693 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39698 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
39699 "#pragma omp target update", pragma_tok
);
39700 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
39701 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
39703 error_at (pragma_tok
->location
,
39704 "%<#pragma omp target update%> must contain at least one "
39705 "%<from%> or %<to%> clauses");
39709 tree stmt
= make_node (OMP_TARGET_UPDATE
);
39710 TREE_TYPE (stmt
) = void_type_node
;
39711 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
39712 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39718 # pragma omp target target-clause[optseq] new-line
39719 structured-block */
39721 #define OMP_TARGET_CLAUSE_MASK \
39722 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
39723 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
39724 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
39725 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
39726 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
39727 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
39728 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
39729 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
39730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
39733 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
39734 enum pragma_context context
, bool *if_p
)
39736 tree
*pc
= NULL
, stmt
;
39740 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
39742 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39744 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39745 const char *p
= IDENTIFIER_POINTER (id
);
39746 enum tree_code ccode
= ERROR_MARK
;
39748 if (strcmp (p
, "teams") == 0)
39750 else if (strcmp (p
, "parallel") == 0)
39751 ccode
= OMP_PARALLEL
;
39752 else if (strcmp (p
, "simd") == 0)
39754 if (ccode
!= ERROR_MARK
)
39756 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
39757 char p_name
[sizeof ("#pragma omp target teams distribute "
39758 "parallel for simd")];
39760 cp_lexer_consume_token (parser
->lexer
);
39761 strcpy (p_name
, "#pragma omp target");
39762 if (!flag_openmp
) /* flag_openmp_simd */
39768 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
39769 OMP_TARGET_CLAUSE_MASK
,
39773 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
39774 OMP_TARGET_CLAUSE_MASK
,
39778 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
39779 OMP_TARGET_CLAUSE_MASK
,
39783 gcc_unreachable ();
39785 return stmt
!= NULL_TREE
;
39787 keep_next_level (true);
39788 tree sb
= begin_omp_structured_block (), ret
;
39789 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
39793 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
39794 OMP_TARGET_CLAUSE_MASK
, cclauses
,
39798 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
39799 OMP_TARGET_CLAUSE_MASK
, cclauses
,
39803 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
39804 OMP_TARGET_CLAUSE_MASK
, cclauses
,
39808 gcc_unreachable ();
39810 cp_parser_end_omp_structured_block (parser
, save
);
39811 tree body
= finish_omp_structured_block (sb
);
39812 if (ret
== NULL_TREE
)
39814 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
39816 /* For combined target teams, ensure the num_teams and
39817 thread_limit clause expressions are evaluated on the host,
39818 before entering the target construct. */
39820 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
39821 c
; c
= OMP_CLAUSE_CHAIN (c
))
39822 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
39823 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
39824 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
39826 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
39827 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
39828 if (expr
== error_mark_node
)
39830 tree tmp
= TARGET_EXPR_SLOT (expr
);
39832 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
39833 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
39834 OMP_CLAUSE_FIRSTPRIVATE
);
39835 OMP_CLAUSE_DECL (tc
) = tmp
;
39836 OMP_CLAUSE_CHAIN (tc
)
39837 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
39838 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
39841 tree stmt
= make_node (OMP_TARGET
);
39842 TREE_TYPE (stmt
) = void_type_node
;
39843 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
39844 OMP_TARGET_BODY (stmt
) = body
;
39845 OMP_TARGET_COMBINED (stmt
) = 1;
39846 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39848 pc
= &OMP_TARGET_CLAUSES (stmt
);
39849 goto check_clauses
;
39851 else if (!flag_openmp
) /* flag_openmp_simd */
39853 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39856 else if (strcmp (p
, "data") == 0)
39858 cp_lexer_consume_token (parser
->lexer
);
39859 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
39862 else if (strcmp (p
, "enter") == 0)
39864 cp_lexer_consume_token (parser
->lexer
);
39865 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
39868 else if (strcmp (p
, "exit") == 0)
39870 cp_lexer_consume_token (parser
->lexer
);
39871 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
39874 else if (strcmp (p
, "update") == 0)
39876 cp_lexer_consume_token (parser
->lexer
);
39877 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
39880 if (!flag_openmp
) /* flag_openmp_simd */
39882 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39886 stmt
= make_node (OMP_TARGET
);
39887 TREE_TYPE (stmt
) = void_type_node
;
39889 OMP_TARGET_CLAUSES (stmt
)
39890 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
39891 "#pragma omp target", pragma_tok
);
39892 pc
= &OMP_TARGET_CLAUSES (stmt
);
39893 keep_next_level (true);
39894 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
39896 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39902 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
39903 switch (OMP_CLAUSE_MAP_KIND (*pc
))
39906 case GOMP_MAP_ALWAYS_TO
:
39907 case GOMP_MAP_FROM
:
39908 case GOMP_MAP_ALWAYS_FROM
:
39909 case GOMP_MAP_TOFROM
:
39910 case GOMP_MAP_ALWAYS_TOFROM
:
39911 case GOMP_MAP_ALLOC
:
39912 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
39913 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
39914 case GOMP_MAP_ALWAYS_POINTER
:
39917 error_at (OMP_CLAUSE_LOCATION (*pc
),
39918 "%<#pragma omp target%> with map-type other "
39919 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
39920 "on %<map%> clause");
39921 *pc
= OMP_CLAUSE_CHAIN (*pc
);
39924 pc
= &OMP_CLAUSE_CHAIN (*pc
);
39930 # pragma acc cache (variable-list) new-line
39934 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
39936 tree stmt
, clauses
;
39938 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
39939 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
39941 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
39943 stmt
= make_node (OACC_CACHE
);
39944 TREE_TYPE (stmt
) = void_type_node
;
39945 OACC_CACHE_CLAUSES (stmt
) = clauses
;
39946 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
39953 # pragma acc data oacc-data-clause[optseq] new-line
39954 structured-block */
39956 #define OACC_DATA_CLAUSE_MASK \
39957 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
39958 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
39959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
39960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
39961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
39962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
39963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
39966 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39968 tree stmt
, clauses
, block
;
39971 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
39972 "#pragma acc data", pragma_tok
);
39974 block
= begin_omp_parallel ();
39975 save
= cp_parser_begin_omp_structured_block (parser
);
39976 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
39977 cp_parser_end_omp_structured_block (parser
, save
);
39978 stmt
= finish_oacc_data (clauses
, block
);
39983 # pragma acc host_data <clauses> new-line
39984 structured-block */
39986 #define OACC_HOST_DATA_CLAUSE_MASK \
39987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
39990 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39992 tree stmt
, clauses
, block
;
39995 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
39996 "#pragma acc host_data", pragma_tok
);
39998 block
= begin_omp_parallel ();
39999 save
= cp_parser_begin_omp_structured_block (parser
);
40000 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40001 cp_parser_end_omp_structured_block (parser
, save
);
40002 stmt
= finish_oacc_host_data (clauses
, block
);
40007 # pragma acc declare oacc-data-clause[optseq] new-line
40010 #define OACC_DECLARE_CLAUSE_MASK \
40011 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40015 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40016 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
40017 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
40018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
40021 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
40023 tree clauses
, stmt
;
40024 bool error
= false;
40026 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
40027 "#pragma acc declare", pragma_tok
, true);
40030 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
40032 error_at (pragma_tok
->location
,
40033 "no valid clauses specified in %<#pragma acc declare%>");
40037 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
40039 location_t loc
= OMP_CLAUSE_LOCATION (t
);
40040 tree decl
= OMP_CLAUSE_DECL (t
);
40041 if (!DECL_P (decl
))
40043 error_at (loc
, "array section in %<#pragma acc declare%>");
40047 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
40048 switch (OMP_CLAUSE_MAP_KIND (t
))
40050 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
40051 case GOMP_MAP_ALLOC
:
40053 case GOMP_MAP_FORCE_DEVICEPTR
:
40054 case GOMP_MAP_DEVICE_RESIDENT
:
40057 case GOMP_MAP_LINK
:
40058 if (!global_bindings_p ()
40059 && (TREE_STATIC (decl
)
40060 || !DECL_EXTERNAL (decl
)))
40063 "%qD must be a global variable in "
40064 "%<#pragma acc declare link%>",
40072 if (global_bindings_p ())
40074 error_at (loc
, "invalid OpenACC clause at file scope");
40078 if (DECL_EXTERNAL (decl
))
40081 "invalid use of %<extern%> variable %qD "
40082 "in %<#pragma acc declare%>", decl
);
40086 else if (TREE_PUBLIC (decl
))
40089 "invalid use of %<global%> variable %qD "
40090 "in %<#pragma acc declare%>", decl
);
40097 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
40098 || lookup_attribute ("omp declare target link",
40099 DECL_ATTRIBUTES (decl
)))
40101 error_at (loc
, "variable %qD used more than once with "
40102 "%<#pragma acc declare%>", decl
);
40111 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
40112 id
= get_identifier ("omp declare target link");
40114 id
= get_identifier ("omp declare target");
40116 DECL_ATTRIBUTES (decl
)
40117 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
40118 if (global_bindings_p ())
40120 symtab_node
*node
= symtab_node::get (decl
);
40123 node
->offloadable
= 1;
40124 if (ENABLE_OFFLOADING
)
40126 g
->have_offload
= true;
40127 if (is_a
<varpool_node
*> (node
))
40128 vec_safe_push (offload_vars
, decl
);
40135 if (error
|| global_bindings_p ())
40138 stmt
= make_node (OACC_DECLARE
);
40139 TREE_TYPE (stmt
) = void_type_node
;
40140 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
40141 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40149 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
40153 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
40155 LOC is the location of the #pragma token.
40158 #define OACC_ENTER_DATA_CLAUSE_MASK \
40159 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40165 #define OACC_EXIT_DATA_CLAUSE_MASK \
40166 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40167 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
40170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
40171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40174 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
40177 location_t loc
= pragma_tok
->location
;
40178 tree stmt
, clauses
;
40179 const char *p
= "";
40181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40182 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
40184 if (strcmp (p
, "data") != 0)
40186 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
40187 enter
? "enter" : "exit");
40188 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40192 cp_lexer_consume_token (parser
->lexer
);
40195 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
40196 "#pragma acc enter data", pragma_tok
);
40198 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
40199 "#pragma acc exit data", pragma_tok
);
40201 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
40203 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
40204 enter
? "enter" : "exit");
40208 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
40209 TREE_TYPE (stmt
) = void_type_node
;
40210 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
40211 SET_EXPR_LOCATION (stmt
, loc
);
40217 # pragma acc loop oacc-loop-clause[optseq] new-line
40218 structured-block */
40220 #define OACC_LOOP_CLAUSE_MASK \
40221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
40222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
40225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
40226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
40227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
40228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
40229 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
40230 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
40233 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
40234 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
40236 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
40238 strcat (p_name
, " loop");
40239 mask
|= OACC_LOOP_CLAUSE_MASK
;
40241 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40245 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
40247 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
40249 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
40252 tree block
= begin_omp_structured_block ();
40253 int save
= cp_parser_begin_omp_structured_block (parser
);
40254 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
40255 cp_parser_end_omp_structured_block (parser
, save
);
40256 add_stmt (finish_omp_structured_block (block
));
40262 # pragma acc kernels oacc-kernels-clause[optseq] new-line
40267 # pragma acc parallel oacc-parallel-clause[optseq] new-line
40272 # pragma acc serial oacc-serial-clause[optseq] new-line
40275 #define OACC_KERNELS_CLAUSE_MASK \
40276 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40278 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40279 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
40285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
40286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
40288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40290 #define OACC_PARALLEL_CLAUSE_MASK \
40291 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
40299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
40301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
40302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40305 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
40306 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40308 #define OACC_SERIAL_CLAUSE_MASK \
40309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
40311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
40312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
40313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
40314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
40315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
40316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
40318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
40319 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
40320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
40321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
40324 cp_parser_oacc_compute (cp_parser
*parser
, cp_token
*pragma_tok
,
40325 char *p_name
, bool *if_p
)
40327 omp_clause_mask mask
;
40328 enum tree_code code
;
40329 switch (cp_parser_pragma_kind (pragma_tok
))
40331 case PRAGMA_OACC_KERNELS
:
40332 strcat (p_name
, " kernels");
40333 mask
= OACC_KERNELS_CLAUSE_MASK
;
40334 code
= OACC_KERNELS
;
40336 case PRAGMA_OACC_PARALLEL
:
40337 strcat (p_name
, " parallel");
40338 mask
= OACC_PARALLEL_CLAUSE_MASK
;
40339 code
= OACC_PARALLEL
;
40341 case PRAGMA_OACC_SERIAL
:
40342 strcat (p_name
, " serial");
40343 mask
= OACC_SERIAL_CLAUSE_MASK
;
40344 code
= OACC_SERIAL
;
40347 gcc_unreachable ();
40350 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40353 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
40354 if (strcmp (p
, "loop") == 0)
40356 cp_lexer_consume_token (parser
->lexer
);
40357 tree block
= begin_omp_parallel ();
40359 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
40361 protected_set_expr_location (stmt
, pragma_tok
->location
);
40362 return finish_omp_construct (code
, block
, clauses
);
40366 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
40368 tree block
= begin_omp_parallel ();
40369 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
40370 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40371 cp_parser_end_omp_structured_block (parser
, save
);
40372 return finish_omp_construct (code
, block
, clauses
);
40376 # pragma acc update oacc-update-clause[optseq] new-line
40379 #define OACC_UPDATE_CLAUSE_MASK \
40380 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
40381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
40382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
40383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
40384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
40385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
40388 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
40390 tree stmt
, clauses
;
40392 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
40393 "#pragma acc update", pragma_tok
);
40395 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
40397 error_at (pragma_tok
->location
,
40398 "%<#pragma acc update%> must contain at least one "
40399 "%<device%> or %<host%> or %<self%> clause");
40403 stmt
= make_node (OACC_UPDATE
);
40404 TREE_TYPE (stmt
) = void_type_node
;
40405 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
40406 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
40412 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
40414 LOC is the location of the #pragma token.
40417 #define OACC_WAIT_CLAUSE_MASK \
40418 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
40421 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
40423 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
40424 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40426 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
40427 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
40429 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
40430 "#pragma acc wait", pragma_tok
);
40432 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
40433 stmt
= finish_expr_stmt (stmt
);
40439 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
40441 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
40442 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
40446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
40447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
40450 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
40451 enum pragma_context context
,
40454 bool first_p
= parser
->omp_declare_simd
== NULL
;
40455 cp_omp_declare_simd_data data
;
40458 data
.error_seen
= false;
40459 data
.fndecl_seen
= false;
40460 data
.variant_p
= variant_p
;
40461 data
.tokens
= vNULL
;
40462 data
.clauses
= NULL_TREE
;
40463 /* It is safe to take the address of a local variable; it will only be
40464 used while this scope is live. */
40465 parser
->omp_declare_simd
= &data
;
40467 else if (parser
->omp_declare_simd
->variant_p
!= variant_p
)
40469 error_at (pragma_tok
->location
,
40470 "%<#pragma omp declare %s%> followed by "
40471 "%<#pragma omp declare %s%>",
40472 parser
->omp_declare_simd
->variant_p
? "variant" : "simd",
40473 parser
->omp_declare_simd
->variant_p
? "simd" : "variant");
40474 parser
->omp_declare_simd
->error_seen
= true;
40477 /* Store away all pragma tokens. */
40478 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
40479 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
40480 cp_lexer_consume_token (parser
->lexer
);
40481 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
40482 parser
->omp_declare_simd
->error_seen
= true;
40483 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40484 struct cp_token_cache
*cp
40485 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
40486 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
40490 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
40491 cp_parser_pragma (parser
, context
, NULL
);
40494 case pragma_external
:
40495 cp_parser_declaration (parser
);
40497 case pragma_member
:
40498 cp_parser_member_declaration (parser
);
40500 case pragma_objc_icode
:
40501 cp_parser_block_declaration (parser
, /*statement_p=*/false);
40504 cp_parser_declaration_statement (parser
);
40507 if (parser
->omp_declare_simd
40508 && !parser
->omp_declare_simd
->error_seen
40509 && !parser
->omp_declare_simd
->fndecl_seen
)
40510 error_at (pragma_tok
->location
,
40511 "%<#pragma omp declare %s%> not immediately followed by "
40512 "function declaration or definition",
40513 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
40514 data
.tokens
.release ();
40515 parser
->omp_declare_simd
= NULL
;
40519 static const char *const omp_construct_selectors
[] = {
40520 "simd", "target", "teams", "parallel", "for", NULL
};
40521 static const char *const omp_device_selectors
[] = {
40522 "kind", "isa", "arch", NULL
};
40523 static const char *const omp_implementation_selectors
[] = {
40524 "vendor", "extension", "atomic_default_mem_order", "unified_address",
40525 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL
};
40526 static const char *const omp_user_selectors
[] = {
40527 "condition", NULL
};
40532 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
40535 score(score-expression) */
40538 cp_parser_omp_context_selector (cp_parser
*parser
, tree set
, bool has_parms_p
)
40540 tree ret
= NULL_TREE
;
40544 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
40545 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40546 selector
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40549 cp_parser_error (parser
, "expected trait selector name");
40550 return error_mark_node
;
40553 tree properties
= NULL_TREE
;
40554 const char *const *selectors
= NULL
;
40555 bool allow_score
= true;
40556 bool allow_user
= false;
40557 int property_limit
= 0;
40558 enum { CTX_PROPERTY_NONE
, CTX_PROPERTY_USER
, CTX_PROPERTY_NAME_LIST
,
40559 CTX_PROPERTY_ID
, CTX_PROPERTY_EXPR
,
40560 CTX_PROPERTY_SIMD
} property_kind
= CTX_PROPERTY_NONE
;
40561 switch (IDENTIFIER_POINTER (set
)[0])
40563 case 'c': /* construct */
40564 selectors
= omp_construct_selectors
;
40565 allow_score
= false;
40566 property_limit
= 1;
40567 property_kind
= CTX_PROPERTY_SIMD
;
40569 case 'd': /* device */
40570 selectors
= omp_device_selectors
;
40571 allow_score
= false;
40573 property_limit
= 3;
40574 property_kind
= CTX_PROPERTY_NAME_LIST
;
40576 case 'i': /* implementation */
40577 selectors
= omp_implementation_selectors
;
40579 property_limit
= 3;
40580 property_kind
= CTX_PROPERTY_NAME_LIST
;
40582 case 'u': /* user */
40583 selectors
= omp_user_selectors
;
40584 property_limit
= 1;
40585 property_kind
= CTX_PROPERTY_EXPR
;
40588 gcc_unreachable ();
40590 for (int i
= 0; ; i
++)
40592 if (selectors
[i
] == NULL
)
40596 property_kind
= CTX_PROPERTY_USER
;
40601 error ("selector %qs not allowed for context selector "
40602 "set %qs", IDENTIFIER_POINTER (selector
),
40603 IDENTIFIER_POINTER (set
));
40604 cp_lexer_consume_token (parser
->lexer
);
40605 return error_mark_node
;
40608 if (i
== property_limit
)
40609 property_kind
= CTX_PROPERTY_NONE
;
40610 if (strcmp (selectors
[i
], IDENTIFIER_POINTER (selector
)) == 0)
40613 if (property_kind
== CTX_PROPERTY_NAME_LIST
40614 && IDENTIFIER_POINTER (set
)[0] == 'i'
40615 && strcmp (IDENTIFIER_POINTER (selector
),
40616 "atomic_default_mem_order") == 0)
40617 property_kind
= CTX_PROPERTY_ID
;
40619 cp_lexer_consume_token (parser
->lexer
);
40621 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
40623 if (property_kind
== CTX_PROPERTY_NONE
)
40625 error ("selector %qs does not accept any properties",
40626 IDENTIFIER_POINTER (selector
));
40627 return error_mark_node
;
40630 matching_parens parens
;
40631 parens
.consume_open (parser
);
40633 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
40635 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
40636 && strcmp (IDENTIFIER_POINTER (token
->u
.value
), "score") == 0
40637 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
40639 cp_lexer_save_tokens (parser
->lexer
);
40640 cp_lexer_consume_token (parser
->lexer
);
40641 cp_lexer_consume_token (parser
->lexer
);
40642 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
40644 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
40646 cp_lexer_rollback_tokens (parser
->lexer
);
40647 cp_lexer_consume_token (parser
->lexer
);
40649 matching_parens parens2
;
40650 parens2
.require_open (parser
);
40651 tree score
= cp_parser_constant_expression (parser
);
40652 if (!parens2
.require_close (parser
))
40653 cp_parser_skip_to_closing_parenthesis (parser
, true,
40655 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
40656 if (score
!= error_mark_node
)
40658 score
= fold_non_dependent_expr (score
);
40659 if (value_dependent_expression_p (score
))
40660 properties
= tree_cons (get_identifier (" score"),
40661 score
, properties
);
40662 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score
))
40663 || TREE_CODE (score
) != INTEGER_CST
)
40664 error_at (token
->location
, "score argument must be "
40665 "constant integer expression");
40666 else if (tree_int_cst_sgn (score
) < 0)
40667 error_at (token
->location
, "score argument must be "
40670 properties
= tree_cons (get_identifier (" score"),
40671 score
, properties
);
40675 cp_lexer_rollback_tokens (parser
->lexer
);
40677 token
= cp_lexer_peek_token (parser
->lexer
);
40680 switch (property_kind
)
40683 case CTX_PROPERTY_USER
:
40686 t
= cp_parser_constant_expression (parser
);
40687 if (t
!= error_mark_node
)
40689 t
= fold_non_dependent_expr (t
);
40690 if (TREE_CODE (t
) == STRING_CST
)
40691 properties
= tree_cons (NULL_TREE
, t
, properties
);
40692 else if (!value_dependent_expression_p (t
)
40693 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
40694 || !tree_fits_shwi_p (t
)))
40695 error_at (token
->location
, "property must be "
40696 "constant integer expression or string "
40699 properties
= tree_cons (NULL_TREE
, t
, properties
);
40702 return error_mark_node
;
40704 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40705 cp_lexer_consume_token (parser
->lexer
);
40711 case CTX_PROPERTY_ID
:
40712 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
40713 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40715 tree prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40716 cp_lexer_consume_token (parser
->lexer
);
40717 properties
= tree_cons (prop
, NULL_TREE
, properties
);
40721 cp_parser_error (parser
, "expected identifier");
40722 return error_mark_node
;
40725 case CTX_PROPERTY_NAME_LIST
:
40728 tree prop
= NULL_TREE
, value
= NULL_TREE
;
40729 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
40730 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40732 prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40733 cp_lexer_consume_token (parser
->lexer
);
40735 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_STRING
))
40736 value
= cp_parser_string_literal (parser
, false, false);
40739 cp_parser_error (parser
, "expected identifier or "
40741 return error_mark_node
;
40744 properties
= tree_cons (prop
, value
, properties
);
40746 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40747 cp_lexer_consume_token (parser
->lexer
);
40753 case CTX_PROPERTY_EXPR
:
40754 t
= cp_parser_constant_expression (parser
);
40755 if (t
!= error_mark_node
)
40757 t
= fold_non_dependent_expr (t
);
40758 if (!value_dependent_expression_p (t
)
40759 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
40760 || !tree_fits_shwi_p (t
)))
40761 error_at (token
->location
, "property must be "
40762 "constant integer expression");
40764 properties
= tree_cons (NULL_TREE
, t
, properties
);
40767 return error_mark_node
;
40769 case CTX_PROPERTY_SIMD
:
40772 error_at (token
->location
, "properties for %<simd%> "
40773 "selector may not be specified in "
40774 "%<metadirective%>");
40775 return error_mark_node
;
40778 = cp_parser_omp_all_clauses (parser
,
40779 OMP_DECLARE_SIMD_CLAUSE_MASK
,
40780 "simd", NULL
, true, 2);
40783 gcc_unreachable ();
40786 if (!parens
.require_close (parser
))
40787 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
40789 properties
= nreverse (properties
);
40791 else if (property_kind
== CTX_PROPERTY_NAME_LIST
40792 || property_kind
== CTX_PROPERTY_ID
40793 || property_kind
== CTX_PROPERTY_EXPR
)
40795 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
40796 return error_mark_node
;
40799 ret
= tree_cons (selector
, properties
, ret
);
40801 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40802 cp_lexer_consume_token (parser
->lexer
);
40808 return nreverse (ret
);
40813 trait-set-selector[,trait-set-selector[,...]]
40815 trait-set-selector:
40816 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
40818 trait-set-selector-name:
40825 cp_parser_omp_context_selector_specification (cp_parser
*parser
,
40828 tree ret
= NULL_TREE
;
40831 const char *setp
= "";
40832 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40834 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
40838 if (strcmp (setp
, "construct") == 0)
40842 if (strcmp (setp
, "device") == 0)
40846 if (strcmp (setp
, "implementation") == 0)
40850 if (strcmp (setp
, "user") == 0)
40858 cp_parser_error (parser
, "expected %<construct%>, %<device%>, "
40859 "%<implementation%> or %<user%>");
40860 return error_mark_node
;
40863 tree set
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40864 cp_lexer_consume_token (parser
->lexer
);
40866 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
40867 return error_mark_node
;
40869 matching_braces braces
;
40870 if (!braces
.require_open (parser
))
40871 return error_mark_node
;
40874 = cp_parser_omp_context_selector (parser
, set
, has_parms_p
);
40875 if (selectors
== error_mark_node
)
40877 cp_parser_skip_to_closing_brace (parser
);
40878 ret
= error_mark_node
;
40880 else if (ret
!= error_mark_node
)
40881 ret
= tree_cons (set
, selectors
, ret
);
40883 braces
.require_close (parser
);
40885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
40886 cp_lexer_consume_token (parser
->lexer
);
40892 if (ret
== error_mark_node
)
40894 return nreverse (ret
);
40897 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
40898 that into "omp declare variant base" attribute. */
40901 cp_finish_omp_declare_variant (cp_parser
*parser
, cp_token
*pragma_tok
,
40904 matching_parens parens
;
40905 if (!parens
.require_open (parser
))
40908 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40913 cp_id_kind idk
= CP_ID_KIND_NONE
;
40914 cp_token
*varid_token
= cp_lexer_peek_token (parser
->lexer
);
40916 = cp_parser_id_expression (parser
, /*template_keyword_p=*/false,
40917 /*check_dependency_p=*/true,
40918 /*template_p=*/&template_p
,
40919 /*declarator_p=*/false,
40920 /*optional_p=*/false);
40921 parens
.require_close (parser
);
40924 if (TREE_CODE (varid
) == TEMPLATE_ID_EXPR
40925 || TREE_CODE (varid
) == TYPE_DECL
40926 || varid
== error_mark_node
)
40928 else if (varid_token
->type
== CPP_NAME
&& varid_token
->error_reported
)
40929 variant
= NULL_TREE
;
40932 tree ambiguous_decls
;
40933 variant
= cp_parser_lookup_name (parser
, varid
, none_type
,
40934 template_p
, /*is_namespace=*/false,
40935 /*check_dependency=*/true,
40937 varid
.get_location ());
40938 if (ambiguous_decls
)
40939 variant
= NULL_TREE
;
40941 if (variant
== NULL_TREE
)
40942 variant
= error_mark_node
;
40943 else if (TREE_CODE (variant
) != SCOPE_REF
)
40945 const char *error_msg
;
40947 = finish_id_expression (varid
, variant
, parser
->scope
,
40949 &parser
->non_integral_constant_expression_p
,
40950 template_p
, true, false, false, &error_msg
,
40951 varid
.get_location ());
40953 cp_parser_error (parser
, error_msg
);
40955 location_t caret_loc
= get_pure_location (varid
.get_location ());
40956 location_t start_loc
= get_start (varid_token
->location
);
40957 location_t finish_loc
= get_finish (varid
.get_location ());
40958 location_t varid_loc
= make_location (caret_loc
, start_loc
, finish_loc
);
40960 const char *clause
= "";
40961 location_t match_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40962 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40963 clause
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
40964 if (strcmp (clause
, "match"))
40966 cp_parser_error (parser
, "expected %<match%>");
40970 cp_lexer_consume_token (parser
->lexer
);
40972 if (!parens
.require_open (parser
))
40975 tree ctx
= cp_parser_omp_context_selector_specification (parser
, true);
40976 if (ctx
== error_mark_node
)
40978 ctx
= c_omp_check_context_selector (match_loc
, ctx
);
40979 if (ctx
!= error_mark_node
&& variant
!= error_mark_node
)
40981 tree match_loc_node
= maybe_wrap_with_location (integer_zero_node
,
40983 tree loc_node
= maybe_wrap_with_location (integer_zero_node
, varid_loc
);
40984 loc_node
= tree_cons (match_loc_node
,
40985 build_int_cst (integer_type_node
, idk
),
40986 build_tree_list (loc_node
, integer_zero_node
));
40987 attrs
= tree_cons (get_identifier ("omp declare variant base"),
40988 tree_cons (variant
, ctx
, loc_node
), attrs
);
40989 if (processing_template_decl
)
40990 ATTR_IS_DEPENDENT (attrs
) = 1;
40993 parens
.require_close (parser
);
40994 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40999 /* Finalize #pragma omp declare simd clauses after direct declarator has
41000 been parsed, and put that into "omp declare simd" attribute. */
41003 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
41005 struct cp_token_cache
*ce
;
41006 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
41009 if (!data
->error_seen
&& data
->fndecl_seen
)
41011 error ("%<#pragma omp declare %s%> not immediately followed by "
41012 "a single function declaration or definition",
41013 data
->variant_p
? "variant" : "simd");
41014 data
->error_seen
= true;
41016 if (data
->error_seen
)
41019 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
41023 cp_parser_push_lexer_for_tokens (parser
, ce
);
41024 parser
->lexer
->in_pragma
= true;
41025 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
41026 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
41027 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41028 const char *kind
= IDENTIFIER_POINTER (id
);
41029 cp_lexer_consume_token (parser
->lexer
);
41030 if (strcmp (kind
, "simd") == 0)
41032 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
41033 "#pragma omp declare simd",
41036 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
41037 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
41038 TREE_CHAIN (c
) = attrs
;
41039 if (processing_template_decl
)
41040 ATTR_IS_DEPENDENT (c
) = 1;
41045 gcc_assert (strcmp (kind
, "variant") == 0);
41046 attrs
= cp_finish_omp_declare_variant (parser
, pragma_tok
, attrs
);
41048 cp_parser_pop_lexer (parser
);
41051 data
->fndecl_seen
= true;
41057 # pragma omp declare target new-line
41058 declarations and definitions
41059 # pragma omp end declare target new-line
41062 # pragma omp declare target ( extended-list ) new-line
41064 # pragma omp declare target declare-target-clauses[seq] new-line */
41066 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
41067 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
41069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
41072 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
41074 tree clauses
= NULL_TREE
;
41075 int device_type
= 0;
41076 bool only_device_type
= true;
41077 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41079 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
41080 "#pragma omp declare target", pragma_tok
);
41081 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
41083 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
41085 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
41086 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41090 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41091 scope_chain
->omp_declare_target_attribute
++;
41094 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
41095 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
41096 device_type
|= OMP_CLAUSE_DEVICE_TYPE_KIND (c
);
41097 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
41099 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
41101 tree t
= OMP_CLAUSE_DECL (c
), id
;
41102 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
41103 tree at2
= lookup_attribute ("omp declare target link",
41104 DECL_ATTRIBUTES (t
));
41105 only_device_type
= false;
41106 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
41108 id
= get_identifier ("omp declare target link");
41109 std::swap (at1
, at2
);
41112 id
= get_identifier ("omp declare target");
41115 error_at (OMP_CLAUSE_LOCATION (c
),
41116 "%qD specified both in declare target %<link%> and %<to%>"
41122 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
41123 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
41126 symtab_node
*node
= symtab_node::get (t
);
41129 node
->offloadable
= 1;
41130 if (ENABLE_OFFLOADING
)
41132 g
->have_offload
= true;
41133 if (is_a
<varpool_node
*> (node
))
41134 vec_safe_push (offload_vars
, t
);
41138 if (TREE_CODE (t
) != FUNCTION_DECL
)
41140 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0)
41142 tree at3
= lookup_attribute ("omp declare target host",
41143 DECL_ATTRIBUTES (t
));
41144 if (at3
== NULL_TREE
)
41146 id
= get_identifier ("omp declare target host");
41147 DECL_ATTRIBUTES (t
)
41148 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
41151 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0)
41153 tree at3
= lookup_attribute ("omp declare target nohost",
41154 DECL_ATTRIBUTES (t
));
41155 if (at3
== NULL_TREE
)
41157 id
= get_identifier ("omp declare target nohost");
41158 DECL_ATTRIBUTES (t
)
41159 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
41163 if (device_type
&& only_device_type
)
41164 warning_at (OMP_CLAUSE_LOCATION (clauses
), 0,
41165 "directive with only %<device_type%> clauses ignored");
41169 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
41171 const char *p
= "";
41172 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41174 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41175 p
= IDENTIFIER_POINTER (id
);
41177 if (strcmp (p
, "declare") == 0)
41179 cp_lexer_consume_token (parser
->lexer
);
41181 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41183 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41184 p
= IDENTIFIER_POINTER (id
);
41186 if (strcmp (p
, "target") == 0)
41187 cp_lexer_consume_token (parser
->lexer
);
41190 cp_parser_error (parser
, "expected %<target%>");
41191 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41197 cp_parser_error (parser
, "expected %<declare%>");
41198 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41201 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41202 if (!scope_chain
->omp_declare_target_attribute
)
41203 error_at (pragma_tok
->location
,
41204 "%<#pragma omp end declare target%> without corresponding "
41205 "%<#pragma omp declare target%>");
41207 scope_chain
->omp_declare_target_attribute
--;
41210 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
41211 expression and optional initializer clause of
41212 #pragma omp declare reduction. We store the expression(s) as
41213 either 3, 6 or 7 special statements inside of the artificial function's
41214 body. The first two statements are DECL_EXPRs for the artificial
41215 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
41216 expression that uses those variables.
41217 If there was any INITIALIZER clause, this is followed by further statements,
41218 the fourth and fifth statements are DECL_EXPRs for the artificial
41219 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
41220 constructor variant (first token after open paren is not omp_priv),
41221 then the sixth statement is a statement with the function call expression
41222 that uses the OMP_PRIV and optionally OMP_ORIG variable.
41223 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
41224 to initialize the OMP_PRIV artificial variable and there is seventh
41225 statement, a DECL_EXPR of the OMP_PRIV statement again. */
41228 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
41230 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
41231 gcc_assert (TYPE_REF_P (type
));
41232 type
= TREE_TYPE (type
);
41233 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
41234 DECL_ARTIFICIAL (omp_out
) = 1;
41235 pushdecl (omp_out
);
41236 add_decl_expr (omp_out
);
41237 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
41238 DECL_ARTIFICIAL (omp_in
) = 1;
41240 add_decl_expr (omp_in
);
41242 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
41244 keep_next_level (true);
41245 tree block
= begin_omp_structured_block ();
41246 combiner
= cp_parser_expression (parser
);
41247 finish_expr_stmt (combiner
);
41248 block
= finish_omp_structured_block (block
);
41249 if (processing_template_decl
)
41250 block
= build_stmt (input_location
, EXPR_STMT
, block
);
41253 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
41256 const char *p
= "";
41257 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41259 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41260 p
= IDENTIFIER_POINTER (id
);
41263 if (strcmp (p
, "initializer") == 0)
41265 cp_lexer_consume_token (parser
->lexer
);
41266 matching_parens parens
;
41267 if (!parens
.require_open (parser
))
41271 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41273 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41274 p
= IDENTIFIER_POINTER (id
);
41277 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
41278 DECL_ARTIFICIAL (omp_priv
) = 1;
41279 pushdecl (omp_priv
);
41280 add_decl_expr (omp_priv
);
41281 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
41282 DECL_ARTIFICIAL (omp_orig
) = 1;
41283 pushdecl (omp_orig
);
41284 add_decl_expr (omp_orig
);
41286 keep_next_level (true);
41287 block
= begin_omp_structured_block ();
41290 if (strcmp (p
, "omp_priv") == 0)
41292 bool is_direct_init
, is_non_constant_init
;
41294 cp_lexer_consume_token (parser
->lexer
);
41295 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
41296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
41297 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
41298 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
41300 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
41301 == CPP_CLOSE_PAREN
))
41303 finish_omp_structured_block (block
);
41304 error ("invalid initializer clause");
41307 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
41308 &is_non_constant_init
);
41309 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
41310 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
41314 cp_parser_parse_tentatively (parser
);
41315 /* Don't create location wrapper nodes here. */
41316 auto_suppress_location_wrappers sentinel
;
41317 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
41318 /*check_dependency_p=*/true,
41319 /*template_p=*/NULL
,
41320 /*declarator_p=*/false,
41321 /*optional_p=*/false);
41322 vec
<tree
, va_gc
> *args
;
41323 if (fn_name
== error_mark_node
41324 || cp_parser_error_occurred (parser
)
41325 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
41326 || ((args
= cp_parser_parenthesized_expression_list
41327 (parser
, non_attr
, /*cast_p=*/false,
41328 /*allow_expansion_p=*/true,
41329 /*non_constant_p=*/NULL
)),
41330 cp_parser_error_occurred (parser
)))
41332 finish_omp_structured_block (block
);
41333 cp_parser_abort_tentative_parse (parser
);
41334 cp_parser_error (parser
, "expected id-expression (arguments)");
41339 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
41340 if (arg
== omp_priv
41341 || (TREE_CODE (arg
) == ADDR_EXPR
41342 && TREE_OPERAND (arg
, 0) == omp_priv
))
41344 cp_parser_abort_tentative_parse (parser
);
41345 if (arg
== NULL_TREE
)
41346 error ("one of the initializer call arguments should be %<omp_priv%>"
41347 " or %<&omp_priv%>");
41348 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
41350 finish_expr_stmt (initializer
);
41353 block
= finish_omp_structured_block (block
);
41354 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
41355 if (processing_template_decl
)
41356 block
= build_stmt (input_location
, EXPR_STMT
, block
);
41360 add_decl_expr (omp_orig
);
41362 if (!parens
.require_close (parser
))
41366 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
41367 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
41374 #pragma omp declare reduction (reduction-id : typename-list : expression) \
41375 initializer-clause[opt] new-line
41377 initializer-clause:
41378 initializer (omp_priv initializer)
41379 initializer (function-name (argument-list)) */
41382 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
41383 enum pragma_context
)
41385 auto_vec
<tree
> types
;
41386 enum tree_code reduc_code
= ERROR_MARK
;
41387 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
41389 cp_token
*first_token
;
41390 cp_token_cache
*cp
;
41394 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
41395 p
= obstack_alloc (&declarator_obstack
, 0);
41397 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
41400 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
41403 reduc_code
= PLUS_EXPR
;
41406 reduc_code
= MULT_EXPR
;
41409 reduc_code
= MINUS_EXPR
;
41412 reduc_code
= BIT_AND_EXPR
;
41415 reduc_code
= BIT_XOR_EXPR
;
41418 reduc_code
= BIT_IOR_EXPR
;
41421 reduc_code
= TRUTH_ANDIF_EXPR
;
41424 reduc_code
= TRUTH_ORIF_EXPR
;
41427 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
41430 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
41431 "%<|%>, %<&&%>, %<||%> or identifier");
41435 if (reduc_code
!= ERROR_MARK
)
41436 cp_lexer_consume_token (parser
->lexer
);
41438 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
41439 if (reduc_id
== error_mark_node
)
41442 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
41445 /* Types may not be defined in declare reduction type list. */
41446 const char *saved_message
;
41447 saved_message
= parser
->type_definition_forbidden_message
;
41448 parser
->type_definition_forbidden_message
41449 = G_("types may not be defined in declare reduction type list");
41450 bool saved_colon_corrects_to_scope_p
;
41451 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
41452 parser
->colon_corrects_to_scope_p
= false;
41453 bool saved_colon_doesnt_start_class_def_p
;
41454 saved_colon_doesnt_start_class_def_p
41455 = parser
->colon_doesnt_start_class_def_p
;
41456 parser
->colon_doesnt_start_class_def_p
= true;
41460 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41461 type
= cp_parser_type_id (parser
);
41462 if (type
== error_mark_node
)
41464 else if (ARITHMETIC_TYPE_P (type
)
41465 && (orig_reduc_id
== NULL_TREE
41466 || (TREE_CODE (type
) != COMPLEX_TYPE
41467 && (id_equal (orig_reduc_id
, "min")
41468 || id_equal (orig_reduc_id
, "max")))))
41469 error_at (loc
, "predeclared arithmetic type %qT in "
41470 "%<#pragma omp declare reduction%>", type
);
41471 else if (FUNC_OR_METHOD_TYPE_P (type
)
41472 || TREE_CODE (type
) == ARRAY_TYPE
)
41473 error_at (loc
, "function or array type %qT in "
41474 "%<#pragma omp declare reduction%>", type
);
41475 else if (TYPE_REF_P (type
))
41476 error_at (loc
, "reference type %qT in "
41477 "%<#pragma omp declare reduction%>", type
);
41478 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
41479 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
41480 "type %qT in %<#pragma omp declare reduction%>", type
);
41482 types
.safe_push (type
);
41484 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41485 cp_lexer_consume_token (parser
->lexer
);
41490 /* Restore the saved message. */
41491 parser
->type_definition_forbidden_message
= saved_message
;
41492 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
41493 parser
->colon_doesnt_start_class_def_p
41494 = saved_colon_doesnt_start_class_def_p
;
41496 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
41497 || types
.is_empty ())
41500 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41504 first_token
= cp_lexer_peek_token (parser
->lexer
);
41507 FOR_EACH_VEC_ELT (types
, i
, type
)
41510 = build_function_type_list (void_type_node
,
41511 cp_build_reference_type (type
, false),
41513 tree this_reduc_id
= reduc_id
;
41514 if (!dependent_type_p (type
))
41515 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
41516 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
41517 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
41518 DECL_ARTIFICIAL (fndecl
) = 1;
41519 DECL_EXTERNAL (fndecl
) = 1;
41520 DECL_DECLARED_INLINE_P (fndecl
) = 1;
41521 DECL_IGNORED_P (fndecl
) = 1;
41522 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
41523 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
41524 DECL_ATTRIBUTES (fndecl
)
41525 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
41526 DECL_ATTRIBUTES (fndecl
));
41527 if (processing_template_decl
)
41528 fndecl
= push_template_decl (fndecl
);
41529 bool block_scope
= false;
41530 tree block
= NULL_TREE
;
41531 if (current_function_decl
)
41533 block_scope
= true;
41534 DECL_CONTEXT (fndecl
) = global_namespace
;
41535 if (!processing_template_decl
)
41538 else if (current_class_type
)
41542 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
41543 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
41544 cp_lexer_consume_token (parser
->lexer
);
41545 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
41547 cp
= cp_token_cache_new (first_token
,
41548 cp_lexer_peek_nth_token (parser
->lexer
,
41551 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
41552 finish_member_declaration (fndecl
);
41553 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
41554 DECL_PENDING_INLINE_P (fndecl
) = 1;
41555 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
41560 DECL_CONTEXT (fndecl
) = current_namespace
;
41564 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
41566 block
= begin_omp_structured_block ();
41569 cp_parser_push_lexer_for_tokens (parser
, cp
);
41570 parser
->lexer
->in_pragma
= true;
41572 if (!cp_parser_omp_declare_reduction_exprs (fndecl
, parser
))
41575 finish_function (/*inline_p=*/false);
41577 DECL_CONTEXT (fndecl
) = current_function_decl
;
41579 cp_parser_pop_lexer (parser
);
41583 cp_parser_pop_lexer (parser
);
41585 finish_function (/*inline_p=*/false);
41588 DECL_CONTEXT (fndecl
) = current_function_decl
;
41589 block
= finish_omp_structured_block (block
);
41590 if (TREE_CODE (block
) == BIND_EXPR
)
41591 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
41592 else if (TREE_CODE (block
) == STATEMENT_LIST
)
41593 DECL_SAVED_TREE (fndecl
) = block
;
41594 if (processing_template_decl
)
41595 add_decl_expr (fndecl
);
41597 cp_check_omp_declare_reduction (fndecl
);
41598 if (cp
== NULL
&& types
.length () > 1)
41599 cp
= cp_token_cache_new (first_token
,
41600 cp_lexer_peek_nth_token (parser
->lexer
, 2));
41601 if (errs
!= errorcount
)
41605 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41608 /* Free any declarators allocated. */
41609 obstack_free (&declarator_obstack
, p
);
41613 #pragma omp declare simd declare-simd-clauses[optseq] new-line
41614 #pragma omp declare reduction (reduction-id : typename-list : expression) \
41615 initializer-clause[opt] new-line
41616 #pragma omp declare target new-line
41619 #pragma omp declare variant (identifier) match (context-selector) */
41622 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
41623 enum pragma_context context
)
41625 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41627 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41628 const char *p
= IDENTIFIER_POINTER (id
);
41630 if (strcmp (p
, "simd") == 0)
41632 cp_lexer_consume_token (parser
->lexer
);
41633 cp_parser_omp_declare_simd (parser
, pragma_tok
,
41637 if (flag_openmp
&& strcmp (p
, "variant") == 0)
41639 cp_lexer_consume_token (parser
->lexer
);
41640 cp_parser_omp_declare_simd (parser
, pragma_tok
,
41644 cp_ensure_no_omp_declare_simd (parser
);
41645 if (strcmp (p
, "reduction") == 0)
41647 cp_lexer_consume_token (parser
->lexer
);
41648 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
41652 if (!flag_openmp
) /* flag_openmp_simd */
41654 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41657 if (strcmp (p
, "target") == 0)
41659 cp_lexer_consume_token (parser
->lexer
);
41660 cp_parser_omp_declare_target (parser
, pragma_tok
);
41664 cp_parser_error (parser
, "expected %<simd%>, %<reduction%>, "
41665 "%<target%> or %<variant%>");
41666 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41671 #pragma omp requires clauses[optseq] new-line */
41674 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
41677 enum omp_requires new_req
= (enum omp_requires
) 0;
41679 location_t loc
= pragma_tok
->location
;
41680 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
41682 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
41683 cp_lexer_consume_token (parser
->lexer
);
41687 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41689 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41690 const char *p
= IDENTIFIER_POINTER (id
);
41691 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41692 enum omp_requires this_req
= (enum omp_requires
) 0;
41694 if (!strcmp (p
, "unified_address"))
41695 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
41696 else if (!strcmp (p
, "unified_shared_memory"))
41697 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
41698 else if (!strcmp (p
, "dynamic_allocators"))
41699 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
41700 else if (!strcmp (p
, "reverse_offload"))
41701 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
41702 else if (!strcmp (p
, "atomic_default_mem_order"))
41704 cp_lexer_consume_token (parser
->lexer
);
41706 matching_parens parens
;
41707 if (parens
.require_open (parser
))
41709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41711 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41712 p
= IDENTIFIER_POINTER (id
);
41714 if (!strcmp (p
, "seq_cst"))
41716 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
41717 else if (!strcmp (p
, "relaxed"))
41719 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
41720 else if (!strcmp (p
, "acq_rel"))
41722 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
41726 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
41727 "expected %<seq_cst%>, %<relaxed%> or "
41729 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
41731 cp_lexer_consume_token (parser
->lexer
);
41734 cp_lexer_consume_token (parser
->lexer
);
41736 if (!parens
.require_close (parser
))
41737 cp_parser_skip_to_closing_parenthesis (parser
,
41738 /*recovering=*/true,
41739 /*or_comma=*/false,
41745 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41753 error_at (cloc
, "expected %<unified_address%>, "
41754 "%<unified_shared_memory%>, "
41755 "%<dynamic_allocators%>, "
41756 "%<reverse_offload%> "
41757 "or %<atomic_default_mem_order%> clause");
41758 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41762 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
41763 "supported yet", p
);
41765 cp_lexer_consume_token (parser
->lexer
);
41768 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
41770 if ((this_req
& new_req
) != 0)
41771 error_at (cloc
, "too many %qs clauses", p
);
41772 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
41773 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
41774 error_at (cloc
, "%qs clause used lexically after first "
41775 "target construct or offloading API", p
);
41777 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
41779 error_at (cloc
, "too many %qs clauses",
41780 "atomic_default_mem_order");
41781 this_req
= (enum omp_requires
) 0;
41783 else if ((omp_requires_mask
41784 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
41786 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
41787 " clause in a single compilation unit");
41789 = (enum omp_requires
)
41791 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
41793 else if ((omp_requires_mask
41794 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
41795 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
41796 "lexically after first %<atomic%> construct "
41797 "without memory order clause");
41798 new_req
= (enum omp_requires
) (new_req
| this_req
);
41800 = (enum omp_requires
) (omp_requires_mask
| this_req
);
41806 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41809 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
41815 #pragma omp taskloop taskloop-clause[optseq] new-line
41818 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
41821 #define OMP_TASKLOOP_CLAUSE_MASK \
41822 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
41828 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
41829 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
41830 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
41835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
41840 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
41841 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
41844 tree clauses
, sb
, ret
;
41846 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41848 strcat (p_name
, " taskloop");
41849 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
41850 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
41852 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
41853 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
41855 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41857 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41858 const char *p
= IDENTIFIER_POINTER (id
);
41860 if (strcmp (p
, "simd") == 0)
41862 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
41863 if (cclauses
== NULL
)
41864 cclauses
= cclauses_buf
;
41866 cp_lexer_consume_token (parser
->lexer
);
41867 if (!flag_openmp
) /* flag_openmp_simd */
41868 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
41870 sb
= begin_omp_structured_block ();
41871 save
= cp_parser_begin_omp_structured_block (parser
);
41872 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
41874 cp_parser_end_omp_structured_block (parser
, save
);
41875 tree body
= finish_omp_structured_block (sb
);
41878 ret
= make_node (OMP_TASKLOOP
);
41879 TREE_TYPE (ret
) = void_type_node
;
41880 OMP_FOR_BODY (ret
) = body
;
41881 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
41882 SET_EXPR_LOCATION (ret
, loc
);
41887 if (!flag_openmp
) /* flag_openmp_simd */
41889 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41893 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
41897 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
41898 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
41901 keep_next_level (true);
41902 sb
= begin_omp_structured_block ();
41903 save
= cp_parser_begin_omp_structured_block (parser
);
41905 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
41908 cp_parser_end_omp_structured_block (parser
, save
);
41909 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
41916 # pragma acc routine oacc-routine-clause[optseq] new-line
41917 function-definition
41919 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
41922 #define OACC_ROUTINE_CLAUSE_MASK \
41923 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
41924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
41925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
41926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
41929 /* Parse the OpenACC routine pragma. This has an optional '( name )'
41930 component, which must resolve to a declared namespace-scope
41931 function. The clauses are either processed directly (for a named
41932 function), or defered until the immediatley following declaration
41936 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
41937 enum pragma_context context
)
41939 gcc_checking_assert (context
== pragma_external
);
41940 /* The checking for "another pragma following this one" in the "no optional
41941 '( name )'" case makes sure that we dont re-enter. */
41942 gcc_checking_assert (parser
->oacc_routine
== NULL
);
41944 cp_oacc_routine_data data
;
41945 data
.error_seen
= false;
41946 data
.fndecl_seen
= false;
41947 data
.tokens
= vNULL
;
41948 data
.clauses
= NULL_TREE
;
41949 data
.loc
= pragma_tok
->location
;
41950 /* It is safe to take the address of a local variable; it will only be
41951 used while this scope is live. */
41952 parser
->oacc_routine
= &data
;
41954 /* Look for optional '( name )'. */
41955 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
41957 matching_parens parens
;
41958 parens
.consume_open (parser
); /* '(' */
41960 /* We parse the name as an id-expression. If it resolves to
41961 anything other than a non-overloaded function at namespace
41962 scope, it's an error. */
41963 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41964 tree name
= cp_parser_id_expression (parser
,
41965 /*template_keyword_p=*/false,
41966 /*check_dependency_p=*/false,
41967 /*template_p=*/NULL
,
41968 /*declarator_p=*/false,
41969 /*optional_p=*/false);
41970 tree decl
= (identifier_p (name
)
41971 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
41973 if (name
!= error_mark_node
&& decl
== error_mark_node
)
41974 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
41976 if (decl
== error_mark_node
41977 || !parens
.require_close (parser
))
41979 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41980 parser
->oacc_routine
= NULL
;
41985 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
41986 "#pragma acc routine",
41987 cp_lexer_peek_token (parser
->lexer
));
41988 /* The clauses are in reverse order; fix that to make later diagnostic
41989 emission easier. */
41990 data
.clauses
= nreverse (data
.clauses
);
41992 if (decl
&& is_overloaded_fn (decl
)
41993 && (TREE_CODE (decl
) != FUNCTION_DECL
41994 || DECL_FUNCTION_TEMPLATE_P (decl
)))
41996 error_at (name_loc
,
41997 "%<#pragma acc routine%> names a set of overloads");
41998 parser
->oacc_routine
= NULL
;
42002 /* Perhaps we should use the same rule as declarations in different
42004 if (!DECL_NAMESPACE_SCOPE_P (decl
))
42006 error_at (name_loc
,
42007 "%qD does not refer to a namespace scope function", decl
);
42008 parser
->oacc_routine
= NULL
;
42012 if (TREE_CODE (decl
) != FUNCTION_DECL
)
42014 error_at (name_loc
, "%qD does not refer to a function", decl
);
42015 parser
->oacc_routine
= NULL
;
42019 cp_finalize_oacc_routine (parser
, decl
, false);
42020 parser
->oacc_routine
= NULL
;
42022 else /* No optional '( name )'. */
42024 /* Store away all pragma tokens. */
42025 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
)
42026 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
42027 cp_lexer_consume_token (parser
->lexer
);
42028 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
42029 parser
->oacc_routine
->error_seen
= true;
42030 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42031 struct cp_token_cache
*cp
42032 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
42033 parser
->oacc_routine
->tokens
.safe_push (cp
);
42035 /* Emit a helpful diagnostic if there's another pragma following this
42037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
42039 cp_ensure_no_oacc_routine (parser
);
42040 data
.tokens
.release ();
42041 /* ..., and then just keep going. */
42045 /* We only have to consider the pragma_external case here. */
42046 cp_parser_declaration (parser
);
42047 if (parser
->oacc_routine
42048 && !parser
->oacc_routine
->fndecl_seen
)
42049 cp_ensure_no_oacc_routine (parser
);
42051 parser
->oacc_routine
= NULL
;
42052 data
.tokens
.release ();
42056 /* Finalize #pragma acc routine clauses after direct declarator has
42060 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
42062 struct cp_token_cache
*ce
;
42063 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
42065 if (!data
->error_seen
&& data
->fndecl_seen
)
42067 error_at (data
->loc
,
42068 "%<#pragma acc routine%> not immediately followed by "
42069 "a single function declaration or definition");
42070 data
->error_seen
= true;
42072 if (data
->error_seen
)
42075 gcc_checking_assert (data
->tokens
.length () == 1);
42076 ce
= data
->tokens
[0];
42078 cp_parser_push_lexer_for_tokens (parser
, ce
);
42079 parser
->lexer
->in_pragma
= true;
42080 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
42082 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
42083 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
42084 parser
->oacc_routine
->clauses
42085 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
42086 "#pragma acc routine", pragma_tok
);
42087 /* The clauses are in reverse order; fix that to make later diagnostic
42088 emission easier. */
42089 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
42090 cp_parser_pop_lexer (parser
);
42091 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
42097 /* Apply any saved OpenACC routine clauses to a just-parsed
42101 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
42103 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
42105 /* Keep going if we're in error reporting mode. */
42106 if (parser
->oacc_routine
->error_seen
42107 || fndecl
== error_mark_node
)
42110 if (parser
->oacc_routine
->fndecl_seen
)
42112 error_at (parser
->oacc_routine
->loc
,
42113 "%<#pragma acc routine%> not immediately followed by"
42114 " a single function declaration or definition");
42115 parser
->oacc_routine
= NULL
;
42118 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
42120 cp_ensure_no_oacc_routine (parser
);
42125 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
42126 parser
->oacc_routine
->loc
,
42127 "#pragma acc routine");
42128 if (compatible
< 0)
42130 parser
->oacc_routine
= NULL
;
42133 if (compatible
> 0)
42138 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
42140 error_at (parser
->oacc_routine
->loc
,
42142 ? G_("%<#pragma acc routine%> must be applied before"
42144 : G_("%<#pragma acc routine%> must be applied before"
42146 parser
->oacc_routine
= NULL
;
42150 /* Set the routine's level of parallelism. */
42151 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
42152 oacc_replace_fn_attrib (fndecl
, dims
);
42154 /* Add an "omp declare target" attribute. */
42155 DECL_ATTRIBUTES (fndecl
)
42156 = tree_cons (get_identifier ("omp declare target"),
42157 parser
->oacc_routine
->clauses
,
42158 DECL_ATTRIBUTES (fndecl
));
42161 /* Don't unset parser->oacc_routine here: we may still need it to
42162 diagnose wrong usage. But, remember that we've used this "#pragma acc
42164 parser
->oacc_routine
->fndecl_seen
= true;
42168 /* Main entry point to OpenMP statement pragmas. */
42171 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
42174 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
42175 omp_clause_mask
mask (0);
42177 switch (cp_parser_pragma_kind (pragma_tok
))
42179 case PRAGMA_OACC_ATOMIC
:
42180 cp_parser_omp_atomic (parser
, pragma_tok
);
42182 case PRAGMA_OACC_CACHE
:
42183 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
42185 case PRAGMA_OACC_DATA
:
42186 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
42188 case PRAGMA_OACC_ENTER_DATA
:
42189 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
42191 case PRAGMA_OACC_EXIT_DATA
:
42192 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
42194 case PRAGMA_OACC_HOST_DATA
:
42195 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
42197 case PRAGMA_OACC_KERNELS
:
42198 case PRAGMA_OACC_PARALLEL
:
42199 case PRAGMA_OACC_SERIAL
:
42200 strcpy (p_name
, "#pragma acc");
42201 stmt
= cp_parser_oacc_compute (parser
, pragma_tok
, p_name
, if_p
);
42203 case PRAGMA_OACC_LOOP
:
42204 strcpy (p_name
, "#pragma acc");
42205 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
42208 case PRAGMA_OACC_UPDATE
:
42209 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
42211 case PRAGMA_OACC_WAIT
:
42212 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
42214 case PRAGMA_OMP_ATOMIC
:
42215 cp_parser_omp_atomic (parser
, pragma_tok
);
42217 case PRAGMA_OMP_CRITICAL
:
42218 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
42220 case PRAGMA_OMP_DISTRIBUTE
:
42221 strcpy (p_name
, "#pragma omp");
42222 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
42225 case PRAGMA_OMP_FOR
:
42226 strcpy (p_name
, "#pragma omp");
42227 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
42230 case PRAGMA_OMP_LOOP
:
42231 strcpy (p_name
, "#pragma omp");
42232 stmt
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
42235 case PRAGMA_OMP_MASTER
:
42236 strcpy (p_name
, "#pragma omp");
42237 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
42240 case PRAGMA_OMP_PARALLEL
:
42241 strcpy (p_name
, "#pragma omp");
42242 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
42245 case PRAGMA_OMP_SECTIONS
:
42246 strcpy (p_name
, "#pragma omp");
42247 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
42249 case PRAGMA_OMP_SIMD
:
42250 strcpy (p_name
, "#pragma omp");
42251 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
42254 case PRAGMA_OMP_SINGLE
:
42255 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
42257 case PRAGMA_OMP_TASK
:
42258 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
42260 case PRAGMA_OMP_TASKGROUP
:
42261 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
42263 case PRAGMA_OMP_TASKLOOP
:
42264 strcpy (p_name
, "#pragma omp");
42265 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
42268 case PRAGMA_OMP_TEAMS
:
42269 strcpy (p_name
, "#pragma omp");
42270 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
42274 gcc_unreachable ();
42277 protected_set_expr_location (stmt
, pragma_tok
->location
);
42280 /* Transactional Memory parsing routines. */
42282 /* Parse a transaction attribute.
42288 We use this instead of cp_parser_attributes_opt for transactions to avoid
42289 the pedwarn in C++98 mode. */
42292 cp_parser_txn_attribute_opt (cp_parser
*parser
)
42295 tree attr_name
, attr
= NULL
;
42297 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
42298 return cp_parser_attributes_opt (parser
);
42300 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
42302 cp_lexer_consume_token (parser
->lexer
);
42303 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
42306 token
= cp_lexer_peek_token (parser
->lexer
);
42307 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
42309 token
= cp_lexer_consume_token (parser
->lexer
);
42311 attr_name
= (token
->type
== CPP_KEYWORD
42312 /* For keywords, use the canonical spelling,
42313 not the parsed identifier. */
42314 ? ridpointers
[(int) token
->keyword
]
42316 attr
= build_tree_list (attr_name
, NULL_TREE
);
42319 cp_parser_error (parser
, "expected identifier");
42321 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
42323 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
42327 /* Parse a __transaction_atomic or __transaction_relaxed statement.
42329 transaction-statement:
42330 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
42332 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
42336 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
42338 unsigned char old_in
= parser
->in_transaction
;
42339 unsigned char this_in
= 1, new_in
;
42340 enum rid keyword
= token
->keyword
;
42341 tree stmt
, attrs
, noex
;
42343 cp_lexer_consume_token (parser
->lexer
);
42345 if (keyword
== RID_TRANSACTION_RELAXED
42346 || keyword
== RID_SYNCHRONIZED
)
42347 this_in
|= TM_STMT_ATTR_RELAXED
;
42350 attrs
= cp_parser_txn_attribute_opt (parser
);
42352 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
42355 /* Parse a noexcept specification. */
42356 if (keyword
== RID_ATOMIC_NOEXCEPT
)
42357 noex
= boolean_true_node
;
42358 else if (keyword
== RID_ATOMIC_CANCEL
)
42360 /* cancel-and-throw is unimplemented. */
42361 sorry ("%<atomic_cancel%>");
42365 noex
= cp_parser_noexcept_specification_opt (parser
,
42366 CP_PARSER_FLAGS_NONE
,
42367 /*require_constexpr=*/true,
42368 /*consumed_expr=*/NULL
,
42369 /*return_cond=*/true);
42371 /* Keep track if we're in the lexical scope of an outer transaction. */
42372 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
42374 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
42376 parser
->in_transaction
= new_in
;
42377 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
42378 parser
->in_transaction
= old_in
;
42380 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
42385 /* Parse a __transaction_atomic or __transaction_relaxed expression.
42387 transaction-expression:
42388 __transaction_atomic txn-noexcept-spec[opt] ( expression )
42389 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
42393 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
42395 unsigned char old_in
= parser
->in_transaction
;
42396 unsigned char this_in
= 1;
42400 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42402 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
42403 || keyword
== RID_TRANSACTION_RELAXED
);
42407 keyword
== RID_TRANSACTION_RELAXED
42408 ? G_("%<__transaction_relaxed%> without transactional memory "
42410 : G_("%<__transaction_atomic%> without transactional memory "
42411 "support enabled"));
42413 token
= cp_parser_require_keyword (parser
, keyword
,
42414 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
42415 : RT_TRANSACTION_RELAXED
));
42416 gcc_assert (token
!= NULL
);
42418 if (keyword
== RID_TRANSACTION_RELAXED
)
42419 this_in
|= TM_STMT_ATTR_RELAXED
;
42421 /* Set this early. This might mean that we allow transaction_cancel in
42422 an expression that we find out later actually has to be a constexpr.
42423 However, we expect that cxx_constant_value will be able to deal with
42424 this; also, if the noexcept has no constexpr, then what we parse next
42425 really is a transaction's body. */
42426 parser
->in_transaction
= this_in
;
42428 /* Parse a noexcept specification. */
42429 noex
= cp_parser_noexcept_specification_opt (parser
,
42430 CP_PARSER_FLAGS_NONE
,
42431 /*require_constexpr=*/false,
42433 /*return_cond=*/true);
42435 if (!noex
|| !noex_expr
42436 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
42438 matching_parens parens
;
42439 parens
.require_open (parser
);
42441 expr
= cp_parser_expression (parser
);
42442 expr
= finish_parenthesized_expr (expr
);
42444 parens
.require_close (parser
);
42448 /* The only expression that is available got parsed for the noexcept
42449 already. noexcept is true then. */
42451 noex
= boolean_true_node
;
42454 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
42455 parser
->in_transaction
= old_in
;
42457 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
42458 return error_mark_node
;
42460 return (flag_tm
? expr
: error_mark_node
);
42463 /* Parse a function-transaction-block.
42465 function-transaction-block:
42466 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
42468 __transaction_atomic txn-attribute[opt] function-try-block
42469 __transaction_relaxed ctor-initializer[opt] function-body
42470 __transaction_relaxed function-try-block
42474 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
42476 unsigned char old_in
= parser
->in_transaction
;
42477 unsigned char new_in
= 1;
42478 tree compound_stmt
, stmt
, attrs
;
42481 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
42482 || keyword
== RID_TRANSACTION_RELAXED
);
42483 token
= cp_parser_require_keyword (parser
, keyword
,
42484 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
42485 : RT_TRANSACTION_RELAXED
));
42486 gcc_assert (token
!= NULL
);
42488 if (keyword
== RID_TRANSACTION_RELAXED
)
42489 new_in
|= TM_STMT_ATTR_RELAXED
;
42492 attrs
= cp_parser_txn_attribute_opt (parser
);
42494 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
42497 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
42499 parser
->in_transaction
= new_in
;
42501 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
42502 cp_parser_function_try_block (parser
);
42504 cp_parser_ctor_initializer_opt_and_function_body
42505 (parser
, /*in_function_try_block=*/false);
42507 parser
->in_transaction
= old_in
;
42509 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
42512 /* Parse a __transaction_cancel statement.
42515 __transaction_cancel txn-attribute[opt] ;
42516 __transaction_cancel txn-attribute[opt] throw-expression ;
42518 ??? Cancel and throw is not yet implemented. */
42521 cp_parser_transaction_cancel (cp_parser
*parser
)
42524 bool is_outer
= false;
42527 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
42528 RT_TRANSACTION_CANCEL
);
42529 gcc_assert (token
!= NULL
);
42531 attrs
= cp_parser_txn_attribute_opt (parser
);
42533 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
42535 /* ??? Parse cancel-and-throw here. */
42537 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
42541 error_at (token
->location
, "%<__transaction_cancel%> without "
42542 "transactional memory support enabled");
42543 return error_mark_node
;
42545 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
42547 error_at (token
->location
, "%<__transaction_cancel%> within a "
42548 "%<__transaction_relaxed%>");
42549 return error_mark_node
;
42553 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
42554 && !is_tm_may_cancel_outer (current_function_decl
))
42556 error_at (token
->location
, "outer %<__transaction_cancel%> not "
42557 "within outer %<__transaction_atomic%>");
42558 error_at (token
->location
,
42559 " or a %<transaction_may_cancel_outer%> function");
42560 return error_mark_node
;
42563 else if (parser
->in_transaction
== 0)
42565 error_at (token
->location
, "%<__transaction_cancel%> not within "
42566 "%<__transaction_atomic%>");
42567 return error_mark_node
;
42570 stmt
= build_tm_abort_call (token
->location
, is_outer
);
42578 static GTY (()) cp_parser
*the_parser
;
42581 /* Special handling for the first token or line in the file. The first
42582 thing in the file might be #pragma GCC pch_preprocess, which loads a
42583 PCH file, which is a GC collection point. So we need to handle this
42584 first pragma without benefit of an existing lexer structure.
42586 Always returns one token to the caller in *FIRST_TOKEN. This is
42587 either the true first token of the file, or the first token after
42588 the initial pragma. */
42591 cp_parser_initial_pragma (cp_token
*first_token
)
42595 cp_lexer_get_preprocessor_token (NULL
, first_token
);
42596 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
42598 c_common_no_more_pch ();
42602 cp_lexer_get_preprocessor_token (NULL
, first_token
);
42603 if (first_token
->type
== CPP_STRING
)
42605 name
= first_token
->u
.value
;
42607 cp_lexer_get_preprocessor_token (NULL
, first_token
);
42608 if (first_token
->type
!= CPP_PRAGMA_EOL
)
42609 error_at (first_token
->location
,
42610 "junk at end of %<#pragma GCC pch_preprocess%>");
42613 error_at (first_token
->location
, "expected string literal");
42615 /* Skip to the end of the pragma. */
42616 while (first_token
->type
!= CPP_PRAGMA_EOL
&& first_token
->type
!= CPP_EOF
)
42617 cp_lexer_get_preprocessor_token (NULL
, first_token
);
42619 /* Now actually load the PCH file. */
42621 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
42623 /* Read one more token to return to our caller. We have to do this
42624 after reading the PCH file in, since its pointers have to be
42626 cp_lexer_get_preprocessor_token (NULL
, first_token
);
42629 /* Parse a pragma GCC ivdep. */
42632 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
42634 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42638 /* Parse a pragma GCC unroll. */
42640 static unsigned short
42641 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
42643 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
42644 tree expr
= cp_parser_constant_expression (parser
);
42645 unsigned short unroll
;
42646 expr
= maybe_constant_value (expr
);
42647 HOST_WIDE_INT lunroll
= 0;
42648 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
42649 || TREE_CODE (expr
) != INTEGER_CST
42650 || (lunroll
= tree_to_shwi (expr
)) < 0
42651 || lunroll
>= USHRT_MAX
)
42653 error_at (location
, "%<#pragma GCC unroll%> requires an"
42654 " assignment-expression that evaluates to a non-negative"
42655 " integral constant less than %u", USHRT_MAX
);
42660 unroll
= (unsigned short)lunroll
;
42664 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42668 /* Normal parsing of a pragma token. Here we can (and must) use the
42672 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
42674 cp_token
*pragma_tok
;
42679 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
42680 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
42681 parser
->lexer
->in_pragma
= true;
42683 id
= cp_parser_pragma_kind (pragma_tok
);
42684 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
42685 cp_ensure_no_omp_declare_simd (parser
);
42688 case PRAGMA_GCC_PCH_PREPROCESS
:
42689 error_at (pragma_tok
->location
,
42690 "%<#pragma GCC pch_preprocess%> must be first");
42693 case PRAGMA_OMP_BARRIER
:
42696 case pragma_compound
:
42697 cp_parser_omp_barrier (parser
, pragma_tok
);
42700 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
42701 "used in compound statements", "omp barrier");
42708 case PRAGMA_OMP_DEPOBJ
:
42711 case pragma_compound
:
42712 cp_parser_omp_depobj (parser
, pragma_tok
);
42715 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
42716 "used in compound statements", "omp depobj");
42723 case PRAGMA_OMP_FLUSH
:
42726 case pragma_compound
:
42727 cp_parser_omp_flush (parser
, pragma_tok
);
42730 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
42731 "used in compound statements", "omp flush");
42738 case PRAGMA_OMP_TASKWAIT
:
42741 case pragma_compound
:
42742 cp_parser_omp_taskwait (parser
, pragma_tok
);
42745 error_at (pragma_tok
->location
,
42746 "%<#pragma %s%> may only be used in compound statements",
42754 case PRAGMA_OMP_TASKYIELD
:
42757 case pragma_compound
:
42758 cp_parser_omp_taskyield (parser
, pragma_tok
);
42761 error_at (pragma_tok
->location
,
42762 "%<#pragma %s%> may only be used in compound statements",
42770 case PRAGMA_OMP_CANCEL
:
42773 case pragma_compound
:
42774 cp_parser_omp_cancel (parser
, pragma_tok
);
42777 error_at (pragma_tok
->location
,
42778 "%<#pragma %s%> may only be used in compound statements",
42786 case PRAGMA_OMP_CANCELLATION_POINT
:
42787 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
42790 case PRAGMA_OMP_THREADPRIVATE
:
42791 cp_parser_omp_threadprivate (parser
, pragma_tok
);
42794 case PRAGMA_OMP_DECLARE
:
42795 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
42797 case PRAGMA_OACC_DECLARE
:
42798 cp_parser_oacc_declare (parser
, pragma_tok
);
42801 case PRAGMA_OACC_ENTER_DATA
:
42802 if (context
== pragma_stmt
)
42804 error_at (pragma_tok
->location
,
42805 "%<#pragma %s%> may only be used in compound statements",
42809 else if (context
!= pragma_compound
)
42811 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
42814 case PRAGMA_OACC_EXIT_DATA
:
42815 if (context
== pragma_stmt
)
42817 error_at (pragma_tok
->location
,
42818 "%<#pragma %s%> may only be used in compound statements",
42822 else if (context
!= pragma_compound
)
42824 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
42827 case PRAGMA_OACC_ROUTINE
:
42828 if (context
!= pragma_external
)
42830 error_at (pragma_tok
->location
,
42831 "%<#pragma acc routine%> must be at file scope");
42834 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
42837 case PRAGMA_OACC_UPDATE
:
42838 if (context
== pragma_stmt
)
42840 error_at (pragma_tok
->location
,
42841 "%<#pragma %s%> may only be used in compound statements",
42845 else if (context
!= pragma_compound
)
42847 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
42850 case PRAGMA_OACC_WAIT
:
42851 if (context
== pragma_stmt
)
42853 error_at (pragma_tok
->location
,
42854 "%<#pragma %s%> may only be used in compound statements",
42858 else if (context
!= pragma_compound
)
42860 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
42863 case PRAGMA_OACC_ATOMIC
:
42864 case PRAGMA_OACC_CACHE
:
42865 case PRAGMA_OACC_DATA
:
42866 case PRAGMA_OACC_HOST_DATA
:
42867 case PRAGMA_OACC_KERNELS
:
42868 case PRAGMA_OACC_LOOP
:
42869 case PRAGMA_OACC_PARALLEL
:
42870 case PRAGMA_OACC_SERIAL
:
42871 case PRAGMA_OMP_ATOMIC
:
42872 case PRAGMA_OMP_CRITICAL
:
42873 case PRAGMA_OMP_DISTRIBUTE
:
42874 case PRAGMA_OMP_FOR
:
42875 case PRAGMA_OMP_LOOP
:
42876 case PRAGMA_OMP_MASTER
:
42877 case PRAGMA_OMP_PARALLEL
:
42878 case PRAGMA_OMP_SECTIONS
:
42879 case PRAGMA_OMP_SIMD
:
42880 case PRAGMA_OMP_SINGLE
:
42881 case PRAGMA_OMP_TASK
:
42882 case PRAGMA_OMP_TASKGROUP
:
42883 case PRAGMA_OMP_TASKLOOP
:
42884 case PRAGMA_OMP_TEAMS
:
42885 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
42887 stmt
= push_omp_privatization_clauses (false);
42888 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
42889 pop_omp_privatization_clauses (stmt
);
42892 case PRAGMA_OMP_REQUIRES
:
42893 return cp_parser_omp_requires (parser
, pragma_tok
);
42895 case PRAGMA_OMP_ORDERED
:
42896 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
42898 stmt
= push_omp_privatization_clauses (false);
42899 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
42900 pop_omp_privatization_clauses (stmt
);
42903 case PRAGMA_OMP_TARGET
:
42904 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
42906 stmt
= push_omp_privatization_clauses (false);
42907 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
42908 pop_omp_privatization_clauses (stmt
);
42911 case PRAGMA_OMP_END_DECLARE_TARGET
:
42912 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
42915 case PRAGMA_OMP_SCAN
:
42916 error_at (pragma_tok
->location
,
42917 "%<#pragma omp scan%> may only be used in "
42918 "a loop construct with %<inscan%> %<reduction%> clause");
42921 case PRAGMA_OMP_SECTION
:
42922 error_at (pragma_tok
->location
,
42923 "%<#pragma omp section%> may only be used in "
42924 "%<#pragma omp sections%> construct");
42929 if (context
== pragma_external
)
42931 error_at (pragma_tok
->location
,
42932 "%<#pragma GCC ivdep%> must be inside a function");
42935 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
42936 unsigned short unroll
;
42937 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
42938 if (tok
->type
== CPP_PRAGMA
42939 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
42941 tok
= cp_lexer_consume_token (parser
->lexer
);
42942 unroll
= cp_parser_pragma_unroll (parser
, tok
);
42943 tok
= cp_lexer_peek_token (the_parser
->lexer
);
42947 if (tok
->type
!= CPP_KEYWORD
42948 || (tok
->keyword
!= RID_FOR
42949 && tok
->keyword
!= RID_WHILE
42950 && tok
->keyword
!= RID_DO
))
42952 cp_parser_error (parser
, "for, while or do statement expected");
42955 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
42959 case PRAGMA_UNROLL
:
42961 if (context
== pragma_external
)
42963 error_at (pragma_tok
->location
,
42964 "%<#pragma GCC unroll%> must be inside a function");
42967 const unsigned short unroll
42968 = cp_parser_pragma_unroll (parser
, pragma_tok
);
42970 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
42971 if (tok
->type
== CPP_PRAGMA
42972 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
42974 tok
= cp_lexer_consume_token (parser
->lexer
);
42975 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
42976 tok
= cp_lexer_peek_token (the_parser
->lexer
);
42980 if (tok
->type
!= CPP_KEYWORD
42981 || (tok
->keyword
!= RID_FOR
42982 && tok
->keyword
!= RID_WHILE
42983 && tok
->keyword
!= RID_DO
))
42985 cp_parser_error (parser
, "for, while or do statement expected");
42988 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
42993 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
42994 c_invoke_pragma_handler (id
);
42998 cp_parser_error (parser
, "expected declaration specifiers");
43002 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43006 /* The interface the pragma parsers have to the lexer. */
43009 pragma_lex (tree
*value
, location_t
*loc
)
43011 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
43012 enum cpp_ttype ret
= tok
->type
;
43014 *value
= tok
->u
.value
;
43016 *loc
= tok
->location
;
43018 if (ret
== CPP_PRAGMA_EOL
|| ret
== CPP_EOF
)
43020 else if (ret
== CPP_STRING
)
43021 *value
= cp_parser_string_literal (the_parser
, false, false);
43024 if (ret
== CPP_KEYWORD
)
43026 cp_lexer_consume_token (the_parser
->lexer
);
43033 /* External interface. */
43035 /* Parse one entire translation unit. */
43038 c_parse_file (void)
43040 static bool already_called
= false;
43042 if (already_called
)
43043 fatal_error (input_location
,
43044 "inter-module optimizations not implemented for C++");
43045 already_called
= true;
43047 the_parser
= cp_parser_new ();
43048 push_deferring_access_checks (flag_access_control
43049 ? dk_no_deferred
: dk_no_check
);
43050 cp_parser_translation_unit (the_parser
);
43053 finish_translation_unit ();
43056 /* Create an identifier for a generic parameter type (a synthesized
43057 template parameter implied by `auto' or a concept identifier). */
43059 static GTY(()) int generic_parm_count
;
43061 make_generic_type_name ()
43064 sprintf (buf
, "auto:%d", ++generic_parm_count
);
43065 return get_identifier (buf
);
43068 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
43069 (creating a new template parameter list if necessary). Returns the newly
43070 created template type parm. */
43073 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
43075 /* A requires-clause is not a function and cannot have placeholders. */
43076 if (current_binding_level
->kind
== sk_block
)
43078 error ("placeholder type not allowed in this context");
43079 return error_mark_node
;
43082 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
43084 /* We are either continuing a function template that already contains implicit
43085 template parameters, creating a new fully-implicit function template, or
43086 extending an existing explicit function template with implicit template
43089 cp_binding_level
*const entry_scope
= current_binding_level
;
43091 bool become_template
= false;
43092 cp_binding_level
*parent_scope
= 0;
43094 if (parser
->implicit_template_scope
)
43096 gcc_assert (parser
->implicit_template_parms
);
43098 current_binding_level
= parser
->implicit_template_scope
;
43102 /* Roll back to the existing template parameter scope (in the case of
43103 extending an explicit function template) or introduce a new template
43104 parameter scope ahead of the function parameter scope (or class scope
43105 in the case of out-of-line member definitions). The function scope is
43106 added back after template parameter synthesis below. */
43108 cp_binding_level
*scope
= entry_scope
;
43110 while (scope
->kind
== sk_function_parms
)
43112 parent_scope
= scope
;
43113 scope
= scope
->level_chain
;
43115 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
43117 /* If not defining a class, then any class scope is a scope level in
43118 an out-of-line member definition. In this case simply wind back
43119 beyond the first such scope to inject the template parameter list.
43120 Otherwise wind back to the class being defined. The latter can
43121 occur in class member friend declarations such as:
43127 friend void A::foo (auto);
43130 The template parameter list synthesized for the friend declaration
43131 must be injected in the scope of 'B'. This can also occur in
43132 erroneous cases such as:
43138 void B::foo (auto) {}
43141 Here the attempted definition of 'B::foo' within 'A' is ill-formed
43142 but, nevertheless, the template parameter list synthesized for the
43143 declarator should be injected into the scope of 'A' as if the
43144 ill-formed template was specified explicitly. */
43146 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
43148 parent_scope
= scope
;
43149 scope
= scope
->level_chain
;
43153 current_binding_level
= scope
;
43155 if (scope
->kind
!= sk_template_parms
43156 || !function_being_declared_is_template_p (parser
))
43158 /* Introduce a new template parameter list for implicit template
43161 become_template
= true;
43163 parser
->implicit_template_scope
43164 = begin_scope (sk_template_parms
, NULL
);
43166 ++processing_template_decl
;
43168 parser
->fully_implicit_function_template_p
= true;
43169 ++parser
->num_template_parameter_lists
;
43173 /* Synthesize implicit template parameters at the end of the explicit
43174 template parameter list. */
43176 gcc_assert (current_template_parms
);
43178 parser
->implicit_template_scope
= scope
;
43180 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
43181 parser
->implicit_template_parms
43182 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
43186 /* Synthesize a new template parameter and track the current template
43187 parameter chain with implicit_template_parms. */
43189 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
43190 tree synth_id
= make_generic_type_name ();
43191 tree synth_tmpl_parm
;
43192 bool non_type
= false;
43194 /* Synthesize the type template parameter. */
43195 gcc_assert(!proto
|| TREE_CODE (proto
) == TYPE_DECL
);
43196 synth_tmpl_parm
= finish_template_type_parm (class_type_node
, synth_id
);
43198 /* Attach the constraint to the parm before processing. */
43199 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
43200 TREE_TYPE (node
) = constr
;
43202 = process_template_parm (parser
->implicit_template_parms
,
43205 /*non_type=*/non_type
,
43206 /*param_pack=*/false);
43208 /* Mark the synthetic declaration "virtual". This is used when
43209 comparing template-heads to determine if whether an abbreviated
43210 function template is equivalent to an explicit template.
43212 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
43213 DECL_VIRTUAL_P (TREE_VALUE (new_parm
)) = true;
43215 // Chain the new parameter to the list of implicit parameters.
43216 if (parser
->implicit_template_parms
)
43217 parser
->implicit_template_parms
43218 = TREE_CHAIN (parser
->implicit_template_parms
);
43220 parser
->implicit_template_parms
= new_parm
;
43222 tree new_decl
= get_local_decls ();
43224 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
43225 new_decl
= DECL_INITIAL (new_decl
);
43227 /* If creating a fully implicit function template, start the new implicit
43228 template parameter list with this synthesized type, otherwise grow the
43229 current template parameter list. */
43231 if (become_template
)
43233 parent_scope
->level_chain
= current_binding_level
;
43235 tree new_parms
= make_tree_vec (1);
43236 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
43237 current_template_parms
= tree_cons (size_int (processing_template_decl
),
43238 new_parms
, current_template_parms
);
43242 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
43243 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
43244 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
43245 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
43248 /* If the new parameter was constrained, we need to add that to the
43249 constraints in the template parameter list. */
43250 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
43252 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
43253 reqs
= combine_constraint_expressions (reqs
, req
);
43254 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
43257 current_binding_level
= entry_scope
;
43262 /* Finish the declaration of a fully implicit function template. Such a
43263 template has no explicit template parameter list so has not been through the
43264 normal template head and tail processing. synthesize_implicit_template_parm
43265 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
43266 provided if the declaration is a class member such that its template
43267 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
43268 form is returned. Otherwise NULL_TREE is returned. */
43271 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
43273 gcc_assert (parser
->fully_implicit_function_template_p
);
43275 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
43276 && DECL_VIRTUAL_P (member_decl_opt
))
43278 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
43279 "implicit templates may not be %<virtual%>");
43280 DECL_VIRTUAL_P (member_decl_opt
) = false;
43283 if (member_decl_opt
)
43284 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
43285 end_template_decl ();
43287 parser
->fully_implicit_function_template_p
= false;
43288 parser
->implicit_template_parms
= 0;
43289 parser
->implicit_template_scope
= 0;
43290 --parser
->num_template_parameter_lists
;
43292 return member_decl_opt
;
43295 /* Like finish_fully_implicit_template, but to be used in error
43296 recovery, rearranging scopes so that we restore the state we had
43297 before synthesize_implicit_template_parm inserted the implement
43298 template parms scope. */
43301 abort_fully_implicit_template (cp_parser
*parser
)
43303 cp_binding_level
*return_to_scope
= current_binding_level
;
43305 if (parser
->implicit_template_scope
43306 && return_to_scope
!= parser
->implicit_template_scope
)
43308 cp_binding_level
*child
= return_to_scope
;
43309 for (cp_binding_level
*scope
= child
->level_chain
;
43310 scope
!= parser
->implicit_template_scope
;
43311 scope
= child
->level_chain
)
43313 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
43314 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
43315 current_binding_level
= parser
->implicit_template_scope
;
43318 return_to_scope
= return_to_scope
->level_chain
;
43320 finish_fully_implicit_template (parser
, NULL
);
43322 gcc_assert (current_binding_level
== return_to_scope
);
43325 /* Helper function for diagnostics that have complained about things
43326 being used with 'extern "C"' linkage.
43328 Attempt to issue a note showing where the 'extern "C"' linkage began. */
43331 maybe_show_extern_c_location (void)
43333 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
43334 inform (the_parser
->innermost_linkage_specification_location
,
43335 "%<extern \"C\"%> linkage started here");
43338 #include "gt-cp-parser.h"