2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
22 #define INCLUDE_UNIQUE_PTR
24 #include "coretypes.h"
26 #include "c-family/c-common.h"
28 #include "stringpool.h"
30 #include "print-tree.h"
32 #include "trans-mem.h"
35 #include "c-family/c-objc.h"
37 #include "tree-pretty-print.h"
39 #include "gomp-constants.h"
40 #include "omp-general.h"
41 #include "omp-offload.h"
42 #include "c-family/c-indentation.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
46 #include "cp-name-hint.h"
48 #include "c-family/known-headers.h"
53 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
54 and c-lex.c) and the C++ parser. */
56 /* The various kinds of non integral constant we encounter. */
57 enum non_integral_constant
{
59 /* floating-point literal */
63 /* %<__FUNCTION__%> */
65 /* %<__PRETTY_FUNCTION__%> */
73 /* %<typeid%> operator */
75 /* non-constant compound literals */
83 /* an array reference */
89 /* the address of a label */
103 /* calls to overloaded operators */
107 /* a comma operator */
109 /* a call to a constructor */
111 /* a transaction expression */
115 /* The various kinds of errors about name-lookup failing. */
116 enum name_lookup_error
{
121 /* is not a class or namespace */
123 /* is not a class, namespace, or enumeration */
127 /* The various kinds of required token */
128 enum required_token
{
130 RT_SEMICOLON
, /* ';' */
131 RT_OPEN_PAREN
, /* '(' */
132 RT_CLOSE_BRACE
, /* '}' */
133 RT_OPEN_BRACE
, /* '{' */
134 RT_CLOSE_SQUARE
, /* ']' */
135 RT_OPEN_SQUARE
, /* '[' */
139 RT_GREATER
, /* '>' */
141 RT_ELLIPSIS
, /* '...' */
145 RT_COLON_SCOPE
, /* ':' or '::' */
146 RT_CLOSE_PAREN
, /* ')' */
147 RT_COMMA_CLOSE_PAREN
, /* ',' or ')' */
148 RT_PRAGMA_EOL
, /* end of line */
149 RT_NAME
, /* identifier */
151 /* The type is CPP_KEYWORD */
153 RT_DELETE
, /* delete */
154 RT_RETURN
, /* return */
155 RT_WHILE
, /* while */
156 RT_EXTERN
, /* extern */
157 RT_STATIC_ASSERT
, /* static_assert */
158 RT_DECLTYPE
, /* decltype */
159 RT_OPERATOR
, /* operator */
160 RT_CLASS
, /* class */
161 RT_TEMPLATE
, /* template */
162 RT_NAMESPACE
, /* namespace */
163 RT_USING
, /* using */
166 RT_CATCH
, /* catch */
167 RT_THROW
, /* throw */
169 RT_LABEL
, /* __label__ */
170 RT_AT_TRY
, /* @try */
171 RT_AT_SYNCHRONIZED
, /* @synchronized */
172 RT_AT_THROW
, /* @throw */
174 RT_SELECT
, /* selection-statement */
175 RT_ITERATION
, /* iteration-statement */
176 RT_JUMP
, /* jump-statement */
177 RT_CLASS_KEY
, /* class-key */
178 RT_CLASS_TYPENAME_TEMPLATE
, /* class, typename, or template */
179 RT_TRANSACTION_ATOMIC
, /* __transaction_atomic */
180 RT_TRANSACTION_RELAXED
, /* __transaction_relaxed */
181 RT_TRANSACTION_CANCEL
, /* __transaction_cancel */
183 RT_CO_YIELD
/* co_yield */
186 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
187 reverting it on destruction. */
189 class type_id_in_expr_sentinel
194 type_id_in_expr_sentinel (cp_parser
*parser
, bool set
= true)
196 saved (parser
->in_type_id_in_expr_p
)
197 { parser
->in_type_id_in_expr_p
= set
; }
198 ~type_id_in_expr_sentinel ()
199 { parser
->in_type_id_in_expr_p
= saved
; }
204 static cp_lexer
*cp_lexer_new_main
206 static cp_lexer
*cp_lexer_new_from_tokens
207 (cp_token_cache
*tokens
);
208 static void cp_lexer_destroy
210 static int cp_lexer_saving_tokens
212 static cp_token
*cp_lexer_token_at
213 (cp_lexer
*, cp_token_position
);
214 static void cp_lexer_get_preprocessor_token
215 (unsigned, cp_token
*);
216 static inline cp_token
*cp_lexer_peek_token
218 static cp_token
*cp_lexer_peek_nth_token
219 (cp_lexer
*, size_t);
220 static inline bool cp_lexer_next_token_is
221 (cp_lexer
*, enum cpp_ttype
);
222 static bool cp_lexer_next_token_is_not
223 (cp_lexer
*, enum cpp_ttype
);
224 static bool cp_lexer_next_token_is_keyword
225 (cp_lexer
*, enum rid
);
226 static cp_token
*cp_lexer_consume_token
228 static void cp_lexer_purge_token
230 static void cp_lexer_purge_tokens_after
231 (cp_lexer
*, cp_token_position
);
232 static void cp_lexer_save_tokens
234 static void cp_lexer_commit_tokens
236 static void cp_lexer_rollback_tokens
238 static void cp_lexer_print_token
239 (FILE *, cp_token
*);
240 static inline bool cp_lexer_debugging_p
242 static void cp_lexer_start_debugging
243 (cp_lexer
*) ATTRIBUTE_UNUSED
;
244 static void cp_lexer_stop_debugging
245 (cp_lexer
*) ATTRIBUTE_UNUSED
;
247 static cp_token_cache
*cp_token_cache_new
248 (cp_token
*, cp_token
*);
249 static tree cp_parser_late_noexcept_specifier
251 static void noexcept_override_late_checks
254 static void cp_parser_initial_pragma
257 static bool cp_parser_omp_declare_reduction_exprs
259 static void cp_finalize_oacc_routine
260 (cp_parser
*, tree
, bool);
262 /* Manifest constants. */
263 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
264 #define CP_SAVED_TOKEN_STACK 5
268 /* The stream to which debugging output should be written. */
269 static FILE *cp_lexer_debug_stream
;
271 /* Nonzero if we are parsing an unevaluated operand: an operand to
272 sizeof, typeof, or alignof. */
273 int cp_unevaluated_operand
;
275 /* Dump up to NUM tokens in BUFFER to FILE starting with token
276 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
277 first token in BUFFER. If NUM is 0, dump all the tokens. If
278 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
279 highlighted by surrounding it in [[ ]]. */
282 cp_lexer_dump_tokens (FILE *file
, vec
<cp_token
, va_gc
> *buffer
,
283 cp_token
*start_token
, unsigned num
,
284 cp_token
*curr_token
)
286 unsigned i
, nprinted
;
290 fprintf (file
, "%u tokens\n", vec_safe_length (buffer
));
296 num
= buffer
->length ();
298 if (start_token
== NULL
)
299 start_token
= buffer
->address ();
301 if (start_token
> buffer
->address ())
303 cp_lexer_print_token (file
, &(*buffer
)[0]);
304 fprintf (file
, " ... ");
309 for (i
= 0; buffer
->iterate (i
, &token
) && nprinted
< num
; i
++)
311 if (token
== start_token
)
318 if (token
== curr_token
)
319 fprintf (file
, "[[");
321 cp_lexer_print_token (file
, token
);
323 if (token
== curr_token
)
324 fprintf (file
, "]]");
330 case CPP_CLOSE_BRACE
:
340 if (i
== num
&& i
< buffer
->length ())
342 fprintf (file
, " ... ");
343 cp_lexer_print_token (file
, &buffer
->last ());
346 fprintf (file
, "\n");
350 /* Dump all tokens in BUFFER to stderr. */
353 cp_lexer_debug_tokens (vec
<cp_token
, va_gc
> *buffer
)
355 cp_lexer_dump_tokens (stderr
, buffer
, NULL
, 0, NULL
);
359 debug (vec
<cp_token
, va_gc
> &ref
)
361 cp_lexer_dump_tokens (stderr
, &ref
, NULL
, 0, NULL
);
365 debug (vec
<cp_token
, va_gc
> *ptr
)
370 fprintf (stderr
, "<nil>\n");
374 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
375 description for T. */
378 cp_debug_print_tree_if_set (FILE *file
, const char *desc
, tree t
)
382 fprintf (file
, "%s: ", desc
);
383 print_node_brief (file
, "", t
, 0);
388 /* Dump parser context C to FILE. */
391 cp_debug_print_context (FILE *file
, cp_parser_context
*c
)
393 const char *status_s
[] = { "OK", "ERROR", "COMMITTED" };
394 fprintf (file
, "{ status = %s, scope = ", status_s
[c
->status
]);
395 print_node_brief (file
, "", c
->object_type
, 0);
396 fprintf (file
, "}\n");
400 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 cp_debug_print_context_stack (FILE *file
, cp_parser_context
*first
)
406 cp_parser_context
*c
;
408 fprintf (file
, "Parsing context stack:\n");
409 for (i
= 0, c
= first
; c
; c
= c
->next
, i
++)
411 fprintf (file
, "\t#%u: ", i
);
412 cp_debug_print_context (file
, c
);
417 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 cp_debug_print_flag (FILE *file
, const char *desc
, bool flag
)
423 fprintf (file
, "%s: true\n", desc
);
427 /* Print an unparsed function entry UF to FILE. */
430 cp_debug_print_unparsed_function (FILE *file
, cp_unparsed_functions_entry
*uf
)
433 cp_default_arg_entry
*default_arg_fn
;
436 fprintf (file
, "\tFunctions with default args:\n");
438 vec_safe_iterate (uf
->funs_with_default_args
, i
, &default_arg_fn
);
441 fprintf (file
, "\t\tClass type: ");
442 print_node_brief (file
, "", default_arg_fn
->class_type
, 0);
443 fprintf (file
, "\t\tDeclaration: ");
444 print_node_brief (file
, "", default_arg_fn
->decl
, 0);
445 fprintf (file
, "\n");
448 fprintf (file
, "\n\tFunctions with definitions that require "
449 "post-processing\n\t\t");
450 for (i
= 0; vec_safe_iterate (uf
->funs_with_definitions
, i
, &fn
); i
++)
452 print_node_brief (file
, "", fn
, 0);
455 fprintf (file
, "\n");
457 fprintf (file
, "\n\tNon-static data members with initializers that require "
458 "post-processing\n\t\t");
459 for (i
= 0; vec_safe_iterate (uf
->nsdmis
, i
, &fn
); i
++)
461 print_node_brief (file
, "", fn
, 0);
464 fprintf (file
, "\n");
468 /* Print the stack of unparsed member functions S to FILE. */
471 cp_debug_print_unparsed_queues (FILE *file
,
472 vec
<cp_unparsed_functions_entry
, va_gc
> *s
)
475 cp_unparsed_functions_entry
*uf
;
477 fprintf (file
, "Unparsed functions\n");
478 for (i
= 0; vec_safe_iterate (s
, i
, &uf
); i
++)
480 fprintf (file
, "#%u:\n", i
);
481 cp_debug_print_unparsed_function (file
, uf
);
486 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
487 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 cp_debug_parser_tokens (FILE *file
, cp_parser
*parser
, int window_size
)
492 cp_token
*next_token
, *first_token
, *start_token
;
497 next_token
= parser
->lexer
->next_token
;
498 first_token
= parser
->lexer
->buffer
->address ();
499 start_token
= (next_token
> first_token
+ window_size
/ 2)
500 ? next_token
- window_size
/ 2
502 cp_lexer_dump_tokens (file
, parser
->lexer
->buffer
, start_token
, window_size
,
507 /* Dump debugging information for the given PARSER. If FILE is NULL,
508 the output is printed on stderr. */
511 cp_debug_parser (FILE *file
, cp_parser
*parser
)
513 const size_t window_size
= 20;
515 expanded_location eloc
;
520 fprintf (file
, "Parser state\n\n");
521 fprintf (file
, "Number of tokens: %u\n",
522 vec_safe_length (parser
->lexer
->buffer
));
523 cp_debug_print_tree_if_set (file
, "Lookup scope", parser
->scope
);
524 cp_debug_print_tree_if_set (file
, "Object scope",
525 parser
->object_scope
);
526 cp_debug_print_tree_if_set (file
, "Qualifying scope",
527 parser
->qualifying_scope
);
528 cp_debug_print_context_stack (file
, parser
->context
);
529 cp_debug_print_flag (file
, "Allow GNU extensions",
530 parser
->allow_gnu_extensions_p
);
531 cp_debug_print_flag (file
, "'>' token is greater-than",
532 parser
->greater_than_is_operator_p
);
533 cp_debug_print_flag (file
, "Default args allowed in current "
534 "parameter list", parser
->default_arg_ok_p
);
535 cp_debug_print_flag (file
, "Parsing integral constant-expression",
536 parser
->integral_constant_expression_p
);
537 cp_debug_print_flag (file
, "Allow non-constant expression in current "
538 "constant-expression",
539 parser
->allow_non_integral_constant_expression_p
);
540 cp_debug_print_flag (file
, "Seen non-constant expression",
541 parser
->non_integral_constant_expression_p
);
542 cp_debug_print_flag (file
, "Local names forbidden in current context",
543 (parser
->local_variables_forbidden_p
544 & LOCAL_VARS_FORBIDDEN
));
545 cp_debug_print_flag (file
, "'this' forbidden in current context",
546 (parser
->local_variables_forbidden_p
548 cp_debug_print_flag (file
, "In unbraced linkage specification",
549 parser
->in_unbraced_linkage_specification_p
);
550 cp_debug_print_flag (file
, "Parsing a declarator",
551 parser
->in_declarator_p
);
552 cp_debug_print_flag (file
, "In template argument list",
553 parser
->in_template_argument_list_p
);
554 cp_debug_print_flag (file
, "Parsing an iteration statement",
555 parser
->in_statement
& IN_ITERATION_STMT
);
556 cp_debug_print_flag (file
, "Parsing a switch statement",
557 parser
->in_statement
& IN_SWITCH_STMT
);
558 cp_debug_print_flag (file
, "Parsing a structured OpenMP block",
559 parser
->in_statement
& IN_OMP_BLOCK
);
560 cp_debug_print_flag (file
, "Parsing an OpenMP loop",
561 parser
->in_statement
& IN_OMP_FOR
);
562 cp_debug_print_flag (file
, "Parsing an if statement",
563 parser
->in_statement
& IN_IF_STMT
);
564 cp_debug_print_flag (file
, "Parsing a type-id in an expression "
565 "context", parser
->in_type_id_in_expr_p
);
566 cp_debug_print_flag (file
, "String expressions should be translated "
567 "to execution character set",
568 parser
->translate_strings_p
);
569 cp_debug_print_flag (file
, "Parsing function body outside of a "
570 "local class", parser
->in_function_body
);
571 cp_debug_print_flag (file
, "Auto correct a colon to a scope operator",
572 parser
->colon_corrects_to_scope_p
);
573 cp_debug_print_flag (file
, "Colon doesn't start a class definition",
574 parser
->colon_doesnt_start_class_def_p
);
575 if (parser
->type_definition_forbidden_message
)
576 fprintf (file
, "Error message for forbidden type definitions: %s %s\n",
577 parser
->type_definition_forbidden_message
,
578 parser
->type_definition_forbidden_message_arg
579 ? parser
->type_definition_forbidden_message_arg
: "<none>");
580 cp_debug_print_unparsed_queues (file
, parser
->unparsed_queues
);
581 fprintf (file
, "Number of class definitions in progress: %u\n",
582 parser
->num_classes_being_defined
);
583 fprintf (file
, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser
->num_template_parameter_lists
);
585 cp_debug_parser_tokens (file
, parser
, window_size
);
586 token
= parser
->lexer
->next_token
;
587 fprintf (file
, "Next token to parse:\n");
588 fprintf (file
, "\tToken: ");
589 cp_lexer_print_token (file
, token
);
590 eloc
= expand_location (token
->location
);
591 fprintf (file
, "\n\tFile: %s\n", eloc
.file
);
592 fprintf (file
, "\tLine: %d\n", eloc
.line
);
593 fprintf (file
, "\tColumn: %d\n", eloc
.column
);
597 debug (cp_parser
&ref
)
599 cp_debug_parser (stderr
, &ref
);
603 debug (cp_parser
*ptr
)
608 fprintf (stderr
, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
614 cp_lexer_alloc (void)
616 /* Allocate the memory. */
617 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
619 /* Initially we are not debugging. */
620 lexer
->debugging_p
= false;
622 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
624 /* Create the buffer. */
625 vec_alloc (lexer
->buffer
, CP_LEXER_BUFFER_SIZE
);
630 /* Create a new main C++ lexer, the lexer that gets tokens from the
634 cp_lexer_new_main (void)
638 /* It's possible that parsing the first pragma will load a PCH file,
639 which is a GC collection point. So we have to do that before
640 allocating any memory. */
641 cp_lexer_get_preprocessor_token (0, &token
);
642 cp_parser_initial_pragma (&token
);
643 c_common_no_more_pch ();
645 cp_lexer
*lexer
= cp_lexer_alloc ();
646 /* Put the first token in the buffer. */
647 cp_token
*tok
= lexer
->buffer
->quick_push (token
);
649 uintptr_t filter
= 0;
651 filter
= module_token_cdtor (parse_in
, filter
);
653 /* Get the remaining tokens from the preprocessor. */
654 while (tok
->type
!= CPP_EOF
)
657 /* Process the previous token. */
658 module_token_lang (tok
->type
, tok
->keyword
, tok
->u
.value
,
659 tok
->location
, filter
);
660 tok
= vec_safe_push (lexer
->buffer
, cp_token ());
661 cp_lexer_get_preprocessor_token (C_LEX_STRING_NO_JOIN
, tok
);
664 lexer
->next_token
= lexer
->buffer
->address ();
665 lexer
->last_token
= lexer
->next_token
666 + lexer
->buffer
->length ()
669 if (lexer
->buffer
->length () != 1)
671 /* Set the EOF token's location to be the just after the previous
672 token's range. That way 'at-eof' diagnostics point at something
674 auto range
= get_range_from_loc (line_table
, tok
[-1].location
);
676 = linemap_position_for_loc_and_offset (line_table
, range
.m_finish
, 1);
680 module_token_cdtor (parse_in
, filter
);
682 /* Subsequent preprocessor diagnostics should use compiler
683 diagnostic functions to get the compiler source location. */
686 maybe_check_all_macros (parse_in
);
688 gcc_assert (!lexer
->next_token
->purged_p
);
692 /* Create a new lexer whose token stream is primed with the tokens in
693 CACHE. When these tokens are exhausted, no new tokens will be read. */
696 cp_lexer_new_from_tokens (cp_token_cache
*cache
)
698 cp_token
*first
= cache
->first
;
699 cp_token
*last
= cache
->last
;
700 cp_lexer
*lexer
= ggc_cleared_alloc
<cp_lexer
> ();
702 /* We do not own the buffer. */
703 lexer
->buffer
= NULL
;
705 /* Insert an EOF token. */
706 lexer
->saved_type
= last
->type
;
707 lexer
->saved_keyword
= last
->keyword
;
708 last
->type
= CPP_EOF
;
709 last
->keyword
= RID_MAX
;
711 lexer
->next_token
= first
;
712 lexer
->last_token
= last
;
714 lexer
->saved_tokens
.create (CP_SAVED_TOKEN_STACK
);
716 /* Initially we are not debugging. */
717 lexer
->debugging_p
= false;
719 gcc_assert (!lexer
->next_token
->purged_p
720 && !lexer
->last_token
->purged_p
);
724 /* Frees all resources associated with LEXER. */
727 cp_lexer_destroy (cp_lexer
*lexer
)
730 vec_free (lexer
->buffer
);
733 /* Restore the token we overwrite with EOF. */
734 lexer
->last_token
->type
= lexer
->saved_type
;
735 lexer
->last_token
->keyword
= lexer
->saved_keyword
;
737 lexer
->saved_tokens
.release ();
741 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
742 be used. The point of this flag is to help the compiler to fold away calls
743 to cp_lexer_debugging_p within this source file at compile time, when the
744 lexer is not being debugged. */
746 #define LEXER_DEBUGGING_ENABLED_P false
748 /* Returns nonzero if debugging information should be output. */
751 cp_lexer_debugging_p (cp_lexer
*lexer
)
753 if (!LEXER_DEBUGGING_ENABLED_P
)
756 return lexer
->debugging_p
;
760 static inline cp_token_position
761 cp_lexer_token_position (cp_lexer
*lexer
, bool previous_p
)
763 return lexer
->next_token
- previous_p
;
766 static inline cp_token
*
767 cp_lexer_token_at (cp_lexer
* /*lexer*/, cp_token_position pos
)
773 cp_lexer_set_token_position (cp_lexer
*lexer
, cp_token_position pos
)
775 lexer
->next_token
= cp_lexer_token_at (lexer
, pos
);
778 static inline cp_token_position
779 cp_lexer_previous_token_position (cp_lexer
*lexer
)
781 return cp_lexer_token_position (lexer
, true);
784 static inline cp_token
*
785 cp_lexer_previous_token (cp_lexer
*lexer
)
787 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
789 /* Skip past purged tokens. */
792 gcc_assert (tp
!= vec_safe_address (lexer
->buffer
));
796 return cp_lexer_token_at (lexer
, tp
);
799 /* Same as above, but return NULL when the lexer doesn't own the token
800 buffer or if the next_token is at the start of the token
801 vector or if all previous tokens are purged. */
804 cp_lexer_safe_previous_token (cp_lexer
*lexer
)
807 && lexer
->next_token
!= lexer
->buffer
->address ())
809 cp_token_position tp
= cp_lexer_previous_token_position (lexer
);
811 /* Skip past purged tokens. */
814 if (tp
== lexer
->buffer
->address ())
818 return cp_lexer_token_at (lexer
, tp
);
824 /* Overload for make_location, taking the lexer to mean the location of the
827 static inline location_t
828 make_location (location_t caret
, location_t start
, cp_lexer
*lexer
)
830 cp_token
*t
= cp_lexer_previous_token (lexer
);
831 return make_location (caret
, start
, t
->location
);
834 /* Overload for make_location taking tokens instead of locations. */
836 static inline location_t
837 make_location (cp_token
*caret
, cp_token
*start
, cp_token
*end
)
839 return make_location (caret
->location
, start
->location
, end
->location
);
842 /* nonzero if we are presently saving tokens. */
845 cp_lexer_saving_tokens (const cp_lexer
* lexer
)
847 return lexer
->saved_tokens
.length () != 0;
850 /* Store the next token from the preprocessor in *TOKEN. Return true
851 if we reach EOF. If LEXER is NULL, assume we are handling an
852 initial #pragma pch_preprocess, and thus want the lexer to return
853 processed strings. */
856 cp_lexer_get_preprocessor_token (unsigned flags
, cp_token
*token
)
858 static int is_extern_c
= 0;
860 /* Get a new token from the preprocessor. */
862 = c_lex_with_flags (&token
->u
.value
, &token
->location
, &token
->flags
,
864 token
->keyword
= RID_MAX
;
865 token
->purged_p
= false;
866 token
->error_reported
= false;
867 token
->tree_check_p
= false;
868 /* Usually never see a zero, but just in case ... */
869 token
->main_source_p
= line_table
->depth
<= 1;
871 /* On some systems, some header files are surrounded by an
872 implicit extern "C" block. Set a flag in the token if it
873 comes from such a header. */
874 is_extern_c
+= pending_lang_change
;
875 pending_lang_change
= 0;
876 token
->implicit_extern_c
= is_extern_c
> 0;
878 /* Check to see if this token is a keyword. */
879 if (token
->type
== CPP_NAME
)
881 if (IDENTIFIER_KEYWORD_P (token
->u
.value
))
883 /* Mark this token as a keyword. */
884 token
->type
= CPP_KEYWORD
;
885 /* Record which keyword. */
886 token
->keyword
= C_RID_CODE (token
->u
.value
);
890 if (warn_cxx11_compat
891 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX11
892 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX11
)
894 /* Warn about the C++0x keyword (but still treat it as
896 warning_at (token
->location
, OPT_Wc__11_compat
,
897 "identifier %qE is a keyword in C++11",
900 /* Clear out the C_RID_CODE so we don't warn about this
901 particular identifier-turned-keyword again. */
902 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
904 if (warn_cxx20_compat
905 && C_RID_CODE (token
->u
.value
) >= RID_FIRST_CXX20
906 && C_RID_CODE (token
->u
.value
) <= RID_LAST_CXX20
)
908 /* Warn about the C++20 keyword (but still treat it as
910 warning_at (token
->location
, OPT_Wc__20_compat
,
911 "identifier %qE is a keyword in C++20",
914 /* Clear out the C_RID_CODE so we don't warn about this
915 particular identifier-turned-keyword again. */
916 C_SET_RID_CODE (token
->u
.value
, RID_MAX
);
919 token
->keyword
= RID_MAX
;
922 else if (token
->type
== CPP_AT_NAME
)
924 /* This only happens in Objective-C++; it must be a keyword. */
925 token
->type
= CPP_KEYWORD
;
926 switch (C_RID_CODE (token
->u
.value
))
928 /* Replace 'class' with '@class', 'private' with '@private',
929 etc. This prevents confusion with the C++ keyword
930 'class', and makes the tokens consistent with other
931 Objective-C 'AT' keywords. For example '@class' is
932 reported as RID_AT_CLASS which is consistent with
933 '@synchronized', which is reported as
936 case RID_CLASS
: token
->keyword
= RID_AT_CLASS
; break;
937 case RID_PRIVATE
: token
->keyword
= RID_AT_PRIVATE
; break;
938 case RID_PROTECTED
: token
->keyword
= RID_AT_PROTECTED
; break;
939 case RID_PUBLIC
: token
->keyword
= RID_AT_PUBLIC
; break;
940 case RID_THROW
: token
->keyword
= RID_AT_THROW
; break;
941 case RID_TRY
: token
->keyword
= RID_AT_TRY
; break;
942 case RID_CATCH
: token
->keyword
= RID_AT_CATCH
; break;
943 case RID_SYNCHRONIZED
: token
->keyword
= RID_AT_SYNCHRONIZED
; break;
944 default: token
->keyword
= C_RID_CODE (token
->u
.value
);
949 /* Update the globals input_location and the input file stack from TOKEN. */
951 cp_lexer_set_source_position_from_token (cp_token
*token
)
953 input_location
= token
->location
;
956 /* Update the globals input_location and the input file stack from LEXER. */
958 cp_lexer_set_source_position (cp_lexer
*lexer
)
960 cp_token
*token
= cp_lexer_peek_token (lexer
);
961 cp_lexer_set_source_position_from_token (token
);
964 /* Return a pointer to the next token in the token stream, but do not
967 static inline cp_token
*
968 cp_lexer_peek_token (cp_lexer
*lexer
)
970 if (cp_lexer_debugging_p (lexer
))
972 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream
);
973 cp_lexer_print_token (cp_lexer_debug_stream
, lexer
->next_token
);
974 putc ('\n', cp_lexer_debug_stream
);
976 return lexer
->next_token
;
979 /* Return true if the next token has the indicated TYPE. */
982 cp_lexer_next_token_is (cp_lexer
* lexer
, enum cpp_ttype type
)
984 return cp_lexer_peek_token (lexer
)->type
== type
;
987 /* Return true if the next token does not have the indicated TYPE. */
990 cp_lexer_next_token_is_not (cp_lexer
* lexer
, enum cpp_ttype type
)
992 return !cp_lexer_next_token_is (lexer
, type
);
995 /* Return true if the next token is the indicated KEYWORD. */
998 cp_lexer_next_token_is_keyword (cp_lexer
* lexer
, enum rid keyword
)
1000 return cp_lexer_peek_token (lexer
)->keyword
== keyword
;
1004 cp_lexer_nth_token_is (cp_lexer
* lexer
, size_t n
, enum cpp_ttype type
)
1006 return cp_lexer_peek_nth_token (lexer
, n
)->type
== type
;
1010 cp_lexer_nth_token_is_keyword (cp_lexer
* lexer
, size_t n
, enum rid keyword
)
1012 return cp_lexer_peek_nth_token (lexer
, n
)->keyword
== keyword
;
1015 /* Return true if KEYWORD can start a decl-specifier. */
1018 cp_keyword_starts_decl_specifier_p (enum rid keyword
)
1022 /* auto specifier: storage-class-specifier in C++,
1023 simple-type-specifier in C++0x. */
1025 /* Storage classes. */
1031 /* Elaborated type specifiers. */
1037 /* Simple type specifiers. */
1052 /* GNU extensions. */
1055 /* C++11 extensions. */
1057 case RID_UNDERLYING_TYPE
:
1059 /* C++20 extensions. */
1065 if (keyword
>= RID_FIRST_INT_N
1066 && keyword
< RID_FIRST_INT_N
+ NUM_INT_N_ENTS
1067 && int_n_enabled_p
[keyword
- RID_FIRST_INT_N
])
1073 /* Return true if the next token is a keyword for a decl-specifier. */
1076 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer
*lexer
)
1080 token
= cp_lexer_peek_token (lexer
);
1081 return cp_keyword_starts_decl_specifier_p (token
->keyword
);
1084 /* Returns TRUE iff the token T begins a decltype type. */
1087 token_is_decltype (cp_token
*t
)
1089 return (t
->keyword
== RID_DECLTYPE
1090 || t
->type
== CPP_DECLTYPE
);
1093 /* Returns TRUE iff the next token begins a decltype type. */
1096 cp_lexer_next_token_is_decltype (cp_lexer
*lexer
)
1098 cp_token
*t
= cp_lexer_peek_token (lexer
);
1099 return token_is_decltype (t
);
1102 /* Called when processing a token with tree_check_value; perform or defer the
1103 associated checks and return the value. */
1106 saved_checks_value (struct tree_check
*check_value
)
1108 /* Perform any access checks that were deferred. */
1109 vec
<deferred_access_check
, va_gc
> *checks
;
1110 deferred_access_check
*chk
;
1111 checks
= check_value
->checks
;
1115 FOR_EACH_VEC_SAFE_ELT (checks
, i
, chk
)
1116 perform_or_defer_access_check (chk
->binfo
,
1118 chk
->diag_decl
, tf_warning_or_error
);
1120 /* Return the stored value. */
1121 return check_value
->value
;
1124 /* Return a pointer to the Nth token in the token stream. If N is 1,
1125 then this is precisely equivalent to cp_lexer_peek_token (except
1126 that it is not inline). One would like to disallow that case, but
1127 there is one case (cp_parser_nth_token_starts_template_id) where
1128 the caller passes a variable for N and it might be 1. */
1131 cp_lexer_peek_nth_token (cp_lexer
* lexer
, size_t n
)
1135 /* N is 1-based, not zero-based. */
1138 if (cp_lexer_debugging_p (lexer
))
1139 fprintf (cp_lexer_debug_stream
,
1140 "cp_lexer: peeking ahead %ld at token: ", (long)n
);
1143 token
= lexer
->next_token
;
1144 while (n
&& token
->type
!= CPP_EOF
)
1147 if (!token
->purged_p
)
1151 if (cp_lexer_debugging_p (lexer
))
1153 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1154 putc ('\n', cp_lexer_debug_stream
);
1160 /* Return the next token, and advance the lexer's next_token pointer
1161 to point to the next non-purged token. */
1164 cp_lexer_consume_token (cp_lexer
* lexer
)
1166 cp_token
*token
= lexer
->next_token
;
1170 gcc_assert (token
->type
!= CPP_EOF
);
1171 lexer
->next_token
++;
1173 while (lexer
->next_token
->purged_p
);
1175 cp_lexer_set_source_position_from_token (token
);
1177 /* Provide debugging output. */
1178 if (cp_lexer_debugging_p (lexer
))
1180 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream
);
1181 cp_lexer_print_token (cp_lexer_debug_stream
, token
);
1182 putc ('\n', cp_lexer_debug_stream
);
1188 /* Permanently remove the next token from the token stream, and
1189 advance the next_token pointer to refer to the next non-purged
1193 cp_lexer_purge_token (cp_lexer
*lexer
)
1195 cp_token
*tok
= lexer
->next_token
;
1197 gcc_assert (tok
->type
!= CPP_EOF
);
1198 tok
->purged_p
= true;
1199 tok
->location
= UNKNOWN_LOCATION
;
1200 tok
->u
.value
= NULL_TREE
;
1201 tok
->keyword
= RID_MAX
;
1205 while (tok
->purged_p
);
1206 lexer
->next_token
= tok
;
1209 /* Permanently remove all tokens after TOK, up to, but not
1210 including, the token that will be returned next by
1211 cp_lexer_peek_token. */
1214 cp_lexer_purge_tokens_after (cp_lexer
*lexer
, cp_token
*tok
)
1216 cp_token
*peek
= lexer
->next_token
;
1218 gcc_assert (tok
< peek
);
1220 for (tok
++; tok
!= peek
; tok
++)
1222 tok
->purged_p
= true;
1223 tok
->location
= UNKNOWN_LOCATION
;
1224 tok
->u
.value
= NULL_TREE
;
1225 tok
->keyword
= RID_MAX
;
1229 /* Begin saving tokens. All tokens consumed after this point will be
1233 cp_lexer_save_tokens (cp_lexer
* lexer
)
1235 /* Provide debugging output. */
1236 if (cp_lexer_debugging_p (lexer
))
1237 fprintf (cp_lexer_debug_stream
, "cp_lexer: saving tokens\n");
1239 lexer
->saved_tokens
.safe_push (lexer
->next_token
);
1242 /* Commit to the portion of the token stream most recently saved. */
1245 cp_lexer_commit_tokens (cp_lexer
* lexer
)
1247 /* Provide debugging output. */
1248 if (cp_lexer_debugging_p (lexer
))
1249 fprintf (cp_lexer_debug_stream
, "cp_lexer: committing tokens\n");
1251 lexer
->saved_tokens
.pop ();
1254 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1255 to the token stream. Stop saving tokens. */
1258 cp_lexer_rollback_tokens (cp_lexer
* lexer
)
1260 /* Provide debugging output. */
1261 if (cp_lexer_debugging_p (lexer
))
1262 fprintf (cp_lexer_debug_stream
, "cp_lexer: restoring tokens\n");
1264 lexer
->next_token
= lexer
->saved_tokens
.pop ();
1267 /* RAII wrapper around the above functions, with sanity checking. Creating
1268 a variable saves tokens, which are committed when the variable is
1269 destroyed unless they are explicitly rolled back by calling the rollback
1272 struct saved_token_sentinel
1277 saved_token_sentinel(cp_lexer
*lexer
): lexer(lexer
), commit(true)
1279 len
= lexer
->saved_tokens
.length ();
1280 cp_lexer_save_tokens (lexer
);
1284 cp_lexer_rollback_tokens (lexer
);
1287 ~saved_token_sentinel()
1290 cp_lexer_commit_tokens (lexer
);
1291 gcc_assert (lexer
->saved_tokens
.length () == len
);
1295 /* Print a representation of the TOKEN on the STREAM. */
1298 cp_lexer_print_token (FILE * stream
, cp_token
*token
)
1300 /* We don't use cpp_type2name here because the parser defines
1301 a few tokens of its own. */
1302 static const char *const token_names
[] = {
1303 /* cpplib-defined token types */
1304 #define OP(e, s) #e,
1305 #define TK(e, s) #e,
1309 /* C++ parser token types - see "Manifest constants", above. */
1312 "NESTED_NAME_SPECIFIER",
1315 /* For some tokens, print the associated data. */
1316 switch (token
->type
)
1319 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1320 For example, `struct' is mapped to an INTEGER_CST. */
1321 if (!identifier_p (token
->u
.value
))
1325 fputs (IDENTIFIER_POINTER (token
->u
.value
), stream
);
1332 case CPP_UTF8STRING
:
1333 fprintf (stream
, " \"%s\"", TREE_STRING_POINTER (token
->u
.value
));
1337 print_generic_expr (stream
, token
->u
.value
);
1341 /* If we have a name for the token, print it out. Otherwise, we
1342 simply give the numeric code. */
1343 if (token
->type
< ARRAY_SIZE(token_names
))
1344 fputs (token_names
[token
->type
], stream
);
1346 fprintf (stream
, "[%d]", token
->type
);
1352 debug (cp_token
&ref
)
1354 cp_lexer_print_token (stderr
, &ref
);
1355 fprintf (stderr
, "\n");
1359 debug (cp_token
*ptr
)
1364 fprintf (stderr
, "<nil>\n");
1368 /* Start emitting debugging information. */
1371 cp_lexer_start_debugging (cp_lexer
* lexer
)
1373 if (!LEXER_DEBUGGING_ENABLED_P
)
1374 fatal_error (input_location
,
1375 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1377 lexer
->debugging_p
= true;
1378 cp_lexer_debug_stream
= stderr
;
1381 /* Stop emitting debugging information. */
1384 cp_lexer_stop_debugging (cp_lexer
* lexer
)
1386 if (!LEXER_DEBUGGING_ENABLED_P
)
1387 fatal_error (input_location
,
1388 "%<LEXER_DEBUGGING_ENABLED_P%> is not set to true");
1390 lexer
->debugging_p
= false;
1391 cp_lexer_debug_stream
= NULL
;
1394 /* Create a new cp_token_cache, representing a range of tokens. */
1396 static cp_token_cache
*
1397 cp_token_cache_new (cp_token
*first
, cp_token
*last
)
1399 cp_token_cache
*cache
= ggc_alloc
<cp_token_cache
> ();
1400 cache
->first
= first
;
1405 /* Diagnose if #pragma omp declare simd isn't followed immediately
1406 by function declaration or definition. */
1409 cp_ensure_no_omp_declare_simd (cp_parser
*parser
)
1411 if (parser
->omp_declare_simd
&& !parser
->omp_declare_simd
->error_seen
)
1413 error ("%<#pragma omp declare %s%> not immediately followed by "
1414 "function declaration or definition",
1415 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
1416 parser
->omp_declare_simd
= NULL
;
1420 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1421 and put that into "omp declare simd" attribute. */
1424 cp_finalize_omp_declare_simd (cp_parser
*parser
, tree fndecl
)
1426 if (__builtin_expect (parser
->omp_declare_simd
!= NULL
, 0))
1428 if (fndecl
== error_mark_node
)
1430 parser
->omp_declare_simd
= NULL
;
1433 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
1435 cp_ensure_no_omp_declare_simd (parser
);
1441 /* Diagnose if #pragma acc routine isn't followed immediately by function
1442 declaration or definition. */
1445 cp_ensure_no_oacc_routine (cp_parser
*parser
)
1447 if (parser
->oacc_routine
&& !parser
->oacc_routine
->error_seen
)
1449 error_at (parser
->oacc_routine
->loc
,
1450 "%<#pragma acc routine%> not immediately followed by "
1451 "function declaration or definition");
1452 parser
->oacc_routine
= NULL
;
1456 /* Decl-specifiers. */
1458 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1461 clear_decl_specs (cp_decl_specifier_seq
*decl_specs
)
1463 memset (decl_specs
, 0, sizeof (cp_decl_specifier_seq
));
1468 /* Nothing other than the parser should be creating declarators;
1469 declarators are a semi-syntactic representation of C++ entities.
1470 Other parts of the front end that need to create entities (like
1471 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1473 static cp_declarator
*make_call_declarator
1474 (cp_declarator
*, tree
, cp_cv_quals
, cp_virt_specifiers
, cp_ref_qualifier
,
1475 tree
, tree
, tree
, tree
, location_t
);
1476 static cp_declarator
*make_array_declarator
1477 (cp_declarator
*, tree
);
1478 static cp_declarator
*make_pointer_declarator
1479 (cp_cv_quals
, cp_declarator
*, tree
);
1480 static cp_declarator
*make_reference_declarator
1481 (cp_cv_quals
, cp_declarator
*, bool, tree
);
1482 static cp_declarator
*make_ptrmem_declarator
1483 (cp_cv_quals
, tree
, cp_declarator
*, tree
);
1485 /* An erroneous declarator. */
1486 static cp_declarator
*cp_error_declarator
;
1488 /* The obstack on which declarators and related data structures are
1490 static struct obstack declarator_obstack
;
1492 /* Alloc BYTES from the declarator memory pool. */
1494 static inline void *
1495 alloc_declarator (size_t bytes
)
1497 return obstack_alloc (&declarator_obstack
, bytes
);
1500 /* Allocate a declarator of the indicated KIND. Clear fields that are
1501 common to all declarators. */
1503 static cp_declarator
*
1504 make_declarator (cp_declarator_kind kind
)
1506 cp_declarator
*declarator
;
1508 declarator
= (cp_declarator
*) alloc_declarator (sizeof (cp_declarator
));
1509 declarator
->kind
= kind
;
1510 declarator
->parenthesized
= UNKNOWN_LOCATION
;
1511 declarator
->attributes
= NULL_TREE
;
1512 declarator
->std_attributes
= NULL_TREE
;
1513 declarator
->declarator
= NULL
;
1514 declarator
->parameter_pack_p
= false;
1515 declarator
->id_loc
= UNKNOWN_LOCATION
;
1520 /* Make a declarator for a generalized identifier. If
1521 QUALIFYING_SCOPE is non-NULL, the identifier is
1522 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1523 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1526 static cp_declarator
*
1527 make_id_declarator (tree qualifying_scope
, tree unqualified_name
,
1528 special_function_kind sfk
, location_t id_location
)
1530 cp_declarator
*declarator
;
1532 /* It is valid to write:
1534 class C { void f(); };
1538 The standard is not clear about whether `typedef const C D' is
1539 legal; as of 2002-09-15 the committee is considering that
1540 question. EDG 3.0 allows that syntax. Therefore, we do as
1542 if (qualifying_scope
&& TYPE_P (qualifying_scope
))
1543 qualifying_scope
= TYPE_MAIN_VARIANT (qualifying_scope
);
1545 gcc_assert (identifier_p (unqualified_name
)
1546 || TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
1547 || TREE_CODE (unqualified_name
) == TEMPLATE_ID_EXPR
);
1549 declarator
= make_declarator (cdk_id
);
1550 declarator
->u
.id
.qualifying_scope
= qualifying_scope
;
1551 declarator
->u
.id
.unqualified_name
= unqualified_name
;
1552 declarator
->u
.id
.sfk
= sfk
;
1553 declarator
->id_loc
= id_location
;
1558 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1559 of modifiers such as const or volatile to apply to the pointer
1560 type, represented as identifiers. ATTRIBUTES represent the attributes that
1561 appertain to the pointer or reference. */
1564 make_pointer_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1567 cp_declarator
*declarator
;
1569 declarator
= make_declarator (cdk_pointer
);
1570 declarator
->declarator
= target
;
1571 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1572 declarator
->u
.pointer
.class_type
= NULL_TREE
;
1575 declarator
->id_loc
= target
->id_loc
;
1576 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1577 target
->parameter_pack_p
= false;
1580 declarator
->parameter_pack_p
= false;
1582 declarator
->std_attributes
= attributes
;
1587 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1588 represent the attributes that appertain to the pointer or
1592 make_reference_declarator (cp_cv_quals cv_qualifiers
, cp_declarator
*target
,
1593 bool rvalue_ref
, tree attributes
)
1595 cp_declarator
*declarator
;
1597 declarator
= make_declarator (cdk_reference
);
1598 declarator
->declarator
= target
;
1599 declarator
->u
.reference
.qualifiers
= cv_qualifiers
;
1600 declarator
->u
.reference
.rvalue_ref
= rvalue_ref
;
1603 declarator
->id_loc
= target
->id_loc
;
1604 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1605 target
->parameter_pack_p
= false;
1608 declarator
->parameter_pack_p
= false;
1610 declarator
->std_attributes
= attributes
;
1615 /* Like make_pointer_declarator -- but for a pointer to a non-static
1616 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1617 appertain to the pointer or reference. */
1620 make_ptrmem_declarator (cp_cv_quals cv_qualifiers
, tree class_type
,
1621 cp_declarator
*pointee
,
1624 cp_declarator
*declarator
;
1626 declarator
= make_declarator (cdk_ptrmem
);
1627 declarator
->declarator
= pointee
;
1628 declarator
->u
.pointer
.qualifiers
= cv_qualifiers
;
1629 declarator
->u
.pointer
.class_type
= class_type
;
1633 declarator
->parameter_pack_p
= pointee
->parameter_pack_p
;
1634 pointee
->parameter_pack_p
= false;
1637 declarator
->parameter_pack_p
= false;
1639 declarator
->std_attributes
= attributes
;
1644 /* Make a declarator for the function given by TARGET, with the
1645 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1646 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1647 indicates what exceptions can be thrown. */
1650 make_call_declarator (cp_declarator
*target
,
1652 cp_cv_quals cv_qualifiers
,
1653 cp_virt_specifiers virt_specifiers
,
1654 cp_ref_qualifier ref_qualifier
,
1656 tree exception_specification
,
1657 tree late_return_type
,
1658 tree requires_clause
,
1659 location_t parens_loc
)
1661 cp_declarator
*declarator
;
1663 declarator
= make_declarator (cdk_function
);
1664 declarator
->declarator
= target
;
1665 declarator
->u
.function
.parameters
= parms
;
1666 declarator
->u
.function
.qualifiers
= cv_qualifiers
;
1667 declarator
->u
.function
.virt_specifiers
= virt_specifiers
;
1668 declarator
->u
.function
.ref_qualifier
= ref_qualifier
;
1669 declarator
->u
.function
.tx_qualifier
= tx_qualifier
;
1670 declarator
->u
.function
.exception_specification
= exception_specification
;
1671 declarator
->u
.function
.late_return_type
= late_return_type
;
1672 declarator
->u
.function
.requires_clause
= requires_clause
;
1673 declarator
->u
.function
.parens_loc
= parens_loc
;
1676 declarator
->id_loc
= target
->id_loc
;
1677 declarator
->parameter_pack_p
= target
->parameter_pack_p
;
1678 target
->parameter_pack_p
= false;
1681 declarator
->parameter_pack_p
= false;
1686 /* Make a declarator for an array of BOUNDS elements, each of which is
1687 defined by ELEMENT. */
1690 make_array_declarator (cp_declarator
*element
, tree bounds
)
1692 cp_declarator
*declarator
;
1694 declarator
= make_declarator (cdk_array
);
1695 declarator
->declarator
= element
;
1696 declarator
->u
.array
.bounds
= bounds
;
1699 declarator
->id_loc
= element
->id_loc
;
1700 declarator
->parameter_pack_p
= element
->parameter_pack_p
;
1701 element
->parameter_pack_p
= false;
1704 declarator
->parameter_pack_p
= false;
1709 /* Determine whether the declarator we've seen so far can be a
1710 parameter pack, when followed by an ellipsis. */
1712 declarator_can_be_parameter_pack (cp_declarator
*declarator
)
1714 if (declarator
&& declarator
->parameter_pack_p
)
1715 /* We already saw an ellipsis. */
1718 /* Search for a declarator name, or any other declarator that goes
1719 after the point where the ellipsis could appear in a parameter
1720 pack. If we find any of these, then this declarator cannot be
1721 made into a parameter pack. */
1723 while (declarator
&& !found
)
1725 switch ((int)declarator
->kind
)
1737 declarator
= declarator
->declarator
;
1745 cp_parameter_declarator
*no_parameters
;
1747 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1748 DECLARATOR and DEFAULT_ARGUMENT. */
1750 cp_parameter_declarator
*
1751 make_parameter_declarator (cp_decl_specifier_seq
*decl_specifiers
,
1752 cp_declarator
*declarator
,
1753 tree default_argument
,
1755 bool template_parameter_pack_p
= false)
1757 cp_parameter_declarator
*parameter
;
1759 parameter
= ((cp_parameter_declarator
*)
1760 alloc_declarator (sizeof (cp_parameter_declarator
)));
1761 parameter
->next
= NULL
;
1762 if (decl_specifiers
)
1763 parameter
->decl_specifiers
= *decl_specifiers
;
1765 clear_decl_specs (¶meter
->decl_specifiers
);
1766 parameter
->declarator
= declarator
;
1767 parameter
->default_argument
= default_argument
;
1768 parameter
->template_parameter_pack_p
= template_parameter_pack_p
;
1769 parameter
->loc
= loc
;
1774 /* Returns true iff DECLARATOR is a declaration for a function. */
1777 function_declarator_p (const cp_declarator
*declarator
)
1781 if (declarator
->kind
== cdk_function
1782 && declarator
->declarator
->kind
== cdk_id
)
1784 if (declarator
->kind
== cdk_id
1785 || declarator
->kind
== cdk_decomp
1786 || declarator
->kind
== cdk_error
)
1788 declarator
= declarator
->declarator
;
1798 A cp_parser parses the token stream as specified by the C++
1799 grammar. Its job is purely parsing, not semantic analysis. For
1800 example, the parser breaks the token stream into declarators,
1801 expressions, statements, and other similar syntactic constructs.
1802 It does not check that the types of the expressions on either side
1803 of an assignment-statement are compatible, or that a function is
1804 not declared with a parameter of type `void'.
1806 The parser invokes routines elsewhere in the compiler to perform
1807 semantic analysis and to build up the abstract syntax tree for the
1810 The parser (and the template instantiation code, which is, in a
1811 way, a close relative of parsing) are the only parts of the
1812 compiler that should be calling push_scope and pop_scope, or
1813 related functions. The parser (and template instantiation code)
1814 keeps track of what scope is presently active; everything else
1815 should simply honor that. (The code that generates static
1816 initializers may also need to set the scope, in order to check
1817 access control correctly when emitting the initializers.)
1822 The parser is of the standard recursive-descent variety. Upcoming
1823 tokens in the token stream are examined in order to determine which
1824 production to use when parsing a non-terminal. Some C++ constructs
1825 require arbitrary look ahead to disambiguate. For example, it is
1826 impossible, in the general case, to tell whether a statement is an
1827 expression or declaration without scanning the entire statement.
1828 Therefore, the parser is capable of "parsing tentatively." When the
1829 parser is not sure what construct comes next, it enters this mode.
1830 Then, while we attempt to parse the construct, the parser queues up
1831 error messages, rather than issuing them immediately, and saves the
1832 tokens it consumes. If the construct is parsed successfully, the
1833 parser "commits", i.e., it issues any queued error messages and
1834 the tokens that were being preserved are permanently discarded.
1835 If, however, the construct is not parsed successfully, the parser
1836 rolls back its state completely so that it can resume parsing using
1837 a different alternative.
1842 The performance of the parser could probably be improved substantially.
1843 We could often eliminate the need to parse tentatively by looking ahead
1844 a little bit. In some places, this approach might not entirely eliminate
1845 the need to parse tentatively, but it might still speed up the average
1848 /* Flags that are passed to some parsing functions. These values can
1849 be bitwise-ored together. */
1854 CP_PARSER_FLAGS_NONE
= 0x0,
1855 /* The construct is optional. If it is not present, then no error
1856 should be issued. */
1857 CP_PARSER_FLAGS_OPTIONAL
= 0x1,
1858 /* When parsing a type-specifier, treat user-defined type-names
1859 as non-type identifiers. */
1860 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
= 0x2,
1861 /* When parsing a type-specifier, do not try to parse a class-specifier
1862 or enum-specifier. */
1863 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
= 0x4,
1864 /* When parsing a decl-specifier-seq, only allow type-specifier or
1866 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
= 0x8,
1867 /* When parsing a decl-specifier-seq, only allow mutable, constexpr or
1868 for C++20 consteval. */
1869 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
= 0x10,
1870 /* When parsing a decl-specifier-seq, allow missing typename. */
1871 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
= 0x20,
1872 /* When parsing of the noexcept-specifier should be delayed. */
1873 CP_PARSER_FLAGS_DELAY_NOEXCEPT
= 0x40,
1874 /* When parsing a consteval declarator. */
1875 CP_PARSER_FLAGS_CONSTEVAL
= 0x80
1878 /* This type is used for parameters and variables which hold
1879 combinations of the above flags. */
1880 typedef int cp_parser_flags
;
1882 /* The different kinds of declarators we want to parse. */
1884 enum cp_parser_declarator_kind
1886 /* We want an abstract declarator. */
1887 CP_PARSER_DECLARATOR_ABSTRACT
,
1888 /* We want a named declarator. */
1889 CP_PARSER_DECLARATOR_NAMED
,
1890 /* We don't mind, but the name must be an unqualified-id. */
1891 CP_PARSER_DECLARATOR_EITHER
1894 /* The precedence values used to parse binary expressions. The minimum value
1895 of PREC must be 1, because zero is reserved to quickly discriminate
1896 binary operators from other tokens. */
1901 PREC_LOGICAL_OR_EXPRESSION
,
1902 PREC_LOGICAL_AND_EXPRESSION
,
1903 PREC_INCLUSIVE_OR_EXPRESSION
,
1904 PREC_EXCLUSIVE_OR_EXPRESSION
,
1905 PREC_AND_EXPRESSION
,
1906 PREC_EQUALITY_EXPRESSION
,
1907 PREC_RELATIONAL_EXPRESSION
,
1908 PREC_SPACESHIP_EXPRESSION
,
1909 PREC_SHIFT_EXPRESSION
,
1910 PREC_ADDITIVE_EXPRESSION
,
1911 PREC_MULTIPLICATIVE_EXPRESSION
,
1913 NUM_PREC_VALUES
= PREC_PM_EXPRESSION
1916 /* A mapping from a token type to a corresponding tree node type, with a
1917 precedence value. */
1919 struct cp_parser_binary_operations_map_node
1921 /* The token type. */
1922 enum cpp_ttype token_type
;
1923 /* The corresponding tree code. */
1924 enum tree_code tree_type
;
1925 /* The precedence of this operator. */
1926 enum cp_parser_prec prec
;
1929 struct cp_parser_expression_stack_entry
1931 /* Left hand side of the binary operation we are currently
1934 /* Original tree code for left hand side, if it was a binary
1935 expression itself (used for -Wparentheses). */
1936 enum tree_code lhs_type
;
1937 /* Tree code for the binary operation we are parsing. */
1938 enum tree_code tree_type
;
1939 /* Precedence of the binary operation we are parsing. */
1940 enum cp_parser_prec prec
;
1941 /* Location of the binary operation we are parsing. */
1945 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1946 entries because precedence levels on the stack are monotonically
1948 typedef struct cp_parser_expression_stack_entry
1949 cp_parser_expression_stack
[NUM_PREC_VALUES
];
1953 /* Constructors and destructors. */
1955 static cp_parser_context
*cp_parser_context_new
1956 (cp_parser_context
*);
1958 /* Class variables. */
1960 static GTY((deletable
)) cp_parser_context
* cp_parser_context_free_list
;
1962 /* The operator-precedence table used by cp_parser_binary_expression.
1963 Transformed into an associative array (binops_by_token) by
1966 static const cp_parser_binary_operations_map_node binops
[] = {
1967 { CPP_DEREF_STAR
, MEMBER_REF
, PREC_PM_EXPRESSION
},
1968 { CPP_DOT_STAR
, DOTSTAR_EXPR
, PREC_PM_EXPRESSION
},
1970 { CPP_MULT
, MULT_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1971 { CPP_DIV
, TRUNC_DIV_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1972 { CPP_MOD
, TRUNC_MOD_EXPR
, PREC_MULTIPLICATIVE_EXPRESSION
},
1974 { CPP_PLUS
, PLUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1975 { CPP_MINUS
, MINUS_EXPR
, PREC_ADDITIVE_EXPRESSION
},
1977 { CPP_LSHIFT
, LSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1978 { CPP_RSHIFT
, RSHIFT_EXPR
, PREC_SHIFT_EXPRESSION
},
1980 { CPP_SPACESHIP
, SPACESHIP_EXPR
, PREC_SPACESHIP_EXPRESSION
},
1982 { CPP_LESS
, LT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1983 { CPP_GREATER
, GT_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1984 { CPP_LESS_EQ
, LE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1985 { CPP_GREATER_EQ
, GE_EXPR
, PREC_RELATIONAL_EXPRESSION
},
1987 { CPP_EQ_EQ
, EQ_EXPR
, PREC_EQUALITY_EXPRESSION
},
1988 { CPP_NOT_EQ
, NE_EXPR
, PREC_EQUALITY_EXPRESSION
},
1990 { CPP_AND
, BIT_AND_EXPR
, PREC_AND_EXPRESSION
},
1992 { CPP_XOR
, BIT_XOR_EXPR
, PREC_EXCLUSIVE_OR_EXPRESSION
},
1994 { CPP_OR
, BIT_IOR_EXPR
, PREC_INCLUSIVE_OR_EXPRESSION
},
1996 { CPP_AND_AND
, TRUTH_ANDIF_EXPR
, PREC_LOGICAL_AND_EXPRESSION
},
1998 { CPP_OR_OR
, TRUTH_ORIF_EXPR
, PREC_LOGICAL_OR_EXPRESSION
}
2001 /* The same as binops, but initialized by cp_parser_new so that
2002 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
2004 static cp_parser_binary_operations_map_node binops_by_token
[N_CP_TTYPES
];
2006 /* Constructors and destructors. */
2008 /* Construct a new context. The context below this one on the stack
2009 is given by NEXT. */
2011 static cp_parser_context
*
2012 cp_parser_context_new (cp_parser_context
* next
)
2014 cp_parser_context
*context
;
2016 /* Allocate the storage. */
2017 if (cp_parser_context_free_list
!= NULL
)
2019 /* Pull the first entry from the free list. */
2020 context
= cp_parser_context_free_list
;
2021 cp_parser_context_free_list
= context
->next
;
2022 memset (context
, 0, sizeof (*context
));
2025 context
= ggc_cleared_alloc
<cp_parser_context
> ();
2027 /* No errors have occurred yet in this context. */
2028 context
->status
= CP_PARSER_STATUS_KIND_NO_ERROR
;
2029 /* If this is not the bottommost context, copy information that we
2030 need from the previous context. */
2033 /* If, in the NEXT context, we are parsing an `x->' or `x.'
2034 expression, then we are parsing one in this context, too. */
2035 context
->object_type
= next
->object_type
;
2036 /* Thread the stack. */
2037 context
->next
= next
;
2043 /* Managing the unparsed function queues. */
2045 #define unparsed_funs_with_default_args \
2046 parser->unparsed_queues->last ().funs_with_default_args
2047 #define unparsed_funs_with_definitions \
2048 parser->unparsed_queues->last ().funs_with_definitions
2049 #define unparsed_nsdmis \
2050 parser->unparsed_queues->last ().nsdmis
2051 #define unparsed_noexcepts \
2052 parser->unparsed_queues->last ().noexcepts
2055 push_unparsed_function_queues (cp_parser
*parser
)
2057 cp_unparsed_functions_entry e
= { NULL
, make_tree_vector (), NULL
, NULL
};
2058 vec_safe_push (parser
->unparsed_queues
, e
);
2062 pop_unparsed_function_queues (cp_parser
*parser
)
2064 release_tree_vector (unparsed_funs_with_definitions
);
2065 parser
->unparsed_queues
->pop ();
2070 /* Constructors and destructors. */
2072 static cp_parser
*cp_parser_new
2075 /* Routines to parse various constructs.
2077 Those that return `tree' will return the error_mark_node (rather
2078 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2079 Sometimes, they will return an ordinary node if error-recovery was
2080 attempted, even though a parse error occurred. So, to check
2081 whether or not a parse error occurred, you should always use
2082 cp_parser_error_occurred. If the construct is optional (indicated
2083 either by an `_opt' in the name of the function that does the
2084 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2085 the construct is not present. */
2087 /* Lexical conventions [gram.lex] */
2089 static cp_expr cp_parser_identifier
2091 static cp_expr cp_parser_string_literal
2092 (cp_parser
*, bool, bool, bool);
2093 static cp_expr cp_parser_userdef_char_literal
2095 static tree cp_parser_userdef_string_literal
2097 static cp_expr cp_parser_userdef_numeric_literal
2100 /* Basic concepts [gram.basic] */
2102 static void cp_parser_translation_unit (cp_parser
*);
2104 /* Expressions [gram.expr] */
2106 static cp_expr cp_parser_primary_expression
2107 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2108 static cp_expr cp_parser_id_expression
2109 (cp_parser
*, bool, bool, bool *, bool, bool);
2110 static cp_expr cp_parser_unqualified_id
2111 (cp_parser
*, bool, bool, bool, bool);
2112 static tree cp_parser_nested_name_specifier_opt
2113 (cp_parser
*, bool, bool, bool, bool, bool = false);
2114 static tree cp_parser_nested_name_specifier
2115 (cp_parser
*, bool, bool, bool, bool);
2116 static tree cp_parser_qualifying_entity
2117 (cp_parser
*, bool, bool, bool, bool, bool);
2118 static cp_expr cp_parser_postfix_expression
2119 (cp_parser
*, bool, bool, bool, bool, cp_id_kind
*);
2120 static tree cp_parser_postfix_open_square_expression
2121 (cp_parser
*, tree
, bool, bool);
2122 static tree cp_parser_postfix_dot_deref_expression
2123 (cp_parser
*, enum cpp_ttype
, cp_expr
, bool, cp_id_kind
*, location_t
);
2124 static vec
<tree
, va_gc
> *cp_parser_parenthesized_expression_list
2125 (cp_parser
*, int, bool, bool, bool *, location_t
* = NULL
,
2127 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2128 enum { non_attr
= 0, normal_attr
= 1, id_attr
= 2 };
2129 static void cp_parser_pseudo_destructor_name
2130 (cp_parser
*, tree
, tree
*, tree
*);
2131 static cp_expr cp_parser_unary_expression
2132 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2133 static enum tree_code cp_parser_unary_operator
2135 static tree cp_parser_has_attribute_expression
2137 static tree cp_parser_new_expression
2139 static vec
<tree
, va_gc
> *cp_parser_new_placement
2141 static tree cp_parser_new_type_id
2142 (cp_parser
*, tree
*);
2143 static cp_declarator
*cp_parser_new_declarator_opt
2145 static cp_declarator
*cp_parser_direct_new_declarator
2147 static vec
<tree
, va_gc
> *cp_parser_new_initializer
2149 static tree cp_parser_delete_expression
2151 static cp_expr cp_parser_cast_expression
2152 (cp_parser
*, bool, bool, bool, cp_id_kind
*);
2153 static cp_expr cp_parser_binary_expression
2154 (cp_parser
*, bool, bool, enum cp_parser_prec
, cp_id_kind
*);
2155 static tree cp_parser_question_colon_clause
2156 (cp_parser
*, cp_expr
);
2157 static cp_expr cp_parser_assignment_expression
2158 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false);
2159 static enum tree_code cp_parser_assignment_operator_opt
2161 static cp_expr cp_parser_expression
2162 (cp_parser
*, cp_id_kind
* = NULL
, bool = false, bool = false, bool = false);
2163 static cp_expr cp_parser_constant_expression
2164 (cp_parser
*, bool = false, bool * = NULL
, bool = false);
2165 static cp_expr cp_parser_builtin_offsetof
2167 static cp_expr cp_parser_lambda_expression
2169 static void cp_parser_lambda_introducer
2170 (cp_parser
*, tree
);
2171 static bool cp_parser_lambda_declarator_opt
2172 (cp_parser
*, tree
);
2173 static void cp_parser_lambda_body
2174 (cp_parser
*, tree
);
2176 /* Statements [gram.stmt.stmt] */
2178 static void cp_parser_statement
2179 (cp_parser
*, tree
, bool, bool *, vec
<tree
> * = NULL
, location_t
* = NULL
);
2180 static void cp_parser_label_for_labeled_statement
2181 (cp_parser
*, tree
);
2182 static tree cp_parser_expression_statement
2183 (cp_parser
*, tree
);
2184 static tree cp_parser_compound_statement
2185 (cp_parser
*, tree
, int, bool);
2186 static void cp_parser_statement_seq_opt
2187 (cp_parser
*, tree
);
2188 static tree cp_parser_selection_statement
2189 (cp_parser
*, bool *, vec
<tree
> *);
2190 static tree cp_parser_condition
2192 static tree cp_parser_iteration_statement
2193 (cp_parser
*, bool *, bool, unsigned short);
2194 static bool cp_parser_init_statement
2195 (cp_parser
*, tree
*decl
);
2196 static tree cp_parser_for
2197 (cp_parser
*, bool, unsigned short);
2198 static tree cp_parser_c_for
2199 (cp_parser
*, tree
, tree
, bool, unsigned short);
2200 static tree cp_parser_range_for
2201 (cp_parser
*, tree
, tree
, tree
, bool, unsigned short, bool);
2202 static void do_range_for_auto_deduction
2204 static tree cp_parser_perform_range_for_lookup
2205 (tree
, tree
*, tree
*);
2206 static tree cp_parser_range_for_member_function
2208 static tree cp_parser_jump_statement
2210 static void cp_parser_declaration_statement
2213 static tree cp_parser_implicitly_scoped_statement
2214 (cp_parser
*, bool *, const token_indent_info
&, vec
<tree
> * = NULL
);
2215 static void cp_parser_already_scoped_statement
2216 (cp_parser
*, bool *, const token_indent_info
&);
2218 /* State of module-declaration parsing. */
2221 MP_NOT_MODULE
, /* Not a module. */
2225 MP_FIRST
, /* First declaration of TU. */
2226 MP_GLOBAL
, /* Global Module Fragment. */
2228 MP_PURVIEW_IMPORTS
, /* Imports of a module. */
2229 MP_PURVIEW
, /* Purview of a named module. */
2231 MP_PRIVATE_IMPORTS
, /* Imports of a Private Module Fragment. */
2232 MP_PRIVATE
, /* Private Module Fragment. */
2235 static module_parse cp_parser_module_declaration
2236 (cp_parser
*parser
, module_parse
, bool exporting
);
2237 static void cp_parser_import_declaration
2238 (cp_parser
*parser
, module_parse
, bool exporting
);
2240 /* Declarations [gram.dcl.dcl] */
2242 static void cp_parser_declaration_seq_opt
2244 static void cp_parser_declaration
2245 (cp_parser
*, tree
);
2246 static void cp_parser_toplevel_declaration
2248 static void cp_parser_block_declaration
2249 (cp_parser
*, bool);
2250 static void cp_parser_simple_declaration
2251 (cp_parser
*, bool, tree
*);
2252 static void cp_parser_decl_specifier_seq
2253 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, int *);
2254 static tree cp_parser_storage_class_specifier_opt
2256 static tree cp_parser_function_specifier_opt
2257 (cp_parser
*, cp_decl_specifier_seq
*);
2258 static tree cp_parser_type_specifier
2259 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*, bool,
2261 static tree cp_parser_simple_type_specifier
2262 (cp_parser
*, cp_decl_specifier_seq
*, cp_parser_flags
);
2263 static tree cp_parser_placeholder_type_specifier
2264 (cp_parser
*, location_t
, tree
, bool);
2265 static tree cp_parser_type_name
2266 (cp_parser
*, bool);
2267 static tree cp_parser_nonclass_name
2268 (cp_parser
* parser
);
2269 static tree cp_parser_elaborated_type_specifier
2270 (cp_parser
*, bool, bool);
2271 static tree cp_parser_enum_specifier
2273 static void cp_parser_enumerator_list
2274 (cp_parser
*, tree
);
2275 static void cp_parser_enumerator_definition
2276 (cp_parser
*, tree
);
2277 static tree cp_parser_namespace_name
2279 static void cp_parser_namespace_definition
2281 static void cp_parser_namespace_body
2283 static tree cp_parser_qualified_namespace_specifier
2285 static void cp_parser_namespace_alias_definition
2287 static bool cp_parser_using_declaration
2288 (cp_parser
*, bool);
2289 static void cp_parser_using_directive
2291 static void cp_parser_using_enum
2293 static tree cp_parser_alias_declaration
2295 static void cp_parser_asm_definition
2297 static void cp_parser_linkage_specification
2298 (cp_parser
*, tree
);
2299 static void cp_parser_static_assert
2300 (cp_parser
*, bool);
2301 static tree cp_parser_decltype
2303 static tree cp_parser_decomposition_declaration
2304 (cp_parser
*, cp_decl_specifier_seq
*, tree
*, location_t
*);
2306 /* Declarators [gram.dcl.decl] */
2308 static tree cp_parser_init_declarator
2309 (cp_parser
*, cp_parser_flags
, cp_decl_specifier_seq
*,
2310 vec
<deferred_access_check
, va_gc
> *, bool, bool, int, bool *, tree
*,
2311 location_t
*, tree
*);
2312 static cp_declarator
*cp_parser_declarator
2313 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool *,
2315 static cp_declarator
*cp_parser_direct_declarator
2316 (cp_parser
*, cp_parser_declarator_kind
, cp_parser_flags
, int *, bool, bool,
2318 static enum tree_code cp_parser_ptr_operator
2319 (cp_parser
*, tree
*, cp_cv_quals
*, tree
*);
2320 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2322 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2324 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2326 static tree cp_parser_tx_qualifier_opt
2328 static tree cp_parser_late_return_type_opt
2329 (cp_parser
*, cp_declarator
*, tree
&);
2330 static tree cp_parser_declarator_id
2331 (cp_parser
*, bool);
2332 static tree cp_parser_type_id
2333 (cp_parser
*, cp_parser_flags
= CP_PARSER_FLAGS_NONE
, location_t
* = NULL
);
2334 static tree cp_parser_template_type_arg
2336 static tree
cp_parser_trailing_type_id (cp_parser
*);
2337 static tree cp_parser_type_id_1
2338 (cp_parser
*, cp_parser_flags
, bool, bool, location_t
*);
2339 static void cp_parser_type_specifier_seq
2340 (cp_parser
*, cp_parser_flags
, bool, bool, cp_decl_specifier_seq
*);
2341 static tree cp_parser_parameter_declaration_clause
2342 (cp_parser
*, cp_parser_flags
);
2343 static tree cp_parser_parameter_declaration_list
2344 (cp_parser
*, cp_parser_flags
);
2345 static cp_parameter_declarator
*cp_parser_parameter_declaration
2346 (cp_parser
*, cp_parser_flags
, bool, bool *);
2347 static tree cp_parser_default_argument
2348 (cp_parser
*, bool);
2349 static void cp_parser_function_body
2350 (cp_parser
*, bool);
2351 static tree cp_parser_initializer
2352 (cp_parser
*, bool *, bool *, bool = false);
2353 static cp_expr cp_parser_initializer_clause
2354 (cp_parser
*, bool *);
2355 static cp_expr cp_parser_braced_list
2356 (cp_parser
*, bool*);
2357 static vec
<constructor_elt
, va_gc
> *cp_parser_initializer_list
2358 (cp_parser
*, bool *, bool *);
2360 static void cp_parser_ctor_initializer_opt_and_function_body
2361 (cp_parser
*, bool);
2363 static tree cp_parser_late_parsing_omp_declare_simd
2364 (cp_parser
*, tree
);
2366 static tree cp_parser_late_parsing_oacc_routine
2367 (cp_parser
*, tree
);
2369 static tree synthesize_implicit_template_parm
2370 (cp_parser
*, tree
);
2371 static tree finish_fully_implicit_template
2372 (cp_parser
*, tree
);
2373 static void abort_fully_implicit_template
2376 /* Classes [gram.class] */
2378 static tree cp_parser_class_name
2379 (cp_parser
*, bool, bool, enum tag_types
, bool, bool, bool, bool = false);
2380 static tree cp_parser_class_specifier
2382 static tree cp_parser_class_head
2383 (cp_parser
*, bool *);
2384 static enum tag_types cp_parser_class_key
2386 static void cp_parser_type_parameter_key
2387 (cp_parser
* parser
);
2388 static void cp_parser_member_specification_opt
2390 static void cp_parser_member_declaration
2392 static tree cp_parser_pure_specifier
2394 static tree cp_parser_constant_initializer
2397 /* Derived classes [gram.class.derived] */
2399 static tree cp_parser_base_clause
2401 static tree cp_parser_base_specifier
2404 /* Special member functions [gram.special] */
2406 static tree cp_parser_conversion_function_id
2408 static tree cp_parser_conversion_type_id
2410 static cp_declarator
*cp_parser_conversion_declarator_opt
2412 static void cp_parser_ctor_initializer_opt
2414 static void cp_parser_mem_initializer_list
2416 static tree cp_parser_mem_initializer
2418 static tree cp_parser_mem_initializer_id
2421 /* Overloading [gram.over] */
2423 static cp_expr cp_parser_operator_function_id
2425 static cp_expr cp_parser_operator
2426 (cp_parser
*, location_t
);
2428 /* Templates [gram.temp] */
2430 static void cp_parser_template_declaration
2431 (cp_parser
*, bool);
2432 static tree cp_parser_template_parameter_list
2434 static tree cp_parser_template_parameter
2435 (cp_parser
*, bool *, bool *);
2436 static tree cp_parser_type_parameter
2437 (cp_parser
*, bool *);
2438 static tree cp_parser_template_id
2439 (cp_parser
*, bool, bool, enum tag_types
, bool);
2440 static tree cp_parser_template_id_expr
2441 (cp_parser
*, bool, bool, bool);
2442 static tree cp_parser_template_name
2443 (cp_parser
*, bool, bool, bool, enum tag_types
, bool *);
2444 static tree cp_parser_template_argument_list
2446 static tree cp_parser_template_argument
2448 static void cp_parser_explicit_instantiation
2450 static void cp_parser_explicit_specialization
2453 /* Exception handling [gram.except] */
2455 static tree cp_parser_try_block
2457 static void cp_parser_function_try_block
2459 static void cp_parser_handler_seq
2461 static void cp_parser_handler
2463 static tree cp_parser_exception_declaration
2465 static tree cp_parser_throw_expression
2467 static tree cp_parser_exception_specification_opt
2468 (cp_parser
*, cp_parser_flags
);
2469 static tree cp_parser_type_id_list
2471 static tree cp_parser_noexcept_specification_opt
2472 (cp_parser
*, cp_parser_flags
, bool, bool *, bool);
2474 /* GNU Extensions */
2476 static tree cp_parser_asm_specification_opt
2478 static tree cp_parser_asm_operand_list
2480 static tree cp_parser_asm_clobber_list
2482 static tree cp_parser_asm_label_list
2484 static bool cp_next_tokens_can_be_attribute_p
2486 static bool cp_next_tokens_can_be_gnu_attribute_p
2488 static bool cp_next_tokens_can_be_std_attribute_p
2490 static bool cp_nth_tokens_can_be_std_attribute_p
2491 (cp_parser
*, size_t);
2492 static bool cp_nth_tokens_can_be_gnu_attribute_p
2493 (cp_parser
*, size_t);
2494 static bool cp_nth_tokens_can_be_attribute_p
2495 (cp_parser
*, size_t);
2496 static tree cp_parser_attributes_opt
2498 static tree cp_parser_gnu_attributes_opt
2500 static tree cp_parser_gnu_attribute_list
2501 (cp_parser
*, bool = false);
2502 static tree cp_parser_std_attribute
2503 (cp_parser
*, tree
);
2504 static tree cp_parser_std_attribute_spec
2506 static tree cp_parser_std_attribute_spec_seq
2508 static size_t cp_parser_skip_attributes_opt
2509 (cp_parser
*, size_t);
2510 static bool cp_parser_extension_opt
2511 (cp_parser
*, int *);
2512 static void cp_parser_label_declaration
2515 /* Concept Extensions */
2517 static tree cp_parser_concept_definition
2519 static tree cp_parser_constraint_expression
2521 static tree cp_parser_requires_clause_opt
2522 (cp_parser
*, bool);
2523 static tree cp_parser_requires_expression
2525 static tree cp_parser_requirement_parameter_list
2527 static tree cp_parser_requirement_body
2529 static tree cp_parser_requirement_seq
2531 static tree cp_parser_requirement
2533 static tree cp_parser_simple_requirement
2535 static tree cp_parser_compound_requirement
2537 static tree cp_parser_type_requirement
2539 static tree cp_parser_nested_requirement
2542 /* Transactional Memory Extensions */
2544 static tree cp_parser_transaction
2545 (cp_parser
*, cp_token
*);
2546 static tree cp_parser_transaction_expression
2547 (cp_parser
*, enum rid
);
2548 static void cp_parser_function_transaction
2549 (cp_parser
*, enum rid
);
2550 static tree cp_parser_transaction_cancel
2553 /* Coroutine extensions. */
2555 static tree cp_parser_yield_expression
2559 enum pragma_context
{
2566 static bool cp_parser_pragma
2567 (cp_parser
*, enum pragma_context
, bool *);
2569 /* Objective-C++ Productions */
2571 static tree cp_parser_objc_message_receiver
2573 static tree cp_parser_objc_message_args
2575 static tree cp_parser_objc_message_expression
2577 static cp_expr cp_parser_objc_encode_expression
2579 static tree cp_parser_objc_defs_expression
2581 static tree cp_parser_objc_protocol_expression
2583 static tree cp_parser_objc_selector_expression
2585 static cp_expr cp_parser_objc_expression
2587 static bool cp_parser_objc_selector_p
2589 static tree cp_parser_objc_selector
2591 static tree cp_parser_objc_protocol_refs_opt
2593 static void cp_parser_objc_declaration
2594 (cp_parser
*, tree
);
2595 static tree cp_parser_objc_statement
2597 static bool cp_parser_objc_valid_prefix_attributes
2598 (cp_parser
*, tree
*);
2599 static void cp_parser_objc_at_property_declaration
2601 static void cp_parser_objc_at_synthesize_declaration
2603 static void cp_parser_objc_at_dynamic_declaration
2605 static tree cp_parser_objc_struct_declaration
2608 /* Utility Routines */
2610 static cp_expr cp_parser_lookup_name
2611 (cp_parser
*, tree
, enum tag_types
, bool, bool, bool, tree
*, location_t
);
2612 static tree cp_parser_lookup_name_simple
2613 (cp_parser
*, tree
, location_t
);
2614 static tree cp_parser_maybe_treat_template_as_class
2616 static bool cp_parser_check_declarator_template_parameters
2617 (cp_parser
*, cp_declarator
*, location_t
);
2618 static bool cp_parser_check_template_parameters
2619 (cp_parser
*, unsigned, bool, location_t
, cp_declarator
*);
2620 static cp_expr cp_parser_simple_cast_expression
2622 static tree cp_parser_global_scope_opt
2623 (cp_parser
*, bool);
2624 static bool cp_parser_constructor_declarator_p
2625 (cp_parser
*, cp_parser_flags
, bool);
2626 static tree cp_parser_function_definition_from_specifiers_and_declarator
2627 (cp_parser
*, cp_decl_specifier_seq
*, tree
, const cp_declarator
*);
2628 static tree cp_parser_function_definition_after_declarator
2629 (cp_parser
*, bool);
2630 static bool cp_parser_template_declaration_after_export
2631 (cp_parser
*, bool);
2632 static void cp_parser_perform_template_parameter_access_checks
2633 (vec
<deferred_access_check
, va_gc
> *);
2634 static tree cp_parser_single_declaration
2635 (cp_parser
*, vec
<deferred_access_check
, va_gc
> *, bool, bool, bool *);
2636 static cp_expr cp_parser_functional_cast
2637 (cp_parser
*, tree
);
2638 static tree cp_parser_save_member_function_body
2639 (cp_parser
*, cp_decl_specifier_seq
*, cp_declarator
*, tree
);
2640 static tree cp_parser_save_nsdmi
2642 static tree cp_parser_enclosed_template_argument_list
2644 static void cp_parser_save_default_args
2645 (cp_parser
*, tree
);
2646 static void cp_parser_late_parsing_for_member
2647 (cp_parser
*, tree
);
2648 static tree cp_parser_late_parse_one_default_arg
2649 (cp_parser
*, tree
, tree
, tree
);
2650 static void cp_parser_late_parsing_nsdmi
2651 (cp_parser
*, tree
);
2652 static void cp_parser_late_parsing_default_args
2653 (cp_parser
*, tree
);
2654 static tree cp_parser_sizeof_operand
2655 (cp_parser
*, enum rid
);
2656 static cp_expr cp_parser_trait_expr
2657 (cp_parser
*, enum rid
);
2658 static bool cp_parser_declares_only_class_p
2660 static void cp_parser_set_storage_class
2661 (cp_parser
*, cp_decl_specifier_seq
*, enum rid
, cp_token
*);
2662 static void cp_parser_set_decl_spec_type
2663 (cp_decl_specifier_seq
*, tree
, cp_token
*, bool);
2664 static void set_and_check_decl_spec_loc
2665 (cp_decl_specifier_seq
*decl_specs
,
2666 cp_decl_spec ds
, cp_token
*);
2667 static bool cp_parser_friend_p
2668 (const cp_decl_specifier_seq
*);
2669 static void cp_parser_required_error
2670 (cp_parser
*, required_token
, bool, location_t
);
2671 static cp_token
*cp_parser_require
2672 (cp_parser
*, enum cpp_ttype
, required_token
, location_t
= UNKNOWN_LOCATION
);
2673 static cp_token
*cp_parser_require_keyword
2674 (cp_parser
*, enum rid
, required_token
);
2675 static bool cp_parser_token_starts_function_definition_p
2677 static bool cp_parser_next_token_starts_class_definition_p
2679 static bool cp_parser_next_token_ends_template_argument_p
2681 static bool cp_parser_nth_token_starts_template_argument_list_p
2682 (cp_parser
*, size_t);
2683 static enum tag_types cp_parser_token_is_class_key
2685 static enum tag_types cp_parser_token_is_type_parameter_key
2687 static void cp_parser_maybe_warn_enum_key (cp_parser
*, location_t
, tree
, rid
);
2688 static void cp_parser_check_class_key
2689 (cp_parser
*, location_t
, enum tag_types
, tree type
, bool, bool);
2690 static void cp_parser_check_access_in_redeclaration
2691 (tree type
, location_t location
);
2692 static bool cp_parser_optional_template_keyword
2694 static void cp_parser_pre_parsed_nested_name_specifier
2696 static bool cp_parser_cache_group
2697 (cp_parser
*, enum cpp_ttype
, unsigned);
2698 static tree cp_parser_cache_defarg
2699 (cp_parser
*parser
, bool nsdmi
);
2700 static void cp_parser_parse_tentatively
2702 static void cp_parser_commit_to_tentative_parse
2704 static void cp_parser_commit_to_topmost_tentative_parse
2706 static void cp_parser_abort_tentative_parse
2708 static bool cp_parser_parse_definitely
2710 static inline bool cp_parser_parsing_tentatively
2712 static bool cp_parser_uncommitted_to_tentative_parse_p
2714 static void cp_parser_error
2715 (cp_parser
*, const char *);
2716 static void cp_parser_name_lookup_error
2717 (cp_parser
*, tree
, tree
, name_lookup_error
, location_t
);
2718 static bool cp_parser_simulate_error
2720 static bool cp_parser_check_type_definition
2722 static void cp_parser_check_for_definition_in_return_type
2723 (cp_declarator
*, tree
, location_t type_location
);
2724 static void cp_parser_check_for_invalid_template_id
2725 (cp_parser
*, tree
, enum tag_types
, location_t location
);
2726 static bool cp_parser_non_integral_constant_expression
2727 (cp_parser
*, non_integral_constant
);
2728 static void cp_parser_diagnose_invalid_type_name
2729 (cp_parser
*, tree
, location_t
);
2730 static bool cp_parser_parse_and_diagnose_invalid_type_name
2732 static int cp_parser_skip_to_closing_parenthesis
2733 (cp_parser
*, bool, bool, bool);
2734 static void cp_parser_skip_to_end_of_statement
2736 static void cp_parser_consume_semicolon_at_end_of_statement
2738 static void cp_parser_skip_to_end_of_block_or_statement
2740 static bool cp_parser_skip_to_closing_brace
2742 static void cp_parser_skip_to_end_of_template_parameter_list
2744 static void cp_parser_skip_to_pragma_eol
2745 (cp_parser
*, cp_token
*);
2746 static bool cp_parser_error_occurred
2748 static bool cp_parser_allow_gnu_extensions_p
2750 static bool cp_parser_is_pure_string_literal
2752 static bool cp_parser_is_string_literal
2754 static bool cp_parser_is_keyword
2755 (cp_token
*, enum rid
);
2756 static tree cp_parser_make_typename_type
2757 (cp_parser
*, tree
, location_t location
);
2758 static cp_declarator
* cp_parser_make_indirect_declarator
2759 (enum tree_code
, tree
, cp_cv_quals
, cp_declarator
*, tree
);
2760 static bool cp_parser_compound_literal_p
2762 static bool cp_parser_array_designator_p
2764 static bool cp_parser_init_statement_p
2766 static bool cp_parser_skip_to_closing_square_bracket
2768 static size_t cp_parser_skip_balanced_tokens (cp_parser
*, size_t);
2770 // -------------------------------------------------------------------------- //
2771 // Unevaluated Operand Guard
2773 // Implementation of an RAII helper for unevaluated operand parsing.
2774 cp_unevaluated::cp_unevaluated ()
2776 ++cp_unevaluated_operand
;
2777 ++c_inhibit_evaluation_warnings
;
2780 cp_unevaluated::~cp_unevaluated ()
2782 --c_inhibit_evaluation_warnings
;
2783 --cp_unevaluated_operand
;
2786 // -------------------------------------------------------------------------- //
2787 // Tentative Parsing
2789 /* Returns nonzero if we are parsing tentatively. */
2792 cp_parser_parsing_tentatively (cp_parser
* parser
)
2794 return parser
->context
->next
!= NULL
;
2797 /* Returns nonzero if TOKEN is a string literal. */
2800 cp_parser_is_pure_string_literal (cp_token
* token
)
2802 return (token
->type
== CPP_STRING
||
2803 token
->type
== CPP_STRING16
||
2804 token
->type
== CPP_STRING32
||
2805 token
->type
== CPP_WSTRING
||
2806 token
->type
== CPP_UTF8STRING
);
2809 /* Returns nonzero if TOKEN is a string literal
2810 of a user-defined string literal. */
2813 cp_parser_is_string_literal (cp_token
* token
)
2815 return (cp_parser_is_pure_string_literal (token
) ||
2816 token
->type
== CPP_STRING_USERDEF
||
2817 token
->type
== CPP_STRING16_USERDEF
||
2818 token
->type
== CPP_STRING32_USERDEF
||
2819 token
->type
== CPP_WSTRING_USERDEF
||
2820 token
->type
== CPP_UTF8STRING_USERDEF
);
2823 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2826 cp_parser_is_keyword (cp_token
* token
, enum rid keyword
)
2828 return token
->keyword
== keyword
;
2831 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2834 static enum pragma_kind
2835 cp_parser_pragma_kind (cp_token
*token
)
2837 if (token
->type
!= CPP_PRAGMA
)
2839 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2840 return (enum pragma_kind
) TREE_INT_CST_LOW (token
->u
.value
);
2843 /* Helper function for cp_parser_error.
2844 Having peeked a token of kind TOK1_KIND that might signify
2845 a conflict marker, peek successor tokens to determine
2846 if we actually do have a conflict marker.
2847 Specifically, we consider a run of 7 '<', '=' or '>' characters
2848 at the start of a line as a conflict marker.
2849 These come through the lexer as three pairs and a single,
2850 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2851 If it returns true, *OUT_LOC is written to with the location/range
2855 cp_lexer_peek_conflict_marker (cp_lexer
*lexer
, enum cpp_ttype tok1_kind
,
2856 location_t
*out_loc
)
2858 cp_token
*token2
= cp_lexer_peek_nth_token (lexer
, 2);
2859 if (token2
->type
!= tok1_kind
)
2861 cp_token
*token3
= cp_lexer_peek_nth_token (lexer
, 3);
2862 if (token3
->type
!= tok1_kind
)
2864 cp_token
*token4
= cp_lexer_peek_nth_token (lexer
, 4);
2865 if (token4
->type
!= conflict_marker_get_final_tok_kind (tok1_kind
))
2868 /* It must be at the start of the line. */
2869 location_t start_loc
= cp_lexer_peek_token (lexer
)->location
;
2870 if (LOCATION_COLUMN (start_loc
) != 1)
2873 /* We have a conflict marker. Construct a location of the form:
2876 with start == caret, finishing at the end of the marker. */
2877 location_t finish_loc
= get_finish (token4
->location
);
2878 *out_loc
= make_location (start_loc
, start_loc
, finish_loc
);
2883 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
2887 get_matching_symbol (required_token token_desc
)
2894 case RT_CLOSE_BRACE
:
2896 case RT_CLOSE_PAREN
:
2901 /* Attempt to convert TOKEN_DESC from a required_token to an
2902 enum cpp_ttype, returning CPP_EOF if there is no good conversion. */
2904 static enum cpp_ttype
2905 get_required_cpp_ttype (required_token token_desc
)
2910 return CPP_SEMICOLON
;
2912 return CPP_OPEN_PAREN
;
2913 case RT_CLOSE_BRACE
:
2914 return CPP_CLOSE_BRACE
;
2916 return CPP_OPEN_BRACE
;
2917 case RT_CLOSE_SQUARE
:
2918 return CPP_CLOSE_SQUARE
;
2919 case RT_OPEN_SQUARE
:
2920 return CPP_OPEN_SQUARE
;
2925 case RT_CLOSE_PAREN
:
2926 return CPP_CLOSE_PAREN
;
2929 /* Use CPP_EOF as a "no completions possible" code. */
2935 /* Subroutine of cp_parser_error and cp_parser_required_error.
2937 Issue a diagnostic of the form
2938 FILE:LINE: MESSAGE before TOKEN
2939 where TOKEN is the next token in the input stream. MESSAGE
2940 (specified by the caller) is usually of the form "expected
2943 This bypasses the check for tentative passing, and potentially
2944 adds material needed by cp_parser_required_error.
2946 If MISSING_TOKEN_DESC is not RT_NONE, then potentially add fix-it hints
2947 suggesting insertion of the missing token.
2949 Additionally, if MATCHING_LOCATION is not UNKNOWN_LOCATION, then we
2950 have an unmatched symbol at MATCHING_LOCATION; highlight this secondary
2954 cp_parser_error_1 (cp_parser
* parser
, const char* gmsgid
,
2955 required_token missing_token_desc
,
2956 location_t matching_location
)
2958 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
2959 /* This diagnostic makes more sense if it is tagged to the line
2960 of the token we just peeked at. */
2961 cp_lexer_set_source_position_from_token (token
);
2963 if (token
->type
== CPP_PRAGMA
)
2965 error_at (token
->location
,
2966 "%<#pragma%> is not allowed here");
2967 cp_parser_skip_to_pragma_eol (parser
, token
);
2971 /* If this is actually a conflict marker, report it as such. */
2972 if (token
->type
== CPP_LSHIFT
2973 || token
->type
== CPP_RSHIFT
2974 || token
->type
== CPP_EQ_EQ
)
2977 if (cp_lexer_peek_conflict_marker (parser
->lexer
, token
->type
, &loc
))
2979 error_at (loc
, "version control conflict marker in file");
2980 expanded_location token_exploc
= expand_location (token
->location
);
2981 /* Consume tokens until the end of the source line. */
2984 cp_lexer_consume_token (parser
->lexer
);
2985 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
2986 if (next
->type
== CPP_EOF
)
2988 if (next
->location
== UNKNOWN_LOCATION
2989 || loc
== UNKNOWN_LOCATION
)
2992 expanded_location next_exploc
= expand_location (next
->location
);
2993 if (next_exploc
.file
!= token_exploc
.file
)
2995 if (next_exploc
.line
!= token_exploc
.line
)
3002 auto_diagnostic_group d
;
3003 gcc_rich_location
richloc (input_location
);
3005 bool added_matching_location
= false;
3007 if (missing_token_desc
!= RT_NONE
)
3008 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
3010 /* Potentially supply a fix-it hint, suggesting to add the
3011 missing token immediately after the *previous* token.
3012 This may move the primary location within richloc. */
3013 enum cpp_ttype ttype
= get_required_cpp_ttype (missing_token_desc
);
3014 location_t prev_token_loc
= prev_token
->location
;
3015 maybe_suggest_missing_token_insertion (&richloc
, ttype
,
3018 /* If matching_location != UNKNOWN_LOCATION, highlight it.
3019 Attempt to consolidate diagnostics by printing it as a
3020 secondary range within the main diagnostic. */
3021 if (matching_location
!= UNKNOWN_LOCATION
)
3022 added_matching_location
3023 = richloc
.add_location_if_nearby (matching_location
);
3026 /* If we were parsing a string-literal and there is an unknown name
3027 token right after, then check to see if that could also have been
3028 a literal string by checking the name against a list of known
3029 standard string literal constants defined in header files. If
3030 there is one, then add that as an hint to the error message. */
3032 if (token
->type
== CPP_NAME
)
3033 if (cp_token
*prev_token
= cp_lexer_safe_previous_token (parser
->lexer
))
3034 if (cp_parser_is_string_literal (prev_token
))
3036 tree name
= token
->u
.value
;
3037 const char *token_name
= IDENTIFIER_POINTER (name
);
3038 const char *header_hint
3039 = get_cp_stdlib_header_for_string_macro_name (token_name
);
3040 if (header_hint
!= NULL
)
3041 h
= name_hint (NULL
, new suggest_missing_header (token
->location
,
3046 /* Actually emit the error. */
3047 c_parse_error (gmsgid
,
3048 /* Because c_parser_error does not understand
3049 CPP_KEYWORD, keywords are treated like
3051 (token
->type
== CPP_KEYWORD
? CPP_NAME
: token
->type
),
3052 token
->u
.value
, token
->flags
, &richloc
);
3054 if (missing_token_desc
!= RT_NONE
)
3056 /* If we weren't able to consolidate matching_location, then
3057 print it as a secondary diagnostic. */
3058 if (matching_location
!= UNKNOWN_LOCATION
3059 && !added_matching_location
)
3060 inform (matching_location
, "to match this %qs",
3061 get_matching_symbol (missing_token_desc
));
3065 /* If not parsing tentatively, issue a diagnostic of the form
3066 FILE:LINE: MESSAGE before TOKEN
3067 where TOKEN is the next token in the input stream. MESSAGE
3068 (specified by the caller) is usually of the form "expected
3072 cp_parser_error (cp_parser
* parser
, const char* gmsgid
)
3074 if (!cp_parser_simulate_error (parser
))
3075 cp_parser_error_1 (parser
, gmsgid
, RT_NONE
, UNKNOWN_LOCATION
);
3078 /* Issue an error about name-lookup failing. NAME is the
3079 IDENTIFIER_NODE DECL is the result of
3080 the lookup (as returned from cp_parser_lookup_name). DESIRED is
3081 the thing that we hoped to find. */
3084 cp_parser_name_lookup_error (cp_parser
* parser
,
3087 name_lookup_error desired
,
3088 location_t location
)
3090 /* If name lookup completely failed, tell the user that NAME was not
3092 if (decl
== error_mark_node
)
3094 if (parser
->scope
&& parser
->scope
!= global_namespace
)
3095 error_at (location
, "%<%E::%E%> has not been declared",
3096 parser
->scope
, name
);
3097 else if (parser
->scope
== global_namespace
)
3098 error_at (location
, "%<::%E%> has not been declared", name
);
3099 else if (parser
->object_scope
3100 && !CLASS_TYPE_P (parser
->object_scope
))
3101 error_at (location
, "request for member %qE in non-class type %qT",
3102 name
, parser
->object_scope
);
3103 else if (parser
->object_scope
)
3104 error_at (location
, "%<%T::%E%> has not been declared",
3105 parser
->object_scope
, name
);
3107 error_at (location
, "%qE has not been declared", name
);
3109 else if (parser
->scope
&& parser
->scope
!= global_namespace
)
3114 error_at (location
, "%<%E::%E%> is not a type",
3115 parser
->scope
, name
);
3118 error_at (location
, "%<%E::%E%> is not a class or namespace",
3119 parser
->scope
, name
);
3123 "%<%E::%E%> is not a class, namespace, or enumeration",
3124 parser
->scope
, name
);
3131 else if (parser
->scope
== global_namespace
)
3136 error_at (location
, "%<::%E%> is not a type", name
);
3139 error_at (location
, "%<::%E%> is not a class or namespace", name
);
3143 "%<::%E%> is not a class, namespace, or enumeration",
3155 error_at (location
, "%qE is not a type", name
);
3158 error_at (location
, "%qE is not a class or namespace", name
);
3162 "%qE is not a class, namespace, or enumeration", name
);
3170 /* If we are parsing tentatively, remember that an error has occurred
3171 during this tentative parse. Returns true if the error was
3172 simulated; false if a message should be issued by the caller. */
3175 cp_parser_simulate_error (cp_parser
* parser
)
3177 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3179 parser
->context
->status
= CP_PARSER_STATUS_KIND_ERROR
;
3185 /* This function is called when a type is defined. If type
3186 definitions are forbidden at this point, an error message is
3190 cp_parser_check_type_definition (cp_parser
* parser
)
3192 /* If types are forbidden here, issue a message. */
3193 if (parser
->type_definition_forbidden_message
)
3195 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
3196 or %qs in the message need to be interpreted. */
3197 error (parser
->type_definition_forbidden_message
,
3198 parser
->type_definition_forbidden_message_arg
);
3204 /* This function is called when the DECLARATOR is processed. The TYPE
3205 was a type defined in the decl-specifiers. If it is invalid to
3206 define a type in the decl-specifiers for DECLARATOR, an error is
3207 issued. TYPE_LOCATION is the location of TYPE and is used
3208 for error reporting. */
3211 cp_parser_check_for_definition_in_return_type (cp_declarator
*declarator
,
3212 tree type
, location_t type_location
)
3214 /* [dcl.fct] forbids type definitions in return types.
3215 Unfortunately, it's not easy to know whether or not we are
3216 processing a return type until after the fact. */
3218 && (declarator
->kind
== cdk_pointer
3219 || declarator
->kind
== cdk_reference
3220 || declarator
->kind
== cdk_ptrmem
))
3221 declarator
= declarator
->declarator
;
3223 && declarator
->kind
== cdk_function
)
3225 error_at (type_location
,
3226 "new types may not be defined in a return type");
3227 inform (type_location
,
3228 "(perhaps a semicolon is missing after the definition of %qT)",
3233 /* A type-specifier (TYPE) has been parsed which cannot be followed by
3234 "<" in any valid C++ program. If the next token is indeed "<",
3235 issue a message warning the user about what appears to be an
3236 invalid attempt to form a template-id. LOCATION is the location
3237 of the type-specifier (TYPE) */
3240 cp_parser_check_for_invalid_template_id (cp_parser
* parser
,
3242 enum tag_types tag_type
,
3243 location_t location
)
3245 cp_token_position start
= 0;
3247 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3249 if (TREE_CODE (type
) == TYPE_DECL
)
3250 type
= TREE_TYPE (type
);
3251 if (TYPE_P (type
) && !template_placeholder_p (type
))
3252 error_at (location
, "%qT is not a template", type
);
3253 else if (identifier_p (type
))
3255 if (tag_type
!= none_type
)
3256 error_at (location
, "%qE is not a class template", type
);
3258 error_at (location
, "%qE is not a template", type
);
3261 error_at (location
, "invalid template-id");
3262 /* Remember the location of the invalid "<". */
3263 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
3264 start
= cp_lexer_token_position (parser
->lexer
, true);
3265 /* Consume the "<". */
3266 cp_lexer_consume_token (parser
->lexer
);
3267 /* Parse the template arguments. */
3268 cp_parser_enclosed_template_argument_list (parser
);
3269 /* Permanently remove the invalid template arguments so that
3270 this error message is not issued again. */
3272 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
3276 /* If parsing an integral constant-expression, issue an error message
3277 about the fact that THING appeared and return true. Otherwise,
3278 return false. In either case, set
3279 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3282 cp_parser_non_integral_constant_expression (cp_parser
*parser
,
3283 non_integral_constant thing
)
3285 parser
->non_integral_constant_expression_p
= true;
3286 if (parser
->integral_constant_expression_p
)
3288 if (!parser
->allow_non_integral_constant_expression_p
)
3290 const char *msg
= NULL
;
3294 pedwarn (input_location
, OPT_Wpedantic
,
3295 "ISO C++ forbids using a floating-point literal "
3296 "in a constant-expression");
3299 error ("a cast to a type other than an integral or "
3300 "enumeration type cannot appear in a "
3301 "constant-expression");
3304 error ("%<typeid%> operator "
3305 "cannot appear in a constant-expression");
3308 error ("non-constant compound literals "
3309 "cannot appear in a constant-expression");
3312 error ("a function call "
3313 "cannot appear in a constant-expression");
3316 error ("an increment "
3317 "cannot appear in a constant-expression");
3320 error ("an decrement "
3321 "cannot appear in a constant-expression");
3324 error ("an array reference "
3325 "cannot appear in a constant-expression");
3327 case NIC_ADDR_LABEL
:
3328 error ("the address of a label "
3329 "cannot appear in a constant-expression");
3331 case NIC_OVERLOADED
:
3332 error ("calls to overloaded operators "
3333 "cannot appear in a constant-expression");
3335 case NIC_ASSIGNMENT
:
3336 error ("an assignment cannot appear in a constant-expression");
3339 error ("a comma operator "
3340 "cannot appear in a constant-expression");
3342 case NIC_CONSTRUCTOR
:
3343 error ("a call to a constructor "
3344 "cannot appear in a constant-expression");
3346 case NIC_TRANSACTION
:
3347 error ("a transaction expression "
3348 "cannot appear in a constant-expression");
3354 msg
= "__FUNCTION__";
3356 case NIC_PRETTY_FUNC
:
3357 msg
= "__PRETTY_FUNCTION__";
3377 case NIC_PREINCREMENT
:
3380 case NIC_PREDECREMENT
:
3393 error ("%qs cannot appear in a constant-expression", msg
);
3400 /* Emit a diagnostic for an invalid type name. This function commits
3401 to the current active tentative parse, if any. (Otherwise, the
3402 problematic construct might be encountered again later, resulting
3403 in duplicate error messages.) LOCATION is the location of ID. */
3406 cp_parser_diagnose_invalid_type_name (cp_parser
*parser
, tree id
,
3407 location_t location
)
3409 tree decl
, ambiguous_decls
;
3410 cp_parser_commit_to_tentative_parse (parser
);
3411 /* Try to lookup the identifier. */
3412 decl
= cp_parser_lookup_name (parser
, id
, none_type
,
3413 /*is_template=*/false,
3414 /*is_namespace=*/false,
3415 /*check_dependency=*/true,
3416 &ambiguous_decls
, location
);
3417 if (ambiguous_decls
)
3418 /* If the lookup was ambiguous, an error will already have
3421 /* If the lookup found a template-name, it means that the user forgot
3422 to specify an argument list. Emit a useful error message. */
3423 if (DECL_TYPE_TEMPLATE_P (decl
))
3425 auto_diagnostic_group d
;
3427 "invalid use of template-name %qE without an argument list",
3429 if (DECL_CLASS_TEMPLATE_P (decl
) && cxx_dialect
< cxx17
)
3430 inform (location
, "class template argument deduction is only available "
3431 "with %<-std=c++17%> or %<-std=gnu++17%>");
3432 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3434 else if (TREE_CODE (id
) == BIT_NOT_EXPR
)
3435 error_at (location
, "invalid use of destructor %qD as a type", id
);
3436 else if (TREE_CODE (decl
) == TYPE_DECL
)
3437 /* Something like 'unsigned A a;' */
3438 error_at (location
, "invalid combination of multiple type-specifiers");
3439 else if (!parser
->scope
)
3441 /* Issue an error message. */
3442 auto_diagnostic_group d
;
3444 if (TREE_CODE (id
) == IDENTIFIER_NODE
)
3445 hint
= lookup_name_fuzzy (id
, FUZZY_LOOKUP_TYPENAME
, location
);
3446 if (const char *suggestion
= hint
.suggestion ())
3448 gcc_rich_location
richloc (location
);
3449 richloc
.add_fixit_replace (suggestion
);
3451 "%qE does not name a type; did you mean %qs?",
3455 error_at (location
, "%qE does not name a type", id
);
3456 /* If we're in a template class, it's possible that the user was
3457 referring to a type from a base class. For example:
3459 template <typename T> struct A { typedef T X; };
3460 template <typename T> struct B : public A<T> { X x; };
3462 The user should have said "typename A<T>::X". */
3463 if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_CONSTEXPR
])
3464 inform (location
, "C++11 %<constexpr%> only available with "
3465 "%<-std=c++11%> or %<-std=gnu++11%>");
3466 else if (cxx_dialect
< cxx11
&& id
== ridpointers
[(int)RID_NOEXCEPT
])
3467 inform (location
, "C++11 %<noexcept%> only available with "
3468 "%<-std=c++11%> or %<-std=gnu++11%>");
3469 else if (TREE_CODE (id
) == IDENTIFIER_NODE
3470 && (id_equal (id
, "module") || id_equal (id
, "import")))
3473 inform (location
, "%qE only available with %<-fmodules-ts%>", id
);
3475 inform (location
, "%qE was not recognized as a module control-line",
3478 else if (cxx_dialect
< cxx11
3479 && TREE_CODE (id
) == IDENTIFIER_NODE
3480 && id_equal (id
, "thread_local"))
3481 inform (location
, "C++11 %<thread_local%> only available with "
3482 "%<-std=c++11%> or %<-std=gnu++11%>");
3483 else if (cxx_dialect
< cxx20
&& id
== ridpointers
[(int)RID_CONSTINIT
])
3484 inform (location
, "C++20 %<constinit%> only available with "
3485 "%<-std=c++20%> or %<-std=gnu++20%>");
3486 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_CONCEPT
])
3487 inform (location
, "%<concept%> only available with %<-std=c++20%> or "
3489 else if (!flag_concepts
&& id
== ridpointers
[(int)RID_REQUIRES
])
3490 inform (location
, "%<requires%> only available with %<-std=c++20%> or "
3492 else if (processing_template_decl
&& current_class_type
3493 && TYPE_BINFO (current_class_type
))
3495 for (tree b
= TREE_CHAIN (TYPE_BINFO (current_class_type
));
3496 b
; b
= TREE_CHAIN (b
))
3498 tree base_type
= BINFO_TYPE (b
);
3499 if (CLASS_TYPE_P (base_type
)
3500 && dependent_type_p (base_type
))
3502 /* Go from a particular instantiation of the
3503 template (which will have an empty TYPE_FIELDs),
3504 to the main version. */
3505 base_type
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type
);
3506 for (tree field
= TYPE_FIELDS (base_type
);
3507 field
; field
= DECL_CHAIN (field
))
3508 if (TREE_CODE (field
) == TYPE_DECL
3509 && DECL_NAME (field
) == id
)
3512 "(perhaps %<typename %T::%E%> was intended)",
3513 BINFO_TYPE (b
), id
);
3521 /* Here we diagnose qualified-ids where the scope is actually correct,
3522 but the identifier does not resolve to a valid type name. */
3523 else if (parser
->scope
!= error_mark_node
)
3525 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
3527 auto_diagnostic_group d
;
3529 if (decl
== error_mark_node
)
3530 hint
= suggest_alternative_in_explicit_scope (location
, id
,
3532 const char *suggestion
= hint
.suggestion ();
3533 gcc_rich_location
richloc (location_of (id
));
3535 richloc
.add_fixit_replace (suggestion
);
3536 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3540 "%qE in namespace %qE does not name a template"
3541 " type; did you mean %qs?",
3542 id
, parser
->scope
, suggestion
);
3545 "%qE in namespace %qE does not name a template type",
3548 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3552 "%qE in namespace %qE does not name a template"
3553 " type; did you mean %qs?",
3554 TREE_OPERAND (id
, 0), parser
->scope
, suggestion
);
3557 "%qE in namespace %qE does not name a template"
3559 TREE_OPERAND (id
, 0), parser
->scope
);
3565 "%qE in namespace %qE does not name a type"
3566 "; did you mean %qs?",
3567 id
, parser
->scope
, suggestion
);
3570 "%qE in namespace %qE does not name a type",
3574 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3576 else if (CLASS_TYPE_P (parser
->scope
)
3577 && constructor_name_p (id
, parser
->scope
))
3580 auto_diagnostic_group d
;
3581 error_at (location
, "%<%T::%E%> names the constructor, not"
3582 " the type", parser
->scope
, id
);
3583 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3584 error_at (location
, "and %qT has no template constructors",
3587 else if (TYPE_P (parser
->scope
)
3588 && dependent_scope_p (parser
->scope
))
3590 gcc_rich_location
richloc (location
);
3591 richloc
.add_fixit_insert_before ("typename ");
3592 if (TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
3594 "need %<typename%> before %<%T::%D::%E%> because "
3595 "%<%T::%D%> is a dependent scope",
3596 TYPE_CONTEXT (parser
->scope
),
3597 TYPENAME_TYPE_FULLNAME (parser
->scope
),
3599 TYPE_CONTEXT (parser
->scope
),
3600 TYPENAME_TYPE_FULLNAME (parser
->scope
));
3602 error_at (&richloc
, "need %<typename%> before %<%T::%E%> because "
3603 "%qT is a dependent scope",
3604 parser
->scope
, id
, parser
->scope
);
3606 else if (TYPE_P (parser
->scope
))
3608 auto_diagnostic_group d
;
3609 if (!COMPLETE_TYPE_P (parser
->scope
))
3610 cxx_incomplete_type_error (location_of (id
), NULL_TREE
,
3612 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
3613 error_at (location_of (id
),
3614 "%qE in %q#T does not name a template type",
3616 else if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
)
3617 error_at (location_of (id
),
3618 "%qE in %q#T does not name a template type",
3619 TREE_OPERAND (id
, 0), parser
->scope
);
3621 error_at (location_of (id
),
3622 "%qE in %q#T does not name a type",
3625 inform (DECL_SOURCE_LOCATION (decl
), "%qD declared here", decl
);
3632 /* Check for a common situation where a type-name should be present,
3633 but is not, and issue a sensible error message. Returns true if an
3634 invalid type-name was detected.
3636 The situation handled by this function are variable declarations of the
3637 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3638 Usually, `ID' should name a type, but if we got here it means that it
3639 does not. We try to emit the best possible error message depending on
3640 how exactly the id-expression looks like. */
3643 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser
*parser
)
3646 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3648 /* Avoid duplicate error about ambiguous lookup. */
3649 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
3651 cp_token
*next
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
3652 if (next
->type
== CPP_NAME
&& next
->error_reported
)
3656 cp_parser_parse_tentatively (parser
);
3657 id
= cp_parser_id_expression (parser
,
3658 /*template_keyword_p=*/false,
3659 /*check_dependency_p=*/true,
3660 /*template_p=*/NULL
,
3661 /*declarator_p=*/false,
3662 /*optional_p=*/false);
3663 /* If the next token is a (, this is a function with no explicit return
3664 type, i.e. constructor, destructor or conversion op. */
3665 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
3666 || TREE_CODE (id
) == TYPE_DECL
)
3668 cp_parser_abort_tentative_parse (parser
);
3671 if (!cp_parser_parse_definitely (parser
))
3674 /* Emit a diagnostic for the invalid type. */
3675 cp_parser_diagnose_invalid_type_name (parser
, id
, token
->location
);
3677 /* If we aren't in the middle of a declarator (i.e. in a
3678 parameter-declaration-clause), skip to the end of the declaration;
3679 there's no point in trying to process it. */
3680 if (!parser
->in_declarator_p
)
3681 cp_parser_skip_to_end_of_block_or_statement (parser
);
3685 /* Consume tokens up to, and including, the next non-nested closing `)'.
3686 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3687 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3688 found an unnested token of that type. */
3691 cp_parser_skip_to_closing_parenthesis_1 (cp_parser
*parser
,
3696 unsigned paren_depth
= 0;
3697 unsigned brace_depth
= 0;
3698 unsigned square_depth
= 0;
3699 unsigned condop_depth
= 0;
3701 if (recovering
&& or_ttype
== CPP_EOF
3702 && cp_parser_uncommitted_to_tentative_parse_p (parser
))
3707 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
3709 /* Have we found what we're looking for before the closing paren? */
3710 if (token
->type
== or_ttype
&& or_ttype
!= CPP_EOF
3711 && !brace_depth
&& !paren_depth
&& !square_depth
&& !condop_depth
)
3714 switch (token
->type
)
3716 case CPP_PRAGMA_EOL
:
3717 if (!parser
->lexer
->in_pragma
)
3721 /* If we've run out of tokens, then there is no closing `)'. */
3724 /* This is good for lambda expression capture-lists. */
3725 case CPP_OPEN_SQUARE
:
3728 case CPP_CLOSE_SQUARE
:
3729 if (!square_depth
--)
3734 /* This matches the processing in skip_to_end_of_statement. */
3739 case CPP_OPEN_BRACE
:
3742 case CPP_CLOSE_BRACE
:
3747 case CPP_OPEN_PAREN
:
3752 case CPP_CLOSE_PAREN
:
3753 if (!brace_depth
&& !paren_depth
--)
3756 cp_lexer_consume_token (parser
->lexer
);
3762 if (!brace_depth
&& !paren_depth
&& !square_depth
)
3767 if (!brace_depth
&& !paren_depth
&& !square_depth
&& condop_depth
> 0)
3772 if (token
->keyword
!= RID__EXPORT
3773 && token
->keyword
!= RID__MODULE
3774 && token
->keyword
!= RID__IMPORT
)
3779 /* We fell into a pragma. Skip it, and continue. */
3780 cp_parser_skip_to_pragma_eol (parser
, recovering
? token
: nullptr);
3787 /* Consume the token. */
3788 cp_lexer_consume_token (parser
->lexer
);
3792 /* Consume tokens up to, and including, the next non-nested closing `)'.
3793 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3794 are doing error recovery. Returns -1 if OR_COMMA is true and we
3795 found an unnested token of that type. */
3798 cp_parser_skip_to_closing_parenthesis (cp_parser
*parser
,
3803 cpp_ttype ttype
= or_comma
? CPP_COMMA
: CPP_EOF
;
3804 return cp_parser_skip_to_closing_parenthesis_1 (parser
, recovering
,
3805 ttype
, consume_paren
);
3808 /* Consume tokens until we reach the end of the current statement.
3809 Normally, that will be just before consuming a `;'. However, if a
3810 non-nested `}' comes first, then we stop before consuming that. */
3813 cp_parser_skip_to_end_of_statement (cp_parser
* parser
)
3815 unsigned nesting_depth
= 0;
3817 /* Unwind generic function template scope if necessary. */
3818 if (parser
->fully_implicit_function_template_p
)
3819 abort_fully_implicit_template (parser
);
3823 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3825 switch (token
->type
)
3827 case CPP_PRAGMA_EOL
:
3828 if (!parser
->lexer
->in_pragma
)
3832 /* If we've run out of tokens, stop. */
3836 /* If the next token is a `;', we have reached the end of the
3842 case CPP_CLOSE_BRACE
:
3843 /* If this is a non-nested '}', stop before consuming it.
3844 That way, when confronted with something like:
3848 we stop before consuming the closing '}', even though we
3849 have not yet reached a `;'. */
3850 if (nesting_depth
== 0)
3853 /* If it is the closing '}' for a block that we have
3854 scanned, stop -- but only after consuming the token.
3860 we will stop after the body of the erroneously declared
3861 function, but before consuming the following `typedef'
3863 if (--nesting_depth
== 0)
3865 cp_lexer_consume_token (parser
->lexer
);
3870 case CPP_OPEN_BRACE
:
3875 if (token
->keyword
!= RID__EXPORT
3876 && token
->keyword
!= RID__MODULE
3877 && token
->keyword
!= RID__IMPORT
)
3882 /* We fell into a pragma. Skip it, and continue or return. */
3883 cp_parser_skip_to_pragma_eol (parser
, token
);
3892 /* Consume the token. */
3893 cp_lexer_consume_token (parser
->lexer
);
3897 /* This function is called at the end of a statement or declaration.
3898 If the next token is a semicolon, it is consumed; otherwise, error
3899 recovery is attempted. */
3902 cp_parser_consume_semicolon_at_end_of_statement (cp_parser
*parser
)
3904 /* Look for the trailing `;'. */
3905 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
3907 /* If there is additional (erroneous) input, skip to the end of
3909 cp_parser_skip_to_end_of_statement (parser
);
3910 /* If the next token is now a `;', consume it. */
3911 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
3912 cp_lexer_consume_token (parser
->lexer
);
3916 /* Skip tokens until we have consumed an entire block, or until we
3917 have consumed a non-nested `;'. */
3920 cp_parser_skip_to_end_of_block_or_statement (cp_parser
* parser
)
3922 int nesting_depth
= 0;
3924 /* Unwind generic function template scope if necessary. */
3925 if (parser
->fully_implicit_function_template_p
)
3926 abort_fully_implicit_template (parser
);
3928 while (nesting_depth
>= 0)
3930 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3932 switch (token
->type
)
3934 case CPP_PRAGMA_EOL
:
3935 if (!parser
->lexer
->in_pragma
)
3939 /* If we've run out of tokens, stop. */
3943 /* Stop if this is an unnested ';'. */
3948 case CPP_CLOSE_BRACE
:
3949 /* Stop if this is an unnested '}', or closes the outermost
3952 if (nesting_depth
< 0)
3958 case CPP_OPEN_BRACE
:
3964 if (token
->keyword
!= RID__EXPORT
3965 && token
->keyword
!= RID__MODULE
3966 && token
->keyword
!= RID__IMPORT
)
3971 /* Skip it, and continue or return. */
3972 cp_parser_skip_to_pragma_eol (parser
, token
);
3981 /* Consume the token. */
3982 cp_lexer_consume_token (parser
->lexer
);
3986 /* Skip tokens until a non-nested closing curly brace is the next
3987 token, or there are no more tokens. Return true in the first case,
3991 cp_parser_skip_to_closing_brace (cp_parser
*parser
)
3993 unsigned nesting_depth
= 0;
3997 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
3999 switch (token
->type
)
4001 case CPP_PRAGMA_EOL
:
4002 if (!parser
->lexer
->in_pragma
)
4006 /* If we've run out of tokens, stop. */
4009 case CPP_CLOSE_BRACE
:
4010 /* If the next token is a non-nested `}', then we have reached
4011 the end of the current block. */
4012 if (nesting_depth
-- == 0)
4016 case CPP_OPEN_BRACE
:
4017 /* If it the next token is a `{', then we are entering a new
4018 block. Consume the entire block. */
4026 /* Consume the token. */
4027 cp_lexer_consume_token (parser
->lexer
);
4031 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
4032 parameter is the PRAGMA token, allowing us to purge the entire pragma
4033 sequence. PRAGMA_TOK can be NULL, if we're speculatively scanning
4034 forwards (not error recovery). */
4037 cp_parser_skip_to_pragma_eol (cp_parser
* parser
, cp_token
*pragma_tok
)
4043 /* The preprocessor makes sure that a PRAGMA_EOL token appears
4044 before an EOF token, even when the EOF is on the pragma line.
4045 We should never get here without being inside a deferred
4047 gcc_checking_assert (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
));
4048 token
= cp_lexer_consume_token (parser
->lexer
);
4050 while (token
->type
!= CPP_PRAGMA_EOL
);
4054 /* Ensure that the pragma is not parsed again. */
4055 cp_lexer_purge_tokens_after (parser
->lexer
, pragma_tok
);
4056 parser
->lexer
->in_pragma
= false;
4060 /* Require pragma end of line, resyncing with it as necessary. The
4061 arguments are as for cp_parser_skip_to_pragma_eol. */
4064 cp_parser_require_pragma_eol (cp_parser
*parser
, cp_token
*pragma_tok
)
4066 parser
->lexer
->in_pragma
= false;
4067 if (!cp_parser_require (parser
, CPP_PRAGMA_EOL
, RT_PRAGMA_EOL
))
4068 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
4071 /* This is a simple wrapper around make_typename_type. When the id is
4072 an unresolved identifier node, we can provide a superior diagnostic
4073 using cp_parser_diagnose_invalid_type_name. */
4076 cp_parser_make_typename_type (cp_parser
*parser
, tree id
,
4077 location_t id_location
)
4080 if (identifier_p (id
))
4082 result
= make_typename_type (parser
->scope
, id
, typename_type
,
4083 /*complain=*/tf_none
);
4084 if (result
== error_mark_node
)
4085 cp_parser_diagnose_invalid_type_name (parser
, id
, id_location
);
4088 return make_typename_type (parser
->scope
, id
, typename_type
, tf_error
);
4091 /* This is a wrapper around the
4092 make_{pointer,ptrmem,reference}_declarator functions that decides
4093 which one to call based on the CODE and CLASS_TYPE arguments. The
4094 CODE argument should be one of the values returned by
4095 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
4096 appertain to the pointer or reference. */
4098 static cp_declarator
*
4099 cp_parser_make_indirect_declarator (enum tree_code code
, tree class_type
,
4100 cp_cv_quals cv_qualifiers
,
4101 cp_declarator
*target
,
4104 if (code
== ERROR_MARK
|| target
== cp_error_declarator
)
4105 return cp_error_declarator
;
4107 if (code
== INDIRECT_REF
)
4108 if (class_type
== NULL_TREE
)
4109 return make_pointer_declarator (cv_qualifiers
, target
, attributes
);
4111 return make_ptrmem_declarator (cv_qualifiers
, class_type
,
4112 target
, attributes
);
4113 else if (code
== ADDR_EXPR
&& class_type
== NULL_TREE
)
4114 return make_reference_declarator (cv_qualifiers
, target
,
4116 else if (code
== NON_LVALUE_EXPR
&& class_type
== NULL_TREE
)
4117 return make_reference_declarator (cv_qualifiers
, target
,
4122 /* Create a new C++ parser. */
4125 cp_parser_new (cp_lexer
*lexer
)
4127 /* Initialize the binops_by_token so that we can get the tree
4128 directly from the token. */
4129 for (unsigned i
= 0; i
< sizeof (binops
) / sizeof (binops
[0]); i
++)
4130 binops_by_token
[binops
[i
].token_type
] = binops
[i
];
4132 cp_parser
*parser
= ggc_cleared_alloc
<cp_parser
> ();
4133 parser
->lexer
= lexer
;
4134 parser
->context
= cp_parser_context_new (NULL
);
4136 /* For now, we always accept GNU extensions. */
4137 parser
->allow_gnu_extensions_p
= 1;
4139 /* The `>' token is a greater-than operator, not the end of a
4141 parser
->greater_than_is_operator_p
= true;
4143 parser
->default_arg_ok_p
= true;
4145 /* We are not parsing a constant-expression. */
4146 parser
->integral_constant_expression_p
= false;
4147 parser
->allow_non_integral_constant_expression_p
= false;
4148 parser
->non_integral_constant_expression_p
= false;
4150 /* Local variable names are not forbidden. */
4151 parser
->local_variables_forbidden_p
= 0;
4153 /* We are not processing an `extern "C"' declaration. */
4154 parser
->in_unbraced_linkage_specification_p
= false;
4156 /* We are not processing a declarator. */
4157 parser
->in_declarator_p
= false;
4159 /* We are not processing a template-argument-list. */
4160 parser
->in_template_argument_list_p
= false;
4162 /* We are not in an iteration statement. */
4163 parser
->in_statement
= 0;
4165 /* We are not in a switch statement. */
4166 parser
->in_switch_statement_p
= false;
4168 /* We are not parsing a type-id inside an expression. */
4169 parser
->in_type_id_in_expr_p
= false;
4171 /* String literals should be translated to the execution character set. */
4172 parser
->translate_strings_p
= true;
4174 /* We are not parsing a function body. */
4175 parser
->in_function_body
= false;
4177 /* We can correct until told otherwise. */
4178 parser
->colon_corrects_to_scope_p
= true;
4180 /* The unparsed function queue is empty. */
4181 push_unparsed_function_queues (parser
);
4183 /* There are no classes being defined. */
4184 parser
->num_classes_being_defined
= 0;
4186 /* No template parameters apply. */
4187 parser
->num_template_parameter_lists
= 0;
4189 /* Special parsing data structures. */
4190 parser
->omp_declare_simd
= NULL
;
4191 parser
->oacc_routine
= NULL
;
4193 /* Not declaring an implicit function template. */
4194 parser
->auto_is_implicit_function_template_parm_p
= false;
4195 parser
->fully_implicit_function_template_p
= false;
4196 parser
->implicit_template_parms
= 0;
4197 parser
->implicit_template_scope
= 0;
4199 /* Allow constrained-type-specifiers. */
4200 parser
->prevent_constrained_type_specifiers
= 0;
4202 /* We haven't yet seen an 'extern "C"'. */
4203 parser
->innermost_linkage_specification_location
= UNKNOWN_LOCATION
;
4208 /* Create a cp_lexer structure which will emit the tokens in CACHE
4209 and push it onto the parser's lexer stack. This is used for delayed
4210 parsing of in-class method bodies and default arguments, and should
4211 not be confused with tentative parsing. */
4213 cp_parser_push_lexer_for_tokens (cp_parser
*parser
, cp_token_cache
*cache
)
4215 cp_lexer
*lexer
= cp_lexer_new_from_tokens (cache
);
4216 lexer
->next
= parser
->lexer
;
4217 parser
->lexer
= lexer
;
4219 /* Move the current source position to that of the first token in the
4221 cp_lexer_set_source_position_from_token (lexer
->next_token
);
4224 /* Pop the top lexer off the parser stack. This is never used for the
4225 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
4227 cp_parser_pop_lexer (cp_parser
*parser
)
4229 cp_lexer
*lexer
= parser
->lexer
;
4230 parser
->lexer
= lexer
->next
;
4231 cp_lexer_destroy (lexer
);
4233 /* Put the current source position back where it was before this
4234 lexer was pushed. */
4235 cp_lexer_set_source_position_from_token (parser
->lexer
->next_token
);
4238 /* Lexical conventions [gram.lex] */
4240 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
4244 cp_parser_identifier (cp_parser
* parser
)
4248 /* Look for the identifier. */
4249 token
= cp_parser_require (parser
, CPP_NAME
, RT_NAME
);
4250 /* Return the value. */
4252 return cp_expr (token
->u
.value
, token
->location
);
4254 return error_mark_node
;
4257 /* Parse a sequence of adjacent string constants. Returns a
4258 TREE_STRING representing the combined, nul-terminated string
4259 constant. If TRANSLATE is true, translate the string to the
4260 execution character set. If WIDE_OK is true, a wide string is
4263 C++98 [lex.string] says that if a narrow string literal token is
4264 adjacent to a wide string literal token, the behavior is undefined.
4265 However, C99 6.4.5p4 says that this results in a wide string literal.
4266 We follow C99 here, for consistency with the C front end.
4268 This code is largely lifted from lex_string() in c-lex.c.
4270 FUTURE: ObjC++ will need to handle @-strings here. */
4272 cp_parser_string_literal (cp_parser
*parser
, bool translate
, bool wide_ok
,
4273 bool lookup_udlit
= true)
4277 struct obstack str_ob
;
4278 struct obstack loc_ob
;
4279 cpp_string str
, istr
, *strs
;
4281 enum cpp_ttype type
, curr_type
;
4282 int have_suffix_p
= 0;
4284 tree suffix_id
= NULL_TREE
;
4285 bool curr_tok_is_userdef_p
= false;
4287 tok
= cp_lexer_peek_token (parser
->lexer
);
4288 if (!cp_parser_is_string_literal (tok
))
4290 cp_parser_error (parser
, "expected string-literal");
4291 return error_mark_node
;
4294 location_t loc
= tok
->location
;
4296 if (cpp_userdef_string_p (tok
->type
))
4298 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4299 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4300 curr_tok_is_userdef_p
= true;
4304 string_tree
= tok
->u
.value
;
4305 curr_type
= tok
->type
;
4309 /* Try to avoid the overhead of creating and destroying an obstack
4310 for the common case of just one string. */
4311 if (!cp_parser_is_string_literal
4312 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
4314 cp_lexer_consume_token (parser
->lexer
);
4316 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4317 str
.len
= TREE_STRING_LENGTH (string_tree
);
4320 if (curr_tok_is_userdef_p
)
4322 suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4324 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4327 curr_type
= tok
->type
;
4333 location_t last_tok_loc
= tok
->location
;
4334 gcc_obstack_init (&str_ob
);
4335 gcc_obstack_init (&loc_ob
);
4340 cp_lexer_consume_token (parser
->lexer
);
4342 str
.text
= (const unsigned char *)TREE_STRING_POINTER (string_tree
);
4343 str
.len
= TREE_STRING_LENGTH (string_tree
);
4345 if (curr_tok_is_userdef_p
)
4347 tree curr_suffix_id
= USERDEF_LITERAL_SUFFIX_ID (tok
->u
.value
);
4348 if (have_suffix_p
== 0)
4350 suffix_id
= curr_suffix_id
;
4353 else if (have_suffix_p
== 1
4354 && curr_suffix_id
!= suffix_id
)
4356 error ("inconsistent user-defined literal suffixes"
4357 " %qD and %qD in string literal",
4358 suffix_id
, curr_suffix_id
);
4361 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4364 curr_type
= tok
->type
;
4366 if (type
!= curr_type
)
4368 if (type
== CPP_STRING
)
4370 else if (curr_type
!= CPP_STRING
)
4372 rich_location
rich_loc (line_table
, tok
->location
);
4373 rich_loc
.add_range (last_tok_loc
);
4374 error_at (&rich_loc
,
4375 "unsupported non-standard concatenation "
4376 "of string literals");
4380 obstack_grow (&str_ob
, &str
, sizeof (cpp_string
));
4381 obstack_grow (&loc_ob
, &tok
->location
, sizeof (location_t
));
4383 last_tok_loc
= tok
->location
;
4385 tok
= cp_lexer_peek_token (parser
->lexer
);
4386 if (cpp_userdef_string_p (tok
->type
))
4388 string_tree
= USERDEF_LITERAL_VALUE (tok
->u
.value
);
4389 curr_type
= cpp_userdef_string_remove_type (tok
->type
);
4390 curr_tok_is_userdef_p
= true;
4394 string_tree
= tok
->u
.value
;
4395 curr_type
= tok
->type
;
4396 curr_tok_is_userdef_p
= false;
4399 while (cp_parser_is_string_literal (tok
));
4401 /* A string literal built by concatenation has its caret=start at
4402 the start of the initial string, and its finish at the finish of
4403 the final string literal. */
4404 loc
= make_location (loc
, loc
, get_finish (last_tok_loc
));
4406 strs
= (cpp_string
*) obstack_finish (&str_ob
);
4409 if (type
!= CPP_STRING
&& !wide_ok
)
4411 cp_parser_error (parser
, "a wide string is invalid in this context");
4415 if ((translate
? cpp_interpret_string
: cpp_interpret_string_notranslate
)
4416 (parse_in
, strs
, count
, &istr
, type
))
4418 value
= build_string (istr
.len
, (const char *)istr
.text
);
4419 free (CONST_CAST (unsigned char *, istr
.text
));
4422 location_t
*locs
= (location_t
*)obstack_finish (&loc_ob
);
4423 gcc_assert (g_string_concat_db
);
4424 g_string_concat_db
->record_string_concatenation (count
, locs
);
4431 TREE_TYPE (value
) = char_array_type_node
;
4433 case CPP_UTF8STRING
:
4435 TREE_TYPE (value
) = char8_array_type_node
;
4437 TREE_TYPE (value
) = char_array_type_node
;
4440 TREE_TYPE (value
) = char16_array_type_node
;
4443 TREE_TYPE (value
) = char32_array_type_node
;
4446 TREE_TYPE (value
) = wchar_array_type_node
;
4450 value
= fix_string_type (value
);
4454 tree literal
= build_userdef_literal (suffix_id
, value
,
4455 OT_NONE
, NULL_TREE
);
4457 value
= cp_parser_userdef_string_literal (literal
);
4463 /* cpp_interpret_string has issued an error. */
4464 value
= error_mark_node
;
4468 obstack_free (&str_ob
, 0);
4469 obstack_free (&loc_ob
, 0);
4472 return cp_expr (value
, loc
);
4475 /* Look up a literal operator with the name and the exact arguments. */
4478 lookup_literal_operator (tree name
, vec
<tree
, va_gc
> *args
)
4480 tree decl
= lookup_name (name
);
4481 if (!decl
|| !is_overloaded_fn (decl
))
4482 return error_mark_node
;
4484 for (lkp_iterator
iter (decl
); iter
; ++iter
)
4488 if (tree parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4494 found
&& ix
< vec_safe_length (args
) && parmtypes
!= NULL_TREE
;
4495 ++ix
, parmtypes
= TREE_CHAIN (parmtypes
))
4497 tree tparm
= TREE_VALUE (parmtypes
);
4498 tree targ
= TREE_TYPE ((*args
)[ix
]);
4499 bool ptr
= TYPE_PTR_P (tparm
);
4500 bool arr
= TREE_CODE (targ
) == ARRAY_TYPE
;
4501 if ((ptr
|| arr
|| !same_type_p (tparm
, targ
))
4503 || !same_type_p (TREE_TYPE (tparm
),
4509 && ix
== vec_safe_length (args
)
4510 /* May be this should be sufficient_parms_p instead,
4511 depending on how exactly should user-defined literals
4512 work in presence of default arguments on the literal
4513 operator parameters. */
4514 && parmtypes
== void_list_node
)
4519 return error_mark_node
;
4522 /* Parse a user-defined char constant. Returns a call to a user-defined
4523 literal operator taking the character as an argument. */
4526 cp_parser_userdef_char_literal (cp_parser
*parser
)
4528 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4529 tree literal
= token
->u
.value
;
4530 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4531 tree value
= USERDEF_LITERAL_VALUE (literal
);
4532 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4535 /* Build up a call to the user-defined operator */
4536 /* Lookup the name we got back from the id-expression. */
4538 vec_safe_push (args
, value
);
4539 decl
= lookup_literal_operator (name
, args
);
4540 if (!decl
|| decl
== error_mark_node
)
4542 error ("unable to find character literal operator %qD with %qT argument",
4543 name
, TREE_TYPE (value
));
4544 return error_mark_node
;
4546 result
= finish_call_expr (decl
, &args
, false, true, tf_warning_or_error
);
4550 /* A subroutine of cp_parser_userdef_numeric_literal to
4551 create a char... template parameter pack from a string node. */
4554 make_char_string_pack (tree value
)
4557 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4558 const char *str
= TREE_STRING_POINTER (value
);
4559 int i
, len
= TREE_STRING_LENGTH (value
) - 1;
4560 tree argvec
= make_tree_vec (1);
4562 /* Fill in CHARVEC with all of the parameters. */
4563 charvec
= make_tree_vec (len
);
4564 for (i
= 0; i
< len
; ++i
)
4566 unsigned char s
[3] = { '\'', str
[i
], '\'' };
4567 cpp_string in
= { 3, s
};
4568 cpp_string out
= { 0, 0 };
4569 if (!cpp_interpret_string (parse_in
, &in
, 1, &out
, CPP_STRING
))
4571 gcc_assert (out
.len
== 2);
4572 TREE_VEC_ELT (charvec
, i
) = build_int_cst (char_type_node
,
4576 /* Build the argument packs. */
4577 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4579 TREE_VEC_ELT (argvec
, 0) = argpack
;
4584 /* A subroutine of cp_parser_userdef_numeric_literal to
4585 create a char... template parameter pack from a string node. */
4588 make_string_pack (tree value
)
4591 tree argpack
= make_node (NONTYPE_ARGUMENT_PACK
);
4592 const unsigned char *str
4593 = (const unsigned char *) TREE_STRING_POINTER (value
);
4594 int sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
))));
4595 int len
= TREE_STRING_LENGTH (value
) / sz
- 1;
4596 tree argvec
= make_tree_vec (2);
4598 tree str_char_type_node
= TREE_TYPE (TREE_TYPE (value
));
4599 str_char_type_node
= TYPE_MAIN_VARIANT (str_char_type_node
);
4601 /* First template parm is character type. */
4602 TREE_VEC_ELT (argvec
, 0) = str_char_type_node
;
4604 /* Fill in CHARVEC with all of the parameters. */
4605 charvec
= make_tree_vec (len
);
4606 for (int i
= 0; i
< len
; ++i
)
4607 TREE_VEC_ELT (charvec
, i
)
4608 = double_int_to_tree (str_char_type_node
,
4609 double_int::from_buffer (str
+ i
* sz
, sz
));
4611 /* Build the argument packs. */
4612 SET_ARGUMENT_PACK_ARGS (argpack
, charvec
);
4614 TREE_VEC_ELT (argvec
, 1) = argpack
;
4619 /* Parse a user-defined numeric constant. returns a call to a user-defined
4620 literal operator. */
4623 cp_parser_userdef_numeric_literal (cp_parser
*parser
)
4625 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
4626 tree literal
= token
->u
.value
;
4627 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4628 tree value
= USERDEF_LITERAL_VALUE (literal
);
4629 int overflow
= USERDEF_LITERAL_OVERFLOW (literal
);
4630 tree num_string
= USERDEF_LITERAL_NUM_STRING (literal
);
4631 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4634 /* Look for a literal operator taking the exact type of numeric argument
4635 as the literal value. */
4637 vec_safe_push (args
, value
);
4638 decl
= lookup_literal_operator (name
, args
);
4639 if (decl
&& decl
!= error_mark_node
)
4641 result
= finish_call_expr (decl
, &args
, false, true,
4642 tf_warning_or_error
);
4644 if (TREE_CODE (TREE_TYPE (value
)) == INTEGER_TYPE
&& overflow
> 0)
4646 warning_at (token
->location
, OPT_Woverflow
,
4647 "integer literal exceeds range of %qT type",
4648 long_long_unsigned_type_node
);
4653 warning_at (token
->location
, OPT_Woverflow
,
4654 "floating literal exceeds range of %qT type",
4655 long_double_type_node
);
4656 else if (overflow
< 0)
4657 warning_at (token
->location
, OPT_Woverflow
,
4658 "floating literal truncated to zero");
4664 /* If the numeric argument didn't work, look for a raw literal
4665 operator taking a const char* argument consisting of the number
4666 in string format. */
4668 vec_safe_push (args
, num_string
);
4669 decl
= lookup_literal_operator (name
, args
);
4670 if (decl
&& decl
!= error_mark_node
)
4672 result
= finish_call_expr (decl
, &args
, false, true,
4673 tf_warning_or_error
);
4677 /* If the raw literal didn't work, look for a non-type template
4678 function with parameter pack char.... Call the function with
4679 template parameter characters representing the number. */
4681 decl
= lookup_literal_operator (name
, args
);
4682 if (decl
&& decl
!= error_mark_node
)
4684 tree tmpl_args
= make_char_string_pack (num_string
);
4685 if (tmpl_args
== NULL_TREE
)
4687 error ("failed to translate literal to execution character set %qT",
4689 return error_mark_node
;
4691 decl
= lookup_template_function (decl
, tmpl_args
);
4692 result
= finish_call_expr (decl
, &args
, false, true,
4693 tf_warning_or_error
);
4697 /* In C++14 the standard library defines complex number suffixes that
4698 conflict with GNU extensions. Prefer them if <complex> is #included. */
4699 bool ext
= cpp_get_options (parse_in
)->ext_numeric_literals
;
4700 bool i14
= (cxx_dialect
> cxx11
4701 && (id_equal (suffix_id
, "i")
4702 || id_equal (suffix_id
, "if")
4703 || id_equal (suffix_id
, "il")));
4704 diagnostic_t kind
= DK_ERROR
;
4709 tree cxlit
= lookup_qualified_name (std_node
, "complex_literals",
4710 LOOK_want::NORMAL
, false);
4711 if (cxlit
== error_mark_node
)
4713 /* No <complex>, so pedwarn and use GNU semantics. */
4715 opt
= OPT_Wpedantic
;
4720 = emit_diagnostic (kind
, input_location
, opt
,
4721 "unable to find numeric literal operator %qD", name
);
4724 /* Don't inform either. */;
4727 inform (token
->location
, "add %<using namespace std::complex_literals%> "
4728 "(from %<<complex>%>) to enable the C++14 user-defined literal "
4731 inform (token
->location
, "or use %<j%> instead of %<i%> for the "
4732 "GNU built-in suffix");
4735 inform (token
->location
, "use %<-fext-numeric-literals%> "
4736 "to enable more built-in suffixes");
4738 if (kind
== DK_ERROR
)
4739 value
= error_mark_node
;
4742 /* Use the built-in semantics. */
4744 if (id_equal (suffix_id
, "i"))
4746 if (TREE_CODE (value
) == INTEGER_CST
)
4747 type
= integer_type_node
;
4749 type
= double_type_node
;
4751 else if (id_equal (suffix_id
, "if"))
4752 type
= float_type_node
;
4753 else /* if (id_equal (suffix_id, "il")) */
4754 type
= long_double_type_node
;
4756 value
= build_complex (build_complex_type (type
),
4757 fold_convert (type
, integer_zero_node
),
4758 fold_convert (type
, value
));
4761 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
4762 /* Avoid repeated diagnostics. */
4763 token
->u
.value
= value
;
4767 /* Parse a user-defined string constant. Returns a call to a user-defined
4768 literal operator taking a character pointer and the length of the string
4772 cp_parser_userdef_string_literal (tree literal
)
4774 tree suffix_id
= USERDEF_LITERAL_SUFFIX_ID (literal
);
4775 tree name
= cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id
));
4776 tree value
= USERDEF_LITERAL_VALUE (literal
);
4777 int len
= TREE_STRING_LENGTH (value
)
4778 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value
)))) - 1;
4781 /* Build up a call to the user-defined operator. */
4782 /* Lookup the name we got back from the id-expression. */
4784 vec_safe_push (args
, value
);
4785 vec_safe_push (args
, build_int_cst (size_type_node
, len
));
4786 decl
= lookup_literal_operator (name
, args
);
4788 if (decl
&& decl
!= error_mark_node
)
4789 return finish_call_expr (decl
, &args
, false, true,
4790 tf_warning_or_error
);
4792 /* Look for a suitable template function, either (C++20) with a single
4793 parameter of class type, or (N3599) with typename parameter CharT and
4794 parameter pack CharT... */
4796 decl
= lookup_literal_operator (name
, args
);
4797 if (decl
&& decl
!= error_mark_node
)
4799 /* Use resolve_nondeduced_context to try to choose one form of template
4801 tree tmpl_args
= make_tree_vec (1);
4802 TREE_VEC_ELT (tmpl_args
, 0) = value
;
4803 decl
= lookup_template_function (decl
, tmpl_args
);
4804 tree res
= resolve_nondeduced_context (decl
, tf_none
);
4809 TREE_OPERAND (decl
, 1) = make_string_pack (value
);
4810 res
= resolve_nondeduced_context (decl
, tf_none
);
4814 if (!DECL_P (decl
) && cxx_dialect
> cxx17
)
4815 TREE_OPERAND (decl
, 1) = tmpl_args
;
4816 return finish_call_expr (decl
, &args
, false, true,
4817 tf_warning_or_error
);
4820 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4821 name
, TREE_TYPE (value
), size_type_node
);
4822 return error_mark_node
;
4826 /* Basic concepts [gram.basic] */
4828 /* Parse a translation-unit.
4831 declaration-seq [opt] */
4834 cp_parser_translation_unit (cp_parser
* parser
)
4836 gcc_checking_assert (!cp_error_declarator
);
4838 /* Create the declarator obstack. */
4839 gcc_obstack_init (&declarator_obstack
);
4840 /* Create the error declarator. */
4841 cp_error_declarator
= make_declarator (cdk_error
);
4842 /* Create the empty parameter list. */
4843 no_parameters
= make_parameter_declarator (NULL
, NULL
, NULL_TREE
,
4845 /* Remember where the base of the declarator obstack lies. */
4846 void *declarator_obstack_base
= obstack_next_free (&declarator_obstack
);
4848 push_deferring_access_checks (flag_access_control
4849 ? dk_no_deferred
: dk_no_check
);
4851 module_parse mp_state
= MP_NOT_MODULE
;
4852 if (modules_p () && !header_module_p ())
4853 mp_state
= MP_FIRST
;
4855 bool implicit_extern_c
= false;
4857 /* Parse until EOF. */
4860 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
4862 /* If we're entering or exiting a region that's implicitly
4863 extern "C", modify the lang context appropriately. This is
4864 so horrible. Please die. */
4865 if (implicit_extern_c
4866 != cp_lexer_peek_token (parser
->lexer
)->implicit_extern_c
)
4868 implicit_extern_c
= !implicit_extern_c
;
4869 if (implicit_extern_c
)
4870 push_lang_context (lang_name_c
);
4872 pop_lang_context ();
4875 if (token
->type
== CPP_EOF
)
4880 /* Top-level module declarations are ok, and change the
4881 portion of file we're in. Top-level import declarations
4882 are significant for the import portions. */
4884 cp_token
*next
= token
;
4885 bool exporting
= token
->keyword
== RID__EXPORT
;
4888 cp_lexer_consume_token (parser
->lexer
);
4889 next
= cp_lexer_peek_token (parser
->lexer
);
4891 if (next
->keyword
== RID__MODULE
)
4894 = cp_parser_module_declaration (parser
, mp_state
, exporting
);
4897 else if (next
->keyword
== RID__IMPORT
)
4899 if (mp_state
== MP_FIRST
)
4900 mp_state
= MP_NOT_MODULE
;
4901 cp_parser_import_declaration (parser
, mp_state
, exporting
);
4905 gcc_checking_assert (!exporting
);
4907 if (mp_state
== MP_GLOBAL
&& token
->main_source_p
)
4909 static bool warned
= false;
4913 error_at (token
->location
,
4914 "global module fragment contents must be"
4915 " from preprocessor inclusion");
4920 /* This relies on the ordering of module_parse values. */
4921 if (mp_state
== MP_PURVIEW_IMPORTS
|| mp_state
== MP_PRIVATE_IMPORTS
)
4922 /* We're no longer in the import portion of a named module. */
4923 mp_state
= module_parse (mp_state
+ 1);
4924 else if (mp_state
== MP_FIRST
)
4925 mp_state
= MP_NOT_MODULE
;
4927 if (token
->type
== CPP_CLOSE_BRACE
)
4929 cp_parser_error (parser
, "expected declaration");
4930 cp_lexer_consume_token (parser
->lexer
);
4931 /* If the next token is now a `;', consume it. */
4932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
4933 cp_lexer_consume_token (parser
->lexer
);
4936 cp_parser_toplevel_declaration (parser
);
4939 /* Get rid of the token array; we don't need it any more. */
4940 cp_lexer_destroy (parser
->lexer
);
4941 parser
->lexer
= NULL
;
4943 /* The EOF should have reset this. */
4944 gcc_checking_assert (!implicit_extern_c
);
4946 /* Make sure the declarator obstack was fully cleaned up. */
4947 gcc_assert (obstack_next_free (&declarator_obstack
)
4948 == declarator_obstack_base
);
4951 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4952 decltype context. */
4954 static inline tsubst_flags_t
4955 complain_flags (bool decltype_p
)
4957 tsubst_flags_t complain
= tf_warning_or_error
;
4959 complain
|= tf_decltype
;
4963 /* We're about to parse a collection of statements. If we're currently
4964 parsing tentatively, set up a firewall so that any nested
4965 cp_parser_commit_to_tentative_parse won't affect the current context. */
4967 static cp_token_position
4968 cp_parser_start_tentative_firewall (cp_parser
*parser
)
4970 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
4973 cp_parser_parse_tentatively (parser
);
4974 cp_parser_commit_to_topmost_tentative_parse (parser
);
4975 return cp_lexer_token_position (parser
->lexer
, false);
4978 /* We've finished parsing the collection of statements. Wrap up the
4979 firewall and replace the relevant tokens with the parsed form. */
4982 cp_parser_end_tentative_firewall (cp_parser
*parser
, cp_token_position start
,
4988 /* Finish the firewall level. */
4989 cp_parser_parse_definitely (parser
);
4990 /* And remember the result of the parse for when we try again. */
4991 cp_token
*token
= cp_lexer_token_at (parser
->lexer
, start
);
4992 token
->type
= CPP_PREPARSED_EXPR
;
4993 token
->u
.value
= expr
;
4994 token
->keyword
= RID_MAX
;
4995 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
4998 /* Like the above functions, but let the user modify the tokens. Used by
4999 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
5000 later parses, so it makes sense to localize the effects of
5001 cp_parser_commit_to_tentative_parse. */
5003 struct tentative_firewall
5008 tentative_firewall (cp_parser
*p
): parser(p
)
5010 /* If we're currently parsing tentatively, start a committed level as a
5011 firewall and then an inner tentative parse. */
5012 if ((set
= cp_parser_uncommitted_to_tentative_parse_p (parser
)))
5014 cp_parser_parse_tentatively (parser
);
5015 cp_parser_commit_to_topmost_tentative_parse (parser
);
5016 cp_parser_parse_tentatively (parser
);
5020 ~tentative_firewall()
5024 /* Finish the inner tentative parse and the firewall, propagating any
5025 uncommitted error state to the outer tentative parse. */
5026 bool err
= cp_parser_error_occurred (parser
);
5027 cp_parser_parse_definitely (parser
);
5028 cp_parser_parse_definitely (parser
);
5030 cp_parser_simulate_error (parser
);
5035 /* Some tokens naturally come in pairs e.g.'(' and ')'.
5036 This class is for tracking such a matching pair of symbols.
5037 In particular, it tracks the location of the first token,
5038 so that if the second token is missing, we can highlight the
5039 location of the first token when notifying the user about the
5042 template <typename traits_t
>
5046 /* token_pair's ctor. */
5047 token_pair () : m_open_loc (UNKNOWN_LOCATION
) {}
5049 /* If the next token is the opening symbol for this pair, consume it and
5051 Otherwise, issue an error and return false.
5052 In either case, record the location of the opening token. */
5054 bool require_open (cp_parser
*parser
)
5056 m_open_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5057 return cp_parser_require (parser
, traits_t::open_token_type
,
5058 traits_t::required_token_open
);
5061 /* Consume the next token from PARSER, recording its location as
5062 that of the opening token within the pair. */
5064 cp_token
* consume_open (cp_parser
*parser
)
5066 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
5067 gcc_assert (tok
->type
== traits_t::open_token_type
);
5068 m_open_loc
= tok
->location
;
5072 /* If the next token is the closing symbol for this pair, consume it
5074 Otherwise, issue an error, highlighting the location of the
5075 corresponding opening token, and return NULL. */
5077 cp_token
*require_close (cp_parser
*parser
) const
5079 return cp_parser_require (parser
, traits_t::close_token_type
,
5080 traits_t::required_token_close
,
5084 location_t
open_location () const { return m_open_loc
; }
5087 location_t m_open_loc
;
5090 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
5092 struct matching_paren_traits
5094 static const enum cpp_ttype open_token_type
= CPP_OPEN_PAREN
;
5095 static const enum required_token required_token_open
= RT_OPEN_PAREN
;
5096 static const enum cpp_ttype close_token_type
= CPP_CLOSE_PAREN
;
5097 static const enum required_token required_token_close
= RT_CLOSE_PAREN
;
5100 /* "matching_parens" is a token_pair<T> class for tracking matching
5101 pairs of parentheses. */
5103 typedef token_pair
<matching_paren_traits
> matching_parens
;
5105 /* Traits for token_pair<T> for tracking matching pairs of braces. */
5107 struct matching_brace_traits
5109 static const enum cpp_ttype open_token_type
= CPP_OPEN_BRACE
;
5110 static const enum required_token required_token_open
= RT_OPEN_BRACE
;
5111 static const enum cpp_ttype close_token_type
= CPP_CLOSE_BRACE
;
5112 static const enum required_token required_token_close
= RT_CLOSE_BRACE
;
5115 /* "matching_braces" is a token_pair<T> class for tracking matching
5118 typedef token_pair
<matching_brace_traits
> matching_braces
;
5121 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
5122 enclosing parentheses. */
5125 cp_parser_statement_expr (cp_parser
*parser
)
5127 cp_token_position start
= cp_parser_start_tentative_firewall (parser
);
5129 /* Consume the '('. */
5130 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5131 matching_parens parens
;
5132 parens
.consume_open (parser
);
5133 /* Start the statement-expression. */
5134 tree expr
= begin_stmt_expr ();
5135 /* Parse the compound-statement. */
5136 cp_parser_compound_statement (parser
, expr
, BCS_NORMAL
, false);
5138 expr
= finish_stmt_expr (expr
, false);
5139 /* Consume the ')'. */
5140 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
5141 if (!parens
.require_close (parser
))
5142 cp_parser_skip_to_end_of_statement (parser
);
5144 cp_parser_end_tentative_firewall (parser
, start
, expr
);
5145 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
5146 return cp_expr (expr
, combined_loc
);
5149 /* Expressions [gram.expr] */
5151 /* Parse a fold-operator.
5154 - * / % ^ & | = < > << >>
5155 = -= *= /= %= ^= &= |= <<= >>=
5156 == != <= >= && || , .* ->*
5158 This returns the tree code corresponding to the matched operator
5159 as an int. When the current token matches a compound assignment
5160 operator, the resulting tree code is the negative value of the
5161 non-assignment operator. */
5164 cp_parser_fold_operator (cp_token
*token
)
5166 switch (token
->type
)
5168 case CPP_PLUS
: return PLUS_EXPR
;
5169 case CPP_MINUS
: return MINUS_EXPR
;
5170 case CPP_MULT
: return MULT_EXPR
;
5171 case CPP_DIV
: return TRUNC_DIV_EXPR
;
5172 case CPP_MOD
: return TRUNC_MOD_EXPR
;
5173 case CPP_XOR
: return BIT_XOR_EXPR
;
5174 case CPP_AND
: return BIT_AND_EXPR
;
5175 case CPP_OR
: return BIT_IOR_EXPR
;
5176 case CPP_LSHIFT
: return LSHIFT_EXPR
;
5177 case CPP_RSHIFT
: return RSHIFT_EXPR
;
5179 case CPP_EQ
: return -NOP_EXPR
;
5180 case CPP_PLUS_EQ
: return -PLUS_EXPR
;
5181 case CPP_MINUS_EQ
: return -MINUS_EXPR
;
5182 case CPP_MULT_EQ
: return -MULT_EXPR
;
5183 case CPP_DIV_EQ
: return -TRUNC_DIV_EXPR
;
5184 case CPP_MOD_EQ
: return -TRUNC_MOD_EXPR
;
5185 case CPP_XOR_EQ
: return -BIT_XOR_EXPR
;
5186 case CPP_AND_EQ
: return -BIT_AND_EXPR
;
5187 case CPP_OR_EQ
: return -BIT_IOR_EXPR
;
5188 case CPP_LSHIFT_EQ
: return -LSHIFT_EXPR
;
5189 case CPP_RSHIFT_EQ
: return -RSHIFT_EXPR
;
5191 case CPP_EQ_EQ
: return EQ_EXPR
;
5192 case CPP_NOT_EQ
: return NE_EXPR
;
5193 case CPP_LESS
: return LT_EXPR
;
5194 case CPP_GREATER
: return GT_EXPR
;
5195 case CPP_LESS_EQ
: return LE_EXPR
;
5196 case CPP_GREATER_EQ
: return GE_EXPR
;
5198 case CPP_AND_AND
: return TRUTH_ANDIF_EXPR
;
5199 case CPP_OR_OR
: return TRUTH_ORIF_EXPR
;
5201 case CPP_COMMA
: return COMPOUND_EXPR
;
5203 case CPP_DOT_STAR
: return DOTSTAR_EXPR
;
5204 case CPP_DEREF_STAR
: return MEMBER_REF
;
5206 default: return ERROR_MARK
;
5210 /* Returns true if CODE indicates a binary expression, which is not allowed in
5211 the LHS of a fold-expression. More codes will need to be added to use this
5212 function in other contexts. */
5215 is_binary_op (tree_code code
)
5220 case POINTER_PLUS_EXPR
:
5223 case TRUNC_DIV_EXPR
:
5224 case TRUNC_MOD_EXPR
:
5240 case TRUTH_ANDIF_EXPR
:
5241 case TRUTH_ORIF_EXPR
:
5254 /* If the next token is a suitable fold operator, consume it and return as
5255 the function above. */
5258 cp_parser_fold_operator (cp_parser
*parser
)
5260 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5261 int code
= cp_parser_fold_operator (token
);
5262 if (code
!= ERROR_MARK
)
5263 cp_lexer_consume_token (parser
->lexer
);
5267 /* Parse a fold-expression.
5270 ( ... folding-operator cast-expression)
5271 ( cast-expression folding-operator ... )
5272 ( cast-expression folding operator ... folding-operator cast-expression)
5274 Note that the '(' and ')' are matched in primary expression. */
5277 cp_parser_fold_expression (cp_parser
*parser
, tree expr1
)
5282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5285 return error_mark_node
;
5286 cp_lexer_consume_token (parser
->lexer
);
5287 int op
= cp_parser_fold_operator (parser
);
5288 if (op
== ERROR_MARK
)
5290 cp_parser_error (parser
, "expected binary operator");
5291 return error_mark_node
;
5294 tree expr
= cp_parser_cast_expression (parser
, false, false,
5296 if (expr
== error_mark_node
)
5297 return error_mark_node
;
5298 return finish_left_unary_fold_expr (expr
, op
);
5301 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
5302 int op
= cp_parser_fold_operator (parser
);
5303 if (op
== ERROR_MARK
)
5305 cp_parser_error (parser
, "expected binary operator");
5306 return error_mark_node
;
5309 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
))
5311 cp_parser_error (parser
, "expected ...");
5312 return error_mark_node
;
5314 cp_lexer_consume_token (parser
->lexer
);
5316 /* The operands of a fold-expression are cast-expressions, so binary or
5317 conditional expressions are not allowed. We check this here to avoid
5318 tentative parsing. */
5319 if (EXPR_P (expr1
) && TREE_NO_WARNING (expr1
))
5320 /* OK, the expression was parenthesized. */;
5321 else if (is_binary_op (TREE_CODE (expr1
)))
5322 error_at (location_of (expr1
),
5323 "binary expression in operand of fold-expression");
5324 else if (TREE_CODE (expr1
) == COND_EXPR
5325 || (REFERENCE_REF_P (expr1
)
5326 && TREE_CODE (TREE_OPERAND (expr1
, 0)) == COND_EXPR
))
5327 error_at (location_of (expr1
),
5328 "conditional expression in operand of fold-expression");
5331 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5332 return finish_right_unary_fold_expr (expr1
, op
);
5334 if (cp_lexer_next_token_is_not (parser
->lexer
, token
->type
))
5336 cp_parser_error (parser
, "mismatched operator in fold-expression");
5337 return error_mark_node
;
5339 cp_lexer_consume_token (parser
->lexer
);
5341 // Binary left or right fold.
5342 tree expr2
= cp_parser_cast_expression (parser
, false, false, false, &pidk
);
5343 if (expr2
== error_mark_node
)
5344 return error_mark_node
;
5345 return finish_binary_fold_expr (expr1
, expr2
, op
);
5348 /* Parse a primary-expression.
5355 lambda-expression (C++11)
5360 ( compound-statement )
5361 __builtin_va_arg ( assignment-expression , type-id )
5362 __builtin_offsetof ( type-id , offsetof-expression )
5365 __has_nothrow_assign ( type-id )
5366 __has_nothrow_constructor ( type-id )
5367 __has_nothrow_copy ( type-id )
5368 __has_trivial_assign ( type-id )
5369 __has_trivial_constructor ( type-id )
5370 __has_trivial_copy ( type-id )
5371 __has_trivial_destructor ( type-id )
5372 __has_virtual_destructor ( type-id )
5373 __is_abstract ( type-id )
5374 __is_base_of ( type-id , type-id )
5375 __is_class ( type-id )
5376 __is_empty ( type-id )
5377 __is_enum ( type-id )
5378 __is_final ( type-id )
5379 __is_literal_type ( type-id )
5380 __is_pod ( type-id )
5381 __is_polymorphic ( type-id )
5382 __is_std_layout ( type-id )
5383 __is_trivial ( type-id )
5384 __is_union ( type-id )
5386 Objective-C++ Extension:
5394 ADDRESS_P is true iff this expression was immediately preceded by
5395 "&" and therefore might denote a pointer-to-member. CAST_P is true
5396 iff this expression is the target of a cast. TEMPLATE_ARG_P is
5397 true iff this expression is a template argument.
5399 Returns a representation of the expression. Upon return, *IDK
5400 indicates what kind of id-expression (if any) was present. */
5403 cp_parser_primary_expression (cp_parser
*parser
,
5406 bool template_arg_p
,
5410 cp_token
*token
= NULL
;
5412 /* Assume the primary expression is not an id-expression. */
5413 *idk
= CP_ID_KIND_NONE
;
5415 /* Peek at the next token. */
5416 token
= cp_lexer_peek_token (parser
->lexer
);
5417 switch ((int) token
->type
)
5426 user-defined-literal */
5433 case CPP_PREPARSED_EXPR
:
5434 if (TREE_CODE (token
->u
.value
) == USERDEF_LITERAL
)
5435 return cp_parser_userdef_numeric_literal (parser
);
5436 token
= cp_lexer_consume_token (parser
->lexer
);
5437 if (TREE_CODE (token
->u
.value
) == FIXED_CST
)
5439 error_at (token
->location
,
5440 "fixed-point types not supported in C++");
5441 return error_mark_node
;
5443 /* Floating-point literals are only allowed in an integral
5444 constant expression if they are cast to an integral or
5445 enumeration type. */
5446 if (TREE_CODE (token
->u
.value
) == REAL_CST
5447 && parser
->integral_constant_expression_p
5450 /* CAST_P will be set even in invalid code like "int(2.7 +
5451 ...)". Therefore, we have to check that the next token
5452 is sure to end the cast. */
5455 cp_token
*next_token
;
5457 next_token
= cp_lexer_peek_token (parser
->lexer
);
5458 if (/* The comma at the end of an
5459 enumerator-definition. */
5460 next_token
->type
!= CPP_COMMA
5461 /* The curly brace at the end of an enum-specifier. */
5462 && next_token
->type
!= CPP_CLOSE_BRACE
5463 /* The end of a statement. */
5464 && next_token
->type
!= CPP_SEMICOLON
5465 /* The end of the cast-expression. */
5466 && next_token
->type
!= CPP_CLOSE_PAREN
5467 /* The end of an array bound. */
5468 && next_token
->type
!= CPP_CLOSE_SQUARE
5469 /* The closing ">" in a template-argument-list. */
5470 && (next_token
->type
!= CPP_GREATER
5471 || parser
->greater_than_is_operator_p
)
5472 /* C++0x only: A ">>" treated like two ">" tokens,
5473 in a template-argument-list. */
5474 && (next_token
->type
!= CPP_RSHIFT
5475 || (cxx_dialect
== cxx98
)
5476 || parser
->greater_than_is_operator_p
))
5480 /* If we are within a cast, then the constraint that the
5481 cast is to an integral or enumeration type will be
5482 checked at that point. If we are not within a cast, then
5483 this code is invalid. */
5485 cp_parser_non_integral_constant_expression (parser
, NIC_FLOAT
);
5487 return (cp_expr (token
->u
.value
, token
->location
)
5488 .maybe_add_location_wrapper ());
5490 case CPP_CHAR_USERDEF
:
5491 case CPP_CHAR16_USERDEF
:
5492 case CPP_CHAR32_USERDEF
:
5493 case CPP_WCHAR_USERDEF
:
5494 case CPP_UTF8CHAR_USERDEF
:
5495 return cp_parser_userdef_char_literal (parser
);
5501 case CPP_UTF8STRING
:
5502 case CPP_STRING_USERDEF
:
5503 case CPP_STRING16_USERDEF
:
5504 case CPP_STRING32_USERDEF
:
5505 case CPP_WSTRING_USERDEF
:
5506 case CPP_UTF8STRING_USERDEF
:
5507 /* ??? Should wide strings be allowed when parser->translate_strings_p
5508 is false (i.e. in attributes)? If not, we can kill the third
5509 argument to cp_parser_string_literal. */
5510 return (cp_parser_string_literal (parser
,
5511 parser
->translate_strings_p
,
5513 .maybe_add_location_wrapper ());
5515 case CPP_OPEN_PAREN
:
5516 /* If we see `( { ' then we are looking at the beginning of
5517 a GNU statement-expression. */
5518 if (cp_parser_allow_gnu_extensions_p (parser
)
5519 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_BRACE
))
5521 /* Statement-expressions are not allowed by the standard. */
5522 pedwarn (token
->location
, OPT_Wpedantic
,
5523 "ISO C++ forbids braced-groups within expressions");
5525 /* And they're not allowed outside of a function-body; you
5526 cannot, for example, write:
5528 int i = ({ int j = 3; j + 1; });
5530 at class or namespace scope. */
5531 if (!parser
->in_function_body
5532 || parser
->in_template_argument_list_p
)
5534 error_at (token
->location
,
5535 "statement-expressions are not allowed outside "
5536 "functions nor in template-argument lists");
5537 cp_parser_skip_to_end_of_block_or_statement (parser
);
5538 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
5539 cp_lexer_consume_token (parser
->lexer
);
5540 return error_mark_node
;
5543 return cp_parser_statement_expr (parser
);
5545 /* Otherwise it's a normal parenthesized expression. */
5548 bool saved_greater_than_is_operator_p
;
5550 location_t open_paren_loc
= token
->location
;
5552 /* Consume the `('. */
5553 matching_parens parens
;
5554 parens
.consume_open (parser
);
5555 /* Within a parenthesized expression, a `>' token is always
5556 the greater-than operator. */
5557 saved_greater_than_is_operator_p
5558 = parser
->greater_than_is_operator_p
;
5559 parser
->greater_than_is_operator_p
= true;
5561 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
5562 /* Left fold expression. */
5565 /* Parse the parenthesized expression. */
5566 expr
= cp_parser_expression (parser
, idk
, cast_p
, decltype_p
);
5568 token
= cp_lexer_peek_token (parser
->lexer
);
5569 if (token
->type
== CPP_ELLIPSIS
|| cp_parser_fold_operator (token
))
5571 expr
= cp_parser_fold_expression (parser
, expr
);
5572 if (expr
!= error_mark_node
5573 && cxx_dialect
< cxx17
)
5574 pedwarn (input_location
, 0, "fold-expressions only available "
5575 "with %<-std=c++17%> or %<-std=gnu++17%>");
5578 /* Let the front end know that this expression was
5579 enclosed in parentheses. This matters in case, for
5580 example, the expression is of the form `A::B', since
5581 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5583 expr
= finish_parenthesized_expr (expr
);
5585 /* DR 705: Wrapping an unqualified name in parentheses
5586 suppresses arg-dependent lookup. We want to pass back
5587 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5588 (c++/37862), but none of the others. */
5589 if (*idk
!= CP_ID_KIND_QUALIFIED
)
5590 *idk
= CP_ID_KIND_NONE
;
5592 /* The `>' token might be the end of a template-id or
5593 template-parameter-list now. */
5594 parser
->greater_than_is_operator_p
5595 = saved_greater_than_is_operator_p
;
5597 /* Consume the `)'. */
5598 token
= cp_lexer_peek_token (parser
->lexer
);
5599 location_t close_paren_loc
= token
->location
;
5600 expr
.set_range (open_paren_loc
, close_paren_loc
);
5601 if (!parens
.require_close (parser
)
5602 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
5603 cp_parser_skip_to_end_of_statement (parser
);
5608 case CPP_OPEN_SQUARE
:
5610 if (c_dialect_objc ())
5612 /* We might have an Objective-C++ message. */
5613 cp_parser_parse_tentatively (parser
);
5614 tree msg
= cp_parser_objc_message_expression (parser
);
5615 /* If that works out, we're done ... */
5616 if (cp_parser_parse_definitely (parser
))
5618 /* ... else, fall though to see if it's a lambda. */
5620 cp_expr lam
= cp_parser_lambda_expression (parser
);
5621 /* Don't warn about a failed tentative parse. */
5622 if (cp_parser_error_occurred (parser
))
5623 return error_mark_node
;
5624 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR
);
5628 case CPP_OBJC_STRING
:
5629 if (c_dialect_objc ())
5630 /* We have an Objective-C++ string literal. */
5631 return cp_parser_objc_expression (parser
);
5632 cp_parser_error (parser
, "expected primary-expression");
5633 return error_mark_node
;
5636 switch (token
->keyword
)
5638 /* These two are the boolean literals. */
5640 cp_lexer_consume_token (parser
->lexer
);
5641 return cp_expr (boolean_true_node
, token
->location
);
5643 cp_lexer_consume_token (parser
->lexer
);
5644 return cp_expr (boolean_false_node
, token
->location
);
5646 /* The `__null' literal. */
5648 cp_lexer_consume_token (parser
->lexer
);
5649 return cp_expr (null_node
, token
->location
);
5651 /* The `nullptr' literal. */
5653 cp_lexer_consume_token (parser
->lexer
);
5654 return cp_expr (nullptr_node
, token
->location
);
5656 /* Recognize the `this' keyword. */
5658 cp_lexer_consume_token (parser
->lexer
);
5659 if (parser
->local_variables_forbidden_p
& THIS_FORBIDDEN
)
5661 error_at (token
->location
,
5662 "%<this%> may not be used in this context");
5663 return error_mark_node
;
5665 /* Pointers cannot appear in constant-expressions. */
5666 if (cp_parser_non_integral_constant_expression (parser
, NIC_THIS
))
5667 return error_mark_node
;
5668 return cp_expr (finish_this_expr (), token
->location
);
5670 /* The `operator' keyword can be the beginning of an
5675 case RID_FUNCTION_NAME
:
5676 case RID_PRETTY_FUNCTION_NAME
:
5677 case RID_C99_FUNCTION_NAME
:
5679 non_integral_constant name
;
5681 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5682 __func__ are the names of variables -- but they are
5683 treated specially. Therefore, they are handled here,
5684 rather than relying on the generic id-expression logic
5685 below. Grammatically, these names are id-expressions.
5687 Consume the token. */
5688 token
= cp_lexer_consume_token (parser
->lexer
);
5690 switch (token
->keyword
)
5692 case RID_FUNCTION_NAME
:
5693 name
= NIC_FUNC_NAME
;
5695 case RID_PRETTY_FUNCTION_NAME
:
5696 name
= NIC_PRETTY_FUNC
;
5698 case RID_C99_FUNCTION_NAME
:
5699 name
= NIC_C99_FUNC
;
5705 if (cp_parser_non_integral_constant_expression (parser
, name
))
5706 return error_mark_node
;
5708 /* Look up the name. */
5709 return finish_fname (token
->u
.value
);
5716 location_t type_location
;
5717 location_t start_loc
5718 = cp_lexer_peek_token (parser
->lexer
)->location
;
5719 /* The `__builtin_va_arg' construct is used to handle
5720 `va_arg'. Consume the `__builtin_va_arg' token. */
5721 cp_lexer_consume_token (parser
->lexer
);
5722 /* Look for the opening `('. */
5723 matching_parens parens
;
5724 parens
.require_open (parser
);
5725 /* Now, parse the assignment-expression. */
5726 expression
= cp_parser_assignment_expression (parser
);
5727 /* Look for the `,'. */
5728 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
5729 type_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
5730 /* Parse the type-id. */
5732 type_id_in_expr_sentinel
s (parser
);
5733 type
= cp_parser_type_id (parser
);
5735 /* Look for the closing `)'. */
5736 location_t finish_loc
5737 = cp_lexer_peek_token (parser
->lexer
)->location
;
5738 parens
.require_close (parser
);
5739 /* Using `va_arg' in a constant-expression is not
5741 if (cp_parser_non_integral_constant_expression (parser
,
5743 return error_mark_node
;
5744 /* Construct a location of the form:
5745 __builtin_va_arg (v, int)
5746 ~~~~~~~~~~~~~~~~~~~~~^~~~
5747 with the caret at the type, ranging from the start of the
5748 "__builtin_va_arg" token to the close paren. */
5749 location_t combined_loc
5750 = make_location (type_location
, start_loc
, finish_loc
);
5751 return build_x_va_arg (combined_loc
, expression
, type
);
5755 return cp_parser_builtin_offsetof (parser
);
5757 case RID_HAS_NOTHROW_ASSIGN
:
5758 case RID_HAS_NOTHROW_CONSTRUCTOR
:
5759 case RID_HAS_NOTHROW_COPY
:
5760 case RID_HAS_TRIVIAL_ASSIGN
:
5761 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
5762 case RID_HAS_TRIVIAL_COPY
:
5763 case RID_HAS_TRIVIAL_DESTRUCTOR
:
5764 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
5765 case RID_HAS_VIRTUAL_DESTRUCTOR
:
5766 case RID_IS_ABSTRACT
:
5767 case RID_IS_AGGREGATE
:
5768 case RID_IS_BASE_OF
:
5773 case RID_IS_LITERAL_TYPE
:
5775 case RID_IS_POLYMORPHIC
:
5776 case RID_IS_SAME_AS
:
5777 case RID_IS_STD_LAYOUT
:
5778 case RID_IS_TRIVIAL
:
5779 case RID_IS_TRIVIALLY_ASSIGNABLE
:
5780 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
5781 case RID_IS_TRIVIALLY_COPYABLE
:
5783 case RID_IS_ASSIGNABLE
:
5784 case RID_IS_CONSTRUCTIBLE
:
5785 case RID_IS_NOTHROW_ASSIGNABLE
:
5786 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
5787 return cp_parser_trait_expr (parser
, token
->keyword
);
5791 return cp_parser_requires_expression (parser
);
5793 /* Objective-C++ expressions. */
5795 case RID_AT_PROTOCOL
:
5796 case RID_AT_SELECTOR
:
5797 return cp_parser_objc_expression (parser
);
5800 if (parser
->in_function_body
5801 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
5804 error_at (token
->location
,
5805 "a template declaration cannot appear at block scope");
5806 cp_parser_skip_to_end_of_block_or_statement (parser
);
5807 return error_mark_node
;
5811 cp_parser_error (parser
, "expected primary-expression");
5812 return error_mark_node
;
5815 /* An id-expression can start with either an identifier, a
5816 `::' as the beginning of a qualified-id, or the "operator"
5820 case CPP_TEMPLATE_ID
:
5821 case CPP_NESTED_NAME_SPECIFIER
:
5824 cp_expr id_expression
;
5826 const char *error_msg
;
5829 cp_token
*id_expr_token
;
5831 /* Parse the id-expression. */
5833 = cp_parser_id_expression (parser
,
5834 /*template_keyword_p=*/false,
5835 /*check_dependency_p=*/true,
5837 /*declarator_p=*/false,
5838 /*optional_p=*/false);
5839 if (id_expression
== error_mark_node
)
5840 return error_mark_node
;
5841 id_expr_token
= token
;
5842 token
= cp_lexer_peek_token (parser
->lexer
);
5843 done
= (token
->type
!= CPP_OPEN_SQUARE
5844 && token
->type
!= CPP_OPEN_PAREN
5845 && token
->type
!= CPP_DOT
5846 && token
->type
!= CPP_DEREF
5847 && token
->type
!= CPP_PLUS_PLUS
5848 && token
->type
!= CPP_MINUS_MINUS
);
5849 /* If we have a template-id, then no further lookup is
5850 required. If the template-id was for a template-class, we
5851 will sometimes have a TYPE_DECL at this point. */
5852 if (TREE_CODE (id_expression
) == TEMPLATE_ID_EXPR
5853 || TREE_CODE (id_expression
) == TYPE_DECL
)
5854 decl
= id_expression
;
5855 /* Look up the name. */
5858 tree ambiguous_decls
;
5860 /* If we already know that this lookup is ambiguous, then
5861 we've already issued an error message; there's no reason
5863 if (id_expr_token
->type
== CPP_NAME
5864 && id_expr_token
->error_reported
)
5866 cp_parser_simulate_error (parser
);
5867 return error_mark_node
;
5870 decl
= cp_parser_lookup_name (parser
, id_expression
,
5873 /*is_namespace=*/false,
5874 /*check_dependency=*/true,
5876 id_expression
.get_location ());
5877 /* If the lookup was ambiguous, an error will already have
5879 if (ambiguous_decls
)
5880 return error_mark_node
;
5882 /* In Objective-C++, we may have an Objective-C 2.0
5883 dot-syntax for classes here. */
5884 if (c_dialect_objc ()
5885 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
5886 && TREE_CODE (decl
) == TYPE_DECL
5887 && objc_is_class_name (decl
))
5890 cp_lexer_consume_token (parser
->lexer
);
5891 component
= cp_parser_identifier (parser
);
5892 if (component
== error_mark_node
)
5893 return error_mark_node
;
5895 tree result
= objc_build_class_component_ref (id_expression
,
5897 /* Build a location of the form:
5900 with caret at the start of the component name (at
5901 input_location), ranging from the start of the id_expression
5902 to the end of the component name. */
5903 location_t combined_loc
5904 = make_location (input_location
, id_expression
.get_start (),
5905 get_finish (input_location
));
5906 protected_set_expr_location (result
, combined_loc
);
5910 /* In Objective-C++, an instance variable (ivar) may be preferred
5911 to whatever cp_parser_lookup_name() found.
5912 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5913 rest of c-family, we have to do a little extra work to preserve
5914 any location information in cp_expr "decl". Given that
5915 objc_lookup_ivar is implemented in "c-family" and "objc", we
5916 have a trip through the pure "tree" type, rather than cp_expr.
5917 Naively copying it back to "decl" would implicitly give the
5918 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5919 store an EXPR_LOCATION. Hence we only update "decl" (and
5920 hence its location_t) if we get back a different tree node. */
5921 tree decl_tree
= objc_lookup_ivar (decl
.get_value (),
5923 if (decl_tree
!= decl
.get_value ())
5924 decl
= cp_expr (decl_tree
);
5926 /* If name lookup gives us a SCOPE_REF, then the
5927 qualifying scope was dependent. */
5928 if (TREE_CODE (decl
) == SCOPE_REF
)
5930 /* At this point, we do not know if DECL is a valid
5931 integral constant expression. We assume that it is
5932 in fact such an expression, so that code like:
5934 template <int N> struct A {
5938 is accepted. At template-instantiation time, we
5939 will check that B<N>::i is actually a constant. */
5942 /* Check to see if DECL is a local variable in a context
5943 where that is forbidden. */
5944 if ((parser
->local_variables_forbidden_p
& LOCAL_VARS_FORBIDDEN
)
5945 && local_variable_p (decl
))
5948 = (TREE_CODE (decl
) == PARM_DECL
5949 ? _("parameter %qD may not appear in this context")
5950 : _("local variable %qD may not appear in this context"));
5951 error_at (id_expression
.get_location (), msg
,
5953 return error_mark_node
;
5957 decl
= (finish_id_expression
5958 (id_expression
, decl
, parser
->scope
,
5960 parser
->integral_constant_expression_p
,
5961 parser
->allow_non_integral_constant_expression_p
,
5962 &parser
->non_integral_constant_expression_p
,
5963 template_p
, done
, address_p
,
5966 id_expression
.get_location ()));
5968 cp_parser_error (parser
, error_msg
);
5969 /* Build a location for an id-expression of the form:
5975 i.e. from the start of the first token to the end of the final
5976 token, with the caret at the start of the unqualified-id. */
5977 location_t caret_loc
= get_pure_location (id_expression
.get_location ());
5978 location_t start_loc
= get_start (id_expr_token
->location
);
5979 location_t finish_loc
= get_finish (id_expression
.get_location ());
5980 location_t combined_loc
5981 = make_location (caret_loc
, start_loc
, finish_loc
);
5983 decl
.set_location (combined_loc
);
5987 /* Anything else is an error. */
5989 cp_parser_error (parser
, "expected primary-expression");
5990 return error_mark_node
;
5994 static inline cp_expr
5995 cp_parser_primary_expression (cp_parser
*parser
,
5998 bool template_arg_p
,
6001 return cp_parser_primary_expression (parser
, address_p
, cast_p
, template_arg_p
,
6002 /*decltype*/false, idk
);
6005 /* Parse an id-expression.
6012 :: [opt] nested-name-specifier template [opt] unqualified-id
6014 :: operator-function-id
6017 Return a representation of the unqualified portion of the
6018 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
6019 a `::' or nested-name-specifier.
6021 Often, if the id-expression was a qualified-id, the caller will
6022 want to make a SCOPE_REF to represent the qualified-id. This
6023 function does not do this in order to avoid wastefully creating
6024 SCOPE_REFs when they are not required.
6026 If TEMPLATE_KEYWORD_P is true, then we have just seen the
6029 If CHECK_DEPENDENCY_P is false, then names are looked up inside
6030 uninstantiated templates.
6032 If *TEMPLATE_P is non-NULL, it is set to true iff the
6033 `template' keyword is used to explicitly indicate that the entity
6034 named is a template.
6036 If DECLARATOR_P is true, the id-expression is appearing as part of
6037 a declarator, rather than as part of an expression. */
6040 cp_parser_id_expression (cp_parser
*parser
,
6041 bool template_keyword_p
,
6042 bool check_dependency_p
,
6047 bool global_scope_p
;
6048 bool nested_name_specifier_p
;
6050 /* Assume the `template' keyword was not used. */
6052 *template_p
= template_keyword_p
;
6054 /* Look for the optional `::' operator. */
6056 = (!template_keyword_p
6057 && (cp_parser_global_scope_opt (parser
,
6058 /*current_scope_valid_p=*/false)
6061 /* Look for the optional nested-name-specifier. */
6062 nested_name_specifier_p
6063 = (cp_parser_nested_name_specifier_opt (parser
,
6064 /*typename_keyword_p=*/false,
6071 /* If there is a nested-name-specifier, then we are looking at
6072 the first qualified-id production. */
6073 if (nested_name_specifier_p
)
6076 tree saved_object_scope
;
6077 tree saved_qualifying_scope
;
6078 cp_expr unqualified_id
;
6081 /* See if the next token is the `template' keyword. */
6083 template_p
= &is_template
;
6084 *template_p
= cp_parser_optional_template_keyword (parser
);
6085 /* Name lookup we do during the processing of the
6086 unqualified-id might obliterate SCOPE. */
6087 saved_scope
= parser
->scope
;
6088 saved_object_scope
= parser
->object_scope
;
6089 saved_qualifying_scope
= parser
->qualifying_scope
;
6090 /* Process the final unqualified-id. */
6091 unqualified_id
= cp_parser_unqualified_id (parser
, *template_p
,
6094 /*optional_p=*/false);
6095 /* Restore the SAVED_SCOPE for our caller. */
6096 parser
->scope
= saved_scope
;
6097 parser
->object_scope
= saved_object_scope
;
6098 parser
->qualifying_scope
= saved_qualifying_scope
;
6100 return unqualified_id
;
6102 /* Otherwise, if we are in global scope, then we are looking at one
6103 of the other qualified-id productions. */
6104 else if (global_scope_p
)
6109 /* Peek at the next token. */
6110 token
= cp_lexer_peek_token (parser
->lexer
);
6112 /* If it's an identifier, and the next token is not a "<", then
6113 we can avoid the template-id case. This is an optimization
6114 for this common case. */
6115 if (token
->type
== CPP_NAME
6116 && !cp_parser_nth_token_starts_template_argument_list_p
6118 return cp_parser_identifier (parser
);
6120 cp_parser_parse_tentatively (parser
);
6121 /* Try a template-id. */
6122 id
= cp_parser_template_id_expr (parser
,
6123 /*template_keyword_p=*/false,
6124 /*check_dependency_p=*/true,
6126 /* If that worked, we're done. */
6127 if (cp_parser_parse_definitely (parser
))
6130 /* Peek at the next token. (Changes in the token buffer may
6131 have invalidated the pointer obtained above.) */
6132 token
= cp_lexer_peek_token (parser
->lexer
);
6134 switch (token
->type
)
6137 return cp_parser_identifier (parser
);
6140 if (token
->keyword
== RID_OPERATOR
)
6141 return cp_parser_operator_function_id (parser
);
6145 cp_parser_error (parser
, "expected id-expression");
6146 return error_mark_node
;
6150 return cp_parser_unqualified_id (parser
, template_keyword_p
,
6151 /*check_dependency_p=*/true,
6156 /* Parse an unqualified-id.
6160 operator-function-id
6161 conversion-function-id
6165 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
6166 keyword, in a construct like `A::template ...'.
6168 Returns a representation of unqualified-id. For the `identifier'
6169 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
6170 production a BIT_NOT_EXPR is returned; the operand of the
6171 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
6172 other productions, see the documentation accompanying the
6173 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
6174 names are looked up in uninstantiated templates. If DECLARATOR_P
6175 is true, the unqualified-id is appearing as part of a declarator,
6176 rather than as part of an expression. */
6179 cp_parser_unqualified_id (cp_parser
* parser
,
6180 bool template_keyword_p
,
6181 bool check_dependency_p
,
6187 /* Peek at the next token. */
6188 token
= cp_lexer_peek_token (parser
->lexer
);
6190 switch ((int) token
->type
)
6196 /* We don't know yet whether or not this will be a
6198 cp_parser_parse_tentatively (parser
);
6199 /* Try a template-id. */
6200 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6203 /* If it worked, we're done. */
6204 if (cp_parser_parse_definitely (parser
))
6206 /* Otherwise, it's an ordinary identifier. */
6207 return cp_parser_identifier (parser
);
6210 case CPP_TEMPLATE_ID
:
6211 return cp_parser_template_id_expr (parser
, template_keyword_p
,
6218 tree qualifying_scope
;
6222 location_t tilde_loc
= token
->location
;
6224 /* Consume the `~' token. */
6225 cp_lexer_consume_token (parser
->lexer
);
6226 /* Parse the class-name. The standard, as written, seems to
6229 template <typename T> struct S { ~S (); };
6230 template <typename T> S<T>::~S() {}
6232 is invalid, since `~' must be followed by a class-name, but
6233 `S<T>' is dependent, and so not known to be a class.
6234 That's not right; we need to look in uninstantiated
6235 templates. A further complication arises from:
6237 template <typename T> void f(T t) {
6241 Here, it is not possible to look up `T' in the scope of `T'
6242 itself. We must look in both the current scope, and the
6243 scope of the containing complete expression.
6245 Yet another issue is:
6254 The standard does not seem to say that the `S' in `~S'
6255 should refer to the type `S' and not the data member
6258 /* DR 244 says that we look up the name after the "~" in the
6259 same scope as we looked up the qualifying name. That idea
6260 isn't fully worked out; it's more complicated than that. */
6261 scope
= parser
->scope
;
6262 object_scope
= parser
->object_scope
;
6263 qualifying_scope
= parser
->qualifying_scope
;
6265 /* Check for invalid scopes. */
6266 if (scope
== error_mark_node
)
6268 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6269 cp_lexer_consume_token (parser
->lexer
);
6270 return error_mark_node
;
6272 if (scope
&& TREE_CODE (scope
) == NAMESPACE_DECL
)
6274 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6275 error_at (token
->location
,
6276 "scope %qT before %<~%> is not a class-name",
6278 cp_parser_simulate_error (parser
);
6279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
6280 cp_lexer_consume_token (parser
->lexer
);
6281 return error_mark_node
;
6283 if (template_keyword_p
)
6285 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6286 error_at (tilde_loc
, "%<template%> keyword not permitted in "
6288 cp_parser_simulate_error (parser
);
6289 return error_mark_node
;
6292 gcc_assert (!scope
|| TYPE_P (scope
));
6294 token
= cp_lexer_peek_token (parser
->lexer
);
6296 /* Create a location with caret == start at the tilde,
6297 finishing at the end of the peeked token, e.g:
6301 = make_location (tilde_loc
, tilde_loc
, token
->location
);
6303 /* If the name is of the form "X::~X" it's OK even if X is a
6307 && token
->type
== CPP_NAME
6308 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6310 && (token
->u
.value
== TYPE_IDENTIFIER (scope
)
6311 || (CLASS_TYPE_P (scope
)
6312 && constructor_name_p (token
->u
.value
, scope
))))
6314 cp_lexer_consume_token (parser
->lexer
);
6315 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6318 /* ~auto means the destructor of whatever the object is. */
6319 if (cp_parser_is_keyword (token
, RID_AUTO
))
6321 if (cxx_dialect
< cxx14
)
6323 "%<~auto%> only available with "
6324 "%<-std=c++14%> or %<-std=gnu++14%>");
6325 cp_lexer_consume_token (parser
->lexer
);
6326 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, make_auto ());
6329 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
6330 declarator-id of a constructor or destructor. */
6331 if (token
->type
== CPP_TEMPLATE_ID
&& cxx_dialect
>= cxx20
)
6333 if (!cp_parser_simulate_error (parser
))
6334 error_at (tilde_loc
, "template-id not allowed for destructor");
6335 return error_mark_node
;
6338 /* If there was an explicit qualification (S::~T), first look
6339 in the scope given by the qualification (i.e., S).
6341 Note: in the calls to cp_parser_class_name below we pass
6342 typename_type so that lookup finds the injected-class-name
6343 rather than the constructor. */
6345 type_decl
= NULL_TREE
;
6348 cp_parser_parse_tentatively (parser
);
6349 type_decl
= cp_parser_class_name (parser
,
6350 /*typename_keyword_p=*/false,
6351 /*template_keyword_p=*/false,
6353 /*check_dependency=*/false,
6354 /*class_head_p=*/false,
6356 if (cp_parser_parse_definitely (parser
))
6359 /* In "N::S::~S", look in "N" as well. */
6360 if (!done
&& scope
&& qualifying_scope
)
6362 cp_parser_parse_tentatively (parser
);
6363 parser
->scope
= qualifying_scope
;
6364 parser
->object_scope
= NULL_TREE
;
6365 parser
->qualifying_scope
= NULL_TREE
;
6367 = cp_parser_class_name (parser
,
6368 /*typename_keyword_p=*/false,
6369 /*template_keyword_p=*/false,
6371 /*check_dependency=*/false,
6372 /*class_head_p=*/false,
6374 if (cp_parser_parse_definitely (parser
))
6377 /* In "p->S::~T", look in the scope given by "*p" as well. */
6378 else if (!done
&& object_scope
)
6380 cp_parser_parse_tentatively (parser
);
6381 parser
->scope
= object_scope
;
6382 parser
->object_scope
= NULL_TREE
;
6383 parser
->qualifying_scope
= NULL_TREE
;
6385 = cp_parser_class_name (parser
,
6386 /*typename_keyword_p=*/false,
6387 /*template_keyword_p=*/false,
6389 /*check_dependency=*/false,
6390 /*class_head_p=*/false,
6392 if (cp_parser_parse_definitely (parser
))
6395 /* Look in the surrounding context. */
6398 parser
->scope
= NULL_TREE
;
6399 parser
->object_scope
= NULL_TREE
;
6400 parser
->qualifying_scope
= NULL_TREE
;
6401 if (processing_template_decl
)
6402 cp_parser_parse_tentatively (parser
);
6404 = cp_parser_class_name (parser
,
6405 /*typename_keyword_p=*/false,
6406 /*template_keyword_p=*/false,
6408 /*check_dependency=*/false,
6409 /*class_head_p=*/false,
6411 if (processing_template_decl
6412 && ! cp_parser_parse_definitely (parser
))
6414 /* We couldn't find a type with this name. If we're parsing
6415 tentatively, fail and try something else. */
6416 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6418 cp_parser_simulate_error (parser
);
6419 return error_mark_node
;
6421 /* Otherwise, accept it and check for a match at instantiation
6423 type_decl
= cp_parser_identifier (parser
);
6424 if (type_decl
!= error_mark_node
)
6425 type_decl
= build_min_nt_loc (loc
, BIT_NOT_EXPR
, type_decl
);
6429 /* If an error occurred, assume that the name of the
6430 destructor is the same as the name of the qualifying
6431 class. That allows us to keep parsing after running
6432 into ill-formed destructor names. */
6433 if (type_decl
== error_mark_node
&& scope
)
6434 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, scope
);
6435 else if (type_decl
== error_mark_node
)
6436 return error_mark_node
;
6438 /* Check that destructor name and scope match. */
6439 if (declarator_p
&& scope
&& !check_dtor_name (scope
, type_decl
))
6441 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
6443 "declaration of %<~%T%> as member of %qT",
6445 cp_parser_simulate_error (parser
);
6446 return error_mark_node
;
6451 A typedef-name that names a class shall not be used as the
6452 identifier in the declarator for a destructor declaration. */
6454 && !DECL_IMPLICIT_TYPEDEF_P (type_decl
)
6455 && !DECL_SELF_REFERENCE_P (type_decl
)
6456 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
6458 "typedef-name %qD used as destructor declarator",
6461 return build_min_nt_loc (loc
, BIT_NOT_EXPR
, TREE_TYPE (type_decl
));
6465 if (token
->keyword
== RID_OPERATOR
)
6469 /* This could be a template-id, so we try that first. */
6470 cp_parser_parse_tentatively (parser
);
6471 /* Try a template-id. */
6472 id
= cp_parser_template_id_expr (parser
, template_keyword_p
,
6473 /*check_dependency_p=*/true,
6475 /* If that worked, we're done. */
6476 if (cp_parser_parse_definitely (parser
))
6478 /* We still don't know whether we're looking at an
6479 operator-function-id or a conversion-function-id. */
6480 cp_parser_parse_tentatively (parser
);
6481 /* Try an operator-function-id. */
6482 id
= cp_parser_operator_function_id (parser
);
6483 /* If that didn't work, try a conversion-function-id. */
6484 if (!cp_parser_parse_definitely (parser
))
6485 id
= cp_parser_conversion_function_id (parser
);
6494 cp_parser_error (parser
, "expected unqualified-id");
6495 return error_mark_node
;
6499 /* Check [temp.names]/5: A name prefixed by the keyword template shall
6500 be a template-id or the name shall refer to a class template or an
6504 check_template_keyword_in_nested_name_spec (tree name
)
6506 if (CLASS_TYPE_P (name
)
6507 && ((CLASSTYPE_USE_TEMPLATE (name
)
6508 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (name
)))
6509 || CLASSTYPE_IS_TEMPLATE (name
)))
6512 if (TREE_CODE (name
) == TYPENAME_TYPE
6513 && TREE_CODE (TYPENAME_TYPE_FULLNAME (name
)) == TEMPLATE_ID_EXPR
)
6515 /* Alias templates are also OK. */
6516 else if (alias_template_specialization_p (name
, nt_opaque
))
6519 permerror (input_location
, TYPE_P (name
)
6520 ? G_("%qT is not a template")
6521 : G_("%qD is not a template"),
6525 /* Parse an (optional) nested-name-specifier.
6527 nested-name-specifier: [C++98]
6528 class-or-namespace-name :: nested-name-specifier [opt]
6529 class-or-namespace-name :: template nested-name-specifier [opt]
6531 nested-name-specifier: [C++0x]
6534 nested-name-specifier identifier ::
6535 nested-name-specifier template [opt] simple-template-id ::
6537 PARSER->SCOPE should be set appropriately before this function is
6538 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
6539 effect. TYPE_P is TRUE if we non-type bindings should be ignored
6542 Sets PARSER->SCOPE to the class (TYPE) or namespace
6543 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
6544 it unchanged if there is no nested-name-specifier. Returns the new
6545 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
6547 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
6548 part of a declaration and/or decl-specifier. */
6551 cp_parser_nested_name_specifier_opt (cp_parser
*parser
,
6552 bool typename_keyword_p
,
6553 bool check_dependency_p
,
6555 bool is_declaration
,
6556 bool template_keyword_p
/* = false */)
6558 bool success
= false;
6559 cp_token_position start
= 0;
6562 /* Remember where the nested-name-specifier starts. */
6563 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
6564 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_NESTED_NAME_SPECIFIER
))
6566 start
= cp_lexer_token_position (parser
->lexer
, false);
6567 push_deferring_access_checks (dk_deferred
);
6574 tree saved_qualifying_scope
;
6576 /* Spot cases that cannot be the beginning of a
6577 nested-name-specifier. */
6578 token
= cp_lexer_peek_token (parser
->lexer
);
6580 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6581 the already parsed nested-name-specifier. */
6582 if (token
->type
== CPP_NESTED_NAME_SPECIFIER
)
6584 /* Grab the nested-name-specifier and continue the loop. */
6585 cp_parser_pre_parsed_nested_name_specifier (parser
);
6586 /* If we originally encountered this nested-name-specifier
6587 with IS_DECLARATION set to false, we will not have
6588 resolved TYPENAME_TYPEs, so we must do so here. */
6590 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6592 new_scope
= resolve_typename_type (parser
->scope
,
6593 /*only_current_p=*/false);
6594 if (TREE_CODE (new_scope
) != TYPENAME_TYPE
)
6595 parser
->scope
= new_scope
;
6601 /* Spot cases that cannot be the beginning of a
6602 nested-name-specifier. On the second and subsequent times
6603 through the loop, we look for the `template' keyword. */
6604 if (success
&& token
->keyword
== RID_TEMPLATE
)
6606 /* A template-id can start a nested-name-specifier. */
6607 else if (token
->type
== CPP_TEMPLATE_ID
)
6609 /* DR 743: decltype can be used in a nested-name-specifier. */
6610 else if (token_is_decltype (token
))
6614 /* If the next token is not an identifier, then it is
6615 definitely not a type-name or namespace-name. */
6616 if (token
->type
!= CPP_NAME
)
6618 /* If the following token is neither a `<' (to begin a
6619 template-id), nor a `::', then we are not looking at a
6620 nested-name-specifier. */
6621 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
6623 if (token
->type
== CPP_COLON
6624 && parser
->colon_corrects_to_scope_p
6625 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_NAME
)
6627 gcc_rich_location
richloc (token
->location
);
6628 richloc
.add_fixit_replace ("::");
6630 "found %<:%> in nested-name-specifier, "
6632 token
->type
= CPP_SCOPE
;
6635 if (token
->type
!= CPP_SCOPE
6636 && !cp_parser_nth_token_starts_template_argument_list_p
6641 /* The nested-name-specifier is optional, so we parse
6643 cp_parser_parse_tentatively (parser
);
6645 /* Look for the optional `template' keyword, if this isn't the
6646 first time through the loop. */
6649 template_keyword_p
= cp_parser_optional_template_keyword (parser
);
6650 /* DR1710: "In a qualified-id used as the name in
6651 a typename-specifier, elaborated-type-specifier, using-declaration,
6652 or class-or-decltype, an optional keyword template appearing at
6653 the top level is ignored." */
6654 if (!template_keyword_p
6655 && typename_keyword_p
6656 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
6657 template_keyword_p
= true;
6660 /* Save the old scope since the name lookup we are about to do
6661 might destroy it. */
6662 old_scope
= parser
->scope
;
6663 saved_qualifying_scope
= parser
->qualifying_scope
;
6664 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6665 look up names in "X<T>::I" in order to determine that "Y" is
6666 a template. So, if we have a typename at this point, we make
6667 an effort to look through it. */
6669 && !typename_keyword_p
6671 && TREE_CODE (parser
->scope
) == TYPENAME_TYPE
)
6672 parser
->scope
= resolve_typename_type (parser
->scope
,
6673 /*only_current_p=*/false);
6674 /* Parse the qualifying entity. */
6676 = cp_parser_qualifying_entity (parser
,
6682 /* Look for the `::' token. */
6683 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
6685 /* If we found what we wanted, we keep going; otherwise, we're
6687 if (!cp_parser_parse_definitely (parser
))
6689 bool error_p
= false;
6691 /* Restore the OLD_SCOPE since it was valid before the
6692 failed attempt at finding the last
6693 class-or-namespace-name. */
6694 parser
->scope
= old_scope
;
6695 parser
->qualifying_scope
= saved_qualifying_scope
;
6697 /* If the next token is a decltype, and the one after that is a
6698 `::', then the decltype has failed to resolve to a class or
6699 enumeration type. Give this error even when parsing
6700 tentatively since it can't possibly be valid--and we're going
6701 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6702 won't get another chance.*/
6703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DECLTYPE
)
6704 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6707 token
= cp_lexer_consume_token (parser
->lexer
);
6708 tree dtype
= token
->u
.tree_check_value
->value
;
6709 if (dtype
!= error_mark_node
)
6710 error_at (token
->location
, "%<decltype%> evaluates to %qT, "
6711 "which is not a class or enumeration type",
6713 parser
->scope
= error_mark_node
;
6717 cp_lexer_consume_token (parser
->lexer
);
6720 if (cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
)
6721 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_SCOPE
))
6723 /* If we have a non-type template-id followed by ::, it can't
6724 possibly be valid. */
6725 token
= cp_lexer_peek_token (parser
->lexer
);
6726 tree tid
= token
->u
.tree_check_value
->value
;
6727 if (TREE_CODE (tid
) == TEMPLATE_ID_EXPR
6728 && TREE_CODE (TREE_OPERAND (tid
, 0)) != IDENTIFIER_NODE
)
6730 tree tmpl
= NULL_TREE
;
6731 if (is_overloaded_fn (tid
))
6733 tree fns
= get_fns (tid
);
6734 if (OVL_SINGLE_P (fns
))
6735 tmpl
= OVL_FIRST (fns
);
6736 if (function_concept_p (fns
))
6737 error_at (token
->location
, "concept-id %qD "
6738 "in nested-name-specifier", tid
);
6740 error_at (token
->location
, "function template-id "
6741 "%qD in nested-name-specifier", tid
);
6745 tmpl
= TREE_OPERAND (tid
, 0);
6746 if (variable_concept_p (tmpl
)
6747 || standard_concept_p (tmpl
))
6748 error_at (token
->location
, "concept-id %qD "
6749 "in nested-name-specifier", tid
);
6752 /* Variable template. */
6753 gcc_assert (variable_template_p (tmpl
));
6754 error_at (token
->location
, "variable template-id "
6755 "%qD in nested-name-specifier", tid
);
6759 inform (DECL_SOURCE_LOCATION (tmpl
),
6760 "%qD declared here", tmpl
);
6762 parser
->scope
= error_mark_node
;
6766 cp_lexer_consume_token (parser
->lexer
);
6767 cp_lexer_consume_token (parser
->lexer
);
6771 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
6773 /* If the next token is an identifier, and the one after
6774 that is a `::', then any valid interpretation would have
6775 found a class-or-namespace-name. */
6776 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
6777 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
6779 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
6782 token
= cp_lexer_consume_token (parser
->lexer
);
6785 if (!token
->error_reported
)
6788 tree ambiguous_decls
;
6790 decl
= cp_parser_lookup_name (parser
, token
->u
.value
,
6792 /*is_template=*/false,
6793 /*is_namespace=*/false,
6794 /*check_dependency=*/true,
6797 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
6798 error_at (token
->location
,
6799 "%qD used without template arguments",
6801 else if (ambiguous_decls
)
6803 // cp_parser_lookup_name has the same diagnostic,
6804 // thus make sure to emit it at most once.
6805 if (cp_parser_uncommitted_to_tentative_parse_p
6808 error_at (token
->location
,
6809 "reference to %qD is ambiguous",
6811 print_candidates (ambiguous_decls
);
6813 decl
= error_mark_node
;
6817 if (cxx_dialect
!= cxx98
)
6818 cp_parser_name_lookup_error
6819 (parser
, token
->u
.value
, decl
, NLE_NOT_CXX98
,
6822 cp_parser_name_lookup_error
6823 (parser
, token
->u
.value
, decl
, NLE_CXX98
,
6827 parser
->scope
= error_mark_node
;
6829 /* Treat this as a successful nested-name-specifier
6834 If the name found is not a class-name (clause
6835 _class_) or namespace-name (_namespace.def_), the
6836 program is ill-formed. */
6839 cp_lexer_consume_token (parser
->lexer
);
6843 /* We've found one valid nested-name-specifier. */
6845 /* Name lookup always gives us a DECL. */
6846 if (TREE_CODE (new_scope
) == TYPE_DECL
)
6847 new_scope
= TREE_TYPE (new_scope
);
6848 /* Uses of "template" must be followed by actual templates. */
6849 if (template_keyword_p
)
6850 check_template_keyword_in_nested_name_spec (new_scope
);
6851 /* If it is a class scope, try to complete it; we are about to
6852 be looking up names inside the class. */
6853 if (TYPE_P (new_scope
)
6854 /* Since checking types for dependency can be expensive,
6855 avoid doing it if the type is already complete. */
6856 && !COMPLETE_TYPE_P (new_scope
)
6857 /* Do not try to complete dependent types. */
6858 && !dependent_type_p (new_scope
))
6860 new_scope
= complete_type (new_scope
);
6861 /* If it is a typedef to current class, use the current
6862 class instead, as the typedef won't have any names inside
6864 if (!COMPLETE_TYPE_P (new_scope
)
6865 && currently_open_class (new_scope
))
6866 new_scope
= TYPE_MAIN_VARIANT (new_scope
);
6868 /* Make sure we look in the right scope the next time through
6870 parser
->scope
= new_scope
;
6873 /* If parsing tentatively, replace the sequence of tokens that makes
6874 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6875 token. That way, should we re-parse the token stream, we will
6876 not have to repeat the effort required to do the parse, nor will
6877 we issue duplicate error messages. */
6878 if (success
&& start
)
6882 token
= cp_lexer_token_at (parser
->lexer
, start
);
6883 /* Reset the contents of the START token. */
6884 token
->type
= CPP_NESTED_NAME_SPECIFIER
;
6885 /* Retrieve any deferred checks. Do not pop this access checks yet
6886 so the memory will not be reclaimed during token replacing below. */
6887 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
6888 token
->tree_check_p
= true;
6889 token
->u
.tree_check_value
->value
= parser
->scope
;
6890 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
6891 token
->u
.tree_check_value
->qualifying_scope
=
6892 parser
->qualifying_scope
;
6893 token
->keyword
= RID_MAX
;
6895 /* Purge all subsequent tokens. */
6896 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
6900 pop_to_parent_deferring_access_checks ();
6902 return success
? parser
->scope
: NULL_TREE
;
6905 /* Parse a nested-name-specifier. See
6906 cp_parser_nested_name_specifier_opt for details. This function
6907 behaves identically, except that it will an issue an error if no
6908 nested-name-specifier is present. */
6911 cp_parser_nested_name_specifier (cp_parser
*parser
,
6912 bool typename_keyword_p
,
6913 bool check_dependency_p
,
6915 bool is_declaration
)
6919 /* Look for the nested-name-specifier. */
6920 scope
= cp_parser_nested_name_specifier_opt (parser
,
6925 /* If it was not present, issue an error message. */
6928 cp_parser_error (parser
, "expected nested-name-specifier");
6929 parser
->scope
= NULL_TREE
;
6935 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6936 this is either a class-name or a namespace-name (which corresponds
6937 to the class-or-namespace-name production in the grammar). For
6938 C++0x, it can also be a type-name that refers to an enumeration
6939 type or a simple-template-id.
6941 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6942 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6943 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6944 TYPE_P is TRUE iff the next name should be taken as a class-name,
6945 even the same name is declared to be another entity in the same
6948 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6949 specified by the class-or-namespace-name. If neither is found the
6950 ERROR_MARK_NODE is returned. */
6953 cp_parser_qualifying_entity (cp_parser
*parser
,
6954 bool typename_keyword_p
,
6955 bool template_keyword_p
,
6956 bool check_dependency_p
,
6958 bool is_declaration
)
6961 tree saved_qualifying_scope
;
6962 tree saved_object_scope
;
6965 bool successful_parse_p
;
6967 /* DR 743: decltype can appear in a nested-name-specifier. */
6968 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
6970 scope
= cp_parser_decltype (parser
);
6971 if (TREE_CODE (scope
) != ENUMERAL_TYPE
6972 && !MAYBE_CLASS_TYPE_P (scope
))
6974 cp_parser_simulate_error (parser
);
6975 return error_mark_node
;
6977 if (TYPE_NAME (scope
))
6978 scope
= TYPE_NAME (scope
);
6982 /* Before we try to parse the class-name, we must save away the
6983 current PARSER->SCOPE since cp_parser_class_name will destroy
6985 saved_scope
= parser
->scope
;
6986 saved_qualifying_scope
= parser
->qualifying_scope
;
6987 saved_object_scope
= parser
->object_scope
;
6988 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6989 there is no need to look for a namespace-name. */
6990 only_class_p
= template_keyword_p
6991 || (saved_scope
&& TYPE_P (saved_scope
) && cxx_dialect
== cxx98
);
6993 cp_parser_parse_tentatively (parser
);
6994 scope
= cp_parser_class_name (parser
,
6997 type_p
? class_type
: none_type
,
6999 /*class_head_p=*/false,
7001 /*enum_ok=*/cxx_dialect
> cxx98
);
7002 successful_parse_p
= only_class_p
|| cp_parser_parse_definitely (parser
);
7003 /* If that didn't work, try for a namespace-name. */
7004 if (!only_class_p
&& !successful_parse_p
)
7006 /* Restore the saved scope. */
7007 parser
->scope
= saved_scope
;
7008 parser
->qualifying_scope
= saved_qualifying_scope
;
7009 parser
->object_scope
= saved_object_scope
;
7010 /* If we are not looking at an identifier followed by the scope
7011 resolution operator, then this is not part of a
7012 nested-name-specifier. (Note that this function is only used
7013 to parse the components of a nested-name-specifier.) */
7014 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
)
7015 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
7016 return error_mark_node
;
7017 scope
= cp_parser_namespace_name (parser
);
7023 /* Return true if we are looking at a compound-literal, false otherwise. */
7026 cp_parser_compound_literal_p (cp_parser
*parser
)
7028 cp_lexer_save_tokens (parser
->lexer
);
7030 /* Skip tokens until the next token is a closing parenthesis.
7031 If we find the closing `)', and the next token is a `{', then
7032 we are looking at a compound-literal. */
7033 bool compound_literal_p
7034 = (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
7035 /*consume_paren=*/true)
7036 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
));
7038 /* Roll back the tokens we skipped. */
7039 cp_lexer_rollback_tokens (parser
->lexer
);
7041 return compound_literal_p
;
7044 /* Return true if EXPR is the integer constant zero or a complex constant
7045 of zero, without any folding, but ignoring location wrappers. */
7048 literal_integer_zerop (const_tree expr
)
7050 return (location_wrapper_p (expr
)
7051 && integer_zerop (TREE_OPERAND (expr
, 0)));
7054 /* Parse a postfix-expression.
7058 postfix-expression [ expression ]
7059 postfix-expression ( expression-list [opt] )
7060 simple-type-specifier ( expression-list [opt] )
7061 typename :: [opt] nested-name-specifier identifier
7062 ( expression-list [opt] )
7063 typename :: [opt] nested-name-specifier template [opt] template-id
7064 ( expression-list [opt] )
7065 postfix-expression . template [opt] id-expression
7066 postfix-expression -> template [opt] id-expression
7067 postfix-expression . pseudo-destructor-name
7068 postfix-expression -> pseudo-destructor-name
7069 postfix-expression ++
7070 postfix-expression --
7071 dynamic_cast < type-id > ( expression )
7072 static_cast < type-id > ( expression )
7073 reinterpret_cast < type-id > ( expression )
7074 const_cast < type-id > ( expression )
7075 typeid ( expression )
7081 ( type-id ) { initializer-list , [opt] }
7083 This extension is a GNU version of the C99 compound-literal
7084 construct. (The C99 grammar uses `type-name' instead of `type-id',
7085 but they are essentially the same concept.)
7087 If ADDRESS_P is true, the postfix expression is the operand of the
7088 `&' operator. CAST_P is true if this expression is the target of a
7091 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
7092 class member access expressions [expr.ref].
7094 Returns a representation of the expression. */
7097 cp_parser_postfix_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
7098 bool member_access_only_p
, bool decltype_p
,
7099 cp_id_kind
* pidk_return
)
7104 cp_id_kind idk
= CP_ID_KIND_NONE
;
7105 cp_expr postfix_expression
= NULL_TREE
;
7106 bool is_member_access
= false;
7108 /* Peek at the next token. */
7109 token
= cp_lexer_peek_token (parser
->lexer
);
7110 loc
= token
->location
;
7111 location_t start_loc
= get_range_from_loc (line_table
, loc
).m_start
;
7113 /* Some of the productions are determined by keywords. */
7114 keyword
= token
->keyword
;
7124 const char *saved_message
;
7125 bool saved_in_type_id_in_expr_p
;
7127 /* All of these can be handled in the same way from the point
7128 of view of parsing. Begin by consuming the token
7129 identifying the cast. */
7130 cp_lexer_consume_token (parser
->lexer
);
7132 /* New types cannot be defined in the cast. */
7133 saved_message
= parser
->type_definition_forbidden_message
;
7134 parser
->type_definition_forbidden_message
7135 = G_("types may not be defined in casts");
7137 /* Look for the opening `<'. */
7138 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
7139 /* Parse the type to which we are casting. */
7140 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7141 parser
->in_type_id_in_expr_p
= true;
7142 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
7144 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7145 /* Look for the closing `>'. */
7146 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
7147 /* Restore the old message. */
7148 parser
->type_definition_forbidden_message
= saved_message
;
7150 bool saved_greater_than_is_operator_p
7151 = parser
->greater_than_is_operator_p
;
7152 parser
->greater_than_is_operator_p
= true;
7154 /* And the expression which is being cast. */
7155 matching_parens parens
;
7156 parens
.require_open (parser
);
7157 expression
= cp_parser_expression (parser
, & idk
, /*cast_p=*/true);
7158 cp_token
*close_paren
= cp_parser_require (parser
, CPP_CLOSE_PAREN
,
7160 location_t end_loc
= close_paren
?
7161 close_paren
->location
: UNKNOWN_LOCATION
;
7163 parser
->greater_than_is_operator_p
7164 = saved_greater_than_is_operator_p
;
7166 /* Only type conversions to integral or enumeration types
7167 can be used in constant-expressions. */
7168 if (!cast_valid_in_integral_constant_expression_p (type
)
7169 && cp_parser_non_integral_constant_expression (parser
, NIC_CAST
))
7171 postfix_expression
= error_mark_node
;
7175 /* Construct a location e.g. :
7176 reinterpret_cast <int *> (expr)
7177 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7178 ranging from the start of the "*_cast" token to the final closing
7179 paren, with the caret at the start. */
7180 location_t cp_cast_loc
= make_location (start_loc
, start_loc
, end_loc
);
7186 = build_dynamic_cast (cp_cast_loc
, type
, expression
,
7187 tf_warning_or_error
);
7191 = build_static_cast (cp_cast_loc
, type
, expression
,
7192 tf_warning_or_error
);
7196 = build_reinterpret_cast (cp_cast_loc
, type
, expression
,
7197 tf_warning_or_error
);
7201 = build_const_cast (cp_cast_loc
, type
, expression
,
7202 tf_warning_or_error
);
7213 const char *saved_message
;
7214 bool saved_in_type_id_in_expr_p
;
7216 /* Consume the `typeid' token. */
7217 cp_lexer_consume_token (parser
->lexer
);
7218 /* Look for the `(' token. */
7219 matching_parens parens
;
7220 parens
.require_open (parser
);
7221 /* Types cannot be defined in a `typeid' expression. */
7222 saved_message
= parser
->type_definition_forbidden_message
;
7223 parser
->type_definition_forbidden_message
7224 = G_("types may not be defined in a %<typeid%> expression");
7225 /* We can't be sure yet whether we're looking at a type-id or an
7227 cp_parser_parse_tentatively (parser
);
7228 /* Try a type-id first. */
7229 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7230 parser
->in_type_id_in_expr_p
= true;
7231 type
= cp_parser_type_id (parser
);
7232 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7233 /* Look for the `)' token. Otherwise, we can't be sure that
7234 we're not looking at an expression: consider `typeid (int
7235 (3))', for example. */
7236 cp_token
*close_paren
= parens
.require_close (parser
);
7237 /* If all went well, simply lookup the type-id. */
7238 if (cp_parser_parse_definitely (parser
))
7239 postfix_expression
= get_typeid (type
, tf_warning_or_error
);
7240 /* Otherwise, fall back to the expression variant. */
7245 /* Look for an expression. */
7246 expression
= cp_parser_expression (parser
, & idk
);
7247 /* Compute its typeid. */
7248 postfix_expression
= build_typeid (expression
, tf_warning_or_error
);
7249 /* Look for the `)' token. */
7250 close_paren
= parens
.require_close (parser
);
7252 /* Restore the saved message. */
7253 parser
->type_definition_forbidden_message
= saved_message
;
7254 /* `typeid' may not appear in an integral constant expression. */
7255 if (cp_parser_non_integral_constant_expression (parser
, NIC_TYPEID
))
7256 postfix_expression
= error_mark_node
;
7258 /* Construct a location e.g. :
7261 ranging from the start of the "typeid" token to the final closing
7262 paren, with the caret at the start. */
7265 location_t typeid_loc
7266 = make_location (start_loc
, start_loc
, close_paren
->location
);
7267 postfix_expression
.set_location (typeid_loc
);
7268 postfix_expression
.maybe_add_location_wrapper ();
7276 /* The syntax permitted here is the same permitted for an
7277 elaborated-type-specifier. */
7278 ++parser
->prevent_constrained_type_specifiers
;
7279 type
= cp_parser_elaborated_type_specifier (parser
,
7280 /*is_friend=*/false,
7281 /*is_declaration=*/false);
7282 --parser
->prevent_constrained_type_specifiers
;
7283 postfix_expression
= cp_parser_functional_cast (parser
, type
);
7288 case RID_BUILTIN_SHUFFLE
:
7289 case RID_BUILTIN_LAUNDER
:
7291 vec
<tree
, va_gc
> *vec
;
7295 cp_lexer_consume_token (parser
->lexer
);
7296 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
7297 /*cast_p=*/false, /*allow_expansion_p=*/true,
7298 /*non_constant_p=*/NULL
);
7301 postfix_expression
= error_mark_node
;
7305 FOR_EACH_VEC_ELT (*vec
, i
, p
)
7311 if (vec
->length () == 1)
7313 = cp_build_addressof (loc
, (*vec
)[0], tf_warning_or_error
);
7316 error_at (loc
, "wrong number of arguments to "
7317 "%<__builtin_addressof%>");
7318 postfix_expression
= error_mark_node
;
7322 case RID_BUILTIN_LAUNDER
:
7323 if (vec
->length () == 1)
7324 postfix_expression
= finish_builtin_launder (loc
, (*vec
)[0],
7325 tf_warning_or_error
);
7328 error_at (loc
, "wrong number of arguments to "
7329 "%<__builtin_launder%>");
7330 postfix_expression
= error_mark_node
;
7334 case RID_BUILTIN_SHUFFLE
:
7335 if (vec
->length () == 2)
7337 = build_x_vec_perm_expr (loc
, (*vec
)[0], NULL_TREE
,
7338 (*vec
)[1], tf_warning_or_error
);
7339 else if (vec
->length () == 3)
7341 = build_x_vec_perm_expr (loc
, (*vec
)[0], (*vec
)[1],
7342 (*vec
)[2], tf_warning_or_error
);
7345 error_at (loc
, "wrong number of arguments to "
7346 "%<__builtin_shuffle%>");
7347 postfix_expression
= error_mark_node
;
7357 case RID_BUILTIN_CONVERTVECTOR
:
7361 /* Consume the `__builtin_convertvector' token. */
7362 cp_lexer_consume_token (parser
->lexer
);
7363 /* Look for the opening `('. */
7364 matching_parens parens
;
7365 parens
.require_open (parser
);
7366 /* Now, parse the assignment-expression. */
7367 expression
= cp_parser_assignment_expression (parser
);
7368 /* Look for the `,'. */
7369 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7370 location_t type_location
7371 = cp_lexer_peek_token (parser
->lexer
)->location
;
7372 /* Parse the type-id. */
7374 type_id_in_expr_sentinel
s (parser
);
7375 type
= cp_parser_type_id (parser
);
7377 /* Look for the closing `)'. */
7378 parens
.require_close (parser
);
7379 return cp_build_vec_convert (expression
, type_location
, type
,
7380 tf_warning_or_error
);
7383 case RID_BUILTIN_BIT_CAST
:
7387 /* Consume the `__builtin_bit_cast' token. */
7388 cp_lexer_consume_token (parser
->lexer
);
7389 /* Look for the opening `('. */
7390 matching_parens parens
;
7391 parens
.require_open (parser
);
7392 location_t type_location
7393 = cp_lexer_peek_token (parser
->lexer
)->location
;
7394 /* Parse the type-id. */
7396 type_id_in_expr_sentinel
s (parser
);
7397 type
= cp_parser_type_id (parser
);
7399 /* Look for the `,'. */
7400 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
7401 /* Now, parse the assignment-expression. */
7402 expression
= cp_parser_assignment_expression (parser
);
7403 /* Look for the closing `)'. */
7404 parens
.require_close (parser
);
7405 return cp_build_bit_cast (type_location
, type
, expression
,
7406 tf_warning_or_error
);
7413 /* If the next thing is a simple-type-specifier, we may be
7414 looking at a functional cast. We could also be looking at
7415 an id-expression. So, we try the functional cast, and if
7416 that doesn't work we fall back to the primary-expression. */
7417 cp_parser_parse_tentatively (parser
);
7418 /* Look for the simple-type-specifier. */
7419 ++parser
->prevent_constrained_type_specifiers
;
7420 type
= cp_parser_simple_type_specifier (parser
,
7421 /*decl_specs=*/NULL
,
7422 CP_PARSER_FLAGS_NONE
);
7423 --parser
->prevent_constrained_type_specifiers
;
7424 /* Parse the cast itself. */
7425 if (!cp_parser_error_occurred (parser
))
7427 = cp_parser_functional_cast (parser
, type
);
7428 /* If that worked, we're done. */
7429 if (cp_parser_parse_definitely (parser
))
7432 /* If the functional-cast didn't work out, try a
7433 compound-literal. */
7434 if (cp_parser_allow_gnu_extensions_p (parser
)
7435 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
7437 cp_expr initializer
= NULL_TREE
;
7439 cp_parser_parse_tentatively (parser
);
7441 matching_parens parens
;
7442 parens
.consume_open (parser
);
7444 /* Avoid calling cp_parser_type_id pointlessly, see comment
7445 in cp_parser_cast_expression about c++/29234. */
7446 if (!cp_parser_compound_literal_p (parser
))
7447 cp_parser_simulate_error (parser
);
7450 /* Parse the type. */
7451 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
7452 parser
->in_type_id_in_expr_p
= true;
7453 type
= cp_parser_type_id (parser
);
7454 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
7455 parens
.require_close (parser
);
7458 /* If things aren't going well, there's no need to
7460 if (!cp_parser_error_occurred (parser
))
7462 bool non_constant_p
;
7463 /* Parse the brace-enclosed initializer list. */
7464 initializer
= cp_parser_braced_list (parser
,
7467 /* If that worked, we're definitely looking at a
7468 compound-literal expression. */
7469 if (cp_parser_parse_definitely (parser
))
7471 /* Warn the user that a compound literal is not
7472 allowed in standard C++. */
7473 pedwarn (input_location
, OPT_Wpedantic
,
7474 "ISO C++ forbids compound-literals");
7475 /* For simplicity, we disallow compound literals in
7476 constant-expressions. We could
7477 allow compound literals of integer type, whose
7478 initializer was a constant, in constant
7479 expressions. Permitting that usage, as a further
7480 extension, would not change the meaning of any
7481 currently accepted programs. (Of course, as
7482 compound literals are not part of ISO C++, the
7483 standard has nothing to say.) */
7484 if (cp_parser_non_integral_constant_expression (parser
,
7487 postfix_expression
= error_mark_node
;
7490 /* Form the representation of the compound-literal. */
7492 = finish_compound_literal (type
, initializer
,
7493 tf_warning_or_error
, fcl_c99
);
7494 postfix_expression
.set_location (initializer
.get_location ());
7499 /* It must be a primary-expression. */
7501 = cp_parser_primary_expression (parser
, address_p
, cast_p
,
7502 /*template_arg_p=*/false,
7509 /* Note that we don't need to worry about calling build_cplus_new on a
7510 class-valued CALL_EXPR in decltype when it isn't the end of the
7511 postfix-expression; unary_complex_lvalue will take care of that for
7514 /* Keep looping until the postfix-expression is complete. */
7517 if (idk
== CP_ID_KIND_UNQUALIFIED
7518 && identifier_p (postfix_expression
)
7519 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
7520 /* It is not a Koenig lookup function call. */
7522 = unqualified_name_lookup_error (postfix_expression
);
7524 /* Peek at the next token. */
7525 token
= cp_lexer_peek_token (parser
->lexer
);
7527 switch (token
->type
)
7529 case CPP_OPEN_SQUARE
:
7530 if (cp_next_tokens_can_be_std_attribute_p (parser
))
7532 cp_parser_error (parser
,
7533 "two consecutive %<[%> shall "
7534 "only introduce an attribute");
7535 return error_mark_node
;
7538 = cp_parser_postfix_open_square_expression (parser
,
7542 postfix_expression
.set_range (start_loc
,
7543 postfix_expression
.get_location ());
7545 idk
= CP_ID_KIND_NONE
;
7546 is_member_access
= false;
7549 case CPP_OPEN_PAREN
:
7550 /* postfix-expression ( expression-list [opt] ) */
7553 bool is_builtin_constant_p
;
7554 bool saved_integral_constant_expression_p
= false;
7555 bool saved_non_integral_constant_expression_p
= false;
7556 tsubst_flags_t complain
= complain_flags (decltype_p
);
7557 vec
<tree
, va_gc
> *args
;
7558 location_t close_paren_loc
= UNKNOWN_LOCATION
;
7560 is_member_access
= false;
7562 tree stripped_expression
7563 = tree_strip_any_location_wrapper (postfix_expression
);
7564 is_builtin_constant_p
7565 = DECL_IS_BUILTIN_CONSTANT_P (stripped_expression
);
7566 if (is_builtin_constant_p
)
7568 /* The whole point of __builtin_constant_p is to allow
7569 non-constant expressions to appear as arguments. */
7570 saved_integral_constant_expression_p
7571 = parser
->integral_constant_expression_p
;
7572 saved_non_integral_constant_expression_p
7573 = parser
->non_integral_constant_expression_p
;
7574 parser
->integral_constant_expression_p
= false;
7576 args
= (cp_parser_parenthesized_expression_list
7578 /*cast_p=*/false, /*allow_expansion_p=*/true,
7579 /*non_constant_p=*/NULL
,
7580 /*close_paren_loc=*/&close_paren_loc
,
7581 /*wrap_locations_p=*/true));
7582 if (is_builtin_constant_p
)
7584 parser
->integral_constant_expression_p
7585 = saved_integral_constant_expression_p
;
7586 parser
->non_integral_constant_expression_p
7587 = saved_non_integral_constant_expression_p
;
7592 postfix_expression
= error_mark_node
;
7596 /* Function calls are not permitted in
7597 constant-expressions. */
7598 if (! builtin_valid_in_constant_expr_p (postfix_expression
)
7599 && cp_parser_non_integral_constant_expression (parser
,
7602 postfix_expression
= error_mark_node
;
7603 release_tree_vector (args
);
7608 if (idk
== CP_ID_KIND_UNQUALIFIED
7609 || idk
== CP_ID_KIND_TEMPLATE_ID
)
7611 if (identifier_p (postfix_expression
)
7612 /* In C++20, we may need to perform ADL for a template
7614 || (TREE_CODE (postfix_expression
) == TEMPLATE_ID_EXPR
7615 && identifier_p (TREE_OPERAND (postfix_expression
, 0))))
7617 if (!args
->is_empty ())
7620 if (!any_type_dependent_arguments_p (args
))
7622 = perform_koenig_lookup (postfix_expression
, args
,
7627 = unqualified_fn_lookup_error (postfix_expression
);
7629 /* We do not perform argument-dependent lookup if
7630 normal lookup finds a non-function, in accordance
7631 with the expected resolution of DR 218. */
7632 else if (!args
->is_empty ()
7633 && is_overloaded_fn (postfix_expression
))
7635 /* Do not do argument dependent lookup if regular
7636 lookup finds a member function or a block-scope
7637 function declaration. [basic.lookup.argdep]/3 */
7638 bool do_adl_p
= true;
7639 tree fns
= get_fns (postfix_expression
);
7640 for (lkp_iterator
iter (fns
); iter
; ++iter
)
7642 tree fn
= STRIP_TEMPLATE (*iter
);
7643 if ((TREE_CODE (fn
) == USING_DECL
7644 && DECL_DEPENDENT_P (fn
))
7645 || DECL_FUNCTION_MEMBER_P (fn
)
7646 || DECL_LOCAL_DECL_P (fn
))
7656 if (!any_type_dependent_arguments_p (args
))
7658 = perform_koenig_lookup (postfix_expression
, args
,
7664 if (TREE_CODE (postfix_expression
) == COMPONENT_REF
)
7666 tree instance
= TREE_OPERAND (postfix_expression
, 0);
7667 tree fn
= TREE_OPERAND (postfix_expression
, 1);
7669 if (processing_template_decl
7670 && (type_dependent_object_expression_p (instance
)
7671 || (!BASELINK_P (fn
)
7672 && TREE_CODE (fn
) != FIELD_DECL
)
7673 || type_dependent_expression_p (fn
)
7674 || any_type_dependent_arguments_p (args
)))
7676 maybe_generic_this_capture (instance
, fn
);
7678 = build_min_nt_call_vec (postfix_expression
, args
);
7680 else if (BASELINK_P (fn
))
7683 = (build_new_method_call
7684 (instance
, fn
, &args
, NULL_TREE
,
7685 (idk
== CP_ID_KIND_QUALIFIED
7686 ? LOOKUP_NORMAL
|LOOKUP_NONVIRTUAL
7693 = finish_call_expr (postfix_expression
, &args
,
7694 /*disallow_virtual=*/false,
7698 else if (TREE_CODE (postfix_expression
) == OFFSET_REF
7699 || TREE_CODE (postfix_expression
) == MEMBER_REF
7700 || TREE_CODE (postfix_expression
) == DOTSTAR_EXPR
)
7701 postfix_expression
= (build_offset_ref_call_from_tree
7702 (postfix_expression
, &args
,
7704 else if (idk
== CP_ID_KIND_QUALIFIED
)
7705 /* A call to a static class member, or a namespace-scope
7708 = finish_call_expr (postfix_expression
, &args
,
7709 /*disallow_virtual=*/true,
7713 /* All other function calls. */
7715 = finish_call_expr (postfix_expression
, &args
,
7716 /*disallow_virtual=*/false,
7720 if (close_paren_loc
!= UNKNOWN_LOCATION
)
7722 location_t combined_loc
= make_location (token
->location
,
7725 postfix_expression
.set_location (combined_loc
);
7728 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7729 idk
= CP_ID_KIND_NONE
;
7731 release_tree_vector (args
);
7737 /* postfix-expression . template [opt] id-expression
7738 postfix-expression . pseudo-destructor-name
7739 postfix-expression -> template [opt] id-expression
7740 postfix-expression -> pseudo-destructor-name */
7742 /* Consume the `.' or `->' operator. */
7743 cp_lexer_consume_token (parser
->lexer
);
7746 = cp_parser_postfix_dot_deref_expression (parser
, token
->type
,
7750 is_member_access
= true;
7754 /* postfix-expression ++ */
7755 /* Consume the `++' token. */
7756 cp_lexer_consume_token (parser
->lexer
);
7757 /* Generate a representation for the complete expression. */
7759 = finish_increment_expr (postfix_expression
,
7760 POSTINCREMENT_EXPR
);
7761 /* Increments may not appear in constant-expressions. */
7762 if (cp_parser_non_integral_constant_expression (parser
, NIC_INC
))
7763 postfix_expression
= error_mark_node
;
7764 idk
= CP_ID_KIND_NONE
;
7765 is_member_access
= false;
7768 case CPP_MINUS_MINUS
:
7769 /* postfix-expression -- */
7770 /* Consume the `--' token. */
7771 cp_lexer_consume_token (parser
->lexer
);
7772 /* Generate a representation for the complete expression. */
7774 = finish_increment_expr (postfix_expression
,
7775 POSTDECREMENT_EXPR
);
7776 /* Decrements may not appear in constant-expressions. */
7777 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEC
))
7778 postfix_expression
= error_mark_node
;
7779 idk
= CP_ID_KIND_NONE
;
7780 is_member_access
= false;
7784 if (pidk_return
!= NULL
)
7785 * pidk_return
= idk
;
7786 if (member_access_only_p
)
7787 return is_member_access
7788 ? postfix_expression
7789 : cp_expr (error_mark_node
);
7791 return postfix_expression
;
7795 /* We should never get here. */
7797 return error_mark_node
;
7800 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7801 by cp_parser_builtin_offsetof. We're looking for
7803 postfix-expression [ expression ]
7804 postfix-expression [ braced-init-list ] (C++11)
7806 FOR_OFFSETOF is set if we're being called in that context, which
7807 changes how we deal with integer constant expressions. */
7810 cp_parser_postfix_open_square_expression (cp_parser
*parser
,
7811 tree postfix_expression
,
7815 tree index
= NULL_TREE
;
7816 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
7817 bool saved_greater_than_is_operator_p
;
7819 /* Consume the `[' token. */
7820 cp_lexer_consume_token (parser
->lexer
);
7822 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
7823 parser
->greater_than_is_operator_p
= true;
7825 /* Parse the index expression. */
7826 /* ??? For offsetof, there is a question of what to allow here. If
7827 offsetof is not being used in an integral constant expression context,
7828 then we *could* get the right answer by computing the value at runtime.
7829 If we are in an integral constant expression context, then we might
7830 could accept any constant expression; hard to say without analysis.
7831 Rather than open the barn door too wide right away, allow only integer
7832 constant expressions here. */
7834 index
= cp_parser_constant_expression (parser
);
7837 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
7839 bool expr_nonconst_p
;
7840 cp_lexer_set_source_position (parser
->lexer
);
7841 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
7842 index
= cp_parser_braced_list (parser
, &expr_nonconst_p
);
7845 index
= cp_parser_expression (parser
, NULL
, /*cast_p=*/false,
7846 /*decltype_p=*/false,
7847 /*warn_comma_p=*/warn_comma_subscript
);
7850 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
7852 /* Look for the closing `]'. */
7853 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
7855 /* Build the ARRAY_REF. */
7856 postfix_expression
= grok_array_decl (loc
, postfix_expression
,
7859 /* When not doing offsetof, array references are not permitted in
7860 constant-expressions. */
7862 && (cp_parser_non_integral_constant_expression (parser
, NIC_ARRAY_REF
)))
7863 postfix_expression
= error_mark_node
;
7865 return postfix_expression
;
7868 /* A subroutine of cp_parser_postfix_dot_deref_expression. Handle dot
7869 dereference of incomplete type, returns true if error_mark_node should
7870 be returned from caller, otherwise adjusts *SCOPE, *POSTFIX_EXPRESSION
7871 and *DEPENDENT_P. */
7874 cp_parser_dot_deref_incomplete (tree
*scope
, cp_expr
*postfix_expression
,
7877 /* In a template, be permissive by treating an object expression
7878 of incomplete type as dependent (after a pedwarn). */
7879 diagnostic_t kind
= (processing_template_decl
7880 && MAYBE_CLASS_TYPE_P (*scope
) ? DK_PEDWARN
: DK_ERROR
);
7882 switch (TREE_CODE (*postfix_expression
))
7885 case REINTERPRET_CAST_EXPR
:
7886 case CONST_CAST_EXPR
:
7887 case STATIC_CAST_EXPR
:
7888 case DYNAMIC_CAST_EXPR
:
7889 case IMPLICIT_CONV_EXPR
:
7890 case VIEW_CONVERT_EXPR
:
7891 case NON_LVALUE_EXPR
:
7895 /* Don't emit any diagnostic for OVERLOADs. */
7899 /* Avoid clobbering e.g. DECLs. */
7900 if (!EXPR_P (*postfix_expression
))
7905 if (kind
== DK_IGNORED
)
7908 location_t exploc
= location_of (*postfix_expression
);
7909 cxx_incomplete_type_diagnostic (exploc
, *postfix_expression
, *scope
, kind
);
7910 if (!MAYBE_CLASS_TYPE_P (*scope
))
7912 if (kind
== DK_ERROR
)
7913 *scope
= *postfix_expression
= error_mark_node
;
7914 else if (processing_template_decl
)
7916 *dependent_p
= true;
7917 *scope
= TREE_TYPE (*postfix_expression
) = NULL_TREE
;
7922 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7923 by cp_parser_builtin_offsetof. We're looking for
7925 postfix-expression . template [opt] id-expression
7926 postfix-expression . pseudo-destructor-name
7927 postfix-expression -> template [opt] id-expression
7928 postfix-expression -> pseudo-destructor-name
7930 FOR_OFFSETOF is set if we're being called in that context. That sorta
7931 limits what of the above we'll actually accept, but nevermind.
7932 TOKEN_TYPE is the "." or "->" token, which will already have been
7933 removed from the stream. */
7936 cp_parser_postfix_dot_deref_expression (cp_parser
*parser
,
7937 enum cpp_ttype token_type
,
7938 cp_expr postfix_expression
,
7939 bool for_offsetof
, cp_id_kind
*idk
,
7940 location_t location
)
7944 bool pseudo_destructor_p
;
7945 tree scope
= NULL_TREE
;
7946 location_t start_loc
= postfix_expression
.get_start ();
7948 /* If this is a `->' operator, dereference the pointer. */
7949 if (token_type
== CPP_DEREF
)
7950 postfix_expression
= build_x_arrow (location
, postfix_expression
,
7951 tf_warning_or_error
);
7952 /* Check to see whether or not the expression is type-dependent and
7953 not the current instantiation. */
7954 dependent_p
= type_dependent_object_expression_p (postfix_expression
);
7955 /* The identifier following the `->' or `.' is not qualified. */
7956 parser
->scope
= NULL_TREE
;
7957 parser
->qualifying_scope
= NULL_TREE
;
7958 parser
->object_scope
= NULL_TREE
;
7959 *idk
= CP_ID_KIND_NONE
;
7961 /* Enter the scope corresponding to the type of the object
7962 given by the POSTFIX_EXPRESSION. */
7965 scope
= TREE_TYPE (postfix_expression
);
7966 /* According to the standard, no expression should ever have
7967 reference type. Unfortunately, we do not currently match
7968 the standard in this respect in that our internal representation
7969 of an expression may have reference type even when the standard
7970 says it does not. Therefore, we have to manually obtain the
7971 underlying type here. */
7972 scope
= non_reference (scope
);
7973 /* The type of the POSTFIX_EXPRESSION must be complete. */
7974 /* Unlike the object expression in other contexts, *this is not
7975 required to be of complete type for purposes of class member
7976 access (5.2.5) outside the member function body. */
7977 if (postfix_expression
!= current_class_ref
7978 && scope
!= error_mark_node
7979 && !currently_open_class (scope
))
7981 scope
= complete_type (scope
);
7982 if (!COMPLETE_TYPE_P (scope
)
7983 && cp_parser_dot_deref_incomplete (&scope
, &postfix_expression
,
7985 return error_mark_node
;
7990 /* Let the name lookup machinery know that we are processing a
7991 class member access expression. */
7992 parser
->context
->object_type
= scope
;
7993 /* If something went wrong, we want to be able to discern that case,
7994 as opposed to the case where there was no SCOPE due to the type
7995 of expression being dependent. */
7997 scope
= error_mark_node
;
7998 /* If the SCOPE was erroneous, make the various semantic analysis
7999 functions exit quickly -- and without issuing additional error
8001 if (scope
== error_mark_node
)
8002 postfix_expression
= error_mark_node
;
8008 tree type
= TREE_TYPE (postfix_expression
);
8009 /* If we don't have a (type-dependent) object of class type, use
8010 typeof to figure out the type of the object. */
8011 if (type
== NULL_TREE
)
8012 type
= finish_typeof (postfix_expression
);
8013 parser
->context
->object_type
= type
;
8016 /* Assume this expression is not a pseudo-destructor access. */
8017 pseudo_destructor_p
= false;
8019 /* If the SCOPE is a scalar type, then, if this is a valid program,
8020 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
8021 is type dependent, it can be pseudo-destructor-name or something else.
8022 Try to parse it as pseudo-destructor-name first. */
8023 if ((scope
&& SCALAR_TYPE_P (scope
)) || dependent_p
)
8028 cp_parser_parse_tentatively (parser
);
8029 /* Parse the pseudo-destructor-name. */
8031 cp_parser_pseudo_destructor_name (parser
, postfix_expression
,
8034 && (cp_parser_error_occurred (parser
)
8035 || !SCALAR_TYPE_P (type
)))
8036 cp_parser_abort_tentative_parse (parser
);
8037 else if (cp_parser_parse_definitely (parser
))
8039 pseudo_destructor_p
= true;
8041 = finish_pseudo_destructor_expr (postfix_expression
,
8046 if (!pseudo_destructor_p
)
8048 /* If the SCOPE is not a scalar type, we are looking at an
8049 ordinary class member access expression, rather than a
8050 pseudo-destructor-name. */
8052 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
8053 /* Parse the id-expression. */
8054 name
= (cp_parser_id_expression
8056 cp_parser_optional_template_keyword (parser
),
8057 /*check_dependency_p=*/true,
8059 /*declarator_p=*/false,
8060 /*optional_p=*/false));
8061 /* In general, build a SCOPE_REF if the member name is qualified.
8062 However, if the name was not dependent and has already been
8063 resolved; there is no need to build the SCOPE_REF. For example;
8065 struct X { void f(); };
8066 template <typename T> void f(T* t) { t->X::f(); }
8068 Even though "t" is dependent, "X::f" is not and has been resolved
8069 to a BASELINK; there is no need to include scope information. */
8071 /* But we do need to remember that there was an explicit scope for
8072 virtual function calls. */
8074 *idk
= CP_ID_KIND_QUALIFIED
;
8076 /* If the name is a template-id that names a type, we will get a
8077 TYPE_DECL here. That is invalid code. */
8078 if (TREE_CODE (name
) == TYPE_DECL
)
8080 error_at (token
->location
, "invalid use of %qD", name
);
8081 postfix_expression
= error_mark_node
;
8085 if (name
!= error_mark_node
&& !BASELINK_P (name
) && parser
->scope
)
8087 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
8089 error_at (token
->location
, "%<%D::%D%> is not a class member",
8090 parser
->scope
, name
);
8091 postfix_expression
= error_mark_node
;
8094 name
= build_qualified_name (/*type=*/NULL_TREE
,
8098 parser
->scope
= NULL_TREE
;
8099 parser
->qualifying_scope
= NULL_TREE
;
8100 parser
->object_scope
= NULL_TREE
;
8102 if (parser
->scope
&& name
&& BASELINK_P (name
))
8103 adjust_result_of_qualified_name_lookup
8104 (name
, parser
->scope
, scope
);
8106 = finish_class_member_access_expr (postfix_expression
, name
,
8108 tf_warning_or_error
);
8109 /* Build a location e.g.:
8112 where the caret is at the deref token, ranging from
8113 the start of postfix_expression to the end of the access expr. */
8114 location_t combined_loc
8115 = make_location (input_location
, start_loc
, parser
->lexer
);
8116 protected_set_expr_location (postfix_expression
, combined_loc
);
8120 /* We no longer need to look up names in the scope of the object on
8121 the left-hand side of the `.' or `->' operator. */
8122 parser
->context
->object_type
= NULL_TREE
;
8124 /* Outside of offsetof, these operators may not appear in
8125 constant-expressions. */
8127 && (cp_parser_non_integral_constant_expression
8128 (parser
, token_type
== CPP_DEREF
? NIC_ARROW
: NIC_POINT
)))
8129 postfix_expression
= error_mark_node
;
8131 return postfix_expression
;
8134 /* Parse a parenthesized expression-list.
8137 assignment-expression
8138 expression-list, assignment-expression
8143 identifier, expression-list
8145 CAST_P is true if this expression is the target of a cast.
8147 ALLOW_EXPANSION_P is true if this expression allows expansion of an
8150 WRAP_LOCATIONS_P is true if expressions within this list for which
8151 CAN_HAVE_LOCATION_P is false should be wrapped with nodes expressing
8152 their source locations.
8154 Returns a vector of trees. Each element is a representation of an
8155 assignment-expression. NULL is returned if the ( and or ) are
8156 missing. An empty, but allocated, vector is returned on no
8157 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
8158 if we are parsing an attribute list for an attribute that wants a
8159 plain identifier argument, normal_attr for an attribute that wants
8160 an expression, or non_attr if we aren't parsing an attribute list. If
8161 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
8162 not all of the expressions in the list were constant.
8163 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
8164 will be written to with the location of the closing parenthesis. If
8165 an error occurs, it may or may not be written to. */
8167 static vec
<tree
, va_gc
> *
8168 cp_parser_parenthesized_expression_list (cp_parser
* parser
,
8169 int is_attribute_list
,
8171 bool allow_expansion_p
,
8172 bool *non_constant_p
,
8173 location_t
*close_paren_loc
,
8174 bool wrap_locations_p
)
8176 vec
<tree
, va_gc
> *expression_list
;
8177 bool fold_expr_p
= is_attribute_list
!= non_attr
;
8178 tree identifier
= NULL_TREE
;
8179 bool saved_greater_than_is_operator_p
;
8181 /* Assume all the expressions will be constant. */
8183 *non_constant_p
= false;
8185 matching_parens parens
;
8186 if (!parens
.require_open (parser
))
8189 expression_list
= make_tree_vector ();
8191 /* Within a parenthesized expression, a `>' token is always
8192 the greater-than operator. */
8193 saved_greater_than_is_operator_p
8194 = parser
->greater_than_is_operator_p
;
8195 parser
->greater_than_is_operator_p
= true;
8197 cp_expr
expr (NULL_TREE
);
8199 /* Consume expressions until there are no more. */
8200 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
8203 /* At the beginning of attribute lists, check to see if the
8204 next token is an identifier. */
8205 if (is_attribute_list
== id_attr
8206 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_NAME
)
8210 /* Consume the identifier. */
8211 token
= cp_lexer_consume_token (parser
->lexer
);
8212 /* Save the identifier. */
8213 identifier
= token
->u
.value
;
8217 bool expr_non_constant_p
;
8219 /* Parse the next assignment-expression. */
8220 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
8222 /* A braced-init-list. */
8223 cp_lexer_set_source_position (parser
->lexer
);
8224 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
8225 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
8226 if (non_constant_p
&& expr_non_constant_p
)
8227 *non_constant_p
= true;
8229 else if (non_constant_p
)
8231 expr
= (cp_parser_constant_expression
8232 (parser
, /*allow_non_constant_p=*/true,
8233 &expr_non_constant_p
));
8234 if (expr_non_constant_p
)
8235 *non_constant_p
= true;
8238 expr
= cp_parser_assignment_expression (parser
, /*pidk=*/NULL
,
8242 expr
= instantiate_non_dependent_expr (expr
);
8244 /* If we have an ellipsis, then this is an expression
8246 if (allow_expansion_p
8247 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
8249 /* Consume the `...'. */
8250 cp_lexer_consume_token (parser
->lexer
);
8252 /* Build the argument pack. */
8253 expr
= make_pack_expansion (expr
);
8256 if (wrap_locations_p
)
8257 expr
.maybe_add_location_wrapper ();
8259 /* Add it to the list. We add error_mark_node
8260 expressions to the list, so that we can still tell if
8261 the correct form for a parenthesized expression-list
8262 is found. That gives better errors. */
8263 vec_safe_push (expression_list
, expr
.get_value ());
8265 if (expr
== error_mark_node
)
8269 /* After the first item, attribute lists look the same as
8270 expression lists. */
8271 is_attribute_list
= non_attr
;
8274 /* If the next token isn't a `,', then we are done. */
8275 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
8278 /* Otherwise, consume the `,' and keep going. */
8279 cp_lexer_consume_token (parser
->lexer
);
8282 if (close_paren_loc
)
8283 *close_paren_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8285 if (!parens
.require_close (parser
))
8290 /* We try and resync to an unnested comma, as that will give the
8291 user better diagnostics. */
8292 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
8293 /*recovering=*/true,
8295 /*consume_paren=*/true);
8300 parser
->greater_than_is_operator_p
8301 = saved_greater_than_is_operator_p
;
8306 parser
->greater_than_is_operator_p
8307 = saved_greater_than_is_operator_p
;
8310 vec_safe_insert (expression_list
, 0, identifier
);
8312 return expression_list
;
8315 /* Parse a pseudo-destructor-name.
8317 pseudo-destructor-name:
8318 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
8319 :: [opt] nested-name-specifier template template-id :: ~ type-name
8320 :: [opt] nested-name-specifier [opt] ~ type-name
8322 If either of the first two productions is used, sets *SCOPE to the
8323 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
8324 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
8325 or ERROR_MARK_NODE if the parse fails. */
8328 cp_parser_pseudo_destructor_name (cp_parser
* parser
,
8333 bool nested_name_specifier_p
;
8336 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMPL
)
8337 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_AUTO
)
8338 && !type_dependent_expression_p (object
))
8340 if (cxx_dialect
< cxx14
)
8341 pedwarn (input_location
, 0,
8342 "%<~auto%> only available with "
8343 "%<-std=c++14%> or %<-std=gnu++14%>");
8344 cp_lexer_consume_token (parser
->lexer
);
8345 cp_lexer_consume_token (parser
->lexer
);
8347 *type
= TREE_TYPE (object
);
8351 /* Assume that things will not work out. */
8352 *type
= error_mark_node
;
8354 /* Look for the optional `::' operator. */
8355 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/true);
8356 /* Look for the optional nested-name-specifier. */
8357 nested_name_specifier_p
8358 = (cp_parser_nested_name_specifier_opt (parser
,
8359 /*typename_keyword_p=*/false,
8360 /*check_dependency_p=*/true,
8362 /*is_declaration=*/false)
8364 /* Now, if we saw a nested-name-specifier, we might be doing the
8365 second production. */
8366 if (nested_name_specifier_p
8367 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
8369 /* Consume the `template' keyword. */
8370 cp_lexer_consume_token (parser
->lexer
);
8371 /* Parse the template-id. */
8372 cp_parser_template_id (parser
,
8373 /*template_keyword_p=*/true,
8374 /*check_dependency_p=*/false,
8376 /*is_declaration=*/true);
8377 /* Look for the `::' token. */
8378 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8380 /* If the next token is not a `~', then there might be some
8381 additional qualification. */
8382 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMPL
))
8384 /* At this point, we're looking for "type-name :: ~". The type-name
8385 must not be a class-name, since this is a pseudo-destructor. So,
8386 it must be either an enum-name, or a typedef-name -- both of which
8387 are just identifiers. So, we peek ahead to check that the "::"
8388 and "~" tokens are present; if they are not, then we can avoid
8389 calling type_name. */
8390 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
8391 || cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
8392 || cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_COMPL
)
8394 cp_parser_error (parser
, "non-scalar type");
8398 /* Look for the type-name. */
8399 *scope
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8400 if (*scope
== error_mark_node
)
8403 /* Look for the `::' token. */
8404 cp_parser_require (parser
, CPP_SCOPE
, RT_SCOPE
);
8409 /* Look for the `~'. */
8410 cp_parser_require (parser
, CPP_COMPL
, RT_COMPL
);
8412 /* Once we see the ~, this has to be a pseudo-destructor. */
8413 if (!processing_template_decl
&& !cp_parser_error_occurred (parser
))
8414 cp_parser_commit_to_topmost_tentative_parse (parser
);
8416 /* Look for the type-name again. We are not responsible for
8417 checking that it matches the first type-name. */
8418 *type
= TREE_TYPE (cp_parser_nonclass_name (parser
));
8421 /* Parse a unary-expression.
8428 unary-operator cast-expression
8429 sizeof unary-expression
8431 alignof ( type-id ) [C++0x]
8438 __extension__ cast-expression
8439 __alignof__ unary-expression
8440 __alignof__ ( type-id )
8441 alignof unary-expression [C++0x]
8442 __real__ cast-expression
8443 __imag__ cast-expression
8445 sizeof ( type-id ) { initializer-list , [opt] }
8446 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
8447 __alignof__ ( type-id ) { initializer-list , [opt] }
8449 ADDRESS_P is true iff the unary-expression is appearing as the
8450 operand of the `&' operator. CAST_P is true if this expression is
8451 the target of a cast.
8453 Returns a representation of the expression. */
8456 cp_parser_unary_expression (cp_parser
*parser
, cp_id_kind
* pidk
,
8457 bool address_p
, bool cast_p
, bool decltype_p
)
8460 enum tree_code unary_operator
;
8462 /* Peek at the next token. */
8463 token
= cp_lexer_peek_token (parser
->lexer
);
8464 /* Some keywords give away the kind of expression. */
8465 if (token
->type
== CPP_KEYWORD
)
8467 enum rid keyword
= token
->keyword
;
8476 location_t start_loc
= token
->location
;
8478 op
= keyword
== RID_ALIGNOF
? ALIGNOF_EXPR
: SIZEOF_EXPR
;
8479 bool std_alignof
= id_equal (token
->u
.value
, "alignof");
8481 /* Consume the token. */
8482 cp_lexer_consume_token (parser
->lexer
);
8483 /* Parse the operand. */
8484 operand
= cp_parser_sizeof_operand (parser
, keyword
);
8486 /* Construct a location e.g. :
8489 with start == caret at the start of the "alignof"/"sizeof"
8490 token, with the endpoint at the final closing paren. */
8491 location_t compound_loc
8492 = make_location (start_loc
, start_loc
, parser
->lexer
);
8494 if (TYPE_P (operand
))
8495 ret
= cxx_sizeof_or_alignof_type (compound_loc
, operand
, op
,
8499 /* ISO C++ defines alignof only with types, not with
8500 expressions. So pedwarn if alignof is used with a non-
8501 type expression. However, __alignof__ is ok. */
8503 pedwarn (token
->location
, OPT_Wpedantic
,
8504 "ISO C++ does not allow %<alignof%> "
8507 ret
= cxx_sizeof_or_alignof_expr (compound_loc
, operand
, op
,
8510 /* For SIZEOF_EXPR, just issue diagnostics, but keep
8511 SIZEOF_EXPR with the original operand. */
8512 if (op
== SIZEOF_EXPR
&& ret
!= error_mark_node
)
8514 if (TREE_CODE (ret
) != SIZEOF_EXPR
|| TYPE_P (operand
))
8516 if (!processing_template_decl
&& TYPE_P (operand
))
8518 ret
= build_min (SIZEOF_EXPR
, size_type_node
,
8519 build1 (NOP_EXPR
, operand
,
8521 SIZEOF_EXPR_TYPE_P (ret
) = 1;
8524 ret
= build_min (SIZEOF_EXPR
, size_type_node
, operand
);
8525 TREE_SIDE_EFFECTS (ret
) = 0;
8526 TREE_READONLY (ret
) = 1;
8527 SET_EXPR_LOCATION (ret
, compound_loc
);
8531 cp_expr
ret_expr (ret
, compound_loc
);
8532 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8536 case RID_BUILTIN_HAS_ATTRIBUTE
:
8537 return cp_parser_has_attribute_expression (parser
);
8540 return cp_parser_new_expression (parser
);
8543 return cp_parser_delete_expression (parser
);
8547 /* The saved value of the PEDANTIC flag. */
8551 /* Save away the PEDANTIC flag. */
8552 cp_parser_extension_opt (parser
, &saved_pedantic
);
8553 /* Parse the cast-expression. */
8554 expr
= cp_parser_simple_cast_expression (parser
);
8555 /* Restore the PEDANTIC flag. */
8556 pedantic
= saved_pedantic
;
8566 /* Consume the `__real__' or `__imag__' token. */
8567 cp_lexer_consume_token (parser
->lexer
);
8568 /* Parse the cast-expression. */
8569 expression
= cp_parser_simple_cast_expression (parser
);
8570 /* Create the complete representation. */
8571 return build_x_unary_op (token
->location
,
8572 (keyword
== RID_REALPART
8573 ? REALPART_EXPR
: IMAGPART_EXPR
),
8575 tf_warning_or_error
);
8579 case RID_TRANSACTION_ATOMIC
:
8580 case RID_TRANSACTION_RELAXED
:
8581 return cp_parser_transaction_expression (parser
, keyword
);
8586 const char *saved_message
;
8587 bool saved_integral_constant_expression_p
;
8588 bool saved_non_integral_constant_expression_p
;
8589 bool saved_greater_than_is_operator_p
;
8591 location_t start_loc
= token
->location
;
8593 cp_lexer_consume_token (parser
->lexer
);
8594 matching_parens parens
;
8595 parens
.require_open (parser
);
8597 saved_message
= parser
->type_definition_forbidden_message
;
8598 parser
->type_definition_forbidden_message
8599 = G_("types may not be defined in %<noexcept%> expressions");
8601 saved_integral_constant_expression_p
8602 = parser
->integral_constant_expression_p
;
8603 saved_non_integral_constant_expression_p
8604 = parser
->non_integral_constant_expression_p
;
8605 parser
->integral_constant_expression_p
= false;
8607 saved_greater_than_is_operator_p
8608 = parser
->greater_than_is_operator_p
;
8609 parser
->greater_than_is_operator_p
= true;
8611 ++cp_unevaluated_operand
;
8612 ++c_inhibit_evaluation_warnings
;
8613 ++cp_noexcept_operand
;
8614 expr
= cp_parser_expression (parser
);
8615 --cp_noexcept_operand
;
8616 --c_inhibit_evaluation_warnings
;
8617 --cp_unevaluated_operand
;
8619 parser
->greater_than_is_operator_p
8620 = saved_greater_than_is_operator_p
;
8622 parser
->integral_constant_expression_p
8623 = saved_integral_constant_expression_p
;
8624 parser
->non_integral_constant_expression_p
8625 = saved_non_integral_constant_expression_p
;
8627 parser
->type_definition_forbidden_message
= saved_message
;
8629 parens
.require_close (parser
);
8631 /* Construct a location of the form:
8634 with start == caret, finishing at the close-paren. */
8635 location_t noexcept_loc
8636 = make_location (start_loc
, start_loc
, parser
->lexer
);
8638 return cp_expr (finish_noexcept_expr (expr
, tf_warning_or_error
),
8645 location_t kw_loc
= token
->location
;
8647 /* Consume the `co_await' token. */
8648 cp_lexer_consume_token (parser
->lexer
);
8649 /* Parse its cast-expression. */
8650 expr
= cp_parser_simple_cast_expression (parser
);
8651 if (expr
== error_mark_node
)
8652 return error_mark_node
;
8654 /* Handle [expr.await]. */
8655 return cp_expr (finish_co_await_expr (kw_loc
, expr
));
8663 /* Look for the `:: new' and `:: delete', which also signal the
8664 beginning of a new-expression, or delete-expression,
8665 respectively. If the next token is `::', then it might be one of
8667 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
8671 /* See if the token after the `::' is one of the keywords in
8672 which we're interested. */
8673 keyword
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->keyword
;
8674 /* If it's `new', we have a new-expression. */
8675 if (keyword
== RID_NEW
)
8676 return cp_parser_new_expression (parser
);
8677 /* Similarly, for `delete'. */
8678 else if (keyword
== RID_DELETE
)
8679 return cp_parser_delete_expression (parser
);
8682 /* Look for a unary operator. */
8683 unary_operator
= cp_parser_unary_operator (token
);
8684 /* The `++' and `--' operators can be handled similarly, even though
8685 they are not technically unary-operators in the grammar. */
8686 if (unary_operator
== ERROR_MARK
)
8688 if (token
->type
== CPP_PLUS_PLUS
)
8689 unary_operator
= PREINCREMENT_EXPR
;
8690 else if (token
->type
== CPP_MINUS_MINUS
)
8691 unary_operator
= PREDECREMENT_EXPR
;
8692 /* Handle the GNU address-of-label extension. */
8693 else if (cp_parser_allow_gnu_extensions_p (parser
)
8694 && token
->type
== CPP_AND_AND
)
8698 location_t start_loc
= token
->location
;
8700 /* Consume the '&&' token. */
8701 cp_lexer_consume_token (parser
->lexer
);
8702 /* Look for the identifier. */
8703 identifier
= cp_parser_identifier (parser
);
8704 /* Construct a location of the form:
8707 with caret==start at the "&&", finish at the end of the label. */
8708 location_t combined_loc
8709 = make_location (start_loc
, start_loc
, parser
->lexer
);
8710 /* Create an expression representing the address. */
8711 expression
= finish_label_address_expr (identifier
, combined_loc
);
8712 if (cp_parser_non_integral_constant_expression (parser
,
8714 expression
= error_mark_node
;
8718 if (unary_operator
!= ERROR_MARK
)
8720 cp_expr cast_expression
;
8721 cp_expr expression
= error_mark_node
;
8722 non_integral_constant non_constant_p
= NIC_NONE
;
8723 location_t loc
= token
->location
;
8724 tsubst_flags_t complain
= complain_flags (decltype_p
);
8726 /* Consume the operator token. */
8727 token
= cp_lexer_consume_token (parser
->lexer
);
8728 enum cpp_ttype op_ttype
= cp_lexer_peek_token (parser
->lexer
)->type
;
8730 /* Parse the cast-expression. */
8732 = cp_parser_cast_expression (parser
,
8733 unary_operator
== ADDR_EXPR
,
8739 OP_TOKEN CAST_EXPRESSION
8740 ^~~~~~~~~~~~~~~~~~~~~~~~~
8741 with start==caret at the operator token, and
8742 extending to the end of the cast_expression. */
8743 loc
= make_location (loc
, loc
, cast_expression
.get_finish ());
8745 /* Now, build an appropriate representation. */
8746 switch (unary_operator
)
8749 non_constant_p
= NIC_STAR
;
8750 expression
= build_x_indirect_ref (loc
, cast_expression
,
8753 /* TODO: build_x_indirect_ref does not always honor the
8754 location, so ensure it is set. */
8755 expression
.set_location (loc
);
8759 non_constant_p
= NIC_ADDR
;
8762 expression
= build_x_unary_op (loc
, unary_operator
,
8765 /* TODO: build_x_unary_op does not always honor the location,
8766 so ensure it is set. */
8767 expression
.set_location (loc
);
8770 case PREINCREMENT_EXPR
:
8771 case PREDECREMENT_EXPR
:
8772 non_constant_p
= unary_operator
== PREINCREMENT_EXPR
8773 ? NIC_PREINCREMENT
: NIC_PREDECREMENT
;
8776 /* Immediately fold negation of a constant, unless the constant is 0
8777 (since -0 == 0) or it would overflow. */
8778 if (unary_operator
== NEGATE_EXPR
&& op_ttype
== CPP_NUMBER
)
8781 = tree_strip_any_location_wrapper (cast_expression
);
8782 if (CONSTANT_CLASS_P (stripped_expr
)
8783 && !integer_zerop (stripped_expr
)
8784 && !TREE_OVERFLOW (stripped_expr
))
8786 tree folded
= fold_build1 (unary_operator
,
8787 TREE_TYPE (stripped_expr
),
8789 if (CONSTANT_CLASS_P (folded
) && !TREE_OVERFLOW (folded
))
8791 expression
= maybe_wrap_with_location (folded
, loc
);
8797 case UNARY_PLUS_EXPR
:
8798 case TRUTH_NOT_EXPR
:
8799 expression
= finish_unary_op_expr (loc
, unary_operator
,
8800 cast_expression
, complain
);
8807 if (non_constant_p
!= NIC_NONE
8808 && cp_parser_non_integral_constant_expression (parser
,
8810 expression
= error_mark_node
;
8815 return cp_parser_postfix_expression (parser
, address_p
, cast_p
,
8816 /*member_access_only_p=*/false,
8821 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8822 unary-operator, the corresponding tree code is returned. */
8824 static enum tree_code
8825 cp_parser_unary_operator (cp_token
* token
)
8827 switch (token
->type
)
8830 return INDIRECT_REF
;
8836 return UNARY_PLUS_EXPR
;
8842 return TRUTH_NOT_EXPR
;
8845 return BIT_NOT_EXPR
;
8852 /* Parse a __builtin_has_attribute([expr|type], attribute-spec) expression.
8853 Returns a representation of the expression. */
8856 cp_parser_has_attribute_expression (cp_parser
*parser
)
8858 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8860 /* Consume the __builtin_has_attribute token. */
8861 cp_lexer_consume_token (parser
->lexer
);
8863 matching_parens parens
;
8864 if (!parens
.require_open (parser
))
8865 return error_mark_node
;
8867 /* Types cannot be defined in a `sizeof' expression. Save away the
8869 const char *saved_message
= parser
->type_definition_forbidden_message
;
8870 const char *saved_message_arg
8871 = parser
->type_definition_forbidden_message_arg
;
8872 parser
->type_definition_forbidden_message
8873 = G_("types may not be defined in %qs expressions");
8874 parser
->type_definition_forbidden_message_arg
8875 = IDENTIFIER_POINTER (ridpointers
[RID_BUILTIN_HAS_ATTRIBUTE
]);
8877 /* The restrictions on constant-expressions do not apply inside
8878 sizeof expressions. */
8879 bool saved_integral_constant_expression_p
8880 = parser
->integral_constant_expression_p
;
8881 bool saved_non_integral_constant_expression_p
8882 = parser
->non_integral_constant_expression_p
;
8883 parser
->integral_constant_expression_p
= false;
8885 /* Do not actually evaluate the expression. */
8886 ++cp_unevaluated_operand
;
8887 ++c_inhibit_evaluation_warnings
;
8889 tree oper
= NULL_TREE
;
8891 /* We can't be sure yet whether we're looking at a type-id or an
8893 cp_parser_parse_tentatively (parser
);
8895 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
8896 parser
->in_type_id_in_expr_p
= true;
8897 /* Look for the type-id. */
8898 oper
= cp_parser_type_id (parser
);
8899 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
8901 cp_parser_parse_definitely (parser
);
8903 /* If the type-id production did not work out, then we must be
8904 looking at an expression. */
8905 if (!oper
|| oper
== error_mark_node
)
8906 oper
= cp_parser_assignment_expression (parser
);
8908 STRIP_ANY_LOCATION_WRAPPER (oper
);
8910 /* Go back to evaluating expressions. */
8911 --cp_unevaluated_operand
;
8912 --c_inhibit_evaluation_warnings
;
8914 /* And restore the old one. */
8915 parser
->type_definition_forbidden_message
= saved_message
;
8916 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
8917 parser
->integral_constant_expression_p
8918 = saved_integral_constant_expression_p
;
8919 parser
->non_integral_constant_expression_p
8920 = saved_non_integral_constant_expression_p
;
8922 /* Consume the comma if it's there. */
8923 if (!cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
8925 cp_parser_skip_to_closing_parenthesis (parser
, false, false,
8926 /*consume_paren=*/true);
8927 return error_mark_node
;
8930 /* Parse the attribute specification. */
8932 location_t atloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8933 if (tree attr
= cp_parser_gnu_attribute_list (parser
, /*exactly_one=*/true))
8935 if (oper
== error_mark_node
)
8937 else if (type_dependent_expression_p (oper
))
8938 sorry_at (atloc
, "%<__builtin_has_attribute%> with dependent argument "
8939 "not supported yet");
8942 /* Fold constant expressions used in attributes first. */
8943 cp_check_const_attributes (attr
);
8945 /* Finally, see if OPER has been declared with ATTR. */
8946 ret
= has_attribute (atloc
, oper
, attr
, default_conversion
);
8949 parens
.require_close (parser
);
8953 error_at (atloc
, "expected identifier");
8954 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
8957 /* Construct a location e.g. :
8958 __builtin_has_attribute (oper, attr)
8959 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8960 with start == caret at the start of the built-in token,
8961 and with the endpoint at the final closing paren. */
8962 location_t compound_loc
8963 = make_location (start_loc
, start_loc
, parser
->lexer
);
8965 cp_expr
ret_expr (ret
? boolean_true_node
: boolean_false_node
);
8966 ret_expr
.set_location (compound_loc
);
8967 ret_expr
= ret_expr
.maybe_add_location_wrapper ();
8971 /* Parse a new-expression.
8974 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8975 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8977 Returns a representation of the expression. */
8980 cp_parser_new_expression (cp_parser
* parser
)
8982 bool global_scope_p
;
8983 vec
<tree
, va_gc
> *placement
;
8985 vec
<tree
, va_gc
> *initializer
;
8986 tree nelts
= NULL_TREE
;
8989 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
8991 /* Look for the optional `::' operator. */
8993 = (cp_parser_global_scope_opt (parser
,
8994 /*current_scope_valid_p=*/false)
8996 /* Look for the `new' operator. */
8997 cp_parser_require_keyword (parser
, RID_NEW
, RT_NEW
);
8998 /* There's no easy way to tell a new-placement from the
8999 `( type-id )' construct. */
9000 cp_parser_parse_tentatively (parser
);
9001 /* Look for a new-placement. */
9002 placement
= cp_parser_new_placement (parser
);
9003 /* If that didn't work out, there's no new-placement. */
9004 if (!cp_parser_parse_definitely (parser
))
9006 if (placement
!= NULL
)
9007 release_tree_vector (placement
);
9011 /* If the next token is a `(', then we have a parenthesized
9013 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9016 const char *saved_message
= parser
->type_definition_forbidden_message
;
9018 /* Consume the `('. */
9019 matching_parens parens
;
9020 parens
.consume_open (parser
);
9022 /* Parse the type-id. */
9023 parser
->type_definition_forbidden_message
9024 = G_("types may not be defined in a new-expression");
9026 type_id_in_expr_sentinel
s (parser
);
9027 type
= cp_parser_type_id (parser
);
9029 parser
->type_definition_forbidden_message
= saved_message
;
9031 /* Look for the closing `)'. */
9032 parens
.require_close (parser
);
9033 token
= cp_lexer_peek_token (parser
->lexer
);
9034 /* There should not be a direct-new-declarator in this production,
9035 but GCC used to allowed this, so we check and emit a sensible error
9036 message for this case. */
9037 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9039 error_at (token
->location
,
9040 "array bound forbidden after parenthesized type-id");
9041 inform (token
->location
,
9042 "try removing the parentheses around the type-id");
9043 cp_parser_direct_new_declarator (parser
);
9046 /* Otherwise, there must be a new-type-id. */
9048 type
= cp_parser_new_type_id (parser
, &nelts
);
9050 /* If the next token is a `(' or '{', then we have a new-initializer. */
9051 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9052 if (token
->type
== CPP_OPEN_PAREN
9053 || token
->type
== CPP_OPEN_BRACE
)
9054 initializer
= cp_parser_new_initializer (parser
);
9058 /* A new-expression may not appear in an integral constant
9060 if (cp_parser_non_integral_constant_expression (parser
, NIC_NEW
))
9061 ret
= error_mark_node
;
9062 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
9063 of a new-type-id or type-id of a new-expression, the new-expression shall
9064 contain a new-initializer of the form ( assignment-expression )".
9065 Additionally, consistently with the spirit of DR 1467, we want to accept
9066 'new auto { 2 }' too. */
9067 else if ((ret
= type_uses_auto (type
))
9068 && !CLASS_PLACEHOLDER_TEMPLATE (ret
)
9069 && (vec_safe_length (initializer
) != 1
9070 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer
)[0])
9071 && CONSTRUCTOR_NELTS ((*initializer
)[0]) != 1)))
9073 error_at (token
->location
,
9074 "initialization of new-expression for type %<auto%> "
9075 "requires exactly one element");
9076 ret
= error_mark_node
;
9080 /* Construct a location e.g.:
9083 with caret == start at the start of the "new" token, and the end
9084 at the end of the final token we consumed. */
9085 location_t combined_loc
= make_location (start_loc
, start_loc
,
9087 /* Create a representation of the new-expression. */
9088 ret
= build_new (combined_loc
, &placement
, type
, nelts
, &initializer
,
9089 global_scope_p
, tf_warning_or_error
);
9092 if (placement
!= NULL
)
9093 release_tree_vector (placement
);
9094 if (initializer
!= NULL
)
9095 release_tree_vector (initializer
);
9100 /* Parse a new-placement.
9105 Returns the same representation as for an expression-list. */
9107 static vec
<tree
, va_gc
> *
9108 cp_parser_new_placement (cp_parser
* parser
)
9110 vec
<tree
, va_gc
> *expression_list
;
9112 /* Parse the expression-list. */
9113 expression_list
= (cp_parser_parenthesized_expression_list
9114 (parser
, non_attr
, /*cast_p=*/false,
9115 /*allow_expansion_p=*/true,
9116 /*non_constant_p=*/NULL
));
9118 if (expression_list
&& expression_list
->is_empty ())
9119 error ("expected expression-list or type-id");
9121 return expression_list
;
9124 /* Parse a new-type-id.
9127 type-specifier-seq new-declarator [opt]
9129 Returns the TYPE allocated. If the new-type-id indicates an array
9130 type, *NELTS is set to the number of elements in the last array
9131 bound; the TYPE will not include the last array bound. */
9134 cp_parser_new_type_id (cp_parser
* parser
, tree
*nelts
)
9136 cp_decl_specifier_seq type_specifier_seq
;
9137 cp_declarator
*new_declarator
;
9138 cp_declarator
*declarator
;
9139 cp_declarator
*outer_declarator
;
9140 const char *saved_message
;
9142 /* The type-specifier sequence must not contain type definitions.
9143 (It cannot contain declarations of new types either, but if they
9144 are not definitions we will catch that because they are not
9146 saved_message
= parser
->type_definition_forbidden_message
;
9147 parser
->type_definition_forbidden_message
9148 = G_("types may not be defined in a new-type-id");
9149 /* Parse the type-specifier-seq. */
9150 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
9151 /*is_declaration=*/false,
9152 /*is_trailing_return=*/false,
9153 &type_specifier_seq
);
9154 /* Restore the old message. */
9155 parser
->type_definition_forbidden_message
= saved_message
;
9157 if (type_specifier_seq
.type
== error_mark_node
)
9158 return error_mark_node
;
9160 /* Parse the new-declarator. */
9161 new_declarator
= cp_parser_new_declarator_opt (parser
);
9163 /* Determine the number of elements in the last array dimension, if
9166 /* Skip down to the last array dimension. */
9167 declarator
= new_declarator
;
9168 outer_declarator
= NULL
;
9169 while (declarator
&& (declarator
->kind
== cdk_pointer
9170 || declarator
->kind
== cdk_ptrmem
))
9172 outer_declarator
= declarator
;
9173 declarator
= declarator
->declarator
;
9176 && declarator
->kind
== cdk_array
9177 && declarator
->declarator
9178 && declarator
->declarator
->kind
== cdk_array
)
9180 outer_declarator
= declarator
;
9181 declarator
= declarator
->declarator
;
9184 if (declarator
&& declarator
->kind
== cdk_array
)
9186 *nelts
= declarator
->u
.array
.bounds
;
9187 if (*nelts
== error_mark_node
)
9188 *nelts
= integer_one_node
;
9190 if (*nelts
== NULL_TREE
)
9191 /* Leave [] in the declarator. */;
9192 else if (outer_declarator
)
9193 outer_declarator
->declarator
= declarator
->declarator
;
9195 new_declarator
= NULL
;
9198 return groktypename (&type_specifier_seq
, new_declarator
, false);
9201 /* Parse an (optional) new-declarator.
9204 ptr-operator new-declarator [opt]
9205 direct-new-declarator
9207 Returns the declarator. */
9209 static cp_declarator
*
9210 cp_parser_new_declarator_opt (cp_parser
* parser
)
9212 enum tree_code code
;
9213 tree type
, std_attributes
= NULL_TREE
;
9214 cp_cv_quals cv_quals
;
9216 /* We don't know if there's a ptr-operator next, or not. */
9217 cp_parser_parse_tentatively (parser
);
9218 /* Look for a ptr-operator. */
9219 code
= cp_parser_ptr_operator (parser
, &type
, &cv_quals
, &std_attributes
);
9220 /* If that worked, look for more new-declarators. */
9221 if (cp_parser_parse_definitely (parser
))
9223 cp_declarator
*declarator
;
9225 /* Parse another optional declarator. */
9226 declarator
= cp_parser_new_declarator_opt (parser
);
9228 declarator
= cp_parser_make_indirect_declarator
9229 (code
, type
, cv_quals
, declarator
, std_attributes
);
9234 /* If the next token is a `[', there is a direct-new-declarator. */
9235 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9236 return cp_parser_direct_new_declarator (parser
);
9241 /* Parse a direct-new-declarator.
9243 direct-new-declarator:
9245 direct-new-declarator [constant-expression]
9249 static cp_declarator
*
9250 cp_parser_direct_new_declarator (cp_parser
* parser
)
9252 cp_declarator
*declarator
= NULL
;
9253 bool first_p
= true;
9260 /* Look for the opening `['. */
9261 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
9263 token
= cp_lexer_peek_token (parser
->lexer
);
9264 if (token
->type
== CPP_CLOSE_SQUARE
&& first_p
)
9265 expression
= NULL_TREE
;
9267 expression
= cp_parser_expression (parser
);
9268 /* The standard requires that the expression have integral
9269 type. DR 74 adds enumeration types. We believe that the
9270 real intent is that these expressions be handled like the
9271 expression in a `switch' condition, which also allows
9272 classes with a single conversion to integral or
9273 enumeration type. */
9274 if (expression
&& !processing_template_decl
)
9277 = build_expr_type_conversion (WANT_INT
| WANT_ENUM
,
9282 error_at (token
->location
,
9283 "expression in new-declarator must have integral "
9284 "or enumeration type");
9285 expression
= error_mark_node
;
9289 /* Look for the closing `]'. */
9290 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9292 /* Add this bound to the declarator. */
9293 declarator
= make_array_declarator (declarator
, expression
);
9295 /* If the next token is not a `[', then there are no more
9297 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
9305 /* Parse a new-initializer.
9308 ( expression-list [opt] )
9311 Returns a representation of the expression-list. */
9313 static vec
<tree
, va_gc
> *
9314 cp_parser_new_initializer (cp_parser
* parser
)
9316 vec
<tree
, va_gc
> *expression_list
;
9318 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
9321 bool expr_non_constant_p
;
9322 cp_lexer_set_source_position (parser
->lexer
);
9323 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
9324 t
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
9325 CONSTRUCTOR_IS_DIRECT_INIT (t
) = 1;
9326 expression_list
= make_tree_vector_single (t
);
9329 expression_list
= (cp_parser_parenthesized_expression_list
9330 (parser
, non_attr
, /*cast_p=*/false,
9331 /*allow_expansion_p=*/true,
9332 /*non_constant_p=*/NULL
));
9334 return expression_list
;
9337 /* Parse a delete-expression.
9340 :: [opt] delete cast-expression
9341 :: [opt] delete [ ] cast-expression
9343 Returns a representation of the expression. */
9346 cp_parser_delete_expression (cp_parser
* parser
)
9348 bool global_scope_p
;
9351 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
9353 /* Look for the optional `::' operator. */
9355 = (cp_parser_global_scope_opt (parser
,
9356 /*current_scope_valid_p=*/false)
9358 /* Look for the `delete' keyword. */
9359 cp_parser_require_keyword (parser
, RID_DELETE
, RT_DELETE
);
9360 /* See if the array syntax is in use. */
9361 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
9363 /* Consume the `[' token. */
9364 cp_lexer_consume_token (parser
->lexer
);
9365 /* Look for the `]' token. */
9366 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
9367 /* Remember that this is the `[]' construct. */
9373 /* Parse the cast-expression. */
9374 expression
= cp_parser_simple_cast_expression (parser
);
9376 /* A delete-expression may not appear in an integral constant
9378 if (cp_parser_non_integral_constant_expression (parser
, NIC_DEL
))
9379 return error_mark_node
;
9381 /* Construct a location e.g.:
9384 with caret == start at the start of the "delete" token, and
9385 the end at the end of the final token we consumed. */
9386 location_t combined_loc
= make_location (start_loc
, start_loc
,
9388 expression
= delete_sanity (combined_loc
, expression
, NULL_TREE
, array_p
,
9389 global_scope_p
, tf_warning_or_error
);
9394 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
9395 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
9399 cp_parser_tokens_start_cast_expression (cp_parser
*parser
)
9401 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
9402 switch (token
->type
)
9408 case CPP_CLOSE_SQUARE
:
9409 case CPP_CLOSE_PAREN
:
9410 case CPP_CLOSE_BRACE
:
9411 case CPP_OPEN_BRACE
:
9415 case CPP_DEREF_STAR
:
9423 case CPP_GREATER_EQ
:
9444 case CPP_OPEN_PAREN
:
9445 /* In ((type ()) () the last () isn't a valid cast-expression,
9446 so the whole must be parsed as postfix-expression. */
9447 return cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
9450 case CPP_OPEN_SQUARE
:
9451 /* '[' may start a primary-expression in obj-c++ and in C++11,
9452 as a lambda-expression, eg, '(void)[]{}'. */
9453 if (cxx_dialect
>= cxx11
)
9455 return c_dialect_objc ();
9458 case CPP_MINUS_MINUS
:
9459 /* '++' and '--' may or may not start a cast-expression:
9461 struct T { void operator++(int); };
9462 void f() { (T())++; }
9475 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
9476 in the order: const_cast, static_cast, reinterpret_cast.
9478 Don't suggest dynamic_cast.
9480 Return the first legal cast kind found, or NULL otherwise. */
9483 get_cast_suggestion (tree dst_type
, tree orig_expr
)
9487 /* Reuse the parser logic by attempting to build the various kinds of
9488 cast, with "complain" disabled.
9489 Identify the first such cast that is valid. */
9491 /* Don't attempt to run such logic within template processing. */
9492 if (processing_template_decl
)
9495 /* First try const_cast. */
9496 trial
= build_const_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9497 if (trial
!= error_mark_node
)
9498 return "const_cast";
9500 /* If that fails, try static_cast. */
9501 trial
= build_static_cast (input_location
, dst_type
, orig_expr
, tf_none
);
9502 if (trial
!= error_mark_node
)
9503 return "static_cast";
9505 /* Finally, try reinterpret_cast. */
9506 trial
= build_reinterpret_cast (input_location
, dst_type
, orig_expr
,
9508 if (trial
!= error_mark_node
)
9509 return "reinterpret_cast";
9511 /* No such cast possible. */
9515 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
9516 suggesting how to convert a C-style cast of the form:
9520 to a C++-style cast.
9522 The primary range of RICHLOC is asssumed to be that of the original
9523 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
9524 of the parens in the C-style cast. */
9527 maybe_add_cast_fixit (rich_location
*rich_loc
, location_t open_paren_loc
,
9528 location_t close_paren_loc
, tree orig_expr
,
9531 /* This function is non-trivial, so bail out now if the warning isn't
9532 going to be emitted. */
9533 if (!warn_old_style_cast
)
9536 /* Try to find a legal C++ cast, trying them in order:
9537 const_cast, static_cast, reinterpret_cast. */
9538 const char *cast_suggestion
= get_cast_suggestion (dst_type
, orig_expr
);
9539 if (!cast_suggestion
)
9542 /* Replace the open paren with "CAST_SUGGESTION<". */
9544 pp_string (&pp
, cast_suggestion
);
9546 rich_loc
->add_fixit_replace (open_paren_loc
, pp_formatted_text (&pp
));
9548 /* Replace the close paren with "> (". */
9549 rich_loc
->add_fixit_replace (close_paren_loc
, "> (");
9551 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
9552 rich_loc
->add_fixit_insert_after (")");
9556 /* Parse a cast-expression.
9560 ( type-id ) cast-expression
9562 ADDRESS_P is true iff the unary-expression is appearing as the
9563 operand of the `&' operator. CAST_P is true if this expression is
9564 the target of a cast.
9566 Returns a representation of the expression. */
9569 cp_parser_cast_expression (cp_parser
*parser
, bool address_p
, bool cast_p
,
9570 bool decltype_p
, cp_id_kind
* pidk
)
9572 /* If it's a `(', then we might be looking at a cast. */
9573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
9575 tree type
= NULL_TREE
;
9576 cp_expr
expr (NULL_TREE
);
9577 int cast_expression
= 0;
9578 const char *saved_message
;
9580 /* There's no way to know yet whether or not this is a cast.
9581 For example, `(int (3))' is a unary-expression, while `(int)
9582 3' is a cast. So, we resort to parsing tentatively. */
9583 cp_parser_parse_tentatively (parser
);
9584 /* Types may not be defined in a cast. */
9585 saved_message
= parser
->type_definition_forbidden_message
;
9586 parser
->type_definition_forbidden_message
9587 = G_("types may not be defined in casts");
9588 /* Consume the `('. */
9589 matching_parens parens
;
9590 cp_token
*open_paren
= parens
.consume_open (parser
);
9591 location_t open_paren_loc
= open_paren
->location
;
9592 location_t close_paren_loc
= UNKNOWN_LOCATION
;
9594 /* A very tricky bit is that `(struct S) { 3 }' is a
9595 compound-literal (which we permit in C++ as an extension).
9596 But, that construct is not a cast-expression -- it is a
9597 postfix-expression. (The reason is that `(struct S) { 3 }.i'
9598 is legal; if the compound-literal were a cast-expression,
9599 you'd need an extra set of parentheses.) But, if we parse
9600 the type-id, and it happens to be a class-specifier, then we
9601 will commit to the parse at that point, because we cannot
9602 undo the action that is done when creating a new class. So,
9603 then we cannot back up and do a postfix-expression.
9605 Another tricky case is the following (c++/29234):
9607 struct S { void operator () (); };
9614 As a type-id we parse the parenthesized S()() as a function
9615 returning a function, groktypename complains and we cannot
9616 back up in this case either.
9618 Therefore, we scan ahead to the closing `)', and check to see
9619 if the tokens after the `)' can start a cast-expression. Otherwise
9620 we are dealing with an unary-expression, a postfix-expression
9623 Yet another tricky case, in C++11, is the following (c++/54891):
9627 The issue is that usually, besides the case of lambda-expressions,
9628 the parenthesized type-id cannot be followed by '[', and, eg, we
9629 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
9630 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
9631 we don't commit, we try a cast-expression, then an unary-expression.
9633 Save tokens so that we can put them back. */
9634 cp_lexer_save_tokens (parser
->lexer
);
9636 /* We may be looking at a cast-expression. */
9637 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
9638 /*consume_paren=*/true))
9640 = cp_parser_tokens_start_cast_expression (parser
);
9642 /* Roll back the tokens we skipped. */
9643 cp_lexer_rollback_tokens (parser
->lexer
);
9644 /* If we aren't looking at a cast-expression, simulate an error so
9645 that the call to cp_parser_error_occurred below returns true. */
9646 if (!cast_expression
)
9647 cp_parser_simulate_error (parser
);
9650 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
9651 parser
->in_type_id_in_expr_p
= true;
9652 /* Look for the type-id. */
9653 type
= cp_parser_type_id (parser
);
9654 /* Look for the closing `)'. */
9655 cp_token
*close_paren
= parens
.require_close (parser
);
9657 close_paren_loc
= close_paren
->location
;
9658 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
9661 /* Restore the saved message. */
9662 parser
->type_definition_forbidden_message
= saved_message
;
9664 /* At this point this can only be either a cast or a
9665 parenthesized ctor such as `(T ())' that looks like a cast to
9666 function returning T. */
9667 if (!cp_parser_error_occurred (parser
))
9669 /* Only commit if the cast-expression doesn't start with
9670 '++', '--', or '[' in C++11. */
9671 if (cast_expression
> 0)
9672 cp_parser_commit_to_topmost_tentative_parse (parser
);
9674 expr
= cp_parser_cast_expression (parser
,
9675 /*address_p=*/false,
9677 /*decltype_p=*/false,
9680 if (cp_parser_parse_definitely (parser
))
9682 /* Warn about old-style casts, if so requested. */
9683 if (warn_old_style_cast
9684 && !in_system_header_at (input_location
)
9685 && !VOID_TYPE_P (type
)
9686 && current_lang_name
!= lang_name_c
)
9688 gcc_rich_location
rich_loc (input_location
);
9689 maybe_add_cast_fixit (&rich_loc
, open_paren_loc
, close_paren_loc
,
9691 warning_at (&rich_loc
, OPT_Wold_style_cast
,
9692 "use of old-style cast to %q#T", type
);
9695 /* Only type conversions to integral or enumeration types
9696 can be used in constant-expressions. */
9697 if (!cast_valid_in_integral_constant_expression_p (type
)
9698 && cp_parser_non_integral_constant_expression (parser
,
9700 return error_mark_node
;
9702 /* Perform the cast. */
9706 with start==caret at the open paren, extending to the
9708 location_t cast_loc
= make_location (open_paren_loc
,
9710 expr
.get_finish ());
9711 expr
= build_c_cast (cast_loc
, type
, expr
);
9716 cp_parser_abort_tentative_parse (parser
);
9719 /* If we get here, then it's not a cast, so it must be a
9720 unary-expression. */
9721 return cp_parser_unary_expression (parser
, pidk
, address_p
,
9722 cast_p
, decltype_p
);
9725 /* Parse a binary expression of the general form:
9729 pm-expression .* cast-expression
9730 pm-expression ->* cast-expression
9732 multiplicative-expression:
9734 multiplicative-expression * pm-expression
9735 multiplicative-expression / pm-expression
9736 multiplicative-expression % pm-expression
9738 additive-expression:
9739 multiplicative-expression
9740 additive-expression + multiplicative-expression
9741 additive-expression - multiplicative-expression
9745 shift-expression << additive-expression
9746 shift-expression >> additive-expression
9748 relational-expression:
9750 relational-expression < shift-expression
9751 relational-expression > shift-expression
9752 relational-expression <= shift-expression
9753 relational-expression >= shift-expression
9757 relational-expression:
9758 relational-expression <? shift-expression
9759 relational-expression >? shift-expression
9761 equality-expression:
9762 relational-expression
9763 equality-expression == relational-expression
9764 equality-expression != relational-expression
9768 and-expression & equality-expression
9770 exclusive-or-expression:
9772 exclusive-or-expression ^ and-expression
9774 inclusive-or-expression:
9775 exclusive-or-expression
9776 inclusive-or-expression | exclusive-or-expression
9778 logical-and-expression:
9779 inclusive-or-expression
9780 logical-and-expression && inclusive-or-expression
9782 logical-or-expression:
9783 logical-and-expression
9784 logical-or-expression || logical-and-expression
9786 All these are implemented with a single function like:
9789 simple-cast-expression
9790 binary-expression <token> binary-expression
9792 CAST_P is true if this expression is the target of a cast.
9794 The binops_by_token map is used to get the tree codes for each <token> type.
9795 binary-expressions are associated according to a precedence table. */
9797 #define TOKEN_PRECEDENCE(token) \
9798 (((token->type == CPP_GREATER \
9799 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9800 && !parser->greater_than_is_operator_p) \
9801 ? PREC_NOT_OPERATOR \
9802 : binops_by_token[token->type].prec)
9805 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
9806 bool no_toplevel_fold_p
,
9808 enum cp_parser_prec prec
,
9811 cp_parser_expression_stack stack
;
9812 cp_parser_expression_stack_entry
*sp
= &stack
[0];
9813 cp_parser_expression_stack_entry
*disable_warnings_sp
= NULL
;
9814 cp_parser_expression_stack_entry current
;
9817 enum tree_code rhs_type
;
9818 enum cp_parser_prec new_prec
, lookahead_prec
;
9821 /* Parse the first expression. */
9822 current
.lhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9823 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9824 current
.lhs
= cp_parser_cast_expression (parser
, /*address_p=*/false,
9825 cast_p
, decltype_p
, pidk
);
9826 current
.prec
= prec
;
9828 if (cp_parser_error_occurred (parser
))
9829 return error_mark_node
;
9833 /* Get an operator token. */
9834 token
= cp_lexer_peek_token (parser
->lexer
);
9836 if (warn_cxx11_compat
9837 && token
->type
== CPP_RSHIFT
9838 && !parser
->greater_than_is_operator_p
)
9840 if (warning_at (token
->location
, OPT_Wc__11_compat
,
9841 "%<>>%> operator is treated"
9842 " as two right angle brackets in C++11"))
9843 inform (token
->location
,
9844 "suggest parentheses around %<>>%> expression");
9847 new_prec
= TOKEN_PRECEDENCE (token
);
9848 if (new_prec
!= PREC_NOT_OPERATOR
9849 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9850 /* This is a fold-expression; handle it later. */
9851 new_prec
= PREC_NOT_OPERATOR
;
9853 /* Popping an entry off the stack means we completed a subexpression:
9854 - either we found a token which is not an operator (`>' where it is not
9855 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9856 will happen repeatedly;
9857 - or, we found an operator which has lower priority. This is the case
9858 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9860 if (new_prec
<= current
.prec
)
9869 current
.tree_type
= binops_by_token
[token
->type
].tree_type
;
9870 current
.loc
= token
->location
;
9872 /* We used the operator token. */
9873 cp_lexer_consume_token (parser
->lexer
);
9875 /* For "false && x" or "true || x", x will never be executed;
9876 disable warnings while evaluating it. */
9877 if ((current
.tree_type
== TRUTH_ANDIF_EXPR
9878 && cp_fully_fold (current
.lhs
) == truthvalue_false_node
)
9879 || (current
.tree_type
== TRUTH_ORIF_EXPR
9880 && cp_fully_fold (current
.lhs
) == truthvalue_true_node
))
9882 disable_warnings_sp
= sp
;
9883 ++c_inhibit_evaluation_warnings
;
9886 /* Extract another operand. It may be the RHS of this expression
9887 or the LHS of a new, higher priority expression. */
9888 rhs_type
= (cp_lexer_next_token_is (parser
->lexer
, CPP_NOT
)
9889 ? TRUTH_NOT_EXPR
: ERROR_MARK
);
9890 rhs
= cp_parser_simple_cast_expression (parser
);
9892 /* Get another operator token. Look up its precedence to avoid
9893 building a useless (immediately popped) stack entry for common
9894 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9895 token
= cp_lexer_peek_token (parser
->lexer
);
9896 lookahead_prec
= TOKEN_PRECEDENCE (token
);
9897 if (lookahead_prec
!= PREC_NOT_OPERATOR
9898 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
9899 lookahead_prec
= PREC_NOT_OPERATOR
;
9900 if (lookahead_prec
> new_prec
)
9902 /* ... and prepare to parse the RHS of the new, higher priority
9903 expression. Since precedence levels on the stack are
9904 monotonically increasing, we do not have to care about
9909 current
.lhs_type
= rhs_type
;
9910 current
.prec
= new_prec
;
9911 new_prec
= lookahead_prec
;
9915 lookahead_prec
= new_prec
;
9916 /* If the stack is not empty, we have parsed into LHS the right side
9917 (`4' in the example above) of an expression we had suspended.
9918 We can use the information on the stack to recover the LHS (`3')
9919 from the stack together with the tree code (`MULT_EXPR'), and
9920 the precedence of the higher level subexpression
9921 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9922 which will be used to actually build the additive expression. */
9924 rhs_type
= current
.lhs_type
;
9929 /* Undo the disabling of warnings done above. */
9930 if (sp
== disable_warnings_sp
)
9932 disable_warnings_sp
= NULL
;
9933 --c_inhibit_evaluation_warnings
;
9936 if (warn_logical_not_paren
9937 && TREE_CODE_CLASS (current
.tree_type
) == tcc_comparison
9938 && current
.lhs_type
== TRUTH_NOT_EXPR
9939 /* Avoid warning for !!x == y. */
9940 && (TREE_CODE (current
.lhs
) != NE_EXPR
9941 || !integer_zerop (TREE_OPERAND (current
.lhs
, 1)))
9942 && (TREE_CODE (current
.lhs
) != TRUTH_NOT_EXPR
9943 || (TREE_CODE (TREE_OPERAND (current
.lhs
, 0)) != TRUTH_NOT_EXPR
9944 /* Avoid warning for !b == y where b is boolean. */
9945 && (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)) == NULL_TREE
9946 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current
.lhs
, 0)))
9948 /* Avoid warning for !!b == y where b is boolean. */
9949 && (!DECL_P (tree_strip_any_location_wrapper (current
.lhs
))
9950 || TREE_TYPE (current
.lhs
) == NULL_TREE
9951 || TREE_CODE (TREE_TYPE (current
.lhs
)) != BOOLEAN_TYPE
))
9952 warn_logical_not_parentheses (current
.loc
, current
.tree_type
,
9953 current
.lhs
, maybe_constant_value (rhs
));
9957 location_t combined_loc
= make_location (current
.loc
,
9958 current
.lhs
.get_start (),
9961 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9962 ERROR_MARK for everything that is not a binary expression.
9963 This makes warn_about_parentheses miss some warnings that
9964 involve unary operators. For unary expressions we should
9965 pass the correct tree_code unless the unary expression was
9966 surrounded by parentheses.
9968 if (no_toplevel_fold_p
9969 && lookahead_prec
<= current
.prec
9972 if (current
.lhs
== error_mark_node
|| rhs
== error_mark_node
)
9973 current
.lhs
= error_mark_node
;
9976 current
.lhs
.maybe_add_location_wrapper ();
9977 rhs
.maybe_add_location_wrapper ();
9979 = build_min (current
.tree_type
,
9980 TREE_CODE_CLASS (current
.tree_type
)
9982 ? boolean_type_node
: TREE_TYPE (current
.lhs
),
9983 current
.lhs
.get_value (), rhs
.get_value ());
9984 SET_EXPR_LOCATION (current
.lhs
, combined_loc
);
9989 op_location_t
op_loc (current
.loc
, combined_loc
);
9990 current
.lhs
= build_x_binary_op (op_loc
, current
.tree_type
,
9991 current
.lhs
, current
.lhs_type
,
9992 rhs
, rhs_type
, &overload
,
9993 complain_flags (decltype_p
));
9994 /* TODO: build_x_binary_op doesn't always honor the location. */
9995 current
.lhs
.set_location (combined_loc
);
9997 current
.lhs_type
= current
.tree_type
;
9999 /* If the binary operator required the use of an overloaded operator,
10000 then this expression cannot be an integral constant-expression.
10001 An overloaded operator can be used even if both operands are
10002 otherwise permissible in an integral constant-expression if at
10003 least one of the operands is of enumeration type. */
10006 && cp_parser_non_integral_constant_expression (parser
,
10008 return error_mark_node
;
10011 return current
.lhs
;
10015 cp_parser_binary_expression (cp_parser
* parser
, bool cast_p
,
10016 bool no_toplevel_fold_p
,
10017 enum cp_parser_prec prec
,
10020 return cp_parser_binary_expression (parser
, cast_p
, no_toplevel_fold_p
,
10021 /*decltype*/false, prec
, pidk
);
10024 /* Parse the `? expression : assignment-expression' part of a
10025 conditional-expression. The LOGICAL_OR_EXPR is the
10026 logical-or-expression that started the conditional-expression.
10027 Returns a representation of the entire conditional-expression.
10029 This routine is used by cp_parser_assignment_expression.
10031 ? expression : assignment-expression
10035 ? : assignment-expression */
10038 cp_parser_question_colon_clause (cp_parser
* parser
, cp_expr logical_or_expr
)
10040 tree expr
, folded_logical_or_expr
= cp_fully_fold (logical_or_expr
);
10041 cp_expr assignment_expr
;
10042 struct cp_token
*token
;
10043 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10045 /* Consume the `?' token. */
10046 cp_lexer_consume_token (parser
->lexer
);
10047 token
= cp_lexer_peek_token (parser
->lexer
);
10048 if (cp_parser_allow_gnu_extensions_p (parser
)
10049 && token
->type
== CPP_COLON
)
10051 pedwarn (token
->location
, OPT_Wpedantic
,
10052 "ISO C++ does not allow %<?:%> with omitted middle operand");
10053 /* Implicit true clause. */
10055 c_inhibit_evaluation_warnings
+=
10056 folded_logical_or_expr
== truthvalue_true_node
;
10057 warn_for_omitted_condop (token
->location
, logical_or_expr
);
10061 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
10062 parser
->colon_corrects_to_scope_p
= false;
10063 /* Parse the expression. */
10064 c_inhibit_evaluation_warnings
+=
10065 folded_logical_or_expr
== truthvalue_false_node
;
10066 expr
= cp_parser_expression (parser
);
10067 c_inhibit_evaluation_warnings
+=
10068 ((folded_logical_or_expr
== truthvalue_true_node
)
10069 - (folded_logical_or_expr
== truthvalue_false_node
));
10070 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
10073 /* The next token should be a `:'. */
10074 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
10075 /* Parse the assignment-expression. */
10076 assignment_expr
= cp_parser_assignment_expression (parser
);
10077 c_inhibit_evaluation_warnings
-=
10078 folded_logical_or_expr
== truthvalue_true_node
;
10080 /* Make a location:
10081 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
10082 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
10083 with the caret at the "?", ranging from the start of
10084 the logical_or_expr to the end of the assignment_expr. */
10085 loc
= make_location (loc
,
10086 logical_or_expr
.get_start (),
10087 assignment_expr
.get_finish ());
10089 /* Build the conditional-expression. */
10090 return build_x_conditional_expr (loc
, logical_or_expr
,
10093 tf_warning_or_error
);
10096 /* Parse an assignment-expression.
10098 assignment-expression:
10099 conditional-expression
10100 logical-or-expression assignment-operator assignment_expression
10104 CAST_P is true if this expression is the target of a cast.
10105 DECLTYPE_P is true if this expression is the operand of decltype.
10107 Returns a representation for the expression. */
10110 cp_parser_assignment_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10111 bool cast_p
, bool decltype_p
)
10115 /* If the next token is the `throw' keyword, then we're looking at
10116 a throw-expression. */
10117 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THROW
))
10118 expr
= cp_parser_throw_expression (parser
);
10119 /* If the next token is the `co_yield' keyword, then we're looking at
10120 a yield-expression. */
10121 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CO_YIELD
))
10122 expr
= cp_parser_yield_expression (parser
);
10123 /* Otherwise, it must be that we are looking at a
10124 logical-or-expression. */
10127 /* Parse the binary expressions (logical-or-expression). */
10128 expr
= cp_parser_binary_expression (parser
, cast_p
, false,
10130 PREC_NOT_OPERATOR
, pidk
);
10131 /* If the next token is a `?' then we're actually looking at a
10132 conditional-expression. */
10133 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10134 return cp_parser_question_colon_clause (parser
, expr
);
10137 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10139 /* If it's an assignment-operator, we're using the second
10141 enum tree_code assignment_operator
10142 = cp_parser_assignment_operator_opt (parser
);
10143 if (assignment_operator
!= ERROR_MARK
)
10145 bool non_constant_p
;
10147 /* Parse the right-hand side of the assignment. */
10148 cp_expr rhs
= cp_parser_initializer_clause (parser
,
10151 if (BRACE_ENCLOSED_INITIALIZER_P (rhs
))
10152 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
10154 /* An assignment may not appear in a
10155 constant-expression. */
10156 if (cp_parser_non_integral_constant_expression (parser
,
10158 return error_mark_node
;
10159 /* Build the assignment expression. Its default
10163 is the location of the '=' token as the
10164 caret, ranging from the start of the lhs to the
10166 loc
= make_location (loc
,
10168 rhs
.get_finish ());
10169 expr
= build_x_modify_expr (loc
, expr
,
10170 assignment_operator
,
10172 complain_flags (decltype_p
));
10173 /* TODO: build_x_modify_expr doesn't honor the location,
10174 so we must set it here. */
10175 expr
.set_location (loc
);
10183 /* Parse an (optional) assignment-operator.
10185 assignment-operator: one of
10186 = *= /= %= += -= >>= <<= &= ^= |=
10190 assignment-operator: one of
10193 If the next token is an assignment operator, the corresponding tree
10194 code is returned, and the token is consumed. For example, for
10195 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
10196 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
10197 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
10198 operator, ERROR_MARK is returned. */
10200 static enum tree_code
10201 cp_parser_assignment_operator_opt (cp_parser
* parser
)
10206 /* Peek at the next token. */
10207 token
= cp_lexer_peek_token (parser
->lexer
);
10209 switch (token
->type
)
10220 op
= TRUNC_DIV_EXPR
;
10224 op
= TRUNC_MOD_EXPR
;
10235 case CPP_RSHIFT_EQ
:
10239 case CPP_LSHIFT_EQ
:
10256 /* Nothing else is an assignment operator. */
10260 /* An operator followed by ... is a fold-expression, handled elsewhere. */
10261 if (op
!= ERROR_MARK
10262 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10265 /* If it was an assignment operator, consume it. */
10266 if (op
!= ERROR_MARK
)
10267 cp_lexer_consume_token (parser
->lexer
);
10272 /* Parse an expression.
10275 assignment-expression
10276 expression , assignment-expression
10278 CAST_P is true if this expression is the target of a cast.
10279 DECLTYPE_P is true if this expression is the immediate operand of decltype,
10280 except possibly parenthesized or on the RHS of a comma (N3276).
10281 WARN_COMMA_P is true if a comma should be diagnosed.
10283 Returns a representation of the expression. */
10286 cp_parser_expression (cp_parser
* parser
, cp_id_kind
* pidk
,
10287 bool cast_p
, bool decltype_p
, bool warn_comma_p
)
10289 cp_expr expression
= NULL_TREE
;
10290 location_t loc
= UNKNOWN_LOCATION
;
10294 cp_expr assignment_expression
;
10296 /* Parse the next assignment-expression. */
10297 assignment_expression
10298 = cp_parser_assignment_expression (parser
, pidk
, cast_p
, decltype_p
);
10300 /* We don't create a temporary for a call that is the immediate operand
10301 of decltype or on the RHS of a comma. But when we see a comma, we
10302 need to create a temporary for a call on the LHS. */
10303 if (decltype_p
&& !processing_template_decl
10304 && TREE_CODE (assignment_expression
) == CALL_EXPR
10305 && CLASS_TYPE_P (TREE_TYPE (assignment_expression
))
10306 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10307 assignment_expression
10308 = build_cplus_new (TREE_TYPE (assignment_expression
),
10309 assignment_expression
, tf_warning_or_error
);
10311 /* If this is the first assignment-expression, we can just
10314 expression
= assignment_expression
;
10317 /* Create a location with caret at the comma, ranging
10318 from the start of the LHS to the end of the RHS. */
10319 loc
= make_location (loc
,
10320 expression
.get_start (),
10321 assignment_expression
.get_finish ());
10322 expression
= build_x_compound_expr (loc
, expression
,
10323 assignment_expression
,
10324 complain_flags (decltype_p
));
10325 expression
.set_location (loc
);
10327 /* If the next token is not a comma, or we're in a fold-expression, then
10328 we are done with the expression. */
10329 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
10330 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
))
10332 /* Consume the `,'. */
10333 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10336 /* [depr.comma.subscript]: A comma expression appearing as
10337 the expr-or-braced-init-list of a subscripting expression
10338 is deprecated. A parenthesized comma expression is not
10340 warning_at (loc
, OPT_Wcomma_subscript
,
10341 "top-level comma expression in array subscript "
10343 warn_comma_p
= false;
10345 cp_lexer_consume_token (parser
->lexer
);
10346 /* A comma operator cannot appear in a constant-expression. */
10347 if (cp_parser_non_integral_constant_expression (parser
, NIC_COMMA
))
10348 expression
= error_mark_node
;
10354 /* Parse a constant-expression.
10356 constant-expression:
10357 conditional-expression
10359 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
10360 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
10361 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
10362 is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
10363 only parse a conditional-expression, otherwise parse an
10364 assignment-expression. See below for rationale. */
10367 cp_parser_constant_expression (cp_parser
* parser
,
10368 bool allow_non_constant_p
,
10369 bool *non_constant_p
,
10372 bool saved_integral_constant_expression_p
;
10373 bool saved_allow_non_integral_constant_expression_p
;
10374 bool saved_non_integral_constant_expression_p
;
10375 cp_expr expression
;
10377 /* It might seem that we could simply parse the
10378 conditional-expression, and then check to see if it were
10379 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
10380 one that the compiler can figure out is constant, possibly after
10381 doing some simplifications or optimizations. The standard has a
10382 precise definition of constant-expression, and we must honor
10383 that, even though it is somewhat more restrictive.
10389 is not a legal declaration, because `(2, 3)' is not a
10390 constant-expression. The `,' operator is forbidden in a
10391 constant-expression. However, GCC's constant-folding machinery
10392 will fold this operation to an INTEGER_CST for `3'. */
10394 /* Save the old settings. */
10395 saved_integral_constant_expression_p
= parser
->integral_constant_expression_p
;
10396 saved_allow_non_integral_constant_expression_p
10397 = parser
->allow_non_integral_constant_expression_p
;
10398 saved_non_integral_constant_expression_p
= parser
->non_integral_constant_expression_p
;
10399 /* We are now parsing a constant-expression. */
10400 parser
->integral_constant_expression_p
= true;
10401 parser
->allow_non_integral_constant_expression_p
10402 = (allow_non_constant_p
|| cxx_dialect
>= cxx11
);
10403 parser
->non_integral_constant_expression_p
= false;
10404 /* Although the grammar says "conditional-expression", when not STRICT_P,
10405 we parse an "assignment-expression", which also permits
10406 "throw-expression" and the use of assignment operators. In the case
10407 that ALLOW_NON_CONSTANT_P is false, we get better errors than we would
10408 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
10409 actually essential that we look for an assignment-expression.
10410 For example, cp_parser_initializer_clauses uses this function to
10411 determine whether a particular assignment-expression is in fact
10415 /* Parse the binary expressions (logical-or-expression). */
10416 expression
= cp_parser_binary_expression (parser
, false, false, false,
10417 PREC_NOT_OPERATOR
, NULL
);
10418 /* If the next token is a `?' then we're actually looking at
10419 a conditional-expression; otherwise we're done. */
10420 if (cp_lexer_next_token_is (parser
->lexer
, CPP_QUERY
))
10421 expression
= cp_parser_question_colon_clause (parser
, expression
);
10424 expression
= cp_parser_assignment_expression (parser
);
10425 /* Restore the old settings. */
10426 parser
->integral_constant_expression_p
10427 = saved_integral_constant_expression_p
;
10428 parser
->allow_non_integral_constant_expression_p
10429 = saved_allow_non_integral_constant_expression_p
;
10430 if (cxx_dialect
>= cxx11
)
10432 /* Require an rvalue constant expression here; that's what our
10433 callers expect. Reference constant expressions are handled
10434 separately in e.g. cp_parser_template_argument. */
10435 tree decay
= expression
;
10436 if (TREE_TYPE (expression
)
10437 && TREE_CODE (TREE_TYPE (expression
)) == ARRAY_TYPE
)
10438 decay
= build_address (expression
);
10439 bool is_const
= is_rvalue_constant_expression (decay
);
10440 parser
->non_integral_constant_expression_p
= !is_const
;
10441 if (!is_const
&& !allow_non_constant_p
)
10442 require_rvalue_constant_expression (decay
);
10444 if (allow_non_constant_p
)
10445 *non_constant_p
= parser
->non_integral_constant_expression_p
;
10446 parser
->non_integral_constant_expression_p
10447 = saved_non_integral_constant_expression_p
;
10452 /* Parse __builtin_offsetof.
10454 offsetof-expression:
10455 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
10457 offsetof-member-designator:
10459 | offsetof-member-designator "." id-expression
10460 | offsetof-member-designator "[" expression "]"
10461 | offsetof-member-designator "->" id-expression */
10464 cp_parser_builtin_offsetof (cp_parser
*parser
)
10466 int save_ice_p
, save_non_ice_p
;
10471 location_t finish_loc
;
10473 /* We're about to accept non-integral-constant things, but will
10474 definitely yield an integral constant expression. Save and
10475 restore these values around our local parsing. */
10476 save_ice_p
= parser
->integral_constant_expression_p
;
10477 save_non_ice_p
= parser
->non_integral_constant_expression_p
;
10479 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10481 /* Consume the "__builtin_offsetof" token. */
10482 cp_lexer_consume_token (parser
->lexer
);
10483 /* Consume the opening `('. */
10484 matching_parens parens
;
10485 parens
.require_open (parser
);
10486 /* Parse the type-id. */
10487 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10489 const char *saved_message
= parser
->type_definition_forbidden_message
;
10490 parser
->type_definition_forbidden_message
10491 = G_("types may not be defined within %<__builtin_offsetof%>");
10492 type
= cp_parser_type_id (parser
);
10493 parser
->type_definition_forbidden_message
= saved_message
;
10495 /* Look for the `,'. */
10496 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10497 token
= cp_lexer_peek_token (parser
->lexer
);
10499 /* Build the (type *)null that begins the traditional offsetof macro. */
10501 = build_static_cast (input_location
, build_pointer_type (type
),
10502 null_pointer_node
, tf_warning_or_error
);
10504 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
10505 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DEREF
, object_ptr
,
10506 true, &dummy
, token
->location
);
10509 token
= cp_lexer_peek_token (parser
->lexer
);
10510 switch (token
->type
)
10512 case CPP_OPEN_SQUARE
:
10513 /* offsetof-member-designator "[" expression "]" */
10514 expr
= cp_parser_postfix_open_square_expression (parser
, expr
,
10519 /* offsetof-member-designator "->" identifier */
10520 expr
= grok_array_decl (token
->location
, expr
,
10521 integer_zero_node
, false);
10525 /* offsetof-member-designator "." identifier */
10526 cp_lexer_consume_token (parser
->lexer
);
10527 expr
= cp_parser_postfix_dot_deref_expression (parser
, CPP_DOT
,
10528 expr
, true, &dummy
,
10532 case CPP_CLOSE_PAREN
:
10533 /* Consume the ")" token. */
10534 finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10535 cp_lexer_consume_token (parser
->lexer
);
10539 /* Error. We know the following require will fail, but
10540 that gives the proper error message. */
10541 parens
.require_close (parser
);
10542 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
10543 expr
= error_mark_node
;
10549 /* Make a location of the form:
10550 __builtin_offsetof (struct s, f)
10551 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
10552 with caret at the type-id, ranging from the start of the
10553 "_builtin_offsetof" token to the close paren. */
10554 loc
= make_location (loc
, start_loc
, finish_loc
);
10555 /* The result will be an INTEGER_CST, so we need to explicitly
10556 preserve the location. */
10557 expr
= cp_expr (finish_offsetof (object_ptr
, expr
, loc
), loc
);
10560 parser
->integral_constant_expression_p
= save_ice_p
;
10561 parser
->non_integral_constant_expression_p
= save_non_ice_p
;
10563 expr
= expr
.maybe_add_location_wrapper ();
10567 /* Parse a trait expression.
10569 Returns a representation of the expression, the underlying type
10570 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
10573 cp_parser_trait_expr (cp_parser
* parser
, enum rid keyword
)
10575 cp_trait_kind kind
;
10576 tree type1
, type2
= NULL_TREE
;
10577 bool binary
= false;
10578 bool variadic
= false;
10582 case RID_HAS_NOTHROW_ASSIGN
:
10583 kind
= CPTK_HAS_NOTHROW_ASSIGN
;
10585 case RID_HAS_NOTHROW_CONSTRUCTOR
:
10586 kind
= CPTK_HAS_NOTHROW_CONSTRUCTOR
;
10588 case RID_HAS_NOTHROW_COPY
:
10589 kind
= CPTK_HAS_NOTHROW_COPY
;
10591 case RID_HAS_TRIVIAL_ASSIGN
:
10592 kind
= CPTK_HAS_TRIVIAL_ASSIGN
;
10594 case RID_HAS_TRIVIAL_CONSTRUCTOR
:
10595 kind
= CPTK_HAS_TRIVIAL_CONSTRUCTOR
;
10597 case RID_HAS_TRIVIAL_COPY
:
10598 kind
= CPTK_HAS_TRIVIAL_COPY
;
10600 case RID_HAS_TRIVIAL_DESTRUCTOR
:
10601 kind
= CPTK_HAS_TRIVIAL_DESTRUCTOR
;
10603 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS
:
10604 kind
= CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS
;
10606 case RID_HAS_VIRTUAL_DESTRUCTOR
:
10607 kind
= CPTK_HAS_VIRTUAL_DESTRUCTOR
;
10609 case RID_IS_ABSTRACT
:
10610 kind
= CPTK_IS_ABSTRACT
;
10612 case RID_IS_AGGREGATE
:
10613 kind
= CPTK_IS_AGGREGATE
;
10615 case RID_IS_BASE_OF
:
10616 kind
= CPTK_IS_BASE_OF
;
10620 kind
= CPTK_IS_CLASS
;
10623 kind
= CPTK_IS_EMPTY
;
10626 kind
= CPTK_IS_ENUM
;
10629 kind
= CPTK_IS_FINAL
;
10631 case RID_IS_LITERAL_TYPE
:
10632 kind
= CPTK_IS_LITERAL_TYPE
;
10635 kind
= CPTK_IS_POD
;
10637 case RID_IS_POLYMORPHIC
:
10638 kind
= CPTK_IS_POLYMORPHIC
;
10640 case RID_IS_SAME_AS
:
10641 kind
= CPTK_IS_SAME_AS
;
10644 case RID_IS_STD_LAYOUT
:
10645 kind
= CPTK_IS_STD_LAYOUT
;
10647 case RID_IS_TRIVIAL
:
10648 kind
= CPTK_IS_TRIVIAL
;
10650 case RID_IS_TRIVIALLY_ASSIGNABLE
:
10651 kind
= CPTK_IS_TRIVIALLY_ASSIGNABLE
;
10654 case RID_IS_TRIVIALLY_CONSTRUCTIBLE
:
10655 kind
= CPTK_IS_TRIVIALLY_CONSTRUCTIBLE
;
10658 case RID_IS_TRIVIALLY_COPYABLE
:
10659 kind
= CPTK_IS_TRIVIALLY_COPYABLE
;
10662 kind
= CPTK_IS_UNION
;
10664 case RID_UNDERLYING_TYPE
:
10665 kind
= CPTK_UNDERLYING_TYPE
;
10670 case RID_DIRECT_BASES
:
10671 kind
= CPTK_DIRECT_BASES
;
10673 case RID_IS_ASSIGNABLE
:
10674 kind
= CPTK_IS_ASSIGNABLE
;
10677 case RID_IS_CONSTRUCTIBLE
:
10678 kind
= CPTK_IS_CONSTRUCTIBLE
;
10681 case RID_IS_NOTHROW_ASSIGNABLE
:
10682 kind
= CPTK_IS_NOTHROW_ASSIGNABLE
;
10685 case RID_IS_NOTHROW_CONSTRUCTIBLE
:
10686 kind
= CPTK_IS_NOTHROW_CONSTRUCTIBLE
;
10690 gcc_unreachable ();
10693 /* Get location of initial token. */
10694 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10696 /* Consume the token. */
10697 cp_lexer_consume_token (parser
->lexer
);
10699 matching_parens parens
;
10700 parens
.require_open (parser
);
10703 type_id_in_expr_sentinel
s (parser
);
10704 type1
= cp_parser_type_id (parser
);
10707 if (type1
== error_mark_node
)
10708 return error_mark_node
;
10712 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10715 type_id_in_expr_sentinel
s (parser
);
10716 type2
= cp_parser_type_id (parser
);
10719 if (type2
== error_mark_node
)
10720 return error_mark_node
;
10724 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
10726 cp_lexer_consume_token (parser
->lexer
);
10727 tree elt
= cp_parser_type_id (parser
);
10728 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
10730 cp_lexer_consume_token (parser
->lexer
);
10731 elt
= make_pack_expansion (elt
);
10733 if (elt
== error_mark_node
)
10734 return error_mark_node
;
10735 type2
= tree_cons (NULL_TREE
, elt
, type2
);
10739 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10740 parens
.require_close (parser
);
10742 /* Construct a location of the form:
10743 __is_trivially_copyable(_Tp)
10744 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
10745 with start == caret, finishing at the close-paren. */
10746 location_t trait_loc
= make_location (start_loc
, start_loc
, finish_loc
);
10748 /* Complete the trait expression, which may mean either processing
10749 the trait expr now or saving it for template instantiation. */
10752 case CPTK_UNDERLYING_TYPE
:
10753 return cp_expr (finish_underlying_type (type1
), trait_loc
);
10755 return cp_expr (finish_bases (type1
, false), trait_loc
);
10756 case CPTK_DIRECT_BASES
:
10757 return cp_expr (finish_bases (type1
, true), trait_loc
);
10759 return finish_trait_expr (trait_loc
, kind
, type1
, type2
);
10763 /* Parse a lambda expression.
10766 lambda-introducer lambda-declarator [opt] compound-statement
10767 lambda-introducer < template-parameter-list > requires-clause [opt]
10768 lambda-declarator [opt] compound-statement
10770 Returns a representation of the expression. */
10773 cp_parser_lambda_expression (cp_parser
* parser
)
10775 tree lambda_expr
= build_lambda_expr ();
10778 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
10779 cp_token_position start
= 0;
10781 LAMBDA_EXPR_LOCATION (lambda_expr
) = token
->location
;
10783 if (cxx_dialect
>= cxx20
)
10784 /* C++20 allows lambdas in unevaluated context. */;
10785 else if (cp_unevaluated_operand
)
10787 if (!token
->error_reported
)
10789 error_at (LAMBDA_EXPR_LOCATION (lambda_expr
),
10790 "lambda-expression in unevaluated context"
10791 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10792 token
->error_reported
= true;
10796 else if (parser
->in_template_argument_list_p
|| processing_template_parmlist
)
10798 if (!token
->error_reported
)
10800 error_at (token
->location
, "lambda-expression in template-argument"
10801 " only available with %<-std=c++20%> or %<-std=gnu++20%>");
10802 token
->error_reported
= true;
10807 /* We may be in the middle of deferred access check. Disable
10809 push_deferring_access_checks (dk_no_deferred
);
10811 cp_parser_lambda_introducer (parser
, lambda_expr
);
10812 if (cp_parser_error_occurred (parser
))
10813 return error_mark_node
;
10815 type
= begin_lambda_type (lambda_expr
);
10816 if (type
== error_mark_node
)
10817 return error_mark_node
;
10819 record_lambda_scope (lambda_expr
);
10821 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10822 determine_visibility (TYPE_NAME (type
));
10824 /* Now that we've started the type, add the capture fields for any
10825 explicit captures. */
10826 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10829 /* Inside the class, surrounding template-parameter-lists do not apply. */
10830 unsigned int saved_num_template_parameter_lists
10831 = parser
->num_template_parameter_lists
;
10832 unsigned char in_statement
= parser
->in_statement
;
10833 bool in_switch_statement_p
= parser
->in_switch_statement_p
;
10834 bool fully_implicit_function_template_p
10835 = parser
->fully_implicit_function_template_p
;
10836 tree implicit_template_parms
= parser
->implicit_template_parms
;
10837 cp_binding_level
* implicit_template_scope
= parser
->implicit_template_scope
;
10838 bool auto_is_implicit_function_template_parm_p
10839 = parser
->auto_is_implicit_function_template_parm_p
;
10841 parser
->num_template_parameter_lists
= 0;
10842 parser
->in_statement
= 0;
10843 parser
->in_switch_statement_p
= false;
10844 parser
->fully_implicit_function_template_p
= false;
10845 parser
->implicit_template_parms
= 0;
10846 parser
->implicit_template_scope
= 0;
10847 parser
->auto_is_implicit_function_template_parm_p
= false;
10849 /* The body of a lambda in a discarded statement is not discarded. */
10850 bool discarded
= in_discarded_stmt
;
10851 in_discarded_stmt
= 0;
10853 /* By virtue of defining a local class, a lambda expression has access to
10854 the private variables of enclosing classes. */
10856 if (cp_parser_start_tentative_firewall (parser
))
10859 ok
&= cp_parser_lambda_declarator_opt (parser
, lambda_expr
);
10861 if (ok
&& cp_parser_error_occurred (parser
))
10866 cp_parser_lambda_body (parser
, lambda_expr
);
10868 else if (cp_parser_require (parser
, CPP_OPEN_BRACE
, RT_OPEN_BRACE
))
10870 if (cp_parser_skip_to_closing_brace (parser
))
10871 cp_lexer_consume_token (parser
->lexer
);
10874 /* The capture list was built up in reverse order; fix that now. */
10875 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
)
10876 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr
));
10879 maybe_add_lambda_conv_op (type
);
10881 finish_struct (type
, /*attributes=*/NULL_TREE
);
10883 in_discarded_stmt
= discarded
;
10885 parser
->num_template_parameter_lists
= saved_num_template_parameter_lists
;
10886 parser
->in_statement
= in_statement
;
10887 parser
->in_switch_statement_p
= in_switch_statement_p
;
10888 parser
->fully_implicit_function_template_p
10889 = fully_implicit_function_template_p
;
10890 parser
->implicit_template_parms
= implicit_template_parms
;
10891 parser
->implicit_template_scope
= implicit_template_scope
;
10892 parser
->auto_is_implicit_function_template_parm_p
10893 = auto_is_implicit_function_template_parm_p
;
10896 /* This field is only used during parsing of the lambda. */
10897 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
) = NULL_TREE
;
10899 /* This lambda shouldn't have any proxies left at this point. */
10900 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr
) == NULL
);
10901 /* And now that we're done, push proxies for an enclosing lambda. */
10902 insert_pending_capture_proxies ();
10904 /* Update the lambda expression to a range. */
10905 LAMBDA_EXPR_LOCATION (lambda_expr
) = make_location (token
->location
,
10910 lambda_expr
= build_lambda_object (lambda_expr
);
10912 lambda_expr
= error_mark_node
;
10914 cp_parser_end_tentative_firewall (parser
, start
, lambda_expr
);
10916 pop_deferring_access_checks ();
10918 return lambda_expr
;
10921 /* Parse the beginning of a lambda expression.
10924 [ lambda-capture [opt] ]
10926 LAMBDA_EXPR is the current representation of the lambda expression. */
10929 cp_parser_lambda_introducer (cp_parser
* parser
, tree lambda_expr
)
10931 /* Need commas after the first capture. */
10934 /* Eat the leading `['. */
10935 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
10937 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
10939 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_ELLIPSIS
)
10940 && !cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
)
10941 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
10942 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_REFERENCE
;
10943 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
10944 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) = CPLD_COPY
;
10946 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
)
10948 cp_lexer_consume_token (parser
->lexer
);
10951 if (!(at_function_scope_p () || parsing_nsdmi ()))
10952 error ("non-local lambda expression cannot have a capture-default");
10955 hash_set
<tree
, true> ids
;
10956 tree first_capture_id
= NULL_TREE
;
10957 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_SQUARE
))
10959 cp_token
* capture_token
;
10961 tree capture_init_expr
;
10962 cp_id_kind idk
= CP_ID_KIND_NONE
;
10963 bool explicit_init_p
= false;
10965 enum capture_kind_type
10970 enum capture_kind_type capture_kind
= BY_COPY
;
10972 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
10974 error ("expected end of capture-list");
10981 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
10983 /* Possibly capture `this'. */
10984 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_THIS
))
10986 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
10987 if (cxx_dialect
< cxx20
10988 && LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
)
10989 pedwarn (loc
, 0, "explicit by-copy capture of %<this%> redundant "
10990 "with by-copy capture default");
10991 cp_lexer_consume_token (parser
->lexer
);
10992 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
10993 pedwarn (input_location
, 0,
10994 "already captured %qD in lambda expression",
10997 add_capture (lambda_expr
, /*id=*/this_identifier
,
10998 /*initializer=*/finish_this_expr (),
10999 /*by_reference_p=*/true, explicit_init_p
);
11003 /* Possibly capture `*this'. */
11004 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
11005 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11007 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11008 if (cxx_dialect
< cxx17
)
11009 pedwarn (loc
, 0, "%<*this%> capture only available with "
11010 "%<-std=c++17%> or %<-std=gnu++17%>");
11011 cp_lexer_consume_token (parser
->lexer
);
11012 cp_lexer_consume_token (parser
->lexer
);
11013 if (LAMBDA_EXPR_THIS_CAPTURE (lambda_expr
))
11014 pedwarn (input_location
, 0,
11015 "already captured %qD in lambda expression",
11018 add_capture (lambda_expr
, /*id=*/this_identifier
,
11019 /*initializer=*/finish_this_expr (),
11020 /*by_reference_p=*/false, explicit_init_p
);
11024 /* But reject `&this'. */
11025 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
)
11026 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_THIS
))
11028 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
11029 "%<this%> cannot be captured by reference");
11030 cp_lexer_consume_token (parser
->lexer
);
11031 cp_lexer_consume_token (parser
->lexer
);
11035 /* Remember whether we want to capture as a reference or not. */
11036 if (cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
11038 capture_kind
= BY_REFERENCE
;
11039 cp_lexer_consume_token (parser
->lexer
);
11042 bool init_pack_expansion
= false;
11043 location_t ellipsis_loc
= UNKNOWN_LOCATION
;
11044 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11046 ellipsis_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11047 if (cxx_dialect
< cxx20
)
11048 pedwarn (ellipsis_loc
, 0, "pack init-capture only available with "
11049 "%<-std=c++20%> or %<-std=gnu++20%>");
11050 cp_lexer_consume_token (parser
->lexer
);
11051 init_pack_expansion
= true;
11054 /* Early C++20 drafts had ...& instead of &...; be forgiving. */
11055 if (init_pack_expansion
&& capture_kind
!= BY_REFERENCE
11056 && cp_lexer_next_token_is (parser
->lexer
, CPP_AND
))
11058 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
11059 0, "%<&%> should come before %<...%>");
11060 capture_kind
= BY_REFERENCE
;
11061 cp_lexer_consume_token (parser
->lexer
);
11064 /* Get the identifier. */
11065 capture_token
= cp_lexer_peek_token (parser
->lexer
);
11066 capture_id
= cp_parser_identifier (parser
);
11068 if (capture_id
== error_mark_node
)
11069 /* Would be nice to have a cp_parser_skip_to_closing_x for general
11070 delimiters, but I modified this to stop on unnested ']' as well. It
11071 was already changed to stop on unnested '}', so the
11072 "closing_parenthesis" name is no more misleading with my change. */
11074 cp_parser_skip_to_closing_parenthesis (parser
,
11075 /*recovering=*/true,
11077 /*consume_paren=*/true);
11081 /* Find the initializer for this capture. */
11082 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
11083 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
11084 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11086 bool direct
, non_constant
;
11087 /* An explicit initializer exists. */
11088 if (cxx_dialect
< cxx14
)
11089 pedwarn (input_location
, 0,
11090 "lambda capture initializers "
11091 "only available with %<-std=c++14%> or %<-std=gnu++14%>");
11092 capture_init_expr
= cp_parser_initializer (parser
, &direct
,
11093 &non_constant
, true);
11094 explicit_init_p
= true;
11095 if (capture_init_expr
== NULL_TREE
)
11097 error ("empty initializer for lambda init-capture");
11098 capture_init_expr
= error_mark_node
;
11100 if (init_pack_expansion
)
11101 capture_init_expr
= make_pack_expansion (capture_init_expr
);
11105 const char* error_msg
;
11107 /* Turn the identifier into an id-expression. */
11109 = cp_parser_lookup_name_simple (parser
, capture_id
,
11110 capture_token
->location
);
11112 if (capture_init_expr
== error_mark_node
)
11114 unqualified_name_lookup_error (capture_id
);
11117 else if (!VAR_P (capture_init_expr
)
11118 && TREE_CODE (capture_init_expr
) != PARM_DECL
)
11120 error_at (capture_token
->location
,
11121 "capture of non-variable %qE",
11122 capture_init_expr
);
11123 if (DECL_P (capture_init_expr
))
11124 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
11125 "%q#D declared here", capture_init_expr
);
11128 if (VAR_P (capture_init_expr
)
11129 && decl_storage_duration (capture_init_expr
) != dk_auto
)
11131 if (pedwarn (capture_token
->location
, 0, "capture of variable "
11132 "%qD with non-automatic storage duration",
11133 capture_init_expr
))
11134 inform (DECL_SOURCE_LOCATION (capture_init_expr
),
11135 "%q#D declared here", capture_init_expr
);
11140 = finish_id_expression
11145 /*integral_constant_expression_p=*/false,
11146 /*allow_non_integral_constant_expression_p=*/false,
11147 /*non_integral_constant_expression_p=*/NULL
,
11148 /*template_p=*/false,
11150 /*address_p=*/false,
11151 /*template_arg_p=*/false,
11153 capture_token
->location
);
11155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
11157 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11158 cp_lexer_consume_token (parser
->lexer
);
11159 capture_init_expr
= make_pack_expansion (capture_init_expr
);
11160 if (init_pack_expansion
)
11162 /* If what follows is an initializer, the second '...' is
11163 invalid. But for cases like [...xs...], the first one
11165 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
11166 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
11167 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
11168 ellipsis_loc
= loc
;
11169 error_at (ellipsis_loc
, "too many %<...%> in lambda capture");
11175 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) != CPLD_NONE
11176 && !explicit_init_p
)
11178 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_COPY
11179 && capture_kind
== BY_COPY
)
11180 pedwarn (capture_token
->location
, 0, "explicit by-copy capture "
11181 "of %qD redundant with by-copy capture default",
11183 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr
) == CPLD_REFERENCE
11184 && capture_kind
== BY_REFERENCE
)
11185 pedwarn (capture_token
->location
, 0, "explicit by-reference "
11186 "capture of %qD redundant with by-reference capture "
11187 "default", capture_id
);
11190 /* Check for duplicates.
11191 Optimize for the zero or one explicit captures cases and only create
11192 the hash_set after adding second capture. */
11193 bool found
= false;
11194 if (!ids
.is_empty ())
11195 found
= ids
.add (capture_id
);
11196 else if (first_capture_id
== NULL_TREE
)
11197 first_capture_id
= capture_id
;
11198 else if (capture_id
== first_capture_id
)
11202 ids
.add (first_capture_id
);
11203 ids
.add (capture_id
);
11206 pedwarn (input_location
, 0,
11207 "already captured %qD in lambda expression", capture_id
);
11209 add_capture (lambda_expr
, capture_id
, capture_init_expr
,
11210 /*by_reference_p=*/capture_kind
== BY_REFERENCE
,
11213 /* If there is any qualification still in effect, clear it
11214 now; we will be starting fresh with the next capture. */
11215 parser
->scope
= NULL_TREE
;
11216 parser
->qualifying_scope
= NULL_TREE
;
11217 parser
->object_scope
= NULL_TREE
;
11220 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
11223 /* Parse the (optional) middle of a lambda expression.
11226 ( parameter-declaration-clause )
11227 decl-specifier-seq [opt]
11228 noexcept-specifier [opt]
11229 attribute-specifier-seq [opt]
11230 trailing-return-type [opt]
11231 requires-clause [opt]
11233 LAMBDA_EXPR is the current representation of the lambda expression. */
11236 cp_parser_lambda_declarator_opt (cp_parser
* parser
, tree lambda_expr
)
11238 /* 5.1.1.4 of the standard says:
11239 If a lambda-expression does not include a lambda-declarator, it is as if
11240 the lambda-declarator were ().
11241 This means an empty parameter list, no attributes, and no exception
11243 tree param_list
= void_list_node
;
11244 tree std_attrs
= NULL_TREE
;
11245 tree gnu_attrs
= NULL_TREE
;
11246 tree exception_spec
= NULL_TREE
;
11247 tree template_param_list
= NULL_TREE
;
11248 tree tx_qual
= NULL_TREE
;
11249 tree return_type
= NULL_TREE
;
11250 tree trailing_requires_clause
= NULL_TREE
;
11251 cp_decl_specifier_seq lambda_specs
;
11252 clear_decl_specs (&lambda_specs
);
11253 /* A lambda op() is const unless explicitly 'mutable'. */
11254 cp_cv_quals quals
= TYPE_QUAL_CONST
;
11256 /* The template-parameter-list is optional, but must begin with
11257 an opening angle if present. */
11258 if (cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
11260 if (cxx_dialect
< cxx14
)
11261 pedwarn (parser
->lexer
->next_token
->location
, 0,
11262 "lambda templates are only available with "
11263 "%<-std=c++14%> or %<-std=gnu++14%>");
11264 else if (cxx_dialect
< cxx20
)
11265 pedwarn (parser
->lexer
->next_token
->location
, OPT_Wpedantic
,
11266 "lambda templates are only available with "
11267 "%<-std=c++20%> or %<-std=gnu++20%>");
11269 cp_lexer_consume_token (parser
->lexer
);
11271 template_param_list
= cp_parser_template_parameter_list (parser
);
11272 cp_parser_skip_to_end_of_template_parameter_list (parser
);
11274 /* We may have a constrained generic lambda; parse the requires-clause
11275 immediately after the template-parameter-list and combine with any
11276 shorthand constraints present. */
11277 tree dreqs
= cp_parser_requires_clause_opt (parser
, true);
11280 tree reqs
= get_shorthand_constraints (current_template_parms
);
11282 reqs
= combine_constraint_expressions (reqs
, dreqs
);
11283 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
11286 /* We just processed one more parameter list. */
11287 ++parser
->num_template_parameter_lists
;
11290 /* Committee discussion supports allowing attributes here. */
11291 lambda_specs
.attributes
= cp_parser_attributes_opt (parser
);
11293 /* The parameter-declaration-clause is optional (unless
11294 template-parameter-list was given), but must begin with an
11295 opening parenthesis if present. */
11296 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
11298 bool is_consteval
= false;
11299 /* For C++20, before parsing the parameter list check if there is
11300 a consteval specifier in the corresponding decl-specifier-seq. */
11301 if (cxx_dialect
>= cxx20
)
11303 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1);
11304 cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_KEYWORD
); n
++)
11306 if (cp_lexer_peek_nth_token (parser
->lexer
, n
)->keyword
11309 is_consteval
= true;
11315 matching_parens parens
;
11316 parens
.consume_open (parser
);
11318 begin_scope (sk_function_parms
, /*entity=*/NULL_TREE
);
11321 current_binding_level
->immediate_fn_ctx_p
= true;
11323 /* Parse parameters. */
11325 = cp_parser_parameter_declaration_clause
11326 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
);
11328 /* Default arguments shall not be specified in the
11329 parameter-declaration-clause of a lambda-declarator. */
11330 if (cxx_dialect
< cxx14
)
11331 for (tree t
= param_list
; t
; t
= TREE_CHAIN (t
))
11332 if (TREE_PURPOSE (t
) && DECL_P (TREE_VALUE (t
)))
11333 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t
)), OPT_Wpedantic
,
11334 "default argument specified for lambda parameter");
11336 parens
.require_close (parser
);
11338 /* In the decl-specifier-seq of the lambda-declarator, each
11339 decl-specifier shall either be mutable or constexpr. */
11340 int declares_class_or_enum
;
11341 if (cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
11342 && !cp_next_tokens_can_be_gnu_attribute_p (parser
))
11343 cp_parser_decl_specifier_seq (parser
,
11344 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
,
11345 &lambda_specs
, &declares_class_or_enum
);
11346 if (lambda_specs
.storage_class
== sc_mutable
)
11348 LAMBDA_EXPR_MUTABLE_P (lambda_expr
) = 1;
11349 quals
= TYPE_UNQUALIFIED
;
11350 if (lambda_specs
.conflicting_specifiers_p
)
11351 error_at (lambda_specs
.locations
[ds_storage_class
],
11352 "duplicate %<mutable%>");
11355 tx_qual
= cp_parser_tx_qualifier_opt (parser
);
11357 /* Parse optional exception specification. */
11359 = cp_parser_exception_specification_opt (parser
, CP_PARSER_FLAGS_NONE
);
11361 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11363 /* Parse optional trailing return type. */
11364 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
11366 cp_lexer_consume_token (parser
->lexer
);
11367 return_type
= cp_parser_trailing_type_id (parser
);
11370 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
11371 gnu_attrs
= cp_parser_gnu_attributes_opt (parser
);
11373 /* Parse optional trailing requires clause. */
11374 trailing_requires_clause
= cp_parser_requires_clause_opt (parser
, false);
11376 /* The function parameters must be in scope all the way until after the
11377 trailing-return-type in case of decltype. */
11378 pop_bindings_and_leave_scope ();
11381 /* Create the function call operator.
11383 Messing with declarators like this is no uglier than building up the
11384 FUNCTION_DECL by hand, and this is less likely to get out of sync with
11387 cp_decl_specifier_seq return_type_specs
;
11388 cp_declarator
* declarator
;
11392 clear_decl_specs (&return_type_specs
);
11393 return_type_specs
.type
= make_auto ();
11395 if (lambda_specs
.locations
[ds_constexpr
])
11397 if (cxx_dialect
>= cxx17
)
11398 return_type_specs
.locations
[ds_constexpr
]
11399 = lambda_specs
.locations
[ds_constexpr
];
11401 error_at (lambda_specs
.locations
[ds_constexpr
], "%<constexpr%> "
11402 "lambda only available with %<-std=c++17%> or "
11403 "%<-std=gnu++17%>");
11405 if (lambda_specs
.locations
[ds_consteval
])
11406 return_type_specs
.locations
[ds_consteval
]
11407 = lambda_specs
.locations
[ds_consteval
];
11409 p
= obstack_alloc (&declarator_obstack
, 0);
11411 declarator
= make_id_declarator (NULL_TREE
, call_op_identifier
, sfk_none
,
11412 LAMBDA_EXPR_LOCATION (lambda_expr
));
11414 declarator
= make_call_declarator (declarator
, param_list
, quals
,
11415 VIRT_SPEC_UNSPECIFIED
,
11420 trailing_requires_clause
,
11422 declarator
->std_attributes
= std_attrs
;
11424 fco
= grokmethod (&return_type_specs
,
11426 chainon (gnu_attrs
, lambda_specs
.attributes
));
11427 if (fco
!= error_mark_node
)
11429 DECL_INITIALIZED_IN_CLASS_P (fco
) = 1;
11430 DECL_ARTIFICIAL (fco
) = 1;
11431 /* Give the object parameter a different name. */
11432 DECL_NAME (DECL_ARGUMENTS (fco
)) = closure_identifier
;
11433 DECL_SET_LAMBDA_FUNCTION (fco
, true);
11435 if (template_param_list
)
11437 fco
= finish_member_template_decl (fco
);
11438 finish_template_decl (template_param_list
);
11439 --parser
->num_template_parameter_lists
;
11441 else if (parser
->fully_implicit_function_template_p
)
11442 fco
= finish_fully_implicit_template (parser
, fco
);
11444 finish_member_declaration (fco
);
11446 obstack_free (&declarator_obstack
, p
);
11448 return (fco
!= error_mark_node
);
11452 /* Parse the body of a lambda expression, which is simply
11456 but which requires special handling.
11457 LAMBDA_EXPR is the current representation of the lambda expression. */
11460 cp_parser_lambda_body (cp_parser
* parser
, tree lambda_expr
)
11462 bool nested
= (current_function_decl
!= NULL_TREE
);
11463 unsigned char local_variables_forbidden_p
11464 = parser
->local_variables_forbidden_p
;
11465 bool in_function_body
= parser
->in_function_body
;
11467 /* The body of a lambda-expression is not a subexpression of the enclosing
11472 push_function_context ();
11474 /* Still increment function_depth so that we don't GC in the
11475 middle of an expression. */
11478 vec
<tree
> omp_privatization_save
;
11479 save_omp_privatization_clauses (omp_privatization_save
);
11480 /* Clear this in case we're in the middle of a default argument. */
11481 parser
->local_variables_forbidden_p
= 0;
11482 parser
->in_function_body
= true;
11485 local_specialization_stack
s (lss_copy
);
11486 tree fco
= lambda_function (lambda_expr
);
11487 tree body
= start_lambda_function (fco
, lambda_expr
);
11489 /* Originally C++11 required us to peek for 'return expr'; and
11490 process it specially here to deduce the return type. N3638
11491 removed the need for that. */
11492 cp_parser_function_body (parser
, false);
11494 finish_lambda_function (body
);
11497 restore_omp_privatization_clauses (omp_privatization_save
);
11498 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
11499 parser
->in_function_body
= in_function_body
;
11501 pop_function_context();
11506 /* Statements [gram.stmt.stmt] */
11508 /* Build and add a DEBUG_BEGIN_STMT statement with location LOC. */
11511 add_debug_begin_stmt (location_t loc
)
11513 if (!MAY_HAVE_DEBUG_MARKER_STMTS
)
11515 if (DECL_DECLARED_CONCEPT_P (current_function_decl
))
11516 /* A concept is never expanded normally. */
11519 tree stmt
= build0 (DEBUG_BEGIN_STMT
, void_type_node
);
11520 SET_EXPR_LOCATION (stmt
, loc
);
11524 /* Parse a statement.
11528 expression-statement
11530 selection-statement
11531 iteration-statement
11533 declaration-statement
11540 attribute-specifier-seq (opt) expression-statement
11541 attribute-specifier-seq (opt) compound-statement
11542 attribute-specifier-seq (opt) selection-statement
11543 attribute-specifier-seq (opt) iteration-statement
11544 attribute-specifier-seq (opt) jump-statement
11545 declaration-statement
11546 attribute-specifier-seq (opt) try-block
11549 expression-statement
11557 IN_COMPOUND is true when the statement is nested inside a
11558 cp_parser_compound_statement; this matters for certain pragmas.
11560 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11561 is a (possibly labeled) if statement which is not enclosed in braces
11562 and has an else clause. This is used to implement -Wparentheses.
11564 CHAIN is a vector of if-else-if conditions. */
11567 cp_parser_statement (cp_parser
* parser
, tree in_statement_expr
,
11568 bool in_compound
, bool *if_p
, vec
<tree
> *chain
,
11569 location_t
*loc_after_labels
)
11571 tree statement
, std_attrs
= NULL_TREE
;
11573 location_t statement_location
, attrs_loc
;
11578 /* There is no statement yet. */
11579 statement
= NULL_TREE
;
11581 saved_token_sentinel
saved_tokens (parser
->lexer
);
11582 attrs_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
11583 if (c_dialect_objc ())
11584 /* In obj-c++, seeing '[[' might be the either the beginning of
11585 c++11 attributes, or a nested objc-message-expression. So
11586 let's parse the c++11 attributes tentatively. */
11587 cp_parser_parse_tentatively (parser
);
11588 std_attrs
= cp_parser_std_attribute_spec_seq (parser
);
11590 attrs_loc
= make_location (attrs_loc
, attrs_loc
, parser
->lexer
);
11591 if (c_dialect_objc ())
11593 if (!cp_parser_parse_definitely (parser
))
11594 std_attrs
= NULL_TREE
;
11597 /* Peek at the next token. */
11598 token
= cp_lexer_peek_token (parser
->lexer
);
11599 /* Remember the location of the first token in the statement. */
11600 cp_token
*statement_token
= token
;
11601 statement_location
= token
->location
;
11602 add_debug_begin_stmt (statement_location
);
11603 /* If this is a keyword, then that will often determine what kind of
11604 statement we have. */
11605 if (token
->type
== CPP_KEYWORD
)
11607 enum rid keyword
= token
->keyword
;
11613 /* Looks like a labeled-statement with a case label.
11614 Parse the label, and then use tail recursion to parse
11616 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11617 in_compound
= false;
11622 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11623 statement
= cp_parser_selection_statement (parser
, if_p
, chain
);
11629 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11630 statement
= cp_parser_iteration_statement (parser
, if_p
, false, 0);
11636 case RID_CO_RETURN
:
11638 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11639 statement
= cp_parser_jump_statement (parser
);
11642 /* Objective-C++ exception-handling constructs. */
11645 case RID_AT_FINALLY
:
11646 case RID_AT_SYNCHRONIZED
:
11648 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11649 statement
= cp_parser_objc_statement (parser
);
11653 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11654 statement
= cp_parser_try_block (parser
);
11657 case RID_NAMESPACE
:
11658 /* This must be a namespace alias definition. */
11659 if (std_attrs
!= NULL_TREE
)
11661 /* Attributes should be parsed as part of the
11662 declaration, so let's un-parse them. */
11663 saved_tokens
.rollback();
11664 std_attrs
= NULL_TREE
;
11666 cp_parser_declaration_statement (parser
);
11669 case RID_TRANSACTION_ATOMIC
:
11670 case RID_TRANSACTION_RELAXED
:
11671 case RID_SYNCHRONIZED
:
11672 case RID_ATOMIC_NOEXCEPT
:
11673 case RID_ATOMIC_CANCEL
:
11674 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11675 statement
= cp_parser_transaction (parser
, token
);
11677 case RID_TRANSACTION_CANCEL
:
11678 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11679 statement
= cp_parser_transaction_cancel (parser
);
11683 /* It might be a keyword like `int' that can start a
11684 declaration-statement. */
11688 else if (token
->type
== CPP_NAME
)
11690 /* If the next token is a `:', then we are looking at a
11691 labeled-statement. */
11692 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
11693 if (token
->type
== CPP_COLON
)
11695 /* Looks like a labeled-statement with an ordinary label.
11696 Parse the label, and then use tail recursion to parse
11699 cp_parser_label_for_labeled_statement (parser
, std_attrs
);
11700 in_compound
= false;
11704 /* Anything that starts with a `{' must be a compound-statement. */
11705 else if (token
->type
== CPP_OPEN_BRACE
)
11707 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11708 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
11710 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
11711 a statement all its own. */
11712 else if (token
->type
== CPP_PRAGMA
)
11714 /* Only certain OpenMP pragmas are attached to statements, and thus
11715 are considered statements themselves. All others are not. In
11716 the context of a compound, accept the pragma as a "statement" and
11717 return so that we can check for a close brace. Otherwise we
11718 require a real statement and must go back and read one. */
11720 cp_parser_pragma (parser
, pragma_compound
, if_p
);
11721 else if (!cp_parser_pragma (parser
, pragma_stmt
, if_p
))
11725 else if (token
->type
== CPP_EOF
)
11727 cp_parser_error (parser
, "expected statement");
11731 /* Everything else must be a declaration-statement or an
11732 expression-statement. Try for the declaration-statement
11733 first, unless we are looking at a `;', in which case we know that
11734 we have an expression-statement. */
11737 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11739 if (std_attrs
!= NULL_TREE
)
11740 /* Attributes should be parsed as part of the declaration,
11741 so let's un-parse them. */
11742 saved_tokens
.rollback();
11744 cp_parser_parse_tentatively (parser
);
11745 /* Try to parse the declaration-statement. */
11746 cp_parser_declaration_statement (parser
);
11747 /* If that worked, we're done. */
11748 if (cp_parser_parse_definitely (parser
))
11750 /* It didn't work, restore the post-attribute position. */
11752 cp_lexer_set_token_position (parser
->lexer
, statement_token
);
11754 /* All preceding labels have been parsed at this point. */
11755 if (loc_after_labels
!= NULL
)
11756 *loc_after_labels
= statement_location
;
11758 std_attrs
= process_stmt_hotness_attribute (std_attrs
, attrs_loc
);
11760 /* Look for an expression-statement instead. */
11761 statement
= cp_parser_expression_statement (parser
, in_statement_expr
);
11763 /* Handle [[fallthrough]];. */
11764 if (attribute_fallthrough_p (std_attrs
))
11766 /* The next token after the fallthrough attribute is ';'. */
11767 if (statement
== NULL_TREE
)
11769 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11770 statement
= build_call_expr_internal_loc (statement_location
,
11772 void_type_node
, 0);
11773 finish_expr_stmt (statement
);
11776 warning_at (statement_location
, OPT_Wattributes
,
11777 "%<fallthrough%> attribute not followed by %<;%>");
11778 std_attrs
= NULL_TREE
;
11782 /* Set the line number for the statement. */
11783 if (statement
&& STATEMENT_CODE_P (TREE_CODE (statement
)))
11784 SET_EXPR_LOCATION (statement
, statement_location
);
11786 /* Allow "[[fallthrough]];", but warn otherwise. */
11787 if (std_attrs
!= NULL_TREE
)
11788 warning_at (attrs_loc
,
11790 "attributes at the beginning of statement are ignored");
11793 /* Append ATTR to attribute list ATTRS. */
11796 attr_chainon (tree attrs
, tree attr
)
11798 if (attrs
== error_mark_node
)
11799 return error_mark_node
;
11800 if (attr
== error_mark_node
)
11801 return error_mark_node
;
11802 return chainon (attrs
, attr
);
11805 /* Parse the label for a labeled-statement, i.e.
11808 case constant-expression :
11812 case constant-expression ... constant-expression : statement
11814 When a label is parsed without errors, the label is added to the
11815 parse tree by the finish_* functions, so this function doesn't
11816 have to return the label. */
11819 cp_parser_label_for_labeled_statement (cp_parser
* parser
, tree attributes
)
11822 tree label
= NULL_TREE
;
11823 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
11825 /* The next token should be an identifier. */
11826 token
= cp_lexer_peek_token (parser
->lexer
);
11827 if (token
->type
!= CPP_NAME
11828 && token
->type
!= CPP_KEYWORD
)
11830 cp_parser_error (parser
, "expected labeled-statement");
11834 /* Remember whether this case or a user-defined label is allowed to fall
11836 bool fallthrough_p
= token
->flags
& PREV_FALLTHROUGH
;
11838 parser
->colon_corrects_to_scope_p
= false;
11839 switch (token
->keyword
)
11843 tree expr
, expr_hi
;
11844 cp_token
*ellipsis
;
11846 /* Consume the `case' token. */
11847 cp_lexer_consume_token (parser
->lexer
);
11848 /* Parse the constant-expression. */
11849 expr
= cp_parser_constant_expression (parser
);
11850 if (check_for_bare_parameter_packs (expr
))
11851 expr
= error_mark_node
;
11853 ellipsis
= cp_lexer_peek_token (parser
->lexer
);
11854 if (ellipsis
->type
== CPP_ELLIPSIS
)
11856 /* Consume the `...' token. */
11857 cp_lexer_consume_token (parser
->lexer
);
11858 expr_hi
= cp_parser_constant_expression (parser
);
11859 if (check_for_bare_parameter_packs (expr_hi
))
11860 expr_hi
= error_mark_node
;
11862 /* We don't need to emit warnings here, as the common code
11863 will do this for us. */
11866 expr_hi
= NULL_TREE
;
11868 if (parser
->in_switch_statement_p
)
11870 tree l
= finish_case_label (token
->location
, expr
, expr_hi
);
11871 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11873 label
= CASE_LABEL (l
);
11874 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11878 error_at (token
->location
,
11879 "case label %qE not within a switch statement",
11885 /* Consume the `default' token. */
11886 cp_lexer_consume_token (parser
->lexer
);
11888 if (parser
->in_switch_statement_p
)
11890 tree l
= finish_case_label (token
->location
, NULL_TREE
, NULL_TREE
);
11891 if (l
&& TREE_CODE (l
) == CASE_LABEL_EXPR
)
11893 label
= CASE_LABEL (l
);
11894 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11898 error_at (token
->location
, "case label not within a switch statement");
11902 /* Anything else must be an ordinary label. */
11903 label
= finish_label_stmt (cp_parser_identifier (parser
));
11904 if (label
&& TREE_CODE (label
) == LABEL_DECL
)
11905 FALLTHROUGH_LABEL_P (label
) = fallthrough_p
;
11909 /* Require the `:' token. */
11910 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
11912 /* An ordinary label may optionally be followed by attributes.
11913 However, this is only permitted if the attributes are then
11914 followed by a semicolon. This is because, for backward
11915 compatibility, when parsing
11916 lab: __attribute__ ((unused)) int i;
11917 we want the attribute to attach to "i", not "lab". */
11918 if (label
!= NULL_TREE
11919 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
11922 cp_parser_parse_tentatively (parser
);
11923 attrs
= cp_parser_gnu_attributes_opt (parser
);
11924 if (attrs
== NULL_TREE
11925 /* And fallthrough always binds to the expression-statement. */
11926 || attribute_fallthrough_p (attrs
)
11927 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11928 cp_parser_abort_tentative_parse (parser
);
11929 else if (!cp_parser_parse_definitely (parser
))
11932 attributes
= attr_chainon (attributes
, attrs
);
11935 if (attributes
!= NULL_TREE
)
11936 cplus_decl_attributes (&label
, attributes
, 0);
11938 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
11941 /* Parse an expression-statement.
11943 expression-statement:
11946 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11947 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11948 indicates whether this expression-statement is part of an
11949 expression statement. */
11952 cp_parser_expression_statement (cp_parser
* parser
, tree in_statement_expr
)
11954 tree statement
= NULL_TREE
;
11955 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
11956 location_t loc
= token
->location
;
11958 /* There might be attribute fallthrough. */
11959 tree attr
= cp_parser_gnu_attributes_opt (parser
);
11961 /* If the next token is a ';', then there is no expression
11963 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
11965 statement
= cp_parser_expression (parser
);
11966 if (statement
== error_mark_node
11967 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11969 cp_parser_skip_to_end_of_block_or_statement (parser
);
11970 return error_mark_node
;
11974 /* Handle [[fallthrough]];. */
11975 if (attribute_fallthrough_p (attr
))
11977 /* The next token after the fallthrough attribute is ';'. */
11978 if (statement
== NULL_TREE
)
11979 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11980 statement
= build_call_expr_internal_loc (loc
, IFN_FALLTHROUGH
,
11981 void_type_node
, 0);
11983 warning_at (loc
, OPT_Wattributes
,
11984 "%<fallthrough%> attribute not followed by %<;%>");
11988 /* Allow "[[fallthrough]];", but warn otherwise. */
11989 if (attr
!= NULL_TREE
)
11990 warning_at (loc
, OPT_Wattributes
,
11991 "attributes at the beginning of statement are ignored");
11993 /* Give a helpful message for "A<T>::type t;" and the like. */
11994 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
11995 && !cp_parser_uncommitted_to_tentative_parse_p (parser
))
11997 if (TREE_CODE (statement
) == SCOPE_REF
)
11998 error_at (token
->location
, "need %<typename%> before %qE because "
11999 "%qT is a dependent scope",
12000 statement
, TREE_OPERAND (statement
, 0));
12001 else if (is_overloaded_fn (statement
)
12002 && DECL_CONSTRUCTOR_P (get_first_fn (statement
)))
12005 tree fn
= get_first_fn (statement
);
12006 error_at (token
->location
,
12007 "%<%T::%D%> names the constructor, not the type",
12008 DECL_CONTEXT (fn
), DECL_NAME (fn
));
12012 /* Consume the final `;'. */
12013 cp_parser_consume_semicolon_at_end_of_statement (parser
);
12015 if (in_statement_expr
12016 && cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
12017 /* This is the final expression statement of a statement
12019 statement
= finish_stmt_expr_expr (statement
, in_statement_expr
);
12020 else if (statement
)
12021 statement
= finish_expr_stmt (statement
);
12026 /* Parse a compound-statement.
12028 compound-statement:
12029 { statement-seq [opt] }
12033 compound-statement:
12034 { label-declaration-seq [opt] statement-seq [opt] }
12036 label-declaration-seq:
12038 label-declaration-seq label-declaration
12040 Returns a tree representing the statement. */
12043 cp_parser_compound_statement (cp_parser
*parser
, tree in_statement_expr
,
12044 int bcs_flags
, bool function_body
)
12046 tree compound_stmt
;
12047 matching_braces braces
;
12049 /* Consume the `{'. */
12050 if (!braces
.require_open (parser
))
12051 return error_mark_node
;
12052 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
12053 && !function_body
&& cxx_dialect
< cxx14
)
12054 pedwarn (input_location
, OPT_Wpedantic
,
12055 "compound-statement in %<constexpr%> function");
12056 /* Begin the compound-statement. */
12057 compound_stmt
= begin_compound_stmt (bcs_flags
);
12058 /* If the next keyword is `__label__' we have a label declaration. */
12059 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
12060 cp_parser_label_declaration (parser
);
12061 /* Parse an (optional) statement-seq. */
12062 cp_parser_statement_seq_opt (parser
, in_statement_expr
);
12065 maybe_splice_retval_cleanup (compound_stmt
);
12067 /* Finish the compound-statement. */
12068 finish_compound_stmt (compound_stmt
);
12069 /* Consume the `}'. */
12070 braces
.require_close (parser
);
12072 return compound_stmt
;
12075 /* Parse an (optional) statement-seq.
12079 statement-seq [opt] statement */
12082 cp_parser_statement_seq_opt (cp_parser
* parser
, tree in_statement_expr
)
12084 /* Scan statements until there aren't any more. */
12087 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
12089 /* If we are looking at a `}', then we have run out of
12090 statements; the same is true if we have reached the end
12091 of file, or have stumbled upon a stray '@end'. */
12092 if (token
->type
== CPP_CLOSE_BRACE
12093 || token
->type
== CPP_EOF
12094 || token
->type
== CPP_PRAGMA_EOL
12095 || (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_AT_END
))
12098 /* If we are in a compound statement and find 'else' then
12099 something went wrong. */
12100 else if (token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ELSE
)
12102 if (parser
->in_statement
& IN_IF_STMT
)
12106 token
= cp_lexer_consume_token (parser
->lexer
);
12107 error_at (token
->location
, "%<else%> without a previous %<if%>");
12111 /* Parse the statement. */
12112 cp_parser_statement (parser
, in_statement_expr
, true, NULL
);
12116 /* Return true if this is the C++20 version of range-based-for with
12120 cp_parser_range_based_for_with_init_p (cp_parser
*parser
)
12124 /* Save tokens so that we can put them back. */
12125 cp_lexer_save_tokens (parser
->lexer
);
12127 /* There has to be an unnested ; followed by an unnested :. */
12128 if (cp_parser_skip_to_closing_parenthesis_1 (parser
,
12129 /*recovering=*/false,
12131 /*consume_paren=*/false) != -1)
12134 /* We found the semicolon, eat it now. */
12135 cp_lexer_consume_token (parser
->lexer
);
12137 /* Now look for ':' that is not nested in () or {}. */
12138 r
= (cp_parser_skip_to_closing_parenthesis_1 (parser
,
12139 /*recovering=*/false,
12141 /*consume_paren=*/false) == -1);
12144 /* Roll back the tokens we skipped. */
12145 cp_lexer_rollback_tokens (parser
->lexer
);
12150 /* Return true if we're looking at (init; cond), false otherwise. */
12153 cp_parser_init_statement_p (cp_parser
*parser
)
12155 /* Save tokens so that we can put them back. */
12156 cp_lexer_save_tokens (parser
->lexer
);
12158 /* Look for ';' that is not nested in () or {}. */
12159 int ret
= cp_parser_skip_to_closing_parenthesis_1 (parser
,
12160 /*recovering=*/false,
12162 /*consume_paren=*/false);
12164 /* Roll back the tokens we skipped. */
12165 cp_lexer_rollback_tokens (parser
->lexer
);
12170 /* Parse a selection-statement.
12172 selection-statement:
12173 if ( init-statement [opt] condition ) statement
12174 if ( init-statement [opt] condition ) statement else statement
12175 switch ( init-statement [opt] condition ) statement
12177 Returns the new IF_STMT or SWITCH_STMT.
12179 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12180 is a (possibly labeled) if statement which is not enclosed in
12181 braces and has an else clause. This is used to implement
12184 CHAIN is a vector of if-else-if conditions. This is used to implement
12185 -Wduplicated-cond. */
12188 cp_parser_selection_statement (cp_parser
* parser
, bool *if_p
,
12193 token_indent_info guard_tinfo
;
12198 /* Peek at the next token. */
12199 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_SELECT
);
12200 guard_tinfo
= get_token_indent_info (token
);
12202 /* See what kind of keyword it is. */
12203 keyword
= token
->keyword
;
12213 if (keyword
== RID_IF
12214 && cp_lexer_next_token_is_keyword (parser
->lexer
,
12218 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
12219 if (cxx_dialect
< cxx17
)
12220 pedwarn (tok
->location
, 0, "%<if constexpr%> only available "
12221 "with %<-std=c++17%> or %<-std=gnu++17%>");
12224 /* Look for the `('. */
12225 matching_parens parens
;
12226 if (!parens
.require_open (parser
))
12228 cp_parser_skip_to_end_of_statement (parser
);
12229 return error_mark_node
;
12232 /* Begin the selection-statement. */
12233 if (keyword
== RID_IF
)
12235 statement
= begin_if_stmt ();
12236 IF_STMT_CONSTEXPR_P (statement
) = cx
;
12239 statement
= begin_switch_stmt ();
12241 /* Parse the optional init-statement. */
12242 if (cp_parser_init_statement_p (parser
))
12245 if (cxx_dialect
< cxx17
)
12246 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
12247 "init-statement in selection statements only available "
12248 "with %<-std=c++17%> or %<-std=gnu++17%>");
12249 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12251 /* A non-empty init-statement can have arbitrary side
12256 cp_parser_init_statement (parser
, &decl
);
12259 /* Parse the condition. */
12260 condition
= cp_parser_condition (parser
);
12261 /* Look for the `)'. */
12262 if (!parens
.require_close (parser
))
12263 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
12264 /*consume_paren=*/true);
12266 if (keyword
== RID_IF
)
12269 unsigned char in_statement
;
12271 /* Add the condition. */
12272 condition
= finish_if_stmt_cond (condition
, statement
);
12274 if (warn_duplicated_cond
)
12275 warn_duplicated_cond_add_or_warn (token
->location
, condition
,
12278 /* Parse the then-clause. */
12279 in_statement
= parser
->in_statement
;
12280 parser
->in_statement
|= IN_IF_STMT
;
12282 /* Outside a template, the non-selected branch of a constexpr
12283 if is a 'discarded statement', i.e. unevaluated. */
12284 bool was_discarded
= in_discarded_stmt
;
12285 bool discard_then
= (cx
&& !processing_template_decl
12286 && integer_zerop (condition
));
12289 in_discarded_stmt
= true;
12290 ++c_inhibit_evaluation_warnings
;
12293 cp_parser_implicitly_scoped_statement (parser
, &nested_if
,
12296 parser
->in_statement
= in_statement
;
12298 finish_then_clause (statement
);
12302 THEN_CLAUSE (statement
) = NULL_TREE
;
12303 in_discarded_stmt
= was_discarded
;
12304 --c_inhibit_evaluation_warnings
;
12307 /* If the next token is `else', parse the else-clause. */
12308 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12311 bool discard_else
= (cx
&& !processing_template_decl
12312 && integer_nonzerop (condition
));
12315 in_discarded_stmt
= true;
12316 ++c_inhibit_evaluation_warnings
;
12320 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
12321 /* Consume the `else' keyword. */
12322 cp_lexer_consume_token (parser
->lexer
);
12323 if (warn_duplicated_cond
)
12325 if (cp_lexer_next_token_is_keyword (parser
->lexer
,
12329 /* We've got "if (COND) else if (COND2)". Start
12330 the condition chain and add COND as the first
12332 chain
= new vec
<tree
> ();
12333 if (!CONSTANT_CLASS_P (condition
)
12334 && !TREE_SIDE_EFFECTS (condition
))
12336 /* Wrap it in a NOP_EXPR so that we can set the
12337 location of the condition. */
12338 tree e
= build1 (NOP_EXPR
, TREE_TYPE (condition
),
12340 SET_EXPR_LOCATION (e
, token
->location
);
12341 chain
->safe_push (e
);
12344 else if (!cp_lexer_next_token_is_keyword (parser
->lexer
,
12347 /* This is if-else without subsequent if. Zap the
12348 condition chain; we would have already warned at
12354 begin_else_clause (statement
);
12355 /* Parse the else-clause. */
12356 cp_parser_implicitly_scoped_statement (parser
, NULL
,
12357 guard_tinfo
, chain
);
12359 finish_else_clause (statement
);
12361 /* If we are currently parsing a then-clause, then
12362 IF_P will not be NULL. We set it to true to
12363 indicate that this if statement has an else clause.
12364 This may trigger the Wparentheses warning below
12365 when we get back up to the parent if statement. */
12371 ELSE_CLAUSE (statement
) = NULL_TREE
;
12372 in_discarded_stmt
= was_discarded
;
12373 --c_inhibit_evaluation_warnings
;
12378 /* This if statement does not have an else clause. If
12379 NESTED_IF is true, then the then-clause has an if
12380 statement which does have an else clause. We warn
12381 about the potential ambiguity. */
12383 warning_at (EXPR_LOCATION (statement
), OPT_Wdangling_else
,
12384 "suggest explicit braces to avoid ambiguous"
12386 if (warn_duplicated_cond
)
12388 /* We don't need the condition chain anymore. */
12394 /* Now we're all done with the if-statement. */
12395 finish_if_stmt (statement
);
12399 bool in_switch_statement_p
;
12400 unsigned char in_statement
;
12402 /* Add the condition. */
12403 finish_switch_cond (condition
, statement
);
12405 /* Parse the body of the switch-statement. */
12406 in_switch_statement_p
= parser
->in_switch_statement_p
;
12407 in_statement
= parser
->in_statement
;
12408 parser
->in_switch_statement_p
= true;
12409 parser
->in_statement
|= IN_SWITCH_STMT
;
12410 cp_parser_implicitly_scoped_statement (parser
, if_p
,
12412 parser
->in_switch_statement_p
= in_switch_statement_p
;
12413 parser
->in_statement
= in_statement
;
12415 /* Now we're all done with the switch-statement. */
12416 finish_switch_stmt (statement
);
12424 cp_parser_error (parser
, "expected selection-statement");
12425 return error_mark_node
;
12429 /* Helper function for cp_parser_condition and cp_parser_simple_declaration.
12430 If we have seen at least one decl-specifier, and the next token is not
12431 a parenthesis (after "int (" we might be looking at a functional cast)
12432 neither we are dealing with a concept-check expression then we must be
12433 looking at a declaration. */
12436 cp_parser_maybe_commit_to_declaration (cp_parser
* parser
,
12437 cp_decl_specifier_seq
*decl_specs
)
12439 if (decl_specs
->any_specifiers_p
12440 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)
12441 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
12442 && !cp_parser_error_occurred (parser
)
12443 && !(decl_specs
->type
12444 && TREE_CODE (decl_specs
->type
) == TYPE_DECL
12445 && is_constrained_auto (TREE_TYPE (decl_specs
->type
))))
12446 cp_parser_commit_to_tentative_parse (parser
);
12449 /* Helper function for cp_parser_condition. Enforces [stmt.stmt]/2:
12450 The declarator shall not specify a function or an array. Returns
12451 TRUE if the declarator is valid, FALSE otherwise. */
12454 cp_parser_check_condition_declarator (cp_parser
* parser
,
12455 cp_declarator
*declarator
,
12458 if (declarator
== cp_error_declarator
12459 || function_declarator_p (declarator
)
12460 || declarator
->kind
== cdk_array
)
12462 if (declarator
== cp_error_declarator
)
12463 /* Already complained. */;
12464 else if (declarator
->kind
== cdk_array
)
12465 error_at (loc
, "condition declares an array");
12467 error_at (loc
, "condition declares a function");
12468 if (parser
->fully_implicit_function_template_p
)
12469 abort_fully_implicit_template (parser
);
12470 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
12471 /*or_comma=*/false,
12472 /*consume_paren=*/false);
12479 /* Parse a condition.
12483 type-specifier-seq declarator = initializer-clause
12484 type-specifier-seq declarator braced-init-list
12489 type-specifier-seq declarator asm-specification [opt]
12490 attributes [opt] = assignment-expression
12492 Returns the expression that should be tested. */
12495 cp_parser_condition (cp_parser
* parser
)
12497 cp_decl_specifier_seq type_specifiers
;
12498 const char *saved_message
;
12499 int declares_class_or_enum
;
12501 /* Try the declaration first. */
12502 cp_parser_parse_tentatively (parser
);
12503 /* New types are not allowed in the type-specifier-seq for a
12505 saved_message
= parser
->type_definition_forbidden_message
;
12506 parser
->type_definition_forbidden_message
12507 = G_("types may not be defined in conditions");
12508 /* Parse the type-specifier-seq. */
12509 cp_parser_decl_specifier_seq (parser
,
12510 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
,
12512 &declares_class_or_enum
);
12513 /* Restore the saved message. */
12514 parser
->type_definition_forbidden_message
= saved_message
;
12516 /* Gather the attributes that were provided with the
12517 decl-specifiers. */
12518 tree prefix_attributes
= type_specifiers
.attributes
;
12520 cp_parser_maybe_commit_to_declaration (parser
, &type_specifiers
);
12522 /* If all is well, we might be looking at a declaration. */
12523 if (!cp_parser_error_occurred (parser
))
12526 tree asm_specification
;
12528 cp_declarator
*declarator
;
12529 tree initializer
= NULL_TREE
;
12530 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
12532 /* Parse the declarator. */
12533 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
12534 CP_PARSER_FLAGS_NONE
,
12535 /*ctor_dtor_or_conv_p=*/NULL
,
12536 /*parenthesized_p=*/NULL
,
12537 /*member_p=*/false,
12538 /*friend_p=*/false,
12539 /*static_p=*/false);
12540 /* Parse the attributes. */
12541 attributes
= cp_parser_attributes_opt (parser
);
12542 /* Parse the asm-specification. */
12543 asm_specification
= cp_parser_asm_specification_opt (parser
);
12544 /* If the next token is not an `=' or '{', then we might still be
12545 looking at an expression. For example:
12549 looks like a decl-specifier-seq and a declarator -- but then
12550 there is no `=', so this is an expression. */
12551 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
12552 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
12553 cp_parser_simulate_error (parser
);
12555 /* If we did see an `=' or '{', then we are looking at a declaration
12557 if (cp_parser_parse_definitely (parser
))
12560 bool non_constant_p
= false;
12561 int flags
= LOOKUP_ONLYCONVERTING
;
12563 if (!cp_parser_check_condition_declarator (parser
, declarator
, loc
))
12564 return error_mark_node
;
12566 /* Create the declaration. */
12567 decl
= start_decl (declarator
, &type_specifiers
,
12568 /*initialized_p=*/true,
12569 attributes
, prefix_attributes
,
12572 /* Parse the initializer. */
12573 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12575 initializer
= cp_parser_braced_list (parser
, &non_constant_p
);
12576 CONSTRUCTOR_IS_DIRECT_INIT (initializer
) = 1;
12579 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
12581 /* Consume the `='. */
12582 cp_lexer_consume_token (parser
->lexer
);
12583 initializer
= cp_parser_initializer_clause (parser
,
12588 cp_parser_error (parser
, "expected initializer");
12589 initializer
= error_mark_node
;
12591 if (BRACE_ENCLOSED_INITIALIZER_P (initializer
))
12592 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
12594 /* Process the initializer. */
12595 cp_finish_decl (decl
,
12596 initializer
, !non_constant_p
,
12601 pop_scope (pushed_scope
);
12603 return convert_from_reference (decl
);
12606 /* If we didn't even get past the declarator successfully, we are
12607 definitely not looking at a declaration. */
12609 cp_parser_abort_tentative_parse (parser
);
12611 /* Otherwise, we are looking at an expression. */
12612 return cp_parser_expression (parser
);
12615 /* Parses a for-statement or range-for-statement until the closing ')',
12619 cp_parser_for (cp_parser
*parser
, bool ivdep
, unsigned short unroll
)
12621 tree init
, scope
, decl
;
12624 /* Begin the for-statement. */
12625 scope
= begin_for_scope (&init
);
12627 /* Parse the initialization. */
12628 is_range_for
= cp_parser_init_statement (parser
, &decl
);
12631 return cp_parser_range_for (parser
, scope
, init
, decl
, ivdep
, unroll
,
12634 return cp_parser_c_for (parser
, scope
, init
, ivdep
, unroll
);
12638 cp_parser_c_for (cp_parser
*parser
, tree scope
, tree init
, bool ivdep
,
12639 unsigned short unroll
)
12641 /* Normal for loop */
12642 tree condition
= NULL_TREE
;
12643 tree expression
= NULL_TREE
;
12646 stmt
= begin_for_stmt (scope
, init
);
12647 /* The init-statement has already been parsed in
12648 cp_parser_init_statement, so no work is needed here. */
12649 finish_init_stmt (stmt
);
12651 /* If there's a condition, process it. */
12652 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
12653 condition
= cp_parser_condition (parser
);
12656 cp_parser_error (parser
, "missing loop condition in loop with "
12657 "%<GCC ivdep%> pragma");
12658 condition
= error_mark_node
;
12662 cp_parser_error (parser
, "missing loop condition in loop with "
12663 "%<GCC unroll%> pragma");
12664 condition
= error_mark_node
;
12666 finish_for_cond (condition
, stmt
, ivdep
, unroll
);
12667 /* Look for the `;'. */
12668 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
12670 /* If there's an expression, process it. */
12671 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
12672 expression
= cp_parser_expression (parser
);
12673 finish_for_expr (expression
, stmt
);
12678 /* Tries to parse a range-based for-statement:
12681 decl-specifier-seq declarator : expression
12683 The decl-specifier-seq declarator and the `:' are already parsed by
12684 cp_parser_init_statement. If processing_template_decl it returns a
12685 newly created RANGE_FOR_STMT; if not, it is converted to a
12686 regular FOR_STMT. */
12689 cp_parser_range_for (cp_parser
*parser
, tree scope
, tree init
, tree range_decl
,
12690 bool ivdep
, unsigned short unroll
, bool is_omp
)
12692 tree stmt
, range_expr
;
12693 auto_vec
<cxx_binding
*, 16> bindings
;
12694 auto_vec
<tree
, 16> names
;
12695 tree decomp_first_name
= NULL_TREE
;
12696 unsigned int decomp_cnt
= 0;
12698 /* Get the range declaration momentarily out of the way so that
12699 the range expression doesn't clash with it. */
12700 if (range_decl
!= error_mark_node
)
12702 if (DECL_HAS_VALUE_EXPR_P (range_decl
))
12704 tree v
= DECL_VALUE_EXPR (range_decl
);
12705 /* For decomposition declaration get all of the corresponding
12706 declarations out of the way. */
12707 if (TREE_CODE (v
) == ARRAY_REF
12708 && VAR_P (TREE_OPERAND (v
, 0))
12709 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
12711 tree d
= range_decl
;
12712 range_decl
= TREE_OPERAND (v
, 0);
12713 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
12714 decomp_first_name
= d
;
12715 for (unsigned int i
= 0; i
< decomp_cnt
; i
++, d
= DECL_CHAIN (d
))
12717 tree name
= DECL_NAME (d
);
12718 names
.safe_push (name
);
12719 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12720 IDENTIFIER_BINDING (name
)
12721 = IDENTIFIER_BINDING (name
)->previous
;
12725 if (names
.is_empty ())
12727 tree name
= DECL_NAME (range_decl
);
12728 names
.safe_push (name
);
12729 bindings
.safe_push (IDENTIFIER_BINDING (name
));
12730 IDENTIFIER_BINDING (name
) = IDENTIFIER_BINDING (name
)->previous
;
12734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
12736 bool expr_non_constant_p
;
12737 range_expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
12740 range_expr
= cp_parser_expression (parser
);
12742 /* Put the range declaration(s) back into scope. */
12743 for (unsigned int i
= 0; i
< names
.length (); i
++)
12745 cxx_binding
*binding
= bindings
[i
];
12746 binding
->previous
= IDENTIFIER_BINDING (names
[i
]);
12747 IDENTIFIER_BINDING (names
[i
]) = binding
;
12750 /* finish_omp_for has its own code for the following, so just
12751 return the range_expr instead. */
12755 /* If in template, STMT is converted to a normal for-statement
12756 at instantiation. If not, it is done just ahead. */
12757 if (processing_template_decl
)
12759 if (check_for_bare_parameter_packs (range_expr
))
12760 range_expr
= error_mark_node
;
12761 stmt
= begin_range_for_stmt (scope
, init
);
12763 RANGE_FOR_IVDEP (stmt
) = 1;
12765 RANGE_FOR_UNROLL (stmt
) = build_int_cst (integer_type_node
, unroll
);
12766 finish_range_for_decl (stmt
, range_decl
, range_expr
);
12767 if (!type_dependent_expression_p (range_expr
)
12768 /* do_auto_deduction doesn't mess with template init-lists. */
12769 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr
))
12770 do_range_for_auto_deduction (range_decl
, range_expr
);
12774 stmt
= begin_for_stmt (scope
, init
);
12775 stmt
= cp_convert_range_for (stmt
, range_decl
, range_expr
,
12776 decomp_first_name
, decomp_cnt
, ivdep
,
12782 /* Subroutine of cp_convert_range_for: given the initializer expression,
12783 builds up the range temporary. */
12786 build_range_temp (tree range_expr
)
12788 tree range_type
, range_temp
;
12790 /* Find out the type deduced by the declaration
12791 `auto &&__range = range_expr'. */
12792 range_type
= cp_build_reference_type (make_auto (), true);
12793 range_type
= do_auto_deduction (range_type
, range_expr
,
12794 type_uses_auto (range_type
));
12796 /* Create the __range variable. */
12797 range_temp
= build_decl (input_location
, VAR_DECL
, for_range__identifier
,
12799 TREE_USED (range_temp
) = 1;
12800 DECL_ARTIFICIAL (range_temp
) = 1;
12805 /* Used by cp_parser_range_for in template context: we aren't going to
12806 do a full conversion yet, but we still need to resolve auto in the
12807 type of the for-range-declaration if present. This is basically
12808 a shortcut version of cp_convert_range_for. */
12811 do_range_for_auto_deduction (tree decl
, tree range_expr
)
12813 tree auto_node
= type_uses_auto (TREE_TYPE (decl
));
12816 tree begin_dummy
, end_dummy
, range_temp
, iter_type
, iter_decl
;
12817 range_temp
= convert_from_reference (build_range_temp (range_expr
));
12818 iter_type
= (cp_parser_perform_range_for_lookup
12819 (range_temp
, &begin_dummy
, &end_dummy
));
12822 iter_decl
= build_decl (input_location
, VAR_DECL
, NULL_TREE
,
12824 iter_decl
= build_x_indirect_ref (input_location
, iter_decl
,
12826 tf_warning_or_error
);
12827 TREE_TYPE (decl
) = do_auto_deduction (TREE_TYPE (decl
),
12828 iter_decl
, auto_node
);
12833 /* Warns when the loop variable should be changed to a reference type to
12834 avoid unnecessary copying. I.e., from
12836 for (const auto x : range)
12838 where range returns a reference, to
12840 for (const auto &x : range)
12842 if this version doesn't make a copy.
12844 This function also warns when the loop variable is initialized with
12845 a value of a different type resulting in a copy:
12848 for (const double &x : arr)
12850 DECL is the RANGE_DECL; EXPR is the *__for_begin expression.
12851 This function is never called when processing_template_decl is on. */
12854 warn_for_range_copy (tree decl
, tree expr
)
12856 if (!warn_range_loop_construct
12857 || decl
== error_mark_node
)
12860 location_t loc
= DECL_SOURCE_LOCATION (decl
);
12861 tree type
= TREE_TYPE (decl
);
12863 if (from_macro_expansion_at (loc
))
12866 if (TYPE_REF_P (type
))
12868 if (glvalue_p (expr
) && !ref_conv_binds_directly_p (type
, expr
))
12870 auto_diagnostic_group d
;
12871 if (warning_at (loc
, OPT_Wrange_loop_construct
,
12872 "loop variable %qD of type %qT binds to a temporary "
12873 "constructed from type %qT", decl
, type
,
12876 tree ref
= cp_build_qualified_type (TREE_TYPE (expr
),
12878 ref
= cp_build_reference_type (ref
, /*rval*/false);
12879 inform (loc
, "use non-reference type %qT to make the copy "
12880 "explicit or %qT to prevent copying",
12881 non_reference (type
), ref
);
12886 else if (!CP_TYPE_CONST_P (type
))
12889 /* Since small trivially copyable types are cheap to copy, we suppress the
12890 warning for them. 64B is a common size of a cache line. */
12891 if (TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
12892 || (tree_to_uhwi (TYPE_SIZE_UNIT (type
)) <= 64
12893 && trivially_copyable_p (type
)))
12896 tree rtype
= cp_build_reference_type (type
, /*rval*/false);
12897 /* If we could initialize the reference directly, it wouldn't involve any
12899 if (!ref_conv_binds_directly_p (rtype
, expr
))
12902 auto_diagnostic_group d
;
12903 if (warning_at (loc
, OPT_Wrange_loop_construct
,
12904 "loop variable %qD creates a copy from type %qT",
12907 gcc_rich_location
richloc (loc
);
12908 richloc
.add_fixit_insert_before ("&");
12909 inform (&richloc
, "use reference type to prevent copying");
12913 /* Converts a range-based for-statement into a normal
12914 for-statement, as per the definition.
12916 for (RANGE_DECL : RANGE_EXPR)
12919 should be equivalent to:
12922 auto &&__range = RANGE_EXPR;
12923 for (auto __begin = BEGIN_EXPR, __end = END_EXPR;
12927 RANGE_DECL = *__begin;
12932 If RANGE_EXPR is an array:
12933 BEGIN_EXPR = __range
12934 END_EXPR = __range + ARRAY_SIZE(__range)
12935 Else if RANGE_EXPR has a member 'begin' or 'end':
12936 BEGIN_EXPR = __range.begin()
12937 END_EXPR = __range.end()
12939 BEGIN_EXPR = begin(__range)
12940 END_EXPR = end(__range);
12942 If __range has a member 'begin' but not 'end', or vice versa, we must
12943 still use the second alternative (it will surely fail, however).
12944 When calling begin()/end() in the third alternative we must use
12945 argument dependent lookup, but always considering 'std' as an associated
12949 cp_convert_range_for (tree statement
, tree range_decl
, tree range_expr
,
12950 tree decomp_first_name
, unsigned int decomp_cnt
,
12951 bool ivdep
, unsigned short unroll
)
12954 tree iter_type
, begin_expr
, end_expr
;
12955 tree condition
, expression
;
12957 range_expr
= mark_lvalue_use (range_expr
);
12959 if (range_decl
== error_mark_node
|| range_expr
== error_mark_node
)
12960 /* If an error happened previously do nothing or else a lot of
12961 unhelpful errors would be issued. */
12962 begin_expr
= end_expr
= iter_type
= error_mark_node
;
12967 if (VAR_P (range_expr
)
12968 && array_of_runtime_bound_p (TREE_TYPE (range_expr
)))
12969 /* Can't bind a reference to an array of runtime bound. */
12970 range_temp
= range_expr
;
12973 range_temp
= build_range_temp (range_expr
);
12974 pushdecl (range_temp
);
12975 cp_finish_decl (range_temp
, range_expr
,
12976 /*is_constant_init*/false, NULL_TREE
,
12977 LOOKUP_ONLYCONVERTING
);
12978 range_temp
= convert_from_reference (range_temp
);
12980 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
12981 &begin_expr
, &end_expr
);
12984 /* The new for initialization statement. */
12985 begin
= build_decl (input_location
, VAR_DECL
, for_begin__identifier
,
12987 TREE_USED (begin
) = 1;
12988 DECL_ARTIFICIAL (begin
) = 1;
12990 cp_finish_decl (begin
, begin_expr
,
12991 /*is_constant_init*/false, NULL_TREE
,
12992 LOOKUP_ONLYCONVERTING
);
12994 if (cxx_dialect
>= cxx17
)
12995 iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
12996 end
= build_decl (input_location
, VAR_DECL
, for_end__identifier
, iter_type
);
12997 TREE_USED (end
) = 1;
12998 DECL_ARTIFICIAL (end
) = 1;
13000 cp_finish_decl (end
, end_expr
,
13001 /*is_constant_init*/false, NULL_TREE
,
13002 LOOKUP_ONLYCONVERTING
);
13004 finish_init_stmt (statement
);
13006 /* The new for condition. */
13007 condition
= build_x_binary_op (input_location
, NE_EXPR
,
13010 NULL
, tf_warning_or_error
);
13011 finish_for_cond (condition
, statement
, ivdep
, unroll
);
13013 /* The new increment expression. */
13014 expression
= finish_unary_op_expr (input_location
,
13015 PREINCREMENT_EXPR
, begin
,
13016 tf_warning_or_error
);
13017 finish_for_expr (expression
, statement
);
13019 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
13020 cp_maybe_mangle_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
13022 /* The declaration is initialized with *__begin inside the loop body. */
13023 tree deref_begin
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
13024 tf_warning_or_error
);
13025 cp_finish_decl (range_decl
, deref_begin
,
13026 /*is_constant_init*/false, NULL_TREE
,
13027 LOOKUP_ONLYCONVERTING
);
13028 if (VAR_P (range_decl
) && DECL_DECOMPOSITION_P (range_decl
))
13029 cp_finish_decomp (range_decl
, decomp_first_name
, decomp_cnt
);
13031 warn_for_range_copy (range_decl
, deref_begin
);
13036 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
13037 We need to solve both at the same time because the method used
13038 depends on the existence of members begin or end.
13039 Returns the type deduced for the iterator expression. */
13042 cp_parser_perform_range_for_lookup (tree range
, tree
*begin
, tree
*end
)
13044 if (error_operand_p (range
))
13046 *begin
= *end
= error_mark_node
;
13047 return error_mark_node
;
13050 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range
))))
13052 error ("range-based %<for%> expression of type %qT "
13053 "has incomplete type", TREE_TYPE (range
));
13054 *begin
= *end
= error_mark_node
;
13055 return error_mark_node
;
13057 if (TREE_CODE (TREE_TYPE (range
)) == ARRAY_TYPE
)
13059 /* If RANGE is an array, we will use pointer arithmetic. */
13060 *begin
= decay_conversion (range
, tf_warning_or_error
);
13061 *end
= build_binary_op (input_location
, PLUS_EXPR
,
13063 array_type_nelts_top (TREE_TYPE (range
)),
13065 return TREE_TYPE (*begin
);
13069 /* If it is not an array, we must do a bit of magic. */
13070 tree id_begin
, id_end
;
13071 tree member_begin
, member_end
;
13073 *begin
= *end
= error_mark_node
;
13075 id_begin
= get_identifier ("begin");
13076 id_end
= get_identifier ("end");
13077 member_begin
= lookup_member (TREE_TYPE (range
), id_begin
,
13078 /*protect=*/2, /*want_type=*/false,
13079 tf_warning_or_error
);
13080 member_end
= lookup_member (TREE_TYPE (range
), id_end
,
13081 /*protect=*/2, /*want_type=*/false,
13082 tf_warning_or_error
);
13084 if (member_begin
!= NULL_TREE
&& member_end
!= NULL_TREE
)
13086 /* Use the member functions. */
13087 *begin
= cp_parser_range_for_member_function (range
, id_begin
);
13088 *end
= cp_parser_range_for_member_function (range
, id_end
);
13092 /* Use global functions with ADL. */
13095 vec_safe_push (vec
, range
);
13097 member_begin
= perform_koenig_lookup (id_begin
, vec
,
13098 tf_warning_or_error
);
13099 *begin
= finish_call_expr (member_begin
, &vec
, false, true,
13100 tf_warning_or_error
);
13101 member_end
= perform_koenig_lookup (id_end
, vec
,
13102 tf_warning_or_error
);
13103 *end
= finish_call_expr (member_end
, &vec
, false, true,
13104 tf_warning_or_error
);
13107 /* Last common checks. */
13108 if (*begin
== error_mark_node
|| *end
== error_mark_node
)
13110 /* If one of the expressions is an error do no more checks. */
13111 *begin
= *end
= error_mark_node
;
13112 return error_mark_node
;
13114 else if (type_dependent_expression_p (*begin
)
13115 || type_dependent_expression_p (*end
))
13116 /* Can happen, when, eg, in a template context, Koenig lookup
13117 can't resolve begin/end (c++/58503). */
13121 tree iter_type
= cv_unqualified (TREE_TYPE (*begin
));
13122 /* The unqualified type of the __begin and __end temporaries should
13123 be the same, as required by the multiple auto declaration. */
13124 if (!same_type_p (iter_type
, cv_unqualified (TREE_TYPE (*end
))))
13126 if (cxx_dialect
>= cxx17
13127 && (build_x_binary_op (input_location
, NE_EXPR
,
13128 *begin
, ERROR_MARK
,
13131 != error_mark_node
))
13132 /* P0184R0 allows __begin and __end to have different types,
13133 but make sure they are comparable so we can give a better
13136 error ("inconsistent begin/end types in range-based %<for%> "
13137 "statement: %qT and %qT",
13138 TREE_TYPE (*begin
), TREE_TYPE (*end
));
13145 /* Helper function for cp_parser_perform_range_for_lookup.
13146 Builds a tree for RANGE.IDENTIFIER(). */
13149 cp_parser_range_for_member_function (tree range
, tree identifier
)
13153 member
= finish_class_member_access_expr (range
, identifier
,
13154 false, tf_warning_or_error
);
13155 if (member
== error_mark_node
)
13156 return error_mark_node
;
13159 res
= finish_call_expr (member
, &vec
,
13160 /*disallow_virtual=*/false,
13161 /*koenig_p=*/false,
13162 tf_warning_or_error
);
13166 /* Parse an iteration-statement.
13168 iteration-statement:
13169 while ( condition ) statement
13170 do statement while ( expression ) ;
13171 for ( init-statement condition [opt] ; expression [opt] )
13174 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
13177 cp_parser_iteration_statement (cp_parser
* parser
, bool *if_p
, bool ivdep
,
13178 unsigned short unroll
)
13183 unsigned char in_statement
;
13184 token_indent_info guard_tinfo
;
13186 /* Peek at the next token. */
13187 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_ITERATION
);
13189 return error_mark_node
;
13191 guard_tinfo
= get_token_indent_info (token
);
13193 /* Remember whether or not we are already within an iteration
13195 in_statement
= parser
->in_statement
;
13197 /* See what kind of keyword it is. */
13198 keyword
= token
->keyword
;
13205 /* Begin the while-statement. */
13206 statement
= begin_while_stmt ();
13207 /* Look for the `('. */
13208 matching_parens parens
;
13209 parens
.require_open (parser
);
13210 /* Parse the condition. */
13211 condition
= cp_parser_condition (parser
);
13212 finish_while_stmt_cond (condition
, statement
, ivdep
, unroll
);
13213 /* Look for the `)'. */
13214 parens
.require_close (parser
);
13215 /* Parse the dependent statement. */
13216 parser
->in_statement
= IN_ITERATION_STMT
;
13217 bool prev
= note_iteration_stmt_body_start ();
13218 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
13219 note_iteration_stmt_body_end (prev
);
13220 parser
->in_statement
= in_statement
;
13221 /* We're done with the while-statement. */
13222 finish_while_stmt (statement
);
13230 /* Begin the do-statement. */
13231 statement
= begin_do_stmt ();
13232 /* Parse the body of the do-statement. */
13233 parser
->in_statement
= IN_ITERATION_STMT
;
13234 bool prev
= note_iteration_stmt_body_start ();
13235 cp_parser_implicitly_scoped_statement (parser
, NULL
, guard_tinfo
);
13236 note_iteration_stmt_body_end (prev
);
13237 parser
->in_statement
= in_statement
;
13238 finish_do_body (statement
);
13239 /* Look for the `while' keyword. */
13240 cp_parser_require_keyword (parser
, RID_WHILE
, RT_WHILE
);
13241 /* Look for the `('. */
13242 matching_parens parens
;
13243 parens
.require_open (parser
);
13244 /* Parse the expression. */
13245 expression
= cp_parser_expression (parser
);
13246 /* We're done with the do-statement. */
13247 finish_do_stmt (expression
, statement
, ivdep
, unroll
);
13248 /* Look for the `)'. */
13249 parens
.require_close (parser
);
13250 /* Look for the `;'. */
13251 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13257 /* Look for the `('. */
13258 matching_parens parens
;
13259 parens
.require_open (parser
);
13261 statement
= cp_parser_for (parser
, ivdep
, unroll
);
13263 /* Look for the `)'. */
13264 parens
.require_close (parser
);
13266 /* Parse the body of the for-statement. */
13267 parser
->in_statement
= IN_ITERATION_STMT
;
13268 bool prev
= note_iteration_stmt_body_start ();
13269 cp_parser_already_scoped_statement (parser
, if_p
, guard_tinfo
);
13270 note_iteration_stmt_body_end (prev
);
13271 parser
->in_statement
= in_statement
;
13273 /* We're done with the for-statement. */
13274 finish_for_stmt (statement
);
13279 cp_parser_error (parser
, "expected iteration-statement");
13280 statement
= error_mark_node
;
13287 /* Parse a init-statement or the declarator of a range-based-for.
13288 Returns true if a range-based-for declaration is seen.
13291 expression-statement
13292 simple-declaration */
13295 cp_parser_init_statement (cp_parser
*parser
, tree
*decl
)
13297 /* If the next token is a `;', then we have an empty
13298 expression-statement. Grammatically, this is also a
13299 simple-declaration, but an invalid one, because it does not
13300 declare anything. Therefore, if we did not handle this case
13301 specially, we would issue an error message about an invalid
13303 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13305 bool is_range_for
= false;
13306 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
13308 /* Try to parse the init-statement. */
13309 if (cp_parser_range_based_for_with_init_p (parser
))
13312 cp_parser_parse_tentatively (parser
);
13313 /* Parse the declaration. */
13314 cp_parser_simple_declaration (parser
,
13315 /*function_definition_allowed_p=*/false,
13317 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13318 if (!cp_parser_parse_definitely (parser
))
13319 /* That didn't work, try to parse it as an expression-statement. */
13320 cp_parser_expression_statement (parser
, NULL_TREE
);
13322 if (cxx_dialect
< cxx20
)
13324 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
13325 "range-based %<for%> loops with initializer only "
13326 "available with %<-std=c++20%> or %<-std=gnu++20%>");
13327 *decl
= error_mark_node
;
13331 /* A colon is used in range-based for. */
13332 parser
->colon_corrects_to_scope_p
= false;
13334 /* We're going to speculatively look for a declaration, falling back
13335 to an expression, if necessary. */
13336 cp_parser_parse_tentatively (parser
);
13337 /* Parse the declaration. */
13338 cp_parser_simple_declaration (parser
,
13339 /*function_definition_allowed_p=*/false,
13341 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
13342 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
13344 /* It is a range-for, consume the ':'. */
13345 cp_lexer_consume_token (parser
->lexer
);
13346 is_range_for
= true;
13347 if (cxx_dialect
< cxx11
)
13348 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
, 0,
13349 "range-based %<for%> loops only available with "
13350 "%<-std=c++11%> or %<-std=gnu++11%>");
13353 /* The ';' is not consumed yet because we told
13354 cp_parser_simple_declaration not to. */
13355 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13357 if (cp_parser_parse_definitely (parser
))
13358 return is_range_for
;
13359 /* If the tentative parse failed, then we shall need to look for an
13360 expression-statement. */
13362 /* If we are here, it is an expression-statement. */
13363 cp_parser_expression_statement (parser
, NULL_TREE
);
13367 /* Parse a jump-statement.
13372 return expression [opt] ;
13373 return braced-init-list ;
13374 coroutine-return-statement;
13380 goto * expression ;
13382 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
13385 cp_parser_jump_statement (cp_parser
* parser
)
13387 tree statement
= error_mark_node
;
13390 unsigned char in_statement
;
13392 /* Peek at the next token. */
13393 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_JUMP
);
13395 return error_mark_node
;
13397 /* See what kind of keyword it is. */
13398 keyword
= token
->keyword
;
13402 in_statement
= parser
->in_statement
& ~IN_IF_STMT
;
13403 switch (in_statement
)
13406 error_at (token
->location
, "break statement not within loop or switch");
13409 gcc_assert ((in_statement
& IN_SWITCH_STMT
)
13410 || in_statement
== IN_ITERATION_STMT
);
13411 statement
= finish_break_stmt ();
13412 if (in_statement
== IN_ITERATION_STMT
)
13413 break_maybe_infinite_loop ();
13416 error_at (token
->location
, "invalid exit from OpenMP structured block");
13419 error_at (token
->location
, "break statement used with OpenMP for loop");
13422 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13426 switch (parser
->in_statement
& ~(IN_SWITCH_STMT
| IN_IF_STMT
))
13429 error_at (token
->location
, "continue statement not within a loop");
13431 /* Fall through. */
13432 case IN_ITERATION_STMT
:
13434 statement
= finish_continue_stmt ();
13437 error_at (token
->location
, "invalid exit from OpenMP structured block");
13440 gcc_unreachable ();
13442 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13445 case RID_CO_RETURN
:
13449 bool expr_non_constant_p
;
13451 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13453 cp_lexer_set_source_position (parser
->lexer
);
13454 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
13455 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
13457 else if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
13458 expr
= cp_parser_expression (parser
);
13460 /* If the next token is a `;', then there is no
13463 /* Build the return-statement, check co-return first, since type
13464 deduction is not valid there. */
13465 if (keyword
== RID_CO_RETURN
)
13466 statement
= finish_co_return_stmt (token
->location
, expr
);
13467 else if (FNDECL_USED_AUTO (current_function_decl
) && in_discarded_stmt
)
13468 /* Don't deduce from a discarded return statement. */;
13470 statement
= finish_return_stmt (expr
);
13471 /* Look for the final `;'. */
13472 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13477 if (parser
->in_function_body
13478 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
))
13480 error ("%<goto%> in %<constexpr%> function");
13481 cp_function_chain
->invalid_constexpr
= true;
13484 /* Create the goto-statement. */
13485 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
))
13487 /* Issue a warning about this use of a GNU extension. */
13488 pedwarn (token
->location
, OPT_Wpedantic
, "ISO C++ forbids computed gotos");
13489 /* Consume the '*' token. */
13490 cp_lexer_consume_token (parser
->lexer
);
13491 /* Parse the dependent expression. */
13492 finish_goto_stmt (cp_parser_expression (parser
));
13495 finish_goto_stmt (cp_parser_identifier (parser
));
13496 /* Look for the final `;'. */
13497 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
13501 cp_parser_error (parser
, "expected jump-statement");
13508 /* Parse a declaration-statement.
13510 declaration-statement:
13511 block-declaration */
13514 cp_parser_declaration_statement (cp_parser
* parser
)
13518 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13519 p
= obstack_alloc (&declarator_obstack
, 0);
13521 /* Parse the block-declaration. */
13522 cp_parser_block_declaration (parser
, /*statement_p=*/true);
13524 /* Free any declarators allocated. */
13525 obstack_free (&declarator_obstack
, p
);
13528 /* Some dependent statements (like `if (cond) statement'), are
13529 implicitly in their own scope. In other words, if the statement is
13530 a single statement (as opposed to a compound-statement), it is
13531 none-the-less treated as if it were enclosed in braces. Any
13532 declarations appearing in the dependent statement are out of scope
13533 after control passes that point. This function parses a statement,
13534 but ensures that is in its own scope, even if it is not a
13535 compound-statement.
13537 If IF_P is not NULL, *IF_P is set to indicate whether the statement
13538 is a (possibly labeled) if statement which is not enclosed in
13539 braces and has an else clause. This is used to implement
13542 CHAIN is a vector of if-else-if conditions. This is used to implement
13545 Returns the new statement. */
13548 cp_parser_implicitly_scoped_statement (cp_parser
* parser
, bool *if_p
,
13549 const token_indent_info
&guard_tinfo
,
13553 location_t body_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
13554 location_t body_loc_after_labels
= UNKNOWN_LOCATION
;
13555 token_indent_info body_tinfo
13556 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13561 /* Mark if () ; with a special NOP_EXPR. */
13562 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13564 cp_lexer_consume_token (parser
->lexer
);
13565 statement
= add_stmt (build_empty_stmt (body_loc
));
13567 if (guard_tinfo
.keyword
== RID_IF
13568 && !cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ELSE
))
13569 warning_at (body_loc
, OPT_Wempty_body
,
13570 "suggest braces around empty body in an %<if%> statement");
13571 else if (guard_tinfo
.keyword
== RID_ELSE
)
13572 warning_at (body_loc
, OPT_Wempty_body
,
13573 "suggest braces around empty body in an %<else%> statement");
13575 /* if a compound is opened, we simply parse the statement directly. */
13576 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
13577 statement
= cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
13578 /* If the token is not a `{', then we must take special action. */
13581 /* Create a compound-statement. */
13582 statement
= begin_compound_stmt (0);
13583 /* Parse the dependent-statement. */
13584 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, chain
,
13585 &body_loc_after_labels
);
13586 /* Finish the dummy compound-statement. */
13587 finish_compound_stmt (statement
);
13590 token_indent_info next_tinfo
13591 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13592 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13594 if (body_loc_after_labels
!= UNKNOWN_LOCATION
13595 && next_tinfo
.type
!= CPP_SEMICOLON
)
13596 warn_for_multistatement_macros (body_loc_after_labels
, next_tinfo
.location
,
13597 guard_tinfo
.location
, guard_tinfo
.keyword
);
13599 /* Return the statement. */
13603 /* For some dependent statements (like `while (cond) statement'), we
13604 have already created a scope. Therefore, even if the dependent
13605 statement is a compound-statement, we do not want to create another
13609 cp_parser_already_scoped_statement (cp_parser
* parser
, bool *if_p
,
13610 const token_indent_info
&guard_tinfo
)
13612 /* If the token is a `{', then we must take special action. */
13613 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
13615 token_indent_info body_tinfo
13616 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13617 location_t loc_after_labels
= UNKNOWN_LOCATION
;
13619 cp_parser_statement (parser
, NULL_TREE
, false, if_p
, NULL
,
13620 &loc_after_labels
);
13621 token_indent_info next_tinfo
13622 = get_token_indent_info (cp_lexer_peek_token (parser
->lexer
));
13623 warn_for_misleading_indentation (guard_tinfo
, body_tinfo
, next_tinfo
);
13625 if (loc_after_labels
!= UNKNOWN_LOCATION
13626 && next_tinfo
.type
!= CPP_SEMICOLON
)
13627 warn_for_multistatement_macros (loc_after_labels
, next_tinfo
.location
,
13628 guard_tinfo
.location
,
13629 guard_tinfo
.keyword
);
13633 /* Avoid calling cp_parser_compound_statement, so that we
13634 don't create a new scope. Do everything else by hand. */
13635 matching_braces braces
;
13636 braces
.require_open (parser
);
13637 /* If the next keyword is `__label__' we have a label declaration. */
13638 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_LABEL
))
13639 cp_parser_label_declaration (parser
);
13640 /* Parse an (optional) statement-seq. */
13641 cp_parser_statement_seq_opt (parser
, NULL_TREE
);
13642 braces
.require_close (parser
);
13648 /* Parse a module-name,
13650 module-name . identifier
13653 Returns a pointer to module object, NULL. */
13655 static module_state
*
13656 cp_parser_module_name (cp_parser
*parser
)
13658 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13659 if (token
->type
== CPP_HEADER_NAME
)
13661 cp_lexer_consume_token (parser
->lexer
);
13663 return get_module (token
->u
.value
);
13666 module_state
*parent
= NULL
;
13667 bool partitioned
= false;
13668 if (token
->type
== CPP_COLON
&& named_module_p ())
13670 partitioned
= true;
13671 cp_lexer_consume_token (parser
->lexer
);
13676 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_NAME
)
13678 cp_parser_error (parser
, "expected module-name");
13682 tree name
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
13683 parent
= get_module (name
, parent
, partitioned
);
13684 token
= cp_lexer_peek_token (parser
->lexer
);
13685 if (!partitioned
&& token
->type
== CPP_COLON
)
13686 partitioned
= true;
13687 else if (token
->type
!= CPP_DOT
)
13690 cp_lexer_consume_token (parser
->lexer
);
13696 /* Named module-declaration
13697 __module ; PRAGMA_EOL
13698 __module private ; PRAGMA_EOL (unimplemented)
13699 [__export] __module module-name attr-spec-seq-opt ; PRAGMA_EOL
13702 static module_parse
13703 cp_parser_module_declaration (cp_parser
*parser
, module_parse mp_state
,
13706 /* We're a pseudo pragma. */
13707 parser
->lexer
->in_pragma
= true;
13708 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
13710 if (mp_state
== MP_FIRST
&& !exporting
13711 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
13713 /* Start global module fragment. */
13714 cp_lexer_consume_token (parser
->lexer
);
13715 module_kind
|= MK_GLOBAL
;
13716 mp_state
= MP_GLOBAL
;
13717 cp_parser_require_pragma_eol (parser
, token
);
13719 else if (!exporting
13720 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
13721 && cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_PRIVATE
)
13722 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_SEMICOLON
))
13724 cp_lexer_consume_token (parser
->lexer
);
13725 cp_lexer_consume_token (parser
->lexer
);
13726 cp_lexer_consume_token (parser
->lexer
);
13727 cp_parser_require_pragma_eol (parser
, token
);
13729 if (!(mp_state
== MP_PURVIEW
|| mp_state
== MP_PURVIEW_IMPORTS
)
13730 || !module_interface_p () || module_partition_p ())
13731 error_at (token
->location
,
13732 "private module fragment only permitted in purview"
13733 " of module interface or partition");
13736 mp_state
= MP_PRIVATE_IMPORTS
;
13737 sorry_at (token
->location
, "private module fragment");
13740 else if (!(mp_state
== MP_FIRST
|| mp_state
== MP_GLOBAL
))
13742 /* Neither the first declaration, nor in a GMF. */
13743 error_at (token
->location
, "module-declaration only permitted as first"
13744 " declaration, or ending a global module fragment");
13746 cp_parser_skip_to_pragma_eol (parser
, token
);
13750 module_state
*mod
= cp_parser_module_name (parser
);
13751 tree attrs
= cp_parser_attributes_opt (parser
);
13753 mp_state
= MP_PURVIEW_IMPORTS
;
13754 if (!mod
|| !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
13757 declare_module (mod
, token
->location
, exporting
, attrs
, parse_in
);
13758 cp_parser_require_pragma_eol (parser
, token
);
13764 /* Import-declaration
13765 [__export] __import module-name attr-spec-seq-opt ; PRAGMA_EOL */
13768 cp_parser_import_declaration (cp_parser
*parser
, module_parse mp_state
,
13771 /* We're a pseudo pragma. */
13772 parser
->lexer
->in_pragma
= true;
13773 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
13775 if (mp_state
!= MP_PURVIEW_IMPORTS
13776 && mp_state
!= MP_PRIVATE_IMPORTS
13777 && module_purview_p ()
13778 && !global_purview_p ())
13780 error_at (token
->location
, "post-module-declaration"
13781 " imports must be contiguous");
13783 inform (token
->location
, "perhaps insert a line break, or other"
13784 " disambiguation, to prevent this being considered a"
13785 " module control-line");
13787 cp_parser_skip_to_pragma_eol (parser
, token
);
13789 else if (current_scope () != global_namespace
)
13791 error_at (token
->location
, "import-declaration must be at global scope");
13796 module_state
*mod
= cp_parser_module_name (parser
);
13797 tree attrs
= cp_parser_attributes_opt (parser
);
13799 if (!mod
|| !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
13801 cp_parser_require_pragma_eol (parser
, token
);
13803 if (parser
->in_unbraced_linkage_specification_p
)
13804 error_at (token
->location
, "import cannot appear directly in"
13805 " a linkage-specification");
13807 /* Module-purview imports must not be from source inclusion
13809 if (attrs
&& module_purview_p () && !global_purview_p ()
13810 && private_lookup_attribute ("__translated",
13811 strlen ("__translated"), attrs
))
13812 error_at (token
->location
, "post-module-declaration imports"
13813 " must not be include-translated");
13814 else if ((mp_state
== MP_PURVIEW_IMPORTS
13815 || mp_state
== MP_PRIVATE_IMPORTS
)
13816 && !token
->main_source_p
)
13817 error_at (token
->location
, "post-module-declaration imports"
13818 " must not be from header inclusion");
13820 import_module (mod
, token
->location
, exporting
, attrs
, parse_in
);
13824 /* export-declaration.
13827 export { declaration-seq-opt } */
13830 cp_parser_module_export (cp_parser
*parser
)
13832 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
));
13833 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
13835 if (!module_interface_p ())
13836 error_at (token
->location
,
13837 "%qE may only occur after a module interface declaration",
13840 bool braced
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
);
13842 unsigned mk
= module_kind
;
13843 if (module_exporting_p ())
13844 error_at (token
->location
,
13845 "%qE may only occur once in an export declaration",
13847 module_kind
|= MK_EXPORTING
;
13851 cp_ensure_no_omp_declare_simd (parser
);
13852 cp_ensure_no_oacc_routine (parser
);
13854 cp_lexer_consume_token (parser
->lexer
);
13855 cp_parser_declaration_seq_opt (parser
);
13856 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
13860 /* Explicitly check if the next tokens might be a
13861 module-directive line, so we can give a clearer error message
13862 about why the directive will be rejected. */
13863 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID__MODULE
)
13864 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID__IMPORT
)
13865 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID__EXPORT
))
13866 error_at (token
->location
, "%<export%> not part of following"
13867 " module-directive");
13868 cp_parser_declaration (parser
, NULL_TREE
);
13874 /* Declarations [gram.dcl.dcl] */
13876 /* Parse an optional declaration-sequence. TOP_LEVEL is true, if this
13877 is the top-level declaration sequence. That affects whether we
13878 deal with module-preamble.
13882 declaration-seq declaration */
13885 cp_parser_declaration_seq_opt (cp_parser
* parser
)
13889 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
13891 if (token
->type
== CPP_CLOSE_BRACE
13892 || token
->type
== CPP_EOF
)
13895 cp_parser_toplevel_declaration (parser
);
13899 /* Parse a declaration.
13903 function-definition
13904 template-declaration
13905 explicit-instantiation
13906 explicit-specialization
13907 linkage-specification
13908 namespace-definition
13914 (all these are only allowed at the outermost level, check
13915 that semantically, for better diagnostics)
13917 module-export-declaration
13918 module-import-declaration
13924 __extension__ declaration */
13927 cp_parser_declaration (cp_parser
* parser
, tree prefix_attrs
)
13929 int saved_pedantic
;
13931 /* Check for the `__extension__' keyword. */
13932 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
13934 /* Parse the qualified declaration. */
13935 cp_parser_declaration (parser
, prefix_attrs
);
13936 /* Restore the PEDANTIC flag. */
13937 pedantic
= saved_pedantic
;
13942 /* Try to figure out what kind of declaration is present. */
13943 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
13944 cp_token
*token2
= (token1
->type
== CPP_EOF
13945 ? token1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
13947 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
13948 void *p
= obstack_alloc (&declarator_obstack
, 0);
13950 tree attributes
= NULL_TREE
;
13952 /* Conditionally, allow attributes to precede a linkage specification. */
13953 if (token1
->keyword
== RID_ATTRIBUTE
)
13955 cp_lexer_save_tokens (parser
->lexer
);
13956 attributes
= cp_parser_attributes_opt (parser
);
13957 cp_token
*t1
= cp_lexer_peek_token (parser
->lexer
);
13958 cp_token
*t2
= (t1
->type
== CPP_EOF
13959 ? t1
: cp_lexer_peek_nth_token (parser
->lexer
, 2));
13960 if (t1
->keyword
== RID_EXTERN
13961 && cp_parser_is_pure_string_literal (t2
))
13963 cp_lexer_commit_tokens (parser
->lexer
);
13964 /* We might have already been here. */
13965 if (!c_dialect_objc ())
13967 location_t where
= get_finish (t2
->location
);
13968 warning_at (token1
->location
, OPT_Wattributes
, "attributes are"
13969 " not permitted in this position");
13970 where
= linemap_position_for_loc_and_offset (line_table
,
13972 inform (where
, "attributes may be inserted here");
13973 attributes
= NULL_TREE
;
13980 cp_lexer_rollback_tokens (parser
->lexer
);
13981 attributes
= NULL_TREE
;
13984 /* If we already had some attributes, and we've added more, then prepend.
13985 Otherwise attributes just contains any that we just read. */
13989 TREE_CHAIN (prefix_attrs
) = attributes
;
13990 attributes
= prefix_attrs
;
13993 /* If the next token is `extern' and the following token is a string
13994 literal, then we have a linkage specification. */
13995 if (token1
->keyword
== RID_EXTERN
13996 && cp_parser_is_pure_string_literal (token2
))
13997 cp_parser_linkage_specification (parser
, attributes
);
13998 /* If the next token is `template', then we have either a template
13999 declaration, an explicit instantiation, or an explicit
14001 else if (token1
->keyword
== RID_TEMPLATE
)
14003 /* `template <>' indicates a template specialization. */
14004 if (token2
->type
== CPP_LESS
14005 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
14006 cp_parser_explicit_specialization (parser
);
14007 /* `template <' indicates a template declaration. */
14008 else if (token2
->type
== CPP_LESS
)
14009 cp_parser_template_declaration (parser
, /*member_p=*/false);
14010 /* Anything else must be an explicit instantiation. */
14012 cp_parser_explicit_instantiation (parser
);
14014 /* If the next token is `export', it's new-style modules or
14015 old-style template. */
14016 else if (token1
->keyword
== RID_EXPORT
)
14019 cp_parser_template_declaration (parser
, /*member_p=*/false);
14021 cp_parser_module_export (parser
);
14023 else if (token1
->keyword
== RID__EXPORT
14024 || token1
->keyword
== RID__IMPORT
14025 || token1
->keyword
== RID__MODULE
)
14027 bool exporting
= token1
->keyword
== RID__EXPORT
;
14028 cp_token
*next
= exporting
? token2
: token1
;
14030 cp_lexer_consume_token (parser
->lexer
);
14031 if (next
->keyword
== RID__MODULE
)
14032 cp_parser_module_declaration (parser
, MP_NOT_MODULE
, exporting
);
14034 cp_parser_import_declaration (parser
, MP_NOT_MODULE
, exporting
);
14036 /* If the next token is `extern', 'static' or 'inline' and the one
14037 after that is `template', we have a GNU extended explicit
14038 instantiation directive. */
14039 else if (cp_parser_allow_gnu_extensions_p (parser
)
14040 && token2
->keyword
== RID_TEMPLATE
14041 && (token1
->keyword
== RID_EXTERN
14042 || token1
->keyword
== RID_STATIC
14043 || token1
->keyword
== RID_INLINE
))
14044 cp_parser_explicit_instantiation (parser
);
14045 /* If the next token is `namespace', check for a named or unnamed
14046 namespace definition. */
14047 else if (token1
->keyword
== RID_NAMESPACE
14048 && (/* A named namespace definition. */
14049 (token2
->type
== CPP_NAME
14050 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
14052 || (token2
->type
== CPP_OPEN_SQUARE
14053 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
14054 == CPP_OPEN_SQUARE
)
14055 /* An unnamed namespace definition. */
14056 || token2
->type
== CPP_OPEN_BRACE
14057 || token2
->keyword
== RID_ATTRIBUTE
))
14058 cp_parser_namespace_definition (parser
);
14059 /* An inline (associated) namespace definition. */
14060 else if (token2
->keyword
== RID_NAMESPACE
14061 && token1
->keyword
== RID_INLINE
)
14062 cp_parser_namespace_definition (parser
);
14063 /* Objective-C++ declaration/definition. */
14064 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1
->keyword
))
14065 cp_parser_objc_declaration (parser
, attributes
);
14066 else if (c_dialect_objc ()
14067 && token1
->keyword
== RID_ATTRIBUTE
14068 && cp_parser_objc_valid_prefix_attributes (parser
, &attributes
))
14069 cp_parser_objc_declaration (parser
, attributes
);
14070 /* At this point we may have a template declared by a concept
14072 else if (flag_concepts
14073 && cp_parser_template_declaration_after_export (parser
,
14074 /*member_p=*/false))
14077 /* Try to parse a block-declaration, or a function-definition. */
14078 cp_parser_block_declaration (parser
, /*statement_p=*/false);
14080 /* Free any declarators allocated. */
14081 obstack_free (&declarator_obstack
, p
);
14084 /* Parse a namespace-scope declaration. */
14087 cp_parser_toplevel_declaration (cp_parser
* parser
)
14089 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14091 if (token
->type
== CPP_PRAGMA
)
14092 /* A top-level declaration can consist solely of a #pragma. A
14093 nested declaration cannot, so this is done here and not in
14094 cp_parser_declaration. (A #pragma at block scope is
14095 handled in cp_parser_statement.) */
14096 cp_parser_pragma (parser
, pragma_external
, NULL
);
14097 else if (token
->type
== CPP_SEMICOLON
)
14099 cp_lexer_consume_token (parser
->lexer
);
14100 /* A declaration consisting of a single semicolon is invalid
14101 * before C++11. Allow it unless we're being pedantic. */
14102 if (cxx_dialect
< cxx11
)
14103 pedwarn (input_location
, OPT_Wpedantic
, "extra %<;%>");
14106 /* Parse the declaration itself. */
14107 cp_parser_declaration (parser
, NULL_TREE
);
14110 /* Parse a block-declaration.
14115 namespace-alias-definition
14122 __extension__ block-declaration
14127 static_assert-declaration
14129 If STATEMENT_P is TRUE, then this block-declaration is occurring as
14130 part of a declaration-statement. */
14133 cp_parser_block_declaration (cp_parser
*parser
,
14136 int saved_pedantic
;
14138 /* Check for the `__extension__' keyword. */
14139 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
14141 /* Parse the qualified declaration. */
14142 cp_parser_block_declaration (parser
, statement_p
);
14143 /* Restore the PEDANTIC flag. */
14144 pedantic
= saved_pedantic
;
14149 /* Peek at the next token to figure out which kind of declaration is
14151 cp_token
*token1
= cp_lexer_peek_token (parser
->lexer
);
14153 /* If the next keyword is `asm', we have an asm-definition. */
14154 if (token1
->keyword
== RID_ASM
)
14157 cp_parser_commit_to_tentative_parse (parser
);
14158 cp_parser_asm_definition (parser
);
14160 /* If the next keyword is `namespace', we have a
14161 namespace-alias-definition. */
14162 else if (token1
->keyword
== RID_NAMESPACE
)
14163 cp_parser_namespace_alias_definition (parser
);
14164 /* If the next keyword is `using', we have a
14165 using-declaration, a using-directive, or an alias-declaration. */
14166 else if (token1
->keyword
== RID_USING
)
14171 cp_parser_commit_to_tentative_parse (parser
);
14172 /* If the token after `using' is `namespace', then we have a
14173 using-directive. */
14174 token2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
14175 if (token2
->keyword
== RID_NAMESPACE
)
14176 cp_parser_using_directive (parser
);
14177 else if (token2
->keyword
== RID_ENUM
)
14178 cp_parser_using_enum (parser
);
14179 /* If the second token after 'using' is '=', then we have an
14180 alias-declaration. */
14181 else if (cxx_dialect
>= cxx11
14182 && token2
->type
== CPP_NAME
14183 && ((cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
)
14184 || (cp_nth_tokens_can_be_attribute_p (parser
, 3))))
14185 cp_parser_alias_declaration (parser
);
14186 /* Otherwise, it's a using-declaration. */
14188 cp_parser_using_declaration (parser
,
14189 /*access_declaration_p=*/false);
14191 /* If the next keyword is `__label__' we have a misplaced label
14193 else if (token1
->keyword
== RID_LABEL
)
14195 cp_lexer_consume_token (parser
->lexer
);
14196 error_at (token1
->location
, "%<__label__%> not at the beginning of a block");
14197 cp_parser_skip_to_end_of_statement (parser
);
14198 /* If the next token is now a `;', consume it. */
14199 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14200 cp_lexer_consume_token (parser
->lexer
);
14202 /* If the next token is `static_assert' we have a static assertion. */
14203 else if (token1
->keyword
== RID_STATIC_ASSERT
)
14204 cp_parser_static_assert (parser
, /*member_p=*/false);
14205 /* Anything else must be a simple-declaration. */
14207 cp_parser_simple_declaration (parser
, !statement_p
,
14208 /*maybe_range_for_decl*/NULL
);
14211 /* Parse a simple-declaration.
14213 simple-declaration:
14214 decl-specifier-seq [opt] init-declarator-list [opt] ;
14215 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
14216 brace-or-equal-initializer ;
14218 init-declarator-list:
14220 init-declarator-list , init-declarator
14222 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
14223 function-definition as a simple-declaration.
14225 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14226 parsed declaration if it is an uninitialized single declarator not followed
14227 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14228 if present, will not be consumed. */
14231 cp_parser_simple_declaration (cp_parser
* parser
,
14232 bool function_definition_allowed_p
,
14233 tree
*maybe_range_for_decl
)
14235 cp_decl_specifier_seq decl_specifiers
;
14236 int declares_class_or_enum
;
14237 bool saw_declarator
;
14238 location_t comma_loc
= UNKNOWN_LOCATION
;
14239 location_t init_loc
= UNKNOWN_LOCATION
;
14241 if (maybe_range_for_decl
)
14242 *maybe_range_for_decl
= NULL_TREE
;
14244 /* Defer access checks until we know what is being declared; the
14245 checks for names appearing in the decl-specifier-seq should be
14246 done as if we were in the scope of the thing being declared. */
14247 push_deferring_access_checks (dk_deferred
);
14249 /* Parse the decl-specifier-seq. We have to keep track of whether
14250 or not the decl-specifier-seq declares a named class or
14251 enumeration type, since that is the only case in which the
14252 init-declarator-list is allowed to be empty.
14256 In a simple-declaration, the optional init-declarator-list can be
14257 omitted only when declaring a class or enumeration, that is when
14258 the decl-specifier-seq contains either a class-specifier, an
14259 elaborated-type-specifier, or an enum-specifier. */
14260 cp_parser_decl_specifier_seq (parser
,
14261 CP_PARSER_FLAGS_OPTIONAL
,
14263 &declares_class_or_enum
);
14264 /* We no longer need to defer access checks. */
14265 stop_deferring_access_checks ();
14267 /* In a block scope, a valid declaration must always have a
14268 decl-specifier-seq. By not trying to parse declarators, we can
14269 resolve the declaration/expression ambiguity more quickly. */
14270 if (!function_definition_allowed_p
14271 && !decl_specifiers
.any_specifiers_p
)
14273 cp_parser_error (parser
, "expected declaration");
14277 /* If the next two tokens are both identifiers, the code is
14278 erroneous. The usual cause of this situation is code like:
14282 where "T" should name a type -- but does not. */
14283 if (!decl_specifiers
.any_type_specifiers_p
14284 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
14286 /* If parsing tentatively, we should commit; we really are
14287 looking at a declaration. */
14288 cp_parser_commit_to_tentative_parse (parser
);
14293 cp_parser_maybe_commit_to_declaration (parser
, &decl_specifiers
);
14295 /* Look for C++17 decomposition declaration. */
14296 for (size_t n
= 1; ; n
++)
14297 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND
)
14298 || cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_AND_AND
))
14300 else if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
14301 && !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
)
14302 && decl_specifiers
.any_specifiers_p
)
14305 = cp_parser_decomposition_declaration (parser
, &decl_specifiers
,
14306 maybe_range_for_decl
,
14309 /* The next token should be either a `,' or a `;'. */
14310 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14311 /* If it's a `;', we are done. */
14312 if (token
->type
== CPP_SEMICOLON
)
14314 else if (maybe_range_for_decl
)
14316 if (*maybe_range_for_decl
== NULL_TREE
)
14317 *maybe_range_for_decl
= error_mark_node
;
14320 /* Anything else is an error. */
14323 /* If we have already issued an error message we don't need
14324 to issue another one. */
14325 if ((decl
!= error_mark_node
14326 && DECL_INITIAL (decl
) != error_mark_node
)
14327 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
14328 cp_parser_error (parser
, "expected %<;%>");
14329 /* Skip tokens until we reach the end of the statement. */
14330 cp_parser_skip_to_end_of_statement (parser
);
14331 /* If the next token is now a `;', consume it. */
14332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14333 cp_lexer_consume_token (parser
->lexer
);
14341 bool auto_specifier_p
;
14342 /* NULL_TREE if both variable and function declaration are allowed,
14343 error_mark_node if function declaration are not allowed and
14344 a FUNCTION_DECL that should be diagnosed if it is followed by
14345 variable declarations. */
14346 tree auto_function_declaration
;
14348 last_type
= NULL_TREE
;
14350 = decl_specifiers
.type
&& type_uses_auto (decl_specifiers
.type
);
14351 auto_function_declaration
= NULL_TREE
;
14353 /* Keep going until we hit the `;' at the end of the simple
14355 saw_declarator
= false;
14356 while (cp_lexer_next_token_is_not (parser
->lexer
,
14360 bool function_definition_p
;
14362 tree auto_result
= NULL_TREE
;
14364 if (saw_declarator
)
14366 /* If we are processing next declarator, comma is expected */
14367 token
= cp_lexer_peek_token (parser
->lexer
);
14368 gcc_assert (token
->type
== CPP_COMMA
);
14369 cp_lexer_consume_token (parser
->lexer
);
14370 if (maybe_range_for_decl
)
14372 *maybe_range_for_decl
= error_mark_node
;
14373 if (comma_loc
== UNKNOWN_LOCATION
)
14374 comma_loc
= token
->location
;
14378 saw_declarator
= true;
14380 /* Parse the init-declarator. */
14381 decl
= cp_parser_init_declarator (parser
,
14382 CP_PARSER_FLAGS_NONE
,
14385 function_definition_allowed_p
,
14386 /*member_p=*/false,
14387 declares_class_or_enum
,
14388 &function_definition_p
,
14389 maybe_range_for_decl
,
14392 /* If an error occurred while parsing tentatively, exit quickly.
14393 (That usually happens when in the body of a function; each
14394 statement is treated as a declaration-statement until proven
14396 if (cp_parser_error_occurred (parser
))
14399 if (auto_specifier_p
&& cxx_dialect
>= cxx14
)
14401 /* If the init-declarator-list contains more than one
14402 init-declarator, they shall all form declarations of
14404 if (auto_function_declaration
== NULL_TREE
)
14405 auto_function_declaration
14406 = TREE_CODE (decl
) == FUNCTION_DECL
? decl
: error_mark_node
;
14407 else if (TREE_CODE (decl
) == FUNCTION_DECL
14408 || auto_function_declaration
!= error_mark_node
)
14410 error_at (decl_specifiers
.locations
[ds_type_spec
],
14411 "non-variable %qD in declaration with more than one "
14412 "declarator with placeholder type",
14413 TREE_CODE (decl
) == FUNCTION_DECL
14414 ? decl
: auto_function_declaration
);
14415 auto_function_declaration
= error_mark_node
;
14420 && (!processing_template_decl
|| !type_uses_auto (auto_result
)))
14423 && last_type
!= error_mark_node
14424 && !same_type_p (auto_result
, last_type
))
14426 /* If the list of declarators contains more than one declarator,
14427 the type of each declared variable is determined as described
14428 above. If the type deduced for the template parameter U is not
14429 the same in each deduction, the program is ill-formed. */
14430 error_at (decl_specifiers
.locations
[ds_type_spec
],
14431 "inconsistent deduction for %qT: %qT and then %qT",
14432 decl_specifiers
.type
, last_type
, auto_result
);
14433 last_type
= error_mark_node
;
14436 last_type
= auto_result
;
14439 /* Handle function definitions specially. */
14440 if (function_definition_p
)
14442 /* If the next token is a `,', then we are probably
14443 processing something like:
14447 which is erroneous. */
14448 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14450 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
14451 error_at (token
->location
,
14453 " declarations and function-definitions is forbidden");
14455 /* Otherwise, we're done with the list of declarators. */
14458 pop_deferring_access_checks ();
14462 if (maybe_range_for_decl
&& *maybe_range_for_decl
== NULL_TREE
)
14463 *maybe_range_for_decl
= decl
;
14464 /* The next token should be either a `,' or a `;'. */
14465 token
= cp_lexer_peek_token (parser
->lexer
);
14466 /* If it's a `,', there are more declarators to come. */
14467 if (token
->type
== CPP_COMMA
)
14468 /* will be consumed next time around */;
14469 /* If it's a `;', we are done. */
14470 else if (token
->type
== CPP_SEMICOLON
)
14472 else if (maybe_range_for_decl
)
14474 if ((declares_class_or_enum
& 2) && token
->type
== CPP_COLON
)
14475 permerror (decl_specifiers
.locations
[ds_type_spec
],
14476 "types may not be defined in a for-range-declaration");
14479 /* Anything else is an error. */
14482 /* If we have already issued an error message we don't need
14483 to issue another one. */
14484 if ((decl
!= error_mark_node
14485 && DECL_INITIAL (decl
) != error_mark_node
)
14486 || cp_parser_uncommitted_to_tentative_parse_p (parser
))
14487 cp_parser_error (parser
, "expected %<,%> or %<;%>");
14488 /* Skip tokens until we reach the end of the statement. */
14489 cp_parser_skip_to_end_of_statement (parser
);
14490 /* If the next token is now a `;', consume it. */
14491 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
14492 cp_lexer_consume_token (parser
->lexer
);
14495 /* After the first time around, a function-definition is not
14496 allowed -- even if it was OK at first. For example:
14501 function_definition_allowed_p
= false;
14504 /* Issue an error message if no declarators are present, and the
14505 decl-specifier-seq does not itself declare a class or
14506 enumeration: [dcl.dcl]/3. */
14507 if (!saw_declarator
)
14509 if (cp_parser_declares_only_class_p (parser
))
14511 if (!declares_class_or_enum
14512 && decl_specifiers
.type
14513 && OVERLOAD_TYPE_P (decl_specifiers
.type
))
14514 /* Ensure an error is issued anyway when finish_decltype_type,
14515 called via cp_parser_decl_specifier_seq, returns a class or
14516 an enumeration (c++/51786). */
14517 decl_specifiers
.type
= NULL_TREE
;
14518 shadow_tag (&decl_specifiers
);
14520 /* Perform any deferred access checks. */
14521 perform_deferred_access_checks (tf_warning_or_error
);
14524 /* Consume the `;'. */
14526 if (!maybe_range_for_decl
)
14527 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
14528 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
14530 if (init_loc
!= UNKNOWN_LOCATION
)
14531 error_at (init_loc
, "initializer in range-based %<for%> loop");
14532 if (comma_loc
!= UNKNOWN_LOCATION
)
14533 error_at (comma_loc
,
14534 "multiple declarations in range-based %<for%> loop");
14538 pop_deferring_access_checks ();
14541 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
14542 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
14546 cp_parser_decomposition_declaration (cp_parser
*parser
,
14547 cp_decl_specifier_seq
*decl_specifiers
,
14548 tree
*maybe_range_for_decl
,
14549 location_t
*init_loc
)
14551 cp_ref_qualifier ref_qual
= cp_parser_ref_qualifier_opt (parser
);
14552 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14553 cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
);
14555 /* Parse the identifier-list. */
14556 auto_vec
<cp_expr
, 10> v
;
14557 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
14560 cp_expr e
= cp_parser_identifier (parser
);
14561 if (e
.get_value () == error_mark_node
)
14564 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
14566 cp_lexer_consume_token (parser
->lexer
);
14569 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14570 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
14572 end_loc
= UNKNOWN_LOCATION
;
14573 cp_parser_skip_to_closing_parenthesis_1 (parser
, true, CPP_CLOSE_SQUARE
,
14575 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
14576 cp_lexer_consume_token (parser
->lexer
);
14579 cp_parser_skip_to_end_of_statement (parser
);
14580 return error_mark_node
;
14584 if (cxx_dialect
< cxx17
)
14585 pedwarn (loc
, 0, "structured bindings only available with "
14586 "%<-std=c++17%> or %<-std=gnu++17%>");
14589 cp_declarator
*declarator
= make_declarator (cdk_decomp
);
14590 loc
= end_loc
== UNKNOWN_LOCATION
? loc
: make_location (loc
, loc
, end_loc
);
14591 declarator
->id_loc
= loc
;
14592 if (ref_qual
!= REF_QUAL_NONE
)
14593 declarator
= make_reference_declarator (TYPE_UNQUALIFIED
, declarator
,
14594 ref_qual
== REF_QUAL_RVALUE
,
14596 tree decl
= start_decl (declarator
, decl_specifiers
, SD_INITIALIZED
,
14597 NULL_TREE
, decl_specifiers
->attributes
,
14599 tree orig_decl
= decl
;
14603 cp_decl_specifier_seq decl_specs
;
14604 clear_decl_specs (&decl_specs
);
14605 decl_specs
.type
= make_auto ();
14607 FOR_EACH_VEC_ELT (v
, i
, e
)
14610 declarator
= make_id_declarator (NULL_TREE
, e
.get_value (),
14611 sfk_none
, e
.get_location ());
14614 declarator
->u
.id
.unqualified_name
= e
.get_value ();
14615 declarator
->id_loc
= e
.get_location ();
14617 tree elt_pushed_scope
;
14618 tree decl2
= start_decl (declarator
, &decl_specs
, SD_DECOMPOSITION
,
14619 NULL_TREE
, NULL_TREE
, &elt_pushed_scope
);
14620 if (decl2
== error_mark_node
)
14621 decl
= error_mark_node
;
14622 else if (decl
!= error_mark_node
&& DECL_CHAIN (decl2
) != prev
)
14624 /* Ensure we've diagnosed redeclaration if we aren't creating
14626 gcc_assert (errorcount
);
14627 decl
= error_mark_node
;
14631 if (elt_pushed_scope
)
14632 pop_scope (elt_pushed_scope
);
14637 error_at (loc
, "empty structured binding declaration");
14638 decl
= error_mark_node
;
14641 if (maybe_range_for_decl
== NULL
14642 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
14644 bool non_constant_p
= false, is_direct_init
= false;
14645 *init_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
14646 tree initializer
= cp_parser_initializer (parser
, &is_direct_init
,
14648 if (initializer
== NULL_TREE
14649 || (TREE_CODE (initializer
) == TREE_LIST
14650 && TREE_CHAIN (initializer
))
14652 && BRACE_ENCLOSED_INITIALIZER_P (initializer
)
14653 && CONSTRUCTOR_NELTS (initializer
) != 1))
14655 error_at (loc
, "invalid initializer for structured binding "
14657 initializer
= error_mark_node
;
14660 if (decl
!= error_mark_node
)
14662 cp_maybe_mangle_decomp (decl
, prev
, v
.length ());
14663 cp_finish_decl (decl
, initializer
, non_constant_p
, NULL_TREE
,
14664 (is_direct_init
? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
14665 cp_finish_decomp (decl
, prev
, v
.length ());
14668 else if (decl
!= error_mark_node
)
14670 *maybe_range_for_decl
= prev
;
14671 /* Ensure DECL_VALUE_EXPR is created for all the decls but
14672 the underlying DECL. */
14673 cp_finish_decomp (decl
, prev
, v
.length ());
14677 pop_scope (pushed_scope
);
14679 if (decl
== error_mark_node
&& DECL_P (orig_decl
))
14681 if (DECL_NAMESPACE_SCOPE_P (orig_decl
))
14682 SET_DECL_ASSEMBLER_NAME (orig_decl
, get_identifier ("<decomp>"));
14688 /* Parse a decl-specifier-seq.
14690 decl-specifier-seq:
14691 decl-specifier-seq [opt] decl-specifier
14692 decl-specifier attribute-specifier-seq [opt] (C++11)
14695 storage-class-specifier
14706 Concepts Extension:
14711 Set *DECL_SPECS to a representation of the decl-specifier-seq.
14713 The parser flags FLAGS is used to control type-specifier parsing.
14715 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
14718 1: one of the decl-specifiers is an elaborated-type-specifier
14719 (i.e., a type declaration)
14720 2: one of the decl-specifiers is an enum-specifier or a
14721 class-specifier (i.e., a type definition)
14726 cp_parser_decl_specifier_seq (cp_parser
* parser
,
14727 cp_parser_flags flags
,
14728 cp_decl_specifier_seq
*decl_specs
,
14729 int* declares_class_or_enum
)
14731 bool constructor_possible_p
= !parser
->in_declarator_p
;
14732 bool found_decl_spec
= false;
14733 cp_token
*start_token
= NULL
;
14736 /* Clear DECL_SPECS. */
14737 clear_decl_specs (decl_specs
);
14739 /* Assume no class or enumeration type is declared. */
14740 *declares_class_or_enum
= 0;
14742 /* Keep reading specifiers until there are no more to read. */
14745 bool constructor_p
;
14749 /* Peek at the next token. */
14750 token
= cp_lexer_peek_token (parser
->lexer
);
14752 /* Save the first token of the decl spec list for error
14755 start_token
= token
;
14756 /* Handle attributes. */
14757 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) == 0
14758 && cp_next_tokens_can_be_attribute_p (parser
))
14760 /* Parse the attributes. */
14761 tree attrs
= cp_parser_attributes_opt (parser
);
14763 /* In a sequence of declaration specifiers, c++11 attributes
14764 appertain to the type that precede them. In that case
14767 The attribute-specifier-seq affects the type only for
14768 the declaration it appears in, not other declarations
14769 involving the same type.
14771 But for now let's force the user to position the
14772 attribute either at the beginning of the declaration or
14773 after the declarator-id, which would clearly mean that it
14774 applies to the declarator. */
14775 if (cxx11_attribute_p (attrs
))
14777 if (!found_decl_spec
)
14778 /* The c++11 attribute is at the beginning of the
14779 declaration. It appertains to the entity being
14783 if (decl_specs
->type
&& CLASS_TYPE_P (decl_specs
->type
))
14785 /* This is an attribute following a
14786 class-specifier. */
14787 if (decl_specs
->type_definition_p
)
14788 warn_misplaced_attr_for_class_type (token
->location
,
14794 decl_specs
->std_attributes
14795 = attr_chainon (decl_specs
->std_attributes
, attrs
);
14796 if (decl_specs
->locations
[ds_std_attribute
] == 0)
14797 decl_specs
->locations
[ds_std_attribute
] = token
->location
;
14803 decl_specs
->attributes
14804 = attr_chainon (decl_specs
->attributes
, attrs
);
14805 if (decl_specs
->locations
[ds_attribute
] == 0)
14806 decl_specs
->locations
[ds_attribute
] = token
->location
;
14809 /* Assume we will find a decl-specifier keyword. */
14810 found_decl_spec
= true;
14811 /* If the next token is an appropriate keyword, we can simply
14812 add it to the list. */
14813 switch (token
->keyword
)
14820 if (!at_class_scope_p ())
14822 gcc_rich_location
richloc (token
->location
);
14823 richloc
.add_fixit_remove ();
14824 error_at (&richloc
, "%<friend%> used outside of class");
14825 cp_lexer_purge_token (parser
->lexer
);
14830 /* Consume the token. */
14831 cp_lexer_consume_token (parser
->lexer
);
14835 case RID_CONSTEXPR
:
14837 cp_lexer_consume_token (parser
->lexer
);
14840 case RID_CONSTINIT
:
14842 cp_lexer_consume_token (parser
->lexer
);
14845 case RID_CONSTEVAL
:
14847 cp_lexer_consume_token (parser
->lexer
);
14852 cp_lexer_consume_token (parser
->lexer
);
14854 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14857 /* Warn for concept as a decl-specifier. We'll rewrite these as
14858 concept declarations later. */
14859 if (!flag_concepts_ts
)
14861 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
14862 if (next
->keyword
== RID_BOOL
)
14863 pedwarn (next
->location
, 0, "the %<bool%> keyword is not "
14864 "allowed in a C++20 concept definition");
14866 pedwarn (token
->location
, 0, "C++20 concept definition syntax "
14867 "is %<concept <name> = <expr>%>");
14870 /* In C++20 a concept definition is just 'concept name = expr;'
14871 Support that syntax as a TS extension by pretending we've seen
14872 the 'bool' specifier. */
14873 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
14874 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
)
14875 && !decl_specs
->any_type_specifiers_p
)
14877 cp_parser_set_decl_spec_type (decl_specs
, boolean_type_node
,
14878 token
, /*type_definition*/false);
14879 decl_specs
->any_type_specifiers_p
= true;
14883 /* function-specifier:
14890 cp_parser_function_specifier_opt (parser
, decl_specs
);
14897 /* Consume the token. */
14898 cp_lexer_consume_token (parser
->lexer
);
14900 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14903 /* A constructor declarator cannot appear in a typedef. */
14904 constructor_possible_p
= false;
14905 /* The "typedef" keyword can only occur in a declaration; we
14906 may as well commit at this point. */
14907 cp_parser_commit_to_tentative_parse (parser
);
14909 if (decl_specs
->storage_class
!= sc_none
)
14910 decl_specs
->conflicting_specifiers_p
= true;
14913 /* storage-class-specifier:
14923 if (cxx_dialect
== cxx98
)
14925 /* Consume the token. */
14926 cp_lexer_consume_token (parser
->lexer
);
14928 /* Complain about `auto' as a storage specifier, if
14929 we're complaining about C++0x compatibility. */
14930 gcc_rich_location
richloc (token
->location
);
14931 richloc
.add_fixit_remove ();
14932 warning_at (&richloc
, OPT_Wc__11_compat
,
14933 "%<auto%> changes meaning in C++11; "
14934 "please remove it");
14936 /* Set the storage class anyway. */
14937 cp_parser_set_storage_class (parser
, decl_specs
, RID_AUTO
,
14941 /* C++0x auto type-specifier. */
14942 found_decl_spec
= false;
14949 /* Consume the token. */
14950 cp_lexer_consume_token (parser
->lexer
);
14951 cp_parser_set_storage_class (parser
, decl_specs
, token
->keyword
,
14955 /* Consume the token. */
14957 cp_lexer_consume_token (parser
->lexer
);
14961 /* We did not yet find a decl-specifier yet. */
14962 found_decl_spec
= false;
14966 if (found_decl_spec
14967 && (flags
& CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR
)
14968 && token
->keyword
!= RID_CONSTEXPR
)
14969 error ("%<decl-specifier%> invalid in condition");
14971 if (found_decl_spec
14972 && (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
14973 && token
->keyword
!= RID_MUTABLE
14974 && token
->keyword
!= RID_CONSTEXPR
14975 && token
->keyword
!= RID_CONSTEVAL
)
14976 error_at (token
->location
, "%qD invalid in lambda",
14977 ridpointers
[token
->keyword
]);
14980 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
14982 /* Constructors are a special case. The `S' in `S()' is not a
14983 decl-specifier; it is the beginning of the declarator. */
14985 = (!found_decl_spec
14986 && constructor_possible_p
14987 && (cp_parser_constructor_declarator_p
14988 (parser
, flags
, decl_spec_seq_has_spec_p (decl_specs
,
14991 /* If we don't have a DECL_SPEC yet, then we must be looking at
14992 a type-specifier. */
14993 if (!found_decl_spec
&& !constructor_p
)
14995 int decl_spec_declares_class_or_enum
;
14996 bool is_cv_qualifier
;
14999 if (flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
)
15000 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
15003 = cp_parser_type_specifier (parser
, flags
,
15005 /*is_declaration=*/true,
15006 &decl_spec_declares_class_or_enum
,
15008 *declares_class_or_enum
|= decl_spec_declares_class_or_enum
;
15010 /* If this type-specifier referenced a user-defined type
15011 (a typedef, class-name, etc.), then we can't allow any
15012 more such type-specifiers henceforth.
15016 The longest sequence of decl-specifiers that could
15017 possibly be a type name is taken as the
15018 decl-specifier-seq of a declaration. The sequence shall
15019 be self-consistent as described below.
15023 As a general rule, at most one type-specifier is allowed
15024 in the complete decl-specifier-seq of a declaration. The
15025 only exceptions are the following:
15027 -- const or volatile can be combined with any other
15030 -- signed or unsigned can be combined with char, long,
15038 void g (const int Pc);
15040 Here, Pc is *not* part of the decl-specifier seq; it's
15041 the declarator. Therefore, once we see a type-specifier
15042 (other than a cv-qualifier), we forbid any additional
15043 user-defined types. We *do* still allow things like `int
15044 int' to be considered a decl-specifier-seq, and issue the
15045 error message later. */
15046 if (type_spec
&& !is_cv_qualifier
)
15047 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
15048 /* A constructor declarator cannot follow a type-specifier. */
15051 constructor_possible_p
= false;
15052 found_decl_spec
= true;
15053 if (!is_cv_qualifier
)
15054 decl_specs
->any_type_specifiers_p
= true;
15056 if ((flags
& CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR
) != 0)
15057 error_at (token
->location
, "type-specifier invalid in lambda");
15061 /* If we still do not have a DECL_SPEC, then there are no more
15062 decl-specifiers. */
15063 if (!found_decl_spec
)
15066 decl_specs
->any_specifiers_p
= true;
15067 /* After we see one decl-specifier, further decl-specifiers are
15068 always optional. */
15069 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
15072 /* Don't allow a friend specifier with a class definition. */
15073 if (decl_spec_seq_has_spec_p (decl_specs
, ds_friend
)
15074 && (*declares_class_or_enum
& 2))
15075 error_at (decl_specs
->locations
[ds_friend
],
15076 "class definition may not be declared a friend");
15079 /* Parse an (optional) storage-class-specifier.
15081 storage-class-specifier:
15090 storage-class-specifier:
15093 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
15096 cp_parser_storage_class_specifier_opt (cp_parser
* parser
)
15098 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
15101 if (cxx_dialect
!= cxx98
)
15103 /* Fall through for C++98. */
15104 gcc_fallthrough ();
15111 /* Consume the token. */
15112 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15119 /* Parse an (optional) function-specifier.
15121 function-specifier:
15127 explicit(constant-expression)
15129 Returns an IDENTIFIER_NODE corresponding to the keyword used.
15130 Updates DECL_SPECS, if it is non-NULL. */
15133 cp_parser_function_specifier_opt (cp_parser
* parser
,
15134 cp_decl_specifier_seq
*decl_specs
)
15136 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15137 switch (token
->keyword
)
15140 set_and_check_decl_spec_loc (decl_specs
, ds_inline
, token
);
15144 /* 14.5.2.3 [temp.mem]
15146 A member function template shall not be virtual. */
15147 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15148 && current_class_type
)
15149 error_at (token
->location
, "templates may not be %<virtual%>");
15151 set_and_check_decl_spec_loc (decl_specs
, ds_virtual
, token
);
15156 tree id
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15157 /* If we see '(', it's C++20 explicit(bool). */
15159 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15161 matching_parens parens
;
15162 parens
.consume_open (parser
);
15164 /* New types are not allowed in an explicit-specifier. */
15165 const char *saved_message
15166 = parser
->type_definition_forbidden_message
;
15167 parser
->type_definition_forbidden_message
15168 = G_("types may not be defined in explicit-specifier");
15170 if (cxx_dialect
< cxx20
)
15171 pedwarn (token
->location
, 0,
15172 "%<explicit(bool)%> only available with %<-std=c++20%> "
15173 "or %<-std=gnu++20%>");
15175 /* Parse the constant-expression. */
15176 expr
= cp_parser_constant_expression (parser
);
15178 /* Restore the saved message. */
15179 parser
->type_definition_forbidden_message
= saved_message
;
15180 parens
.require_close (parser
);
15183 /* The explicit-specifier explicit without a constant-expression is
15184 equivalent to the explicit-specifier explicit(true). */
15185 expr
= boolean_true_node
;
15188 "the constant-expression, if supplied, shall be a contextually
15189 converted constant expression of type bool." */
15190 expr
= build_explicit_specifier (expr
, tf_warning_or_error
);
15191 /* We could evaluate it -- mark the decl as appropriate. */
15192 if (expr
== boolean_true_node
)
15193 set_and_check_decl_spec_loc (decl_specs
, ds_explicit
, token
);
15194 else if (expr
== boolean_false_node
)
15195 /* Don't mark the decl as explicit. */;
15196 else if (decl_specs
)
15197 /* The expression was value-dependent. Remember it so that we can
15198 substitute it later. */
15199 decl_specs
->explicit_specifier
= expr
;
15207 /* Consume the token. */
15208 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
15211 /* Parse a linkage-specification.
15213 linkage-specification:
15214 extern string-literal { declaration-seq [opt] }
15215 extern string-literal declaration */
15218 cp_parser_linkage_specification (cp_parser
* parser
, tree prefix_attr
)
15222 /* Look for the `extern' keyword. */
15223 cp_token
*extern_token
15224 = cp_parser_require_keyword (parser
, RID_EXTERN
, RT_EXTERN
);
15226 /* Look for the string-literal. */
15227 cp_token
*string_token
= cp_lexer_peek_token (parser
->lexer
);
15228 linkage
= cp_parser_string_literal (parser
, false, false);
15230 /* Transform the literal into an identifier. If the literal is a
15231 wide-character string, or contains embedded NULs, then we can't
15232 handle it as the user wants. */
15233 if (strlen (TREE_STRING_POINTER (linkage
))
15234 != (size_t) (TREE_STRING_LENGTH (linkage
) - 1))
15236 cp_parser_error (parser
, "invalid linkage-specification");
15237 /* Assume C++ linkage. */
15238 linkage
= lang_name_cplusplus
;
15241 linkage
= get_identifier (TREE_STRING_POINTER (linkage
));
15243 /* We're now using the new linkage. */
15244 push_lang_context (linkage
);
15246 /* Preserve the location of the innermost linkage specification,
15247 tracking the locations of nested specifications via a local. */
15248 location_t saved_location
15249 = parser
->innermost_linkage_specification_location
;
15250 /* Construct a location ranging from the start of the "extern" to
15251 the end of the string-literal, with the caret at the start, e.g.:
15255 parser
->innermost_linkage_specification_location
15256 = make_location (extern_token
->location
,
15257 extern_token
->location
,
15258 get_finish (string_token
->location
));
15260 /* If the next token is a `{', then we're using the first
15262 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15264 cp_ensure_no_omp_declare_simd (parser
);
15265 cp_ensure_no_oacc_routine (parser
);
15267 /* Consume the `{' token. */
15268 matching_braces braces
;
15269 braces
.consume_open (parser
);
15270 /* Parse the declarations. */
15271 cp_parser_declaration_seq_opt (parser
);
15272 /* Look for the closing `}'. */
15273 braces
.require_close (parser
);
15275 /* Otherwise, there's just one declaration. */
15278 bool saved_in_unbraced_linkage_specification_p
;
15280 saved_in_unbraced_linkage_specification_p
15281 = parser
->in_unbraced_linkage_specification_p
;
15282 parser
->in_unbraced_linkage_specification_p
= true;
15283 cp_parser_declaration (parser
, prefix_attr
);
15284 parser
->in_unbraced_linkage_specification_p
15285 = saved_in_unbraced_linkage_specification_p
;
15288 /* We're done with the linkage-specification. */
15289 pop_lang_context ();
15291 /* Restore location of parent linkage specification, if any. */
15292 parser
->innermost_linkage_specification_location
= saved_location
;
15295 /* Parse a static_assert-declaration.
15297 static_assert-declaration:
15298 static_assert ( constant-expression , string-literal ) ;
15299 static_assert ( constant-expression ) ; (C++17)
15301 If MEMBER_P, this static_assert is a class member. */
15304 cp_parser_static_assert(cp_parser
*parser
, bool member_p
)
15307 location_t token_loc
;
15311 /* Peek at the `static_assert' token so we can keep track of exactly
15312 where the static assertion started. */
15313 token_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
15315 /* Look for the `static_assert' keyword. */
15316 if (!cp_parser_require_keyword (parser
, RID_STATIC_ASSERT
,
15320 /* We know we are in a static assertion; commit to any tentative
15322 if (cp_parser_parsing_tentatively (parser
))
15323 cp_parser_commit_to_tentative_parse (parser
);
15325 /* Parse the `(' starting the static assertion condition. */
15326 matching_parens parens
;
15327 parens
.require_open (parser
);
15329 /* Parse the constant-expression. Allow a non-constant expression
15330 here in order to give better diagnostics in finish_static_assert. */
15332 cp_parser_constant_expression (parser
,
15333 /*allow_non_constant_p=*/true,
15334 /*non_constant_p=*/&dummy
);
15336 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
15338 if (cxx_dialect
< cxx17
)
15339 pedwarn (input_location
, OPT_Wpedantic
,
15340 "%<static_assert%> without a message "
15341 "only available with %<-std=c++17%> or %<-std=gnu++17%>");
15343 cp_lexer_consume_token (parser
->lexer
);
15344 message
= build_string (1, "");
15345 TREE_TYPE (message
) = char_array_type_node
;
15346 fix_string_type (message
);
15350 /* Parse the separating `,'. */
15351 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
15353 /* Parse the string-literal message. */
15354 message
= cp_parser_string_literal (parser
,
15355 /*translate=*/false,
15358 /* A `)' completes the static assertion. */
15359 if (!parens
.require_close (parser
))
15360 cp_parser_skip_to_closing_parenthesis (parser
,
15361 /*recovering=*/true,
15362 /*or_comma=*/false,
15363 /*consume_paren=*/true);
15366 /* A semicolon terminates the declaration. */
15367 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
15369 /* Get the location for the static assertion. Use that of the
15370 condition if available, otherwise, use that of the "static_assert"
15372 location_t assert_loc
= condition
.get_location ();
15373 if (assert_loc
== UNKNOWN_LOCATION
)
15374 assert_loc
= token_loc
;
15376 /* Complete the static assertion, which may mean either processing
15377 the static assert now or saving it for template instantiation. */
15378 finish_static_assert (condition
, message
, assert_loc
, member_p
,
15379 /*show_expr_p=*/false);
15382 /* Parse the expression in decltype ( expression ). */
15385 cp_parser_decltype_expr (cp_parser
*parser
,
15386 bool &id_expression_or_member_access_p
)
15388 cp_token
*id_expr_start_token
;
15391 /* First, try parsing an id-expression. */
15392 id_expr_start_token
= cp_lexer_peek_token (parser
->lexer
);
15393 cp_parser_parse_tentatively (parser
);
15394 expr
= cp_parser_id_expression (parser
,
15395 /*template_keyword_p=*/false,
15396 /*check_dependency_p=*/true,
15397 /*template_p=*/NULL
,
15398 /*declarator_p=*/false,
15399 /*optional_p=*/false);
15401 if (!cp_parser_error_occurred (parser
) && expr
!= error_mark_node
)
15403 bool non_integral_constant_expression_p
= false;
15404 tree id_expression
= expr
;
15406 const char *error_msg
;
15408 if (identifier_p (expr
))
15409 /* Lookup the name we got back from the id-expression. */
15410 expr
= cp_parser_lookup_name_simple (parser
, expr
,
15411 id_expr_start_token
->location
);
15413 if (expr
&& TREE_CODE (expr
) == TEMPLATE_DECL
)
15414 /* A template without args is not a complete id-expression. */
15415 expr
= error_mark_node
;
15418 && expr
!= error_mark_node
15419 && TREE_CODE (expr
) != TYPE_DECL
15420 && (TREE_CODE (expr
) != BIT_NOT_EXPR
15421 || !TYPE_P (TREE_OPERAND (expr
, 0)))
15422 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
15424 /* Complete lookup of the id-expression. */
15425 expr
= (finish_id_expression
15426 (id_expression
, expr
, parser
->scope
, &idk
,
15427 /*integral_constant_expression_p=*/false,
15428 /*allow_non_integral_constant_expression_p=*/true,
15429 &non_integral_constant_expression_p
,
15430 /*template_p=*/false,
15432 /*address_p=*/false,
15433 /*template_arg_p=*/false,
15435 id_expr_start_token
->location
));
15437 if (expr
== error_mark_node
)
15438 /* We found an id-expression, but it was something that we
15439 should not have found. This is an error, not something
15440 we can recover from, so note that we found an
15441 id-expression and we'll recover as gracefully as
15443 id_expression_or_member_access_p
= true;
15447 && expr
!= error_mark_node
15448 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
15449 /* We have an id-expression. */
15450 id_expression_or_member_access_p
= true;
15453 if (!id_expression_or_member_access_p
)
15455 /* Abort the id-expression parse. */
15456 cp_parser_abort_tentative_parse (parser
);
15458 /* Parsing tentatively, again. */
15459 cp_parser_parse_tentatively (parser
);
15461 /* Parse a class member access. */
15462 expr
= cp_parser_postfix_expression (parser
, /*address_p=*/false,
15463 /*cast_p=*/false, /*decltype*/true,
15464 /*member_access_only_p=*/true, NULL
);
15467 && expr
!= error_mark_node
15468 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_CLOSE_PAREN
)
15469 /* We have an id-expression. */
15470 id_expression_or_member_access_p
= true;
15473 if (id_expression_or_member_access_p
)
15474 /* We have parsed the complete id-expression or member access. */
15475 cp_parser_parse_definitely (parser
);
15478 /* Abort our attempt to parse an id-expression or member access
15480 cp_parser_abort_tentative_parse (parser
);
15482 /* Parse a full expression. */
15483 expr
= cp_parser_expression (parser
, /*pidk=*/NULL
, /*cast_p=*/false,
15484 /*decltype_p=*/true);
15490 /* Parse a `decltype' type. Returns the type.
15492 decltype-specifier:
15493 decltype ( expression )
15495 decltype ( auto ) */
15498 cp_parser_decltype (cp_parser
*parser
)
15500 bool id_expression_or_member_access_p
= false;
15501 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
15503 if (start_token
->type
== CPP_DECLTYPE
)
15505 /* Already parsed. */
15506 cp_lexer_consume_token (parser
->lexer
);
15507 return saved_checks_value (start_token
->u
.tree_check_value
);
15510 /* Look for the `decltype' token. */
15511 if (!cp_parser_require_keyword (parser
, RID_DECLTYPE
, RT_DECLTYPE
))
15512 return error_mark_node
;
15514 /* Parse the opening `('. */
15515 matching_parens parens
;
15516 if (!parens
.require_open (parser
))
15517 return error_mark_node
;
15519 /* Since we're going to preserve any side-effects from this parse, set up a
15520 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15521 in the expression. */
15522 tentative_firewall
firewall (parser
);
15524 /* If in_declarator_p, a reparse as an expression might succeed (60361).
15525 Otherwise, commit now for better diagnostics. */
15526 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
15527 && !parser
->in_declarator_p
)
15528 cp_parser_commit_to_topmost_tentative_parse (parser
);
15530 push_deferring_access_checks (dk_deferred
);
15532 tree expr
= NULL_TREE
;
15534 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
)
15535 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
15537 /* decltype (auto) */
15538 cp_lexer_consume_token (parser
->lexer
);
15539 if (cxx_dialect
< cxx14
)
15541 error_at (start_token
->location
,
15542 "%<decltype(auto)%> type specifier only available with "
15543 "%<-std=c++14%> or %<-std=gnu++14%>");
15544 expr
= error_mark_node
;
15549 /* decltype (expression) */
15551 /* Types cannot be defined in a `decltype' expression. Save away the
15552 old message and set the new one. */
15553 const char *saved_message
= parser
->type_definition_forbidden_message
;
15554 parser
->type_definition_forbidden_message
15555 = G_("types may not be defined in %<decltype%> expressions");
15557 /* The restrictions on constant-expressions do not apply inside
15558 decltype expressions. */
15559 bool saved_integral_constant_expression_p
15560 = parser
->integral_constant_expression_p
;
15561 bool saved_non_integral_constant_expression_p
15562 = parser
->non_integral_constant_expression_p
;
15563 parser
->integral_constant_expression_p
= false;
15565 /* Within a parenthesized expression, a `>' token is always
15566 the greater-than operator. */
15567 bool saved_greater_than_is_operator_p
15568 = parser
->greater_than_is_operator_p
;
15569 parser
->greater_than_is_operator_p
= true;
15571 /* Do not actually evaluate the expression. */
15572 ++cp_unevaluated_operand
;
15574 /* Do not warn about problems with the expression. */
15575 ++c_inhibit_evaluation_warnings
;
15577 expr
= cp_parser_decltype_expr (parser
, id_expression_or_member_access_p
);
15578 STRIP_ANY_LOCATION_WRAPPER (expr
);
15580 /* Go back to evaluating expressions. */
15581 --cp_unevaluated_operand
;
15582 --c_inhibit_evaluation_warnings
;
15584 /* The `>' token might be the end of a template-id or
15585 template-parameter-list now. */
15586 parser
->greater_than_is_operator_p
15587 = saved_greater_than_is_operator_p
;
15589 /* Restore the old message and the integral constant expression
15591 parser
->type_definition_forbidden_message
= saved_message
;
15592 parser
->integral_constant_expression_p
15593 = saved_integral_constant_expression_p
;
15594 parser
->non_integral_constant_expression_p
15595 = saved_non_integral_constant_expression_p
;
15598 /* Parse to the closing `)'. */
15599 if (expr
== error_mark_node
|| !parens
.require_close (parser
))
15601 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
15602 /*consume_paren=*/true);
15603 expr
= error_mark_node
;
15606 /* If we got a parse error while tentative, bail out now. */
15607 if (cp_parser_error_occurred (parser
))
15609 pop_deferring_access_checks ();
15610 return error_mark_node
;
15615 expr
= make_decltype_auto ();
15617 expr
= finish_decltype_type (expr
, id_expression_or_member_access_p
,
15618 tf_warning_or_error
);
15620 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
15622 start_token
->type
= CPP_DECLTYPE
;
15623 start_token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
15624 start_token
->tree_check_p
= true;
15625 start_token
->u
.tree_check_value
->value
= expr
;
15626 start_token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
15627 start_token
->keyword
= RID_MAX
;
15629 location_t loc
= start_token
->location
;
15630 loc
= make_location (loc
, loc
, parser
->lexer
);
15631 start_token
->location
= loc
;
15633 cp_lexer_purge_tokens_after (parser
->lexer
, start_token
);
15635 pop_to_parent_deferring_access_checks ();
15640 /* Special member functions [gram.special] */
15642 /* Parse a conversion-function-id.
15644 conversion-function-id:
15645 operator conversion-type-id
15647 Returns an IDENTIFIER_NODE representing the operator. */
15650 cp_parser_conversion_function_id (cp_parser
* parser
)
15654 tree saved_qualifying_scope
;
15655 tree saved_object_scope
;
15656 tree pushed_scope
= NULL_TREE
;
15658 /* Look for the `operator' token. */
15659 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
15660 return error_mark_node
;
15661 /* When we parse the conversion-type-id, the current scope will be
15662 reset. However, we need that information in able to look up the
15663 conversion function later, so we save it here. */
15664 saved_scope
= parser
->scope
;
15665 saved_qualifying_scope
= parser
->qualifying_scope
;
15666 saved_object_scope
= parser
->object_scope
;
15667 /* We must enter the scope of the class so that the names of
15668 entities declared within the class are available in the
15669 conversion-type-id. For example, consider:
15676 S::operator I() { ... }
15678 In order to see that `I' is a type-name in the definition, we
15679 must be in the scope of `S'. */
15681 pushed_scope
= push_scope (saved_scope
);
15682 /* Parse the conversion-type-id. */
15683 type
= cp_parser_conversion_type_id (parser
);
15684 /* Leave the scope of the class, if any. */
15686 pop_scope (pushed_scope
);
15687 /* Restore the saved scope. */
15688 parser
->scope
= saved_scope
;
15689 parser
->qualifying_scope
= saved_qualifying_scope
;
15690 parser
->object_scope
= saved_object_scope
;
15691 /* If the TYPE is invalid, indicate failure. */
15692 if (type
== error_mark_node
)
15693 return error_mark_node
;
15694 return make_conv_op_name (type
);
15697 /* Parse a conversion-type-id:
15699 conversion-type-id:
15700 type-specifier-seq conversion-declarator [opt]
15702 Returns the TYPE specified. */
15705 cp_parser_conversion_type_id (cp_parser
* parser
)
15708 cp_decl_specifier_seq type_specifiers
;
15709 cp_declarator
*declarator
;
15710 tree type_specified
;
15711 const char *saved_message
;
15713 /* Parse the attributes. */
15714 attributes
= cp_parser_attributes_opt (parser
);
15716 saved_message
= parser
->type_definition_forbidden_message
;
15717 parser
->type_definition_forbidden_message
15718 = G_("types may not be defined in a conversion-type-id");
15720 /* Parse the type-specifiers. DR 2413 clarifies that `typename' is
15721 optional in conversion-type-id. */
15722 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
15723 /*is_declaration=*/false,
15724 /*is_trailing_return=*/false,
15727 parser
->type_definition_forbidden_message
= saved_message
;
15729 /* If that didn't work, stop. */
15730 if (type_specifiers
.type
== error_mark_node
)
15731 return error_mark_node
;
15732 /* Parse the conversion-declarator. */
15733 declarator
= cp_parser_conversion_declarator_opt (parser
);
15735 type_specified
= grokdeclarator (declarator
, &type_specifiers
, TYPENAME
,
15736 /*initialized=*/0, &attributes
);
15738 cplus_decl_attributes (&type_specified
, attributes
, /*flags=*/0);
15740 /* Don't give this error when parsing tentatively. This happens to
15741 work because we always parse this definitively once. */
15742 if (! cp_parser_uncommitted_to_tentative_parse_p (parser
)
15743 && type_uses_auto (type_specified
))
15745 if (cxx_dialect
< cxx14
)
15747 error ("invalid use of %<auto%> in conversion operator");
15748 return error_mark_node
;
15750 else if (template_parm_scope_p ())
15751 warning (0, "use of %<auto%> in member template "
15752 "conversion operator can never be deduced");
15755 return type_specified
;
15758 /* Parse an (optional) conversion-declarator.
15760 conversion-declarator:
15761 ptr-operator conversion-declarator [opt]
15765 static cp_declarator
*
15766 cp_parser_conversion_declarator_opt (cp_parser
* parser
)
15768 enum tree_code code
;
15769 tree class_type
, std_attributes
= NULL_TREE
;
15770 cp_cv_quals cv_quals
;
15772 /* We don't know if there's a ptr-operator next, or not. */
15773 cp_parser_parse_tentatively (parser
);
15774 /* Try the ptr-operator. */
15775 code
= cp_parser_ptr_operator (parser
, &class_type
, &cv_quals
,
15777 /* If it worked, look for more conversion-declarators. */
15778 if (cp_parser_parse_definitely (parser
))
15780 cp_declarator
*declarator
;
15782 /* Parse another optional declarator. */
15783 declarator
= cp_parser_conversion_declarator_opt (parser
);
15785 declarator
= cp_parser_make_indirect_declarator
15786 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
15794 /* Parse an (optional) ctor-initializer.
15797 : mem-initializer-list */
15800 cp_parser_ctor_initializer_opt (cp_parser
* parser
)
15802 /* If the next token is not a `:', then there is no
15803 ctor-initializer. */
15804 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
15806 /* Do default initialization of any bases and members. */
15807 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15808 finish_mem_initializers (NULL_TREE
);
15812 /* Consume the `:' token. */
15813 cp_lexer_consume_token (parser
->lexer
);
15814 /* And the mem-initializer-list. */
15815 cp_parser_mem_initializer_list (parser
);
15818 /* Parse a mem-initializer-list.
15820 mem-initializer-list:
15821 mem-initializer ... [opt]
15822 mem-initializer ... [opt] , mem-initializer-list */
15825 cp_parser_mem_initializer_list (cp_parser
* parser
)
15827 tree mem_initializer_list
= NULL_TREE
;
15828 tree target_ctor
= error_mark_node
;
15829 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15831 /* Let the semantic analysis code know that we are starting the
15832 mem-initializer-list. */
15833 if (!DECL_CONSTRUCTOR_P (current_function_decl
))
15834 error_at (token
->location
,
15835 "only constructors take member initializers");
15837 /* Loop through the list. */
15840 tree mem_initializer
;
15842 token
= cp_lexer_peek_token (parser
->lexer
);
15843 /* Parse the mem-initializer. */
15844 mem_initializer
= cp_parser_mem_initializer (parser
);
15845 /* If the next token is a `...', we're expanding member initializers. */
15846 bool ellipsis
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
15848 || (mem_initializer
!= error_mark_node
15849 && check_for_bare_parameter_packs (TREE_PURPOSE
15850 (mem_initializer
))))
15852 /* Consume the `...'. */
15854 cp_lexer_consume_token (parser
->lexer
);
15856 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
15857 can be expanded but members cannot. */
15858 if (mem_initializer
!= error_mark_node
15859 && !TYPE_P (TREE_PURPOSE (mem_initializer
)))
15861 error_at (token
->location
,
15862 "cannot expand initializer for member %qD",
15863 TREE_PURPOSE (mem_initializer
));
15864 mem_initializer
= error_mark_node
;
15867 /* Construct the pack expansion type. */
15868 if (mem_initializer
!= error_mark_node
)
15869 mem_initializer
= make_pack_expansion (mem_initializer
);
15871 if (target_ctor
!= error_mark_node
15872 && mem_initializer
!= error_mark_node
)
15874 error ("mem-initializer for %qD follows constructor delegation",
15875 TREE_PURPOSE (mem_initializer
));
15876 mem_initializer
= error_mark_node
;
15878 /* Look for a target constructor. */
15879 if (mem_initializer
!= error_mark_node
15880 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer
))
15881 && same_type_p (TREE_PURPOSE (mem_initializer
), current_class_type
))
15883 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS
);
15884 if (mem_initializer_list
)
15886 error ("constructor delegation follows mem-initializer for %qD",
15887 TREE_PURPOSE (mem_initializer_list
));
15888 mem_initializer
= error_mark_node
;
15890 target_ctor
= mem_initializer
;
15892 /* Add it to the list, unless it was erroneous. */
15893 if (mem_initializer
!= error_mark_node
)
15895 TREE_CHAIN (mem_initializer
) = mem_initializer_list
;
15896 mem_initializer_list
= mem_initializer
;
15898 /* If the next token is not a `,', we're done. */
15899 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
15901 /* Consume the `,' token. */
15902 cp_lexer_consume_token (parser
->lexer
);
15905 /* Perform semantic analysis. */
15906 if (DECL_CONSTRUCTOR_P (current_function_decl
))
15907 finish_mem_initializers (mem_initializer_list
);
15910 /* Parse a mem-initializer.
15913 mem-initializer-id ( expression-list [opt] )
15914 mem-initializer-id braced-init-list
15919 ( expression-list [opt] )
15921 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
15922 class) or FIELD_DECL (for a non-static data member) to initialize;
15923 the TREE_VALUE is the expression-list. An empty initialization
15924 list is represented by void_list_node. */
15927 cp_parser_mem_initializer (cp_parser
* parser
)
15929 tree mem_initializer_id
;
15930 tree expression_list
;
15932 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
15934 /* Find out what is being initialized. */
15935 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
15937 permerror (token
->location
,
15938 "anachronistic old-style base class initializer");
15939 mem_initializer_id
= NULL_TREE
;
15943 mem_initializer_id
= cp_parser_mem_initializer_id (parser
);
15944 if (mem_initializer_id
== error_mark_node
)
15945 return mem_initializer_id
;
15947 member
= expand_member_init (mem_initializer_id
);
15948 if (member
&& !DECL_P (member
))
15949 in_base_initializer
= 1;
15951 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
15953 bool expr_non_constant_p
;
15954 cp_lexer_set_source_position (parser
->lexer
);
15955 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
15956 expression_list
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
15957 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
15958 expression_list
= build_tree_list (NULL_TREE
, expression_list
);
15962 vec
<tree
, va_gc
> *vec
;
15963 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
15965 /*allow_expansion_p=*/true,
15966 /*non_constant_p=*/NULL
,
15967 /*close_paren_loc=*/NULL
,
15968 /*wrap_locations_p=*/true);
15970 return error_mark_node
;
15971 expression_list
= build_tree_list_vec (vec
);
15972 release_tree_vector (vec
);
15975 if (expression_list
== error_mark_node
)
15976 return error_mark_node
;
15977 if (!expression_list
)
15978 expression_list
= void_type_node
;
15980 in_base_initializer
= 0;
15983 return error_mark_node
;
15984 tree node
= build_tree_list (member
, expression_list
);
15986 /* We can't attach the source location of this initializer directly to
15987 the list node, so we instead attach it to a dummy EMPTY_CLASS_EXPR
15988 within the TREE_TYPE of the list node. */
15990 = make_location (token
->location
, token
->location
, parser
->lexer
);
15991 tree dummy
= build0 (EMPTY_CLASS_EXPR
, NULL_TREE
);
15992 SET_EXPR_LOCATION (dummy
, loc
);
15993 TREE_TYPE (node
) = dummy
;
15998 /* Parse a mem-initializer-id.
16000 mem-initializer-id:
16001 :: [opt] nested-name-specifier [opt] class-name
16002 decltype-specifier (C++11)
16005 Returns a TYPE indicating the class to be initialized for the first
16006 production (and the second in C++11). Returns an IDENTIFIER_NODE
16007 indicating the data member to be initialized for the last production. */
16010 cp_parser_mem_initializer_id (cp_parser
* parser
)
16012 bool global_scope_p
;
16013 bool nested_name_specifier_p
;
16014 bool template_p
= false;
16017 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16019 /* `typename' is not allowed in this context ([temp.res]). */
16020 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
16022 error_at (token
->location
,
16023 "keyword %<typename%> not allowed in this context (a qualified "
16024 "member initializer is implicitly a type)");
16025 cp_lexer_consume_token (parser
->lexer
);
16027 /* Look for the optional `::' operator. */
16029 = (cp_parser_global_scope_opt (parser
,
16030 /*current_scope_valid_p=*/false)
16032 /* Look for the optional nested-name-specifier. The simplest way to
16037 The keyword `typename' is not permitted in a base-specifier or
16038 mem-initializer; in these contexts a qualified name that
16039 depends on a template-parameter is implicitly assumed to be a
16042 is to assume that we have seen the `typename' keyword at this
16044 nested_name_specifier_p
16045 = (cp_parser_nested_name_specifier_opt (parser
,
16046 /*typename_keyword_p=*/true,
16047 /*check_dependency_p=*/true,
16049 /*is_declaration=*/true)
16051 if (nested_name_specifier_p
)
16052 template_p
= cp_parser_optional_template_keyword (parser
);
16053 /* If there is a `::' operator or a nested-name-specifier, then we
16054 are definitely looking for a class-name. */
16055 if (global_scope_p
|| nested_name_specifier_p
)
16056 return cp_parser_class_name (parser
,
16057 /*typename_keyword_p=*/true,
16058 /*template_keyword_p=*/template_p
,
16060 /*check_dependency_p=*/true,
16061 /*class_head_p=*/false,
16062 /*is_declaration=*/true);
16063 /* Otherwise, we could also be looking for an ordinary identifier. */
16064 cp_parser_parse_tentatively (parser
);
16065 if (cp_lexer_next_token_is_decltype (parser
->lexer
))
16066 /* Try a decltype-specifier. */
16067 id
= cp_parser_decltype (parser
);
16069 /* Otherwise, try a class-name. */
16070 id
= cp_parser_class_name (parser
,
16071 /*typename_keyword_p=*/true,
16072 /*template_keyword_p=*/false,
16074 /*check_dependency_p=*/true,
16075 /*class_head_p=*/false,
16076 /*is_declaration=*/true);
16077 /* If we found one, we're done. */
16078 if (cp_parser_parse_definitely (parser
))
16080 /* Otherwise, look for an ordinary identifier. */
16081 return cp_parser_identifier (parser
);
16084 /* Overloading [gram.over] */
16086 /* Parse an operator-function-id.
16088 operator-function-id:
16091 Returns an IDENTIFIER_NODE for the operator which is a
16092 human-readable spelling of the identifier, e.g., `operator +'. */
16095 cp_parser_operator_function_id (cp_parser
* parser
)
16097 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16098 /* Look for the `operator' keyword. */
16099 if (!cp_parser_require_keyword (parser
, RID_OPERATOR
, RT_OPERATOR
))
16100 return error_mark_node
;
16101 /* And then the name of the operator itself. */
16102 return cp_parser_operator (parser
, start_loc
);
16105 /* Return an identifier node for a user-defined literal operator.
16106 The suffix identifier is chained to the operator name identifier. */
16109 cp_literal_operator_id (const char* name
)
16112 char *buffer
= XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX
)
16113 + strlen (name
) + 10);
16114 sprintf (buffer
, UDLIT_OP_ANSI_FORMAT
, name
);
16115 identifier
= get_identifier (buffer
);
16116 XDELETEVEC (buffer
);
16121 /* Parse an operator.
16124 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
16125 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
16126 || ++ -- , ->* -> () []
16133 Returns an IDENTIFIER_NODE for the operator which is a
16134 human-readable spelling of the identifier, e.g., `operator +'. */
16137 cp_parser_operator (cp_parser
* parser
, location_t start_loc
)
16139 tree id
= NULL_TREE
;
16143 /* Peek at the next token. */
16144 token
= cp_lexer_peek_token (parser
->lexer
);
16146 location_t end_loc
= token
->location
;
16148 /* Figure out which operator we have. */
16149 enum tree_code op
= ERROR_MARK
;
16150 bool assop
= false;
16151 bool consumed
= false;
16152 switch (token
->type
)
16156 /* The keyword should be either `new', `delete' or `co_await'. */
16157 if (token
->keyword
== RID_NEW
)
16159 else if (token
->keyword
== RID_DELETE
)
16161 else if (token
->keyword
== RID_CO_AWAIT
)
16162 op
= CO_AWAIT_EXPR
;
16166 /* Consume the `new', `delete' or co_await token. */
16167 end_loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
16169 /* Peek at the next token. */
16170 token
= cp_lexer_peek_token (parser
->lexer
);
16171 /* If it's a `[' token then this is the array variant of the
16173 if (token
->type
== CPP_OPEN_SQUARE
16174 && op
!= CO_AWAIT_EXPR
)
16176 /* Consume the `[' token. */
16177 cp_lexer_consume_token (parser
->lexer
);
16178 /* Look for the `]' token. */
16179 if (cp_token
*close_token
16180 = cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
16181 end_loc
= close_token
->location
;
16182 op
= op
== NEW_EXPR
? VEC_NEW_EXPR
: VEC_DELETE_EXPR
;
16201 op
= TRUNC_DIV_EXPR
;
16205 op
= TRUNC_MOD_EXPR
;
16225 op
= TRUTH_NOT_EXPR
;
16258 op
= TRUNC_DIV_EXPR
;
16263 op
= TRUNC_MOD_EXPR
;
16289 case CPP_LSHIFT_EQ
:
16294 case CPP_RSHIFT_EQ
:
16311 case CPP_GREATER_EQ
:
16315 case CPP_SPACESHIP
:
16316 op
= SPACESHIP_EXPR
;
16320 op
= TRUTH_ANDIF_EXPR
;
16324 op
= TRUTH_ORIF_EXPR
;
16327 case CPP_PLUS_PLUS
:
16328 op
= POSTINCREMENT_EXPR
;
16331 case CPP_MINUS_MINUS
:
16332 op
= PREDECREMENT_EXPR
;
16336 op
= COMPOUND_EXPR
;
16339 case CPP_DEREF_STAR
:
16344 op
= COMPONENT_REF
;
16349 /* Consume the `?'. */
16350 cp_lexer_consume_token (parser
->lexer
);
16351 /* Look for the matching `:'. */
16352 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
16356 case CPP_OPEN_PAREN
:
16358 /* Consume the `('. */
16359 matching_parens parens
;
16360 parens
.consume_open (parser
);
16361 /* Look for the matching `)'. */
16362 token
= parens
.require_close (parser
);
16364 end_loc
= token
->location
;
16370 case CPP_OPEN_SQUARE
:
16371 /* Consume the `['. */
16372 cp_lexer_consume_token (parser
->lexer
);
16373 /* Look for the matching `]'. */
16374 token
= cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
16376 end_loc
= token
->location
;
16381 case CPP_UTF8STRING
:
16382 case CPP_UTF8STRING_USERDEF
:
16389 case CPP_STRING_USERDEF
:
16390 case CPP_WSTRING_USERDEF
:
16391 case CPP_STRING16_USERDEF
:
16392 case CPP_STRING32_USERDEF
:
16398 if (cxx_dialect
== cxx98
)
16399 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS
);
16401 /* Consume the string. */
16402 str
= cp_parser_string_literal (parser
, /*translate=*/true,
16403 /*wide_ok=*/true, /*lookup_udlit=*/false);
16404 if (str
== error_mark_node
)
16405 return error_mark_node
;
16406 else if (TREE_CODE (str
) == USERDEF_LITERAL
)
16408 string_tree
= USERDEF_LITERAL_VALUE (str
.get_value ());
16409 id
= USERDEF_LITERAL_SUFFIX_ID (str
.get_value ());
16410 end_loc
= str
.get_location ();
16415 /* Look for the suffix identifier. */
16416 token
= cp_lexer_peek_token (parser
->lexer
);
16417 if (token
->type
== CPP_NAME
)
16419 id
= cp_parser_identifier (parser
);
16420 end_loc
= token
->location
;
16422 else if (token
->type
== CPP_KEYWORD
)
16424 error ("unexpected keyword;"
16425 " remove space between quotes and suffix identifier");
16426 return error_mark_node
;
16430 error ("expected suffix identifier");
16431 return error_mark_node
;
16434 sz
= TREE_INT_CST_LOW (TYPE_SIZE_UNIT
16435 (TREE_TYPE (TREE_TYPE (string_tree
))));
16436 len
= TREE_STRING_LENGTH (string_tree
) / sz
- 1;
16439 error ("expected empty string after %<operator%> keyword");
16440 return error_mark_node
;
16442 if (utf8
|| TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree
)))
16445 error ("invalid encoding prefix in literal operator");
16446 return error_mark_node
;
16448 if (id
!= error_mark_node
)
16450 const char *name
= IDENTIFIER_POINTER (id
);
16451 id
= cp_literal_operator_id (name
);
16453 /* Generate a location of the form:
16454 "" _suffix_identifier
16455 ^~~~~~~~~~~~~~~~~~~~~
16456 with caret == start at the start token, finish at the end of the
16457 suffix identifier. */
16458 location_t combined_loc
16459 = make_location (start_loc
, start_loc
, parser
->lexer
);
16460 return cp_expr (id
, combined_loc
);
16464 /* Anything else is an error. */
16468 /* If we have selected an identifier, we need to consume the
16470 if (op
!= ERROR_MARK
)
16472 id
= ovl_op_identifier (assop
, op
);
16474 cp_lexer_consume_token (parser
->lexer
);
16476 /* Otherwise, no valid operator name was present. */
16479 cp_parser_error (parser
, "expected operator");
16480 id
= error_mark_node
;
16483 start_loc
= make_location (start_loc
, start_loc
, get_finish (end_loc
));
16484 return cp_expr (id
, start_loc
);
16487 /* Parse a template-declaration.
16489 template-declaration:
16490 export [opt] template < template-parameter-list > declaration
16492 If MEMBER_P is TRUE, this template-declaration occurs within a
16495 The grammar rule given by the standard isn't correct. What
16496 is really meant is:
16498 template-declaration:
16499 export [opt] template-parameter-list-seq
16500 decl-specifier-seq [opt] init-declarator [opt] ;
16501 export [opt] template-parameter-list-seq
16502 function-definition
16504 template-parameter-list-seq:
16505 template-parameter-list-seq [opt]
16506 template < template-parameter-list >
16508 Concept Extensions:
16510 template-parameter-list-seq:
16511 template < template-parameter-list > requires-clause [opt]
16514 requires logical-or-expression */
16517 cp_parser_template_declaration (cp_parser
* parser
, bool member_p
)
16519 /* Check for `export'. */
16520 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXPORT
))
16522 /* Consume the `export' token. */
16523 cp_lexer_consume_token (parser
->lexer
);
16524 /* Warn that this use of export is deprecated. */
16525 if (cxx_dialect
< cxx11
)
16526 warning (0, "keyword %<export%> not implemented, and will be ignored");
16527 else if (cxx_dialect
< cxx20
)
16528 warning (0, "keyword %<export%> is deprecated, and is ignored");
16530 warning (0, "keyword %<export%> is enabled with %<-fmodules-ts%>");
16533 cp_parser_template_declaration_after_export (parser
, member_p
);
16536 /* Parse a template-parameter-list.
16538 template-parameter-list:
16540 template-parameter-list , template-parameter
16542 Returns a TREE_LIST. Each node represents a template parameter.
16543 The nodes are connected via their TREE_CHAINs. */
16546 cp_parser_template_parameter_list (cp_parser
* parser
)
16548 tree parameter_list
= NULL_TREE
;
16550 /* Don't create wrapper nodes within a template-parameter-list,
16551 since we don't want to have different types based on the
16552 spelling location of constants and decls within them. */
16553 auto_suppress_location_wrappers sentinel
;
16555 begin_template_parm_list ();
16557 /* The loop below parses the template parms. We first need to know
16558 the total number of template parms to be able to compute proper
16559 canonical types of each dependent type. So after the loop, when
16560 we know the total number of template parms,
16561 end_template_parm_list computes the proper canonical types and
16562 fixes up the dependent types accordingly. */
16567 bool is_parameter_pack
;
16568 location_t parm_loc
;
16570 /* Parse the template-parameter. */
16571 parm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
16572 parameter
= cp_parser_template_parameter (parser
,
16574 &is_parameter_pack
);
16575 /* Add it to the list. */
16576 if (parameter
!= error_mark_node
)
16577 parameter_list
= process_template_parm (parameter_list
,
16581 is_parameter_pack
);
16584 tree err_parm
= build_tree_list (parameter
, parameter
);
16585 parameter_list
= chainon (parameter_list
, err_parm
);
16588 /* If the next token is not a `,', we're done. */
16589 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16591 /* Otherwise, consume the `,' token. */
16592 cp_lexer_consume_token (parser
->lexer
);
16595 return end_template_parm_list (parameter_list
);
16598 /* Parse a introduction-list.
16601 introduced-parameter
16602 introduction-list , introduced-parameter
16604 introduced-parameter:
16605 ...[opt] identifier
16607 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
16608 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
16609 WILDCARD_DECL will also have DECL_NAME set and token location in
16610 DECL_SOURCE_LOCATION. */
16613 cp_parser_introduction_list (cp_parser
*parser
)
16615 vec
<tree
, va_gc
> *introduction_vec
= make_tree_vector ();
16619 bool is_pack
= cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
);
16621 cp_lexer_consume_token (parser
->lexer
);
16623 tree identifier
= cp_parser_identifier (parser
);
16624 if (identifier
== error_mark_node
)
16627 /* Build placeholder. */
16628 tree parm
= build_nt (WILDCARD_DECL
);
16629 DECL_SOURCE_LOCATION (parm
)
16630 = cp_lexer_peek_token (parser
->lexer
)->location
;
16631 DECL_NAME (parm
) = identifier
;
16632 WILDCARD_PACK_P (parm
) = is_pack
;
16633 vec_safe_push (introduction_vec
, parm
);
16635 /* If the next token is not a `,', we're done. */
16636 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
16638 /* Otherwise, consume the `,' token. */
16639 cp_lexer_consume_token (parser
->lexer
);
16642 /* Convert the vec into a TREE_VEC. */
16643 tree introduction_list
= make_tree_vec (introduction_vec
->length ());
16646 FOR_EACH_VEC_ELT (*introduction_vec
, n
, parm
)
16647 TREE_VEC_ELT (introduction_list
, n
) = parm
;
16649 release_tree_vector (introduction_vec
);
16650 return introduction_list
;
16653 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
16654 is an abstract declarator. */
16656 static inline cp_declarator
*
16657 get_id_declarator (cp_declarator
*declarator
)
16659 cp_declarator
*d
= declarator
;
16660 while (d
&& d
->kind
!= cdk_id
)
16665 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
16666 is an abstract declarator. */
16669 get_unqualified_id (cp_declarator
*declarator
)
16671 declarator
= get_id_declarator (declarator
);
16673 return declarator
->u
.id
.unqualified_name
;
16678 /* Returns true if TYPE would declare a constrained constrained-parameter. */
16681 is_constrained_parameter (tree type
)
16684 && TREE_CODE (type
) == TYPE_DECL
16685 && CONSTRAINED_PARM_CONCEPT (type
)
16686 && DECL_P (CONSTRAINED_PARM_CONCEPT (type
)));
16689 /* Returns true if PARM declares a constrained-parameter. */
16692 is_constrained_parameter (cp_parameter_declarator
*parm
)
16694 return is_constrained_parameter (parm
->decl_specifiers
.type
);
16697 /* Check that the type parameter is only a declarator-id, and that its
16698 type is not cv-qualified. */
16701 cp_parser_check_constrained_type_parm (cp_parser
*parser
,
16702 cp_parameter_declarator
*parm
)
16704 if (!parm
->declarator
)
16707 if (parm
->declarator
->kind
!= cdk_id
)
16709 cp_parser_error (parser
, "invalid constrained type parameter");
16713 /* Don't allow cv-qualified type parameters. */
16714 if (decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_const
)
16715 || decl_spec_seq_has_spec_p (&parm
->decl_specifiers
, ds_volatile
))
16717 cp_parser_error (parser
, "cv-qualified type parameter");
16724 /* Finish parsing/processing a template type parameter and checking
16725 various restrictions. */
16728 cp_parser_constrained_type_template_parm (cp_parser
*parser
,
16730 cp_parameter_declarator
* parmdecl
)
16732 if (cp_parser_check_constrained_type_parm (parser
, parmdecl
))
16733 return finish_template_type_parm (class_type_node
, id
);
16735 return error_mark_node
;
16739 finish_constrained_template_template_parm (tree proto
, tree id
)
16741 /* FIXME: This should probably be copied, and we may need to adjust
16742 the template parameter depths. */
16743 tree saved_parms
= current_template_parms
;
16744 begin_template_parm_list ();
16745 current_template_parms
= DECL_TEMPLATE_PARMS (proto
);
16746 end_template_parm_list ();
16748 tree parm
= finish_template_template_parm (class_type_node
, id
);
16749 current_template_parms
= saved_parms
;
16754 /* Finish parsing/processing a template template parameter by borrowing
16755 the template parameter list from the prototype parameter. */
16758 cp_parser_constrained_template_template_parm (cp_parser
*parser
,
16761 cp_parameter_declarator
*parmdecl
)
16763 if (!cp_parser_check_constrained_type_parm (parser
, parmdecl
))
16764 return error_mark_node
;
16765 return finish_constrained_template_template_parm (proto
, id
);
16768 /* Create a new non-type template parameter from the given PARM
16772 cp_parser_constrained_non_type_template_parm (bool *is_non_type
,
16773 cp_parameter_declarator
*parm
)
16775 *is_non_type
= true;
16776 cp_declarator
*decl
= parm
->declarator
;
16777 cp_decl_specifier_seq
*specs
= &parm
->decl_specifiers
;
16778 specs
->type
= TREE_TYPE (DECL_INITIAL (specs
->type
));
16779 return grokdeclarator (decl
, specs
, TPARM
, 0, NULL
);
16782 /* Build a constrained template parameter based on the PARMDECL
16783 declarator. The type of PARMDECL is the constrained type, which
16784 refers to the prototype template parameter that ultimately
16785 specifies the type of the declared parameter. */
16788 finish_constrained_parameter (cp_parser
*parser
,
16789 cp_parameter_declarator
*parmdecl
,
16792 tree decl
= parmdecl
->decl_specifiers
.type
;
16793 tree id
= get_unqualified_id (parmdecl
->declarator
);
16794 tree def
= parmdecl
->default_argument
;
16795 tree proto
= DECL_INITIAL (decl
);
16797 /* Build the parameter. Return an error if the declarator was invalid. */
16799 if (TREE_CODE (proto
) == TYPE_DECL
)
16800 parm
= cp_parser_constrained_type_template_parm (parser
, id
, parmdecl
);
16801 else if (TREE_CODE (proto
) == TEMPLATE_DECL
)
16802 parm
= cp_parser_constrained_template_template_parm (parser
, proto
, id
,
16805 parm
= cp_parser_constrained_non_type_template_parm (is_non_type
, parmdecl
);
16806 if (parm
== error_mark_node
)
16807 return error_mark_node
;
16809 /* Finish the parameter decl and create a node attaching the
16810 default argument and constraint. */
16811 parm
= build_tree_list (def
, parm
);
16812 TEMPLATE_PARM_CONSTRAINTS (parm
) = decl
;
16817 /* Returns true if the parsed type actually represents the declaration
16818 of a type template-parameter. */
16821 declares_constrained_type_template_parameter (tree type
)
16823 return (is_constrained_parameter (type
)
16824 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TYPE_PARM
);
16827 /* Returns true if the parsed type actually represents the declaration of
16828 a template template-parameter. */
16831 declares_constrained_template_template_parameter (tree type
)
16833 return (is_constrained_parameter (type
)
16834 && TREE_CODE (TREE_TYPE (type
)) == TEMPLATE_TEMPLATE_PARM
);
16837 /* Parse a default argument for a type template-parameter.
16838 Note that diagnostics are handled in cp_parser_template_parameter. */
16841 cp_parser_default_type_template_argument (cp_parser
*parser
)
16843 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16845 /* Consume the `=' token. */
16846 cp_lexer_consume_token (parser
->lexer
);
16848 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
16850 /* Parse the default-argument. */
16851 push_deferring_access_checks (dk_no_deferred
);
16852 tree default_argument
= cp_parser_type_id (parser
,
16853 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16855 pop_deferring_access_checks ();
16857 if (flag_concepts
&& type_uses_auto (default_argument
))
16859 error_at (token
->location
,
16860 "invalid use of %<auto%> in default template argument");
16861 return error_mark_node
;
16864 return default_argument
;
16867 /* Parse a default argument for a template template-parameter. */
16870 cp_parser_default_template_template_argument (cp_parser
*parser
)
16872 gcc_assert (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
16876 /* Consume the `='. */
16877 cp_lexer_consume_token (parser
->lexer
);
16878 /* Parse the id-expression. */
16879 push_deferring_access_checks (dk_no_deferred
);
16880 /* save token before parsing the id-expression, for error
16882 const cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
16883 tree default_argument
16884 = cp_parser_id_expression (parser
,
16885 /*template_keyword_p=*/false,
16886 /*check_dependency_p=*/true,
16887 /*template_p=*/&is_template
,
16888 /*declarator_p=*/false,
16889 /*optional_p=*/false);
16890 if (TREE_CODE (default_argument
) == TYPE_DECL
)
16891 /* If the id-expression was a template-id that refers to
16892 a template-class, we already have the declaration here,
16893 so no further lookup is needed. */
16896 /* Look up the name. */
16898 = cp_parser_lookup_name (parser
, default_argument
,
16900 /*is_template=*/is_template
,
16901 /*is_namespace=*/false,
16902 /*check_dependency=*/true,
16903 /*ambiguous_decls=*/NULL
,
16905 /* See if the default argument is valid. */
16906 default_argument
= check_template_template_default_arg (default_argument
);
16907 pop_deferring_access_checks ();
16908 return default_argument
;
16911 /* Parse a template-parameter.
16913 template-parameter:
16915 parameter-declaration
16917 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
16918 the parameter. The TREE_PURPOSE is the default value, if any.
16919 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
16920 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
16921 set to true iff this parameter is a parameter pack. */
16924 cp_parser_template_parameter (cp_parser
* parser
, bool *is_non_type
,
16925 bool *is_parameter_pack
)
16928 cp_parameter_declarator
*parameter_declarator
;
16931 /* Assume it is a type parameter or a template parameter. */
16932 *is_non_type
= false;
16933 /* Assume it not a parameter pack. */
16934 *is_parameter_pack
= false;
16935 /* Peek at the next token. */
16936 token
= cp_lexer_peek_token (parser
->lexer
);
16937 /* If it is `template', we have a type-parameter. */
16938 if (token
->keyword
== RID_TEMPLATE
)
16939 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16940 /* If it is `class' or `typename' we do not know yet whether it is a
16941 type parameter or a non-type parameter. Consider:
16943 template <typename T, typename T::X X> ...
16947 template <class C, class D*> ...
16949 Here, the first parameter is a type parameter, and the second is
16950 a non-type parameter. We can tell by looking at the token after
16951 the identifier -- if it is a `,', `=', or `>' then we have a type
16953 if (token
->keyword
== RID_TYPENAME
|| token
->keyword
== RID_CLASS
)
16955 /* Peek at the token after `class' or `typename'. */
16956 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
16957 /* If it's an ellipsis, we have a template type parameter
16959 if (token
->type
== CPP_ELLIPSIS
)
16960 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16961 /* If it's an identifier, skip it. */
16962 if (token
->type
== CPP_NAME
)
16963 token
= cp_lexer_peek_nth_token (parser
->lexer
, 3);
16964 /* Now, see if the token looks like the end of a template
16966 if (token
->type
== CPP_COMMA
16967 || token
->type
== CPP_EQ
16968 || token
->type
== CPP_GREATER
)
16969 return cp_parser_type_parameter (parser
, is_parameter_pack
);
16972 /* Otherwise, it is a non-type parameter or a constrained parameter.
16976 When parsing a default template-argument for a non-type
16977 template-parameter, the first non-nested `>' is taken as the end
16978 of the template parameter-list rather than a greater-than
16980 parameter_declarator
16981 = cp_parser_parameter_declaration (parser
,
16982 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
16983 /*template_parm_p=*/true,
16984 /*parenthesized_p=*/NULL
);
16986 if (!parameter_declarator
)
16987 return error_mark_node
;
16989 /* If the parameter declaration is marked as a parameter pack, set
16990 *IS_PARAMETER_PACK to notify the caller. */
16991 if (parameter_declarator
->template_parameter_pack_p
)
16992 *is_parameter_pack
= true;
16994 if (parameter_declarator
->default_argument
)
16996 /* Can happen in some cases of erroneous input (c++/34892). */
16997 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
16998 /* Consume the `...' for better error recovery. */
16999 cp_lexer_consume_token (parser
->lexer
);
17002 /* The parameter may have been constrained type parameter. */
17003 if (is_constrained_parameter (parameter_declarator
))
17004 return finish_constrained_parameter (parser
,
17005 parameter_declarator
,
17008 // Now we're sure that the parameter is a non-type parameter.
17009 *is_non_type
= true;
17011 parm
= grokdeclarator (parameter_declarator
->declarator
,
17012 ¶meter_declarator
->decl_specifiers
,
17013 TPARM
, /*initialized=*/0,
17014 /*attrlist=*/NULL
);
17015 if (parm
== error_mark_node
)
17016 return error_mark_node
;
17018 return build_tree_list (parameter_declarator
->default_argument
, parm
);
17021 /* Parse a type-parameter.
17024 class identifier [opt]
17025 class identifier [opt] = type-id
17026 typename identifier [opt]
17027 typename identifier [opt] = type-id
17028 template < template-parameter-list > class identifier [opt]
17029 template < template-parameter-list > class identifier [opt]
17032 GNU Extension (variadic templates):
17035 class ... identifier [opt]
17036 typename ... identifier [opt]
17038 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
17039 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
17040 the declaration of the parameter.
17042 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
17045 cp_parser_type_parameter (cp_parser
* parser
, bool *is_parameter_pack
)
17050 /* Look for a keyword to tell us what kind of parameter this is. */
17051 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_TYPENAME_TEMPLATE
);
17053 return error_mark_node
;
17055 switch (token
->keyword
)
17061 tree default_argument
;
17063 /* If the next token is an ellipsis, we have a template
17065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17067 /* Consume the `...' token. */
17068 cp_lexer_consume_token (parser
->lexer
);
17069 maybe_warn_variadic_templates ();
17071 *is_parameter_pack
= true;
17074 /* If the next token is an identifier, then it names the
17076 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17077 identifier
= cp_parser_identifier (parser
);
17079 identifier
= NULL_TREE
;
17081 /* Create the parameter. */
17082 parameter
= finish_template_type_parm (class_type_node
, identifier
);
17084 /* If the next token is an `=', we have a default argument. */
17085 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17088 = cp_parser_default_type_template_argument (parser
);
17090 /* Template parameter packs cannot have default
17092 if (*is_parameter_pack
)
17095 error_at (token
->location
,
17096 "template parameter pack %qD cannot have a "
17097 "default argument", identifier
);
17099 error_at (token
->location
,
17100 "template parameter packs cannot have "
17101 "default arguments");
17102 default_argument
= NULL_TREE
;
17104 else if (check_for_bare_parameter_packs (default_argument
))
17105 default_argument
= error_mark_node
;
17108 default_argument
= NULL_TREE
;
17110 /* Create the combined representation of the parameter and the
17111 default argument. */
17112 parameter
= build_tree_list (default_argument
, parameter
);
17119 tree default_argument
;
17121 /* Look for the `<'. */
17122 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
17123 /* Parse the template-parameter-list. */
17124 cp_parser_template_parameter_list (parser
);
17125 /* Look for the `>'. */
17126 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
17128 /* If template requirements are present, parse them. */
17131 tree reqs
= get_shorthand_constraints (current_template_parms
);
17132 if (tree dreqs
= cp_parser_requires_clause_opt (parser
, false))
17133 reqs
= combine_constraint_expressions (reqs
, dreqs
);
17134 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
17137 /* Look for the `class' or 'typename' keywords. */
17138 cp_parser_type_parameter_key (parser
);
17139 /* If the next token is an ellipsis, we have a template
17141 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17143 /* Consume the `...' token. */
17144 cp_lexer_consume_token (parser
->lexer
);
17145 maybe_warn_variadic_templates ();
17147 *is_parameter_pack
= true;
17149 /* If the next token is an `=', then there is a
17150 default-argument. If the next token is a `>', we are at
17151 the end of the parameter-list. If the next token is a `,',
17152 then we are at the end of this parameter. */
17153 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
17154 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_GREATER
)
17155 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
17157 identifier
= cp_parser_identifier (parser
);
17158 /* Treat invalid names as if the parameter were nameless. */
17159 if (identifier
== error_mark_node
)
17160 identifier
= NULL_TREE
;
17163 identifier
= NULL_TREE
;
17165 /* Create the template parameter. */
17166 parameter
= finish_template_template_parm (class_type_node
,
17169 /* If the next token is an `=', then there is a
17170 default-argument. */
17171 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
17174 = cp_parser_default_template_template_argument (parser
);
17176 /* Template parameter packs cannot have default
17178 if (*is_parameter_pack
)
17181 error_at (token
->location
,
17182 "template parameter pack %qD cannot "
17183 "have a default argument",
17186 error_at (token
->location
, "template parameter packs cannot "
17187 "have default arguments");
17188 default_argument
= NULL_TREE
;
17192 default_argument
= NULL_TREE
;
17194 /* Create the combined representation of the parameter and the
17195 default argument. */
17196 parameter
= build_tree_list (default_argument
, parameter
);
17201 gcc_unreachable ();
17208 /* Parse a template-id.
17211 template-name < template-argument-list [opt] >
17213 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
17214 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
17215 returned. Otherwise, if the template-name names a function, or set
17216 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
17217 names a class, returns a TYPE_DECL for the specialization.
17219 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
17220 uninstantiated templates. */
17223 cp_parser_template_id (cp_parser
*parser
,
17224 bool template_keyword_p
,
17225 bool check_dependency_p
,
17226 enum tag_types tag_type
,
17227 bool is_declaration
)
17232 cp_token_position start_of_id
= 0;
17233 cp_token
*next_token
= NULL
, *next_token_2
= NULL
;
17234 bool is_identifier
;
17236 /* If the next token corresponds to a template-id, there is no need
17238 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17240 if (token
->type
== CPP_TEMPLATE_ID
)
17242 cp_lexer_consume_token (parser
->lexer
);
17243 return saved_checks_value (token
->u
.tree_check_value
);
17246 /* Avoid performing name lookup if there is no possibility of
17247 finding a template-id. */
17248 if ((token
->type
!= CPP_NAME
&& token
->keyword
!= RID_OPERATOR
)
17249 || (token
->type
== CPP_NAME
17250 && !cp_parser_nth_token_starts_template_argument_list_p
17253 cp_parser_error (parser
, "expected template-id");
17254 return error_mark_node
;
17257 /* Remember where the template-id starts. */
17258 if (cp_parser_uncommitted_to_tentative_parse_p (parser
))
17259 start_of_id
= cp_lexer_token_position (parser
->lexer
, false);
17261 push_deferring_access_checks (dk_deferred
);
17263 /* Parse the template-name. */
17264 is_identifier
= false;
17265 templ
= cp_parser_template_name (parser
, template_keyword_p
,
17266 check_dependency_p
,
17271 /* Push any access checks inside the firewall we're about to create. */
17272 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
17273 pop_deferring_access_checks ();
17274 if (templ
== error_mark_node
|| is_identifier
)
17277 /* Since we're going to preserve any side-effects from this parse, set up a
17278 firewall to protect our callers from cp_parser_commit_to_tentative_parse
17279 in the template arguments. */
17280 tentative_firewall
firewall (parser
);
17281 reopen_deferring_access_checks (checks
);
17283 /* If we find the sequence `[:' after a template-name, it's probably
17284 a digraph-typo for `< ::'. Substitute the tokens and check if we can
17285 parse correctly the argument list. */
17286 if (((next_token
= cp_lexer_peek_token (parser
->lexer
))->type
17287 == CPP_OPEN_SQUARE
)
17288 && next_token
->flags
& DIGRAPH
17289 && ((next_token_2
= cp_lexer_peek_nth_token (parser
->lexer
, 2))->type
17291 && !(next_token_2
->flags
& PREV_WHITE
))
17293 cp_parser_parse_tentatively (parser
);
17294 /* Change `:' into `::'. */
17295 next_token_2
->type
= CPP_SCOPE
;
17296 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
17298 cp_lexer_consume_token (parser
->lexer
);
17300 /* Parse the arguments. */
17301 arguments
= cp_parser_enclosed_template_argument_list (parser
);
17302 if (!cp_parser_parse_definitely (parser
))
17304 /* If we couldn't parse an argument list, then we revert our changes
17305 and return simply an error. Maybe this is not a template-id
17307 next_token_2
->type
= CPP_COLON
;
17308 cp_parser_error (parser
, "expected %<<%>");
17309 pop_deferring_access_checks ();
17310 return error_mark_node
;
17312 /* Otherwise, emit an error about the invalid digraph, but continue
17313 parsing because we got our argument list. */
17314 if (permerror (next_token
->location
,
17315 "%<<::%> cannot begin a template-argument list"))
17317 static bool hint
= false;
17318 inform (next_token
->location
,
17319 "%<<:%> is an alternate spelling for %<[%>."
17320 " Insert whitespace between %<<%> and %<::%>");
17321 if (!hint
&& !flag_permissive
)
17323 inform (next_token
->location
, "(if you use %<-fpermissive%> "
17324 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
17325 "accept your code)");
17332 /* Look for the `<' that starts the template-argument-list. */
17333 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
17335 pop_deferring_access_checks ();
17336 return error_mark_node
;
17338 /* Parse the arguments. */
17339 arguments
= cp_parser_enclosed_template_argument_list (parser
);
17341 if ((cxx_dialect
> cxx17
)
17342 && (TREE_CODE (templ
) == FUNCTION_DECL
|| identifier_p (templ
))
17343 && !template_keyword_p
17344 && (cp_parser_error_occurred (parser
)
17345 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
)))
17347 /* This didn't go well. */
17348 if (TREE_CODE (templ
) == FUNCTION_DECL
)
17350 /* C++20 says that "function-name < a;" is now ill-formed. */
17351 if (cp_parser_error_occurred (parser
))
17353 error_at (token
->location
, "invalid template-argument-list");
17354 inform (token
->location
, "function name as the left hand "
17355 "operand of %<<%> is ill-formed in C++20; wrap the "
17356 "function name in %<()%>");
17359 /* We expect "f<targs>" to be followed by "(args)". */
17360 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
17361 "expected %<(%> after template-argument-list");
17363 /* Purge all subsequent tokens. */
17364 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
17367 cp_parser_simulate_error (parser
);
17368 pop_deferring_access_checks ();
17369 return error_mark_node
;
17373 /* Set the location to be of the form:
17374 template-name < template-argument-list [opt] >
17375 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17376 with caret == start at the start of the template-name,
17377 ranging until the closing '>'. */
17378 location_t combined_loc
17379 = make_location (token
->location
, token
->location
, parser
->lexer
);
17381 /* Check for concepts autos where they don't belong. We could
17382 identify types in some cases of identifier TEMPL, looking ahead
17383 for a CPP_SCOPE, but that would buy us nothing: we accept auto in
17384 types. We reject them in functions, but if what we have is an
17385 identifier, even with none_type we can't conclude it's NOT a
17386 type, we have to wait for template substitution. */
17387 if (flag_concepts
&& check_auto_in_tmpl_args (templ
, arguments
))
17388 template_id
= error_mark_node
;
17389 /* Build a representation of the specialization. */
17390 else if (identifier_p (templ
))
17391 template_id
= build_min_nt_loc (combined_loc
,
17394 else if (DECL_TYPE_TEMPLATE_P (templ
)
17395 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ
))
17397 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
17398 template (rather than some instantiation thereof) only if
17399 is not nested within some other construct. For example, in
17400 "template <typename T> void f(T) { A<T>::", A<T> is just an
17401 instantiation of A. */
17402 bool entering_scope
17403 = (template_parm_scope_p ()
17404 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
));
17406 = finish_template_type (templ
, arguments
, entering_scope
);
17408 else if (concept_definition_p (templ
))
17410 /* The caller will decide whether this is a concept check or type
17412 template_id
= build2_loc (combined_loc
, TEMPLATE_ID_EXPR
,
17413 boolean_type_node
, templ
, arguments
);
17415 else if (variable_template_p (templ
))
17417 template_id
= lookup_template_variable (templ
, arguments
);
17418 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
17419 SET_EXPR_LOCATION (template_id
, combined_loc
);
17423 /* If it's not a class-template or a template-template, it should be
17424 a function-template. */
17425 gcc_assert (OVL_P (templ
) || BASELINK_P (templ
));
17427 template_id
= lookup_template_function (templ
, arguments
);
17428 if (TREE_CODE (template_id
) == TEMPLATE_ID_EXPR
)
17429 SET_EXPR_LOCATION (template_id
, combined_loc
);
17432 /* If parsing tentatively, replace the sequence of tokens that makes
17433 up the template-id with a CPP_TEMPLATE_ID token. That way,
17434 should we re-parse the token stream, we will not have to repeat
17435 the effort required to do the parse, nor will we issue duplicate
17436 error messages about problems during instantiation of the
17439 /* Don't do this if we had a parse error in a declarator; re-parsing
17440 might succeed if a name changes meaning (60361). */
17441 && !(cp_parser_error_occurred (parser
)
17442 && cp_parser_parsing_tentatively (parser
)
17443 && parser
->in_declarator_p
))
17445 /* Reset the contents of the START_OF_ID token. */
17446 token
->type
= CPP_TEMPLATE_ID
;
17447 token
->location
= combined_loc
;
17449 /* Retrieve any deferred checks. Do not pop this access checks yet
17450 so the memory will not be reclaimed during token replacing below. */
17451 token
->u
.tree_check_value
= ggc_cleared_alloc
<struct tree_check
> ();
17452 token
->tree_check_p
= true;
17453 token
->u
.tree_check_value
->value
= template_id
;
17454 token
->u
.tree_check_value
->checks
= get_deferred_access_checks ();
17455 token
->keyword
= RID_MAX
;
17457 /* Purge all subsequent tokens. */
17458 cp_lexer_purge_tokens_after (parser
->lexer
, start_of_id
);
17460 /* ??? Can we actually assume that, if template_id ==
17461 error_mark_node, we will have issued a diagnostic to the
17462 user, as opposed to simply marking the tentative parse as
17464 if (cp_parser_error_occurred (parser
) && template_id
!= error_mark_node
)
17465 error_at (token
->location
, "parse error in template argument list");
17468 pop_to_parent_deferring_access_checks ();
17469 return template_id
;
17472 /* Like cp_parser_template_id, called in non-type context. */
17475 cp_parser_template_id_expr (cp_parser
*parser
,
17476 bool template_keyword_p
,
17477 bool check_dependency_p
,
17478 bool is_declaration
)
17480 tree x
= cp_parser_template_id (parser
, template_keyword_p
, check_dependency_p
,
17481 none_type
, is_declaration
);
17482 if (TREE_CODE (x
) == TEMPLATE_ID_EXPR
17483 && concept_check_p (x
))
17484 /* We didn't check the arguments in cp_parser_template_id; do that now. */
17485 return build_concept_id (x
);
17489 /* Parse a template-name.
17494 The standard should actually say:
17498 operator-function-id
17500 A defect report has been filed about this issue.
17502 A conversion-function-id cannot be a template name because they cannot
17503 be part of a template-id. In fact, looking at this code:
17505 a.operator K<int>()
17507 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
17508 It is impossible to call a templated conversion-function-id with an
17509 explicit argument list, since the only allowed template parameter is
17510 the type to which it is converting.
17512 If TEMPLATE_KEYWORD_P is true, then we have just seen the
17513 `template' keyword, in a construction like:
17517 In that case `f' is taken to be a template-name, even though there
17518 is no way of knowing for sure.
17520 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
17521 name refers to a set of overloaded functions, at least one of which
17522 is a template, or an IDENTIFIER_NODE with the name of the template,
17523 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
17524 names are looked up inside uninstantiated templates. */
17527 cp_parser_template_name (cp_parser
* parser
,
17528 bool template_keyword_p
,
17529 bool check_dependency_p
,
17530 bool is_declaration
,
17531 enum tag_types tag_type
,
17532 bool *is_identifier
)
17536 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17538 /* If the next token is `operator', then we have either an
17539 operator-function-id or a conversion-function-id. */
17540 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_OPERATOR
))
17542 /* We don't know whether we're looking at an
17543 operator-function-id or a conversion-function-id. */
17544 cp_parser_parse_tentatively (parser
);
17545 /* Try an operator-function-id. */
17546 identifier
= cp_parser_operator_function_id (parser
);
17547 /* If that didn't work, try a conversion-function-id. */
17548 if (!cp_parser_parse_definitely (parser
))
17550 cp_parser_error (parser
, "expected template-name");
17551 return error_mark_node
;
17554 /* Look for the identifier. */
17556 identifier
= cp_parser_identifier (parser
);
17558 /* If we didn't find an identifier, we don't have a template-id. */
17559 if (identifier
== error_mark_node
)
17560 return error_mark_node
;
17562 /* If the name immediately followed the `template' keyword, then it
17563 is a template-name. However, if the next token is not `<', then
17564 we do not treat it as a template-name, since it is not being used
17565 as part of a template-id. This enables us to handle constructs
17568 template <typename T> struct S { S(); };
17569 template <typename T> S<T>::S();
17571 correctly. We would treat `S' as a template -- if it were `S<T>'
17572 -- but we do not if there is no `<'. */
17574 if (processing_template_decl
17575 && cp_parser_nth_token_starts_template_argument_list_p (parser
, 1))
17577 /* In a declaration, in a dependent context, we pretend that the
17578 "template" keyword was present in order to improve error
17579 recovery. For example, given:
17581 template <typename T> void f(T::X<int>);
17583 we want to treat "X<int>" as a template-id. */
17585 && !template_keyword_p
17586 && parser
->scope
&& TYPE_P (parser
->scope
)
17587 && check_dependency_p
17588 && dependent_scope_p (parser
->scope
)
17589 /* Do not do this for dtors (or ctors), since they never
17590 need the template keyword before their name. */
17591 && !constructor_name_p (identifier
, parser
->scope
))
17593 cp_token_position start
= 0;
17595 /* Explain what went wrong. */
17596 error_at (token
->location
, "non-template %qD used as template",
17598 inform (token
->location
, "use %<%T::template %D%> to indicate that it is a template",
17599 parser
->scope
, identifier
);
17600 /* If parsing tentatively, find the location of the "<" token. */
17601 if (cp_parser_simulate_error (parser
))
17602 start
= cp_lexer_token_position (parser
->lexer
, true);
17603 /* Parse the template arguments so that we can issue error
17604 messages about them. */
17605 cp_lexer_consume_token (parser
->lexer
);
17606 cp_parser_enclosed_template_argument_list (parser
);
17607 /* Skip tokens until we find a good place from which to
17608 continue parsing. */
17609 cp_parser_skip_to_closing_parenthesis (parser
,
17610 /*recovering=*/true,
17612 /*consume_paren=*/false);
17613 /* If parsing tentatively, permanently remove the
17614 template argument list. That will prevent duplicate
17615 error messages from being issued about the missing
17616 "template" keyword. */
17618 cp_lexer_purge_tokens_after (parser
->lexer
, start
);
17620 *is_identifier
= true;
17621 parser
->context
->object_type
= NULL_TREE
;
17625 /* If the "template" keyword is present, then there is generally
17626 no point in doing name-lookup, so we just return IDENTIFIER.
17627 But, if the qualifying scope is non-dependent then we can
17628 (and must) do name-lookup normally. */
17629 if (template_keyword_p
)
17631 tree scope
= (parser
->scope
? parser
->scope
17632 : parser
->context
->object_type
);
17633 if (scope
&& TYPE_P (scope
)
17634 && (!CLASS_TYPE_P (scope
)
17635 || (check_dependency_p
&& dependent_type_p (scope
))))
17637 /* We're optimizing away the call to cp_parser_lookup_name, but
17638 we still need to do this. */
17639 parser
->context
->object_type
= NULL_TREE
;
17645 /* cp_parser_lookup_name clears OBJECT_TYPE. */
17646 const bool scoped_p
= ((parser
->scope
? parser
->scope
17647 : parser
->context
->object_type
) != NULL_TREE
);
17649 /* Look up the name. */
17650 decl
= cp_parser_lookup_name (parser
, identifier
,
17652 /*is_template=*/true,
17653 /*is_namespace=*/false,
17654 check_dependency_p
,
17655 /*ambiguous_decls=*/NULL
,
17658 decl
= strip_using_decl (decl
);
17660 /* If DECL is a template, then the name was a template-name. */
17661 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
17663 if (TREE_DEPRECATED (decl
)
17664 && deprecated_state
!= DEPRECATED_SUPPRESS
)
17666 tree d
= DECL_TEMPLATE_RESULT (decl
);
17668 if (TREE_CODE (d
) == TYPE_DECL
)
17669 attr
= lookup_attribute ("deprecated",
17670 TYPE_ATTRIBUTES (TREE_TYPE (d
)));
17672 attr
= lookup_attribute ("deprecated",
17673 DECL_ATTRIBUTES (d
));
17674 warn_deprecated_use (decl
, attr
);
17679 /* The standard does not explicitly indicate whether a name that
17680 names a set of overloaded declarations, some of which are
17681 templates, is a template-name. However, such a name should
17682 be a template-name; otherwise, there is no way to form a
17683 template-id for the overloaded templates. */
17684 bool found
= false;
17686 for (lkp_iterator
iter (MAYBE_BASELINK_FUNCTIONS (decl
));
17687 !found
&& iter
; ++iter
)
17688 if (TREE_CODE (*iter
) == TEMPLATE_DECL
)
17692 && (cxx_dialect
> cxx17
)
17694 && cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
)
17695 && tag_type
== none_type
)
17697 /* [temp.names] says "A name is also considered to refer to a template
17698 if it is an unqualified-id followed by a < and name lookup finds
17699 either one or more functions or finds nothing." */
17701 /* The "more functions" case. Just use the OVERLOAD as normally.
17702 We don't use is_overloaded_fn here to avoid considering
17704 if (TREE_CODE (decl
) == OVERLOAD
17705 /* Name lookup found one function. */
17706 || TREE_CODE (decl
) == FUNCTION_DECL
)
17708 /* Name lookup found nothing. */
17709 else if (decl
== error_mark_node
)
17715 /* The name does not name a template. */
17716 cp_parser_error (parser
, "expected template-name");
17717 return error_mark_node
;
17724 /* Parse a template-argument-list.
17726 template-argument-list:
17727 template-argument ... [opt]
17728 template-argument-list , template-argument ... [opt]
17730 Returns a TREE_VEC containing the arguments. */
17733 cp_parser_template_argument_list (cp_parser
* parser
)
17735 tree fixed_args
[10];
17736 unsigned n_args
= 0;
17737 unsigned alloced
= 10;
17738 tree
*arg_ary
= fixed_args
;
17740 bool saved_in_template_argument_list_p
;
17742 bool saved_non_ice_p
;
17744 /* Don't create location wrapper nodes within a template-argument-list. */
17745 auto_suppress_location_wrappers sentinel
;
17747 saved_in_template_argument_list_p
= parser
->in_template_argument_list_p
;
17748 parser
->in_template_argument_list_p
= true;
17749 /* Even if the template-id appears in an integral
17750 constant-expression, the contents of the argument list do
17752 saved_ice_p
= parser
->integral_constant_expression_p
;
17753 parser
->integral_constant_expression_p
= false;
17754 saved_non_ice_p
= parser
->non_integral_constant_expression_p
;
17755 parser
->non_integral_constant_expression_p
= false;
17757 /* Parse the arguments. */
17763 /* Consume the comma. */
17764 cp_lexer_consume_token (parser
->lexer
);
17766 /* Parse the template-argument. */
17767 argument
= cp_parser_template_argument (parser
);
17769 /* If the next token is an ellipsis, we're expanding a template
17771 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
17773 if (argument
== error_mark_node
)
17775 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
17776 error_at (token
->location
,
17777 "expected parameter pack before %<...%>");
17779 /* Consume the `...' token. */
17780 cp_lexer_consume_token (parser
->lexer
);
17782 /* Make the argument into a TYPE_PACK_EXPANSION or
17783 EXPR_PACK_EXPANSION. */
17784 argument
= make_pack_expansion (argument
);
17787 if (n_args
== alloced
)
17791 if (arg_ary
== fixed_args
)
17793 arg_ary
= XNEWVEC (tree
, alloced
);
17794 memcpy (arg_ary
, fixed_args
, sizeof (tree
) * n_args
);
17797 arg_ary
= XRESIZEVEC (tree
, arg_ary
, alloced
);
17799 arg_ary
[n_args
++] = argument
;
17801 while (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
17803 vec
= make_tree_vec (n_args
);
17806 TREE_VEC_ELT (vec
, n_args
) = arg_ary
[n_args
];
17808 if (arg_ary
!= fixed_args
)
17810 parser
->non_integral_constant_expression_p
= saved_non_ice_p
;
17811 parser
->integral_constant_expression_p
= saved_ice_p
;
17812 parser
->in_template_argument_list_p
= saved_in_template_argument_list_p
;
17814 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec
, TREE_VEC_LENGTH (vec
));
17818 /* Parse a template-argument.
17821 assignment-expression
17825 The representation is that of an assignment-expression, type-id, or
17826 id-expression -- except that the qualified id-expression is
17827 evaluated, so that the value returned is either a DECL or an
17830 Although the standard says "assignment-expression", it forbids
17831 throw-expressions or assignments in the template argument.
17832 Therefore, we use "conditional-expression" instead. */
17835 cp_parser_template_argument (cp_parser
* parser
)
17840 bool maybe_type_id
= false;
17841 cp_token
*token
= NULL
, *argument_start_token
= NULL
;
17842 location_t loc
= 0;
17845 /* There's really no way to know what we're looking at, so we just
17846 try each alternative in order.
17850 In a template-argument, an ambiguity between a type-id and an
17851 expression is resolved to a type-id, regardless of the form of
17852 the corresponding template-parameter.
17854 Therefore, we try a type-id first. */
17855 cp_parser_parse_tentatively (parser
);
17856 argument
= cp_parser_template_type_arg (parser
);
17857 /* If there was no error parsing the type-id but the next token is a
17858 '>>', our behavior depends on which dialect of C++ we're
17859 parsing. In C++98, we probably found a typo for '> >'. But there
17860 are type-id which are also valid expressions. For instance:
17862 struct X { int operator >> (int); };
17863 template <int V> struct Foo {};
17866 Here 'X()' is a valid type-id of a function type, but the user just
17867 wanted to write the expression "X() >> 5". Thus, we remember that we
17868 found a valid type-id, but we still try to parse the argument as an
17869 expression to see what happens.
17871 In C++0x, the '>>' will be considered two separate '>'
17873 if (!cp_parser_error_occurred (parser
)
17874 && cxx_dialect
== cxx98
17875 && cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
17877 maybe_type_id
= true;
17878 cp_parser_abort_tentative_parse (parser
);
17882 /* If the next token isn't a `,' or a `>', then this argument wasn't
17883 really finished. This means that the argument is not a valid
17885 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17886 cp_parser_error (parser
, "expected template-argument");
17887 /* If that worked, we're done. */
17888 if (cp_parser_parse_definitely (parser
))
17891 /* We're still not sure what the argument will be. */
17892 cp_parser_parse_tentatively (parser
);
17893 /* Try a template. */
17894 argument_start_token
= cp_lexer_peek_token (parser
->lexer
);
17895 argument
= cp_parser_id_expression (parser
,
17896 /*template_keyword_p=*/false,
17897 /*check_dependency_p=*/true,
17899 /*declarator_p=*/false,
17900 /*optional_p=*/false);
17901 /* If the next token isn't a `,' or a `>', then this argument wasn't
17902 really finished. */
17903 if (!cp_parser_next_token_ends_template_argument_p (parser
))
17904 cp_parser_error (parser
, "expected template-argument");
17905 if (!cp_parser_error_occurred (parser
))
17907 /* Figure out what is being referred to. If the id-expression
17908 was for a class template specialization, then we will have a
17909 TYPE_DECL at this point. There is no need to do name lookup
17910 at this point in that case. */
17911 if (TREE_CODE (argument
) != TYPE_DECL
)
17912 argument
= cp_parser_lookup_name (parser
, argument
,
17914 /*is_template=*/template_p
,
17915 /*is_namespace=*/false,
17916 /*check_dependency=*/true,
17917 /*ambiguous_decls=*/NULL
,
17918 argument_start_token
->location
);
17919 if (TREE_CODE (argument
) != TEMPLATE_DECL
17920 && TREE_CODE (argument
) != UNBOUND_CLASS_TEMPLATE
)
17921 cp_parser_error (parser
, "expected template-name");
17923 if (cp_parser_parse_definitely (parser
))
17925 if (TREE_DEPRECATED (argument
))
17926 warn_deprecated_use (argument
, NULL_TREE
);
17929 /* It must be a non-type argument. In C++17 any constant-expression is
17931 if (cxx_dialect
> cxx14
)
17934 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
17936 -- an integral constant-expression of integral or enumeration
17939 -- the name of a non-type template-parameter; or
17941 -- the name of an object or function with external linkage...
17943 -- the address of an object or function with external linkage...
17945 -- a pointer to member... */
17946 /* Look for a non-type template parameter. */
17947 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
17949 cp_parser_parse_tentatively (parser
);
17950 argument
= cp_parser_primary_expression (parser
,
17951 /*address_p=*/false,
17953 /*template_arg_p=*/true,
17955 if (TREE_CODE (argument
) != TEMPLATE_PARM_INDEX
17956 || !cp_parser_next_token_ends_template_argument_p (parser
))
17957 cp_parser_simulate_error (parser
);
17958 if (cp_parser_parse_definitely (parser
))
17962 /* If the next token is "&", the argument must be the address of an
17963 object or function with external linkage. */
17964 address_p
= cp_lexer_next_token_is (parser
->lexer
, CPP_AND
);
17967 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
17968 cp_lexer_consume_token (parser
->lexer
);
17970 /* See if we might have an id-expression. */
17971 token
= cp_lexer_peek_token (parser
->lexer
);
17972 if (token
->type
== CPP_NAME
17973 || token
->keyword
== RID_OPERATOR
17974 || token
->type
== CPP_SCOPE
17975 || token
->type
== CPP_TEMPLATE_ID
17976 || token
->type
== CPP_NESTED_NAME_SPECIFIER
)
17978 cp_parser_parse_tentatively (parser
);
17979 argument
= cp_parser_primary_expression (parser
,
17982 /*template_arg_p=*/true,
17984 if (cp_parser_error_occurred (parser
)
17985 || !cp_parser_next_token_ends_template_argument_p (parser
))
17986 cp_parser_abort_tentative_parse (parser
);
17991 if (INDIRECT_REF_P (argument
))
17993 /* Strip the dereference temporarily. */
17994 gcc_assert (REFERENCE_REF_P (argument
));
17995 argument
= TREE_OPERAND (argument
, 0);
17998 /* If we're in a template, we represent a qualified-id referring
17999 to a static data member as a SCOPE_REF even if the scope isn't
18000 dependent so that we can check access control later. */
18002 if (TREE_CODE (probe
) == SCOPE_REF
)
18003 probe
= TREE_OPERAND (probe
, 1);
18006 /* A variable without external linkage might still be a
18007 valid constant-expression, so no error is issued here
18008 if the external-linkage check fails. */
18009 if (!address_p
&& !DECL_EXTERNAL_LINKAGE_P (probe
))
18010 cp_parser_simulate_error (parser
);
18012 else if (is_overloaded_fn (argument
))
18013 /* All overloaded functions are allowed; if the external
18014 linkage test does not pass, an error will be issued
18018 && (TREE_CODE (argument
) == OFFSET_REF
18019 || TREE_CODE (argument
) == SCOPE_REF
))
18020 /* A pointer-to-member. */
18022 else if (TREE_CODE (argument
) == TEMPLATE_PARM_INDEX
)
18025 cp_parser_simulate_error (parser
);
18027 if (cp_parser_parse_definitely (parser
))
18030 argument
= build_x_unary_op (loc
, ADDR_EXPR
, argument
,
18031 tf_warning_or_error
);
18033 argument
= convert_from_reference (argument
);
18038 /* If the argument started with "&", there are no other valid
18039 alternatives at this point. */
18042 cp_parser_error (parser
, "invalid non-type template argument");
18043 return error_mark_node
;
18047 /* If the argument wasn't successfully parsed as a type-id followed
18048 by '>>', the argument can only be a constant expression now.
18049 Otherwise, we try parsing the constant-expression tentatively,
18050 because the argument could really be a type-id. */
18052 cp_parser_parse_tentatively (parser
);
18054 if (cxx_dialect
<= cxx14
)
18055 argument
= cp_parser_constant_expression (parser
);
18058 /* In C++20, we can encounter a braced-init-list. */
18059 if (cxx_dialect
>= cxx20
18060 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
18062 bool expr_non_constant_p
;
18063 return cp_parser_braced_list (parser
, &expr_non_constant_p
);
18066 /* With C++17 generalized non-type template arguments we need to handle
18067 lvalue constant expressions, too. */
18068 argument
= cp_parser_assignment_expression (parser
);
18069 require_potential_constant_expression (argument
);
18072 if (!maybe_type_id
)
18074 if (!cp_parser_next_token_ends_template_argument_p (parser
))
18075 cp_parser_error (parser
, "expected template-argument");
18076 if (cp_parser_parse_definitely (parser
))
18078 /* We did our best to parse the argument as a non type-id, but that
18079 was the only alternative that matched (albeit with a '>' after
18080 it). We can assume it's just a typo from the user, and a
18081 diagnostic will then be issued. */
18082 return cp_parser_template_type_arg (parser
);
18085 /* Parse an explicit-instantiation.
18087 explicit-instantiation:
18088 template declaration
18090 Although the standard says `declaration', what it really means is:
18092 explicit-instantiation:
18093 template decl-specifier-seq [opt] declarator [opt] ;
18095 Things like `template int S<int>::i = 5, int S<double>::j;' are not
18096 supposed to be allowed. A defect report has been filed about this
18101 explicit-instantiation:
18102 storage-class-specifier template
18103 decl-specifier-seq [opt] declarator [opt] ;
18104 function-specifier template
18105 decl-specifier-seq [opt] declarator [opt] ; */
18108 cp_parser_explicit_instantiation (cp_parser
* parser
)
18110 int declares_class_or_enum
;
18111 cp_decl_specifier_seq decl_specifiers
;
18112 tree extension_specifier
= NULL_TREE
;
18114 timevar_push (TV_TEMPLATE_INST
);
18116 /* Look for an (optional) storage-class-specifier or
18117 function-specifier. */
18118 if (cp_parser_allow_gnu_extensions_p (parser
))
18120 extension_specifier
18121 = cp_parser_storage_class_specifier_opt (parser
);
18122 if (!extension_specifier
)
18123 extension_specifier
18124 = cp_parser_function_specifier_opt (parser
,
18125 /*decl_specs=*/NULL
);
18128 /* Look for the `template' keyword. */
18129 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
18130 /* Let the front end know that we are processing an explicit
18132 begin_explicit_instantiation ();
18133 /* [temp.explicit] says that we are supposed to ignore access
18134 control while processing explicit instantiation directives. */
18135 push_deferring_access_checks (dk_no_check
);
18136 /* Parse a decl-specifier-seq. */
18137 cp_parser_decl_specifier_seq (parser
,
18138 CP_PARSER_FLAGS_OPTIONAL
,
18140 &declares_class_or_enum
);
18141 /* If there was exactly one decl-specifier, and it declared a class,
18142 and there's no declarator, then we have an explicit type
18144 if (declares_class_or_enum
&& cp_parser_declares_only_class_p (parser
))
18146 tree type
= check_tag_decl (&decl_specifiers
,
18147 /*explicit_type_instantiation_p=*/true);
18148 /* Turn access control back on for names used during
18149 template instantiation. */
18150 pop_deferring_access_checks ();
18152 do_type_instantiation (type
, extension_specifier
,
18153 /*complain=*/tf_error
);
18157 cp_declarator
*declarator
;
18160 /* Parse the declarator. */
18162 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
18163 CP_PARSER_FLAGS_NONE
,
18164 /*ctor_dtor_or_conv_p=*/NULL
,
18165 /*parenthesized_p=*/NULL
,
18166 /*member_p=*/false,
18167 /*friend_p=*/false,
18168 /*static_p=*/false);
18169 if (declares_class_or_enum
& 2)
18170 cp_parser_check_for_definition_in_return_type (declarator
,
18171 decl_specifiers
.type
,
18172 decl_specifiers
.locations
[ds_type_spec
]);
18173 if (declarator
!= cp_error_declarator
)
18175 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_inline
))
18176 permerror (decl_specifiers
.locations
[ds_inline
],
18177 "explicit instantiation shall not use"
18178 " %<inline%> specifier");
18179 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_constexpr
))
18180 permerror (decl_specifiers
.locations
[ds_constexpr
],
18181 "explicit instantiation shall not use"
18182 " %<constexpr%> specifier");
18183 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_consteval
))
18184 permerror (decl_specifiers
.locations
[ds_consteval
],
18185 "explicit instantiation shall not use"
18186 " %<consteval%> specifier");
18188 decl
= grokdeclarator (declarator
, &decl_specifiers
,
18189 NORMAL
, 0, &decl_specifiers
.attributes
);
18190 /* Turn access control back on for names used during
18191 template instantiation. */
18192 pop_deferring_access_checks ();
18193 /* Do the explicit instantiation. */
18194 do_decl_instantiation (decl
, extension_specifier
);
18198 pop_deferring_access_checks ();
18199 /* Skip the body of the explicit instantiation. */
18200 cp_parser_skip_to_end_of_statement (parser
);
18203 /* We're done with the instantiation. */
18204 end_explicit_instantiation ();
18206 cp_parser_consume_semicolon_at_end_of_statement (parser
);
18208 timevar_pop (TV_TEMPLATE_INST
);
18211 /* Parse an explicit-specialization.
18213 explicit-specialization:
18214 template < > declaration
18216 Although the standard says `declaration', what it really means is:
18218 explicit-specialization:
18219 template <> decl-specifier [opt] init-declarator [opt] ;
18220 template <> function-definition
18221 template <> explicit-specialization
18222 template <> template-declaration */
18225 cp_parser_explicit_specialization (cp_parser
* parser
)
18227 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
18229 /* Look for the `template' keyword. */
18230 cp_parser_require_keyword (parser
, RID_TEMPLATE
, RT_TEMPLATE
);
18231 /* Look for the `<'. */
18232 cp_parser_require (parser
, CPP_LESS
, RT_LESS
);
18233 /* Look for the `>'. */
18234 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
18235 /* We have processed another parameter list. */
18236 ++parser
->num_template_parameter_lists
;
18240 A template ... explicit specialization ... shall not have C
18242 bool need_lang_pop
= current_lang_name
== lang_name_c
;
18245 error_at (token
->location
, "template specialization with C linkage");
18246 maybe_show_extern_c_location ();
18248 /* Give it C++ linkage to avoid confusing other parts of the
18250 push_lang_context (lang_name_cplusplus
);
18253 /* Let the front end know that we are beginning a specialization. */
18254 if (begin_specialization ())
18256 /* If the next keyword is `template', we need to figure out
18257 whether or not we're looking a template-declaration. */
18258 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
18260 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
18261 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
!= CPP_GREATER
)
18262 cp_parser_template_declaration_after_export (parser
,
18263 /*member_p=*/false);
18265 cp_parser_explicit_specialization (parser
);
18268 /* Parse the dependent declaration. */
18269 cp_parser_single_declaration (parser
,
18271 /*member_p=*/false,
18272 /*explicit_specialization_p=*/true,
18273 /*friend_p=*/NULL
);
18276 /* We're done with the specialization. */
18277 end_specialization ();
18279 /* For the erroneous case of a template with C linkage, we pushed an
18280 implicit C++ linkage scope; exit that scope now. */
18282 pop_lang_context ();
18284 /* We're done with this parameter list. */
18285 --parser
->num_template_parameter_lists
;
18288 /* Parse a type-specifier.
18291 simple-type-specifier
18294 elaborated-type-specifier
18302 Returns a representation of the type-specifier. For a
18303 class-specifier, enum-specifier, or elaborated-type-specifier, a
18304 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
18306 The parser flags FLAGS is used to control type-specifier parsing.
18308 If IS_DECLARATION is TRUE, then this type-specifier is appearing
18309 in a decl-specifier-seq.
18311 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
18312 class-specifier, enum-specifier, or elaborated-type-specifier, then
18313 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
18314 if a type is declared; 2 if it is defined. Otherwise, it is set to
18317 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
18318 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
18319 is set to FALSE. */
18322 cp_parser_type_specifier (cp_parser
* parser
,
18323 cp_parser_flags flags
,
18324 cp_decl_specifier_seq
*decl_specs
,
18325 bool is_declaration
,
18326 int* declares_class_or_enum
,
18327 bool* is_cv_qualifier
)
18329 tree type_spec
= NULL_TREE
;
18332 cp_decl_spec ds
= ds_last
;
18334 /* Assume this type-specifier does not declare a new type. */
18335 if (declares_class_or_enum
)
18336 *declares_class_or_enum
= 0;
18337 /* And that it does not specify a cv-qualifier. */
18338 if (is_cv_qualifier
)
18339 *is_cv_qualifier
= false;
18340 /* Peek at the next token. */
18341 token
= cp_lexer_peek_token (parser
->lexer
);
18343 /* If we're looking at a keyword, we can use that to guide the
18344 production we choose. */
18345 keyword
= token
->keyword
;
18349 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
18350 goto elaborated_type_specifier
;
18352 /* Look for the enum-specifier. */
18353 type_spec
= cp_parser_enum_specifier (parser
);
18354 /* If that worked, we're done. */
18357 if (declares_class_or_enum
)
18358 *declares_class_or_enum
= 2;
18360 cp_parser_set_decl_spec_type (decl_specs
,
18363 /*type_definition_p=*/true);
18367 goto elaborated_type_specifier
;
18369 /* Any of these indicate either a class-specifier, or an
18370 elaborated-type-specifier. */
18374 if ((flags
& CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
))
18375 goto elaborated_type_specifier
;
18377 /* Parse tentatively so that we can back up if we don't find a
18378 class-specifier. */
18379 cp_parser_parse_tentatively (parser
);
18380 /* Look for the class-specifier. */
18381 type_spec
= cp_parser_class_specifier (parser
);
18382 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE
, type_spec
);
18383 /* If that worked, we're done. */
18384 if (cp_parser_parse_definitely (parser
))
18386 if (declares_class_or_enum
)
18387 *declares_class_or_enum
= 2;
18389 cp_parser_set_decl_spec_type (decl_specs
,
18392 /*type_definition_p=*/true);
18396 /* Fall through. */
18397 elaborated_type_specifier
:
18398 /* We're declaring (not defining) a class or enum. */
18399 if (declares_class_or_enum
)
18400 *declares_class_or_enum
= 1;
18402 /* Fall through. */
18404 /* Look for an elaborated-type-specifier. */
18406 = (cp_parser_elaborated_type_specifier
18408 decl_spec_seq_has_spec_p (decl_specs
, ds_friend
),
18411 cp_parser_set_decl_spec_type (decl_specs
,
18414 /*type_definition_p=*/false);
18419 if (is_cv_qualifier
)
18420 *is_cv_qualifier
= true;
18425 if (is_cv_qualifier
)
18426 *is_cv_qualifier
= true;
18431 if (is_cv_qualifier
)
18432 *is_cv_qualifier
= true;
18436 /* The `__complex__' keyword is a GNU extension. */
18444 /* Handle simple keywords. */
18449 set_and_check_decl_spec_loc (decl_specs
, ds
, token
);
18450 decl_specs
->any_specifiers_p
= true;
18452 return cp_lexer_consume_token (parser
->lexer
)->u
.value
;
18455 /* If we do not already have a type-specifier, assume we are looking
18456 at a simple-type-specifier. */
18457 type_spec
= cp_parser_simple_type_specifier (parser
,
18461 /* If we didn't find a type-specifier, and a type-specifier was not
18462 optional in this context, issue an error message. */
18463 if (!type_spec
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
18465 cp_parser_error (parser
, "expected type specifier");
18466 return error_mark_node
;
18472 /* Parse a simple-type-specifier.
18474 simple-type-specifier:
18475 :: [opt] nested-name-specifier [opt] type-name
18476 :: [opt] nested-name-specifier template template-id
18491 simple-type-specifier:
18493 decltype ( expression )
18496 __underlying_type ( type-id )
18500 nested-name-specifier(opt) template-name
18504 simple-type-specifier:
18506 __typeof__ unary-expression
18507 __typeof__ ( type-id )
18508 __typeof__ ( type-id ) { initializer-list , [opt] }
18510 Concepts Extension:
18512 simple-type-specifier:
18513 constrained-type-specifier
18515 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
18516 appropriately updated. */
18519 cp_parser_simple_type_specifier (cp_parser
* parser
,
18520 cp_decl_specifier_seq
*decl_specs
,
18521 cp_parser_flags flags
)
18523 tree type
= NULL_TREE
;
18527 /* Peek at the next token. */
18528 token
= cp_lexer_peek_token (parser
->lexer
);
18530 /* If we're looking at a keyword, things are easy. */
18531 switch (token
->keyword
)
18535 decl_specs
->explicit_char_p
= true;
18536 type
= char_type_node
;
18539 type
= char8_type_node
;
18542 type
= char16_type_node
;
18545 type
= char32_type_node
;
18548 type
= wchar_type_node
;
18551 type
= boolean_type_node
;
18554 set_and_check_decl_spec_loc (decl_specs
, ds_short
, token
);
18555 type
= short_integer_type_node
;
18559 decl_specs
->explicit_int_p
= true;
18560 type
= integer_type_node
;
18566 idx
= token
->keyword
- RID_INT_N_0
;
18567 if (! int_n_enabled_p
[idx
])
18571 decl_specs
->explicit_intN_p
= true;
18572 decl_specs
->int_n_idx
= idx
;
18573 /* Check if the alternate "__intN__" form has been used instead of
18575 if (strncmp (IDENTIFIER_POINTER (token
->u
.value
)
18576 + (IDENTIFIER_LENGTH (token
->u
.value
) - 2),
18578 decl_specs
->int_n_alt
= true;
18580 type
= int_n_trees
[idx
].signed_type
;
18584 set_and_check_decl_spec_loc (decl_specs
, ds_long
, token
);
18585 type
= long_integer_type_node
;
18588 set_and_check_decl_spec_loc (decl_specs
, ds_signed
, token
);
18589 type
= integer_type_node
;
18592 set_and_check_decl_spec_loc (decl_specs
, ds_unsigned
, token
);
18593 type
= unsigned_type_node
;
18596 type
= float_type_node
;
18599 type
= double_type_node
;
18602 type
= void_type_node
;
18606 maybe_warn_cpp0x (CPP0X_AUTO
);
18607 if (parser
->auto_is_implicit_function_template_parm_p
)
18609 /* The 'auto' might be the placeholder return type for a function decl
18610 with trailing return type. */
18611 bool have_trailing_return_fn_decl
= false;
18613 cp_parser_parse_tentatively (parser
);
18614 cp_lexer_consume_token (parser
->lexer
);
18615 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
)
18616 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
18617 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
18618 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_EOF
))
18620 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
18622 cp_lexer_consume_token (parser
->lexer
);
18623 cp_parser_skip_to_closing_parenthesis (parser
,
18624 /*recovering*/false,
18626 /*consume_paren*/true);
18630 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
18632 have_trailing_return_fn_decl
= true;
18636 cp_lexer_consume_token (parser
->lexer
);
18638 cp_parser_abort_tentative_parse (parser
);
18640 if (have_trailing_return_fn_decl
)
18642 type
= make_auto ();
18646 if (cxx_dialect
>= cxx14
)
18648 type
= synthesize_implicit_template_parm (parser
, NULL_TREE
);
18649 type
= TREE_TYPE (type
);
18652 type
= error_mark_node
;
18654 if (current_class_type
&& LAMBDA_TYPE_P (current_class_type
))
18656 if (cxx_dialect
< cxx14
)
18657 error_at (token
->location
,
18658 "use of %<auto%> in lambda parameter declaration "
18659 "only available with "
18660 "%<-std=c++14%> or %<-std=gnu++14%>");
18662 else if (cxx_dialect
< cxx14
)
18663 error_at (token
->location
,
18664 "use of %<auto%> in parameter declaration "
18665 "only available with "
18666 "%<-std=c++14%> or %<-std=gnu++14%>");
18667 else if (!flag_concepts
)
18668 pedwarn (token
->location
, 0,
18669 "use of %<auto%> in parameter declaration "
18670 "only available with %<-fconcepts-ts%>");
18673 type
= make_auto ();
18677 /* Since DR 743, decltype can either be a simple-type-specifier by
18678 itself or begin a nested-name-specifier. Parsing it will replace
18679 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
18680 handling below decide what to do. */
18681 cp_parser_decltype (parser
);
18682 cp_lexer_set_token_position (parser
->lexer
, token
);
18686 /* Consume the `typeof' token. */
18687 cp_lexer_consume_token (parser
->lexer
);
18688 /* Parse the operand to `typeof'. */
18689 type
= cp_parser_sizeof_operand (parser
, RID_TYPEOF
);
18690 /* If it is not already a TYPE, take its type. */
18691 if (!TYPE_P (type
))
18692 type
= finish_typeof (type
);
18695 cp_parser_set_decl_spec_type (decl_specs
, type
,
18697 /*type_definition_p=*/false);
18701 case RID_UNDERLYING_TYPE
:
18702 type
= cp_parser_trait_expr (parser
, RID_UNDERLYING_TYPE
);
18704 cp_parser_set_decl_spec_type (decl_specs
, type
,
18706 /*type_definition_p=*/false);
18711 case RID_DIRECT_BASES
:
18712 type
= cp_parser_trait_expr (parser
, token
->keyword
);
18714 cp_parser_set_decl_spec_type (decl_specs
, type
,
18716 /*type_definition_p=*/false);
18722 /* If token is an already-parsed decltype not followed by ::,
18723 it's a simple-type-specifier. */
18724 if (token
->type
== CPP_DECLTYPE
18725 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
!= CPP_SCOPE
)
18727 type
= saved_checks_value (token
->u
.tree_check_value
);
18730 cp_parser_set_decl_spec_type (decl_specs
, type
,
18732 /*type_definition_p=*/false);
18733 /* Remember that we are handling a decltype in order to
18734 implement the resolution of DR 1510 when the argument
18735 isn't instantiation dependent. */
18736 decl_specs
->decltype_p
= true;
18738 cp_lexer_consume_token (parser
->lexer
);
18742 /* If the type-specifier was for a built-in type, we're done. */
18745 /* Record the type. */
18747 && (token
->keyword
!= RID_SIGNED
18748 && token
->keyword
!= RID_UNSIGNED
18749 && token
->keyword
!= RID_SHORT
18750 && token
->keyword
!= RID_LONG
))
18751 cp_parser_set_decl_spec_type (decl_specs
,
18754 /*type_definition_p=*/false);
18756 decl_specs
->any_specifiers_p
= true;
18758 /* Consume the token. */
18759 cp_lexer_consume_token (parser
->lexer
);
18761 if (type
== error_mark_node
)
18762 return error_mark_node
;
18764 /* There is no valid C++ program where a non-template type is
18765 followed by a "<". That usually indicates that the user thought
18766 that the type was a template. */
18767 cp_parser_check_for_invalid_template_id (parser
, type
, none_type
,
18770 return TYPE_NAME (type
);
18773 /* The type-specifier must be a user-defined type. */
18774 if (!(flags
& CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
))
18778 const bool typename_p
= (cxx_dialect
>= cxx20
18779 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
18781 /* Don't gobble tokens or issue error messages if this is an
18782 optional type-specifier. */
18783 if (flags
& CP_PARSER_FLAGS_OPTIONAL
)
18784 cp_parser_parse_tentatively (parser
);
18786 /* Remember current tentative parsing state -- if we know we need
18787 a type, we can give better diagnostics here. */
18788 bool tent
= cp_parser_parsing_tentatively (parser
);
18790 token
= cp_lexer_peek_token (parser
->lexer
);
18792 /* Look for the optional `::' operator. */
18794 = (cp_parser_global_scope_opt (parser
,
18795 /*current_scope_valid_p=*/false)
18797 /* Look for the nested-name specifier. */
18799 = (cp_parser_nested_name_specifier_opt (parser
,
18800 /*typename_keyword_p=*/false,
18801 /*check_dependency_p=*/true,
18803 /*is_declaration=*/false)
18805 /* If we have seen a nested-name-specifier, and the next token
18806 is `template', then we are using the template-id production. */
18808 && cp_parser_optional_template_keyword (parser
))
18810 /* Look for the template-id. */
18811 type
= cp_parser_template_id (parser
,
18812 /*template_keyword_p=*/true,
18813 /*check_dependency_p=*/true,
18815 /*is_declaration=*/false);
18816 /* If the template-id did not name a type, we are out of
18818 if (TREE_CODE (type
) != TYPE_DECL
)
18820 /* ...unless we pretend we have seen 'typename'. */
18822 type
= cp_parser_make_typename_type (parser
, type
,
18826 cp_parser_error (parser
, "expected template-id for type");
18827 type
= error_mark_node
;
18831 /* DR 1812: A < following a qualified-id in a typename-specifier
18832 could safely be assumed to begin a template argument list, so
18833 the template keyword should be optional. */
18834 else if (parser
->scope
18837 && cp_lexer_next_token_is (parser
->lexer
, CPP_TEMPLATE_ID
))
18839 cp_parser_parse_tentatively (parser
);
18841 type
= cp_parser_template_id (parser
,
18842 /*template_keyword_p=*/true,
18843 /*check_dependency_p=*/true,
18845 /*is_declaration=*/false);
18846 /* This is handled below, so back off. */
18847 if (type
&& concept_check_p (type
))
18848 cp_parser_simulate_error (parser
);
18850 if (!cp_parser_parse_definitely (parser
))
18852 else if (TREE_CODE (type
) == TEMPLATE_ID_EXPR
)
18853 type
= make_typename_type (parser
->scope
, type
, typename_type
,
18854 /*complain=*/tf_error
);
18855 else if (TREE_CODE (type
) != TYPE_DECL
)
18859 /* Otherwise, look for a type-name. */
18862 if (cxx_dialect
>= cxx17
)
18863 cp_parser_parse_tentatively (parser
);
18865 type
= cp_parser_type_name (parser
, (qualified_p
&& typename_p
));
18867 if (cxx_dialect
>= cxx17
&& !cp_parser_parse_definitely (parser
))
18871 if (!type
&& flag_concepts
&& decl_specs
)
18873 /* Try for a type-constraint with template arguments. We check
18874 decl_specs here to avoid trying this for a functional cast. */
18876 cp_parser_parse_tentatively (parser
);
18878 type
= cp_parser_template_id (parser
,
18879 /*template_keyword_p=*/false,
18880 /*check_dependency_p=*/true,
18882 /*is_declaration=*/false);
18883 if (type
&& concept_check_p (type
))
18885 location_t loc
= EXPR_LOCATION (type
);
18886 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18888 if (tent
&& type
== error_mark_node
)
18889 /* Perhaps it's a concept-check expression. */
18890 cp_parser_simulate_error (parser
);
18893 cp_parser_simulate_error (parser
);
18895 if (!cp_parser_parse_definitely (parser
))
18899 if (!type
&& cxx_dialect
>= cxx17
)
18901 /* Try class template argument deduction or type-constraint without
18902 template arguments. */
18903 tree name
= cp_parser_identifier (parser
);
18904 if (name
&& TREE_CODE (name
) == IDENTIFIER_NODE
18905 && parser
->scope
!= error_mark_node
)
18908 = cp_lexer_previous_token (parser
->lexer
)->location
;
18909 tree tmpl
= cp_parser_lookup_name (parser
, name
,
18911 /*is_template=*/false,
18912 /*is_namespace=*/false,
18913 /*check_dependency=*/true,
18914 /*ambiguous_decls=*/NULL
,
18916 if (tmpl
&& tmpl
!= error_mark_node
18917 && ctad_template_p (tmpl
))
18918 type
= make_template_placeholder (tmpl
);
18919 else if (flag_concepts
&& tmpl
&& concept_definition_p (tmpl
))
18920 type
= cp_parser_placeholder_type_specifier (parser
, loc
,
18924 type
= error_mark_node
;
18925 if (!cp_parser_simulate_error (parser
))
18926 cp_parser_name_lookup_error (parser
, name
, tmpl
,
18927 NLE_TYPE
, token
->location
);
18931 type
= error_mark_node
;
18934 /* If it didn't work out, we don't have a TYPE. */
18935 if ((flags
& CP_PARSER_FLAGS_OPTIONAL
)
18936 && !cp_parser_parse_definitely (parser
))
18939 /* Keep track of all name-lookups performed in class scopes. */
18943 && TREE_CODE (type
) == TYPE_DECL
18944 && identifier_p (DECL_NAME (type
)))
18945 maybe_note_name_used_in_class (DECL_NAME (type
), type
);
18947 if (type
&& decl_specs
)
18948 cp_parser_set_decl_spec_type (decl_specs
, type
,
18950 /*type_definition_p=*/false);
18953 /* If we didn't get a type-name, issue an error message. */
18954 if (!type
&& !(flags
& CP_PARSER_FLAGS_OPTIONAL
))
18956 cp_parser_error (parser
, "expected type-name");
18957 return error_mark_node
;
18960 if (type
&& type
!= error_mark_node
)
18962 /* See if TYPE is an Objective-C type, and if so, parse and
18963 accept any protocol references following it. Do this before
18964 the cp_parser_check_for_invalid_template_id() call, because
18965 Objective-C types can be followed by '<...>' which would
18966 enclose protocol names rather than template arguments, and so
18967 everything is fine. */
18968 if (c_dialect_objc () && !parser
->scope
18969 && (objc_is_id (type
) || objc_is_class_name (type
)))
18971 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
18972 tree qual_type
= objc_get_protocol_qualified_type (type
, protos
);
18974 /* Clobber the "unqualified" type previously entered into
18975 DECL_SPECS with the new, improved protocol-qualified version. */
18977 decl_specs
->type
= qual_type
;
18982 /* There is no valid C++ program where a non-template type is
18983 followed by a "<". That usually indicates that the user
18984 thought that the type was a template. */
18985 cp_parser_check_for_invalid_template_id (parser
, type
,
18993 /* Parse the remainder of a placholder-type-specifier.
18995 placeholder-type-specifier:
18996 type-constraint_opt auto
18997 type-constraint_opt decltype(auto)
18999 The raw form of the constraint is parsed in cp_parser_simple_type_specifier
19000 and passed as TMPL. This function converts TMPL to an actual type-constraint,
19001 parses the placeholder type, and performs some contextual syntactic analysis.
19003 LOC provides the location of the template name.
19005 TENTATIVE is true if the type-specifier parsing is tentative; in that case,
19006 don't give an error if TMPL isn't a valid type-constraint, as the template-id
19007 might actually be a concept-check,
19009 Note that the Concepts TS allows the auto or decltype(auto) to be
19010 omitted in a constrained-type-specifier. */
19013 cp_parser_placeholder_type_specifier (cp_parser
*parser
, location_t loc
,
19014 tree tmpl
, bool tentative
)
19016 if (tmpl
== error_mark_node
)
19017 return error_mark_node
;
19019 tree orig_tmpl
= tmpl
;
19021 /* Get the arguments as written for subsequent analysis. */
19022 tree args
= NULL_TREE
;
19023 if (TREE_CODE (tmpl
) == TEMPLATE_ID_EXPR
)
19025 args
= TREE_OPERAND (tmpl
, 1);
19026 tmpl
= TREE_OPERAND (tmpl
, 0);
19028 if (args
== NULL_TREE
)
19029 /* A concept-name with no arguments can't be an expression. */
19032 tsubst_flags_t complain
= tentative
? tf_none
: tf_warning_or_error
;
19034 /* Get the concept and prototype parameter for the constraint. */
19035 tree_pair info
= finish_type_constraints (tmpl
, args
, complain
);
19036 tree con
= info
.first
;
19037 tree proto
= info
.second
;
19038 if (con
== error_mark_node
)
19039 return error_mark_node
;
19041 /* As per the standard, require auto or decltype(auto), except in some
19042 cases (template parameter lists, -fconcepts-ts enabled). */
19043 cp_token
*placeholder
= NULL
, *close_paren
= NULL
;
19044 if (cxx_dialect
>= cxx20
)
19046 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
19047 placeholder
= cp_lexer_consume_token (parser
->lexer
);
19048 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DECLTYPE
))
19050 placeholder
= cp_lexer_consume_token (parser
->lexer
);
19051 matching_parens parens
;
19052 parens
.require_open (parser
);
19053 cp_parser_require_keyword (parser
, RID_AUTO
, RT_AUTO
);
19054 close_paren
= parens
.require_close (parser
);
19058 /* A type constraint constrains a contextually determined type or type
19059 parameter pack. However, the Concepts TS does allow concepts
19060 to introduce non-type and template template parameters. */
19061 if (TREE_CODE (proto
) != TYPE_DECL
)
19063 if (!flag_concepts_ts
19064 || !processing_template_parmlist
)
19066 error_at (loc
, "%qE does not constrain a type", DECL_NAME (con
));
19067 inform (DECL_SOURCE_LOCATION (con
), "concept defined here");
19068 return error_mark_node
;
19072 /* In a template parameter list, a type-parameter can be introduced
19073 by type-constraints alone. */
19074 if (processing_template_parmlist
&& !placeholder
)
19075 return build_constrained_parameter (con
, proto
, args
);
19077 /* Diagnose issues placeholder issues. */
19078 if (!flag_concepts_ts
19079 && !parser
->in_result_type_constraint_p
19083 /* Perhaps it's a concept-check expression (c++/91073). */
19084 return error_mark_node
;
19086 tree id
= build_nt (TEMPLATE_ID_EXPR
, tmpl
, args
);
19087 tree expr
= DECL_P (orig_tmpl
) ? DECL_NAME (con
) : id
;
19088 error_at (input_location
,
19089 "expected %<auto%> or %<decltype(auto)%> after %qE", expr
);
19090 /* Fall through. This is an error of omission. */
19092 else if (parser
->in_result_type_constraint_p
&& placeholder
)
19094 /* A trailing return type only allows type-constraints. */
19095 error_at (input_location
,
19096 "unexpected placeholder in constrained result type");
19099 /* In a parameter-declaration-clause, a placeholder-type-specifier
19100 results in an invented template parameter. */
19101 if (parser
->auto_is_implicit_function_template_parm_p
)
19105 location_t loc
= make_location (placeholder
->location
,
19106 placeholder
->location
,
19107 close_paren
->location
);
19108 error_at (loc
, "cannot declare a parameter with %<decltype(auto)%>");
19109 return error_mark_node
;
19111 tree parm
= build_constrained_parameter (con
, proto
, args
);
19112 return synthesize_implicit_template_parm (parser
, parm
);
19115 /* Determine if the type should be deduced using template argument
19116 deduction or decltype deduction. Note that the latter is always
19117 used for type-constraints in trailing return types. */
19118 bool decltype_p
= placeholder
19119 ? placeholder
->keyword
== RID_DECLTYPE
19120 : parser
->in_result_type_constraint_p
;
19122 /* Otherwise, this is the type of a variable or return type. */
19124 return make_constrained_decltype_auto (con
, args
);
19126 return make_constrained_auto (con
, args
);
19129 /* Parse a type-name.
19135 simple-template-id [in c++0x]
19152 Returns a TYPE_DECL for the type. */
19155 cp_parser_type_name (cp_parser
* parser
, bool typename_keyword_p
)
19159 /* We can't know yet whether it is a class-name or not. */
19160 cp_parser_parse_tentatively (parser
);
19161 /* Try a class-name. */
19162 type_decl
= cp_parser_class_name (parser
,
19163 typename_keyword_p
,
19164 /*template_keyword_p=*/false,
19166 /*check_dependency_p=*/true,
19167 /*class_head_p=*/false,
19168 /*is_declaration=*/false);
19169 /* If it's not a class-name, keep looking. */
19170 if (!cp_parser_parse_definitely (parser
))
19172 if (cxx_dialect
< cxx11
)
19173 /* It must be a typedef-name or an enum-name. */
19174 return cp_parser_nonclass_name (parser
);
19176 cp_parser_parse_tentatively (parser
);
19177 /* It is either a simple-template-id representing an
19178 instantiation of an alias template... */
19179 type_decl
= cp_parser_template_id (parser
,
19180 /*template_keyword_p=*/false,
19181 /*check_dependency_p=*/true,
19183 /*is_declaration=*/false);
19184 /* Note that this must be an instantiation of an alias template
19185 because [temp.names]/6 says:
19187 A template-id that names an alias template specialization
19190 Whereas [temp.names]/7 says:
19192 A simple-template-id that names a class template
19193 specialization is a class-name.
19195 With concepts, this could also be a partial-concept-id that
19196 declares a non-type template parameter. */
19197 if (type_decl
!= NULL_TREE
19198 && TREE_CODE (type_decl
) == TYPE_DECL
19199 && TYPE_DECL_ALIAS_P (type_decl
))
19200 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl
));
19202 cp_parser_simulate_error (parser
);
19204 if (!cp_parser_parse_definitely (parser
))
19205 /* ... Or a typedef-name or an enum-name. */
19206 return cp_parser_nonclass_name (parser
);
19212 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
19224 Returns a TYPE_DECL for the type. */
19227 cp_parser_nonclass_name (cp_parser
* parser
)
19232 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19233 identifier
= cp_parser_identifier (parser
);
19234 if (identifier
== error_mark_node
)
19235 return error_mark_node
;
19237 /* Look up the type-name. */
19238 type_decl
= cp_parser_lookup_name_simple (parser
, identifier
, token
->location
);
19240 type_decl
= strip_using_decl (type_decl
);
19242 if (TREE_CODE (type_decl
) != TYPE_DECL
19243 && (objc_is_id (identifier
) || objc_is_class_name (identifier
)))
19245 /* See if this is an Objective-C type. */
19246 tree protos
= cp_parser_objc_protocol_refs_opt (parser
);
19247 tree type
= objc_get_protocol_qualified_type (identifier
, protos
);
19249 type_decl
= TYPE_NAME (type
);
19252 /* Issue an error if we did not find a type-name. */
19253 if (TREE_CODE (type_decl
) != TYPE_DECL
19254 /* In Objective-C, we have the complication that class names are
19255 normally type names and start declarations (eg, the
19256 "NSObject" in "NSObject *object;"), but can be used in an
19257 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
19258 is an expression. So, a classname followed by a dot is not a
19259 valid type-name. */
19260 || (objc_is_class_name (TREE_TYPE (type_decl
))
19261 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
))
19263 if (!cp_parser_simulate_error (parser
))
19264 cp_parser_name_lookup_error (parser
, identifier
, type_decl
,
19265 NLE_TYPE
, token
->location
);
19266 return error_mark_node
;
19268 /* Remember that the name was used in the definition of the
19269 current class so that we can check later to see if the
19270 meaning would have been different after the class was
19271 entirely defined. */
19272 else if (type_decl
!= error_mark_node
19274 maybe_note_name_used_in_class (identifier
, type_decl
);
19279 /* Parse an elaborated-type-specifier. Note that the grammar given
19280 here incorporates the resolution to DR68.
19282 elaborated-type-specifier:
19283 class-key :: [opt] nested-name-specifier [opt] identifier
19284 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
19285 enum-key :: [opt] nested-name-specifier [opt] identifier
19286 typename :: [opt] nested-name-specifier identifier
19287 typename :: [opt] nested-name-specifier template [opt]
19292 elaborated-type-specifier:
19293 class-key attributes :: [opt] nested-name-specifier [opt] identifier
19294 class-key attributes :: [opt] nested-name-specifier [opt]
19295 template [opt] template-id
19296 enum attributes :: [opt] nested-name-specifier [opt] identifier
19298 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
19299 declared `friend'. If IS_DECLARATION is TRUE, then this
19300 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
19301 something is being declared.
19303 Returns the TYPE specified. */
19306 cp_parser_elaborated_type_specifier (cp_parser
* parser
,
19308 bool is_declaration
)
19310 enum tag_types tag_type
;
19312 tree type
= NULL_TREE
;
19313 tree attributes
= NULL_TREE
;
19315 cp_token
*token
= NULL
;
19317 /* For class and enum types the location of the class-key or enum-key. */
19318 location_t key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19319 /* For a scoped enum, the 'class' or 'struct' keyword id. */
19320 rid scoped_key
= RID_MAX
;
19322 /* See if we're looking at the `enum' keyword. */
19323 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
))
19325 /* Consume the `enum' token. */
19326 cp_lexer_consume_token (parser
->lexer
);
19327 /* Remember that it's an enumeration type. */
19328 tag_type
= enum_type
;
19329 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
19330 enums) is used here. */
19331 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
19332 if (cp_parser_is_keyword (token
, scoped_key
= RID_CLASS
)
19333 || cp_parser_is_keyword (token
, scoped_key
= RID_STRUCT
))
19335 location_t loc
= token
->location
;
19336 gcc_rich_location
richloc (loc
);
19337 richloc
.add_range (input_location
);
19338 richloc
.add_fixit_remove ();
19339 pedwarn (&richloc
, 0, "elaborated-type-specifier for "
19340 "a scoped enum must not use the %qD keyword",
19342 /* Consume the `struct' or `class' and parse it anyway. */
19343 cp_lexer_consume_token (parser
->lexer
);
19344 /* Create a combined location for the whole scoped-enum-key. */
19345 key_loc
= make_location (key_loc
, key_loc
, loc
);
19348 scoped_key
= RID_MAX
;
19350 /* Parse the attributes. */
19351 attributes
= cp_parser_attributes_opt (parser
);
19353 /* Or, it might be `typename'. */
19354 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
19357 /* Consume the `typename' token. */
19358 cp_lexer_consume_token (parser
->lexer
);
19359 /* Remember that it's a `typename' type. */
19360 tag_type
= typename_type
;
19362 /* Otherwise it must be a class-key. */
19365 key_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
19366 tag_type
= cp_parser_class_key (parser
);
19367 if (tag_type
== none_type
)
19368 return error_mark_node
;
19369 /* Parse the attributes. */
19370 attributes
= cp_parser_attributes_opt (parser
);
19373 /* Look for the `::' operator. */
19374 globalscope
= cp_parser_global_scope_opt (parser
,
19375 /*current_scope_valid_p=*/false);
19376 /* Look for the nested-name-specifier. */
19377 tree nested_name_specifier
;
19378 if (tag_type
== typename_type
&& !globalscope
)
19380 nested_name_specifier
19381 = cp_parser_nested_name_specifier (parser
,
19382 /*typename_keyword_p=*/true,
19383 /*check_dependency_p=*/true,
19386 if (!nested_name_specifier
)
19387 return error_mark_node
;
19390 /* Even though `typename' is not present, the proposed resolution
19391 to Core Issue 180 says that in `class A<T>::B', `B' should be
19392 considered a type-name, even if `A<T>' is dependent. */
19393 nested_name_specifier
19394 = cp_parser_nested_name_specifier_opt (parser
,
19395 /*typename_keyword_p=*/true,
19396 /*check_dependency_p=*/true,
19399 /* For everything but enumeration types, consider a template-id.
19400 For an enumeration type, consider only a plain identifier. */
19401 if (tag_type
!= enum_type
)
19403 bool template_p
= false;
19406 /* Allow the `template' keyword. */
19407 template_p
= cp_parser_optional_template_keyword (parser
);
19408 /* If we didn't see `template', we don't know if there's a
19409 template-id or not. */
19411 cp_parser_parse_tentatively (parser
);
19412 /* The `template' keyword must follow a nested-name-specifier. */
19413 else if (!nested_name_specifier
&& !globalscope
)
19415 cp_parser_error (parser
, "%<template%> must follow a nested-"
19417 return error_mark_node
;
19420 /* Parse the template-id. */
19421 token
= cp_lexer_peek_token (parser
->lexer
);
19422 decl
= cp_parser_template_id (parser
, template_p
,
19423 /*check_dependency_p=*/true,
19426 /* If we didn't find a template-id, look for an ordinary
19428 if (!template_p
&& !cp_parser_parse_definitely (parser
))
19430 /* We can get here when cp_parser_template_id, called by
19431 cp_parser_class_name with tag_type == none_type, succeeds
19432 and caches a BASELINK. Then, when called again here,
19433 instead of failing and returning an error_mark_node
19434 returns it (see template/typename17.C in C++11).
19435 ??? Could we diagnose this earlier? */
19436 else if (tag_type
== typename_type
&& BASELINK_P (decl
))
19438 cp_parser_diagnose_invalid_type_name (parser
, decl
, token
->location
);
19439 type
= error_mark_node
;
19441 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
19442 in effect, then we must assume that, upon instantiation, the
19443 template will correspond to a class. */
19444 else if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
19445 && tag_type
== typename_type
)
19446 type
= make_typename_type (parser
->scope
, decl
,
19448 /*complain=*/tf_error
);
19449 /* If the `typename' keyword is in effect and DECL is not a type
19450 decl, then type is non existent. */
19451 else if (tag_type
== typename_type
&& TREE_CODE (decl
) != TYPE_DECL
)
19453 else if (TREE_CODE (decl
) == TYPE_DECL
)
19455 type
= check_elaborated_type_specifier (tag_type
, decl
,
19456 /*allow_template_p=*/true);
19458 /* If the next token is a semicolon, this must be a specialization,
19459 instantiation, or friend declaration. Check the scope while we
19460 still know whether or not we had a nested-name-specifier. */
19461 if (type
!= error_mark_node
19462 && !nested_name_specifier
&& !is_friend
19463 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
19464 check_unqualified_spec_or_inst (type
, token
->location
);
19466 else if (decl
== error_mark_node
)
19467 type
= error_mark_node
;
19472 token
= cp_lexer_peek_token (parser
->lexer
);
19473 identifier
= cp_parser_identifier (parser
);
19475 if (identifier
== error_mark_node
)
19477 parser
->scope
= NULL_TREE
;
19478 return error_mark_node
;
19481 /* For a `typename', we needn't call xref_tag. */
19482 if (tag_type
== typename_type
19483 && TREE_CODE (parser
->scope
) != NAMESPACE_DECL
)
19484 return cp_parser_make_typename_type (parser
, identifier
,
19487 /* Template parameter lists apply only if we are not within a
19488 function parameter list. */
19489 bool template_parm_lists_apply
19490 = parser
->num_template_parameter_lists
;
19491 if (template_parm_lists_apply
)
19492 for (cp_binding_level
*s
= current_binding_level
;
19493 s
&& s
->kind
!= sk_template_parms
;
19494 s
= s
->level_chain
)
19495 if (s
->kind
== sk_function_parms
)
19496 template_parm_lists_apply
= false;
19498 /* Look up a qualified name in the usual way. */
19502 tree ambiguous_decls
;
19504 decl
= cp_parser_lookup_name (parser
, identifier
,
19506 /*is_template=*/false,
19507 /*is_namespace=*/false,
19508 /*check_dependency=*/true,
19512 /* If the lookup was ambiguous, an error will already have been
19514 if (ambiguous_decls
)
19515 return error_mark_node
;
19517 /* If we are parsing friend declaration, DECL may be a
19518 TEMPLATE_DECL tree node here. However, we need to check
19519 whether this TEMPLATE_DECL results in valid code. Consider
19520 the following example:
19523 template <class T> class C {};
19526 template <class T> friend class N::C; // #1, valid code
19528 template <class T> class Y {
19529 friend class N::C; // #2, invalid code
19532 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
19533 name lookup of `N::C'. We see that friend declaration must
19534 be template for the code to be valid. Note that
19535 processing_template_decl does not work here since it is
19536 always 1 for the above two cases. */
19538 decl
= (cp_parser_maybe_treat_template_as_class
19539 (decl
, /*tag_name_p=*/is_friend
19540 && template_parm_lists_apply
));
19542 if (TREE_CODE (decl
) != TYPE_DECL
)
19544 cp_parser_diagnose_invalid_type_name (parser
,
19547 return error_mark_node
;
19550 if (TREE_CODE (TREE_TYPE (decl
)) != TYPENAME_TYPE
)
19552 bool allow_template
= (template_parm_lists_apply
19553 || DECL_SELF_REFERENCE_P (decl
));
19554 type
= check_elaborated_type_specifier (tag_type
, decl
,
19557 if (type
== error_mark_node
)
19558 return error_mark_node
;
19561 /* Forward declarations of nested types, such as
19566 are invalid unless all components preceding the final '::'
19567 are complete. If all enclosing types are complete, these
19568 declarations become merely pointless.
19570 Invalid forward declarations of nested types are errors
19571 caught elsewhere in parsing. Those that are pointless arrive
19574 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
19575 && !is_friend
&& is_declaration
19576 && !processing_explicit_instantiation
)
19577 warning (0, "declaration %qD does not declare anything", decl
);
19579 type
= TREE_TYPE (decl
);
19583 /* An elaborated-type-specifier sometimes introduces a new type and
19584 sometimes names an existing type. Normally, the rule is that it
19585 introduces a new type only if there is not an existing type of
19586 the same name already in scope. For example, given:
19589 void f() { struct S s; }
19591 the `struct S' in the body of `f' is the same `struct S' as in
19592 the global scope; the existing definition is used. However, if
19593 there were no global declaration, this would introduce a new
19594 local class named `S'.
19596 An exception to this rule applies to the following code:
19598 namespace N { struct S; }
19600 Here, the elaborated-type-specifier names a new type
19601 unconditionally; even if there is already an `S' in the
19602 containing scope this declaration names a new type.
19603 This exception only applies if the elaborated-type-specifier
19604 forms the complete declaration:
19608 A declaration consisting solely of `class-key identifier ;' is
19609 either a redeclaration of the name in the current scope or a
19610 forward declaration of the identifier as a class name. It
19611 introduces the name into the current scope.
19613 We are in this situation precisely when the next token is a `;'.
19615 An exception to the exception is that a `friend' declaration does
19616 *not* name a new type; i.e., given:
19618 struct S { friend struct T; };
19620 `T' is not a new type in the scope of `S'.
19622 Also, `new struct S' or `sizeof (struct S)' never results in the
19623 definition of a new type; a new type can only be declared in a
19624 declaration context. */
19629 /* Friends have special name lookup rules. */
19630 how
= TAG_how::HIDDEN_FRIEND
;
19631 else if (is_declaration
19632 && cp_lexer_next_token_is (parser
->lexer
,
19634 /* This is a `class-key identifier ;' */
19635 how
= TAG_how::CURRENT_ONLY
;
19637 how
= TAG_how::GLOBAL
;
19640 (template_parm_lists_apply
19641 && (cp_parser_next_token_starts_class_definition_p (parser
)
19642 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)));
19643 /* An unqualified name was used to reference this type, so
19644 there were no qualifying templates. */
19645 if (template_parm_lists_apply
19646 && !cp_parser_check_template_parameters (parser
,
19647 /*num_templates=*/0,
19648 /*template_id*/false,
19650 /*declarator=*/NULL
))
19651 return error_mark_node
;
19653 type
= xref_tag (tag_type
, identifier
, how
, template_p
);
19657 if (type
== error_mark_node
)
19658 return error_mark_node
;
19660 /* Allow attributes on forward declarations of classes. */
19663 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19664 warning (OPT_Wattributes
,
19665 "attributes ignored on uninstantiated type");
19666 else if (tag_type
!= enum_type
19667 && TREE_CODE (type
) != BOUND_TEMPLATE_TEMPLATE_PARM
19668 && CLASSTYPE_TEMPLATE_INSTANTIATION (type
)
19669 && ! processing_explicit_instantiation
)
19670 warning (OPT_Wattributes
,
19671 "attributes ignored on template instantiation");
19672 else if (is_declaration
&& cp_parser_declares_only_class_p (parser
))
19673 cplus_decl_attributes (&type
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
19675 warning (OPT_Wattributes
,
19676 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
19679 if (tag_type
== enum_type
)
19680 cp_parser_maybe_warn_enum_key (parser
, key_loc
, type
, scoped_key
);
19683 /* Diagnose class/struct/union mismatches. IS_DECLARATION is false
19684 for alias definition. */
19685 bool decl_class
= (is_declaration
19686 && cp_parser_declares_only_class_p (parser
));
19687 cp_parser_check_class_key (parser
, key_loc
, tag_type
, type
, false,
19690 /* Indicate whether this class was declared as a `class' or as a
19692 if (CLASS_TYPE_P (type
) && !currently_open_class (type
))
19693 CLASSTYPE_DECLARED_CLASS (type
) = (tag_type
== class_type
);
19696 /* A "<" cannot follow an elaborated type specifier. If that
19697 happens, the user was probably trying to form a template-id. */
19698 cp_parser_check_for_invalid_template_id (parser
, type
, tag_type
,
19704 /* Parse an enum-specifier.
19707 enum-head { enumerator-list [opt] }
19708 enum-head { enumerator-list , } [C++0x]
19711 enum-key identifier [opt] enum-base [opt]
19712 enum-key nested-name-specifier identifier enum-base [opt]
19717 enum struct [C++0x]
19720 : type-specifier-seq
19722 opaque-enum-specifier:
19723 enum-key identifier enum-base [opt] ;
19726 enum-key attributes[opt] identifier [opt] enum-base [opt]
19727 { enumerator-list [opt] }attributes[opt]
19728 enum-key attributes[opt] identifier [opt] enum-base [opt]
19729 { enumerator-list, }attributes[opt] [C++0x]
19731 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
19732 if the token stream isn't an enum-specifier after all. */
19735 cp_parser_enum_specifier (cp_parser
* parser
)
19738 tree type
= NULL_TREE
;
19740 tree nested_name_specifier
= NULL_TREE
;
19742 bool scoped_enum_p
= false;
19743 bool has_underlying_type
= false;
19744 bool nested_being_defined
= false;
19745 bool new_value_list
= false;
19746 bool is_new_type
= false;
19747 bool is_unnamed
= false;
19748 tree underlying_type
= NULL_TREE
;
19749 cp_token
*type_start_token
= NULL
;
19750 auto cleanup
= make_temp_override (parser
->colon_corrects_to_scope_p
, false);
19752 /* Parse tentatively so that we can back up if we don't find a
19754 cp_parser_parse_tentatively (parser
);
19756 /* Caller guarantees that the current token is 'enum', an identifier
19757 possibly follows, and the token after that is an opening brace.
19758 If we don't have an identifier, fabricate an anonymous name for
19759 the enumeration being defined. */
19760 cp_lexer_consume_token (parser
->lexer
);
19762 /* Parse the "class" or "struct", which indicates a scoped
19763 enumeration type in C++0x. */
19764 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CLASS
)
19765 || cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STRUCT
))
19767 if (cxx_dialect
< cxx11
)
19768 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19770 /* Consume the `struct' or `class' token. */
19771 cp_lexer_consume_token (parser
->lexer
);
19773 scoped_enum_p
= true;
19776 attributes
= cp_parser_attributes_opt (parser
);
19778 /* Clear the qualification. */
19779 parser
->scope
= NULL_TREE
;
19780 parser
->qualifying_scope
= NULL_TREE
;
19781 parser
->object_scope
= NULL_TREE
;
19783 /* Figure out in what scope the declaration is being placed. */
19784 prev_scope
= current_scope ();
19786 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
19788 push_deferring_access_checks (dk_no_check
);
19789 nested_name_specifier
19790 = cp_parser_nested_name_specifier_opt (parser
,
19791 /*typename_keyword_p=*/true,
19792 /*check_dependency_p=*/false,
19794 /*is_declaration=*/false);
19796 if (nested_name_specifier
)
19800 identifier
= cp_parser_identifier (parser
);
19801 name
= cp_parser_lookup_name (parser
, identifier
,
19803 /*is_template=*/false,
19804 /*is_namespace=*/false,
19805 /*check_dependency=*/true,
19806 /*ambiguous_decls=*/NULL
,
19808 if (name
&& name
!= error_mark_node
)
19810 type
= TREE_TYPE (name
);
19811 if (TREE_CODE (type
) == TYPENAME_TYPE
)
19813 /* Are template enums allowed in ISO? */
19814 if (template_parm_scope_p ())
19815 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
19816 "%qD is an enumeration template", name
);
19817 /* ignore a typename reference, for it will be solved by name
19822 else if (nested_name_specifier
== error_mark_node
)
19823 /* We already issued an error. */;
19826 error_at (type_start_token
->location
,
19827 "%qD does not name an enumeration in %qT",
19828 identifier
, nested_name_specifier
);
19829 nested_name_specifier
= error_mark_node
;
19834 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
19835 identifier
= cp_parser_identifier (parser
);
19838 identifier
= make_anon_name ();
19841 error_at (type_start_token
->location
,
19842 "unnamed scoped enum is not allowed");
19845 pop_deferring_access_checks ();
19847 /* Check for the `:' that denotes a specified underlying type in C++0x.
19848 Note that a ':' could also indicate a bitfield width, however. */
19849 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
19851 cp_decl_specifier_seq type_specifiers
;
19853 /* Consume the `:'. */
19854 cp_lexer_consume_token (parser
->lexer
);
19856 /* Parse the type-specifier-seq. */
19857 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
19858 /*is_declaration=*/false,
19859 /*is_trailing_return=*/false,
19862 /* At this point this is surely not elaborated type specifier. */
19863 if (!cp_parser_parse_definitely (parser
))
19866 if (cxx_dialect
< cxx11
)
19867 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS
);
19869 has_underlying_type
= true;
19871 /* If that didn't work, stop. */
19872 if (type_specifiers
.type
!= error_mark_node
)
19874 underlying_type
= grokdeclarator (NULL
, &type_specifiers
, TYPENAME
,
19875 /*initialized=*/0, NULL
);
19876 if (underlying_type
== error_mark_node
19877 || check_for_bare_parameter_packs (underlying_type
))
19878 underlying_type
= NULL_TREE
;
19882 /* Look for the `{' but don't consume it yet. */
19883 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19885 if (cxx_dialect
< cxx11
|| (!scoped_enum_p
&& !underlying_type
))
19887 if (has_underlying_type
)
19888 cp_parser_commit_to_tentative_parse (parser
);
19889 cp_parser_error (parser
, "expected %<{%>");
19890 if (has_underlying_type
)
19891 return error_mark_node
;
19893 /* An opaque-enum-specifier must have a ';' here. */
19894 if ((scoped_enum_p
|| underlying_type
)
19895 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
19897 if (has_underlying_type
)
19898 cp_parser_commit_to_tentative_parse (parser
);
19899 cp_parser_error (parser
, "expected %<;%> or %<{%>");
19900 if (has_underlying_type
)
19901 return error_mark_node
;
19905 if (!has_underlying_type
&& !cp_parser_parse_definitely (parser
))
19908 if (nested_name_specifier
)
19910 if (CLASS_TYPE_P (nested_name_specifier
))
19912 nested_being_defined
= TYPE_BEING_DEFINED (nested_name_specifier
);
19913 TYPE_BEING_DEFINED (nested_name_specifier
) = 1;
19914 push_scope (nested_name_specifier
);
19916 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
19917 push_nested_namespace (nested_name_specifier
);
19920 /* Issue an error message if type-definitions are forbidden here. */
19921 if (!cp_parser_check_type_definition (parser
))
19922 type
= error_mark_node
;
19924 /* Create the new type. We do this before consuming the opening
19925 brace so the enum will be recorded as being on the line of its
19926 tag (or the 'enum' keyword, if there is no tag). */
19927 type
= start_enum (identifier
, type
, underlying_type
,
19928 attributes
, scoped_enum_p
, &is_new_type
);
19930 /* If the next token is not '{' it is an opaque-enum-specifier or an
19931 elaborated-type-specifier. */
19932 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
19934 timevar_push (TV_PARSE_ENUM
);
19935 if (nested_name_specifier
19936 && nested_name_specifier
!= error_mark_node
)
19938 /* The following catches invalid code such as:
19939 enum class S<int>::E { A, B, C }; */
19940 if (!processing_specialization
19941 && CLASS_TYPE_P (nested_name_specifier
)
19942 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier
))
19943 error_at (type_start_token
->location
, "cannot add an enumerator "
19944 "list to a template instantiation");
19946 if (TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
19948 error_at (type_start_token
->location
,
19949 "%<%T::%E%> has not been declared",
19950 TYPE_CONTEXT (nested_name_specifier
),
19951 nested_name_specifier
);
19952 type
= error_mark_node
;
19954 else if (TREE_CODE (nested_name_specifier
) != NAMESPACE_DECL
19955 && !CLASS_TYPE_P (nested_name_specifier
))
19957 error_at (type_start_token
->location
, "nested name specifier "
19958 "%qT for enum declaration does not name a class "
19959 "or namespace", nested_name_specifier
);
19960 type
= error_mark_node
;
19962 /* If that scope does not contain the scope in which the
19963 class was originally declared, the program is invalid. */
19964 else if (prev_scope
&& !is_ancestor (prev_scope
,
19965 nested_name_specifier
))
19967 if (at_namespace_scope_p ())
19968 error_at (type_start_token
->location
,
19969 "declaration of %qD in namespace %qD which does not "
19971 type
, prev_scope
, nested_name_specifier
);
19973 error_at (type_start_token
->location
,
19974 "declaration of %qD in %qD which does not "
19976 type
, prev_scope
, nested_name_specifier
);
19977 type
= error_mark_node
;
19979 /* If that scope is the scope where the declaration is being placed
19980 the program is invalid. */
19981 else if (CLASS_TYPE_P (nested_name_specifier
)
19982 && CLASS_TYPE_P (prev_scope
)
19983 && same_type_p (nested_name_specifier
, prev_scope
))
19985 permerror (type_start_token
->location
,
19986 "extra qualification not allowed");
19987 nested_name_specifier
= NULL_TREE
;
19992 begin_scope (sk_scoped_enum
, type
);
19994 /* Consume the opening brace. */
19995 matching_braces braces
;
19996 braces
.consume_open (parser
);
19998 if (type
== error_mark_node
)
19999 ; /* Nothing to add */
20000 else if (OPAQUE_ENUM_P (type
)
20001 || (cxx_dialect
> cxx98
&& processing_specialization
))
20003 new_value_list
= true;
20004 SET_OPAQUE_ENUM_P (type
, false);
20005 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
20009 error_at (type_start_token
->location
,
20010 "multiple definition of %q#T", type
);
20011 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type
)),
20012 "previous definition here");
20013 type
= error_mark_node
;
20016 if (type
== error_mark_node
)
20017 cp_parser_skip_to_end_of_block_or_statement (parser
);
20018 /* If the next token is not '}', then there are some enumerators. */
20019 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
20021 if (is_unnamed
&& !scoped_enum_p
)
20022 pedwarn (type_start_token
->location
, OPT_Wpedantic
,
20023 "ISO C++ forbids empty unnamed enum");
20027 /* We've seen a '{' so we know we're in an enum-specifier.
20028 Commit to any tentative parse to get syntax errors. */
20029 cp_parser_commit_to_tentative_parse (parser
);
20030 cp_parser_enumerator_list (parser
, type
);
20033 /* Consume the final '}'. */
20034 braces
.require_close (parser
);
20038 timevar_pop (TV_PARSE_ENUM
);
20042 /* If a ';' follows, then it is an opaque-enum-specifier
20043 and additional restrictions apply. */
20044 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
20047 error_at (type_start_token
->location
,
20048 "opaque-enum-specifier without name");
20049 else if (nested_name_specifier
)
20050 error_at (type_start_token
->location
,
20051 "opaque-enum-specifier must use a simple identifier");
20055 /* Look for trailing attributes to apply to this enumeration, and
20056 apply them if appropriate. */
20057 if (cp_parser_allow_gnu_extensions_p (parser
))
20059 tree trailing_attr
= cp_parser_gnu_attributes_opt (parser
);
20060 cplus_decl_attributes (&type
,
20062 (int) ATTR_FLAG_TYPE_IN_PLACE
);
20065 /* Finish up the enumeration. */
20066 if (type
!= error_mark_node
)
20068 if (new_value_list
)
20069 finish_enum_value_list (type
);
20071 finish_enum (type
);
20074 if (nested_name_specifier
)
20076 if (CLASS_TYPE_P (nested_name_specifier
))
20078 TYPE_BEING_DEFINED (nested_name_specifier
) = nested_being_defined
;
20079 pop_scope (nested_name_specifier
);
20081 else if (TREE_CODE (nested_name_specifier
) == NAMESPACE_DECL
)
20082 pop_nested_namespace (nested_name_specifier
);
20087 /* Parse an enumerator-list. The enumerators all have the indicated
20091 enumerator-definition
20092 enumerator-list , enumerator-definition */
20095 cp_parser_enumerator_list (cp_parser
* parser
, tree type
)
20099 /* Parse an enumerator-definition. */
20100 cp_parser_enumerator_definition (parser
, type
);
20102 /* If the next token is not a ',', we've reached the end of
20104 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
20106 /* Otherwise, consume the `,' and keep going. */
20107 cp_lexer_consume_token (parser
->lexer
);
20108 /* If the next token is a `}', there is a trailing comma. */
20109 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
20111 if (cxx_dialect
< cxx11
)
20112 pedwarn (input_location
, OPT_Wpedantic
,
20113 "comma at end of enumerator list");
20119 /* Parse an enumerator-definition. The enumerator has the indicated
20122 enumerator-definition:
20124 enumerator = constant-expression
20131 enumerator-definition:
20132 enumerator attributes [opt]
20133 enumerator attributes [opt] = constant-expression */
20136 cp_parser_enumerator_definition (cp_parser
* parser
, tree type
)
20142 /* Save the input location because we are interested in the location
20143 of the identifier and not the location of the explicit value. */
20144 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20146 /* Look for the identifier. */
20147 identifier
= cp_parser_identifier (parser
);
20148 if (identifier
== error_mark_node
)
20151 /* Parse any specified attributes. */
20152 tree attrs
= cp_parser_attributes_opt (parser
);
20154 /* If the next token is an '=', then there is an explicit value. */
20155 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
20157 /* Consume the `=' token. */
20158 cp_lexer_consume_token (parser
->lexer
);
20159 /* Parse the value. */
20160 value
= cp_parser_constant_expression (parser
);
20165 /* If we are processing a template, make sure the initializer of the
20166 enumerator doesn't contain any bare template parameter pack. */
20167 if (check_for_bare_parameter_packs (value
))
20168 value
= error_mark_node
;
20170 /* Create the enumerator. */
20171 build_enumerator (identifier
, value
, type
, attrs
, loc
);
20174 /* Parse a namespace-name.
20177 original-namespace-name
20180 Returns the NAMESPACE_DECL for the namespace. */
20183 cp_parser_namespace_name (cp_parser
* parser
)
20186 tree namespace_decl
;
20188 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20190 /* Get the name of the namespace. */
20191 identifier
= cp_parser_identifier (parser
);
20192 if (identifier
== error_mark_node
)
20193 return error_mark_node
;
20195 /* Look up the identifier in the currently active scope. Look only
20196 for namespaces, due to:
20198 [basic.lookup.udir]
20200 When looking up a namespace-name in a using-directive or alias
20201 definition, only namespace names are considered.
20205 [basic.lookup.qual]
20207 During the lookup of a name preceding the :: scope resolution
20208 operator, object, function, and enumerator names are ignored.
20210 (Note that cp_parser_qualifying_entity only calls this
20211 function if the token after the name is the scope resolution
20213 namespace_decl
= cp_parser_lookup_name (parser
, identifier
,
20215 /*is_template=*/false,
20216 /*is_namespace=*/true,
20217 /*check_dependency=*/true,
20218 /*ambiguous_decls=*/NULL
,
20220 /* If it's not a namespace, issue an error. */
20221 if (namespace_decl
== error_mark_node
20222 || TREE_CODE (namespace_decl
) != NAMESPACE_DECL
)
20224 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
20226 auto_diagnostic_group d
;
20228 if (namespace_decl
== error_mark_node
20229 && parser
->scope
&& TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
20230 hint
= suggest_alternative_in_explicit_scope (token
->location
,
20233 if (const char *suggestion
= hint
.suggestion ())
20235 gcc_rich_location
richloc (token
->location
);
20236 richloc
.add_fixit_replace (suggestion
);
20237 error_at (&richloc
,
20238 "%qD is not a namespace-name; did you mean %qs?",
20239 identifier
, suggestion
);
20242 error_at (token
->location
, "%qD is not a namespace-name",
20246 cp_parser_error (parser
, "expected namespace-name");
20247 namespace_decl
= error_mark_node
;
20250 return namespace_decl
;
20253 /* Parse a namespace-definition.
20255 namespace-definition:
20256 named-namespace-definition
20257 unnamed-namespace-definition
20259 named-namespace-definition:
20260 original-namespace-definition
20261 extension-namespace-definition
20263 original-namespace-definition:
20264 namespace identifier { namespace-body }
20266 extension-namespace-definition:
20267 namespace original-namespace-name { namespace-body }
20269 unnamed-namespace-definition:
20270 namespace { namespace-body } */
20273 cp_parser_namespace_definition (cp_parser
* parser
)
20276 int nested_definition_count
= 0;
20278 cp_ensure_no_omp_declare_simd (parser
);
20279 cp_ensure_no_oacc_routine (parser
);
20281 bool is_inline
= cp_lexer_next_token_is_keyword (parser
->lexer
, RID_INLINE
);
20282 const bool topmost_inline_p
= is_inline
;
20286 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES
);
20287 cp_lexer_consume_token (parser
->lexer
);
20290 /* Look for the `namespace' keyword. */
20292 = cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
20294 /* Parse any specified attributes before the identifier. */
20295 tree attribs
= cp_parser_attributes_opt (parser
);
20299 identifier
= NULL_TREE
;
20301 bool nested_inline_p
= cp_lexer_next_token_is_keyword (parser
->lexer
,
20303 if (nested_inline_p
&& nested_definition_count
!= 0)
20305 if (cxx_dialect
< cxx20
)
20306 pedwarn (cp_lexer_peek_token (parser
->lexer
)->location
,
20307 OPT_Wpedantic
, "nested inline namespace definitions only "
20308 "available with %<-std=c++20%> or %<-std=gnu++20%>");
20309 cp_lexer_consume_token (parser
->lexer
);
20312 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
20314 identifier
= cp_parser_identifier (parser
);
20316 if (cp_next_tokens_can_be_std_attribute_p (parser
))
20317 pedwarn (input_location
, OPT_Wpedantic
,
20318 "standard attributes on namespaces must precede "
20319 "the namespace name");
20321 /* Parse any attributes specified after the identifier. */
20322 attribs
= attr_chainon (attribs
, cp_parser_attributes_opt (parser
));
20325 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
20327 /* Don't forget that the innermost namespace might have been
20328 marked as inline. Use |= because we cannot overwrite
20329 IS_INLINE in case the outermost namespace is inline, but
20330 there are no nested inlines. */
20331 is_inline
|= nested_inline_p
;
20335 if (!nested_definition_count
&& cxx_dialect
< cxx17
)
20336 pedwarn (input_location
, OPT_Wpedantic
,
20337 "nested namespace definitions only available with "
20338 "%<-std=c++17%> or %<-std=gnu++17%>");
20340 /* Nested namespace names can create new namespaces (unlike
20341 other qualified-ids). */
20342 if (int count
= (identifier
20343 ? push_namespace (identifier
, nested_inline_p
)
20345 nested_definition_count
+= count
;
20347 cp_parser_error (parser
, "nested namespace name required");
20348 cp_lexer_consume_token (parser
->lexer
);
20351 if (nested_definition_count
&& !identifier
)
20352 cp_parser_error (parser
, "namespace name required");
20354 if (nested_definition_count
&& attribs
)
20355 error_at (token
->location
,
20356 "a nested namespace definition cannot have attributes");
20357 if (nested_definition_count
&& topmost_inline_p
)
20358 error_at (token
->location
,
20359 "a nested namespace definition cannot be inline");
20361 /* Start the namespace. */
20362 nested_definition_count
+= push_namespace (identifier
, is_inline
);
20364 bool has_visibility
= handle_namespace_attrs (current_namespace
, attribs
);
20366 warning (OPT_Wnamespaces
, "namespace %qD entered", current_namespace
);
20368 /* Look for the `{' to validate starting the namespace. */
20369 matching_braces braces
;
20370 if (braces
.require_open (parser
))
20372 /* Parse the body of the namespace. */
20373 cp_parser_namespace_body (parser
);
20375 /* Look for the final `}'. */
20376 braces
.require_close (parser
);
20379 if (has_visibility
)
20380 pop_visibility (1);
20382 /* Pop the nested namespace definitions. */
20383 while (nested_definition_count
--)
20387 /* Parse a namespace-body.
20390 declaration-seq [opt] */
20393 cp_parser_namespace_body (cp_parser
* parser
)
20395 cp_parser_declaration_seq_opt (parser
);
20398 /* Parse a namespace-alias-definition.
20400 namespace-alias-definition:
20401 namespace identifier = qualified-namespace-specifier ; */
20404 cp_parser_namespace_alias_definition (cp_parser
* parser
)
20407 tree namespace_specifier
;
20409 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20411 /* Look for the `namespace' keyword. */
20412 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
20413 /* Look for the identifier. */
20414 identifier
= cp_parser_identifier (parser
);
20415 if (identifier
== error_mark_node
)
20417 /* Look for the `=' token. */
20418 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
)
20419 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
20421 error_at (token
->location
, "%<namespace%> definition is not allowed here");
20422 /* Skip the definition. */
20423 cp_lexer_consume_token (parser
->lexer
);
20424 if (cp_parser_skip_to_closing_brace (parser
))
20425 cp_lexer_consume_token (parser
->lexer
);
20428 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
20429 /* Look for the qualified-namespace-specifier. */
20430 namespace_specifier
20431 = cp_parser_qualified_namespace_specifier (parser
);
20432 cp_warn_deprecated_use_scopes (namespace_specifier
);
20433 /* Look for the `;' token. */
20434 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20436 /* Register the alias in the symbol table. */
20437 do_namespace_alias (identifier
, namespace_specifier
);
20440 /* Parse a qualified-namespace-specifier.
20442 qualified-namespace-specifier:
20443 :: [opt] nested-name-specifier [opt] namespace-name
20445 Returns a NAMESPACE_DECL corresponding to the specified
20449 cp_parser_qualified_namespace_specifier (cp_parser
* parser
)
20451 /* Look for the optional `::'. */
20452 cp_parser_global_scope_opt (parser
,
20453 /*current_scope_valid_p=*/false);
20455 /* Look for the optional nested-name-specifier. */
20456 cp_parser_nested_name_specifier_opt (parser
,
20457 /*typename_keyword_p=*/false,
20458 /*check_dependency_p=*/true,
20460 /*is_declaration=*/true);
20462 return cp_parser_namespace_name (parser
);
20465 /* Subroutine of cp_parser_using_declaration. */
20468 finish_using_decl (tree qscope
, tree identifier
, bool typename_p
= false)
20470 tree decl
= NULL_TREE
;
20471 if (at_class_scope_p ())
20473 /* Create the USING_DECL. */
20474 decl
= do_class_using_decl (qscope
, identifier
);
20476 if (check_for_bare_parameter_packs (decl
))
20477 return error_mark_node
;
20479 if (decl
&& typename_p
)
20480 USING_DECL_TYPENAME_P (decl
) = 1;
20482 /* Add it to the list of members in this class. */
20483 finish_member_declaration (decl
);
20486 finish_nonmember_using_decl (qscope
, identifier
);
20490 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
20491 access declaration.
20494 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
20495 using :: unqualified-id ;
20497 access-declaration:
20503 cp_parser_using_declaration (cp_parser
* parser
,
20504 bool access_declaration_p
)
20507 bool typename_p
= false;
20508 bool global_scope_p
;
20511 int oldcount
= errorcount
;
20512 cp_token
*diag_token
= NULL
;
20514 if (access_declaration_p
)
20516 diag_token
= cp_lexer_peek_token (parser
->lexer
);
20517 cp_parser_parse_tentatively (parser
);
20521 /* Look for the `using' keyword. */
20522 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20525 /* Peek at the next token. */
20526 token
= cp_lexer_peek_token (parser
->lexer
);
20527 /* See if it's `typename'. */
20528 if (token
->keyword
== RID_TYPENAME
)
20530 /* Remember that we've seen it. */
20532 /* Consume the `typename' token. */
20533 cp_lexer_consume_token (parser
->lexer
);
20537 /* Look for the optional global scope qualification. */
20539 = (cp_parser_global_scope_opt (parser
,
20540 /*current_scope_valid_p=*/false)
20543 /* If we saw `typename', or didn't see `::', then there must be a
20544 nested-name-specifier present. */
20545 if (typename_p
|| !global_scope_p
)
20547 qscope
= cp_parser_nested_name_specifier (parser
, typename_p
,
20548 /*check_dependency_p=*/true,
20550 /*is_declaration=*/true);
20551 if (!qscope
&& !cp_parser_uncommitted_to_tentative_parse_p (parser
))
20553 cp_parser_skip_to_end_of_block_or_statement (parser
);
20557 /* Otherwise, we could be in either of the two productions. In that
20558 case, treat the nested-name-specifier as optional. */
20560 qscope
= cp_parser_nested_name_specifier_opt (parser
,
20561 /*typename_keyword_p=*/false,
20562 /*check_dependency_p=*/true,
20564 /*is_declaration=*/true);
20566 qscope
= global_namespace
;
20568 cp_warn_deprecated_use_scopes (qscope
);
20570 if (access_declaration_p
&& cp_parser_error_occurred (parser
))
20571 /* Something has already gone wrong; there's no need to parse
20572 further. Since an error has occurred, the return value of
20573 cp_parser_parse_definitely will be false, as required. */
20574 return cp_parser_parse_definitely (parser
);
20576 token
= cp_lexer_peek_token (parser
->lexer
);
20577 /* Parse the unqualified-id. */
20578 identifier
= cp_parser_unqualified_id (parser
,
20579 /*template_keyword_p=*/false,
20580 /*check_dependency_p=*/true,
20581 /*declarator_p=*/true,
20582 /*optional_p=*/false);
20584 if (access_declaration_p
)
20586 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
20587 cp_parser_simulate_error (parser
);
20588 if (!cp_parser_parse_definitely (parser
))
20591 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
20593 cp_token
*ell
= cp_lexer_consume_token (parser
->lexer
);
20594 if (cxx_dialect
< cxx17
)
20595 pedwarn (ell
->location
, 0,
20596 "pack expansion in using-declaration only available "
20597 "with %<-std=c++17%> or %<-std=gnu++17%>");
20598 qscope
= make_pack_expansion (qscope
);
20601 /* The function we call to handle a using-declaration is different
20602 depending on what scope we are in. */
20603 if (qscope
== error_mark_node
|| identifier
== error_mark_node
)
20605 else if (!identifier_p (identifier
)
20606 && TREE_CODE (identifier
) != BIT_NOT_EXPR
)
20607 /* [namespace.udecl]
20609 A using declaration shall not name a template-id. */
20610 error_at (token
->location
,
20611 "a template-id may not appear in a using-declaration");
20614 tree decl
= finish_using_decl (qscope
, identifier
, typename_p
);
20616 if (decl
== error_mark_node
)
20618 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20623 if (!access_declaration_p
20624 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
20626 cp_token
*comma
= cp_lexer_consume_token (parser
->lexer
);
20627 if (cxx_dialect
< cxx17
)
20628 pedwarn (comma
->location
, 0,
20629 "comma-separated list in using-declaration only available "
20630 "with %<-std=c++17%> or %<-std=gnu++17%>");
20634 /* Look for the final `;'. */
20635 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20637 if (access_declaration_p
&& errorcount
== oldcount
)
20638 warning_at (diag_token
->location
, OPT_Wdeprecated
,
20639 "access declarations are deprecated "
20640 "in favour of using-declarations; "
20641 "suggestion: add the %<using%> keyword");
20646 /* C++20 using enum declaration.
20648 using-enum-declaration :
20649 using elaborated-enum-specifier ; */
20652 cp_parser_using_enum (cp_parser
*parser
)
20654 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20656 /* Using cp_parser_elaborated_type_specifier rejects typedef-names, which
20657 breaks one of the motivating examples in using-enum-5.C.
20658 cp_parser_simple_type_specifier seems to be closer to what we actually
20659 want, though that hasn't been properly specified yet. */
20661 /* Consume 'enum'. */
20662 gcc_checking_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ENUM
));
20663 cp_lexer_consume_token (parser
->lexer
);
20665 cp_token
*start
= cp_lexer_peek_token (parser
->lexer
);
20667 tree type
= (cp_parser_simple_type_specifier
20668 (parser
, NULL
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
20670 cp_token
*end
= cp_lexer_previous_token (parser
->lexer
);
20672 if (type
== error_mark_node
20673 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
20675 cp_parser_skip_to_end_of_block_or_statement (parser
);
20678 if (TREE_CODE (type
) == TYPE_DECL
)
20679 type
= TREE_TYPE (type
);
20681 /* The elaborated-enum-specifier shall not name a dependent type and the type
20682 shall have a reachable enum-specifier. */
20683 const char *msg
= nullptr;
20684 if (cxx_dialect
< cxx20
)
20685 msg
= _("%<using enum%> "
20686 "only available with %<-std=c++20%> or %<-std=gnu++20%>");
20687 else if (dependent_type_p (type
))
20688 msg
= _("%<using enum%> of dependent type %qT");
20689 else if (TREE_CODE (type
) != ENUMERAL_TYPE
)
20690 msg
= _("%<using enum%> of non-enumeration type %q#T");
20691 else if (!COMPLETE_TYPE_P (type
))
20692 msg
= _("%<using enum%> of incomplete type %qT");
20693 else if (OPAQUE_ENUM_P (type
))
20694 msg
= _("%<using enum%> of %qT before its enum-specifier");
20697 location_t loc
= make_location (start
, start
, end
);
20698 auto_diagnostic_group g
;
20699 error_at (loc
, msg
, type
);
20700 loc
= location_of (type
);
20701 if (cxx_dialect
< cxx20
|| loc
== input_location
)
20703 else if (OPAQUE_ENUM_P (type
))
20704 inform (loc
, "opaque-enum-declaration here");
20706 inform (loc
, "declared here");
20709 /* A using-enum-declaration introduces the enumerator names of the named
20710 enumeration as if by a using-declaration for each enumerator. */
20711 if (TREE_CODE (type
) == ENUMERAL_TYPE
)
20712 for (tree v
= TYPE_VALUES (type
); v
; v
= TREE_CHAIN (v
))
20713 finish_using_decl (type
, DECL_NAME (TREE_VALUE (v
)));
20716 /* Parse an alias-declaration.
20719 using identifier attribute-specifier-seq [opt] = type-id */
20722 cp_parser_alias_declaration (cp_parser
* parser
)
20724 tree id
, type
, decl
, pushed_scope
= NULL_TREE
, attributes
;
20725 location_t id_location
, type_location
;
20726 cp_declarator
*declarator
;
20727 cp_decl_specifier_seq decl_specs
;
20729 const char *saved_message
= NULL
;
20731 /* Look for the `using' keyword. */
20732 cp_token
*using_token
20733 = cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20734 if (using_token
== NULL
)
20735 return error_mark_node
;
20737 id_location
= cp_lexer_peek_token (parser
->lexer
)->location
;
20738 id
= cp_parser_identifier (parser
);
20739 if (id
== error_mark_node
)
20740 return error_mark_node
;
20742 cp_token
*attrs_token
= cp_lexer_peek_token (parser
->lexer
);
20743 attributes
= cp_parser_attributes_opt (parser
);
20744 if (attributes
== error_mark_node
)
20745 return error_mark_node
;
20747 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
20749 if (cp_parser_error_occurred (parser
))
20750 return error_mark_node
;
20752 cp_parser_commit_to_tentative_parse (parser
);
20754 /* Now we are going to parse the type-id of the declaration. */
20759 "A type-specifier-seq shall not define a class or enumeration
20760 unless it appears in the type-id of an alias-declaration (7.1.3) that
20761 is not the declaration of a template-declaration."
20763 In other words, if we currently are in an alias template, the
20764 type-id should not define a type.
20766 So let's set parser->type_definition_forbidden_message in that
20767 case; cp_parser_check_type_definition (called by
20768 cp_parser_class_specifier) will then emit an error if a type is
20769 defined in the type-id. */
20770 if (parser
->num_template_parameter_lists
)
20772 saved_message
= parser
->type_definition_forbidden_message
;
20773 parser
->type_definition_forbidden_message
=
20774 G_("types may not be defined in alias template declarations");
20777 type
= cp_parser_type_id (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
20780 /* Restore the error message if need be. */
20781 if (parser
->num_template_parameter_lists
)
20782 parser
->type_definition_forbidden_message
= saved_message
;
20784 if (type
== error_mark_node
20785 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
20787 cp_parser_skip_to_end_of_block_or_statement (parser
);
20788 return error_mark_node
;
20791 /* A typedef-name can also be introduced by an alias-declaration. The
20792 identifier following the using keyword becomes a typedef-name. It has
20793 the same semantics as if it were introduced by the typedef
20794 specifier. In particular, it does not define a new type and it shall
20795 not appear in the type-id. */
20797 clear_decl_specs (&decl_specs
);
20798 decl_specs
.type
= type
;
20799 if (attributes
!= NULL_TREE
)
20801 decl_specs
.attributes
= attributes
;
20802 set_and_check_decl_spec_loc (&decl_specs
,
20806 set_and_check_decl_spec_loc (&decl_specs
,
20809 set_and_check_decl_spec_loc (&decl_specs
,
20812 decl_specs
.locations
[ds_type_spec
] = type_location
;
20814 if (parser
->num_template_parameter_lists
20815 && !cp_parser_check_template_parameters (parser
,
20816 /*num_templates=*/0,
20817 /*template_id*/false,
20819 /*declarator=*/NULL
))
20820 return error_mark_node
;
20822 declarator
= make_id_declarator (NULL_TREE
, id
, sfk_none
, id_location
);
20824 member_p
= at_class_scope_p ();
20826 decl
= grokfield (declarator
, &decl_specs
, NULL_TREE
, false,
20827 NULL_TREE
, attributes
);
20829 decl
= start_decl (declarator
, &decl_specs
, 0,
20830 attributes
, NULL_TREE
, &pushed_scope
);
20831 if (decl
== error_mark_node
)
20834 cp_finish_decl (decl
, NULL_TREE
, 0, NULL_TREE
, 0);
20837 pop_scope (pushed_scope
);
20839 /* If decl is a template, return its TEMPLATE_DECL so that it gets
20840 added into the symbol table; otherwise, return the TYPE_DECL. */
20841 if (DECL_LANG_SPECIFIC (decl
)
20842 && DECL_TEMPLATE_INFO (decl
)
20843 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl
)))
20845 decl
= DECL_TI_TEMPLATE (decl
);
20847 check_member_template (decl
);
20853 /* Parse a using-directive.
20856 using namespace :: [opt] nested-name-specifier [opt]
20857 namespace-name ; */
20860 cp_parser_using_directive (cp_parser
* parser
)
20862 tree namespace_decl
;
20865 /* Look for the `using' keyword. */
20866 cp_parser_require_keyword (parser
, RID_USING
, RT_USING
);
20867 /* And the `namespace' keyword. */
20868 cp_parser_require_keyword (parser
, RID_NAMESPACE
, RT_NAMESPACE
);
20869 /* Look for the optional `::' operator. */
20870 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
20871 /* And the optional nested-name-specifier. */
20872 cp_parser_nested_name_specifier_opt (parser
,
20873 /*typename_keyword_p=*/false,
20874 /*check_dependency_p=*/true,
20876 /*is_declaration=*/true);
20877 /* Get the namespace being used. */
20878 namespace_decl
= cp_parser_namespace_name (parser
);
20879 cp_warn_deprecated_use_scopes (namespace_decl
);
20880 /* And any specified attributes. */
20881 attribs
= cp_parser_attributes_opt (parser
);
20883 /* Update the symbol table. */
20884 finish_using_directive (namespace_decl
, attribs
);
20886 /* Look for the final `;'. */
20887 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
20890 /* Parse an asm-definition.
20897 asm-qualifier-list:
20899 asm-qualifier-list asm-qualifier
20902 asm ( string-literal ) ;
20907 asm asm-qualifier-list [opt] ( string-literal ) ;
20908 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt] ) ;
20909 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20910 : asm-operand-list [opt] ) ;
20911 asm asm-qualifier-list [opt] ( string-literal : asm-operand-list [opt]
20912 : asm-operand-list [opt]
20913 : asm-clobber-list [opt] ) ;
20914 asm asm-qualifier-list [opt] ( string-literal : : asm-operand-list [opt]
20915 : asm-clobber-list [opt]
20916 : asm-goto-list ) ;
20918 The form with asm-goto-list is valid if and only if the asm-qualifier-list
20919 contains goto, and is the only allowed form in that case. No duplicates are
20920 allowed in an asm-qualifier-list. */
20923 cp_parser_asm_definition (cp_parser
* parser
)
20926 tree outputs
= NULL_TREE
;
20927 tree inputs
= NULL_TREE
;
20928 tree clobbers
= NULL_TREE
;
20929 tree labels
= NULL_TREE
;
20931 bool extended_p
= false;
20932 bool invalid_inputs_p
= false;
20933 bool invalid_outputs_p
= false;
20934 required_token missing
= RT_NONE
;
20935 location_t asm_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
20937 /* Look for the `asm' keyword. */
20938 cp_parser_require_keyword (parser
, RID_ASM
, RT_ASM
);
20940 /* In C++20, unevaluated inline assembly is permitted in constexpr
20942 if (parser
->in_function_body
20943 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
20944 && (cxx_dialect
< cxx20
))
20945 pedwarn (asm_loc
, 0, "%<asm%> in %<constexpr%> function only available "
20946 "with %<-std=c++20%> or %<-std=gnu++20%>");
20948 /* Handle the asm-qualifier-list. */
20949 location_t volatile_loc
= UNKNOWN_LOCATION
;
20950 location_t inline_loc
= UNKNOWN_LOCATION
;
20951 location_t goto_loc
= UNKNOWN_LOCATION
;
20952 location_t first_loc
= UNKNOWN_LOCATION
;
20954 if (cp_parser_allow_gnu_extensions_p (parser
))
20957 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
20958 location_t loc
= token
->location
;
20959 switch (cp_lexer_peek_token (parser
->lexer
)->keyword
)
20964 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20966 inform (volatile_loc
, "first seen here");
20970 if (!parser
->in_function_body
)
20971 warning_at (loc
, 0, "%<asm%> qualifier %qT ignored "
20972 "outside of function body", token
->u
.value
);
20973 volatile_loc
= loc
;
20975 cp_lexer_consume_token (parser
->lexer
);
20981 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20983 inform (inline_loc
, "first seen here");
20989 cp_lexer_consume_token (parser
->lexer
);
20995 error_at (loc
, "duplicate %<asm%> qualifier %qT",
20997 inform (goto_loc
, "first seen here");
21003 cp_lexer_consume_token (parser
->lexer
);
21008 error_at (loc
, "%qT is not an %<asm%> qualifier", token
->u
.value
);
21009 cp_lexer_consume_token (parser
->lexer
);
21018 bool volatile_p
= (volatile_loc
!= UNKNOWN_LOCATION
);
21019 bool inline_p
= (inline_loc
!= UNKNOWN_LOCATION
);
21020 bool goto_p
= (goto_loc
!= UNKNOWN_LOCATION
);
21022 if (!parser
->in_function_body
&& (inline_p
|| goto_p
))
21024 error_at (first_loc
, "%<asm%> qualifier outside of function body");
21025 inline_p
= goto_p
= false;
21028 /* Look for the opening `('. */
21029 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
21031 /* Look for the string. */
21032 string
= cp_parser_string_literal (parser
, false, false);
21033 if (string
== error_mark_node
)
21035 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
21036 /*consume_paren=*/true);
21040 /* If we're allowing GNU extensions, check for the extended assembly
21041 syntax. Unfortunately, the `:' tokens need not be separated by
21042 a space in C, and so, for compatibility, we tolerate that here
21043 too. Doing that means that we have to treat the `::' operator as
21045 if (cp_parser_allow_gnu_extensions_p (parser
)
21046 && parser
->in_function_body
21047 && (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
21048 || cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
)))
21050 bool inputs_p
= false;
21051 bool clobbers_p
= false;
21052 bool labels_p
= false;
21054 /* The extended syntax was used. */
21057 /* Look for outputs. */
21058 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21060 /* Consume the `:'. */
21061 cp_lexer_consume_token (parser
->lexer
);
21062 /* Parse the output-operands. */
21063 if (cp_lexer_next_token_is_not (parser
->lexer
,
21065 && cp_lexer_next_token_is_not (parser
->lexer
,
21067 && cp_lexer_next_token_is_not (parser
->lexer
,
21070 outputs
= cp_parser_asm_operand_list (parser
);
21071 if (outputs
== error_mark_node
)
21072 invalid_outputs_p
= true;
21075 /* If the next token is `::', there are no outputs, and the
21076 next token is the beginning of the inputs. */
21077 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21078 /* The inputs are coming next. */
21081 /* Look for inputs. */
21083 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21085 /* Consume the `:' or `::'. */
21086 cp_lexer_consume_token (parser
->lexer
);
21087 /* Parse the output-operands. */
21088 if (cp_lexer_next_token_is_not (parser
->lexer
,
21090 && cp_lexer_next_token_is_not (parser
->lexer
,
21092 && cp_lexer_next_token_is_not (parser
->lexer
,
21095 inputs
= cp_parser_asm_operand_list (parser
);
21096 if (inputs
== error_mark_node
)
21097 invalid_inputs_p
= true;
21100 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21101 /* The clobbers are coming next. */
21104 /* Look for clobbers. */
21106 || cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
21109 /* Consume the `:' or `::'. */
21110 cp_lexer_consume_token (parser
->lexer
);
21111 /* Parse the clobbers. */
21112 if (cp_lexer_next_token_is_not (parser
->lexer
,
21114 && cp_lexer_next_token_is_not (parser
->lexer
,
21116 clobbers
= cp_parser_asm_clobber_list (parser
);
21118 else if (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
21119 /* The labels are coming next. */
21122 /* Look for labels. */
21124 || (goto_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)))
21127 /* Consume the `:' or `::'. */
21128 cp_lexer_consume_token (parser
->lexer
);
21129 /* Parse the labels. */
21130 labels
= cp_parser_asm_label_list (parser
);
21133 if (goto_p
&& !labels_p
)
21134 missing
= clobbers_p
? RT_COLON
: RT_COLON_SCOPE
;
21137 missing
= RT_COLON_SCOPE
;
21139 /* Look for the closing `)'. */
21140 if (!cp_parser_require (parser
, missing
? CPP_COLON
: CPP_CLOSE_PAREN
,
21141 missing
? missing
: RT_CLOSE_PAREN
))
21142 cp_parser_skip_to_closing_parenthesis (parser
, true, false,
21143 /*consume_paren=*/true);
21144 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
21146 if (!invalid_inputs_p
&& !invalid_outputs_p
)
21148 /* Create the ASM_EXPR. */
21149 if (parser
->in_function_body
)
21151 asm_stmt
= finish_asm_stmt (asm_loc
, volatile_p
, string
, outputs
,
21152 inputs
, clobbers
, labels
, inline_p
);
21153 /* If the extended syntax was not used, mark the ASM_EXPR. */
21156 tree temp
= asm_stmt
;
21157 if (TREE_CODE (temp
) == CLEANUP_POINT_EXPR
)
21158 temp
= TREE_OPERAND (temp
, 0);
21160 ASM_INPUT_P (temp
) = 1;
21164 symtab
->finalize_toplevel_asm (string
);
21168 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
21169 type that comes from the decl-specifier-seq. */
21172 strip_declarator_types (tree type
, cp_declarator
*declarator
)
21174 for (cp_declarator
*d
= declarator
; d
;)
21184 if (TYPE_PTRMEMFUNC_P (type
))
21185 type
= TYPE_PTRMEMFUNC_FN_TYPE (type
);
21186 type
= TREE_TYPE (type
);
21194 /* Warn about the most vexing parse syntactic ambiguity, i.e., warn when
21195 a construct looks like a variable definition but is actually a function
21196 declaration. DECL_SPECIFIERS is the decl-specifier-seq and DECLARATOR
21197 is the declarator for this function declaration. */
21200 warn_about_ambiguous_parse (const cp_decl_specifier_seq
*decl_specifiers
,
21201 const cp_declarator
*declarator
)
21203 /* Only warn if we are declaring a function at block scope. */
21204 if (!at_function_scope_p ())
21207 /* And only if there is no storage class specified. */
21208 if (decl_specifiers
->storage_class
!= sc_none
21209 || decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
21212 if (declarator
->kind
!= cdk_function
21213 || !declarator
->declarator
21214 || declarator
->declarator
->kind
!= cdk_id
21215 || !identifier_p (get_unqualified_id
21216 (const_cast<cp_declarator
*>(declarator
))))
21219 /* Don't warn when the whole declarator (not just the declarator-id!)
21220 was parenthesized. That is, don't warn for int(n()) but do warn
21222 if (declarator
->parenthesized
!= UNKNOWN_LOCATION
)
21226 if (decl_specifiers
->type
)
21228 type
= decl_specifiers
->type
;
21229 if (TREE_CODE (type
) == TYPE_DECL
)
21230 type
= TREE_TYPE (type
);
21232 /* If the return type is void there is no ambiguity. */
21233 if (same_type_p (type
, void_type_node
))
21236 else if (decl_specifiers
->any_type_specifiers_p
)
21237 /* Code like long f(); will have null ->type. If we have any
21238 type-specifiers, pretend we've seen int. */
21239 type
= integer_type_node
;
21243 auto_diagnostic_group d
;
21244 location_t loc
= declarator
->u
.function
.parens_loc
;
21245 tree params
= declarator
->u
.function
.parameters
;
21246 const bool has_list_ctor_p
= CLASS_TYPE_P (type
) && TYPE_HAS_LIST_CTOR (type
);
21248 /* The T t() case. */
21249 if (params
== void_list_node
)
21251 if (warning_at (loc
, OPT_Wvexing_parse
,
21252 "empty parentheses were disambiguated as a function "
21255 /* () means value-initialization (C++03 and up); {} (C++11 and up)
21256 means value-initialization or aggregate-initialization, nothing
21257 means default-initialization. We can only suggest removing the
21258 parentheses/adding {} if T has a default constructor. */
21259 if (!CLASS_TYPE_P (type
) || TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
21261 gcc_rich_location
iloc (loc
);
21262 iloc
.add_fixit_remove ();
21263 inform (&iloc
, "remove parentheses to default-initialize "
21265 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
21267 if (CP_AGGREGATE_TYPE_P (type
))
21268 inform (loc
, "or replace parentheses with braces to "
21269 "aggregate-initialize a variable");
21271 inform (loc
, "or replace parentheses with braces to "
21272 "value-initialize a variable");
21279 /* If we had (...) or the parameter-list wasn't parenthesized,
21281 if (params
== NULL_TREE
|| !PARENTHESIZED_LIST_P (params
))
21284 /* The T t(X()) case. */
21285 if (list_length (params
) == 2)
21287 if (warning_at (loc
, OPT_Wvexing_parse
,
21288 "parentheses were disambiguated as a function "
21291 gcc_rich_location
iloc (loc
);
21292 /* {}-initialization means that we can use an initializer-list
21293 constructor if no default constructor is available, so don't
21294 suggest using {} for classes that have an initializer_list
21296 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
21298 iloc
.add_fixit_replace (get_start (loc
), "{");
21299 iloc
.add_fixit_replace (get_finish (loc
), "}");
21300 inform (&iloc
, "replace parentheses with braces to declare a "
21305 iloc
.add_fixit_insert_after (get_start (loc
), "(");
21306 iloc
.add_fixit_insert_before (get_finish (loc
), ")");
21307 inform (&iloc
, "add parentheses to declare a variable");
21311 /* The T t(X(), X()) case. */
21312 else if (warning_at (loc
, OPT_Wvexing_parse
,
21313 "parentheses were disambiguated as a function "
21316 gcc_rich_location
iloc (loc
);
21317 if (cxx_dialect
>= cxx11
&& !has_list_ctor_p
)
21319 iloc
.add_fixit_replace (get_start (loc
), "{");
21320 iloc
.add_fixit_replace (get_finish (loc
), "}");
21321 inform (&iloc
, "replace parentheses with braces to declare a "
21327 /* Declarators [gram.dcl.decl] */
21329 /* Parse an init-declarator.
21332 declarator initializer [opt]
21337 declarator asm-specification [opt] attributes [opt] initializer [opt]
21339 function-definition:
21340 decl-specifier-seq [opt] declarator ctor-initializer [opt]
21342 decl-specifier-seq [opt] declarator function-try-block
21346 function-definition:
21347 __extension__ function-definition
21351 function-definition:
21352 decl-specifier-seq [opt] declarator function-transaction-block
21354 The parser flags FLAGS is used to control type-specifier parsing.
21356 The DECL_SPECIFIERS apply to this declarator. Returns a
21357 representation of the entity declared. If MEMBER_P is TRUE, then
21358 this declarator appears in a class scope. The new DECL created by
21359 this declarator is returned.
21361 The CHECKS are access checks that should be performed once we know
21362 what entity is being declared (and, therefore, what classes have
21365 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
21366 for a function-definition here as well. If the declarator is a
21367 declarator for a function-definition, *FUNCTION_DEFINITION_P will
21368 be TRUE upon return. By that point, the function-definition will
21369 have been completely parsed.
21371 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
21374 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
21375 parsed declaration if it is an uninitialized single declarator not followed
21376 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
21377 if present, will not be consumed. If returned, this declarator will be
21378 created with SD_INITIALIZED but will not call cp_finish_decl.
21380 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
21381 and there is an initializer, the pointed location_t is set to the
21382 location of the '=' or `(', or '{' in C++11 token introducing the
21386 cp_parser_init_declarator (cp_parser
* parser
,
21387 cp_parser_flags flags
,
21388 cp_decl_specifier_seq
*decl_specifiers
,
21389 vec
<deferred_access_check
, va_gc
> *checks
,
21390 bool function_definition_allowed_p
,
21392 int declares_class_or_enum
,
21393 bool* function_definition_p
,
21394 tree
* maybe_range_for_decl
,
21395 location_t
* init_loc
,
21398 cp_token
*token
= NULL
, *asm_spec_start_token
= NULL
,
21399 *attributes_start_token
= NULL
;
21400 cp_declarator
*declarator
;
21401 tree prefix_attributes
;
21402 tree attributes
= NULL
;
21403 tree asm_specification
;
21405 tree decl
= NULL_TREE
;
21407 int is_initialized
;
21408 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
21409 initialized with "= ..", CPP_OPEN_PAREN if initialized with
21411 enum cpp_ttype initialization_kind
;
21412 bool is_direct_init
= false;
21413 bool is_non_constant_init
;
21414 int ctor_dtor_or_conv_p
;
21415 bool friend_p
= cp_parser_friend_p (decl_specifiers
);
21416 tree pushed_scope
= NULL_TREE
;
21417 bool range_for_decl_p
= false;
21418 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
21419 location_t tmp_init_loc
= UNKNOWN_LOCATION
;
21421 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_consteval
))
21422 flags
|= CP_PARSER_FLAGS_CONSTEVAL
;
21424 /* Gather the attributes that were provided with the
21425 decl-specifiers. */
21426 prefix_attributes
= decl_specifiers
->attributes
;
21428 /* Assume that this is not the declarator for a function
21430 if (function_definition_p
)
21431 *function_definition_p
= false;
21433 /* Default arguments are only permitted for function parameters. */
21434 if (decl_spec_seq_has_spec_p (decl_specifiers
, ds_typedef
))
21435 parser
->default_arg_ok_p
= false;
21437 /* Defer access checks while parsing the declarator; we cannot know
21438 what names are accessible until we know what is being
21440 resume_deferring_access_checks ();
21442 token
= cp_lexer_peek_token (parser
->lexer
);
21444 /* Parse the declarator. */
21446 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
21447 flags
, &ctor_dtor_or_conv_p
,
21448 /*parenthesized_p=*/NULL
,
21449 member_p
, friend_p
, /*static_p=*/false);
21450 /* Gather up the deferred checks. */
21451 stop_deferring_access_checks ();
21453 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
21455 /* If the DECLARATOR was erroneous, there's no need to go
21457 if (declarator
== cp_error_declarator
)
21458 return error_mark_node
;
21460 /* Check that the number of template-parameter-lists is OK. */
21461 if (!cp_parser_check_declarator_template_parameters (parser
, declarator
,
21463 return error_mark_node
;
21465 if (declares_class_or_enum
& 2)
21466 cp_parser_check_for_definition_in_return_type (declarator
,
21467 decl_specifiers
->type
,
21468 decl_specifiers
->locations
[ds_type_spec
]);
21470 /* Figure out what scope the entity declared by the DECLARATOR is
21471 located in. `grokdeclarator' sometimes changes the scope, so
21472 we compute it now. */
21473 scope
= get_scope_of_declarator (declarator
);
21475 /* Perform any lookups in the declared type which were thought to be
21476 dependent, but are not in the scope of the declarator. */
21477 decl_specifiers
->type
21478 = maybe_update_decl_type (decl_specifiers
->type
, scope
);
21480 /* If we're allowing GNU extensions, look for an
21481 asm-specification. */
21482 if (cp_parser_allow_gnu_extensions_p (parser
))
21484 /* Look for an asm-specification. */
21485 asm_spec_start_token
= cp_lexer_peek_token (parser
->lexer
);
21486 asm_specification
= cp_parser_asm_specification_opt (parser
);
21489 asm_specification
= NULL_TREE
;
21491 /* Look for attributes. */
21492 attributes_start_token
= cp_lexer_peek_token (parser
->lexer
);
21493 attributes
= cp_parser_attributes_opt (parser
);
21495 /* Peek at the next token. */
21496 token
= cp_lexer_peek_token (parser
->lexer
);
21498 bool bogus_implicit_tmpl
= false;
21500 if (function_declarator_p (declarator
))
21502 /* Handle C++17 deduction guides. */
21503 if (!decl_specifiers
->type
21504 && !decl_specifiers
->any_type_specifiers_p
21505 && ctor_dtor_or_conv_p
<= 0
21506 && cxx_dialect
>= cxx17
)
21508 cp_declarator
*id
= get_id_declarator (declarator
);
21509 tree name
= id
->u
.id
.unqualified_name
;
21510 parser
->scope
= id
->u
.id
.qualifying_scope
;
21511 tree tmpl
= cp_parser_lookup_name_simple (parser
, name
, id
->id_loc
);
21513 && (DECL_CLASS_TEMPLATE_P (tmpl
)
21514 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
)))
21516 id
->u
.id
.unqualified_name
= dguide_name (tmpl
);
21517 id
->u
.id
.sfk
= sfk_deduction_guide
;
21518 ctor_dtor_or_conv_p
= 1;
21522 if (!member_p
&& !cp_parser_error_occurred (parser
))
21523 warn_about_ambiguous_parse (decl_specifiers
, declarator
);
21525 /* Check to see if the token indicates the start of a
21526 function-definition. */
21527 if (cp_parser_token_starts_function_definition_p (token
))
21529 if (!function_definition_allowed_p
)
21531 /* If a function-definition should not appear here, issue an
21533 cp_parser_error (parser
,
21534 "a function-definition is not allowed here");
21535 return error_mark_node
;
21538 location_t func_brace_location
21539 = cp_lexer_peek_token (parser
->lexer
)->location
;
21541 /* Neither attributes nor an asm-specification are allowed
21542 on a function-definition. */
21543 if (asm_specification
)
21544 error_at (asm_spec_start_token
->location
,
21545 "an %<asm%> specification is not allowed "
21546 "on a function-definition");
21548 error_at (attributes_start_token
->location
,
21549 "attributes are not allowed "
21550 "on a function-definition");
21551 /* This is a function-definition. */
21552 *function_definition_p
= true;
21554 /* Parse the function definition. */
21556 decl
= cp_parser_save_member_function_body (parser
,
21559 prefix_attributes
);
21562 (cp_parser_function_definition_from_specifiers_and_declarator
21563 (parser
, decl_specifiers
, prefix_attributes
, declarator
));
21565 if (decl
!= error_mark_node
&& DECL_STRUCT_FUNCTION (decl
))
21567 /* This is where the prologue starts... */
21568 DECL_STRUCT_FUNCTION (decl
)->function_start_locus
21569 = func_brace_location
;
21575 else if (parser
->fully_implicit_function_template_p
)
21577 /* A non-template declaration involving a function parameter list
21578 containing an implicit template parameter will be made into a
21579 template. If the resulting declaration is not going to be an
21580 actual function then finish the template scope here to prevent it.
21581 An error message will be issued once we have a decl to talk about.
21583 FIXME probably we should do type deduction rather than create an
21584 implicit template, but the standard currently doesn't allow it. */
21585 bogus_implicit_tmpl
= true;
21586 finish_fully_implicit_template (parser
, NULL_TREE
);
21591 Only in function declarations for constructors, destructors, type
21592 conversions, and deduction guides can the decl-specifier-seq be omitted.
21594 We explicitly postpone this check past the point where we handle
21595 function-definitions because we tolerate function-definitions
21596 that are missing their return types in some modes. */
21597 if (!decl_specifiers
->any_specifiers_p
&& ctor_dtor_or_conv_p
<= 0)
21599 cp_parser_error (parser
,
21600 "expected constructor, destructor, or type conversion");
21601 return error_mark_node
;
21604 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
21605 if (token
->type
== CPP_EQ
21606 || token
->type
== CPP_OPEN_PAREN
21607 || token
->type
== CPP_OPEN_BRACE
)
21609 is_initialized
= SD_INITIALIZED
;
21610 initialization_kind
= token
->type
;
21611 if (maybe_range_for_decl
)
21612 *maybe_range_for_decl
= error_mark_node
;
21613 tmp_init_loc
= token
->location
;
21614 if (init_loc
&& *init_loc
== UNKNOWN_LOCATION
)
21615 *init_loc
= tmp_init_loc
;
21617 if (token
->type
== CPP_EQ
21618 && function_declarator_p (declarator
))
21620 cp_token
*t2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
21621 if (t2
->keyword
== RID_DEFAULT
)
21622 is_initialized
= SD_DEFAULTED
;
21623 else if (t2
->keyword
== RID_DELETE
)
21624 is_initialized
= SD_DELETED
;
21629 /* If the init-declarator isn't initialized and isn't followed by a
21630 `,' or `;', it's not a valid init-declarator. */
21631 if (token
->type
!= CPP_COMMA
21632 && token
->type
!= CPP_SEMICOLON
)
21634 if (maybe_range_for_decl
&& *maybe_range_for_decl
!= error_mark_node
)
21635 range_for_decl_p
= true;
21638 if (!maybe_range_for_decl
)
21639 cp_parser_error (parser
, "expected initializer");
21640 return error_mark_node
;
21643 is_initialized
= SD_UNINITIALIZED
;
21644 initialization_kind
= CPP_EOF
;
21647 /* Because start_decl has side-effects, we should only call it if we
21648 know we're going ahead. By this point, we know that we cannot
21649 possibly be looking at any other construct. */
21650 cp_parser_commit_to_tentative_parse (parser
);
21652 /* Enter the newly declared entry in the symbol table. If we're
21653 processing a declaration in a class-specifier, we wait until
21654 after processing the initializer. */
21657 if (parser
->in_unbraced_linkage_specification_p
)
21658 decl_specifiers
->storage_class
= sc_extern
;
21659 decl
= start_decl (declarator
, decl_specifiers
,
21660 range_for_decl_p
? SD_INITIALIZED
: is_initialized
,
21661 attributes
, prefix_attributes
, &pushed_scope
);
21662 cp_finalize_omp_declare_simd (parser
, decl
);
21663 cp_finalize_oacc_routine (parser
, decl
, false);
21664 /* Adjust location of decl if declarator->id_loc is more appropriate:
21665 set, and decl wasn't merged with another decl, in which case its
21666 location would be different from input_location, and more accurate. */
21668 && declarator
->id_loc
!= UNKNOWN_LOCATION
21669 && DECL_SOURCE_LOCATION (decl
) == input_location
)
21670 DECL_SOURCE_LOCATION (decl
) = declarator
->id_loc
;
21673 /* Enter the SCOPE. That way unqualified names appearing in the
21674 initializer will be looked up in SCOPE. */
21675 pushed_scope
= push_scope (scope
);
21677 /* Perform deferred access control checks, now that we know in which
21678 SCOPE the declared entity resides. */
21679 if (!member_p
&& decl
)
21681 tree saved_current_function_decl
= NULL_TREE
;
21683 /* If the entity being declared is a function, pretend that we
21684 are in its scope. If it is a `friend', it may have access to
21685 things that would not otherwise be accessible. */
21686 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21688 saved_current_function_decl
= current_function_decl
;
21689 current_function_decl
= decl
;
21692 /* Perform access checks for template parameters. */
21693 cp_parser_perform_template_parameter_access_checks (checks
);
21695 /* Perform the access control checks for the declarator and the
21696 decl-specifiers. */
21697 perform_deferred_access_checks (tf_warning_or_error
);
21699 /* Restore the saved value. */
21700 if (TREE_CODE (decl
) == FUNCTION_DECL
)
21701 current_function_decl
= saved_current_function_decl
;
21704 /* Parse the initializer. */
21705 initializer
= NULL_TREE
;
21706 is_direct_init
= false;
21707 is_non_constant_init
= true;
21708 if (is_initialized
)
21710 if (function_declarator_p (declarator
))
21712 if (initialization_kind
== CPP_EQ
)
21713 initializer
= cp_parser_pure_specifier (parser
);
21716 /* If the declaration was erroneous, we don't really
21717 know what the user intended, so just silently
21718 consume the initializer. */
21719 if (decl
!= error_mark_node
)
21720 error_at (tmp_init_loc
, "initializer provided for function");
21721 cp_parser_skip_to_closing_parenthesis (parser
,
21722 /*recovering=*/true,
21723 /*or_comma=*/false,
21724 /*consume_paren=*/true);
21729 /* We want to record the extra mangling scope for in-class
21730 initializers of class members and initializers of static
21731 data member templates and namespace-scope initializers.
21732 The former involves deferring parsing of the initializer
21733 until end of class as with default arguments. So right
21734 here we only handle the latter two. */
21735 bool has_lambda_scope
= false;
21737 if (decl
!= error_mark_node
21739 && (processing_template_decl
|| DECL_NAMESPACE_SCOPE_P (decl
)))
21740 has_lambda_scope
= true;
21742 if (has_lambda_scope
)
21743 start_lambda_scope (decl
);
21744 initializer
= cp_parser_initializer (parser
,
21746 &is_non_constant_init
);
21747 if (has_lambda_scope
)
21748 finish_lambda_scope ();
21749 if (initializer
== error_mark_node
)
21750 cp_parser_skip_to_end_of_statement (parser
);
21754 /* The old parser allows attributes to appear after a parenthesized
21755 initializer. Mark Mitchell proposed removing this functionality
21756 on the GCC mailing lists on 2002-08-13. This parser accepts the
21757 attributes -- but ignores them. Made a permerror in GCC 8. */
21758 if (cp_parser_allow_gnu_extensions_p (parser
)
21759 && initialization_kind
== CPP_OPEN_PAREN
21760 && cp_parser_attributes_opt (parser
)
21761 && permerror (input_location
,
21762 "attributes after parenthesized initializer ignored"))
21765 if (flag_permissive
&& !hint
)
21768 inform (input_location
,
21769 "this flexibility is deprecated and will be removed");
21773 /* And now complain about a non-function implicit template. */
21774 if (bogus_implicit_tmpl
&& decl
!= error_mark_node
)
21775 error_at (DECL_SOURCE_LOCATION (decl
),
21776 "non-function %qD declared as implicit template", decl
);
21778 /* For an in-class declaration, use `grokfield' to create the
21784 pop_scope (pushed_scope
);
21785 pushed_scope
= NULL_TREE
;
21787 decl
= grokfield (declarator
, decl_specifiers
,
21788 initializer
, !is_non_constant_init
,
21789 /*asmspec=*/NULL_TREE
,
21790 attr_chainon (attributes
, prefix_attributes
));
21791 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
21792 cp_parser_save_default_args (parser
, decl
);
21793 cp_finalize_omp_declare_simd (parser
, decl
);
21794 cp_finalize_oacc_routine (parser
, decl
, false);
21797 /* Finish processing the declaration. But, skip member
21799 if (!member_p
&& decl
&& decl
!= error_mark_node
&& !range_for_decl_p
)
21801 cp_finish_decl (decl
,
21802 initializer
, !is_non_constant_init
,
21804 /* If the initializer is in parentheses, then this is
21805 a direct-initialization, which means that an
21806 `explicit' constructor is OK. Otherwise, an
21807 `explicit' constructor cannot be used. */
21808 ((is_direct_init
|| !is_initialized
)
21809 ? LOOKUP_NORMAL
: LOOKUP_IMPLICIT
));
21811 else if ((cxx_dialect
!= cxx98
) && friend_p
21812 && decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
21813 /* Core issue #226 (C++0x only): A default template-argument
21814 shall not be specified in a friend class template
21816 check_default_tmpl_args (decl
, current_template_parms
, /*is_primary=*/true,
21817 /*is_partial=*/false, /*is_friend_decl=*/1);
21819 if (!friend_p
&& pushed_scope
)
21820 pop_scope (pushed_scope
);
21822 if (function_declarator_p (declarator
)
21823 && parser
->fully_implicit_function_template_p
)
21826 decl
= finish_fully_implicit_template (parser
, decl
);
21828 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
21831 if (auto_result
&& is_initialized
&& decl_specifiers
->type
21832 && type_uses_auto (decl_specifiers
->type
))
21833 *auto_result
= strip_declarator_types (TREE_TYPE (decl
), declarator
);
21838 /* Parse a declarator.
21842 ptr-operator declarator
21844 abstract-declarator:
21845 ptr-operator abstract-declarator [opt]
21846 direct-abstract-declarator
21851 attributes [opt] direct-declarator
21852 attributes [opt] ptr-operator declarator
21854 abstract-declarator:
21855 attributes [opt] ptr-operator abstract-declarator [opt]
21856 attributes [opt] direct-abstract-declarator
21858 The parser flags FLAGS is used to control type-specifier parsing.
21860 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
21861 detect constructors, destructors, deduction guides, or conversion operators.
21862 It is set to -1 if the declarator is a name, and +1 if it is a
21863 function. Otherwise it is set to zero. Usually you just want to
21864 test for >0, but internally the negative value is used.
21866 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
21867 a decl-specifier-seq unless it declares a constructor, destructor,
21868 or conversion. It might seem that we could check this condition in
21869 semantic analysis, rather than parsing, but that makes it difficult
21870 to handle something like `f()'. We want to notice that there are
21871 no decl-specifiers, and therefore realize that this is an
21872 expression, not a declaration.)
21874 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
21875 the declarator is a direct-declarator of the form "(...)".
21877 MEMBER_P is true iff this declarator is a member-declarator.
21879 FRIEND_P is true iff this declarator is a friend.
21881 STATIC_P is true iff the keyword static was seen. */
21883 static cp_declarator
*
21884 cp_parser_declarator (cp_parser
* parser
,
21885 cp_parser_declarator_kind dcl_kind
,
21886 cp_parser_flags flags
,
21887 int* ctor_dtor_or_conv_p
,
21888 bool* parenthesized_p
,
21889 bool member_p
, bool friend_p
, bool static_p
)
21891 cp_declarator
*declarator
;
21892 enum tree_code code
;
21893 cp_cv_quals cv_quals
;
21895 tree gnu_attributes
= NULL_TREE
, std_attributes
= NULL_TREE
;
21897 /* Assume this is not a constructor, destructor, or type-conversion
21899 if (ctor_dtor_or_conv_p
)
21900 *ctor_dtor_or_conv_p
= 0;
21902 if (cp_parser_allow_gnu_extensions_p (parser
))
21903 gnu_attributes
= cp_parser_gnu_attributes_opt (parser
);
21905 /* Check for the ptr-operator production. */
21906 cp_parser_parse_tentatively (parser
);
21907 /* Parse the ptr-operator. */
21908 code
= cp_parser_ptr_operator (parser
,
21913 /* If that worked, then we have a ptr-operator. */
21914 if (cp_parser_parse_definitely (parser
))
21916 /* If a ptr-operator was found, then this declarator was not
21918 if (parenthesized_p
)
21919 *parenthesized_p
= false;
21920 /* The dependent declarator is optional if we are parsing an
21921 abstract-declarator. */
21922 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
21923 cp_parser_parse_tentatively (parser
);
21925 /* Parse the dependent declarator. */
21926 declarator
= cp_parser_declarator (parser
, dcl_kind
,
21927 CP_PARSER_FLAGS_NONE
,
21928 /*ctor_dtor_or_conv_p=*/NULL
,
21929 /*parenthesized_p=*/NULL
,
21930 /*member_p=*/false,
21931 friend_p
, /*static_p=*/false);
21933 /* If we are parsing an abstract-declarator, we must handle the
21934 case where the dependent declarator is absent. */
21935 if (dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
21936 && !cp_parser_parse_definitely (parser
))
21939 declarator
= cp_parser_make_indirect_declarator
21940 (code
, class_type
, cv_quals
, declarator
, std_attributes
);
21942 /* Everything else is a direct-declarator. */
21945 if (parenthesized_p
)
21946 *parenthesized_p
= cp_lexer_next_token_is (parser
->lexer
,
21948 declarator
= cp_parser_direct_declarator (parser
, dcl_kind
,
21949 flags
, ctor_dtor_or_conv_p
,
21950 member_p
, friend_p
, static_p
);
21953 if (gnu_attributes
&& declarator
&& declarator
!= cp_error_declarator
)
21954 declarator
->attributes
= gnu_attributes
;
21958 /* Parse a direct-declarator or direct-abstract-declarator.
21962 direct-declarator ( parameter-declaration-clause )
21963 cv-qualifier-seq [opt]
21964 ref-qualifier [opt]
21965 exception-specification [opt]
21966 direct-declarator [ constant-expression [opt] ]
21969 direct-abstract-declarator:
21970 direct-abstract-declarator [opt]
21971 ( parameter-declaration-clause )
21972 cv-qualifier-seq [opt]
21973 ref-qualifier [opt]
21974 exception-specification [opt]
21975 direct-abstract-declarator [opt] [ constant-expression [opt] ]
21976 ( abstract-declarator )
21978 Returns a representation of the declarator. DCL_KIND is
21979 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
21980 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
21981 we are parsing a direct-declarator. It is
21982 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
21983 of ambiguity we prefer an abstract declarator, as per
21985 The parser flags FLAGS is used to control type-specifier parsing.
21986 CTOR_DTOR_OR_CONV_P, MEMBER_P, FRIEND_P, and STATIC_P are
21987 as for cp_parser_declarator. */
21989 static cp_declarator
*
21990 cp_parser_direct_declarator (cp_parser
* parser
,
21991 cp_parser_declarator_kind dcl_kind
,
21992 cp_parser_flags flags
,
21993 int* ctor_dtor_or_conv_p
,
21994 bool member_p
, bool friend_p
, bool static_p
)
21997 cp_declarator
*declarator
= NULL
;
21998 tree scope
= NULL_TREE
;
21999 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
22000 bool saved_in_declarator_p
= parser
->in_declarator_p
;
22002 tree pushed_scope
= NULL_TREE
;
22003 cp_token
*open_paren
= NULL
, *close_paren
= NULL
;
22007 /* Peek at the next token. */
22008 token
= cp_lexer_peek_token (parser
->lexer
);
22009 if (token
->type
== CPP_OPEN_PAREN
)
22011 /* This is either a parameter-declaration-clause, or a
22012 parenthesized declarator. When we know we are parsing a
22013 named declarator, it must be a parenthesized declarator
22014 if FIRST is true. For instance, `(int)' is a
22015 parameter-declaration-clause, with an omitted
22016 direct-abstract-declarator. But `((*))', is a
22017 parenthesized abstract declarator. Finally, when T is a
22018 template parameter `(T)' is a
22019 parameter-declaration-clause, and not a parenthesized
22022 We first try and parse a parameter-declaration-clause,
22023 and then try a nested declarator (if FIRST is true).
22025 It is not an error for it not to be a
22026 parameter-declaration-clause, even when FIRST is
22032 The first is the declaration of a function while the
22033 second is the definition of a variable, including its
22036 Having seen only the parenthesis, we cannot know which of
22037 these two alternatives should be selected. Even more
22038 complex are examples like:
22043 The former is a function-declaration; the latter is a
22044 variable initialization.
22046 Thus again, we try a parameter-declaration-clause, and if
22047 that fails, we back out and return. */
22049 if (!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
22052 bool is_declarator
= false;
22056 /* In a member-declarator, the only valid interpretation
22057 of a parenthesis is the start of a
22058 parameter-declaration-clause. (It is invalid to
22059 initialize a static data member with a parenthesized
22060 initializer; only the "=" form of initialization is
22063 cp_parser_parse_tentatively (parser
);
22065 /* Consume the `('. */
22066 const location_t parens_start
= token
->location
;
22067 matching_parens parens
;
22068 parens
.consume_open (parser
);
22071 /* If this is going to be an abstract declarator, we're
22072 in a declarator and we can't have default args. */
22073 parser
->default_arg_ok_p
= false;
22074 parser
->in_declarator_p
= true;
22077 begin_scope (sk_function_parms
, NULL_TREE
);
22079 /* Signal we are in the immediate function context. */
22080 if (flags
& CP_PARSER_FLAGS_CONSTEVAL
)
22081 current_binding_level
->immediate_fn_ctx_p
= true;
22083 /* Parse the parameter-declaration-clause. */
22085 = cp_parser_parameter_declaration_clause (parser
, flags
);
22086 const location_t parens_end
22087 = cp_lexer_peek_token (parser
->lexer
)->location
;
22089 /* Consume the `)'. */
22090 parens
.require_close (parser
);
22092 /* If all went well, parse the cv-qualifier-seq,
22093 ref-qualifier and the exception-specification. */
22094 if (member_p
|| cp_parser_parse_definitely (parser
))
22096 cp_cv_quals cv_quals
;
22097 cp_virt_specifiers virt_specifiers
;
22098 cp_ref_qualifier ref_qual
;
22099 tree exception_specification
;
22102 bool memfn
= (member_p
|| (pushed_scope
22103 && CLASS_TYPE_P (pushed_scope
)));
22104 unsigned char local_variables_forbidden_p
22105 = parser
->local_variables_forbidden_p
;
22106 /* 'this' is not allowed in static member functions. */
22107 if (static_p
|| friend_p
)
22108 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
22110 is_declarator
= true;
22112 if (ctor_dtor_or_conv_p
)
22113 *ctor_dtor_or_conv_p
= *ctor_dtor_or_conv_p
< 0;
22116 /* Parse the cv-qualifier-seq. */
22117 cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
22118 /* Parse the ref-qualifier. */
22119 ref_qual
= cp_parser_ref_qualifier_opt (parser
);
22120 /* Parse the tx-qualifier. */
22121 tree tx_qual
= cp_parser_tx_qualifier_opt (parser
);
22123 tree save_ccp
= current_class_ptr
;
22124 tree save_ccr
= current_class_ref
;
22126 /* DR 1207: 'this' is in scope after the cv-quals. */
22127 inject_this_parameter (current_class_type
, cv_quals
);
22129 /* If it turned out that this is e.g. a pointer to a
22130 function, we don't want to delay noexcept parsing. */
22131 if (declarator
== NULL
|| declarator
->kind
!= cdk_id
)
22132 flags
&= ~CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
22134 /* Parse the exception-specification. */
22135 exception_specification
22136 = cp_parser_exception_specification_opt (parser
,
22139 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22141 /* In here, we handle cases where attribute is used after
22142 the function declaration. For example:
22143 void func (int x) __attribute__((vector(..))); */
22144 tree gnu_attrs
= NULL_TREE
;
22145 tree requires_clause
= NULL_TREE
;
22146 late_return
= (cp_parser_late_return_type_opt
22147 (parser
, declarator
, requires_clause
));
22149 /* Parse the virt-specifier-seq. */
22150 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
22152 location_t parens_loc
= make_location (parens_start
,
22155 /* Create the function-declarator. */
22156 declarator
= make_call_declarator (declarator
,
22162 exception_specification
,
22166 declarator
->std_attributes
= attrs
;
22167 declarator
->attributes
= gnu_attrs
;
22168 /* Any subsequent parameter lists are to do with
22169 return type, so are not those of the declared
22171 parser
->default_arg_ok_p
= false;
22173 current_class_ptr
= save_ccp
;
22174 current_class_ref
= save_ccr
;
22176 /* Restore the state of local_variables_forbidden_p. */
22177 parser
->local_variables_forbidden_p
22178 = local_variables_forbidden_p
;
22181 /* Remove the function parms from scope. */
22182 pop_bindings_and_leave_scope ();
22185 /* Repeat the main loop. */
22189 /* If this is the first, we can try a parenthesized
22193 bool saved_in_type_id_in_expr_p
;
22195 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22196 parser
->in_declarator_p
= saved_in_declarator_p
;
22198 open_paren
= token
;
22199 /* Consume the `('. */
22200 matching_parens parens
;
22201 parens
.consume_open (parser
);
22202 /* Parse the nested declarator. */
22203 saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
22204 parser
->in_type_id_in_expr_p
= true;
22206 = cp_parser_declarator (parser
, dcl_kind
, flags
,
22207 ctor_dtor_or_conv_p
,
22208 /*parenthesized_p=*/NULL
,
22209 member_p
, friend_p
,
22210 /*static_p=*/false);
22211 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
22213 /* Expect a `)'. */
22214 close_paren
= cp_lexer_peek_token (parser
->lexer
);
22215 if (!parens
.require_close (parser
))
22216 declarator
= cp_error_declarator
;
22217 if (declarator
== cp_error_declarator
)
22220 goto handle_declarator
;
22222 /* Otherwise, we must be done. */
22226 else if ((!first
|| dcl_kind
!= CP_PARSER_DECLARATOR_NAMED
)
22227 && token
->type
== CPP_OPEN_SQUARE
22228 && !cp_next_tokens_can_be_attribute_p (parser
))
22230 /* Parse an array-declarator. */
22231 tree bounds
, attrs
;
22233 if (ctor_dtor_or_conv_p
)
22234 *ctor_dtor_or_conv_p
= 0;
22238 parser
->default_arg_ok_p
= false;
22239 parser
->in_declarator_p
= true;
22240 /* Consume the `['. */
22241 cp_lexer_consume_token (parser
->lexer
);
22242 /* Peek at the next token. */
22243 token
= cp_lexer_peek_token (parser
->lexer
);
22244 /* If the next token is `]', then there is no
22245 constant-expression. */
22246 if (token
->type
!= CPP_CLOSE_SQUARE
)
22248 bool non_constant_p
;
22250 = cp_parser_constant_expression (parser
,
22251 /*allow_non_constant=*/true,
22253 if (!non_constant_p
)
22255 else if (error_operand_p (bounds
))
22256 /* Already gave an error. */;
22257 else if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
22258 /* Let compute_array_index_type diagnose this. */;
22259 else if (!parser
->in_function_body
22260 || current_binding_level
->kind
== sk_function_parms
)
22262 /* Normally, the array bound must be an integral constant
22263 expression. However, as an extension, we allow VLAs
22264 in function scopes as long as they aren't part of a
22265 parameter declaration. */
22266 cp_parser_error (parser
,
22267 "array bound is not an integer constant");
22268 bounds
= error_mark_node
;
22270 else if (processing_template_decl
22271 && !type_dependent_expression_p (bounds
))
22273 /* Remember this wasn't a constant-expression. */
22274 bounds
= build_nop (TREE_TYPE (bounds
), bounds
);
22275 TREE_SIDE_EFFECTS (bounds
) = 1;
22279 bounds
= NULL_TREE
;
22280 /* Look for the closing `]'. */
22281 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
22283 declarator
= cp_error_declarator
;
22287 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22288 declarator
= make_array_declarator (declarator
, bounds
);
22289 declarator
->std_attributes
= attrs
;
22291 else if (first
&& dcl_kind
!= CP_PARSER_DECLARATOR_ABSTRACT
)
22294 tree qualifying_scope
;
22295 tree unqualified_name
;
22297 special_function_kind sfk
;
22299 bool pack_expansion_p
= false;
22300 cp_token
*declarator_id_start_token
;
22302 /* Parse a declarator-id */
22303 abstract_ok
= (dcl_kind
== CP_PARSER_DECLARATOR_EITHER
);
22306 cp_parser_parse_tentatively (parser
);
22308 /* If we see an ellipsis, we should be looking at a
22310 if (token
->type
== CPP_ELLIPSIS
)
22312 /* Consume the `...' */
22313 cp_lexer_consume_token (parser
->lexer
);
22315 pack_expansion_p
= true;
22319 declarator_id_start_token
= cp_lexer_peek_token (parser
->lexer
);
22321 = cp_parser_declarator_id (parser
, /*optional_p=*/abstract_ok
);
22322 qualifying_scope
= parser
->scope
;
22327 if (!unqualified_name
&& pack_expansion_p
)
22329 /* Check whether an error occurred. */
22330 okay
= !cp_parser_error_occurred (parser
);
22332 /* We already consumed the ellipsis to mark a
22333 parameter pack, but we have no way to report it,
22334 so abort the tentative parse. We will be exiting
22335 immediately anyway. */
22336 cp_parser_abort_tentative_parse (parser
);
22339 okay
= cp_parser_parse_definitely (parser
);
22342 unqualified_name
= error_mark_node
;
22343 else if (unqualified_name
22344 && (qualifying_scope
22345 || (!identifier_p (unqualified_name
))))
22347 cp_parser_error (parser
, "expected unqualified-id");
22348 unqualified_name
= error_mark_node
;
22352 if (!unqualified_name
)
22354 if (unqualified_name
== error_mark_node
)
22356 declarator
= cp_error_declarator
;
22357 pack_expansion_p
= false;
22358 declarator
->parameter_pack_p
= false;
22362 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22364 if (qualifying_scope
&& at_namespace_scope_p ()
22365 && TREE_CODE (qualifying_scope
) == TYPENAME_TYPE
)
22367 /* In the declaration of a member of a template class
22368 outside of the class itself, the SCOPE will sometimes
22369 be a TYPENAME_TYPE. For example, given:
22371 template <typename T>
22372 int S<T>::R::i = 3;
22374 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
22375 this context, we must resolve S<T>::R to an ordinary
22376 type, rather than a typename type.
22378 The reason we normally avoid resolving TYPENAME_TYPEs
22379 is that a specialization of `S' might render
22380 `S<T>::R' not a type. However, if `S' is
22381 specialized, then this `i' will not be used, so there
22382 is no harm in resolving the types here. */
22385 /* Resolve the TYPENAME_TYPE. */
22386 type
= resolve_typename_type (qualifying_scope
,
22387 /*only_current_p=*/false);
22388 /* If that failed, the declarator is invalid. */
22389 if (TREE_CODE (type
) == TYPENAME_TYPE
)
22391 if (typedef_variant_p (type
))
22392 error_at (declarator_id_start_token
->location
,
22393 "cannot define member of dependent typedef "
22396 error_at (declarator_id_start_token
->location
,
22397 "%<%T::%E%> is not a type",
22398 TYPE_CONTEXT (qualifying_scope
),
22399 TYPE_IDENTIFIER (qualifying_scope
));
22401 qualifying_scope
= type
;
22406 if (unqualified_name
)
22410 if (qualifying_scope
22411 && CLASS_TYPE_P (qualifying_scope
))
22412 class_type
= qualifying_scope
;
22414 class_type
= current_class_type
;
22416 if (TREE_CODE (unqualified_name
) == TYPE_DECL
)
22418 tree name_type
= TREE_TYPE (unqualified_name
);
22420 if (!class_type
|| !same_type_p (name_type
, class_type
))
22422 /* We do not attempt to print the declarator
22423 here because we do not have enough
22424 information about its original syntactic
22426 cp_parser_error (parser
, "invalid declarator");
22427 declarator
= cp_error_declarator
;
22430 else if (qualifying_scope
22431 && CLASSTYPE_USE_TEMPLATE (name_type
))
22433 error_at (declarator_id_start_token
->location
,
22434 "invalid use of constructor as a template");
22435 inform (declarator_id_start_token
->location
,
22436 "use %<%T::%D%> instead of %<%T::%D%> to "
22437 "name the constructor in a qualified name",
22439 DECL_NAME (TYPE_TI_TEMPLATE (class_type
)),
22440 class_type
, name_type
);
22441 declarator
= cp_error_declarator
;
22444 unqualified_name
= constructor_name (class_type
);
22449 if (TREE_CODE (unqualified_name
) == BIT_NOT_EXPR
)
22450 sfk
= sfk_destructor
;
22451 else if (identifier_p (unqualified_name
)
22452 && IDENTIFIER_CONV_OP_P (unqualified_name
))
22453 sfk
= sfk_conversion
;
22454 else if (/* There's no way to declare a constructor
22455 for an unnamed type, even if the type
22456 got a name for linkage purposes. */
22457 !TYPE_WAS_UNNAMED (class_type
)
22458 /* Handle correctly (c++/19200):
22472 friend void N::S();
22474 && (!friend_p
|| class_type
== qualifying_scope
)
22475 && constructor_name_p (unqualified_name
,
22477 sfk
= sfk_constructor
;
22478 else if (is_overloaded_fn (unqualified_name
)
22479 && DECL_CONSTRUCTOR_P (get_first_fn
22480 (unqualified_name
)))
22481 sfk
= sfk_constructor
;
22483 if (ctor_dtor_or_conv_p
&& sfk
!= sfk_none
)
22484 *ctor_dtor_or_conv_p
= -1;
22487 declarator
= make_id_declarator (qualifying_scope
,
22489 sfk
, token
->location
);
22490 declarator
->std_attributes
= attrs
;
22491 declarator
->parameter_pack_p
= pack_expansion_p
;
22493 if (pack_expansion_p
)
22494 maybe_warn_variadic_templates ();
22496 /* We're looking for this case in [temp.res]:
22497 A qualified-id is assumed to name a type if [...]
22498 - it is a decl-specifier of the decl-specifier-seq of a
22499 parameter-declaration in a declarator of a function or
22500 function template declaration, ... */
22501 if (cxx_dialect
>= cxx20
22502 && (flags
& CP_PARSER_FLAGS_TYPENAME_OPTIONAL
)
22503 && declarator
->kind
== cdk_id
22504 && !at_class_scope_p ()
22505 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
22507 /* ...whose declarator-id is qualified. If it isn't, never
22508 assume the parameters to refer to types. */
22509 if (qualifying_scope
== NULL_TREE
)
22510 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
22513 /* Now we have something like
22514 template <typename T> int C::x(S::p);
22515 which can be a function template declaration or a
22516 variable template definition. If name lookup for
22517 the declarator-id C::x finds one or more function
22518 templates, assume S::p to name a type. Otherwise,
22521 = cp_parser_lookup_name (parser
, unqualified_name
,
22523 /*is_template=*/false,
22524 /*is_namespace=*/false,
22525 /*check_dependency=*/false,
22526 /*ambiguous_decls=*/NULL
,
22529 if (!is_overloaded_fn (decl
)
22531 template<typename T>
22532 A<T>::A(T::type) { } */
22533 && !(MAYBE_CLASS_TYPE_P (qualifying_scope
)
22534 && constructor_name_p (unqualified_name
,
22535 qualifying_scope
)))
22536 flags
&= ~CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
22541 handle_declarator
:;
22542 scope
= get_scope_of_declarator (declarator
);
22545 /* Any names that appear after the declarator-id for a
22546 member are looked up in the containing scope. */
22547 if (at_function_scope_p ())
22549 /* But declarations with qualified-ids can't appear in a
22551 cp_parser_error (parser
, "qualified-id in declaration");
22552 declarator
= cp_error_declarator
;
22555 pushed_scope
= push_scope (scope
);
22557 parser
->in_declarator_p
= true;
22558 if ((ctor_dtor_or_conv_p
&& *ctor_dtor_or_conv_p
)
22559 || (declarator
&& declarator
->kind
== cdk_id
))
22560 /* Default args are only allowed on function
22562 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22564 parser
->default_arg_ok_p
= false;
22573 /* For an abstract declarator, we might wind up with nothing at this
22574 point. That's an error; the declarator is not optional. */
22576 cp_parser_error (parser
, "expected declarator");
22577 else if (open_paren
)
22579 /* Record overly parenthesized declarator so we can give a
22580 diagnostic about confusing decl/expr disambiguation. */
22581 if (declarator
->kind
== cdk_array
)
22583 /* If the open and close parens are on different lines, this
22584 is probably a formatting thing, so ignore. */
22585 expanded_location open
= expand_location (open_paren
->location
);
22586 expanded_location close
= expand_location (close_paren
->location
);
22587 if (open
.line
!= close
.line
|| open
.file
!= close
.file
)
22591 declarator
->parenthesized
= make_location (open_paren
->location
,
22592 open_paren
->location
,
22593 close_paren
->location
);
22596 /* If we entered a scope, we must exit it now. */
22598 pop_scope (pushed_scope
);
22600 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
22601 parser
->in_declarator_p
= saved_in_declarator_p
;
22606 /* Parse a ptr-operator.
22609 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22610 * cv-qualifier-seq [opt]
22612 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
22613 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
22618 & cv-qualifier-seq [opt]
22620 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
22621 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
22622 an rvalue reference. In the case of a pointer-to-member, *TYPE is
22623 filled in with the TYPE containing the member. *CV_QUALS is
22624 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
22625 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
22626 Note that the tree codes returned by this function have nothing
22627 to do with the types of trees that will be eventually be created
22628 to represent the pointer or reference type being parsed. They are
22629 just constants with suggestive names. */
22630 static enum tree_code
22631 cp_parser_ptr_operator (cp_parser
* parser
,
22633 cp_cv_quals
*cv_quals
,
22636 enum tree_code code
= ERROR_MARK
;
22638 tree attrs
= NULL_TREE
;
22640 /* Assume that it's not a pointer-to-member. */
22642 /* And that there are no cv-qualifiers. */
22643 *cv_quals
= TYPE_UNQUALIFIED
;
22645 /* Peek at the next token. */
22646 token
= cp_lexer_peek_token (parser
->lexer
);
22648 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
22649 if (token
->type
== CPP_MULT
)
22650 code
= INDIRECT_REF
;
22651 else if (token
->type
== CPP_AND
)
22653 else if ((cxx_dialect
!= cxx98
) &&
22654 token
->type
== CPP_AND_AND
) /* C++0x only */
22655 code
= NON_LVALUE_EXPR
;
22657 if (code
!= ERROR_MARK
)
22659 /* Consume the `*', `&' or `&&'. */
22660 cp_lexer_consume_token (parser
->lexer
);
22662 /* A `*' can be followed by a cv-qualifier-seq, and so can a
22663 `&', if we are allowing GNU extensions. (The only qualifier
22664 that can legally appear after `&' is `restrict', but that is
22665 enforced during semantic analysis. */
22666 if (code
== INDIRECT_REF
22667 || cp_parser_allow_gnu_extensions_p (parser
))
22668 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
22670 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22671 if (attributes
!= NULL
)
22672 *attributes
= attrs
;
22676 /* Try the pointer-to-member case. */
22677 cp_parser_parse_tentatively (parser
);
22678 /* Look for the optional `::' operator. */
22679 cp_parser_global_scope_opt (parser
,
22680 /*current_scope_valid_p=*/false);
22681 /* Look for the nested-name specifier. */
22682 token
= cp_lexer_peek_token (parser
->lexer
);
22683 cp_parser_nested_name_specifier (parser
,
22684 /*typename_keyword_p=*/false,
22685 /*check_dependency_p=*/true,
22687 /*is_declaration=*/false);
22688 /* If we found it, and the next token is a `*', then we are
22689 indeed looking at a pointer-to-member operator. */
22690 if (!cp_parser_error_occurred (parser
)
22691 && cp_parser_require (parser
, CPP_MULT
, RT_MULT
))
22693 /* Indicate that the `*' operator was used. */
22694 code
= INDIRECT_REF
;
22696 if (TREE_CODE (parser
->scope
) == NAMESPACE_DECL
)
22697 error_at (token
->location
, "%qD is a namespace", parser
->scope
);
22698 else if (TREE_CODE (parser
->scope
) == ENUMERAL_TYPE
)
22699 error_at (token
->location
, "cannot form pointer to member of "
22700 "non-class %q#T", parser
->scope
);
22703 /* The type of which the member is a member is given by the
22705 *type
= parser
->scope
;
22706 /* The next name will not be qualified. */
22707 parser
->scope
= NULL_TREE
;
22708 parser
->qualifying_scope
= NULL_TREE
;
22709 parser
->object_scope
= NULL_TREE
;
22710 /* Look for optional c++11 attributes. */
22711 attrs
= cp_parser_std_attribute_spec_seq (parser
);
22712 if (attributes
!= NULL
)
22713 *attributes
= attrs
;
22714 /* Look for the optional cv-qualifier-seq. */
22715 *cv_quals
= cp_parser_cv_qualifier_seq_opt (parser
);
22718 /* If that didn't work we don't have a ptr-operator. */
22719 if (!cp_parser_parse_definitely (parser
))
22720 cp_parser_error (parser
, "expected ptr-operator");
22726 /* Parse an (optional) cv-qualifier-seq.
22729 cv-qualifier cv-qualifier-seq [opt]
22740 Returns a bitmask representing the cv-qualifiers. */
22743 cp_parser_cv_qualifier_seq_opt (cp_parser
* parser
)
22745 cp_cv_quals cv_quals
= TYPE_UNQUALIFIED
;
22750 cp_cv_quals cv_qualifier
;
22752 /* Peek at the next token. */
22753 token
= cp_lexer_peek_token (parser
->lexer
);
22754 /* See if it's a cv-qualifier. */
22755 switch (token
->keyword
)
22758 cv_qualifier
= TYPE_QUAL_CONST
;
22762 cv_qualifier
= TYPE_QUAL_VOLATILE
;
22766 cv_qualifier
= TYPE_QUAL_RESTRICT
;
22770 cv_qualifier
= TYPE_UNQUALIFIED
;
22777 if (cv_quals
& cv_qualifier
)
22779 gcc_rich_location
richloc (token
->location
);
22780 richloc
.add_fixit_remove ();
22781 error_at (&richloc
, "duplicate cv-qualifier");
22782 cp_lexer_purge_token (parser
->lexer
);
22786 cp_lexer_consume_token (parser
->lexer
);
22787 cv_quals
|= cv_qualifier
;
22794 /* Parse an (optional) ref-qualifier
22800 Returns cp_ref_qualifier representing ref-qualifier. */
22802 static cp_ref_qualifier
22803 cp_parser_ref_qualifier_opt (cp_parser
* parser
)
22805 cp_ref_qualifier ref_qual
= REF_QUAL_NONE
;
22807 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
22808 if (cxx_dialect
< cxx11
&& cp_parser_parsing_tentatively (parser
))
22813 cp_ref_qualifier curr_ref_qual
= REF_QUAL_NONE
;
22814 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22816 switch (token
->type
)
22819 curr_ref_qual
= REF_QUAL_LVALUE
;
22823 curr_ref_qual
= REF_QUAL_RVALUE
;
22827 curr_ref_qual
= REF_QUAL_NONE
;
22831 if (!curr_ref_qual
)
22835 error_at (token
->location
, "multiple ref-qualifiers");
22836 cp_lexer_purge_token (parser
->lexer
);
22840 ref_qual
= curr_ref_qual
;
22841 cp_lexer_consume_token (parser
->lexer
);
22848 /* Parse an optional tx-qualifier.
22852 transaction_safe_dynamic */
22855 cp_parser_tx_qualifier_opt (cp_parser
*parser
)
22857 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
22858 if (token
->type
== CPP_NAME
)
22860 tree name
= token
->u
.value
;
22861 const char *p
= IDENTIFIER_POINTER (name
);
22862 const int len
= strlen ("transaction_safe");
22863 if (!strncmp (p
, "transaction_safe", len
))
22867 || !strcmp (p
, "_dynamic"))
22869 cp_lexer_consume_token (parser
->lexer
);
22872 error ("%qE requires %<-fgnu-tm%>", name
);
22883 /* Parse an (optional) virt-specifier-seq.
22885 virt-specifier-seq:
22886 virt-specifier virt-specifier-seq [opt]
22892 Returns a bitmask representing the virt-specifiers. */
22894 static cp_virt_specifiers
22895 cp_parser_virt_specifier_seq_opt (cp_parser
* parser
)
22897 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
22902 cp_virt_specifiers virt_specifier
;
22904 /* Peek at the next token. */
22905 token
= cp_lexer_peek_token (parser
->lexer
);
22906 /* See if it's a virt-specifier-qualifier. */
22907 if (token
->type
!= CPP_NAME
)
22909 if (id_equal (token
->u
.value
, "override"))
22911 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
22912 virt_specifier
= VIRT_SPEC_OVERRIDE
;
22914 else if (id_equal (token
->u
.value
, "final"))
22916 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS
);
22917 virt_specifier
= VIRT_SPEC_FINAL
;
22919 else if (id_equal (token
->u
.value
, "__final"))
22921 virt_specifier
= VIRT_SPEC_FINAL
;
22926 if (virt_specifiers
& virt_specifier
)
22928 gcc_rich_location
richloc (token
->location
);
22929 richloc
.add_fixit_remove ();
22930 error_at (&richloc
, "duplicate virt-specifier");
22931 cp_lexer_purge_token (parser
->lexer
);
22935 cp_lexer_consume_token (parser
->lexer
);
22936 virt_specifiers
|= virt_specifier
;
22939 return virt_specifiers
;
22942 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
22943 is in scope even though it isn't real. */
22946 inject_this_parameter (tree ctype
, cp_cv_quals quals
)
22950 if (current_class_ptr
)
22952 /* We don't clear this between NSDMIs. Is it already what we want? */
22953 tree type
= TREE_TYPE (TREE_TYPE (current_class_ptr
));
22954 if (DECL_P (current_class_ptr
)
22955 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
22956 && same_type_ignoring_top_level_qualifiers_p (ctype
, type
)
22957 && cp_type_quals (type
) == quals
)
22961 this_parm
= build_this_parm (NULL_TREE
, ctype
, quals
);
22962 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
22963 current_class_ptr
= NULL_TREE
;
22965 = cp_build_fold_indirect_ref (this_parm
);
22966 current_class_ptr
= this_parm
;
22969 /* Return true iff our current scope is a non-static data member
22973 parsing_nsdmi (void)
22975 /* We recognize NSDMI context by the context-less 'this' pointer set up
22976 by the function above. */
22977 if (current_class_ptr
22978 && TREE_CODE (current_class_ptr
) == PARM_DECL
22979 && DECL_CONTEXT (current_class_ptr
) == NULL_TREE
)
22984 /* Parse a late-specified return type, if any. This is not a separate
22985 non-terminal, but part of a function declarator, which looks like
22987 -> trailing-type-specifier-seq abstract-declarator(opt)
22989 Returns the type indicated by the type-id.
22991 In addition to this, parse any queued up #pragma omp declare simd
22992 clauses, and #pragma acc routine clauses.
22994 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
22998 cp_parser_late_return_type_opt (cp_parser
* parser
, cp_declarator
*declarator
,
22999 tree
& requires_clause
)
23002 tree type
= NULL_TREE
;
23003 bool declare_simd_p
= (parser
->omp_declare_simd
23005 && declarator
->kind
== cdk_id
);
23007 bool oacc_routine_p
= (parser
->oacc_routine
23009 && declarator
->kind
== cdk_id
);
23011 /* Peek at the next token. */
23012 token
= cp_lexer_peek_token (parser
->lexer
);
23013 /* A late-specified return type is indicated by an initial '->'. */
23014 if (token
->type
!= CPP_DEREF
23015 && token
->keyword
!= RID_REQUIRES
23016 && !(token
->type
== CPP_NAME
23017 && token
->u
.value
== ridpointers
[RID_REQUIRES
])
23018 && !(declare_simd_p
|| oacc_routine_p
))
23021 if (token
->type
== CPP_DEREF
)
23023 /* Consume the ->. */
23024 cp_lexer_consume_token (parser
->lexer
);
23026 type
= cp_parser_trailing_type_id (parser
);
23029 /* Function declarations may be followed by a trailing
23030 requires-clause. */
23031 requires_clause
= cp_parser_requires_clause_opt (parser
, false);
23033 if (declare_simd_p
)
23034 declarator
->attributes
23035 = cp_parser_late_parsing_omp_declare_simd (parser
,
23036 declarator
->attributes
);
23037 if (oacc_routine_p
)
23038 declarator
->attributes
23039 = cp_parser_late_parsing_oacc_routine (parser
,
23040 declarator
->attributes
);
23045 /* Parse a declarator-id.
23049 :: [opt] nested-name-specifier [opt] type-name
23051 In the `id-expression' case, the value returned is as for
23052 cp_parser_id_expression if the id-expression was an unqualified-id.
23053 If the id-expression was a qualified-id, then a SCOPE_REF is
23054 returned. The first operand is the scope (either a NAMESPACE_DECL
23055 or TREE_TYPE), but the second is still just a representation of an
23059 cp_parser_declarator_id (cp_parser
* parser
, bool optional_p
)
23062 /* The expression must be an id-expression. Assume that qualified
23063 names are the names of types so that:
23066 int S<T>::R::i = 3;
23068 will work; we must treat `S<T>::R' as the name of a type.
23069 Similarly, assume that qualified names are templates, where
23073 int S<T>::R<T>::i = 3;
23076 id
= cp_parser_id_expression (parser
,
23077 /*template_keyword_p=*/false,
23078 /*check_dependency_p=*/false,
23079 /*template_p=*/NULL
,
23080 /*declarator_p=*/true,
23082 if (id
&& BASELINK_P (id
))
23083 id
= BASELINK_FUNCTIONS (id
);
23087 /* Parse a type-id.
23090 type-specifier-seq abstract-declarator [opt]
23092 The parser flags FLAGS is used to control type-specifier parsing.
23094 If IS_TEMPLATE_ARG is true, we are parsing a template argument.
23096 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
23097 i.e. we've just seen "->".
23099 Returns the TYPE specified. */
23102 cp_parser_type_id_1 (cp_parser
*parser
, cp_parser_flags flags
,
23103 bool is_template_arg
, bool is_trailing_return
,
23104 location_t
*type_location
)
23106 cp_decl_specifier_seq type_specifier_seq
;
23107 cp_declarator
*abstract_declarator
;
23109 /* Parse the type-specifier-seq. */
23110 cp_parser_type_specifier_seq (parser
, flags
,
23111 /*is_declaration=*/false,
23112 is_trailing_return
,
23113 &type_specifier_seq
);
23115 *type_location
= type_specifier_seq
.locations
[ds_type_spec
];
23117 if (is_template_arg
&& type_specifier_seq
.type
23118 && TREE_CODE (type_specifier_seq
.type
) == TEMPLATE_TYPE_PARM
23119 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq
.type
))
23120 /* A bare template name as a template argument is a template template
23121 argument, not a placeholder, so fail parsing it as a type argument. */
23123 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser
));
23124 cp_parser_simulate_error (parser
);
23125 return error_mark_node
;
23127 if (type_specifier_seq
.type
== error_mark_node
)
23128 return error_mark_node
;
23130 /* There might or might not be an abstract declarator. */
23131 cp_parser_parse_tentatively (parser
);
23132 /* Look for the declarator. */
23133 abstract_declarator
23134 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_ABSTRACT
,
23135 CP_PARSER_FLAGS_NONE
, NULL
,
23136 /*parenthesized_p=*/NULL
,
23137 /*member_p=*/false,
23138 /*friend_p=*/false,
23139 /*static_p=*/false);
23140 /* Check to see if there really was a declarator. */
23141 if (!cp_parser_parse_definitely (parser
))
23142 abstract_declarator
= NULL
;
23144 bool auto_typeid_ok
= false;
23145 /* The concepts TS allows 'auto' as a type-id. */
23146 if (flag_concepts_ts
)
23147 auto_typeid_ok
= !parser
->in_type_id_in_expr_p
;
23148 /* DR 625 prohibits use of auto as a template-argument. We allow 'auto'
23149 outside the template-argument-list context here only for the sake of
23150 diagnostic: grokdeclarator then can emit a better error message for
23151 e.g. using T = auto. */
23152 else if (flag_concepts
)
23153 auto_typeid_ok
= (!parser
->in_type_id_in_expr_p
23154 && !parser
->in_template_argument_list_p
);
23156 if (type_specifier_seq
.type
23158 /* None of the valid uses of 'auto' in C++14 involve the type-id
23159 nonterminal, but it is valid in a trailing-return-type. */
23160 && !(cxx_dialect
>= cxx14
&& is_trailing_return
))
23161 if (tree auto_node
= type_uses_auto (type_specifier_seq
.type
))
23163 /* A type-id with type 'auto' is only ok if the abstract declarator
23164 is a function declarator with a late-specified return type.
23166 A type-id with 'auto' is also valid in a trailing-return-type
23167 in a compound-requirement. */
23168 if (abstract_declarator
23169 && abstract_declarator
->kind
== cdk_function
23170 && abstract_declarator
->u
.function
.late_return_type
)
23172 else if (parser
->in_result_type_constraint_p
)
23176 location_t loc
= type_specifier_seq
.locations
[ds_type_spec
];
23177 if (tree tmpl
= CLASS_PLACEHOLDER_TEMPLATE (auto_node
))
23179 error_at (loc
, "missing template arguments after %qT",
23181 inform (DECL_SOURCE_LOCATION (tmpl
), "%qD declared here",
23184 else if (parser
->in_template_argument_list_p
)
23185 error_at (loc
, "%qT not permitted in template argument",
23188 error_at (loc
, "invalid use of %qT", auto_node
);
23189 return error_mark_node
;
23193 return groktypename (&type_specifier_seq
, abstract_declarator
,
23197 /* Wrapper for cp_parser_type_id_1. */
23200 cp_parser_type_id (cp_parser
*parser
, cp_parser_flags flags
,
23201 location_t
*type_location
)
23203 return cp_parser_type_id_1 (parser
, flags
, false, false, type_location
);
23206 /* Wrapper for cp_parser_type_id_1. */
23209 cp_parser_template_type_arg (cp_parser
*parser
)
23212 const char *saved_message
= parser
->type_definition_forbidden_message
;
23213 parser
->type_definition_forbidden_message
23214 = G_("types may not be defined in template arguments");
23215 r
= cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_NONE
, true, false, NULL
);
23216 parser
->type_definition_forbidden_message
= saved_message
;
23217 if (cxx_dialect
>= cxx14
&& !flag_concepts
&& type_uses_auto (r
))
23219 error ("invalid use of %<auto%> in template argument");
23220 r
= error_mark_node
;
23225 /* Wrapper for cp_parser_type_id_1. */
23228 cp_parser_trailing_type_id (cp_parser
*parser
)
23230 return cp_parser_type_id_1 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
23231 false, true, NULL
);
23234 /* Parse a type-specifier-seq.
23236 type-specifier-seq:
23237 type-specifier type-specifier-seq [opt]
23241 type-specifier-seq:
23242 attributes type-specifier-seq [opt]
23244 The parser flags FLAGS is used to control type-specifier parsing.
23246 If IS_DECLARATION is true, we are at the start of a "condition" or
23247 exception-declaration, so we might be followed by a declarator-id.
23249 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
23250 i.e. we've just seen "->".
23252 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
23255 cp_parser_type_specifier_seq (cp_parser
* parser
,
23256 cp_parser_flags flags
,
23257 bool is_declaration
,
23258 bool is_trailing_return
,
23259 cp_decl_specifier_seq
*type_specifier_seq
)
23261 bool seen_type_specifier
= false;
23262 cp_token
*start_token
= NULL
;
23264 /* Clear the TYPE_SPECIFIER_SEQ. */
23265 clear_decl_specs (type_specifier_seq
);
23267 flags
|= CP_PARSER_FLAGS_OPTIONAL
;
23268 /* In the context of a trailing return type, enum E { } is an
23269 elaborated-type-specifier followed by a function-body, not an
23271 if (is_trailing_return
)
23272 flags
|= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS
;
23274 /* Parse the type-specifiers and attributes. */
23277 tree type_specifier
;
23278 bool is_cv_qualifier
;
23280 /* Check for attributes first. */
23281 if (cp_next_tokens_can_be_attribute_p (parser
))
23283 /* GNU attributes at the end of a declaration apply to the
23284 declaration as a whole, not to the trailing return type. So look
23285 ahead to see if these attributes are at the end. */
23286 if (seen_type_specifier
&& is_trailing_return
23287 && cp_next_tokens_can_be_gnu_attribute_p (parser
))
23289 size_t n
= cp_parser_skip_attributes_opt (parser
, 1);
23290 cp_token
*tok
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
23291 if (tok
->type
== CPP_SEMICOLON
|| tok
->type
== CPP_COMMA
23292 || tok
->type
== CPP_EQ
|| tok
->type
== CPP_OPEN_BRACE
)
23295 type_specifier_seq
->attributes
23296 = attr_chainon (type_specifier_seq
->attributes
,
23297 cp_parser_attributes_opt (parser
));
23301 /* record the token of the beginning of the type specifier seq,
23302 for error reporting purposes*/
23304 start_token
= cp_lexer_peek_token (parser
->lexer
);
23306 /* Look for the type-specifier. */
23307 type_specifier
= cp_parser_type_specifier (parser
,
23309 type_specifier_seq
,
23310 /*is_declaration=*/false,
23313 if (!type_specifier
)
23315 /* If the first type-specifier could not be found, this is not a
23316 type-specifier-seq at all. */
23317 if (!seen_type_specifier
)
23319 /* Set in_declarator_p to avoid skipping to the semicolon. */
23320 int in_decl
= parser
->in_declarator_p
;
23321 parser
->in_declarator_p
= true;
23323 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
23324 || !cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23325 cp_parser_error (parser
, "expected type-specifier");
23327 parser
->in_declarator_p
= in_decl
;
23329 type_specifier_seq
->type
= error_mark_node
;
23332 /* If subsequent type-specifiers could not be found, the
23333 type-specifier-seq is complete. */
23337 seen_type_specifier
= true;
23338 /* The standard says that a condition can be:
23340 type-specifier-seq declarator = assignment-expression
23347 we should treat the "S" as a declarator, not as a
23348 type-specifier. The standard doesn't say that explicitly for
23349 type-specifier-seq, but it does say that for
23350 decl-specifier-seq in an ordinary declaration. Perhaps it
23351 would be clearer just to allow a decl-specifier-seq here, and
23352 then add a semantic restriction that if any decl-specifiers
23353 that are not type-specifiers appear, the program is invalid. */
23354 if (is_declaration
&& !is_cv_qualifier
)
23355 flags
|= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES
;
23359 /* Return whether the function currently being declared has an associated
23360 template parameter list. */
23363 function_being_declared_is_template_p (cp_parser
* parser
)
23365 if (!current_template_parms
|| processing_template_parmlist
)
23368 if (parser
->implicit_template_scope
)
23371 if (at_class_scope_p ()
23372 && TYPE_BEING_DEFINED (current_class_type
))
23373 return parser
->num_template_parameter_lists
!= 0;
23375 return ((int) parser
->num_template_parameter_lists
> template_class_depth
23376 (current_class_type
));
23379 /* Parse a parameter-declaration-clause.
23381 parameter-declaration-clause:
23382 parameter-declaration-list [opt] ... [opt]
23383 parameter-declaration-list , ...
23385 The parser flags FLAGS is used to control type-specifier parsing.
23387 Returns a representation for the parameter declarations. A return
23388 value of NULL indicates a parameter-declaration-clause consisting
23389 only of an ellipsis. */
23392 cp_parser_parameter_declaration_clause (cp_parser
* parser
,
23393 cp_parser_flags flags
)
23399 auto cleanup
= make_temp_override
23400 (parser
->auto_is_implicit_function_template_parm_p
);
23402 if (!processing_specialization
23403 && !processing_template_parmlist
23404 && !processing_explicit_instantiation
23405 /* default_arg_ok_p tracks whether this is a parameter-clause for an
23406 actual function or a random abstract declarator. */
23407 && parser
->default_arg_ok_p
)
23408 if (!current_function_decl
23409 || (current_class_type
&& LAMBDA_TYPE_P (current_class_type
)))
23410 parser
->auto_is_implicit_function_template_parm_p
= true;
23412 /* Peek at the next token. */
23413 token
= cp_lexer_peek_token (parser
->lexer
);
23414 /* Check for trivial parameter-declaration-clauses. */
23415 if (token
->type
== CPP_ELLIPSIS
)
23417 /* Consume the `...' token. */
23418 cp_lexer_consume_token (parser
->lexer
);
23421 else if (token
->type
== CPP_CLOSE_PAREN
)
23422 /* There are no parameters. */
23423 return void_list_node
;
23424 /* Check for `(void)', too, which is a special case. */
23425 else if (token
->keyword
== RID_VOID
23426 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
23427 == CPP_CLOSE_PAREN
))
23429 /* Consume the `void' token. */
23430 cp_lexer_consume_token (parser
->lexer
);
23431 /* There are no parameters. */
23432 return explicit_void_list_node
;
23435 /* Parse the parameter-declaration-list. */
23436 parameters
= cp_parser_parameter_declaration_list (parser
, flags
);
23437 /* If a parse error occurred while parsing the
23438 parameter-declaration-list, then the entire
23439 parameter-declaration-clause is erroneous. */
23440 if (parameters
== error_mark_node
)
23443 /* Peek at the next token. */
23444 token
= cp_lexer_peek_token (parser
->lexer
);
23445 /* If it's a `,', the clause should terminate with an ellipsis. */
23446 if (token
->type
== CPP_COMMA
)
23448 /* Consume the `,'. */
23449 cp_lexer_consume_token (parser
->lexer
);
23450 /* Expect an ellipsis. */
23452 = (cp_parser_require (parser
, CPP_ELLIPSIS
, RT_ELLIPSIS
) != NULL
);
23454 /* It might also be `...' if the optional trailing `,' was
23456 else if (token
->type
== CPP_ELLIPSIS
)
23458 /* Consume the `...' token. */
23459 cp_lexer_consume_token (parser
->lexer
);
23460 /* And remember that we saw it. */
23464 ellipsis_p
= false;
23466 /* Finish the parameter list. */
23468 parameters
= chainon (parameters
, void_list_node
);
23473 /* Parse a parameter-declaration-list.
23475 parameter-declaration-list:
23476 parameter-declaration
23477 parameter-declaration-list , parameter-declaration
23479 The parser flags FLAGS is used to control type-specifier parsing.
23481 Returns a representation of the parameter-declaration-list, as for
23482 cp_parser_parameter_declaration_clause. However, the
23483 `void_list_node' is never appended to the list. */
23486 cp_parser_parameter_declaration_list (cp_parser
* parser
, cp_parser_flags flags
)
23488 tree parameters
= NULL_TREE
;
23489 tree
*tail
= ¶meters
;
23490 bool saved_in_unbraced_linkage_specification_p
;
23493 /* The special considerations that apply to a function within an
23494 unbraced linkage specifications do not apply to the parameters
23495 to the function. */
23496 saved_in_unbraced_linkage_specification_p
23497 = parser
->in_unbraced_linkage_specification_p
;
23498 parser
->in_unbraced_linkage_specification_p
= false;
23500 /* Look for more parameters. */
23503 cp_parameter_declarator
*parameter
;
23504 tree decl
= error_mark_node
;
23505 bool parenthesized_p
= false;
23507 /* Parse the parameter. */
23509 = cp_parser_parameter_declaration (parser
, flags
,
23510 /*template_parm_p=*/false,
23513 /* We don't know yet if the enclosing context is deprecated, so wait
23514 and warn in grokparms if appropriate. */
23515 deprecated_state
= DEPRECATED_SUPPRESS
;
23519 decl
= grokdeclarator (parameter
->declarator
,
23520 ¶meter
->decl_specifiers
,
23522 parameter
->default_argument
!= NULL_TREE
,
23523 ¶meter
->decl_specifiers
.attributes
);
23524 if (decl
!= error_mark_node
&& parameter
->loc
!= UNKNOWN_LOCATION
)
23525 DECL_SOURCE_LOCATION (decl
) = parameter
->loc
;
23528 deprecated_state
= DEPRECATED_NORMAL
;
23530 /* If a parse error occurred parsing the parameter declaration,
23531 then the entire parameter-declaration-list is erroneous. */
23532 if (decl
== error_mark_node
)
23534 parameters
= error_mark_node
;
23538 if (parameter
->decl_specifiers
.attributes
)
23539 cplus_decl_attributes (&decl
,
23540 parameter
->decl_specifiers
.attributes
,
23542 if (DECL_NAME (decl
))
23543 decl
= pushdecl (decl
);
23545 if (decl
!= error_mark_node
)
23547 retrofit_lang_decl (decl
);
23548 DECL_PARM_INDEX (decl
) = ++index
;
23549 DECL_PARM_LEVEL (decl
) = function_parm_depth ();
23552 /* Add the new parameter to the list. */
23553 *tail
= build_tree_list (parameter
->default_argument
, decl
);
23554 tail
= &TREE_CHAIN (*tail
);
23556 /* If the parameters were parenthesized, it's the case of
23557 T foo(X(x)) which looks like a variable definition but
23558 is a function declaration. */
23559 if (index
== 1 || PARENTHESIZED_LIST_P (parameters
))
23560 PARENTHESIZED_LIST_P (parameters
) = parenthesized_p
;
23562 /* Peek at the next token. */
23563 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
23564 || cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
)
23565 /* These are for Objective-C++ */
23566 || cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
23567 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
23568 /* The parameter-declaration-list is complete. */
23570 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
23574 /* Peek at the next token. */
23575 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
23576 /* If it's an ellipsis, then the list is complete. */
23577 if (token
->type
== CPP_ELLIPSIS
)
23579 /* Otherwise, there must be more parameters. Consume the
23581 cp_lexer_consume_token (parser
->lexer
);
23582 /* When parsing something like:
23584 int i(float f, double d)
23586 we can tell after seeing the declaration for "f" that we
23587 are not looking at an initialization of a variable "i",
23588 but rather at the declaration of a function "i".
23590 Due to the fact that the parsing of template arguments
23591 (as specified to a template-id) requires backtracking we
23592 cannot use this technique when inside a template argument
23594 if (!parser
->in_template_argument_list_p
23595 && !parser
->in_type_id_in_expr_p
23596 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
23597 /* However, a parameter-declaration of the form
23598 "float(f)" (which is a valid declaration of a
23599 parameter "f") can also be interpreted as an
23600 expression (the conversion of "f" to "float"). */
23601 && !parenthesized_p
)
23602 cp_parser_commit_to_tentative_parse (parser
);
23606 cp_parser_error (parser
, "expected %<,%> or %<...%>");
23607 if (!cp_parser_uncommitted_to_tentative_parse_p (parser
))
23608 cp_parser_skip_to_closing_parenthesis (parser
,
23609 /*recovering=*/true,
23610 /*or_comma=*/false,
23611 /*consume_paren=*/false);
23616 parser
->in_unbraced_linkage_specification_p
23617 = saved_in_unbraced_linkage_specification_p
;
23619 /* Reset implicit_template_scope if we are about to leave the function
23620 parameter list that introduced it. Note that for out-of-line member
23621 definitions, there will be one or more class scopes before we get to
23622 the template parameter scope. */
23624 if (cp_binding_level
*its
= parser
->implicit_template_scope
)
23625 if (cp_binding_level
*maybe_its
= current_binding_level
->level_chain
)
23627 while (maybe_its
->kind
== sk_class
)
23628 maybe_its
= maybe_its
->level_chain
;
23629 if (maybe_its
== its
)
23631 parser
->implicit_template_parms
= 0;
23632 parser
->implicit_template_scope
= 0;
23639 /* Parse a parameter declaration.
23641 parameter-declaration:
23642 decl-specifier-seq ... [opt] declarator
23643 decl-specifier-seq declarator = assignment-expression
23644 decl-specifier-seq ... [opt] abstract-declarator [opt]
23645 decl-specifier-seq abstract-declarator [opt] = assignment-expression
23647 The parser flags FLAGS is used to control type-specifier parsing.
23649 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
23650 declares a template parameter. (In that case, a non-nested `>'
23651 token encountered during the parsing of the assignment-expression
23652 is not interpreted as a greater-than operator.)
23654 Returns a representation of the parameter, or NULL if an error
23655 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
23656 true iff the declarator is of the form "(p)". */
23658 static cp_parameter_declarator
*
23659 cp_parser_parameter_declaration (cp_parser
*parser
,
23660 cp_parser_flags flags
,
23661 bool template_parm_p
,
23662 bool *parenthesized_p
)
23664 int declares_class_or_enum
;
23665 cp_decl_specifier_seq decl_specifiers
;
23666 cp_declarator
*declarator
;
23667 tree default_argument
;
23668 cp_token
*token
= NULL
, *declarator_token_start
= NULL
;
23669 const char *saved_message
;
23670 bool template_parameter_pack_p
= false;
23672 /* In a template parameter, `>' is not an operator.
23676 When parsing a default template-argument for a non-type
23677 template-parameter, the first non-nested `>' is taken as the end
23678 of the template parameter-list rather than a greater-than
23681 /* Type definitions may not appear in parameter types. */
23682 saved_message
= parser
->type_definition_forbidden_message
;
23683 parser
->type_definition_forbidden_message
23684 = G_("types may not be defined in parameter types");
23686 int template_parm_idx
= (function_being_declared_is_template_p (parser
) ?
23687 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
23688 (current_template_parms
)) : 0);
23690 /* Parse the declaration-specifiers. */
23691 cp_token
*decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
23692 cp_parser_decl_specifier_seq (parser
,
23695 &declares_class_or_enum
);
23697 /* Complain about missing 'typename' or other invalid type names. */
23698 if (!decl_specifiers
.any_type_specifiers_p
23699 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
23700 decl_specifiers
.type
= error_mark_node
;
23702 /* If an error occurred, there's no reason to attempt to parse the
23703 rest of the declaration. */
23704 if (cp_parser_error_occurred (parser
))
23706 parser
->type_definition_forbidden_message
= saved_message
;
23710 /* Peek at the next token. */
23711 token
= cp_lexer_peek_token (parser
->lexer
);
23713 /* If the next token is a `)', `,', `=', `>', or `...', then there
23714 is no declarator. However, when variadic templates are enabled,
23715 there may be a declarator following `...'. */
23716 if (token
->type
== CPP_CLOSE_PAREN
23717 || token
->type
== CPP_COMMA
23718 || token
->type
== CPP_EQ
23719 || token
->type
== CPP_GREATER
)
23722 if (parenthesized_p
)
23723 *parenthesized_p
= false;
23725 /* Otherwise, there should be a declarator. */
23728 bool saved_default_arg_ok_p
= parser
->default_arg_ok_p
;
23729 parser
->default_arg_ok_p
= false;
23731 /* After seeing a decl-specifier-seq, if the next token is not a
23732 "(", there is no possibility that the code is a valid
23733 expression. Therefore, if parsing tentatively, we commit at
23735 if (!parser
->in_template_argument_list_p
23736 /* In an expression context, having seen:
23740 we cannot be sure whether we are looking at a
23741 function-type (taking a "char" as a parameter) or a cast
23742 of some object of type "char" to "int". */
23743 && !parser
->in_type_id_in_expr_p
23744 && cp_parser_uncommitted_to_tentative_parse_p (parser
)
23745 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
)
23746 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_PAREN
))
23747 cp_parser_commit_to_tentative_parse (parser
);
23748 /* Parse the declarator. */
23749 declarator_token_start
= token
;
23750 declarator
= cp_parser_declarator (parser
,
23751 CP_PARSER_DECLARATOR_EITHER
,
23752 CP_PARSER_FLAGS_NONE
,
23753 /*ctor_dtor_or_conv_p=*/NULL
,
23755 /*member_p=*/false,
23756 /*friend_p=*/false,
23757 /*static_p=*/false);
23758 parser
->default_arg_ok_p
= saved_default_arg_ok_p
;
23759 /* After the declarator, allow more attributes. */
23760 decl_specifiers
.attributes
23761 = attr_chainon (decl_specifiers
.attributes
,
23762 cp_parser_attributes_opt (parser
));
23764 /* If the declarator is a template parameter pack, remember that and
23765 clear the flag in the declarator itself so we don't get errors
23766 from grokdeclarator. */
23767 if (template_parm_p
&& declarator
&& declarator
->parameter_pack_p
)
23769 declarator
->parameter_pack_p
= false;
23770 template_parameter_pack_p
= true;
23774 /* If the next token is an ellipsis, and we have not seen a declarator
23775 name, and if either the type of the declarator contains parameter
23776 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
23777 for, eg, abbreviated integral type names), then we actually have a
23778 parameter pack expansion expression. Otherwise, leave the ellipsis
23779 for a C-style variadic function. */
23780 token
= cp_lexer_peek_token (parser
->lexer
);
23782 /* If a function parameter pack was specified and an implicit template
23783 parameter was introduced during cp_parser_parameter_declaration,
23784 change any implicit parameters introduced into packs. */
23785 if (parser
->implicit_template_parms
23786 && ((token
->type
== CPP_ELLIPSIS
23787 && declarator_can_be_parameter_pack (declarator
))
23788 || (declarator
&& declarator
->parameter_pack_p
)))
23790 int latest_template_parm_idx
= TREE_VEC_LENGTH
23791 (INNERMOST_TEMPLATE_PARMS (current_template_parms
));
23793 if (latest_template_parm_idx
!= template_parm_idx
)
23794 decl_specifiers
.type
= convert_generic_types_to_packs
23795 (decl_specifiers
.type
,
23796 template_parm_idx
, latest_template_parm_idx
);
23799 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
23801 tree type
= decl_specifiers
.type
;
23803 if (type
&& DECL_P (type
))
23804 type
= TREE_TYPE (type
);
23807 && TREE_CODE (type
) != TYPE_PACK_EXPANSION
23808 && (template_parm_p
|| uses_parameter_packs (type
)))
23809 || (!type
&& template_parm_p
))
23810 && declarator_can_be_parameter_pack (declarator
))
23812 /* Consume the `...'. */
23813 cp_lexer_consume_token (parser
->lexer
);
23814 maybe_warn_variadic_templates ();
23816 /* Build a pack expansion type */
23817 if (template_parm_p
)
23818 template_parameter_pack_p
= true;
23819 else if (declarator
)
23820 declarator
->parameter_pack_p
= true;
23822 decl_specifiers
.type
= make_pack_expansion (type
);
23826 /* The restriction on defining new types applies only to the type
23827 of the parameter, not to the default argument. */
23828 parser
->type_definition_forbidden_message
= saved_message
;
23830 /* If the next token is `=', then process a default argument. */
23831 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
23833 tree type
= decl_specifiers
.type
;
23834 token
= cp_lexer_peek_token (parser
->lexer
);
23835 /* If we are defining a class, then the tokens that make up the
23836 default argument must be saved and processed later. */
23837 if (!template_parm_p
&& at_class_scope_p ()
23838 && TYPE_BEING_DEFINED (current_class_type
)
23839 && !LAMBDA_TYPE_P (current_class_type
))
23840 default_argument
= cp_parser_cache_defarg (parser
, /*nsdmi=*/false);
23842 /* A constrained-type-specifier may declare a type
23843 template-parameter. */
23844 else if (declares_constrained_type_template_parameter (type
))
23846 = cp_parser_default_type_template_argument (parser
);
23848 /* A constrained-type-specifier may declare a
23849 template-template-parameter. */
23850 else if (declares_constrained_template_template_parameter (type
))
23852 = cp_parser_default_template_template_argument (parser
);
23854 /* Outside of a class definition, we can just parse the
23855 assignment-expression. */
23858 = cp_parser_default_argument (parser
, template_parm_p
);
23860 if (!parser
->default_arg_ok_p
)
23862 permerror (token
->location
,
23863 "default arguments are only "
23864 "permitted for function parameters");
23866 else if ((declarator
&& declarator
->parameter_pack_p
)
23867 || template_parameter_pack_p
23868 || (decl_specifiers
.type
23869 && PACK_EXPANSION_P (decl_specifiers
.type
)))
23871 /* Find the name of the parameter pack. */
23872 cp_declarator
*id_declarator
= declarator
;
23873 while (id_declarator
&& id_declarator
->kind
!= cdk_id
)
23874 id_declarator
= id_declarator
->declarator
;
23876 if (id_declarator
&& id_declarator
->kind
== cdk_id
)
23877 error_at (declarator_token_start
->location
,
23879 ? G_("template parameter pack %qD "
23880 "cannot have a default argument")
23881 : G_("parameter pack %qD cannot have "
23882 "a default argument"),
23883 id_declarator
->u
.id
.unqualified_name
);
23885 error_at (declarator_token_start
->location
,
23887 ? G_("template parameter pack cannot have "
23888 "a default argument")
23889 : G_("parameter pack cannot have a "
23890 "default argument"));
23892 default_argument
= NULL_TREE
;
23896 default_argument
= NULL_TREE
;
23898 if (default_argument
)
23899 STRIP_ANY_LOCATION_WRAPPER (default_argument
);
23901 /* Generate a location for the parameter, ranging from the start of the
23902 initial token to the end of the final token (using input_location for
23903 the latter, set up by cp_lexer_set_source_position_from_token when
23906 If we have a identifier, then use it for the caret location, e.g.
23908 extern int callee (int one, int (*two)(int, int), float three);
23909 ~~~~~~^~~~~~~~~~~~~~
23911 otherwise, reuse the start location for the caret location e.g.:
23913 extern int callee (int one, int (*)(int, int), float three);
23917 location_t caret_loc
= (declarator
&& declarator
->id_loc
!= UNKNOWN_LOCATION
23918 ? declarator
->id_loc
23919 : decl_spec_token_start
->location
);
23920 location_t param_loc
= make_location (caret_loc
,
23921 decl_spec_token_start
->location
,
23924 return make_parameter_declarator (&decl_specifiers
,
23928 template_parameter_pack_p
);
23931 /* Parse a default argument and return it.
23933 TEMPLATE_PARM_P is true if this is a default argument for a
23934 non-type template parameter. */
23936 cp_parser_default_argument (cp_parser
*parser
, bool template_parm_p
)
23938 tree default_argument
= NULL_TREE
;
23939 bool saved_greater_than_is_operator_p
;
23940 unsigned char saved_local_variables_forbidden_p
;
23941 bool non_constant_p
, is_direct_init
;
23943 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
23945 saved_greater_than_is_operator_p
= parser
->greater_than_is_operator_p
;
23946 parser
->greater_than_is_operator_p
= !template_parm_p
;
23947 /* Local variable names (and the `this' keyword) may not
23948 appear in a default argument. */
23949 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
23950 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
23951 /* Parse the assignment-expression. */
23952 if (template_parm_p
)
23953 push_deferring_access_checks (dk_no_deferred
);
23954 tree saved_class_ptr
= NULL_TREE
;
23955 tree saved_class_ref
= NULL_TREE
;
23956 /* The "this" pointer is not valid in a default argument. */
23959 saved_class_ptr
= current_class_ptr
;
23960 cp_function_chain
->x_current_class_ptr
= NULL_TREE
;
23961 saved_class_ref
= current_class_ref
;
23962 cp_function_chain
->x_current_class_ref
= NULL_TREE
;
23965 = cp_parser_initializer (parser
, &is_direct_init
, &non_constant_p
);
23966 /* Restore the "this" pointer. */
23969 cp_function_chain
->x_current_class_ptr
= saved_class_ptr
;
23970 cp_function_chain
->x_current_class_ref
= saved_class_ref
;
23972 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument
))
23973 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
23974 if (template_parm_p
)
23975 pop_deferring_access_checks ();
23976 parser
->greater_than_is_operator_p
= saved_greater_than_is_operator_p
;
23977 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
23979 return default_argument
;
23982 /* Parse a function-body.
23985 compound_statement */
23988 cp_parser_function_body (cp_parser
*parser
, bool in_function_try_block
)
23990 cp_parser_compound_statement (parser
, NULL
, (in_function_try_block
23991 ? BCS_TRY_BLOCK
: BCS_NORMAL
),
23995 /* Parse a ctor-initializer-opt followed by a function-body. Return
23996 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
23997 is true we are parsing a function-try-block. */
24000 cp_parser_ctor_initializer_opt_and_function_body (cp_parser
*parser
,
24001 bool in_function_try_block
)
24004 const bool check_body_p
24005 = (DECL_CONSTRUCTOR_P (current_function_decl
)
24006 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
));
24009 if (in_function_try_block
24010 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
24011 && cxx_dialect
< cxx20
)
24013 if (DECL_CONSTRUCTOR_P (current_function_decl
))
24014 pedwarn (input_location
, 0,
24015 "function-try-block body of %<constexpr%> constructor only "
24016 "available with %<-std=c++20%> or %<-std=gnu++20%>");
24018 pedwarn (input_location
, 0,
24019 "function-try-block body of %<constexpr%> function only "
24020 "available with %<-std=c++20%> or %<-std=gnu++20%>");
24023 /* Begin the function body. */
24024 body
= begin_function_body ();
24025 /* Parse the optional ctor-initializer. */
24026 cp_parser_ctor_initializer_opt (parser
);
24028 /* If we're parsing a constexpr constructor definition, we need
24029 to check that the constructor body is indeed empty. However,
24030 before we get to cp_parser_function_body lot of junk has been
24031 generated, so we can't just check that we have an empty block.
24032 Rather we take a snapshot of the outermost block, and check whether
24033 cp_parser_function_body changed its state. */
24036 list
= cur_stmt_list
;
24037 if (STATEMENT_LIST_TAIL (list
))
24038 last
= STATEMENT_LIST_TAIL (list
)->stmt
;
24040 /* Parse the function-body. */
24041 cp_parser_function_body (parser
, in_function_try_block
);
24043 check_constexpr_ctor_body (last
, list
, /*complain=*/true);
24044 /* Finish the function body. */
24045 finish_function_body (body
);
24048 /* Parse an initializer.
24051 = initializer-clause
24052 ( expression-list )
24054 Returns an expression representing the initializer. If no
24055 initializer is present, NULL_TREE is returned.
24057 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
24058 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
24059 set to TRUE if there is no initializer present. If there is an
24060 initializer, and it is not a constant-expression, *NON_CONSTANT_P
24061 is set to true; otherwise it is set to false. */
24064 cp_parser_initializer (cp_parser
* parser
, bool* is_direct_init
,
24065 bool* non_constant_p
, bool subexpression_p
)
24070 /* Peek at the next token. */
24071 token
= cp_lexer_peek_token (parser
->lexer
);
24073 /* Let our caller know whether or not this initializer was
24075 *is_direct_init
= (token
->type
!= CPP_EQ
);
24076 /* Assume that the initializer is constant. */
24077 *non_constant_p
= false;
24079 if (token
->type
== CPP_EQ
)
24081 /* Consume the `='. */
24082 cp_lexer_consume_token (parser
->lexer
);
24083 /* Parse the initializer-clause. */
24084 init
= cp_parser_initializer_clause (parser
, non_constant_p
);
24086 else if (token
->type
== CPP_OPEN_PAREN
)
24088 vec
<tree
, va_gc
> *vec
;
24089 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
24091 /*allow_expansion_p=*/true,
24094 return error_mark_node
;
24095 init
= build_tree_list_vec (vec
);
24096 release_tree_vector (vec
);
24098 else if (token
->type
== CPP_OPEN_BRACE
)
24100 cp_lexer_set_source_position (parser
->lexer
);
24101 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
24102 init
= cp_parser_braced_list (parser
, non_constant_p
);
24103 CONSTRUCTOR_IS_DIRECT_INIT (init
) = 1;
24107 /* Anything else is an error. */
24108 cp_parser_error (parser
, "expected initializer");
24109 init
= error_mark_node
;
24112 if (!subexpression_p
&& check_for_bare_parameter_packs (init
))
24113 init
= error_mark_node
;
24118 /* Parse an initializer-clause.
24120 initializer-clause:
24121 assignment-expression
24124 Returns an expression representing the initializer.
24126 If the `assignment-expression' production is used the value
24127 returned is simply a representation for the expression.
24129 Otherwise, calls cp_parser_braced_list. */
24132 cp_parser_initializer_clause (cp_parser
* parser
, bool* non_constant_p
)
24134 cp_expr initializer
;
24136 /* Assume the expression is constant. */
24137 *non_constant_p
= false;
24139 /* If it is not a `{', then we are looking at an
24140 assignment-expression. */
24141 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
24144 = cp_parser_constant_expression (parser
,
24145 /*allow_non_constant_p=*/true,
24149 initializer
= cp_parser_braced_list (parser
, non_constant_p
);
24151 return initializer
;
24154 /* Parse a brace-enclosed initializer list.
24157 { initializer-list , [opt] }
24158 { designated-initializer-list , [opt] }
24161 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
24162 the elements of the initializer-list (or NULL, if the last
24163 production is used). The TREE_TYPE for the CONSTRUCTOR will be
24164 NULL_TREE. There is no way to detect whether or not the optional
24165 trailing `,' was provided. NON_CONSTANT_P is as for
24166 cp_parser_initializer. */
24169 cp_parser_braced_list (cp_parser
* parser
, bool* non_constant_p
)
24172 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24174 /* Consume the `{' token. */
24175 matching_braces braces
;
24176 braces
.require_open (parser
);
24177 /* Create a CONSTRUCTOR to represent the braced-initializer. */
24178 initializer
= make_node (CONSTRUCTOR
);
24179 /* If it's not a `}', then there is a non-trivial initializer. */
24180 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
))
24183 /* Parse the initializer list. */
24184 CONSTRUCTOR_ELTS (initializer
)
24185 = cp_parser_initializer_list (parser
, non_constant_p
, &designated
);
24186 CONSTRUCTOR_IS_DESIGNATED_INIT (initializer
) = designated
;
24187 /* A trailing `,' token is allowed. */
24188 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
24189 cp_lexer_consume_token (parser
->lexer
);
24192 *non_constant_p
= false;
24193 /* Now, there should be a trailing `}'. */
24194 location_t finish_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24195 braces
.require_close (parser
);
24196 TREE_TYPE (initializer
) = init_list_type_node
;
24198 cp_expr
result (initializer
);
24199 /* Build a location of the form:
24202 with caret==start at the open brace, finish at the close brace. */
24203 location_t combined_loc
= make_location (start_loc
, start_loc
, finish_loc
);
24204 result
.set_location (combined_loc
);
24208 /* Consume tokens up to, and including, the next non-nested closing `]'.
24209 Returns true iff we found a closing `]'. */
24212 cp_parser_skip_to_closing_square_bracket (cp_parser
*parser
)
24214 unsigned square_depth
= 0;
24218 cp_token
* token
= cp_lexer_peek_token (parser
->lexer
);
24220 switch (token
->type
)
24222 case CPP_PRAGMA_EOL
:
24223 if (!parser
->lexer
->in_pragma
)
24227 /* If we've run out of tokens, then there is no closing `]'. */
24230 case CPP_OPEN_SQUARE
:
24234 case CPP_CLOSE_SQUARE
:
24235 if (!square_depth
--)
24237 cp_lexer_consume_token (parser
->lexer
);
24246 /* Consume the token. */
24247 cp_lexer_consume_token (parser
->lexer
);
24251 /* Return true if we are looking at an array-designator, false otherwise. */
24254 cp_parser_array_designator_p (cp_parser
*parser
)
24256 /* Consume the `['. */
24257 cp_lexer_consume_token (parser
->lexer
);
24259 cp_lexer_save_tokens (parser
->lexer
);
24261 /* Skip tokens until the next token is a closing square bracket.
24262 If we find the closing `]', and the next token is a `=', then
24263 we are looking at an array designator. */
24264 bool array_designator_p
24265 = (cp_parser_skip_to_closing_square_bracket (parser
)
24266 && cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
));
24268 /* Roll back the tokens we skipped. */
24269 cp_lexer_rollback_tokens (parser
->lexer
);
24271 return array_designator_p
;
24274 /* Parse an initializer-list.
24277 initializer-clause ... [opt]
24278 initializer-list , initializer-clause ... [opt]
24282 designated-initializer-list:
24283 designated-initializer-clause
24284 designated-initializer-list , designated-initializer-clause
24286 designated-initializer-clause:
24287 designator brace-or-equal-initializer
24295 designation initializer-clause ...[opt]
24296 initializer-list , designation initializer-clause ...[opt]
24301 [ constant-expression ] =
24303 Returns a vec of constructor_elt. The VALUE of each elt is an expression
24304 for the initializer. If the INDEX of the elt is non-NULL, it is the
24305 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
24306 as for cp_parser_initializer. Set *DESIGNATED to a boolean whether there
24307 are any designators. */
24309 static vec
<constructor_elt
, va_gc
> *
24310 cp_parser_initializer_list (cp_parser
* parser
, bool* non_constant_p
,
24313 vec
<constructor_elt
, va_gc
> *v
= NULL
;
24314 bool first_p
= true;
24315 tree first_designator
= NULL_TREE
;
24317 /* Assume all of the expressions are constant. */
24318 *non_constant_p
= false;
24320 unsigned nelts
= 0;
24321 int suppress
= suppress_location_wrappers
;
24323 /* Parse the rest of the list. */
24329 bool clause_non_constant_p
;
24330 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24332 /* Handle the C++20 syntax, '. id ='. */
24333 if ((cxx_dialect
>= cxx20
24334 || cp_parser_allow_gnu_extensions_p (parser
))
24335 && cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
24336 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_NAME
24337 && (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_EQ
24338 || (cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
24339 == CPP_OPEN_BRACE
)))
24341 if (cxx_dialect
< cxx20
)
24342 pedwarn (loc
, OPT_Wpedantic
,
24343 "C++ designated initializers only available with "
24344 "%<-std=c++20%> or %<-std=gnu++20%>");
24345 /* Consume the `.'. */
24346 cp_lexer_consume_token (parser
->lexer
);
24347 /* Consume the identifier. */
24348 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
24349 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
24350 /* Consume the `='. */
24351 cp_lexer_consume_token (parser
->lexer
);
24353 /* Also, if the next token is an identifier and the following one is a
24354 colon, we are looking at the GNU designated-initializer
24356 else if (cp_parser_allow_gnu_extensions_p (parser
)
24357 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
24358 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
24361 /* Warn the user that they are using an extension. */
24362 pedwarn (loc
, OPT_Wpedantic
,
24363 "ISO C++ does not allow GNU designated initializers");
24364 /* Consume the identifier. */
24365 designator
= cp_lexer_consume_token (parser
->lexer
)->u
.value
;
24366 /* Consume the `:'. */
24367 cp_lexer_consume_token (parser
->lexer
);
24369 /* Also handle C99 array designators, '[ const ] ='. */
24370 else if (cp_parser_allow_gnu_extensions_p (parser
)
24371 && !c_dialect_objc ()
24372 && cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
24374 /* In C++11, [ could start a lambda-introducer. */
24375 bool non_const
= false;
24377 cp_parser_parse_tentatively (parser
);
24379 if (!cp_parser_array_designator_p (parser
))
24381 cp_parser_simulate_error (parser
);
24382 designator
= NULL_TREE
;
24386 designator
= cp_parser_constant_expression (parser
, true,
24388 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
24389 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
24392 if (!cp_parser_parse_definitely (parser
))
24393 designator
= NULL_TREE
;
24395 && (!require_potential_rvalue_constant_expression
24397 designator
= NULL_TREE
;
24399 /* Warn the user that they are using an extension. */
24400 pedwarn (loc
, OPT_Wpedantic
,
24401 "ISO C++ does not allow C99 designated initializers");
24404 designator
= NULL_TREE
;
24408 first_designator
= designator
;
24411 else if (cxx_dialect
>= cxx20
24412 && first_designator
!= error_mark_node
24413 && (!first_designator
!= !designator
))
24415 error_at (loc
, "either all initializer clauses should be designated "
24416 "or none of them should be");
24417 first_designator
= error_mark_node
;
24419 else if (cxx_dialect
< cxx20
&& !first_designator
)
24420 first_designator
= designator
;
24422 /* Parse the initializer. */
24423 initializer
= cp_parser_initializer_clause (parser
,
24424 &clause_non_constant_p
);
24425 /* If any clause is non-constant, so is the entire initializer. */
24426 if (clause_non_constant_p
)
24427 *non_constant_p
= true;
24429 /* If we have an ellipsis, this is an initializer pack
24431 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
24433 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
24435 /* Consume the `...'. */
24436 cp_lexer_consume_token (parser
->lexer
);
24438 if (designator
&& cxx_dialect
>= cxx20
)
24440 "%<...%> not allowed in designated initializer list");
24442 /* Turn the initializer into an initializer expansion. */
24443 initializer
= make_pack_expansion (initializer
);
24446 /* Add it to the vector. */
24447 CONSTRUCTOR_APPEND_ELT (v
, designator
, initializer
);
24449 /* If the next token is not a comma, we have reached the end of
24451 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
24454 /* Peek at the next token. */
24455 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24456 /* If the next token is a `}', then we're still done. An
24457 initializer-clause can have a trailing `,' after the
24458 initializer-list and before the closing `}'. */
24459 if (token
->type
== CPP_CLOSE_BRACE
)
24462 /* Suppress location wrappers in a long initializer to save memory
24463 (14179). The cutoff is chosen arbitrarily. */
24464 const unsigned loc_max
= 256;
24466 if (TREE_CODE (initializer
) == CONSTRUCTOR
)
24467 /* Look one level down because it's easy. Looking deeper would require
24468 passing down a nelts pointer, and I don't think multi-level massive
24469 initializers are common enough to justify this. */
24470 incr
= CONSTRUCTOR_NELTS (initializer
);
24472 if (nelts
>= loc_max
&& (nelts
- incr
) < loc_max
)
24473 ++suppress_location_wrappers
;
24475 /* Consume the `,' token. */
24476 cp_lexer_consume_token (parser
->lexer
);
24479 /* The same identifier shall not appear in multiple designators
24480 of a designated-initializer-list. */
24481 if (first_designator
)
24484 tree designator
, val
;
24485 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
24486 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
24488 if (IDENTIFIER_MARKED (designator
))
24490 error_at (cp_expr_loc_or_input_loc (val
),
24491 "%<.%s%> designator used multiple times in "
24492 "the same initializer list",
24493 IDENTIFIER_POINTER (designator
));
24494 (*v
)[i
].index
= error_mark_node
;
24497 IDENTIFIER_MARKED (designator
) = 1;
24499 FOR_EACH_CONSTRUCTOR_ELT (v
, i
, designator
, val
)
24500 if (designator
&& TREE_CODE (designator
) == IDENTIFIER_NODE
)
24501 IDENTIFIER_MARKED (designator
) = 0;
24504 suppress_location_wrappers
= suppress
;
24506 *designated
= first_designator
!= NULL_TREE
;
24510 /* Classes [gram.class] */
24512 /* Parse a class-name.
24518 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
24519 to indicate that names looked up in dependent types should be
24520 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
24521 keyword has been used to indicate that the name that appears next
24522 is a template. TAG_TYPE indicates the explicit tag given before
24523 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
24524 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
24525 is the class being defined in a class-head. If ENUM_OK is TRUE,
24526 enum-names are also accepted.
24528 Returns the TYPE_DECL representing the class. */
24531 cp_parser_class_name (cp_parser
*parser
,
24532 bool typename_keyword_p
,
24533 bool template_keyword_p
,
24534 enum tag_types tag_type
,
24535 bool check_dependency_p
,
24537 bool is_declaration
,
24541 tree identifier
= NULL_TREE
;
24543 /* All class-names start with an identifier. */
24544 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24545 if (token
->type
!= CPP_NAME
&& token
->type
!= CPP_TEMPLATE_ID
)
24547 cp_parser_error (parser
, "expected class-name");
24548 return error_mark_node
;
24551 /* PARSER->SCOPE can be cleared when parsing the template-arguments
24552 to a template-id, so we save it here. Consider object scope too,
24553 so that make_typename_type below can use it (cp_parser_template_name
24554 considers object scope also). This may happen with code like
24556 p->template A<T>::a()
24558 where we first want to look up A<T>::a in the class of the object
24559 expression, as per [basic.lookup.classref]. */
24560 tree scope
= parser
->scope
? parser
->scope
: parser
->context
->object_type
;
24561 if (scope
== error_mark_node
)
24562 return error_mark_node
;
24564 /* Any name names a type if we're following the `typename' keyword
24565 in a qualified name where the enclosing scope is type-dependent. */
24566 const bool typename_p
= (typename_keyword_p
24568 && TYPE_P (parser
->scope
)
24569 && dependent_type_p (parser
->scope
));
24570 /* Handle the common case (an identifier, but not a template-id)
24572 if (token
->type
== CPP_NAME
24573 && !cp_parser_nth_token_starts_template_argument_list_p (parser
, 2))
24575 cp_token
*identifier_token
;
24578 /* Look for the identifier. */
24579 identifier_token
= cp_lexer_peek_token (parser
->lexer
);
24580 ambiguous_p
= identifier_token
->error_reported
;
24581 identifier
= cp_parser_identifier (parser
);
24582 /* If the next token isn't an identifier, we are certainly not
24583 looking at a class-name. */
24584 if (identifier
== error_mark_node
)
24585 decl
= error_mark_node
;
24586 /* If we know this is a type-name, there's no need to look it
24588 else if (typename_p
)
24592 tree ambiguous_decls
;
24593 /* If we already know that this lookup is ambiguous, then
24594 we've already issued an error message; there's no reason
24598 cp_parser_simulate_error (parser
);
24599 return error_mark_node
;
24601 /* If the next token is a `::', then the name must be a type
24604 [basic.lookup.qual]
24606 During the lookup for a name preceding the :: scope
24607 resolution operator, object, function, and enumerator
24608 names are ignored. */
24609 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
24610 tag_type
= scope_type
;
24611 /* Look up the name. */
24612 decl
= cp_parser_lookup_name (parser
, identifier
,
24614 /*is_template=*/false,
24615 /*is_namespace=*/false,
24616 check_dependency_p
,
24618 identifier_token
->location
);
24619 if (ambiguous_decls
)
24621 if (cp_parser_parsing_tentatively (parser
))
24622 cp_parser_simulate_error (parser
);
24623 return error_mark_node
;
24629 /* Try a template-id. */
24630 decl
= cp_parser_template_id (parser
, template_keyword_p
,
24631 check_dependency_p
,
24634 if (decl
== error_mark_node
)
24635 return error_mark_node
;
24638 decl
= cp_parser_maybe_treat_template_as_class (decl
, class_head_p
);
24640 /* If this is a typename, create a TYPENAME_TYPE. */
24642 && decl
!= error_mark_node
24643 && !is_overloaded_fn (decl
))
24645 decl
= make_typename_type (scope
, decl
, typename_type
,
24646 /*complain=*/tf_error
);
24647 if (decl
!= error_mark_node
)
24648 decl
= TYPE_NAME (decl
);
24651 decl
= strip_using_decl (decl
);
24653 /* Check to see that it is really the name of a class. */
24654 if (TREE_CODE (decl
) == TEMPLATE_ID_EXPR
24655 && identifier_p (TREE_OPERAND (decl
, 0))
24656 && cp_lexer_next_token_is (parser
->lexer
, CPP_SCOPE
))
24657 /* Situations like this:
24659 template <typename T> struct A {
24660 typename T::template X<int>::I i;
24663 are problematic. Is `T::template X<int>' a class-name? The
24664 standard does not seem to be definitive, but there is no other
24665 valid interpretation of the following `::'. Therefore, those
24666 names are considered class-names. */
24668 decl
= make_typename_type (scope
, decl
, tag_type
, tf_error
);
24669 if (decl
!= error_mark_node
)
24670 decl
= TYPE_NAME (decl
);
24672 else if (TREE_CODE (decl
) != TYPE_DECL
24673 || TREE_TYPE (decl
) == error_mark_node
24674 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl
))
24675 || (enum_ok
&& TREE_CODE (TREE_TYPE (decl
)) == ENUMERAL_TYPE
))
24676 /* In Objective-C 2.0, a classname followed by '.' starts a
24677 dot-syntax expression, and it's not a type-name. */
24678 || (c_dialect_objc ()
24679 && cp_lexer_peek_token (parser
->lexer
)->type
== CPP_DOT
24680 && objc_is_class_name (decl
)))
24681 decl
= error_mark_node
;
24683 if (decl
== error_mark_node
)
24684 cp_parser_error (parser
, "expected class-name");
24685 else if (identifier
&& !parser
->scope
)
24686 maybe_note_name_used_in_class (identifier
, decl
);
24691 /* Make sure that any member-function parameters are in scope.
24692 For instance, a function's noexcept-specifier can use the function's
24696 void fn (int p) noexcept(noexcept(p));
24699 so we need to make sure name lookup can find them. This is used
24700 when we delay parsing of the noexcept-specifier. */
24703 inject_parm_decls (tree decl
)
24705 begin_scope (sk_function_parms
, decl
);
24706 tree args
= DECL_ARGUMENTS (decl
);
24708 do_push_parm_decls (decl
, args
, /*nonparms=*/NULL
);
24710 if (args
&& is_this_parameter (args
))
24712 gcc_checking_assert (current_class_ptr
== NULL_TREE
);
24713 current_class_ptr
= NULL_TREE
;
24714 current_class_ref
= cp_build_fold_indirect_ref (args
);
24715 current_class_ptr
= args
;
24719 /* Undo the effects of inject_parm_decls. */
24722 pop_injected_parms (void)
24724 pop_bindings_and_leave_scope ();
24725 current_class_ptr
= current_class_ref
= NULL_TREE
;
24728 /* Parse a class-specifier.
24731 class-head { member-specification [opt] }
24733 Returns the TREE_TYPE representing the class. */
24736 cp_parser_class_specifier_1 (cp_parser
* parser
)
24739 tree attributes
= NULL_TREE
;
24740 bool nested_name_specifier_p
;
24741 unsigned saved_num_template_parameter_lists
;
24742 bool saved_in_function_body
;
24743 unsigned char in_statement
;
24744 bool in_switch_statement_p
;
24745 bool saved_in_unbraced_linkage_specification_p
;
24746 tree old_scope
= NULL_TREE
;
24747 tree scope
= NULL_TREE
;
24748 cp_token
*closing_brace
;
24750 push_deferring_access_checks (dk_no_deferred
);
24752 /* Parse the class-head. */
24753 type
= cp_parser_class_head (parser
,
24754 &nested_name_specifier_p
);
24755 /* If the class-head was a semantic disaster, skip the entire body
24759 cp_parser_skip_to_end_of_block_or_statement (parser
);
24760 pop_deferring_access_checks ();
24761 return error_mark_node
;
24764 /* Look for the `{'. */
24765 matching_braces braces
;
24766 if (!braces
.require_open (parser
))
24768 pop_deferring_access_checks ();
24769 return error_mark_node
;
24772 cp_ensure_no_omp_declare_simd (parser
);
24773 cp_ensure_no_oacc_routine (parser
);
24775 /* Issue an error message if type-definitions are forbidden here. */
24776 bool type_definition_ok_p
= cp_parser_check_type_definition (parser
);
24777 /* Remember that we are defining one more class. */
24778 ++parser
->num_classes_being_defined
;
24779 /* Inside the class, surrounding template-parameter-lists do not
24781 saved_num_template_parameter_lists
24782 = parser
->num_template_parameter_lists
;
24783 parser
->num_template_parameter_lists
= 0;
24784 /* We are not in a function body. */
24785 saved_in_function_body
= parser
->in_function_body
;
24786 parser
->in_function_body
= false;
24787 /* Or in a loop. */
24788 in_statement
= parser
->in_statement
;
24789 parser
->in_statement
= 0;
24790 /* Or in a switch. */
24791 in_switch_statement_p
= parser
->in_switch_statement_p
;
24792 parser
->in_switch_statement_p
= false;
24793 /* We are not immediately inside an extern "lang" block. */
24794 saved_in_unbraced_linkage_specification_p
24795 = parser
->in_unbraced_linkage_specification_p
;
24796 parser
->in_unbraced_linkage_specification_p
= false;
24798 /* Start the class. */
24799 if (nested_name_specifier_p
)
24801 scope
= CP_DECL_CONTEXT (TYPE_MAIN_DECL (type
));
24802 /* SCOPE must be a scope nested inside current scope. */
24803 if (is_nested_namespace (current_namespace
,
24804 decl_namespace_context (scope
)))
24805 old_scope
= push_inner_scope (scope
);
24807 nested_name_specifier_p
= false;
24809 type
= begin_class_definition (type
);
24811 if (type
== error_mark_node
)
24812 /* If the type is erroneous, skip the entire body of the class. */
24813 cp_parser_skip_to_closing_brace (parser
);
24815 /* Parse the member-specification. */
24816 cp_parser_member_specification_opt (parser
);
24818 /* Look for the trailing `}'. */
24819 closing_brace
= braces
.require_close (parser
);
24820 /* Look for trailing attributes to apply to this class. */
24821 if (cp_parser_allow_gnu_extensions_p (parser
))
24822 attributes
= cp_parser_gnu_attributes_opt (parser
);
24823 if (type
!= error_mark_node
)
24824 type
= finish_struct (type
, attributes
);
24825 if (nested_name_specifier_p
)
24826 pop_inner_scope (old_scope
, scope
);
24828 /* We've finished a type definition. Check for the common syntax
24829 error of forgetting a semicolon after the definition. We need to
24830 be careful, as we can't just check for not-a-semicolon and be done
24831 with it; the user might have typed:
24833 class X { } c = ...;
24834 class X { } *p = ...;
24836 and so forth. Instead, enumerate all the possible tokens that
24837 might follow this production; if we don't see one of them, then
24838 complain and silently insert the semicolon. */
24840 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
24841 bool want_semicolon
= true;
24843 if (cp_next_tokens_can_be_std_attribute_p (parser
))
24844 /* Don't try to parse c++11 attributes here. As per the
24845 grammar, that should be a task for
24846 cp_parser_decl_specifier_seq. */
24847 want_semicolon
= false;
24849 switch (token
->type
)
24852 case CPP_SEMICOLON
:
24855 case CPP_OPEN_PAREN
:
24856 case CPP_CLOSE_PAREN
:
24858 want_semicolon
= false;
24861 /* While it's legal for type qualifiers and storage class
24862 specifiers to follow type definitions in the grammar, only
24863 compiler testsuites contain code like that. Assume that if
24864 we see such code, then what we're really seeing is a case
24868 const <type> var = ...;
24873 static <type> func (...) ...
24875 i.e. the qualifier or specifier applies to the next
24876 declaration. To do so, however, we need to look ahead one
24877 more token to see if *that* token is a type specifier.
24879 This code could be improved to handle:
24882 static const <type> var = ...; */
24884 if (keyword_is_decl_specifier (token
->keyword
))
24886 cp_token
*lookahead
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
24888 /* Handling user-defined types here would be nice, but very
24891 = (lookahead
->type
== CPP_KEYWORD
24892 && keyword_begins_type_specifier (lookahead
->keyword
));
24899 /* If we don't have a type, then something is very wrong and we
24900 shouldn't try to do anything clever. Likewise for not seeing the
24902 if (closing_brace
&& TYPE_P (type
) && want_semicolon
)
24904 /* Locate the closing brace. */
24905 cp_token_position prev
24906 = cp_lexer_previous_token_position (parser
->lexer
);
24907 cp_token
*prev_token
= cp_lexer_token_at (parser
->lexer
, prev
);
24908 location_t loc
= prev_token
->location
;
24910 /* We want to suggest insertion of a ';' immediately *after* the
24911 closing brace, so, if we can, offset the location by 1 column. */
24912 location_t next_loc
= loc
;
24913 if (!linemap_location_from_macro_expansion_p (line_table
, loc
))
24914 next_loc
= linemap_position_for_loc_and_offset (line_table
, loc
, 1);
24916 rich_location
richloc (line_table
, next_loc
);
24918 /* If we successfully offset the location, suggest the fix-it. */
24919 if (next_loc
!= loc
)
24920 richloc
.add_fixit_insert_before (next_loc
, ";");
24922 if (CLASSTYPE_DECLARED_CLASS (type
))
24923 error_at (&richloc
,
24924 "expected %<;%> after class definition");
24925 else if (TREE_CODE (type
) == RECORD_TYPE
)
24926 error_at (&richloc
,
24927 "expected %<;%> after struct definition");
24928 else if (TREE_CODE (type
) == UNION_TYPE
)
24929 error_at (&richloc
,
24930 "expected %<;%> after union definition");
24932 gcc_unreachable ();
24934 /* Unget one token and smash it to look as though we encountered
24935 a semicolon in the input stream. */
24936 cp_lexer_set_token_position (parser
->lexer
, prev
);
24937 token
= cp_lexer_peek_token (parser
->lexer
);
24938 token
->type
= CPP_SEMICOLON
;
24939 token
->keyword
= RID_MAX
;
24943 /* If this class is not itself within the scope of another class,
24944 then we need to parse the bodies of all of the queued function
24945 definitions. Note that the queued functions defined in a class
24946 are not always processed immediately following the
24947 class-specifier for that class. Consider:
24950 struct B { void f() { sizeof (A); } };
24953 If `f' were processed before the processing of `A' were
24954 completed, there would be no way to compute the size of `A'.
24955 Note that the nesting we are interested in here is lexical --
24956 not the semantic nesting given by TYPE_CONTEXT. In particular,
24959 struct A { struct B; };
24960 struct A::B { void f() { } };
24962 there is no need to delay the parsing of `A::B::f'. */
24963 if (--parser
->num_classes_being_defined
== 0)
24966 tree class_type
= NULL_TREE
;
24967 tree pushed_scope
= NULL_TREE
;
24969 cp_default_arg_entry
*e
;
24970 tree save_ccp
, save_ccr
;
24972 if (!type_definition_ok_p
|| any_erroneous_template_args_p (type
))
24974 /* Skip default arguments, NSDMIs, etc, in order to improve
24975 error recovery (c++/71169, c++/71832). */
24976 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
24977 vec_safe_truncate (unparsed_nsdmis
, 0);
24978 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
24981 /* In a first pass, parse default arguments to the functions.
24982 Then, in a second pass, parse the bodies of the functions.
24983 This two-phased approach handles cases like:
24991 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args
, ix
, e
)
24994 /* If there are default arguments that have not yet been processed,
24995 take care of them now. */
24996 if (class_type
!= e
->class_type
)
24999 pop_scope (pushed_scope
);
25000 class_type
= e
->class_type
;
25001 pushed_scope
= push_scope (class_type
);
25003 /* Make sure that any template parameters are in scope. */
25004 maybe_begin_member_template_processing (decl
);
25005 /* Parse the default argument expressions. */
25006 cp_parser_late_parsing_default_args (parser
, decl
);
25007 /* Remove any template parameters from the symbol table. */
25008 maybe_end_member_template_processing ();
25010 vec_safe_truncate (unparsed_funs_with_default_args
, 0);
25012 /* If there are noexcept-specifiers that have not yet been processed,
25013 take care of them now. Do this before processing NSDMIs as they
25014 may depend on noexcept-specifiers already having been processed. */
25015 FOR_EACH_VEC_SAFE_ELT (unparsed_noexcepts
, ix
, decl
)
25017 tree ctx
= DECL_CONTEXT (decl
);
25018 if (class_type
!= ctx
)
25021 pop_scope (pushed_scope
);
25023 pushed_scope
= push_scope (class_type
);
25026 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
25027 spec
= TREE_PURPOSE (spec
);
25029 /* Make sure that any template parameters are in scope. */
25030 maybe_begin_member_template_processing (decl
);
25032 /* Make sure that any member-function parameters are in scope. */
25033 inject_parm_decls (decl
);
25035 /* 'this' is not allowed in static member functions. */
25036 unsigned char local_variables_forbidden_p
25037 = parser
->local_variables_forbidden_p
;
25038 if (DECL_THIS_STATIC (decl
))
25039 parser
->local_variables_forbidden_p
|= THIS_FORBIDDEN
;
25041 /* Now we can parse the noexcept-specifier. */
25042 spec
= cp_parser_late_noexcept_specifier (parser
, spec
);
25044 if (spec
== error_mark_node
)
25047 /* Update the fn's type directly -- it might have escaped
25048 beyond this decl :( */
25049 fixup_deferred_exception_variants (TREE_TYPE (decl
), spec
);
25051 /* Restore the state of local_variables_forbidden_p. */
25052 parser
->local_variables_forbidden_p
= local_variables_forbidden_p
;
25054 /* The finish_struct call above performed various override checking,
25055 but it skipped unparsed noexcept-specifier operands. Now that we
25056 have resolved them, check again. */
25057 noexcept_override_late_checks (type
, decl
);
25059 /* Remove any member-function parameters from the symbol table. */
25060 pop_injected_parms ();
25062 /* Remove any template parameters from the symbol table. */
25063 maybe_end_member_template_processing ();
25065 vec_safe_truncate (unparsed_noexcepts
, 0);
25067 /* Now parse any NSDMIs. */
25068 save_ccp
= current_class_ptr
;
25069 save_ccr
= current_class_ref
;
25070 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis
, ix
, decl
)
25072 if (class_type
!= DECL_CONTEXT (decl
))
25075 pop_scope (pushed_scope
);
25076 class_type
= DECL_CONTEXT (decl
);
25077 pushed_scope
= push_scope (class_type
);
25079 inject_this_parameter (class_type
, TYPE_UNQUALIFIED
);
25080 cp_parser_late_parsing_nsdmi (parser
, decl
);
25082 vec_safe_truncate (unparsed_nsdmis
, 0);
25083 current_class_ptr
= save_ccp
;
25084 current_class_ref
= save_ccr
;
25086 pop_scope (pushed_scope
);
25088 /* Now parse the body of the functions. */
25091 /* OpenMP UDRs need to be parsed before all other functions. */
25092 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
25093 if (DECL_OMP_DECLARE_REDUCTION_P (decl
))
25094 cp_parser_late_parsing_for_member (parser
, decl
);
25095 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
25096 if (!DECL_OMP_DECLARE_REDUCTION_P (decl
))
25097 cp_parser_late_parsing_for_member (parser
, decl
);
25100 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions
, ix
, decl
)
25101 cp_parser_late_parsing_for_member (parser
, decl
);
25102 vec_safe_truncate (unparsed_funs_with_definitions
, 0);
25105 /* Put back any saved access checks. */
25106 pop_deferring_access_checks ();
25108 /* Restore saved state. */
25109 parser
->in_switch_statement_p
= in_switch_statement_p
;
25110 parser
->in_statement
= in_statement
;
25111 parser
->in_function_body
= saved_in_function_body
;
25112 parser
->num_template_parameter_lists
25113 = saved_num_template_parameter_lists
;
25114 parser
->in_unbraced_linkage_specification_p
25115 = saved_in_unbraced_linkage_specification_p
;
25121 cp_parser_class_specifier (cp_parser
* parser
)
25124 timevar_push (TV_PARSE_STRUCT
);
25125 ret
= cp_parser_class_specifier_1 (parser
);
25126 timevar_pop (TV_PARSE_STRUCT
);
25130 /* Parse a class-head.
25133 class-key identifier [opt] base-clause [opt]
25134 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
25135 class-key nested-name-specifier [opt] template-id
25138 class-virt-specifier:
25142 class-key attributes identifier [opt] base-clause [opt]
25143 class-key attributes nested-name-specifier identifier base-clause [opt]
25144 class-key attributes nested-name-specifier [opt] template-id
25147 Upon return BASES is initialized to the list of base classes (or
25148 NULL, if there are none) in the same form returned by
25149 cp_parser_base_clause.
25151 Returns the TYPE of the indicated class. Sets
25152 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
25153 involving a nested-name-specifier was used, and FALSE otherwise.
25155 Returns error_mark_node if this is not a class-head.
25157 Returns NULL_TREE if the class-head is syntactically valid, but
25158 semantically invalid in a way that means we should skip the entire
25159 body of the class. */
25162 cp_parser_class_head (cp_parser
* parser
,
25163 bool* nested_name_specifier_p
)
25165 tree nested_name_specifier
;
25166 enum tag_types class_key
;
25167 tree id
= NULL_TREE
;
25168 tree type
= NULL_TREE
;
25171 cp_virt_specifiers virt_specifiers
= VIRT_SPEC_UNSPECIFIED
;
25172 bool template_id_p
= false;
25173 bool qualified_p
= false;
25174 bool invalid_nested_name_p
= false;
25175 bool invalid_explicit_specialization_p
= false;
25176 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
25177 tree pushed_scope
= NULL_TREE
;
25178 unsigned num_templates
;
25179 cp_token
*type_start_token
= NULL
, *nested_name_specifier_token_start
= NULL
;
25180 /* Assume no nested-name-specifier will be present. */
25181 *nested_name_specifier_p
= false;
25182 /* Assume no template parameter lists will be used in defining the
25185 parser
->colon_corrects_to_scope_p
= false;
25187 /* Look for the class-key. */
25188 class_key
= cp_parser_class_key (parser
);
25189 if (class_key
== none_type
)
25190 return error_mark_node
;
25192 location_t class_head_start_location
= input_location
;
25194 /* Parse the attributes. */
25195 attributes
= cp_parser_attributes_opt (parser
);
25197 /* If the next token is `::', that is invalid -- but sometimes
25198 people do try to write:
25202 Handle this gracefully by accepting the extra qualifier, and then
25203 issuing an error about it later if this really is a
25204 class-head. If it turns out just to be an elaborated type
25205 specifier, remain silent. */
25206 if (cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false))
25207 qualified_p
= true;
25209 push_deferring_access_checks (dk_no_check
);
25211 /* Determine the name of the class. Begin by looking for an
25212 optional nested-name-specifier. */
25213 nested_name_specifier_token_start
= cp_lexer_peek_token (parser
->lexer
);
25214 nested_name_specifier
25215 = cp_parser_nested_name_specifier_opt (parser
,
25216 /*typename_keyword_p=*/false,
25217 /*check_dependency_p=*/false,
25219 /*is_declaration=*/false);
25220 /* If there was a nested-name-specifier, then there *must* be an
25223 cp_token
*bad_template_keyword
= NULL
;
25225 if (nested_name_specifier
)
25227 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
25228 /* Although the grammar says `identifier', it really means
25229 `class-name' or `template-name'. You are only allowed to
25230 define a class that has already been declared with this
25233 The proposed resolution for Core Issue 180 says that wherever
25234 you see `class T::X' you should treat `X' as a type-name.
25236 It is OK to define an inaccessible class; for example:
25238 class A { class B; };
25241 We do not know if we will see a class-name, or a
25242 template-name. We look for a class-name first, in case the
25243 class-name is a template-id; if we looked for the
25244 template-name first we would stop after the template-name. */
25245 cp_parser_parse_tentatively (parser
);
25246 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25247 bad_template_keyword
= cp_lexer_consume_token (parser
->lexer
);
25248 type
= cp_parser_class_name (parser
,
25249 /*typename_keyword_p=*/false,
25250 /*template_keyword_p=*/false,
25252 /*check_dependency_p=*/false,
25253 /*class_head_p=*/true,
25254 /*is_declaration=*/false);
25255 /* If that didn't work, ignore the nested-name-specifier. */
25256 if (!cp_parser_parse_definitely (parser
))
25258 invalid_nested_name_p
= true;
25259 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
25260 id
= cp_parser_identifier (parser
);
25261 if (id
== error_mark_node
)
25264 /* If we could not find a corresponding TYPE, treat this
25265 declaration like an unqualified declaration. */
25266 if (type
== error_mark_node
)
25267 nested_name_specifier
= NULL_TREE
;
25268 /* Otherwise, count the number of templates used in TYPE and its
25269 containing scopes. */
25271 num_templates
= num_template_headers_for_class (TREE_TYPE (type
));
25273 /* Otherwise, the identifier is optional. */
25276 /* We don't know whether what comes next is a template-id,
25277 an identifier, or nothing at all. */
25278 cp_parser_parse_tentatively (parser
);
25279 /* Check for a template-id. */
25280 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
25281 id
= cp_parser_template_id (parser
,
25282 /*template_keyword_p=*/false,
25283 /*check_dependency_p=*/true,
25285 /*is_declaration=*/true);
25286 /* If that didn't work, it could still be an identifier. */
25287 if (!cp_parser_parse_definitely (parser
))
25289 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
25291 type_start_token
= cp_lexer_peek_token (parser
->lexer
);
25292 id
= cp_parser_identifier (parser
);
25299 template_id_p
= true;
25304 pop_deferring_access_checks ();
25308 cp_parser_check_for_invalid_template_id (parser
, id
,
25310 type_start_token
->location
);
25312 virt_specifiers
= cp_parser_virt_specifier_seq_opt (parser
);
25314 /* If it's not a `:' or a `{' then we can't really be looking at a
25315 class-head, since a class-head only appears as part of a
25316 class-specifier. We have to detect this situation before calling
25317 xref_tag, since that has irreversible side-effects. */
25318 if (!cp_parser_next_token_starts_class_definition_p (parser
))
25320 cp_parser_error (parser
, "expected %<{%> or %<:%>");
25321 type
= error_mark_node
;
25325 /* At this point, we're going ahead with the class-specifier, even
25326 if some other problem occurs. */
25327 cp_parser_commit_to_tentative_parse (parser
);
25328 if (virt_specifiers
& VIRT_SPEC_OVERRIDE
)
25330 cp_parser_error (parser
,
25331 "cannot specify %<override%> for a class");
25332 type
= error_mark_node
;
25335 /* Issue the error about the overly-qualified name now. */
25338 cp_parser_error (parser
,
25339 "global qualification of class name is invalid");
25340 type
= error_mark_node
;
25343 else if (invalid_nested_name_p
)
25345 cp_parser_error (parser
,
25346 "qualified name does not name a class");
25347 type
= error_mark_node
;
25350 else if (nested_name_specifier
)
25354 if (bad_template_keyword
)
25355 /* [temp.names]: in a qualified-id formed by a class-head-name, the
25356 keyword template shall not appear at the top level. */
25357 pedwarn (bad_template_keyword
->location
, OPT_Wpedantic
,
25358 "keyword %<template%> not allowed in class-head-name");
25360 /* Reject typedef-names in class heads. */
25361 if (!DECL_IMPLICIT_TYPEDEF_P (type
))
25363 error_at (type_start_token
->location
,
25364 "invalid class name in declaration of %qD",
25370 /* Figure out in what scope the declaration is being placed. */
25371 scope
= current_scope ();
25372 /* If that scope does not contain the scope in which the
25373 class was originally declared, the program is invalid. */
25374 if (scope
&& !is_ancestor (scope
, nested_name_specifier
))
25376 if (at_namespace_scope_p ())
25377 error_at (type_start_token
->location
,
25378 "declaration of %qD in namespace %qD which does not "
25380 type
, scope
, nested_name_specifier
);
25382 error_at (type_start_token
->location
,
25383 "declaration of %qD in %qD which does not enclose %qD",
25384 type
, scope
, nested_name_specifier
);
25390 A declarator-id shall not be qualified except for the
25391 definition of a ... nested class outside of its class
25392 ... [or] the definition or explicit instantiation of a
25393 class member of a namespace outside of its namespace. */
25394 if (scope
== nested_name_specifier
)
25395 permerror (nested_name_specifier_token_start
->location
,
25396 "extra qualification not allowed");
25398 /* An explicit-specialization must be preceded by "template <>". If
25399 it is not, try to recover gracefully. */
25400 if (at_namespace_scope_p ()
25401 && parser
->num_template_parameter_lists
== 0
25402 && !processing_template_parmlist
25405 /* Build a location of this form:
25406 struct typename <ARGS>
25407 ^~~~~~~~~~~~~~~~~~~~~~
25408 with caret==start at the start token, and
25409 finishing at the end of the type. */
25410 location_t reported_loc
25411 = make_location (class_head_start_location
,
25412 class_head_start_location
,
25413 get_finish (type_start_token
->location
));
25414 rich_location
richloc (line_table
, reported_loc
);
25415 richloc
.add_fixit_insert_before (class_head_start_location
,
25417 error_at (&richloc
,
25418 "an explicit specialization must be preceded by"
25419 " %<template <>%>");
25420 invalid_explicit_specialization_p
= true;
25421 /* Take the same action that would have been taken by
25422 cp_parser_explicit_specialization. */
25423 ++parser
->num_template_parameter_lists
;
25424 begin_specialization ();
25426 /* There must be no "return" statements between this point and the
25427 end of this function; set "type "to the correct return value and
25428 use "goto done;" to return. */
25429 /* Make sure that the right number of template parameters were
25431 if (!cp_parser_check_template_parameters (parser
, num_templates
,
25433 type_start_token
->location
,
25434 /*declarator=*/NULL
))
25436 /* If something went wrong, there is no point in even trying to
25437 process the class-definition. */
25442 /* Look up the type. */
25445 if (TREE_CODE (id
) == TEMPLATE_ID_EXPR
25446 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id
, 0))
25447 || TREE_CODE (TREE_OPERAND (id
, 0)) == OVERLOAD
))
25449 error_at (type_start_token
->location
,
25450 "function template %qD redeclared as a class template", id
);
25451 type
= error_mark_node
;
25455 type
= TREE_TYPE (id
);
25456 type
= maybe_process_partial_specialization (type
);
25458 /* Check the scope while we still know whether or not we had a
25459 nested-name-specifier. */
25460 if (type
!= error_mark_node
)
25461 check_unqualified_spec_or_inst (type
, type_start_token
->location
);
25463 if (nested_name_specifier
)
25464 pushed_scope
= push_scope (nested_name_specifier
);
25466 else if (nested_name_specifier
)
25472 template <typename T> struct S { struct T };
25473 template <typename T> struct S<T>::T { };
25475 we will get a TYPENAME_TYPE when processing the definition of
25476 `S::T'. We need to resolve it to the actual type before we
25477 try to define it. */
25478 if (TREE_CODE (TREE_TYPE (type
)) == TYPENAME_TYPE
)
25480 class_type
= resolve_typename_type (TREE_TYPE (type
),
25481 /*only_current_p=*/false);
25482 if (TREE_CODE (class_type
) != TYPENAME_TYPE
)
25483 type
= TYPE_NAME (class_type
);
25486 cp_parser_error (parser
, "could not resolve typename type");
25487 type
= error_mark_node
;
25491 if (maybe_process_partial_specialization (TREE_TYPE (type
))
25492 == error_mark_node
)
25498 class_type
= current_class_type
;
25499 /* Enter the scope indicated by the nested-name-specifier. */
25500 pushed_scope
= push_scope (nested_name_specifier
);
25501 /* Get the canonical version of this type. */
25502 type
= TYPE_MAIN_DECL (TREE_TYPE (type
));
25503 /* Call push_template_decl if it seems like we should be defining a
25504 template either from the template headers or the type we're
25505 defining, so that we diagnose both extra and missing headers. */
25506 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
25507 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type
)))
25508 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type
)))
25510 type
= push_template_decl (type
);
25511 if (type
== error_mark_node
)
25518 type
= TREE_TYPE (type
);
25519 *nested_name_specifier_p
= true;
25521 else /* The name is not a nested name. */
25523 /* If the class was unnamed, create a dummy name. */
25525 id
= make_anon_name ();
25526 TAG_how how
= (parser
->in_type_id_in_expr_p
25527 ? TAG_how::INNERMOST_NON_CLASS
25528 : TAG_how::CURRENT_ONLY
);
25529 type
= xref_tag (class_key
, id
, how
,
25530 parser
->num_template_parameter_lists
);
25533 /* Diagnose class/struct/union mismatches. */
25534 cp_parser_check_class_key (parser
, UNKNOWN_LOCATION
, class_key
, type
,
25537 /* Indicate whether this class was declared as a `class' or as a
25539 if (TREE_CODE (type
) == RECORD_TYPE
)
25540 CLASSTYPE_DECLARED_CLASS (type
) = class_key
== class_type
;
25542 /* If this type was already complete, and we see another definition,
25543 that's an error. Likewise if the type is already being defined:
25544 this can happen, eg, when it's defined from within an expression
25546 if (type
!= error_mark_node
25547 && (COMPLETE_TYPE_P (type
) || TYPE_BEING_DEFINED (type
)))
25549 error_at (type_start_token
->location
, "redefinition of %q#T",
25551 inform (location_of (type
), "previous definition of %q#T",
25556 else if (type
== error_mark_node
)
25561 /* Apply attributes now, before any use of the class as a template
25562 argument in its base list. */
25563 cplus_decl_attributes (&type
, attributes
, (int)ATTR_FLAG_TYPE_IN_PLACE
);
25564 fixup_attribute_variants (type
);
25567 /* Associate constraints with the type. */
25569 type
= associate_classtype_constraints (type
);
25571 /* We will have entered the scope containing the class; the names of
25572 base classes should be looked up in that context. For example:
25574 struct A { struct B {}; struct C; };
25575 struct A::C : B {};
25579 /* Get the list of base-classes, if there is one. Defer access checking
25580 until the entire list has been seen, as per [class.access.general]. */
25581 push_deferring_access_checks (dk_deferred
);
25582 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
25583 bases
= cp_parser_base_clause (parser
);
25587 /* If we're really defining a class, process the base classes.
25588 If they're invalid, fail. */
25589 if (type
&& cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
25590 xref_basetypes (type
, bases
);
25592 /* Now that all bases have been seen and attached to the class, check
25593 accessibility of the types named in the base-clause. This must be
25594 done relative to the class scope, so that we accept e.g.
25596 struct A { protected: struct B {}; };
25597 struct C : A::B, A {}; // OK: A::B is accessible via base A
25599 as per [class.access.general]. */
25602 pop_to_parent_deferring_access_checks ();
25607 /* Leave the scope given by the nested-name-specifier. We will
25608 enter the class scope itself while processing the members. */
25610 pop_scope (pushed_scope
);
25612 if (invalid_explicit_specialization_p
)
25614 end_specialization ();
25615 --parser
->num_template_parameter_lists
;
25619 DECL_SOURCE_LOCATION (TYPE_NAME (type
)) = type_start_token
->location
;
25620 if (type
&& (virt_specifiers
& VIRT_SPEC_FINAL
))
25621 CLASSTYPE_FINAL (type
) = 1;
25623 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
25627 /* Parse a class-key.
25634 Returns the kind of class-key specified, or none_type to indicate
25637 static enum tag_types
25638 cp_parser_class_key (cp_parser
* parser
)
25641 enum tag_types tag_type
;
25643 /* Look for the class-key. */
25644 token
= cp_parser_require (parser
, CPP_KEYWORD
, RT_CLASS_KEY
);
25648 /* Check to see if the TOKEN is a class-key. */
25649 tag_type
= cp_parser_token_is_class_key (token
);
25651 cp_parser_error (parser
, "expected class-key");
25655 /* Parse a type-parameter-key.
25657 type-parameter-key:
25663 cp_parser_type_parameter_key (cp_parser
* parser
)
25665 /* Look for the type-parameter-key. */
25666 enum tag_types tag_type
= none_type
;
25667 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25668 if ((tag_type
= cp_parser_token_is_type_parameter_key (token
)) != none_type
)
25670 cp_lexer_consume_token (parser
->lexer
);
25671 if (pedantic
&& tag_type
== typename_type
&& cxx_dialect
< cxx17
)
25672 /* typename is not allowed in a template template parameter
25673 by the standard until C++17. */
25674 pedwarn (token
->location
, OPT_Wpedantic
,
25675 "ISO C++ forbids typename key in template template parameter;"
25676 " use %<-std=c++17%> or %<-std=gnu++17%>");
25679 cp_parser_error (parser
, "expected %<class%> or %<typename%>");
25684 /* Parse an (optional) member-specification.
25686 member-specification:
25687 member-declaration member-specification [opt]
25688 access-specifier : member-specification [opt] */
25691 cp_parser_member_specification_opt (cp_parser
* parser
)
25698 /* Peek at the next token. */
25699 token
= cp_lexer_peek_token (parser
->lexer
);
25700 /* If it's a `}', or EOF then we've seen all the members. */
25701 if (token
->type
== CPP_CLOSE_BRACE
25702 || token
->type
== CPP_EOF
25703 || token
->type
== CPP_PRAGMA_EOL
)
25706 /* See if this token is a keyword. */
25707 keyword
= token
->keyword
;
25711 case RID_PROTECTED
:
25713 /* Consume the access-specifier. */
25714 cp_lexer_consume_token (parser
->lexer
);
25715 /* Remember which access-specifier is active. */
25716 current_access_specifier
= token
->u
.value
;
25717 /* Look for the `:'. */
25718 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
25722 /* Accept #pragmas at class scope. */
25723 if (token
->type
== CPP_PRAGMA
)
25725 cp_parser_pragma (parser
, pragma_member
, NULL
);
25729 /* Otherwise, the next construction must be a
25730 member-declaration. */
25731 cp_parser_member_declaration (parser
);
25736 /* Parse a member-declaration.
25738 member-declaration:
25739 decl-specifier-seq [opt] member-declarator-list [opt] ;
25740 function-definition ; [opt]
25741 :: [opt] nested-name-specifier template [opt] unqualified-id ;
25743 template-declaration
25746 member-declarator-list:
25748 member-declarator-list , member-declarator
25751 declarator pure-specifier [opt]
25752 declarator constant-initializer [opt]
25753 identifier [opt] : constant-expression
25757 member-declaration:
25758 __extension__ member-declaration
25761 declarator attributes [opt] pure-specifier [opt]
25762 declarator attributes [opt] constant-initializer [opt]
25763 identifier [opt] attributes [opt] : constant-expression
25767 member-declaration:
25768 static_assert-declaration */
25771 cp_parser_member_declaration (cp_parser
* parser
)
25773 cp_decl_specifier_seq decl_specifiers
;
25774 tree prefix_attributes
;
25776 int declares_class_or_enum
;
25778 cp_token
*token
= NULL
;
25779 cp_token
*decl_spec_token_start
= NULL
;
25780 cp_token
*initializer_token_start
= NULL
;
25781 int saved_pedantic
;
25782 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
25784 /* Check for the `__extension__' keyword. */
25785 if (cp_parser_extension_opt (parser
, &saved_pedantic
))
25788 cp_parser_member_declaration (parser
);
25789 /* Restore the old value of the PEDANTIC flag. */
25790 pedantic
= saved_pedantic
;
25795 /* Check for a template-declaration. */
25796 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
25798 /* An explicit specialization here is an error condition, and we
25799 expect the specialization handler to detect and report this. */
25800 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_LESS
25801 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
== CPP_GREATER
)
25802 cp_parser_explicit_specialization (parser
);
25804 cp_parser_template_declaration (parser
, /*member_p=*/true);
25808 /* Check for a template introduction. */
25809 else if (cp_parser_template_declaration_after_export (parser
, true))
25812 /* Check for a using-declaration. */
25813 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
25815 if (cxx_dialect
< cxx11
)
25816 /* Parse the using-declaration. */
25817 cp_parser_using_declaration (parser
, /*access_declaration_p=*/false);
25818 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_ENUM
))
25819 cp_parser_using_enum (parser
);
25823 bool alias_decl_expected
;
25824 cp_parser_parse_tentatively (parser
);
25825 decl
= cp_parser_alias_declaration (parser
);
25826 /* Note that if we actually see the '=' token after the
25827 identifier, cp_parser_alias_declaration commits the
25828 tentative parse. In that case, we really expect an
25829 alias-declaration. Otherwise, we expect a using
25831 alias_decl_expected
=
25832 !cp_parser_uncommitted_to_tentative_parse_p (parser
);
25833 cp_parser_parse_definitely (parser
);
25835 if (alias_decl_expected
)
25836 finish_member_declaration (decl
);
25838 cp_parser_using_declaration (parser
,
25839 /*access_declaration_p=*/false);
25844 /* Check for @defs. */
25845 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_DEFS
))
25848 tree ivar_chains
= cp_parser_objc_defs_expression (parser
);
25849 ivar
= ivar_chains
;
25853 ivar
= TREE_CHAIN (member
);
25854 TREE_CHAIN (member
) = NULL_TREE
;
25855 finish_member_declaration (member
);
25860 /* If the next token is `static_assert' we have a static assertion. */
25861 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC_ASSERT
))
25863 cp_parser_static_assert (parser
, /*member_p=*/true);
25867 parser
->colon_corrects_to_scope_p
= false;
25869 if (cp_parser_using_declaration (parser
, /*access_declaration=*/true))
25872 /* Parse the decl-specifier-seq. */
25873 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
25874 cp_parser_decl_specifier_seq (parser
,
25875 (CP_PARSER_FLAGS_OPTIONAL
25876 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
25878 &declares_class_or_enum
);
25879 /* Check for an invalid type-name. */
25880 if (!decl_specifiers
.any_type_specifiers_p
25881 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
25883 /* If there is no declarator, then the decl-specifier-seq should
25885 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
25887 /* If there was no decl-specifier-seq, and the next token is a
25888 `;', then we have something like:
25894 Each member-declaration shall declare at least one member
25895 name of the class. */
25896 if (!decl_specifiers
.any_specifiers_p
)
25898 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
25899 if (!in_system_header_at (token
->location
))
25901 gcc_rich_location
richloc (token
->location
);
25902 richloc
.add_fixit_remove ();
25903 pedwarn (&richloc
, OPT_Wpedantic
, "extra %<;%>");
25908 /* See if this declaration is a friend. */
25909 friend_p
= cp_parser_friend_p (&decl_specifiers
);
25910 /* If there were decl-specifiers, check to see if there was
25911 a class-declaration. */
25912 tree type
= check_tag_decl (&decl_specifiers
,
25913 /*explicit_type_instantiation_p=*/false);
25914 /* Nested classes have already been added to the class, but
25915 a `friend' needs to be explicitly registered. */
25918 /* If the `friend' keyword was present, the friend must
25919 be introduced with a class-key. */
25920 if (!declares_class_or_enum
&& cxx_dialect
< cxx11
)
25921 pedwarn (decl_spec_token_start
->location
, OPT_Wpedantic
,
25922 "in C++03 a class-key must be used "
25923 "when declaring a friend");
25926 template <typename T> struct A {
25927 friend struct A<T>::B;
25930 A<T>::B will be represented by a TYPENAME_TYPE, and
25931 therefore not recognized by check_tag_decl. */
25934 type
= decl_specifiers
.type
;
25935 if (type
&& TREE_CODE (type
) == TYPE_DECL
)
25936 type
= TREE_TYPE (type
);
25938 if (!type
|| !TYPE_P (type
))
25939 error_at (decl_spec_token_start
->location
,
25940 "friend declaration does not name a class or "
25943 make_friend_class (current_class_type
, type
,
25944 /*complain=*/true);
25946 /* If there is no TYPE, an error message will already have
25948 else if (!type
|| type
== error_mark_node
)
25950 /* An anonymous aggregate has to be handled specially; such
25951 a declaration really declares a data member (with a
25952 particular type), as opposed to a nested class. */
25953 else if (ANON_AGGR_TYPE_P (type
))
25956 if (decl_specifiers
.storage_class
!= sc_none
)
25957 error_at (decl_spec_token_start
->location
,
25958 "a storage class on an anonymous aggregate "
25959 "in class scope is not allowed");
25961 /* Remove constructors and such from TYPE, now that we
25962 know it is an anonymous aggregate. */
25963 fixup_anonymous_aggr (type
);
25964 /* And make the corresponding data member. */
25965 decl
= build_decl (decl_spec_token_start
->location
,
25966 FIELD_DECL
, NULL_TREE
, type
);
25967 /* Add it to the class. */
25968 finish_member_declaration (decl
);
25971 cp_parser_check_access_in_redeclaration
25973 decl_spec_token_start
->location
);
25978 bool assume_semicolon
= false;
25980 /* Clear attributes from the decl_specifiers but keep them
25981 around as prefix attributes that apply them to the entity
25983 prefix_attributes
= decl_specifiers
.attributes
;
25984 decl_specifiers
.attributes
= NULL_TREE
;
25986 /* See if these declarations will be friends. */
25987 friend_p
= cp_parser_friend_p (&decl_specifiers
);
25989 /* Keep going until we hit the `;' at the end of the
25991 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
25993 tree attributes
= NULL_TREE
;
25994 tree first_attribute
;
25996 bool named_bitfld
= false;
25998 /* Peek at the next token. */
25999 token
= cp_lexer_peek_token (parser
->lexer
);
26001 /* The following code wants to know early if it is a bit-field
26002 or some other declaration. Attributes can appear before
26003 the `:' token. Skip over them without consuming any tokens
26004 to peek if they are followed by `:'. */
26005 if (cp_next_tokens_can_be_attribute_p (parser
)
26006 || (token
->type
== CPP_NAME
26007 && cp_nth_tokens_can_be_attribute_p (parser
, 2)
26008 && (named_bitfld
= true)))
26011 = cp_parser_skip_attributes_opt (parser
, 1 + named_bitfld
);
26012 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
26015 /* Check for a bitfield declaration. */
26016 if (token
->type
== CPP_COLON
26017 || (token
->type
== CPP_NAME
26018 && token
== cp_lexer_peek_token (parser
->lexer
)
26019 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
)
26020 && (named_bitfld
= true)))
26024 tree late_attributes
= NULL_TREE
;
26025 location_t id_location
26026 = cp_lexer_peek_token (parser
->lexer
)->location
;
26029 identifier
= cp_parser_identifier (parser
);
26031 identifier
= NULL_TREE
;
26033 /* Look for attributes that apply to the bitfield. */
26034 attributes
= cp_parser_attributes_opt (parser
);
26036 /* Consume the `:' token. */
26037 cp_lexer_consume_token (parser
->lexer
);
26039 /* Get the width of the bitfield. */
26040 width
= cp_parser_constant_expression (parser
, false, NULL
,
26041 cxx_dialect
>= cxx11
);
26043 /* In C++20 and as extension for C++11 and above we allow
26044 default member initializers for bit-fields. */
26045 initializer
= NULL_TREE
;
26046 if (cxx_dialect
>= cxx11
26047 && (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
)
26048 || cp_lexer_next_token_is (parser
->lexer
,
26052 = cp_lexer_peek_token (parser
->lexer
)->location
;
26053 if (cxx_dialect
< cxx20
26054 && identifier
!= NULL_TREE
)
26056 "default member initializers for bit-fields "
26057 "only available with %<-std=c++20%> or "
26058 "%<-std=gnu++20%>");
26060 initializer
= cp_parser_save_nsdmi (parser
);
26061 if (identifier
== NULL_TREE
)
26063 error_at (loc
, "default member initializer for "
26064 "unnamed bit-field");
26065 initializer
= NULL_TREE
;
26070 /* Look for attributes that apply to the bitfield after
26071 the `:' token and width. This is where GCC used to
26072 parse attributes in the past, pedwarn if there is
26073 a std attribute. */
26074 if (cp_next_tokens_can_be_std_attribute_p (parser
))
26075 pedwarn (input_location
, OPT_Wpedantic
,
26076 "ISO C++ allows bit-field attributes only "
26077 "before the %<:%> token");
26079 late_attributes
= cp_parser_attributes_opt (parser
);
26082 attributes
= attr_chainon (attributes
, late_attributes
);
26084 /* Remember which attributes are prefix attributes and
26086 first_attribute
= attributes
;
26087 /* Combine the attributes. */
26088 attributes
= attr_chainon (prefix_attributes
, attributes
);
26090 /* Create the bitfield declaration. */
26091 decl
= grokbitfield (identifier
26092 ? make_id_declarator (NULL_TREE
,
26098 width
, initializer
,
26103 cp_declarator
*declarator
;
26104 tree asm_specification
;
26105 int ctor_dtor_or_conv_p
;
26106 bool static_p
= (decl_specifiers
.storage_class
== sc_static
);
26107 cp_parser_flags flags
= CP_PARSER_FLAGS_TYPENAME_OPTIONAL
;
26108 /* We can't delay parsing for friends,
26109 alias-declarations, and typedefs, even though the
26110 standard seems to require it. */
26112 && !decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
26113 flags
|= CP_PARSER_FLAGS_DELAY_NOEXCEPT
;
26115 /* Parse the declarator. */
26117 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
26119 &ctor_dtor_or_conv_p
,
26120 /*parenthesized_p=*/NULL
,
26122 friend_p
, static_p
);
26124 /* If something went wrong parsing the declarator, make sure
26125 that we at least consume some tokens. */
26126 if (declarator
== cp_error_declarator
)
26128 /* Skip to the end of the statement. */
26129 cp_parser_skip_to_end_of_statement (parser
);
26130 /* If the next token is not a semicolon, that is
26131 probably because we just skipped over the body of
26132 a function. So, we consume a semicolon if
26133 present, but do not issue an error message if it
26135 if (cp_lexer_next_token_is (parser
->lexer
,
26137 cp_lexer_consume_token (parser
->lexer
);
26141 if (declares_class_or_enum
& 2)
26142 cp_parser_check_for_definition_in_return_type
26143 (declarator
, decl_specifiers
.type
,
26144 decl_specifiers
.locations
[ds_type_spec
]);
26146 /* Look for an asm-specification. */
26147 asm_specification
= cp_parser_asm_specification_opt (parser
);
26148 /* Look for attributes that apply to the declaration. */
26149 attributes
= cp_parser_attributes_opt (parser
);
26150 /* Remember which attributes are prefix attributes and
26152 first_attribute
= attributes
;
26153 /* Combine the attributes. */
26154 attributes
= attr_chainon (prefix_attributes
, attributes
);
26156 /* If it's an `=', then we have a constant-initializer or a
26157 pure-specifier. It is not correct to parse the
26158 initializer before registering the member declaration
26159 since the member declaration should be in scope while
26160 its initializer is processed. However, the rest of the
26161 front end does not yet provide an interface that allows
26162 us to handle this correctly. */
26163 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
26167 A pure-specifier shall be used only in the declaration of
26168 a virtual function.
26170 A member-declarator can contain a constant-initializer
26171 only if it declares a static member of integral or
26174 Therefore, if the DECLARATOR is for a function, we look
26175 for a pure-specifier; otherwise, we look for a
26176 constant-initializer. When we call `grokfield', it will
26177 perform more stringent semantics checks. */
26178 initializer_token_start
= cp_lexer_peek_token (parser
->lexer
);
26179 if (function_declarator_p (declarator
)
26180 || (decl_specifiers
.type
26181 && TREE_CODE (decl_specifiers
.type
) == TYPE_DECL
26182 && declarator
->kind
== cdk_id
26183 && (TREE_CODE (TREE_TYPE (decl_specifiers
.type
))
26184 == FUNCTION_TYPE
)))
26185 initializer
= cp_parser_pure_specifier (parser
);
26186 else if (decl_specifiers
.storage_class
!= sc_static
)
26187 initializer
= cp_parser_save_nsdmi (parser
);
26188 else if (cxx_dialect
>= cxx11
)
26191 /* Don't require a constant rvalue in C++11, since we
26192 might want a reference constant. We'll enforce
26193 constancy later. */
26194 cp_lexer_consume_token (parser
->lexer
);
26195 /* Parse the initializer. */
26196 initializer
= cp_parser_initializer_clause (parser
,
26200 /* Parse the initializer. */
26201 initializer
= cp_parser_constant_initializer (parser
);
26203 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
)
26204 && !function_declarator_p (declarator
))
26207 if (decl_specifiers
.storage_class
!= sc_static
)
26208 initializer
= cp_parser_save_nsdmi (parser
);
26210 initializer
= cp_parser_initializer (parser
, &x
, &x
);
26212 /* Detect invalid bit-field cases such as
26218 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
)
26219 /* If there were no type specifiers, it was a
26221 && decl_specifiers
.any_type_specifiers_p
)
26223 /* This is called for a decent diagnostic only. */
26224 tree d
= grokdeclarator (declarator
, &decl_specifiers
,
26225 BITFIELD
, /*initialized=*/false,
26227 if (!error_operand_p (d
))
26228 error_at (DECL_SOURCE_LOCATION (d
),
26229 "bit-field %qD has non-integral type %qT",
26231 cp_parser_skip_to_end_of_statement (parser
);
26232 /* Avoid "extra ;" pedwarns. */
26233 if (cp_lexer_next_token_is (parser
->lexer
,
26235 cp_lexer_consume_token (parser
->lexer
);
26238 /* Otherwise, there is no initializer. */
26240 initializer
= NULL_TREE
;
26242 /* See if we are probably looking at a function
26243 definition. We are certainly not looking at a
26244 member-declarator. Calling `grokfield' has
26245 side-effects, so we must not do it unless we are sure
26246 that we are looking at a member-declarator. */
26247 if (cp_parser_token_starts_function_definition_p
26248 (cp_lexer_peek_token (parser
->lexer
)))
26250 /* The grammar does not allow a pure-specifier to be
26251 used when a member function is defined. (It is
26252 possible that this fact is an oversight in the
26253 standard, since a pure function may be defined
26254 outside of the class-specifier. */
26255 if (initializer
&& initializer_token_start
)
26256 error_at (initializer_token_start
->location
,
26257 "pure-specifier on function-definition");
26258 decl
= cp_parser_save_member_function_body (parser
,
26262 if (parser
->fully_implicit_function_template_p
)
26263 decl
= finish_fully_implicit_template (parser
, decl
);
26264 /* If the member was not a friend, declare it here. */
26266 finish_member_declaration (decl
);
26267 /* Peek at the next token. */
26268 token
= cp_lexer_peek_token (parser
->lexer
);
26269 /* If the next token is a semicolon, consume it. */
26270 if (token
->type
== CPP_SEMICOLON
)
26272 location_t semicolon_loc
26273 = cp_lexer_consume_token (parser
->lexer
)->location
;
26274 gcc_rich_location
richloc (semicolon_loc
);
26275 richloc
.add_fixit_remove ();
26276 warning_at (&richloc
, OPT_Wextra_semi
,
26277 "extra %<;%> after in-class "
26278 "function definition");
26283 if (declarator
->kind
== cdk_function
)
26284 declarator
->id_loc
= token
->location
;
26285 /* Create the declaration. */
26286 decl
= grokfield (declarator
, &decl_specifiers
,
26287 initializer
, /*init_const_expr_p=*/true,
26288 asm_specification
, attributes
);
26289 if (parser
->fully_implicit_function_template_p
)
26292 finish_fully_implicit_template (parser
, 0);
26294 decl
= finish_fully_implicit_template (parser
, decl
);
26298 cp_finalize_omp_declare_simd (parser
, decl
);
26299 cp_finalize_oacc_routine (parser
, decl
, false);
26301 /* Reset PREFIX_ATTRIBUTES. */
26302 if (attributes
!= error_mark_node
)
26304 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
26305 attributes
= TREE_CHAIN (attributes
);
26307 TREE_CHAIN (attributes
) = NULL_TREE
;
26310 /* If there is any qualification still in effect, clear it
26311 now; we will be starting fresh with the next declarator. */
26312 parser
->scope
= NULL_TREE
;
26313 parser
->qualifying_scope
= NULL_TREE
;
26314 parser
->object_scope
= NULL_TREE
;
26315 /* If it's a `,', then there are more declarators. */
26316 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
26318 cp_lexer_consume_token (parser
->lexer
);
26319 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
26321 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
26322 gcc_rich_location
richloc (token
->location
);
26323 richloc
.add_fixit_remove ();
26324 error_at (&richloc
, "stray %<,%> at end of "
26325 "member declaration");
26328 /* If the next token isn't a `;', then we have a parse error. */
26329 else if (cp_lexer_next_token_is_not (parser
->lexer
,
26332 /* The next token might be a ways away from where the
26333 actual semicolon is missing. Find the previous token
26334 and use that for our error position. */
26335 cp_token
*token
= cp_lexer_previous_token (parser
->lexer
);
26336 gcc_rich_location
richloc (token
->location
);
26337 richloc
.add_fixit_insert_after (";");
26338 error_at (&richloc
, "expected %<;%> at end of "
26339 "member declaration");
26341 /* Assume that the user meant to provide a semicolon. If
26342 we were to cp_parser_skip_to_end_of_statement, we might
26343 skip to a semicolon inside a member function definition
26344 and issue nonsensical error messages. */
26345 assume_semicolon
= true;
26350 /* Add DECL to the list of members. */
26352 /* Explicitly include, eg, NSDMIs, for better error
26353 recovery (c++/58650). */
26354 || !DECL_DECLARES_FUNCTION_P (decl
))
26355 finish_member_declaration (decl
);
26357 if (TREE_CODE (decl
) == FUNCTION_DECL
)
26358 cp_parser_save_default_args (parser
, decl
);
26359 else if (TREE_CODE (decl
) == FIELD_DECL
26360 && DECL_INITIAL (decl
))
26361 /* Add DECL to the queue of NSDMI to be parsed later. */
26362 vec_safe_push (unparsed_nsdmis
, decl
);
26365 if (assume_semicolon
)
26370 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
26372 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
26375 /* Parse a pure-specifier.
26380 Returns INTEGER_ZERO_NODE if a pure specifier is found.
26381 Otherwise, ERROR_MARK_NODE is returned. */
26384 cp_parser_pure_specifier (cp_parser
* parser
)
26388 /* Look for the `=' token. */
26389 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26390 return error_mark_node
;
26391 /* Look for the `0' token. */
26392 token
= cp_lexer_peek_token (parser
->lexer
);
26394 if (token
->type
== CPP_EOF
26395 || token
->type
== CPP_PRAGMA_EOL
)
26396 return error_mark_node
;
26398 cp_lexer_consume_token (parser
->lexer
);
26400 /* Accept = default or = delete in c++0x mode. */
26401 if (token
->keyword
== RID_DEFAULT
26402 || token
->keyword
== RID_DELETE
)
26404 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED
);
26405 return token
->u
.value
;
26408 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
26409 if (token
->type
!= CPP_NUMBER
|| !(token
->flags
& PURE_ZERO
))
26411 cp_parser_error (parser
,
26412 "invalid pure specifier (only %<= 0%> is allowed)");
26413 cp_parser_skip_to_end_of_statement (parser
);
26414 return error_mark_node
;
26416 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
26418 error_at (token
->location
, "templates may not be %<virtual%>");
26419 return error_mark_node
;
26422 return integer_zero_node
;
26425 /* Parse a constant-initializer.
26427 constant-initializer:
26428 = constant-expression
26430 Returns a representation of the constant-expression. */
26433 cp_parser_constant_initializer (cp_parser
* parser
)
26435 /* Look for the `=' token. */
26436 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
26437 return error_mark_node
;
26439 /* It is invalid to write:
26441 struct S { static const int i = { 7 }; };
26444 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
26446 cp_parser_error (parser
,
26447 "a brace-enclosed initializer is not allowed here");
26448 /* Consume the opening brace. */
26449 matching_braces braces
;
26450 braces
.consume_open (parser
);
26451 /* Skip the initializer. */
26452 cp_parser_skip_to_closing_brace (parser
);
26453 /* Look for the trailing `}'. */
26454 braces
.require_close (parser
);
26456 return error_mark_node
;
26459 return cp_parser_constant_expression (parser
);
26462 /* Derived classes [gram.class.derived] */
26464 /* Parse a base-clause.
26467 : base-specifier-list
26469 base-specifier-list:
26470 base-specifier ... [opt]
26471 base-specifier-list , base-specifier ... [opt]
26473 Returns a TREE_LIST representing the base-classes, in the order in
26474 which they were declared. The representation of each node is as
26475 described by cp_parser_base_specifier.
26477 In the case that no bases are specified, this function will return
26478 NULL_TREE, not ERROR_MARK_NODE. */
26481 cp_parser_base_clause (cp_parser
* parser
)
26483 tree bases
= NULL_TREE
;
26485 /* Look for the `:' that begins the list. */
26486 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
26488 /* Scan the base-specifier-list. */
26493 bool pack_expansion_p
= false;
26495 /* Look for the base-specifier. */
26496 base
= cp_parser_base_specifier (parser
);
26497 /* Look for the (optional) ellipsis. */
26498 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26500 /* Consume the `...'. */
26501 cp_lexer_consume_token (parser
->lexer
);
26503 pack_expansion_p
= true;
26506 /* Add BASE to the front of the list. */
26507 if (base
&& base
!= error_mark_node
)
26509 if (pack_expansion_p
)
26510 /* Make this a pack expansion type. */
26511 TREE_VALUE (base
) = make_pack_expansion (TREE_VALUE (base
));
26513 if (!check_for_bare_parameter_packs (TREE_VALUE (base
)))
26515 TREE_CHAIN (base
) = bases
;
26519 /* Peek at the next token. */
26520 token
= cp_lexer_peek_token (parser
->lexer
);
26521 /* If it's not a comma, then the list is complete. */
26522 if (token
->type
!= CPP_COMMA
)
26524 /* Consume the `,'. */
26525 cp_lexer_consume_token (parser
->lexer
);
26528 /* PARSER->SCOPE may still be non-NULL at this point, if the last
26529 base class had a qualified name. However, the next name that
26530 appears is certainly not qualified. */
26531 parser
->scope
= NULL_TREE
;
26532 parser
->qualifying_scope
= NULL_TREE
;
26533 parser
->object_scope
= NULL_TREE
;
26535 return nreverse (bases
);
26538 /* Parse a base-specifier.
26541 :: [opt] nested-name-specifier [opt] class-name
26542 virtual access-specifier [opt] :: [opt] nested-name-specifier
26544 access-specifier virtual [opt] :: [opt] nested-name-specifier
26547 Returns a TREE_LIST. The TREE_PURPOSE will be one of
26548 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
26549 indicate the specifiers provided. The TREE_VALUE will be a TYPE
26550 (or the ERROR_MARK_NODE) indicating the type that was specified. */
26553 cp_parser_base_specifier (cp_parser
* parser
)
26557 bool virtual_p
= false;
26558 bool duplicate_virtual_error_issued_p
= false;
26559 bool duplicate_access_error_issued_p
= false;
26560 bool class_scope_p
, template_p
;
26561 tree access
= access_default_node
;
26564 /* Process the optional `virtual' and `access-specifier'. */
26567 /* Peek at the next token. */
26568 token
= cp_lexer_peek_token (parser
->lexer
);
26569 /* Process `virtual'. */
26570 switch (token
->keyword
)
26573 /* If `virtual' appears more than once, issue an error. */
26574 if (virtual_p
&& !duplicate_virtual_error_issued_p
)
26576 cp_parser_error (parser
,
26577 "%<virtual%> specified more than once in base-specifier");
26578 duplicate_virtual_error_issued_p
= true;
26583 /* Consume the `virtual' token. */
26584 cp_lexer_consume_token (parser
->lexer
);
26589 case RID_PROTECTED
:
26591 /* If more than one access specifier appears, issue an
26593 if (access
!= access_default_node
26594 && !duplicate_access_error_issued_p
)
26596 cp_parser_error (parser
,
26597 "more than one access specifier in base-specifier");
26598 duplicate_access_error_issued_p
= true;
26601 access
= ridpointers
[(int) token
->keyword
];
26603 /* Consume the access-specifier. */
26604 cp_lexer_consume_token (parser
->lexer
);
26613 /* It is not uncommon to see programs mechanically, erroneously, use
26614 the 'typename' keyword to denote (dependent) qualified types
26615 as base classes. */
26616 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
26618 token
= cp_lexer_peek_token (parser
->lexer
);
26619 if (!processing_template_decl
)
26620 error_at (token
->location
,
26621 "keyword %<typename%> not allowed outside of templates");
26623 error_at (token
->location
,
26624 "keyword %<typename%> not allowed in this context "
26625 "(the base class is implicitly a type)");
26626 cp_lexer_consume_token (parser
->lexer
);
26629 /* Look for the optional `::' operator. */
26630 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
26631 /* Look for the nested-name-specifier. The simplest way to
26636 The keyword `typename' is not permitted in a base-specifier or
26637 mem-initializer; in these contexts a qualified name that
26638 depends on a template-parameter is implicitly assumed to be a
26641 is to pretend that we have seen the `typename' keyword at this
26643 cp_parser_nested_name_specifier_opt (parser
,
26644 /*typename_keyword_p=*/true,
26645 /*check_dependency_p=*/true,
26647 /*is_declaration=*/true);
26648 /* If the base class is given by a qualified name, assume that names
26649 we see are type names or templates, as appropriate. */
26650 class_scope_p
= (parser
->scope
&& TYPE_P (parser
->scope
));
26651 template_p
= class_scope_p
&& cp_parser_optional_template_keyword (parser
);
26654 && cp_lexer_next_token_is_decltype (parser
->lexer
))
26655 /* DR 950 allows decltype as a base-specifier. */
26656 type
= cp_parser_decltype (parser
);
26659 /* Otherwise, look for the class-name. */
26660 type
= cp_parser_class_name (parser
,
26664 /*check_dependency_p=*/true,
26665 /*class_head_p=*/false,
26666 /*is_declaration=*/true);
26667 type
= TREE_TYPE (type
);
26670 if (type
== error_mark_node
)
26671 return error_mark_node
;
26673 return finish_base_specifier (type
, access
, virtual_p
);
26676 /* Exception handling [gram.exception] */
26678 /* Save the tokens that make up the noexcept-specifier for a member-function.
26679 Returns a DEFERRED_PARSE. */
26682 cp_parser_save_noexcept (cp_parser
*parser
)
26684 cp_token
*first
= parser
->lexer
->next_token
;
26685 /* We want everything up to, including, the final ')'. */
26686 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0);
26687 cp_token
*last
= parser
->lexer
->next_token
;
26689 /* As with default arguments and NSDMIs, make use of DEFERRED_PARSE
26690 to carry the information we will need. */
26691 tree expr
= make_node (DEFERRED_PARSE
);
26692 /* Save away the noexcept-specifier; we will process it when the
26693 class is complete. */
26694 DEFPARSE_TOKENS (expr
) = cp_token_cache_new (first
, last
);
26695 expr
= build_tree_list (expr
, NULL_TREE
);
26699 /* Used for late processing of noexcept-specifiers of member-functions.
26700 DEFAULT_ARG is the unparsed operand of a noexcept-specifier which
26701 we saved for later; parse it now. DECL is the declaration of the
26702 member function. */
26705 cp_parser_late_noexcept_specifier (cp_parser
*parser
, tree default_arg
)
26707 /* Make sure we've gotten something that hasn't been parsed yet. */
26708 gcc_assert (TREE_CODE (default_arg
) == DEFERRED_PARSE
);
26710 push_unparsed_function_queues (parser
);
26712 /* Push the saved tokens for the noexcept-specifier onto the parser's
26714 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_arg
);
26715 cp_parser_push_lexer_for_tokens (parser
, tokens
);
26717 /* Parse the cached noexcept-specifier. */
26719 = cp_parser_noexcept_specification_opt (parser
,
26720 CP_PARSER_FLAGS_NONE
,
26721 /*require_constexpr=*/true,
26722 /*consumed_expr=*/NULL
,
26723 /*return_cond=*/false);
26725 /* Revert to the main lexer. */
26726 cp_parser_pop_lexer (parser
);
26728 /* Restore the queue. */
26729 pop_unparsed_function_queues (parser
);
26731 /* And we're done. */
26735 /* Perform late checking of overriding function with respect to their
26736 noexcept-specifiers. TYPE is the class and FNDECL is the function
26737 that potentially overrides some virtual function with the same
26741 noexcept_override_late_checks (tree type
, tree fndecl
)
26743 tree binfo
= TYPE_BINFO (type
);
26746 if (DECL_STATIC_FUNCTION_P (fndecl
))
26749 for (int i
= 0; BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); ++i
)
26751 tree basetype
= BINFO_TYPE (base_binfo
);
26753 if (!TYPE_POLYMORPHIC_P (basetype
))
26756 tree fn
= look_for_overrides_here (basetype
, fndecl
);
26758 maybe_check_overriding_exception_spec (fndecl
, fn
);
26762 /* Parse an (optional) noexcept-specification.
26764 noexcept-specification:
26765 noexcept ( constant-expression ) [opt]
26767 If no noexcept-specification is present, returns NULL_TREE.
26768 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
26769 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
26770 there are no parentheses. CONSUMED_EXPR will be set accordingly.
26771 Otherwise, returns a noexcept specification unless RETURN_COND is true,
26772 in which case a boolean condition is returned instead. The parser flags
26773 FLAGS is used to control parsing. QUALS are qualifiers indicating whether
26774 the (member) function is `const'. */
26777 cp_parser_noexcept_specification_opt (cp_parser
* parser
,
26778 cp_parser_flags flags
,
26779 bool require_constexpr
,
26780 bool* consumed_expr
,
26784 const char *saved_message
;
26786 /* Peek at the next token. */
26787 token
= cp_lexer_peek_token (parser
->lexer
);
26789 /* Is it a noexcept-specification? */
26790 if (cp_parser_is_keyword (token
, RID_NOEXCEPT
))
26794 /* [class.mem]/6 says that a noexcept-specifer (within the
26795 member-specification of the class) is a complete-class context of
26796 a class. So, if the noexcept-specifier has the optional expression,
26797 just save the tokens, and reparse this after we're done with the
26800 if ((flags
& CP_PARSER_FLAGS_DELAY_NOEXCEPT
)
26801 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
)
26802 /* No need to delay parsing for a number literal or true/false. */
26803 && !((cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NUMBER
)
26804 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
26805 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_PAREN
))
26806 && at_class_scope_p ()
26807 && TYPE_BEING_DEFINED (current_class_type
)
26808 && !LAMBDA_TYPE_P (current_class_type
))
26809 return cp_parser_save_noexcept (parser
);
26811 cp_lexer_consume_token (parser
->lexer
);
26813 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
26815 matching_parens parens
;
26816 parens
.consume_open (parser
);
26818 if (require_constexpr
)
26820 /* Types may not be defined in an exception-specification. */
26821 saved_message
= parser
->type_definition_forbidden_message
;
26822 parser
->type_definition_forbidden_message
26823 = G_("types may not be defined in an exception-specification");
26825 bool non_constant_p
;
26827 = cp_parser_constant_expression (parser
,
26828 /*allow_non_constant=*/true,
26831 && !require_potential_rvalue_constant_expression (expr
))
26834 return_cond
= true;
26837 /* Restore the saved message. */
26838 parser
->type_definition_forbidden_message
= saved_message
;
26842 expr
= cp_parser_expression (parser
);
26843 *consumed_expr
= true;
26846 parens
.require_close (parser
);
26850 expr
= boolean_true_node
;
26851 if (!require_constexpr
)
26852 *consumed_expr
= false;
26855 /* We cannot build a noexcept-spec right away because this will check
26856 that expr is a constexpr. */
26858 return build_noexcept_spec (expr
, tf_warning_or_error
);
26866 /* Parse an (optional) exception-specification.
26868 exception-specification:
26869 throw ( type-id-list [opt] )
26871 Returns a TREE_LIST representing the exception-specification. The
26872 TREE_VALUE of each node is a type. The parser flags FLAGS is used to
26873 control parsing. QUALS are qualifiers indicating whether the (member)
26874 function is `const'. */
26877 cp_parser_exception_specification_opt (cp_parser
* parser
,
26878 cp_parser_flags flags
)
26882 const char *saved_message
;
26884 /* Peek at the next token. */
26885 token
= cp_lexer_peek_token (parser
->lexer
);
26887 /* Is it a noexcept-specification? */
26889 = cp_parser_noexcept_specification_opt (parser
, flags
,
26890 /*require_constexpr=*/true,
26891 /*consumed_expr=*/NULL
,
26892 /*return_cond=*/false);
26893 if (type_id_list
!= NULL_TREE
)
26894 return type_id_list
;
26896 /* If it's not `throw', then there's no exception-specification. */
26897 if (!cp_parser_is_keyword (token
, RID_THROW
))
26900 location_t loc
= token
->location
;
26902 /* Consume the `throw'. */
26903 cp_lexer_consume_token (parser
->lexer
);
26905 /* Look for the `('. */
26906 matching_parens parens
;
26907 parens
.require_open (parser
);
26909 /* Peek at the next token. */
26910 token
= cp_lexer_peek_token (parser
->lexer
);
26911 /* If it's not a `)', then there is a type-id-list. */
26912 if (token
->type
!= CPP_CLOSE_PAREN
)
26914 /* Types may not be defined in an exception-specification. */
26915 saved_message
= parser
->type_definition_forbidden_message
;
26916 parser
->type_definition_forbidden_message
26917 = G_("types may not be defined in an exception-specification");
26918 /* Parse the type-id-list. */
26919 type_id_list
= cp_parser_type_id_list (parser
);
26920 /* Restore the saved message. */
26921 parser
->type_definition_forbidden_message
= saved_message
;
26923 if (cxx_dialect
>= cxx17
)
26925 error_at (loc
, "ISO C++17 does not allow dynamic exception "
26927 type_id_list
= NULL_TREE
;
26929 else if (cxx_dialect
>= cxx11
)
26930 warning_at (loc
, OPT_Wdeprecated
,
26931 "dynamic exception specifications are deprecated in "
26934 /* In C++17, throw() is equivalent to noexcept (true). throw()
26935 is deprecated in C++11 and above as well, but is still widely used,
26936 so don't warn about it yet. */
26937 else if (cxx_dialect
>= cxx17
)
26938 type_id_list
= noexcept_true_spec
;
26940 type_id_list
= empty_except_spec
;
26942 /* Look for the `)'. */
26943 parens
.require_close (parser
);
26945 return type_id_list
;
26948 /* Parse an (optional) type-id-list.
26952 type-id-list , type-id ... [opt]
26954 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
26955 in the order that the types were presented. */
26958 cp_parser_type_id_list (cp_parser
* parser
)
26960 tree types
= NULL_TREE
;
26967 token
= cp_lexer_peek_token (parser
->lexer
);
26969 /* Get the next type-id. */
26970 type
= cp_parser_type_id (parser
);
26971 /* Check for invalid 'auto'. */
26972 if (flag_concepts
&& type_uses_auto (type
))
26974 error_at (token
->location
,
26975 "invalid use of %<auto%> in exception-specification");
26976 type
= error_mark_node
;
26978 /* Parse the optional ellipsis. */
26979 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
26981 /* Consume the `...'. */
26982 cp_lexer_consume_token (parser
->lexer
);
26984 /* Turn the type into a pack expansion expression. */
26985 type
= make_pack_expansion (type
);
26987 /* Add it to the list. */
26988 types
= add_exception_specifier (types
, type
, /*complain=*/1);
26989 /* Peek at the next token. */
26990 token
= cp_lexer_peek_token (parser
->lexer
);
26991 /* If it is not a `,', we are done. */
26992 if (token
->type
!= CPP_COMMA
)
26994 /* Consume the `,'. */
26995 cp_lexer_consume_token (parser
->lexer
);
26998 return nreverse (types
);
27001 /* Parse a try-block.
27004 try compound-statement handler-seq */
27007 cp_parser_try_block (cp_parser
* parser
)
27011 cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
);
27012 if (parser
->in_function_body
27013 && DECL_DECLARED_CONSTEXPR_P (current_function_decl
)
27014 && cxx_dialect
< cxx20
)
27015 pedwarn (input_location
, 0,
27016 "%<try%> in %<constexpr%> function only "
27017 "available with %<-std=c++20%> or %<-std=gnu++20%>");
27019 try_block
= begin_try_block ();
27020 cp_parser_compound_statement (parser
, NULL
, BCS_TRY_BLOCK
, false);
27021 finish_try_block (try_block
);
27022 cp_parser_handler_seq (parser
);
27023 finish_handler_sequence (try_block
);
27028 /* Parse a function-try-block.
27030 function-try-block:
27031 try ctor-initializer [opt] function-body handler-seq */
27034 cp_parser_function_try_block (cp_parser
* parser
)
27036 tree compound_stmt
;
27039 /* Look for the `try' keyword. */
27040 if (!cp_parser_require_keyword (parser
, RID_TRY
, RT_TRY
))
27042 /* Let the rest of the front end know where we are. */
27043 try_block
= begin_function_try_block (&compound_stmt
);
27044 /* Parse the function-body. */
27045 cp_parser_ctor_initializer_opt_and_function_body
27046 (parser
, /*in_function_try_block=*/true);
27047 /* We're done with the `try' part. */
27048 finish_function_try_block (try_block
);
27049 /* Parse the handlers. */
27050 cp_parser_handler_seq (parser
);
27051 /* We're done with the handlers. */
27052 finish_function_handler_sequence (try_block
, compound_stmt
);
27055 /* Parse a handler-seq.
27058 handler handler-seq [opt] */
27061 cp_parser_handler_seq (cp_parser
* parser
)
27067 /* Parse the handler. */
27068 cp_parser_handler (parser
);
27069 /* Peek at the next token. */
27070 token
= cp_lexer_peek_token (parser
->lexer
);
27071 /* If it's not `catch' then there are no more handlers. */
27072 if (!cp_parser_is_keyword (token
, RID_CATCH
))
27077 /* Parse a handler.
27080 catch ( exception-declaration ) compound-statement */
27083 cp_parser_handler (cp_parser
* parser
)
27088 cp_parser_require_keyword (parser
, RID_CATCH
, RT_CATCH
);
27089 handler
= begin_handler ();
27090 matching_parens parens
;
27091 parens
.require_open (parser
);
27092 declaration
= cp_parser_exception_declaration (parser
);
27093 finish_handler_parms (declaration
, handler
);
27094 parens
.require_close (parser
);
27095 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
27096 finish_handler (handler
);
27099 /* Parse an exception-declaration.
27101 exception-declaration:
27102 type-specifier-seq declarator
27103 type-specifier-seq abstract-declarator
27107 Returns a VAR_DECL for the declaration, or NULL_TREE if the
27108 ellipsis variant is used. */
27111 cp_parser_exception_declaration (cp_parser
* parser
)
27113 cp_decl_specifier_seq type_specifiers
;
27114 cp_declarator
*declarator
;
27115 const char *saved_message
;
27117 /* If it's an ellipsis, it's easy to handle. */
27118 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
27120 /* Consume the `...' token. */
27121 cp_lexer_consume_token (parser
->lexer
);
27125 /* Types may not be defined in exception-declarations. */
27126 saved_message
= parser
->type_definition_forbidden_message
;
27127 parser
->type_definition_forbidden_message
27128 = G_("types may not be defined in exception-declarations");
27130 /* Parse the type-specifier-seq. */
27131 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
27132 /*is_declaration=*/true,
27133 /*is_trailing_return=*/false,
27135 /* If it's a `)', then there is no declarator. */
27136 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
27139 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_EITHER
,
27140 CP_PARSER_FLAGS_NONE
,
27141 /*ctor_dtor_or_conv_p=*/NULL
,
27142 /*parenthesized_p=*/NULL
,
27143 /*member_p=*/false,
27144 /*friend_p=*/false,
27145 /*static_p=*/false);
27147 /* Restore the saved message. */
27148 parser
->type_definition_forbidden_message
= saved_message
;
27150 if (!type_specifiers
.any_specifiers_p
)
27151 return error_mark_node
;
27153 return grokdeclarator (declarator
, &type_specifiers
, CATCHPARM
, 1, NULL
);
27156 /* Parse a throw-expression.
27159 throw assignment-expression [opt]
27161 Returns a THROW_EXPR representing the throw-expression. */
27164 cp_parser_throw_expression (cp_parser
* parser
)
27168 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27170 cp_parser_require_keyword (parser
, RID_THROW
, RT_THROW
);
27171 token
= cp_lexer_peek_token (parser
->lexer
);
27172 /* Figure out whether or not there is an assignment-expression
27173 following the "throw" keyword. */
27174 if (token
->type
== CPP_COMMA
27175 || token
->type
== CPP_SEMICOLON
27176 || token
->type
== CPP_CLOSE_PAREN
27177 || token
->type
== CPP_CLOSE_SQUARE
27178 || token
->type
== CPP_CLOSE_BRACE
27179 || token
->type
== CPP_COLON
)
27180 expression
= NULL_TREE
;
27182 expression
= cp_parser_assignment_expression (parser
);
27184 /* Construct a location e.g.:
27187 with caret == start at the start of the "throw" token, and
27188 the end at the end of the final token we consumed. */
27189 location_t combined_loc
= make_location (start_loc
, start_loc
,
27191 expression
= build_throw (combined_loc
, expression
);
27196 /* Parse a yield-expression.
27199 co_yield assignment-expression
27200 co_yield braced-init-list
27202 Returns a CO_YIELD_EXPR representing the yield-expression. */
27205 cp_parser_yield_expression (cp_parser
* parser
)
27209 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27210 location_t kw_loc
= token
->location
; /* Save for later. */
27212 cp_parser_require_keyword (parser
, RID_CO_YIELD
, RT_CO_YIELD
);
27214 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
27216 bool expr_non_constant_p
;
27217 cp_lexer_set_source_position (parser
->lexer
);
27218 /* ??? : probably a moot point? */
27219 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
27220 expr
= cp_parser_braced_list (parser
, &expr_non_constant_p
);
27223 expr
= cp_parser_assignment_expression (parser
);
27225 if (expr
== error_mark_node
)
27228 return finish_co_yield_expr (kw_loc
, expr
);
27231 /* GNU Extensions */
27233 /* Parse an (optional) asm-specification.
27236 asm ( string-literal )
27238 If the asm-specification is present, returns a STRING_CST
27239 corresponding to the string-literal. Otherwise, returns
27243 cp_parser_asm_specification_opt (cp_parser
* parser
)
27246 tree asm_specification
;
27248 /* Peek at the next token. */
27249 token
= cp_lexer_peek_token (parser
->lexer
);
27250 /* If the next token isn't the `asm' keyword, then there's no
27251 asm-specification. */
27252 if (!cp_parser_is_keyword (token
, RID_ASM
))
27255 /* Consume the `asm' token. */
27256 cp_lexer_consume_token (parser
->lexer
);
27257 /* Look for the `('. */
27258 matching_parens parens
;
27259 parens
.require_open (parser
);
27261 /* Look for the string-literal. */
27262 asm_specification
= cp_parser_string_literal (parser
, false, false);
27264 /* Look for the `)'. */
27265 parens
.require_close (parser
);
27267 return asm_specification
;
27270 /* Parse an asm-operand-list.
27274 asm-operand-list , asm-operand
27277 string-literal ( expression )
27278 [ string-literal ] string-literal ( expression )
27280 Returns a TREE_LIST representing the operands. The TREE_VALUE of
27281 each node is the expression. The TREE_PURPOSE is itself a
27282 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
27283 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
27284 is a STRING_CST for the string literal before the parenthesis. Returns
27285 ERROR_MARK_NODE if any of the operands are invalid. */
27288 cp_parser_asm_operand_list (cp_parser
* parser
)
27290 tree asm_operands
= NULL_TREE
;
27291 bool invalid_operands
= false;
27295 tree string_literal
;
27299 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
27301 /* Consume the `[' token. */
27302 cp_lexer_consume_token (parser
->lexer
);
27303 /* Read the operand name. */
27304 name
= cp_parser_identifier (parser
);
27305 if (name
!= error_mark_node
)
27306 name
= build_string (IDENTIFIER_LENGTH (name
),
27307 IDENTIFIER_POINTER (name
));
27308 /* Look for the closing `]'. */
27309 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
27313 /* Look for the string-literal. */
27314 string_literal
= cp_parser_string_literal (parser
, false, false);
27316 /* Look for the `('. */
27317 matching_parens parens
;
27318 parens
.require_open (parser
);
27319 /* Parse the expression. */
27320 expression
= cp_parser_expression (parser
);
27321 /* Look for the `)'. */
27322 parens
.require_close (parser
);
27324 if (name
== error_mark_node
27325 || string_literal
== error_mark_node
27326 || expression
== error_mark_node
)
27327 invalid_operands
= true;
27329 /* Add this operand to the list. */
27330 asm_operands
= tree_cons (build_tree_list (name
, string_literal
),
27333 /* If the next token is not a `,', there are no more
27335 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27337 /* Consume the `,'. */
27338 cp_lexer_consume_token (parser
->lexer
);
27341 return invalid_operands
? error_mark_node
: nreverse (asm_operands
);
27344 /* Parse an asm-clobber-list.
27348 asm-clobber-list , string-literal
27350 Returns a TREE_LIST, indicating the clobbers in the order that they
27351 appeared. The TREE_VALUE of each node is a STRING_CST. */
27354 cp_parser_asm_clobber_list (cp_parser
* parser
)
27356 tree clobbers
= NULL_TREE
;
27360 tree string_literal
;
27362 /* Look for the string literal. */
27363 string_literal
= cp_parser_string_literal (parser
, false, false);
27364 /* Add it to the list. */
27365 clobbers
= tree_cons (NULL_TREE
, string_literal
, clobbers
);
27366 /* If the next token is not a `,', then the list is
27368 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27370 /* Consume the `,' token. */
27371 cp_lexer_consume_token (parser
->lexer
);
27377 /* Parse an asm-label-list.
27381 asm-label-list , identifier
27383 Returns a TREE_LIST, indicating the labels in the order that they
27384 appeared. The TREE_VALUE of each node is a label. */
27387 cp_parser_asm_label_list (cp_parser
* parser
)
27389 tree labels
= NULL_TREE
;
27393 tree identifier
, label
, name
;
27395 /* Look for the identifier. */
27396 identifier
= cp_parser_identifier (parser
);
27397 if (!error_operand_p (identifier
))
27399 label
= lookup_label (identifier
);
27400 if (TREE_CODE (label
) == LABEL_DECL
)
27402 TREE_USED (label
) = 1;
27403 check_goto (label
);
27404 name
= build_string (IDENTIFIER_LENGTH (identifier
),
27405 IDENTIFIER_POINTER (identifier
));
27406 labels
= tree_cons (name
, label
, labels
);
27409 /* If the next token is not a `,', then the list is
27411 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
27413 /* Consume the `,' token. */
27414 cp_lexer_consume_token (parser
->lexer
);
27417 return nreverse (labels
);
27420 /* Return TRUE iff the next tokens in the stream are possibly the
27421 beginning of a GNU extension attribute. */
27424 cp_next_tokens_can_be_gnu_attribute_p (cp_parser
*parser
)
27426 return cp_nth_tokens_can_be_gnu_attribute_p (parser
, 1);
27429 /* Return TRUE iff the next tokens in the stream are possibly the
27430 beginning of a standard C++-11 attribute specifier. */
27433 cp_next_tokens_can_be_std_attribute_p (cp_parser
*parser
)
27435 return cp_nth_tokens_can_be_std_attribute_p (parser
, 1);
27438 /* Return TRUE iff the next Nth tokens in the stream are possibly the
27439 beginning of a standard C++-11 attribute specifier. */
27442 cp_nth_tokens_can_be_std_attribute_p (cp_parser
*parser
, size_t n
)
27444 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
27446 return (cxx_dialect
>= cxx11
27447 && ((token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ALIGNAS
)
27448 || (token
->type
== CPP_OPEN_SQUARE
27449 && (token
= cp_lexer_peek_nth_token (parser
->lexer
, n
+ 1))
27450 && token
->type
== CPP_OPEN_SQUARE
)));
27453 /* Return TRUE iff the next Nth tokens in the stream are possibly the
27454 beginning of a GNU extension attribute. */
27457 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser
*parser
, size_t n
)
27459 cp_token
*token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
27461 return token
->type
== CPP_KEYWORD
&& token
->keyword
== RID_ATTRIBUTE
;
27464 /* Return true iff the next tokens can be the beginning of either a
27465 GNU attribute list, or a standard C++11 attribute sequence. */
27468 cp_next_tokens_can_be_attribute_p (cp_parser
*parser
)
27470 return (cp_next_tokens_can_be_gnu_attribute_p (parser
)
27471 || cp_next_tokens_can_be_std_attribute_p (parser
));
27474 /* Return true iff the next Nth tokens can be the beginning of either
27475 a GNU attribute list, or a standard C++11 attribute sequence. */
27478 cp_nth_tokens_can_be_attribute_p (cp_parser
*parser
, size_t n
)
27480 return (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
)
27481 || cp_nth_tokens_can_be_std_attribute_p (parser
, n
));
27484 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
27485 of GNU attributes, or return NULL. */
27488 cp_parser_attributes_opt (cp_parser
*parser
)
27490 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
27491 return cp_parser_gnu_attributes_opt (parser
);
27492 return cp_parser_std_attribute_spec_seq (parser
);
27495 /* Parse an (optional) series of attributes.
27498 attributes attribute
27501 __attribute__ (( attribute-list [opt] ))
27503 The return value is as for cp_parser_gnu_attribute_list. */
27506 cp_parser_gnu_attributes_opt (cp_parser
* parser
)
27508 tree attributes
= NULL_TREE
;
27510 auto cleanup
= make_temp_override
27511 (parser
->auto_is_implicit_function_template_parm_p
, false);
27516 tree attribute_list
;
27519 /* Peek at the next token. */
27520 token
= cp_lexer_peek_token (parser
->lexer
);
27521 /* If it's not `__attribute__', then we're done. */
27522 if (token
->keyword
!= RID_ATTRIBUTE
)
27525 /* Consume the `__attribute__' keyword. */
27526 cp_lexer_consume_token (parser
->lexer
);
27527 /* Look for the two `(' tokens. */
27528 matching_parens outer_parens
;
27529 if (!outer_parens
.require_open (parser
))
27531 matching_parens inner_parens
;
27532 if (!inner_parens
.require_open (parser
))
27535 /* Peek at the next token. */
27536 token
= cp_lexer_peek_token (parser
->lexer
);
27537 if (token
->type
!= CPP_CLOSE_PAREN
)
27538 /* Parse the attribute-list. */
27539 attribute_list
= cp_parser_gnu_attribute_list (parser
);
27541 /* If the next token is a `)', then there is no attribute
27543 attribute_list
= NULL
;
27545 /* Look for the two `)' tokens. */
27546 if (!inner_parens
.require_close (parser
))
27548 if (!outer_parens
.require_close (parser
))
27551 cp_parser_skip_to_end_of_statement (parser
);
27553 /* Add these new attributes to the list. */
27554 attributes
= attr_chainon (attributes
, attribute_list
);
27560 /* Parse a GNU attribute-list.
27564 attribute-list , attribute
27568 identifier ( identifier )
27569 identifier ( identifier , expression-list )
27570 identifier ( expression-list )
27572 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
27573 to an attribute. The TREE_PURPOSE of each node is the identifier
27574 indicating which attribute is in use. The TREE_VALUE represents
27575 the arguments, if any. */
27578 cp_parser_gnu_attribute_list (cp_parser
* parser
, bool exactly_one
/* = false */)
27580 tree attribute_list
= NULL_TREE
;
27581 bool save_translate_strings_p
= parser
->translate_strings_p
;
27583 /* Don't create wrapper nodes within attributes: the
27584 handlers don't know how to handle them. */
27585 auto_suppress_location_wrappers sentinel
;
27587 parser
->translate_strings_p
= false;
27594 /* Look for the identifier. We also allow keywords here; for
27595 example `__attribute__ ((const))' is legal. */
27596 token
= cp_lexer_peek_token (parser
->lexer
);
27597 if (token
->type
== CPP_NAME
27598 || token
->type
== CPP_KEYWORD
)
27600 tree arguments
= NULL_TREE
;
27602 /* Consume the token, but save it since we need it for the
27603 SIMD enabled function parsing. */
27604 cp_token
*id_token
= cp_lexer_consume_token (parser
->lexer
);
27606 /* Save away the identifier that indicates which attribute
27608 identifier
= (token
->type
== CPP_KEYWORD
)
27609 /* For keywords, use the canonical spelling, not the
27610 parsed identifier. */
27611 ? ridpointers
[(int) token
->keyword
]
27612 : id_token
->u
.value
;
27614 identifier
= canonicalize_attr_name (identifier
);
27615 attribute
= build_tree_list (identifier
, NULL_TREE
);
27617 /* Peek at the next token. */
27618 token
= cp_lexer_peek_token (parser
->lexer
);
27619 /* If it's an `(', then parse the attribute arguments. */
27620 if (token
->type
== CPP_OPEN_PAREN
)
27622 vec
<tree
, va_gc
> *vec
;
27623 int attr_flag
= (attribute_takes_identifier_p (identifier
)
27624 ? id_attr
: normal_attr
);
27625 vec
= cp_parser_parenthesized_expression_list
27626 (parser
, attr_flag
, /*cast_p=*/false,
27627 /*allow_expansion_p=*/false,
27628 /*non_constant_p=*/NULL
);
27630 arguments
= error_mark_node
;
27633 arguments
= build_tree_list_vec (vec
);
27634 release_tree_vector (vec
);
27636 /* Save the arguments away. */
27637 TREE_VALUE (attribute
) = arguments
;
27640 if (arguments
!= error_mark_node
)
27642 /* Add this attribute to the list. */
27643 TREE_CHAIN (attribute
) = attribute_list
;
27644 attribute_list
= attribute
;
27647 token
= cp_lexer_peek_token (parser
->lexer
);
27649 /* Unless EXACTLY_ONE is set look for more attributes.
27650 If the next token isn't a `,', we're done. */
27651 if (exactly_one
|| token
->type
!= CPP_COMMA
)
27654 /* Consume the comma and keep going. */
27655 cp_lexer_consume_token (parser
->lexer
);
27657 parser
->translate_strings_p
= save_translate_strings_p
;
27659 /* We built up the list in reverse order. */
27660 return nreverse (attribute_list
);
27663 /* Parse a standard C++11 attribute.
27665 The returned representation is a TREE_LIST which TREE_PURPOSE is
27666 the scoped name of the attribute, and the TREE_VALUE is its
27669 Note that the scoped name of the attribute is itself a TREE_LIST
27670 which TREE_PURPOSE is the namespace of the attribute, and
27671 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
27672 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
27673 and which TREE_PURPOSE is directly the attribute name.
27675 Clients of the attribute code should use get_attribute_namespace
27676 and get_attribute_name to get the actual namespace and name of
27677 attributes, regardless of their being GNU or C++11 attributes.
27680 attribute-token attribute-argument-clause [opt]
27684 attribute-scoped-token
27686 attribute-scoped-token:
27687 attribute-namespace :: identifier
27689 attribute-namespace:
27692 attribute-argument-clause:
27693 ( balanced-token-seq )
27695 balanced-token-seq:
27696 balanced-token [opt]
27697 balanced-token-seq balanced-token
27700 ( balanced-token-seq )
27701 [ balanced-token-seq ]
27702 { balanced-token-seq }. */
27705 cp_parser_std_attribute (cp_parser
*parser
, tree attr_ns
)
27707 tree attribute
, attr_id
= NULL_TREE
, arguments
;
27710 auto cleanup
= make_temp_override
27711 (parser
->auto_is_implicit_function_template_parm_p
, false);
27713 /* First, parse name of the attribute, a.k.a attribute-token. */
27715 token
= cp_lexer_peek_token (parser
->lexer
);
27716 if (token
->type
== CPP_NAME
)
27717 attr_id
= token
->u
.value
;
27718 else if (token
->type
== CPP_KEYWORD
)
27719 attr_id
= ridpointers
[(int) token
->keyword
];
27720 else if (token
->flags
& NAMED_OP
)
27721 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
27723 if (attr_id
== NULL_TREE
)
27726 cp_lexer_consume_token (parser
->lexer
);
27728 token
= cp_lexer_peek_token (parser
->lexer
);
27729 if (token
->type
== CPP_SCOPE
)
27731 /* We are seeing a scoped attribute token. */
27733 cp_lexer_consume_token (parser
->lexer
);
27735 error_at (token
->location
, "attribute using prefix used together "
27736 "with scoped attribute token");
27739 token
= cp_lexer_peek_token (parser
->lexer
);
27740 if (token
->type
== CPP_NAME
)
27741 attr_id
= token
->u
.value
;
27742 else if (token
->type
== CPP_KEYWORD
)
27743 attr_id
= ridpointers
[(int) token
->keyword
];
27744 else if (token
->flags
& NAMED_OP
)
27745 attr_id
= get_identifier (cpp_type2name (token
->type
, token
->flags
));
27748 error_at (token
->location
,
27749 "expected an identifier for the attribute name");
27750 return error_mark_node
;
27752 cp_lexer_consume_token (parser
->lexer
);
27754 attr_ns
= canonicalize_attr_name (attr_ns
);
27755 attr_id
= canonicalize_attr_name (attr_id
);
27756 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
27758 token
= cp_lexer_peek_token (parser
->lexer
);
27762 attr_ns
= canonicalize_attr_name (attr_ns
);
27763 attr_id
= canonicalize_attr_name (attr_id
);
27764 attribute
= build_tree_list (build_tree_list (attr_ns
, attr_id
),
27769 attr_id
= canonicalize_attr_name (attr_id
);
27770 attribute
= build_tree_list (build_tree_list (NULL_TREE
, attr_id
),
27772 /* We used to treat C++11 noreturn attribute as equivalent to GNU's,
27773 but no longer: we have to be able to tell [[noreturn]] and
27774 __attribute__((noreturn)) apart. */
27775 /* C++14 deprecated attribute is equivalent to GNU's. */
27776 if (is_attribute_p ("deprecated", attr_id
))
27777 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
27778 /* C++17 fallthrough attribute is equivalent to GNU's. */
27779 else if (is_attribute_p ("fallthrough", attr_id
))
27780 TREE_PURPOSE (TREE_PURPOSE (attribute
)) = gnu_identifier
;
27781 /* Transactional Memory TS optimize_for_synchronized attribute is
27782 equivalent to GNU transaction_callable. */
27783 else if (is_attribute_p ("optimize_for_synchronized", attr_id
))
27784 TREE_PURPOSE (attribute
)
27785 = get_identifier ("transaction_callable");
27786 /* Transactional Memory attributes are GNU attributes. */
27787 else if (tm_attr_to_mask (attr_id
))
27788 TREE_PURPOSE (attribute
) = attr_id
;
27791 /* Now parse the optional argument clause of the attribute. */
27793 if (token
->type
!= CPP_OPEN_PAREN
)
27797 vec
<tree
, va_gc
> *vec
;
27798 int attr_flag
= normal_attr
;
27800 /* Maybe we don't expect to see any arguments for this attribute. */
27801 const attribute_spec
*as
27802 = lookup_attribute_spec (TREE_PURPOSE (attribute
));
27803 if (as
&& as
->max_length
== 0)
27805 error_at (token
->location
, "%qE attribute does not take any arguments",
27807 cp_parser_skip_to_closing_parenthesis (parser
,
27808 /*recovering=*/true,
27809 /*or_comma=*/false,
27810 /*consume_paren=*/true);
27811 return error_mark_node
;
27814 if (attr_ns
== gnu_identifier
27815 && attribute_takes_identifier_p (attr_id
))
27816 /* A GNU attribute that takes an identifier in parameter. */
27817 attr_flag
= id_attr
;
27821 /* For unknown attributes, just skip balanced tokens instead of
27822 trying to parse the arguments. */
27823 for (size_t n
= cp_parser_skip_balanced_tokens (parser
, 1) - 1; n
; --n
)
27824 cp_lexer_consume_token (parser
->lexer
);
27828 vec
= cp_parser_parenthesized_expression_list
27829 (parser
, attr_flag
, /*cast_p=*/false,
27830 /*allow_expansion_p=*/true,
27831 /*non_constant_p=*/NULL
);
27833 arguments
= error_mark_node
;
27836 if (vec
->is_empty ())
27837 /* e.g. [[attr()]]. */
27838 error_at (token
->location
, "parentheses must be omitted if "
27839 "%qE attribute argument list is empty",
27841 arguments
= build_tree_list_vec (vec
);
27842 release_tree_vector (vec
);
27845 if (arguments
== error_mark_node
)
27846 attribute
= error_mark_node
;
27848 TREE_VALUE (attribute
) = arguments
;
27854 /* Warn if the attribute ATTRIBUTE appears more than once in the
27855 attribute-list ATTRIBUTES. This used to be enforced for certain
27856 attributes, but the restriction was removed in P2156. Note that
27857 carries_dependency ([dcl.attr.depend]) isn't implemented yet in GCC.
27858 LOC is the location of ATTRIBUTE. Returns true if ATTRIBUTE was not
27859 found in ATTRIBUTES. */
27862 cp_parser_check_std_attribute (location_t loc
, tree attributes
, tree attribute
)
27864 static auto alist
= { "noreturn", "deprecated", "nodiscard", "maybe_unused",
27865 "likely", "unlikely", "fallthrough",
27866 "no_unique_address" };
27868 for (const auto &a
: alist
)
27869 if (is_attribute_p (a
, get_attribute_name (attribute
))
27870 && lookup_attribute (a
, attributes
))
27872 if (!from_macro_expansion_at (loc
))
27873 warning_at (loc
, OPT_Wattributes
, "attribute %qs specified "
27874 "multiple times", a
);
27880 /* Parse a list of standard C++-11 attributes.
27884 attribute-list , attribute[opt]
27886 attribute-list , attribute ...
27890 cp_parser_std_attribute_list (cp_parser
*parser
, tree attr_ns
)
27892 tree attributes
= NULL_TREE
, attribute
= NULL_TREE
;
27893 cp_token
*token
= NULL
;
27897 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
27898 attribute
= cp_parser_std_attribute (parser
, attr_ns
);
27899 if (attribute
== error_mark_node
)
27901 if (attribute
!= NULL_TREE
)
27903 if (cp_parser_check_std_attribute (loc
, attributes
, attribute
))
27905 TREE_CHAIN (attribute
) = attributes
;
27906 attributes
= attribute
;
27909 token
= cp_lexer_peek_token (parser
->lexer
);
27910 if (token
->type
== CPP_ELLIPSIS
)
27912 cp_lexer_consume_token (parser
->lexer
);
27913 if (attribute
== NULL_TREE
)
27914 error_at (token
->location
,
27915 "expected attribute before %<...%>");
27918 tree pack
= make_pack_expansion (TREE_VALUE (attribute
));
27919 if (pack
== error_mark_node
)
27920 return error_mark_node
;
27921 TREE_VALUE (attribute
) = pack
;
27923 token
= cp_lexer_peek_token (parser
->lexer
);
27925 if (token
->type
!= CPP_COMMA
)
27927 cp_lexer_consume_token (parser
->lexer
);
27929 attributes
= nreverse (attributes
);
27933 /* Parse a standard C++-11 attribute specifier.
27935 attribute-specifier:
27936 [ [ attribute-using-prefix [opt] attribute-list ] ]
27937 alignment-specifier
27939 attribute-using-prefix:
27940 using attribute-namespace :
27942 alignment-specifier:
27943 alignas ( type-id ... [opt] )
27944 alignas ( alignment-expression ... [opt] ). */
27947 cp_parser_std_attribute_spec (cp_parser
*parser
)
27949 tree attributes
= NULL_TREE
;
27950 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
27952 if (token
->type
== CPP_OPEN_SQUARE
27953 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_OPEN_SQUARE
)
27955 tree attr_ns
= NULL_TREE
;
27957 cp_lexer_consume_token (parser
->lexer
);
27958 cp_lexer_consume_token (parser
->lexer
);
27960 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
27962 token
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
27963 if (token
->type
== CPP_NAME
)
27964 attr_ns
= token
->u
.value
;
27965 else if (token
->type
== CPP_KEYWORD
)
27966 attr_ns
= ridpointers
[(int) token
->keyword
];
27967 else if (token
->flags
& NAMED_OP
)
27968 attr_ns
= get_identifier (cpp_type2name (token
->type
,
27971 && cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_COLON
))
27973 if (cxx_dialect
< cxx17
)
27974 pedwarn (input_location
, 0,
27975 "attribute using prefix only available "
27976 "with %<-std=c++17%> or %<-std=gnu++17%>");
27978 cp_lexer_consume_token (parser
->lexer
);
27979 cp_lexer_consume_token (parser
->lexer
);
27980 cp_lexer_consume_token (parser
->lexer
);
27983 attr_ns
= NULL_TREE
;
27986 attributes
= cp_parser_std_attribute_list (parser
, attr_ns
);
27988 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
)
27989 || !cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
))
27990 cp_parser_skip_to_end_of_statement (parser
);
27992 /* Warn about parsing c++11 attribute in non-c++11 mode, only
27993 when we are sure that we have actually parsed them. */
27994 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
28000 /* Look for an alignment-specifier. */
28002 token
= cp_lexer_peek_token (parser
->lexer
);
28004 if (token
->type
!= CPP_KEYWORD
28005 || token
->keyword
!= RID_ALIGNAS
)
28008 cp_lexer_consume_token (parser
->lexer
);
28009 maybe_warn_cpp0x (CPP0X_ATTRIBUTES
);
28011 matching_parens parens
;
28012 if (!parens
.require_open (parser
))
28013 return error_mark_node
;
28015 cp_parser_parse_tentatively (parser
);
28016 alignas_expr
= cp_parser_type_id (parser
);
28018 if (!cp_parser_parse_definitely (parser
))
28020 alignas_expr
= cp_parser_assignment_expression (parser
);
28021 if (alignas_expr
== error_mark_node
)
28022 cp_parser_skip_to_end_of_statement (parser
);
28023 if (alignas_expr
== NULL_TREE
28024 || alignas_expr
== error_mark_node
)
28025 return alignas_expr
;
28028 alignas_expr
= cxx_alignas_expr (alignas_expr
);
28029 alignas_expr
= build_tree_list (NULL_TREE
, alignas_expr
);
28031 /* Handle alignas (pack...). */
28032 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
28034 cp_lexer_consume_token (parser
->lexer
);
28035 alignas_expr
= make_pack_expansion (alignas_expr
);
28038 /* Something went wrong, so don't build the attribute. */
28039 if (alignas_expr
== error_mark_node
)
28040 return error_mark_node
;
28042 /* Missing ')' means the code cannot possibly be valid; go ahead
28043 and commit to make sure we issue a hard error. */
28044 if (cp_parser_uncommitted_to_tentative_parse_p (parser
)
28045 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
28046 cp_parser_commit_to_tentative_parse (parser
);
28048 if (!parens
.require_close (parser
))
28049 return error_mark_node
;
28051 /* Build the C++-11 representation of an 'aligned'
28054 = build_tree_list (build_tree_list (gnu_identifier
,
28055 aligned_identifier
), alignas_expr
);
28061 /* Parse a standard C++-11 attribute-specifier-seq.
28063 attribute-specifier-seq:
28064 attribute-specifier-seq [opt] attribute-specifier
28068 cp_parser_std_attribute_spec_seq (cp_parser
*parser
)
28070 tree attr_specs
= NULL_TREE
;
28071 tree attr_last
= NULL_TREE
;
28073 /* Don't create wrapper nodes within attributes: the
28074 handlers don't know how to handle them. */
28075 auto_suppress_location_wrappers sentinel
;
28079 tree attr_spec
= cp_parser_std_attribute_spec (parser
);
28080 if (attr_spec
== NULL_TREE
)
28082 if (attr_spec
== error_mark_node
)
28083 return error_mark_node
;
28086 TREE_CHAIN (attr_last
) = attr_spec
;
28088 attr_specs
= attr_last
= attr_spec
;
28089 attr_last
= tree_last (attr_last
);
28095 /* Skip a balanced-token starting at Nth token (with 1 as the next token),
28096 return index of the first token after balanced-token, or N on failure. */
28099 cp_parser_skip_balanced_tokens (cp_parser
*parser
, size_t n
)
28102 int nparens
= 0, nbraces
= 0, nsquares
= 0;
28104 switch (cp_lexer_peek_nth_token (parser
->lexer
, n
++)->type
)
28106 case CPP_PRAGMA_EOL
:
28107 if (!parser
->lexer
->in_pragma
)
28111 /* Ran out of tokens. */
28113 case CPP_OPEN_PAREN
:
28116 case CPP_OPEN_BRACE
:
28119 case CPP_OPEN_SQUARE
:
28122 case CPP_CLOSE_PAREN
:
28125 case CPP_CLOSE_BRACE
:
28128 case CPP_CLOSE_SQUARE
:
28134 while (nparens
|| nbraces
|| nsquares
);
28138 /* Skip GNU attribute tokens starting at Nth token (with 1 as the next token),
28139 return index of the first token after the GNU attribute tokens, or N on
28143 cp_parser_skip_gnu_attributes_opt (cp_parser
*parser
, size_t n
)
28147 if (!cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ATTRIBUTE
)
28148 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
)
28149 || !cp_lexer_nth_token_is (parser
->lexer
, n
+ 2, CPP_OPEN_PAREN
))
28152 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 2);
28155 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_PAREN
))
28162 /* Skip standard C++11 attribute tokens starting at Nth token (with 1 as the
28163 next token), return index of the first token after the standard C++11
28164 attribute tokens, or N on failure. */
28167 cp_parser_skip_std_attribute_spec_seq (cp_parser
*parser
, size_t n
)
28171 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_OPEN_SQUARE
)
28172 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_SQUARE
))
28174 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
28177 if (!cp_lexer_nth_token_is (parser
->lexer
, n2
, CPP_CLOSE_SQUARE
))
28181 else if (cp_lexer_nth_token_is_keyword (parser
->lexer
, n
, RID_ALIGNAS
)
28182 && cp_lexer_nth_token_is (parser
->lexer
, n
+ 1, CPP_OPEN_PAREN
))
28184 size_t n2
= cp_parser_skip_balanced_tokens (parser
, n
+ 1);
28195 /* Skip standard C++11 or GNU attribute tokens starting at Nth token (with 1
28196 as the next token), return index of the first token after the attribute
28197 tokens, or N on failure. */
28200 cp_parser_skip_attributes_opt (cp_parser
*parser
, size_t n
)
28202 if (cp_nth_tokens_can_be_gnu_attribute_p (parser
, n
))
28203 return cp_parser_skip_gnu_attributes_opt (parser
, n
);
28204 return cp_parser_skip_std_attribute_spec_seq (parser
, n
);
28207 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
28208 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
28209 current value of the PEDANTIC flag, regardless of whether or not
28210 the `__extension__' keyword is present. The caller is responsible
28211 for restoring the value of the PEDANTIC flag. */
28214 cp_parser_extension_opt (cp_parser
* parser
, int* saved_pedantic
)
28216 /* Save the old value of the PEDANTIC flag. */
28217 *saved_pedantic
= pedantic
;
28219 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_EXTENSION
))
28221 /* Consume the `__extension__' token. */
28222 cp_lexer_consume_token (parser
->lexer
);
28223 /* We're not being pedantic while the `__extension__' keyword is
28233 /* Parse a label declaration.
28236 __label__ label-declarator-seq ;
28238 label-declarator-seq:
28239 identifier , label-declarator-seq
28243 cp_parser_label_declaration (cp_parser
* parser
)
28245 /* Look for the `__label__' keyword. */
28246 cp_parser_require_keyword (parser
, RID_LABEL
, RT_LABEL
);
28252 /* Look for an identifier. */
28253 identifier
= cp_parser_identifier (parser
);
28254 /* If we failed, stop. */
28255 if (identifier
== error_mark_node
)
28257 /* Declare it as a label. */
28258 finish_label_decl (identifier
);
28259 /* If the next token is a `;', stop. */
28260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
28262 /* Look for the `,' separating the label declarations. */
28263 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
28266 /* Look for the final `;'. */
28267 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
28270 // -------------------------------------------------------------------------- //
28271 // Concept definitions
28274 cp_parser_concept_definition (cp_parser
*parser
)
28276 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
));
28277 cp_lexer_consume_token (parser
->lexer
);
28279 cp_expr id
= cp_parser_identifier (parser
);
28280 if (id
== error_mark_node
)
28282 cp_parser_skip_to_end_of_statement (parser
);
28283 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28287 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
28289 cp_parser_skip_to_end_of_statement (parser
);
28290 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28291 return error_mark_node
;
28294 processing_constraint_expression_sentinel parsing_constraint
;
28295 tree init
= cp_parser_constraint_expression (parser
);
28296 if (init
== error_mark_node
)
28297 cp_parser_skip_to_end_of_statement (parser
);
28299 /* Consume the trailing ';'. Diagnose the problem if it isn't there,
28300 but continue as if it were. */
28301 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28303 return finish_concept_definition (id
, init
);
28306 // -------------------------------------------------------------------------- //
28309 /* Diagnose an expression that should appear in ()'s within a requires-clause
28310 and suggest where to place those parentheses. */
28313 cp_parser_diagnose_ungrouped_constraint_plain (location_t loc
)
28315 error_at (loc
, "expression must be enclosed in parentheses");
28319 cp_parser_diagnose_ungrouped_constraint_rich (location_t loc
)
28321 gcc_rich_location
richloc (loc
);
28322 richloc
.add_fixit_insert_before ("(");
28323 richloc
.add_fixit_insert_after (")");
28324 error_at (&richloc
, "expression must be enclosed in parentheses");
28327 /* Characterizes the likely kind of expression intended by a mis-written
28328 primary constraint. */
28329 enum primary_constraint_error
28332 pce_maybe_operator
,
28336 /* Returns true if the token(s) following a primary-expression in a
28337 constraint-logical-* expression would require parentheses. */
28339 static primary_constraint_error
28340 cp_parser_constraint_requires_parens (cp_parser
*parser
, bool lambda_p
)
28342 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28343 switch (token
->type
)
28350 /* An equal sign may be part of the definition of a function,
28351 and not an assignment operator, when parsing the expression
28352 for a trailing requires-clause. For example:
28354 template<typename T>
28356 S() requires C<T> = default;
28359 Don't try to reparse this a binary operator. */
28360 if (cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DELETE
)
28361 || cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_DEFAULT
))
28364 gcc_fallthrough ();
28367 /* Arithmetic operators. */
28373 /* Bitwise operators. */
28379 /* Relational operators. */
28385 case CPP_GREATER_EQ
:
28386 case CPP_SPACESHIP
:
28387 /* Pointer-to-member. */
28389 case CPP_DEREF_STAR
:
28390 /* Assignment operators. */
28399 case CPP_RSHIFT_EQ
:
28400 case CPP_LSHIFT_EQ
:
28401 /* Conditional operator */
28403 /* Unenclosed binary or conditional operator. */
28404 return pce_maybe_operator
;
28406 case CPP_OPEN_PAREN
:
28408 /* A primary constraint that precedes the parameter-list of a
28409 lambda expression is followed by an open paren.
28411 []<typename T> requires C (T a, T b) { ... }
28413 Don't try to re-parse this as a postfix expression. */
28417 gcc_fallthrough ();
28419 case CPP_OPEN_SQUARE
:
28421 /* A primary-constraint-expression followed by a '[[' is not a
28422 postfix expression. */
28423 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
))
28426 gcc_fallthrough ();
28428 case CPP_PLUS_PLUS
:
28429 case CPP_MINUS_MINUS
:
28432 /* Unenclosed postfix operator. */
28433 return pce_maybe_postfix
;
28437 /* Returns true if the next token begins a unary expression, preceded by
28438 an operator or keyword. */
28441 cp_parser_unary_constraint_requires_parens (cp_parser
*parser
)
28443 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
28444 switch (token
->type
)
28451 case CPP_PLUS_PLUS
:
28452 case CPP_MINUS_MINUS
:
28457 switch (token
->keyword
)
28461 case RID_REINTCAST
:
28462 case RID_CONSTCAST
:
28484 /* Parse a primary expression within a constraint. */
28487 cp_parser_constraint_primary_expression (cp_parser
*parser
, bool lambda_p
)
28489 /* If this looks like a unary expression, parse it as such, but diagnose
28490 it as ill-formed; it requires parens. */
28491 if (cp_parser_unary_constraint_requires_parens (parser
))
28493 cp_expr e
= cp_parser_assignment_expression (parser
, NULL
, false, false);
28494 cp_parser_diagnose_ungrouped_constraint_rich (e
.get_location());
28498 cp_lexer_save_tokens (parser
->lexer
);
28500 location_t loc
= input_location
;
28501 cp_expr expr
= cp_parser_primary_expression (parser
,
28502 /*address_p=*/false,
28504 /*template_arg_p=*/false,
28506 expr
.maybe_add_location_wrapper ();
28508 primary_constraint_error pce
= pce_ok
;
28509 if (expr
!= error_mark_node
)
28511 /* The primary-expression could be part of an unenclosed non-logical
28512 compound expression. */
28513 pce
= cp_parser_constraint_requires_parens (parser
, lambda_p
);
28517 cp_lexer_commit_tokens (parser
->lexer
);
28518 return finish_constraint_primary_expr (expr
);
28521 /* Retry the parse at a lower precedence. If that succeeds, diagnose the
28522 error, but return the expression as if it were valid. */
28523 cp_lexer_rollback_tokens (parser
->lexer
);
28524 cp_parser_parse_tentatively (parser
);
28525 if (pce
== pce_maybe_operator
)
28526 expr
= cp_parser_assignment_expression (parser
, NULL
, false, false);
28528 expr
= cp_parser_simple_cast_expression (parser
);
28529 if (cp_parser_parse_definitely (parser
))
28531 cp_parser_diagnose_ungrouped_constraint_rich (expr
.get_location());
28535 /* Otherwise, something has gone very wrong, and we can't generate a more
28536 meaningful diagnostic or recover. */
28537 cp_parser_diagnose_ungrouped_constraint_plain (loc
);
28538 return error_mark_node
;
28541 /* Parse a constraint-logical-and-expression.
28543 constraint-logical-and-expression:
28545 constraint-logical-and-expression '&&' primary-expression */
28548 cp_parser_constraint_logical_and_expression (cp_parser
*parser
, bool lambda_p
)
28550 cp_expr lhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
28551 while (cp_lexer_next_token_is (parser
->lexer
, CPP_AND_AND
))
28553 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
28554 tree rhs
= cp_parser_constraint_primary_expression (parser
, lambda_p
);
28555 lhs
= finish_constraint_and_expr (op
->location
, lhs
, rhs
);
28560 /* Parse a constraint-logical-or-expression.
28562 constraint-logical-or-expression:
28563 constraint-logical-and-expression
28564 constraint-logical-or-expression '||' constraint-logical-and-expression */
28567 cp_parser_constraint_logical_or_expression (cp_parser
*parser
, bool lambda_p
)
28569 cp_expr lhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
28570 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OR_OR
))
28572 cp_token
*op
= cp_lexer_consume_token (parser
->lexer
);
28573 cp_expr rhs
= cp_parser_constraint_logical_and_expression (parser
, lambda_p
);
28574 lhs
= finish_constraint_or_expr (op
->location
, lhs
, rhs
);
28579 /* Parse the expression after a requires-clause. This has a different grammar
28580 than that in the concepts TS. */
28583 cp_parser_requires_clause_expression (cp_parser
*parser
, bool lambda_p
)
28585 processing_constraint_expression_sentinel parsing_constraint
;
28586 temp_override
<int> ovr (processing_template_decl
);
28587 if (!processing_template_decl
)
28588 /* Adjust processing_template_decl so that we always obtain template
28589 trees here. We don't do the usual ++processing_template_decl
28590 because that would skew the template parameter depth of a lambda
28591 within if we're already inside a template. */
28592 processing_template_decl
= 1;
28593 cp_expr expr
= cp_parser_constraint_logical_or_expression (parser
, lambda_p
);
28594 if (check_for_bare_parameter_packs (expr
))
28595 expr
= error_mark_node
;
28599 /* Parse a expression after a requires clause.
28601 constraint-expression:
28602 logical-or-expression
28604 The required logical-or-expression must be a constant expression. Note
28605 that we don't check that the expression is constepxr here. We defer until
28606 we analyze constraints and then, we only check atomic constraints. */
28609 cp_parser_constraint_expression (cp_parser
*parser
)
28611 processing_constraint_expression_sentinel parsing_constraint
;
28612 temp_override
<int> ovr (processing_template_decl
);
28613 if (!processing_template_decl
)
28614 /* As in cp_parser_requires_clause_expression. */
28615 processing_template_decl
= 1;
28616 cp_expr expr
= cp_parser_binary_expression (parser
, false, true,
28617 PREC_NOT_OPERATOR
, NULL
);
28618 if (check_for_bare_parameter_packs (expr
))
28619 expr
= error_mark_node
;
28620 expr
.maybe_add_location_wrapper ();
28624 /* Optionally parse a requires clause:
28627 `requires` constraint-logical-or-expression.
28629 `requires constraint-expression.
28631 LAMBDA_P is true when the requires-clause is parsed before the
28632 parameter-list of a lambda-declarator. */
28635 cp_parser_requires_clause_opt (cp_parser
*parser
, bool lambda_p
)
28637 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
28638 if (tok
->keyword
!= RID_REQUIRES
)
28640 if (!flag_concepts
&& tok
->type
== CPP_NAME
28641 && tok
->u
.value
== ridpointers
[RID_REQUIRES
])
28643 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
28644 "%<requires%> only available with "
28645 "%<-std=c++20%> or %<-fconcepts%>");
28646 /* Parse and discard the requires-clause. */
28647 cp_lexer_consume_token (parser
->lexer
);
28648 cp_parser_constraint_expression (parser
);
28653 cp_token
*tok2
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
28654 if (tok2
->type
== CPP_OPEN_BRACE
)
28656 /* An opening brace following the start of a requires-clause is
28657 ill-formed; the user likely forgot the second `requires' that
28658 would start a requires-expression. */
28659 gcc_rich_location
richloc (tok2
->location
);
28660 richloc
.add_fixit_insert_after (tok
->location
, " requires");
28661 error_at (&richloc
, "missing additional %<requires%> to start "
28662 "a requires-expression");
28663 /* Don't consume the `requires', so that it's reused as the start of a
28664 requires-expression. */
28667 cp_lexer_consume_token (parser
->lexer
);
28669 if (!flag_concepts_ts
)
28670 return cp_parser_requires_clause_expression (parser
, lambda_p
);
28672 return cp_parser_constraint_expression (parser
);
28675 /*---------------------------------------------------------------------------
28676 Requires expressions
28677 ---------------------------------------------------------------------------*/
28679 /* Parse a requires expression
28681 requirement-expression:
28682 'requires' requirement-parameter-list [opt] requirement-body */
28685 cp_parser_requires_expression (cp_parser
*parser
)
28687 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
28688 location_t loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
28690 /* Avoid committing to outer tentative parse. */
28691 tentative_firewall
firewall (parser
);
28693 /* This is definitely a requires-expression. */
28694 cp_parser_commit_to_tentative_parse (parser
);
28698 /* Local parameters are delared as variables within the scope
28699 of the expression. They are not visible past the end of
28700 the expression. Expressions within the requires-expression
28701 are unevaluated. */
28702 struct scope_sentinel
28706 ++cp_unevaluated_operand
;
28707 begin_scope (sk_block
, NULL_TREE
);
28712 pop_bindings_and_leave_scope ();
28713 --cp_unevaluated_operand
;
28717 /* Parse the optional parameter list. */
28718 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
28720 parms
= cp_parser_requirement_parameter_list (parser
);
28721 if (parms
== error_mark_node
)
28722 return error_mark_node
;
28727 /* Parse the requirement body. */
28728 temp_override
<int> ovr (processing_template_decl
);
28729 if (!processing_template_decl
)
28730 /* As in cp_parser_requires_clause_expression. */
28731 processing_template_decl
= 1;
28732 reqs
= cp_parser_requirement_body (parser
);
28733 if (reqs
== error_mark_node
)
28734 return error_mark_node
;
28737 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
28739 grokparms (parms
, &parms
);
28740 loc
= make_location (loc
, loc
, parser
->lexer
);
28741 tree expr
= finish_requires_expr (loc
, parms
, reqs
);
28742 if (!processing_template_decl
)
28744 /* Perform semantic processing now to diagnose any invalid types and
28746 int saved_errorcount
= errorcount
;
28747 tsubst_requires_expr (expr
, NULL_TREE
, tf_warning_or_error
, NULL_TREE
);
28748 if (errorcount
> saved_errorcount
)
28749 return error_mark_node
;
28754 /* Parse a parameterized requirement.
28756 requirement-parameter-list:
28757 '(' parameter-declaration-clause ')' */
28760 cp_parser_requirement_parameter_list (cp_parser
*parser
)
28762 matching_parens parens
;
28763 if (!parens
.require_open (parser
))
28764 return error_mark_node
;
28766 tree parms
= (cp_parser_parameter_declaration_clause
28767 (parser
, CP_PARSER_FLAGS_TYPENAME_OPTIONAL
));
28769 if (!parens
.require_close (parser
))
28770 return error_mark_node
;
28775 /* Parse the body of a requirement.
28778 '{' requirement-list '}' */
28780 cp_parser_requirement_body (cp_parser
*parser
)
28782 matching_braces braces
;
28783 if (!braces
.require_open (parser
))
28784 return error_mark_node
;
28786 tree reqs
= cp_parser_requirement_seq (parser
);
28788 if (!braces
.require_close (parser
))
28789 return error_mark_node
;
28794 /* Parse a sequence of requirements.
28798 requirement-seq requirement */
28801 cp_parser_requirement_seq (cp_parser
*parser
)
28803 tree result
= NULL_TREE
;
28806 tree req
= cp_parser_requirement (parser
);
28807 if (req
!= error_mark_node
)
28808 result
= tree_cons (NULL_TREE
, req
, result
);
28809 } while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_BRACE
));
28811 /* If there are no valid requirements, this is not a valid expression. */
28813 return error_mark_node
;
28815 /* Reverse the order of requirements so they are analyzed in order. */
28816 return nreverse (result
);
28819 /* Parse a syntactic requirement or type requirement.
28823 compound-requirement
28825 nested-requirement */
28828 cp_parser_requirement (cp_parser
*parser
)
28830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
28831 return cp_parser_compound_requirement (parser
);
28832 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TYPENAME
))
28833 return cp_parser_type_requirement (parser
);
28834 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
))
28835 return cp_parser_nested_requirement (parser
);
28837 return cp_parser_simple_requirement (parser
);
28840 /* Parse a simple requirement.
28842 simple-requirement:
28846 cp_parser_simple_requirement (cp_parser
*parser
)
28848 location_t start
= cp_lexer_peek_token (parser
->lexer
)->location
;
28849 cp_expr expr
= cp_parser_expression (parser
, NULL
, false, false);
28850 if (expr
== error_mark_node
)
28851 cp_parser_skip_to_end_of_statement (parser
);
28853 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28855 if (!expr
|| expr
== error_mark_node
)
28856 return error_mark_node
;
28858 /* Sometimes we don't get locations, so use the cached token location
28859 as a reasonable approximation. */
28860 if (expr
.get_location() == UNKNOWN_LOCATION
)
28861 expr
.set_location (start
);
28863 return finish_simple_requirement (expr
.get_location (), expr
);
28866 /* Parse a type requirement
28869 nested-name-specifier [opt] required-type-name ';'
28871 required-type-name:
28873 'template' [opt] simple-template-id */
28876 cp_parser_type_requirement (cp_parser
*parser
)
28878 cp_token
*start_tok
= cp_lexer_consume_token (parser
->lexer
);
28879 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
28881 // Save the scope before parsing name specifiers.
28882 tree saved_scope
= parser
->scope
;
28883 tree saved_object_scope
= parser
->object_scope
;
28884 tree saved_qualifying_scope
= parser
->qualifying_scope
;
28885 cp_parser_global_scope_opt (parser
, /*current_scope_valid_p=*/false);
28886 cp_parser_nested_name_specifier_opt (parser
,
28887 /*typename_keyword_p=*/true,
28888 /*check_dependency_p=*/false,
28890 /*is_declaration=*/false);
28893 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
28895 cp_lexer_consume_token (parser
->lexer
);
28896 type
= cp_parser_template_id (parser
,
28897 /*template_keyword_p=*/true,
28898 /*check_dependency=*/false,
28899 /*tag_type=*/none_type
,
28900 /*is_declaration=*/false);
28901 type
= make_typename_type (parser
->scope
, type
, typename_type
,
28902 /*complain=*/tf_error
);
28905 type
= cp_parser_type_name (parser
, /*typename_keyword_p=*/true);
28907 if (TREE_CODE (type
) == TYPE_DECL
)
28908 type
= TREE_TYPE (type
);
28910 parser
->scope
= saved_scope
;
28911 parser
->object_scope
= saved_object_scope
;
28912 parser
->qualifying_scope
= saved_qualifying_scope
;
28914 if (type
== error_mark_node
)
28915 cp_parser_skip_to_end_of_statement (parser
);
28917 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28919 if (type
== error_mark_node
)
28920 return error_mark_node
;
28922 loc
= make_location (loc
, start_tok
->location
, parser
->lexer
);
28923 return finish_type_requirement (loc
, type
);
28926 /* Parse a compound requirement
28928 compound-requirement:
28929 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
28932 cp_parser_compound_requirement (cp_parser
*parser
)
28934 /* Parse an expression enclosed in '{ }'s. */
28935 matching_braces braces
;
28936 if (!braces
.require_open (parser
))
28937 return error_mark_node
;
28939 cp_token
*expr_token
= cp_lexer_peek_token (parser
->lexer
);
28941 tree expr
= cp_parser_expression (parser
, NULL
, false, false);
28942 if (expr
== error_mark_node
)
28943 cp_parser_skip_to_closing_brace (parser
);
28945 if (!braces
.require_close (parser
))
28947 cp_parser_skip_to_end_of_statement (parser
);
28948 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28949 return error_mark_node
;
28952 /* If the expression was invalid, skip the remainder of the requirement. */
28953 if (!expr
|| expr
== error_mark_node
)
28955 cp_parser_skip_to_end_of_statement (parser
);
28956 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28957 return error_mark_node
;
28960 /* Parse the optional noexcept. */
28961 bool noexcept_p
= false;
28962 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_NOEXCEPT
))
28964 cp_lexer_consume_token (parser
->lexer
);
28968 /* Parse the optional trailing return type. */
28969 tree type
= NULL_TREE
;
28970 if (cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
))
28972 cp_lexer_consume_token (parser
->lexer
);
28973 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
28975 bool saved_result_type_constraint_p
= parser
->in_result_type_constraint_p
;
28976 parser
->in_result_type_constraint_p
= true;
28977 /* C++20 allows either a type-id or a type-constraint. Parsing
28978 a type-id will subsume the parsing for a type-constraint but
28979 allow for more syntactic forms (e.g., const C<T>*). */
28980 type
= cp_parser_trailing_type_id (parser
);
28981 parser
->in_result_type_constraint_p
= saved_result_type_constraint_p
;
28982 if (type
== error_mark_node
)
28983 return error_mark_node
;
28985 location_t type_loc
= make_location (tok
->location
, tok
->location
,
28988 /* Check that we haven't written something like 'const C<T>*'. */
28989 if (type_uses_auto (type
))
28991 if (!is_auto (type
))
28993 error_at (type_loc
,
28994 "result type is not a plain type-constraint");
28995 cp_parser_consume_semicolon_at_end_of_statement (parser
);
28996 return error_mark_node
;
28999 else if (!flag_concepts_ts
)
29000 /* P1452R2 removed the trailing-return-type option. */
29001 error_at (type_loc
,
29002 "return-type-requirement is not a type-constraint");
29005 location_t loc
= make_location (expr_token
->location
,
29006 braces
.open_location (),
29009 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29011 if (expr
== error_mark_node
|| type
== error_mark_node
)
29012 return error_mark_node
;
29014 return finish_compound_requirement (loc
, expr
, type
, noexcept_p
);
29017 /* Parse a nested requirement. This is the same as a requires clause.
29019 nested-requirement:
29023 cp_parser_nested_requirement (cp_parser
*parser
)
29025 gcc_assert (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_REQUIRES
));
29026 cp_token
*tok
= cp_lexer_consume_token (parser
->lexer
);
29027 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
29028 tree req
= cp_parser_constraint_expression (parser
);
29029 if (req
== error_mark_node
)
29030 cp_parser_skip_to_end_of_statement (parser
);
29031 loc
= make_location (loc
, tok
->location
, parser
->lexer
);
29032 cp_parser_consume_semicolon_at_end_of_statement (parser
);
29033 if (req
== error_mark_node
)
29034 return error_mark_node
;
29035 return finish_nested_requirement (loc
, req
);
29038 /* Support Functions */
29040 /* Return the appropriate prefer_type argument for lookup_name based on
29043 static inline LOOK_want
29044 prefer_type_arg (tag_types tag_type
)
29048 case none_type
: return LOOK_want::NORMAL
; // No preference.
29049 case scope_type
: return LOOK_want::TYPE_NAMESPACE
; // Type or namespace.
29050 default: return LOOK_want::TYPE
; // Type only.
29054 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
29055 NAME should have one of the representations used for an
29056 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
29057 is returned. If PARSER->SCOPE is a dependent type, then a
29058 SCOPE_REF is returned.
29060 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
29061 returned; the name was already resolved when the TEMPLATE_ID_EXPR
29062 was formed. Abstractly, such entities should not be passed to this
29063 function, because they do not need to be looked up, but it is
29064 simpler to check for this special case here, rather than at the
29067 In cases not explicitly covered above, this function returns a
29068 DECL, OVERLOAD, or baselink representing the result of the lookup.
29069 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
29072 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
29073 (e.g., "struct") that was used. In that case bindings that do not
29074 refer to types are ignored.
29076 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
29079 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
29082 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
29085 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
29086 TREE_LIST of candidates if name-lookup results in an ambiguity, and
29087 NULL_TREE otherwise. */
29090 cp_parser_lookup_name (cp_parser
*parser
, tree name
,
29091 enum tag_types tag_type
,
29094 bool check_dependency
,
29095 tree
*ambiguous_decls
,
29096 location_t name_location
)
29099 tree object_type
= parser
->context
->object_type
;
29101 /* Assume that the lookup will be unambiguous. */
29102 if (ambiguous_decls
)
29103 *ambiguous_decls
= NULL_TREE
;
29105 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
29106 no longer valid. Note that if we are parsing tentatively, and
29107 the parse fails, OBJECT_TYPE will be automatically restored. */
29108 parser
->context
->object_type
= NULL_TREE
;
29110 if (name
== error_mark_node
)
29111 return error_mark_node
;
29113 /* A template-id has already been resolved; there is no lookup to
29115 if (TREE_CODE (name
) == TEMPLATE_ID_EXPR
)
29117 if (BASELINK_P (name
))
29119 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name
))
29120 == TEMPLATE_ID_EXPR
);
29124 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
29125 it should already have been checked to make sure that the name
29126 used matches the type being destroyed. */
29127 if (TREE_CODE (name
) == BIT_NOT_EXPR
)
29131 /* Figure out to which type this destructor applies. */
29133 type
= parser
->scope
;
29134 else if (object_type
)
29135 type
= object_type
;
29137 type
= current_class_type
;
29138 /* If that's not a class type, there is no destructor. */
29139 if (!type
|| !CLASS_TYPE_P (type
))
29140 return error_mark_node
;
29142 /* In a non-static member function, check implicit this->. */
29143 if (current_class_ref
)
29144 return lookup_destructor (current_class_ref
, parser
->scope
, name
,
29145 tf_warning_or_error
);
29147 if (CLASSTYPE_LAZY_DESTRUCTOR (type
))
29148 lazily_declare_fn (sfk_destructor
, type
);
29150 if (tree dtor
= CLASSTYPE_DESTRUCTOR (type
))
29153 return error_mark_node
;
29156 /* By this point, the NAME should be an ordinary identifier. If
29157 the id-expression was a qualified name, the qualifying scope is
29158 stored in PARSER->SCOPE at this point. */
29159 gcc_assert (identifier_p (name
));
29161 /* Perform the lookup. */
29166 if (parser
->scope
== error_mark_node
)
29167 return error_mark_node
;
29169 /* If the SCOPE is dependent, the lookup must be deferred until
29170 the template is instantiated -- unless we are explicitly
29171 looking up names in uninstantiated templates. Even then, we
29172 cannot look up the name if the scope is not a class type; it
29173 might, for example, be a template type parameter. */
29174 dependent_p
= (TYPE_P (parser
->scope
)
29175 && dependent_scope_p (parser
->scope
));
29176 if ((check_dependency
|| !CLASS_TYPE_P (parser
->scope
))
29178 /* Defer lookup. */
29179 decl
= error_mark_node
;
29182 tree pushed_scope
= NULL_TREE
;
29184 /* If PARSER->SCOPE is a dependent type, then it must be a
29185 class type, and we must not be checking dependencies;
29186 otherwise, we would have processed this lookup above. So
29187 that PARSER->SCOPE is not considered a dependent base by
29188 lookup_member, we must enter the scope here. */
29190 pushed_scope
= push_scope (parser
->scope
);
29192 /* If the PARSER->SCOPE is a template specialization, it
29193 may be instantiated during name lookup. In that case,
29194 errors may be issued. Even if we rollback the current
29195 tentative parse, those errors are valid. */
29196 decl
= lookup_qualified_name (parser
->scope
, name
,
29197 prefer_type_arg (tag_type
),
29198 /*complain=*/true);
29200 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
29201 lookup result and the nested-name-specifier nominates a class C:
29202 * if the name specified after the nested-name-specifier, when
29203 looked up in C, is the injected-class-name of C (Clause 9), or
29204 * if the name specified after the nested-name-specifier is the
29205 same as the identifier or the simple-template-id's template-
29206 name in the last component of the nested-name-specifier,
29207 the name is instead considered to name the constructor of
29208 class C. [ Note: for example, the constructor is not an
29209 acceptable lookup result in an elaborated-type-specifier so
29210 the constructor would not be used in place of the
29211 injected-class-name. --end note ] Such a constructor name
29212 shall be used only in the declarator-id of a declaration that
29213 names a constructor or in a using-declaration. */
29214 if (tag_type
== none_type
29215 && DECL_SELF_REFERENCE_P (decl
)
29216 && same_type_p (DECL_CONTEXT (decl
), parser
->scope
))
29217 decl
= lookup_qualified_name (parser
->scope
, ctor_identifier
,
29218 prefer_type_arg (tag_type
),
29219 /*complain=*/true);
29222 pop_scope (pushed_scope
);
29225 /* If the scope is a dependent type and either we deferred lookup or
29226 we did lookup but didn't find the name, rememeber the name. */
29227 if (decl
== error_mark_node
&& TYPE_P (parser
->scope
)
29228 && dependent_type_p (parser
->scope
))
29234 /* The resolution to Core Issue 180 says that `struct
29235 A::B' should be considered a type-name, even if `A'
29237 type
= make_typename_type (parser
->scope
, name
, tag_type
,
29238 /*complain=*/tf_error
);
29239 if (type
!= error_mark_node
)
29240 decl
= TYPE_NAME (type
);
29242 else if (is_template
29243 && (cp_parser_next_token_ends_template_argument_p (parser
)
29244 || cp_lexer_next_token_is (parser
->lexer
,
29246 decl
= make_unbound_class_template (parser
->scope
,
29248 /*complain=*/tf_error
);
29250 decl
= build_qualified_name (/*type=*/NULL_TREE
,
29251 parser
->scope
, name
,
29254 parser
->qualifying_scope
= parser
->scope
;
29255 parser
->object_scope
= NULL_TREE
;
29257 else if (object_type
)
29259 /* Look up the name in the scope of the OBJECT_TYPE, unless the
29260 OBJECT_TYPE is not a class. */
29261 if (CLASS_TYPE_P (object_type
))
29262 /* If the OBJECT_TYPE is a template specialization, it may
29263 be instantiated during name lookup. In that case, errors
29264 may be issued. Even if we rollback the current tentative
29265 parse, those errors are valid. */
29266 decl
= lookup_member (object_type
,
29269 /*prefer_type=*/tag_type
!= none_type
,
29270 tf_warning_or_error
);
29275 /* Look it up in the enclosing context. DR 141: When looking for a
29276 template-name after -> or ., only consider class templates. */
29277 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
29278 /* DR 141: When looking in the
29279 current enclosing context for a
29280 template-name after -> or ., only
29281 consider class templates. */
29282 : is_template
? LOOK_want::TYPE
29283 : prefer_type_arg (tag_type
));
29284 parser
->object_scope
= object_type
;
29285 parser
->qualifying_scope
= NULL_TREE
;
29289 decl
= lookup_name (name
, is_namespace
? LOOK_want::NAMESPACE
29290 : prefer_type_arg (tag_type
));
29291 parser
->qualifying_scope
= NULL_TREE
;
29292 parser
->object_scope
= NULL_TREE
;
29295 /* If the lookup failed, let our caller know. */
29296 if (!decl
|| decl
== error_mark_node
)
29297 return error_mark_node
;
29299 /* Pull out the template from an injected-class-name (or multiple). */
29301 decl
= maybe_get_template_decl_from_type_decl (decl
);
29303 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
29304 if (TREE_CODE (decl
) == TREE_LIST
)
29306 if (ambiguous_decls
)
29307 *ambiguous_decls
= decl
;
29308 /* The error message we have to print is too complicated for
29309 cp_parser_error, so we incorporate its actions directly. */
29310 if (!cp_parser_simulate_error (parser
))
29312 error_at (name_location
, "reference to %qD is ambiguous",
29314 print_candidates (decl
);
29316 return error_mark_node
;
29319 gcc_assert (DECL_P (decl
)
29320 || TREE_CODE (decl
) == OVERLOAD
29321 || TREE_CODE (decl
) == SCOPE_REF
29322 || TREE_CODE (decl
) == UNBOUND_CLASS_TEMPLATE
29323 || BASELINK_P (decl
));
29325 /* If we have resolved the name of a member declaration, check to
29326 see if the declaration is accessible. When the name resolves to
29327 set of overloaded functions, accessibility is checked when
29328 overload resolution is done.
29330 During an explicit instantiation, access is not checked at all,
29331 as per [temp.explicit]. */
29333 check_accessibility_of_qualified_id (decl
, object_type
, parser
->scope
,
29334 tf_warning_or_error
);
29336 maybe_record_typedef_use (decl
);
29338 return cp_expr (decl
, name_location
);
29341 /* Like cp_parser_lookup_name, but for use in the typical case where
29342 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
29343 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
29346 cp_parser_lookup_name_simple (cp_parser
* parser
, tree name
, location_t location
)
29348 return cp_parser_lookup_name (parser
, name
,
29350 /*is_template=*/false,
29351 /*is_namespace=*/false,
29352 /*check_dependency=*/true,
29353 /*ambiguous_decls=*/NULL
,
29357 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
29358 the current context, return the TYPE_DECL. If TAG_NAME_P is
29359 true, the DECL indicates the class being defined in a class-head,
29360 or declared in an elaborated-type-specifier.
29362 Otherwise, return DECL. */
29365 cp_parser_maybe_treat_template_as_class (tree decl
, bool tag_name_p
)
29367 /* If the TEMPLATE_DECL is being declared as part of a class-head,
29368 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
29371 template <typename T> struct B;
29374 template <typename T> struct A::B {};
29376 Similarly, in an elaborated-type-specifier:
29378 namespace N { struct X{}; }
29381 template <typename T> friend struct N::X;
29384 However, if the DECL refers to a class type, and we are in
29385 the scope of the class, then the name lookup automatically
29386 finds the TYPE_DECL created by build_self_reference rather
29387 than a TEMPLATE_DECL. For example, in:
29389 template <class T> struct S {
29393 there is no need to handle such case. */
29395 if (DECL_CLASS_TEMPLATE_P (decl
) && tag_name_p
)
29396 return DECL_TEMPLATE_RESULT (decl
);
29401 /* If too many, or too few, template-parameter lists apply to the
29402 declarator, issue an error message. Returns TRUE if all went well,
29403 and FALSE otherwise. */
29406 cp_parser_check_declarator_template_parameters (cp_parser
* parser
,
29407 cp_declarator
*declarator
,
29408 location_t declarator_location
)
29410 switch (declarator
->kind
)
29414 unsigned num_templates
= 0;
29415 tree scope
= declarator
->u
.id
.qualifying_scope
;
29416 bool template_id_p
= false;
29419 num_templates
= num_template_headers_for_class (scope
);
29420 else if (TREE_CODE (declarator
->u
.id
.unqualified_name
)
29421 == TEMPLATE_ID_EXPR
)
29423 /* If the DECLARATOR has the form `X<y>' then it uses one
29424 additional level of template parameters. */
29426 template_id_p
= true;
29429 return cp_parser_check_template_parameters
29430 (parser
, num_templates
, template_id_p
, declarator_location
,
29437 case cdk_reference
:
29439 return (cp_parser_check_declarator_template_parameters
29440 (parser
, declarator
->declarator
, declarator_location
));
29447 gcc_unreachable ();
29452 /* NUM_TEMPLATES were used in the current declaration. If that is
29453 invalid, return FALSE and issue an error messages. Otherwise,
29454 return TRUE. If DECLARATOR is non-NULL, then we are checking a
29455 declarator and we can print more accurate diagnostics. */
29458 cp_parser_check_template_parameters (cp_parser
* parser
,
29459 unsigned num_templates
,
29460 bool template_id_p
,
29461 location_t location
,
29462 cp_declarator
*declarator
)
29464 /* If there are the same number of template classes and parameter
29465 lists, that's OK. */
29466 if (parser
->num_template_parameter_lists
== num_templates
)
29468 /* If there are more, but only one more, and the name ends in an identifier,
29469 then we are declaring a primary template. That's OK too. */
29471 && parser
->num_template_parameter_lists
== num_templates
+ 1)
29474 if (cp_parser_simulate_error (parser
))
29477 /* If there are more template classes than parameter lists, we have
29480 template <class T> void S<T>::R<T>::f (); */
29481 if (parser
->num_template_parameter_lists
< num_templates
)
29483 if (declarator
&& !current_function_decl
)
29484 error_at (location
, "specializing member %<%T::%E%> "
29485 "requires %<template<>%> syntax",
29486 declarator
->u
.id
.qualifying_scope
,
29487 declarator
->u
.id
.unqualified_name
);
29488 else if (declarator
)
29489 error_at (location
, "invalid declaration of %<%T::%E%>",
29490 declarator
->u
.id
.qualifying_scope
,
29491 declarator
->u
.id
.unqualified_name
);
29493 error_at (location
, "too few template-parameter-lists");
29496 /* Otherwise, there are too many template parameter lists. We have
29499 template <class T> template <class U> void S::f(); */
29500 error_at (location
, "too many template-parameter-lists");
29504 /* Parse an optional `::' token indicating that the following name is
29505 from the global namespace. If so, PARSER->SCOPE is set to the
29506 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
29507 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
29508 Returns the new value of PARSER->SCOPE, if the `::' token is
29509 present, and NULL_TREE otherwise. */
29512 cp_parser_global_scope_opt (cp_parser
* parser
, bool current_scope_valid_p
)
29516 /* Peek at the next token. */
29517 token
= cp_lexer_peek_token (parser
->lexer
);
29518 /* If we're looking at a `::' token then we're starting from the
29519 global namespace, not our current location. */
29520 if (token
->type
== CPP_SCOPE
)
29522 /* Consume the `::' token. */
29523 cp_lexer_consume_token (parser
->lexer
);
29524 /* Set the SCOPE so that we know where to start the lookup. */
29525 parser
->scope
= global_namespace
;
29526 parser
->qualifying_scope
= global_namespace
;
29527 parser
->object_scope
= NULL_TREE
;
29529 return parser
->scope
;
29531 else if (!current_scope_valid_p
)
29533 parser
->scope
= NULL_TREE
;
29534 parser
->qualifying_scope
= NULL_TREE
;
29535 parser
->object_scope
= NULL_TREE
;
29541 /* Returns TRUE if the upcoming token sequence is the start of a
29542 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
29543 declarator is preceded by the `friend' specifier. The parser flags FLAGS
29544 is used to control type-specifier parsing. */
29547 cp_parser_constructor_declarator_p (cp_parser
*parser
, cp_parser_flags flags
,
29550 bool constructor_p
;
29551 bool outside_class_specifier_p
;
29552 tree nested_name_specifier
;
29553 cp_token
*next_token
;
29555 /* The common case is that this is not a constructor declarator, so
29556 try to avoid doing lots of work if at all possible. It's not
29557 valid declare a constructor at function scope. */
29558 if (parser
->in_function_body
)
29560 /* And only certain tokens can begin a constructor declarator. */
29561 next_token
= cp_lexer_peek_token (parser
->lexer
);
29562 if (next_token
->type
!= CPP_NAME
29563 && next_token
->type
!= CPP_SCOPE
29564 && next_token
->type
!= CPP_NESTED_NAME_SPECIFIER
29565 /* DR 2237 (C++20 only): A simple-template-id is no longer valid as the
29566 declarator-id of a constructor or destructor. */
29567 && (next_token
->type
!= CPP_TEMPLATE_ID
|| cxx_dialect
>= cxx20
))
29570 /* Parse tentatively; we are going to roll back all of the tokens
29572 cp_parser_parse_tentatively (parser
);
29573 /* Assume that we are looking at a constructor declarator. */
29574 constructor_p
= true;
29576 /* Look for the optional `::' operator. */
29577 cp_parser_global_scope_opt (parser
,
29578 /*current_scope_valid_p=*/false);
29579 /* Look for the nested-name-specifier. */
29580 nested_name_specifier
29581 = (cp_parser_nested_name_specifier_opt (parser
,
29582 /*typename_keyword_p=*/false,
29583 /*check_dependency_p=*/false,
29585 /*is_declaration=*/false));
29587 /* Resolve the TYPENAME_TYPE, because the call above didn't do it. */
29588 if (nested_name_specifier
29589 && TREE_CODE (nested_name_specifier
) == TYPENAME_TYPE
)
29591 tree s
= resolve_typename_type (nested_name_specifier
,
29592 /*only_current_p=*/false);
29593 if (TREE_CODE (s
) != TYPENAME_TYPE
)
29594 nested_name_specifier
= s
;
29597 outside_class_specifier_p
= (!at_class_scope_p ()
29598 || !TYPE_BEING_DEFINED (current_class_type
)
29601 /* Outside of a class-specifier, there must be a
29602 nested-name-specifier. Except in C++17 mode, where we
29603 might be declaring a guiding declaration. */
29604 if (!nested_name_specifier
&& outside_class_specifier_p
29605 && cxx_dialect
< cxx17
)
29606 constructor_p
= false;
29607 else if (nested_name_specifier
== error_mark_node
)
29608 constructor_p
= false;
29610 /* If we have a class scope, this is easy; DR 147 says that S::S always
29611 names the constructor, and no other qualified name could. */
29612 if (constructor_p
&& nested_name_specifier
29613 && CLASS_TYPE_P (nested_name_specifier
))
29615 tree id
= cp_parser_unqualified_id (parser
,
29616 /*template_keyword_p=*/false,
29617 /*check_dependency_p=*/false,
29618 /*declarator_p=*/true,
29619 /*optional_p=*/false);
29620 if (is_overloaded_fn (id
))
29621 id
= DECL_NAME (get_first_fn (id
));
29622 if (!constructor_name_p (id
, nested_name_specifier
))
29623 constructor_p
= false;
29625 /* If we still think that this might be a constructor-declarator,
29626 look for a class-name. */
29627 else if (constructor_p
)
29631 template <typename T> struct S {
29635 we must recognize that the nested `S' names a class. */
29636 if (cxx_dialect
>= cxx17
)
29637 cp_parser_parse_tentatively (parser
);
29640 type_decl
= cp_parser_class_name (parser
,
29641 /*typename_keyword_p=*/false,
29642 /*template_keyword_p=*/false,
29644 /*check_dependency_p=*/false,
29645 /*class_head_p=*/false,
29646 /*is_declaration=*/false);
29648 if (cxx_dialect
>= cxx17
29649 && !cp_parser_parse_definitely (parser
))
29651 type_decl
= NULL_TREE
;
29652 tree tmpl
= cp_parser_template_name (parser
,
29653 /*template_keyword*/false,
29654 /*check_dependency_p*/false,
29655 /*is_declaration*/false,
29657 /*is_identifier*/NULL
);
29658 if (DECL_CLASS_TEMPLATE_P (tmpl
)
29659 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl
))
29660 /* It's a deduction guide, return true. */;
29662 cp_parser_simulate_error (parser
);
29665 /* If there was no class-name, then this is not a constructor.
29666 Otherwise, if we are in a class-specifier and we aren't
29667 handling a friend declaration, check that its type matches
29668 current_class_type (c++/38313). Note: error_mark_node
29669 is left alone for error recovery purposes. */
29670 constructor_p
= (!cp_parser_error_occurred (parser
)
29671 && (outside_class_specifier_p
29672 || type_decl
== NULL_TREE
29673 || type_decl
== error_mark_node
29674 || same_type_p (current_class_type
,
29675 TREE_TYPE (type_decl
))));
29677 /* If we're still considering a constructor, we have to see a `(',
29678 to begin the parameter-declaration-clause, followed by either a
29679 `)', an `...', or a decl-specifier. We need to check for a
29680 type-specifier to avoid being fooled into thinking that:
29684 is a constructor. (It is actually a function named `f' that
29685 takes one parameter (of type `int') and returns a value of type
29688 && !cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
29689 constructor_p
= false;
29692 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
29693 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_ELLIPSIS
)
29694 /* A parameter declaration begins with a decl-specifier,
29695 which is either the "attribute" keyword, a storage class
29696 specifier, or (usually) a type-specifier. */
29697 && (!cp_lexer_next_token_is_decl_specifier_keyword (parser
->lexer
)
29698 /* GNU attributes can actually appear both at the start of
29699 a parameter and parenthesized declarator.
29700 S (__attribute__((unused)) int);
29701 is a constructor, but
29702 S (__attribute__((unused)) foo) (int);
29703 is a function declaration. */
29704 || (cp_parser_allow_gnu_extensions_p (parser
)
29705 && cp_next_tokens_can_be_gnu_attribute_p (parser
)))
29706 /* A parameter declaration can also begin with [[attribute]]. */
29707 && !cp_next_tokens_can_be_std_attribute_p (parser
))
29710 tree pushed_scope
= NULL_TREE
;
29711 unsigned saved_num_template_parameter_lists
;
29713 if (cp_next_tokens_can_be_gnu_attribute_p (parser
))
29715 unsigned int n
= cp_parser_skip_gnu_attributes_opt (parser
, 1);
29717 cp_lexer_consume_token (parser
->lexer
);
29720 /* Names appearing in the type-specifier should be looked up
29721 in the scope of the class. */
29722 if (current_class_type
)
29724 else if (type_decl
)
29726 type
= TREE_TYPE (type_decl
);
29727 if (TREE_CODE (type
) == TYPENAME_TYPE
)
29729 type
= resolve_typename_type (type
,
29730 /*only_current_p=*/false);
29731 if (TREE_CODE (type
) == TYPENAME_TYPE
)
29733 cp_parser_abort_tentative_parse (parser
);
29737 pushed_scope
= push_scope (type
);
29740 /* Inside the constructor parameter list, surrounding
29741 template-parameter-lists do not apply. */
29742 saved_num_template_parameter_lists
29743 = parser
->num_template_parameter_lists
;
29744 parser
->num_template_parameter_lists
= 0;
29746 /* Look for the type-specifier. It's not optional, but its typename
29747 might be. Unless this is a friend declaration; we don't want to
29750 friend S (T::fn)(int);
29752 as a constructor, but with P0634, we might assume a type when
29753 looking for the type-specifier. It is actually a function named
29754 `T::fn' that takes one parameter (of type `int') and returns a
29755 value of type `S'. Constructors can be friends, but they must
29756 use a qualified name.
29758 Parse with an empty set of declaration specifiers since we're
29759 trying to match a decl-specifier-seq of the first parameter.
29760 This must be non-null so that cp_parser_simple_type_specifier
29761 will recognize a constrained placeholder type such as:
29762 'C<int> auto' where C is a type concept. */
29763 cp_decl_specifier_seq ctor_specs
;
29764 clear_decl_specs (&ctor_specs
);
29765 cp_parser_type_specifier (parser
,
29766 (friend_p
? CP_PARSER_FLAGS_NONE
29767 : (flags
& ~CP_PARSER_FLAGS_OPTIONAL
)),
29768 /*decl_specs=*/&ctor_specs
,
29769 /*is_declarator=*/true,
29770 /*declares_class_or_enum=*/NULL
,
29771 /*is_cv_qualifier=*/NULL
);
29773 parser
->num_template_parameter_lists
29774 = saved_num_template_parameter_lists
;
29776 /* Leave the scope of the class. */
29778 pop_scope (pushed_scope
);
29780 constructor_p
= !cp_parser_error_occurred (parser
);
29784 /* We did not really want to consume any tokens. */
29785 cp_parser_abort_tentative_parse (parser
);
29787 return constructor_p
;
29790 /* Parse the definition of the function given by the DECL_SPECIFIERS,
29791 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
29792 they must be performed once we are in the scope of the function.
29794 Returns the function defined. */
29797 cp_parser_function_definition_from_specifiers_and_declarator
29798 (cp_parser
* parser
,
29799 cp_decl_specifier_seq
*decl_specifiers
,
29801 const cp_declarator
*declarator
)
29806 /* Begin the function-definition. */
29807 success_p
= start_function (decl_specifiers
, declarator
, attributes
);
29809 /* The things we're about to see are not directly qualified by any
29810 template headers we've seen thus far. */
29811 reset_specialization ();
29813 /* If there were names looked up in the decl-specifier-seq that we
29814 did not check, check them now. We must wait until we are in the
29815 scope of the function to perform the checks, since the function
29816 might be a friend. */
29817 perform_deferred_access_checks (tf_warning_or_error
);
29821 cp_finalize_omp_declare_simd (parser
, current_function_decl
);
29822 parser
->omp_declare_simd
= NULL
;
29823 cp_finalize_oacc_routine (parser
, current_function_decl
, true);
29824 parser
->oacc_routine
= NULL
;
29829 /* Skip the entire function. */
29830 cp_parser_skip_to_end_of_block_or_statement (parser
);
29831 fn
= error_mark_node
;
29833 else if (DECL_INITIAL (current_function_decl
) != error_mark_node
)
29835 /* Seen already, skip it. An error message has already been output. */
29836 cp_parser_skip_to_end_of_block_or_statement (parser
);
29837 fn
= current_function_decl
;
29838 current_function_decl
= NULL_TREE
;
29839 /* If this is a function from a class, pop the nested class. */
29840 if (current_class_name
)
29841 pop_nested_class ();
29846 if (DECL_DECLARED_INLINE_P (current_function_decl
))
29847 tv
= TV_PARSE_INLINE
;
29849 tv
= TV_PARSE_FUNC
;
29851 fn
= cp_parser_function_definition_after_declarator (parser
,
29852 /*inline_p=*/false);
29859 /* Parse the part of a function-definition that follows the
29860 declarator. INLINE_P is TRUE iff this function is an inline
29861 function defined within a class-specifier.
29863 Returns the function defined. */
29866 cp_parser_function_definition_after_declarator (cp_parser
* parser
,
29870 bool saved_in_unbraced_linkage_specification_p
;
29871 bool saved_in_function_body
;
29872 unsigned saved_num_template_parameter_lists
;
29874 bool fully_implicit_function_template_p
29875 = parser
->fully_implicit_function_template_p
;
29876 parser
->fully_implicit_function_template_p
= false;
29877 tree implicit_template_parms
29878 = parser
->implicit_template_parms
;
29879 parser
->implicit_template_parms
= 0;
29880 cp_binding_level
* implicit_template_scope
29881 = parser
->implicit_template_scope
;
29882 parser
->implicit_template_scope
= 0;
29884 saved_in_function_body
= parser
->in_function_body
;
29885 parser
->in_function_body
= true;
29886 /* If the next token is `return', then the code may be trying to
29887 make use of the "named return value" extension that G++ used to
29889 token
= cp_lexer_peek_token (parser
->lexer
);
29890 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_RETURN
))
29892 /* Consume the `return' keyword. */
29893 cp_lexer_consume_token (parser
->lexer
);
29894 /* Look for the identifier that indicates what value is to be
29896 cp_parser_identifier (parser
);
29897 /* Issue an error message. */
29898 error_at (token
->location
,
29899 "named return values are no longer supported");
29900 /* Skip tokens until we reach the start of the function body. */
29903 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
29904 if (token
->type
== CPP_OPEN_BRACE
29905 || token
->type
== CPP_EOF
29906 || token
->type
== CPP_PRAGMA_EOL
)
29908 cp_lexer_consume_token (parser
->lexer
);
29911 /* The `extern' in `extern "C" void f () { ... }' does not apply to
29912 anything declared inside `f'. */
29913 saved_in_unbraced_linkage_specification_p
29914 = parser
->in_unbraced_linkage_specification_p
;
29915 parser
->in_unbraced_linkage_specification_p
= false;
29916 /* Inside the function, surrounding template-parameter-lists do not
29918 saved_num_template_parameter_lists
29919 = parser
->num_template_parameter_lists
;
29920 parser
->num_template_parameter_lists
= 0;
29922 /* If the next token is `try', `__transaction_atomic', or
29923 `__transaction_relaxed`, then we are looking at either function-try-block
29924 or function-transaction-block. Note that all of these include the
29926 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_ATOMIC
))
29927 cp_parser_function_transaction (parser
, RID_TRANSACTION_ATOMIC
);
29928 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
29929 RID_TRANSACTION_RELAXED
))
29930 cp_parser_function_transaction (parser
, RID_TRANSACTION_RELAXED
);
29931 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
29932 cp_parser_function_try_block (parser
);
29934 cp_parser_ctor_initializer_opt_and_function_body
29935 (parser
, /*in_function_try_block=*/false);
29937 /* Finish the function. */
29938 fn
= finish_function (inline_p
);
29942 && TYPE_P (DECL_CONTEXT (fn
))
29943 && (DECL_DECLARED_INLINE_P (fn
)
29944 || processing_template_decl
))
29945 set_defining_module (fn
);
29947 /* Generate code for it, if necessary. */
29948 expand_or_defer_fn (fn
);
29949 /* Restore the saved values. */
29950 parser
->in_unbraced_linkage_specification_p
29951 = saved_in_unbraced_linkage_specification_p
;
29952 parser
->num_template_parameter_lists
29953 = saved_num_template_parameter_lists
;
29954 parser
->in_function_body
= saved_in_function_body
;
29956 parser
->fully_implicit_function_template_p
29957 = fully_implicit_function_template_p
;
29958 parser
->implicit_template_parms
29959 = implicit_template_parms
;
29960 parser
->implicit_template_scope
29961 = implicit_template_scope
;
29963 if (parser
->fully_implicit_function_template_p
)
29964 finish_fully_implicit_template (parser
, /*member_decl_opt=*/0);
29969 /* Parse a template-declaration body (following argument list). */
29972 cp_parser_template_declaration_after_parameters (cp_parser
* parser
,
29973 tree parameter_list
,
29976 tree decl
= NULL_TREE
;
29977 bool friend_p
= false;
29979 /* We just processed one more parameter list. */
29980 ++parser
->num_template_parameter_lists
;
29982 /* Get the deferred access checks from the parameter list. These
29983 will be checked once we know what is being declared, as for a
29984 member template the checks must be performed in the scope of the
29985 class containing the member. */
29986 vec
<deferred_access_check
, va_gc
> *checks
= get_deferred_access_checks ();
29988 /* Tentatively parse for a new template parameter list, which can either be
29989 the template keyword or a template introduction. */
29990 if (cp_parser_template_declaration_after_export (parser
, member_p
))
29992 else if (cxx_dialect
>= cxx11
29993 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_USING
))
29994 decl
= cp_parser_alias_declaration (parser
);
29995 else if (cxx_dialect
>= cxx20
/* Implies flag_concept. */
29996 && cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CONCEPT
)
29997 && !cp_lexer_nth_token_is_keyword (parser
->lexer
, 2, RID_BOOL
))
29998 /* Allow 'concept bool' to be handled as per the TS. */
29999 decl
= cp_parser_concept_definition (parser
);
30002 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30003 decl
= cp_parser_single_declaration (parser
,
30006 /*explicit_specialization_p=*/false,
30009 /* If this is a member template declaration, let the front
30011 if (member_p
&& !friend_p
&& decl
)
30013 if (TREE_CODE (decl
) == TYPE_DECL
)
30014 cp_parser_check_access_in_redeclaration (decl
, token
->location
);
30016 decl
= finish_member_template_decl (decl
);
30018 else if (friend_p
&& decl
30019 && DECL_DECLARES_TYPE_P (decl
))
30020 make_friend_class (current_class_type
, TREE_TYPE (decl
),
30021 /*complain=*/true);
30023 /* We are done with the current parameter list. */
30024 --parser
->num_template_parameter_lists
;
30026 pop_deferring_access_checks ();
30029 finish_template_decl (parameter_list
);
30031 /* Check the template arguments for a literal operator template. */
30033 && DECL_DECLARES_FUNCTION_P (decl
)
30034 && UDLIT_OPER_P (DECL_NAME (decl
)))
30037 if (parameter_list
== NULL_TREE
)
30041 int num_parms
= TREE_VEC_LENGTH (parameter_list
);
30042 if (num_parms
== 1)
30044 tree parm_list
= TREE_VEC_ELT (parameter_list
, 0);
30045 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
30046 if (TREE_CODE (parm
) != PARM_DECL
)
30048 else if (MAYBE_CLASS_TYPE_P (TREE_TYPE (parm
))
30049 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
30050 /* OK, C++20 string literal operator template. We don't need
30051 to warn in lower dialects here because we will have already
30052 warned about the template parameter. */;
30053 else if (TREE_TYPE (parm
) != char_type_node
30054 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
30057 else if (num_parms
== 2 && cxx_dialect
>= cxx14
)
30059 tree parm_type
= TREE_VEC_ELT (parameter_list
, 0);
30060 tree type
= INNERMOST_TEMPLATE_PARMS (parm_type
);
30061 tree parm_list
= TREE_VEC_ELT (parameter_list
, 1);
30062 tree parm
= INNERMOST_TEMPLATE_PARMS (parm_list
);
30063 if (TREE_CODE (parm
) != PARM_DECL
30064 || TREE_TYPE (parm
) != TREE_TYPE (type
)
30065 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm
)))
30068 /* http://cplusplus.github.io/EWG/ewg-active.html#66 */
30069 pedwarn (DECL_SOURCE_LOCATION (decl
), OPT_Wpedantic
,
30070 "ISO C++ did not adopt string literal operator templa"
30071 "tes taking an argument pack of characters");
30078 if (cxx_dialect
> cxx17
)
30079 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
30080 "template %qD has invalid parameter list; expected "
30081 "non-type template parameter pack %<<char...>%> or "
30082 "single non-type parameter of class type",
30085 error_at (DECL_SOURCE_LOCATION (decl
), "literal operator "
30086 "template %qD has invalid parameter list; expected "
30087 "non-type template parameter pack %<<char...>%>",
30092 /* Register member declarations. */
30093 if (member_p
&& !friend_p
&& decl
&& !DECL_CLASS_TEMPLATE_P (decl
))
30094 finish_member_declaration (decl
);
30095 /* If DECL is a function template, we must return to parse it later.
30096 (Even though there is no definition, there might be default
30097 arguments that need handling.) */
30098 if (member_p
&& decl
30099 && DECL_DECLARES_FUNCTION_P (decl
))
30100 vec_safe_push (unparsed_funs_with_definitions
, decl
);
30103 /* Parse a template introduction header for a template-declaration. Returns
30104 false if tentative parse fails. */
30107 cp_parser_template_introduction (cp_parser
* parser
, bool member_p
)
30109 cp_parser_parse_tentatively (parser
);
30111 tree saved_scope
= parser
->scope
;
30112 tree saved_object_scope
= parser
->object_scope
;
30113 tree saved_qualifying_scope
= parser
->qualifying_scope
;
30115 cp_token
*start_token
= cp_lexer_peek_token (parser
->lexer
);
30117 /* Look for the optional `::' operator. */
30118 cp_parser_global_scope_opt (parser
,
30119 /*current_scope_valid_p=*/false);
30120 /* Look for the nested-name-specifier. */
30121 cp_parser_nested_name_specifier_opt (parser
,
30122 /*typename_keyword_p=*/false,
30123 /*check_dependency_p=*/true,
30125 /*is_declaration=*/false);
30127 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30128 tree concept_name
= cp_parser_identifier (parser
);
30130 /* Look up the concept for which we will be matching
30131 template parameters. */
30132 tree tmpl_decl
= cp_parser_lookup_name_simple (parser
, concept_name
,
30134 parser
->scope
= saved_scope
;
30135 parser
->object_scope
= saved_object_scope
;
30136 parser
->qualifying_scope
= saved_qualifying_scope
;
30138 if (concept_name
== error_mark_node
30139 || (seen_error () && !concept_definition_p (tmpl_decl
)))
30140 cp_parser_simulate_error (parser
);
30142 /* Look for opening brace for introduction. */
30143 matching_braces braces
;
30144 braces
.require_open (parser
);
30145 location_t open_loc
= input_location
;
30147 if (!cp_parser_parse_definitely (parser
))
30150 push_deferring_access_checks (dk_deferred
);
30152 /* Build vector of placeholder parameters and grab
30153 matching identifiers. */
30154 tree introduction_list
= cp_parser_introduction_list (parser
);
30156 /* Look for closing brace for introduction. */
30157 if (!braces
.require_close (parser
))
30160 /* The introduction-list shall not be empty. */
30161 int nargs
= TREE_VEC_LENGTH (introduction_list
);
30164 /* In cp_parser_introduction_list we have already issued an error. */
30168 if (tmpl_decl
== error_mark_node
)
30170 cp_parser_name_lookup_error (parser
, concept_name
, tmpl_decl
, NLE_NULL
,
30175 /* Build and associate the constraint. */
30176 location_t introduction_loc
= make_location (open_loc
,
30177 start_token
->location
,
30179 tree parms
= finish_template_introduction (tmpl_decl
,
30182 if (parms
&& parms
!= error_mark_node
)
30184 if (!flag_concepts_ts
)
30185 pedwarn (introduction_loc
, 0, "template-introductions"
30186 " are not part of C++20 concepts; use %qs to enable",
30189 cp_parser_template_declaration_after_parameters (parser
, parms
,
30194 if (parms
== NULL_TREE
)
30195 error_at (token
->location
, "no matching concept for template-introduction");
30200 /* Parse a normal template-declaration following the template keyword. */
30203 cp_parser_explicit_template_declaration (cp_parser
* parser
, bool member_p
)
30205 tree parameter_list
;
30206 bool need_lang_pop
;
30207 location_t location
= input_location
;
30209 /* Look for the `<' token. */
30210 if (!cp_parser_require (parser
, CPP_LESS
, RT_LESS
))
30212 if (at_class_scope_p () && current_function_decl
)
30214 /* 14.5.2.2 [temp.mem]
30216 A local class shall not have member templates. */
30217 error_at (location
,
30218 "invalid declaration of member template in local class");
30219 cp_parser_skip_to_end_of_block_or_statement (parser
);
30224 A template ... shall not have C linkage. */
30225 if (current_lang_name
== lang_name_c
)
30227 error_at (location
, "template with C linkage");
30228 maybe_show_extern_c_location ();
30229 /* Give it C++ linkage to avoid confusing other parts of the
30231 push_lang_context (lang_name_cplusplus
);
30232 need_lang_pop
= true;
30235 need_lang_pop
= false;
30237 /* We cannot perform access checks on the template parameter
30238 declarations until we know what is being declared, just as we
30239 cannot check the decl-specifier list. */
30240 push_deferring_access_checks (dk_deferred
);
30242 /* If the next token is `>', then we have an invalid
30243 specialization. Rather than complain about an invalid template
30244 parameter, issue an error message here. */
30245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
))
30247 cp_parser_error (parser
, "invalid explicit specialization");
30248 begin_specialization ();
30249 parameter_list
= NULL_TREE
;
30253 /* Parse the template parameters. */
30254 parameter_list
= cp_parser_template_parameter_list (parser
);
30257 /* Look for the `>'. */
30258 cp_parser_skip_to_end_of_template_parameter_list (parser
);
30260 /* Manage template requirements */
30263 tree reqs
= get_shorthand_constraints (current_template_parms
);
30264 if (tree treqs
= cp_parser_requires_clause_opt (parser
, false))
30265 reqs
= combine_constraint_expressions (reqs
, treqs
);
30266 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
30269 cp_parser_template_declaration_after_parameters (parser
, parameter_list
,
30272 /* For the erroneous case of a template with C linkage, we pushed an
30273 implicit C++ linkage scope; exit that scope now. */
30275 pop_lang_context ();
30278 /* Parse a template-declaration, assuming that the `export' (and
30279 `extern') keywords, if present, has already been scanned. MEMBER_P
30280 is as for cp_parser_template_declaration. */
30283 cp_parser_template_declaration_after_export (cp_parser
* parser
, bool member_p
)
30285 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
30287 cp_lexer_consume_token (parser
->lexer
);
30288 cp_parser_explicit_template_declaration (parser
, member_p
);
30291 else if (flag_concepts
)
30292 return cp_parser_template_introduction (parser
, member_p
);
30297 /* Perform the deferred access checks from a template-parameter-list.
30298 CHECKS is a TREE_LIST of access checks, as returned by
30299 get_deferred_access_checks. */
30302 cp_parser_perform_template_parameter_access_checks (vec
<deferred_access_check
, va_gc
> *checks
)
30304 ++processing_template_parmlist
;
30305 perform_access_checks (checks
, tf_warning_or_error
);
30306 --processing_template_parmlist
;
30309 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
30310 `function-definition' sequence that follows a template header.
30311 If MEMBER_P is true, this declaration appears in a class scope.
30313 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
30314 *FRIEND_P is set to TRUE iff the declaration is a friend. */
30317 cp_parser_single_declaration (cp_parser
* parser
,
30318 vec
<deferred_access_check
, va_gc
> *checks
,
30320 bool explicit_specialization_p
,
30323 int declares_class_or_enum
;
30324 tree decl
= NULL_TREE
;
30325 cp_decl_specifier_seq decl_specifiers
;
30326 bool function_definition_p
= false;
30327 cp_token
*decl_spec_token_start
;
30329 /* This function is only used when processing a template
30331 gcc_assert (innermost_scope_kind () == sk_template_parms
30332 || innermost_scope_kind () == sk_template_spec
);
30334 /* Defer access checks until we know what is being declared. */
30335 push_deferring_access_checks (dk_deferred
);
30337 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
30339 decl_spec_token_start
= cp_lexer_peek_token (parser
->lexer
);
30340 cp_parser_decl_specifier_seq (parser
,
30341 (CP_PARSER_FLAGS_OPTIONAL
30342 | CP_PARSER_FLAGS_TYPENAME_OPTIONAL
),
30344 &declares_class_or_enum
);
30346 *friend_p
= cp_parser_friend_p (&decl_specifiers
);
30348 /* There are no template typedefs. */
30349 if (decl_spec_seq_has_spec_p (&decl_specifiers
, ds_typedef
))
30351 error_at (decl_spec_token_start
->location
,
30352 "template declaration of %<typedef%>");
30353 decl
= error_mark_node
;
30356 /* Gather up the access checks that occurred the
30357 decl-specifier-seq. */
30358 stop_deferring_access_checks ();
30360 /* Check for the declaration of a template class. */
30361 if (declares_class_or_enum
)
30363 if (cp_parser_declares_only_class_p (parser
)
30364 || (declares_class_or_enum
& 2))
30366 /* If this is a declaration, but not a definition, associate
30367 any constraints with the type declaration. Constraints
30368 are associated with definitions in cp_parser_class_specifier. */
30369 if (declares_class_or_enum
== 1)
30370 associate_classtype_constraints (decl_specifiers
.type
);
30372 decl
= shadow_tag (&decl_specifiers
);
30377 friend template <typename T> struct A<T>::B;
30380 A<T>::B will be represented by a TYPENAME_TYPE, and
30381 therefore not recognized by shadow_tag. */
30382 if (friend_p
&& *friend_p
30384 && decl_specifiers
.type
30385 && TYPE_P (decl_specifiers
.type
))
30386 decl
= decl_specifiers
.type
;
30388 if (decl
&& decl
!= error_mark_node
)
30389 decl
= TYPE_NAME (decl
);
30391 decl
= error_mark_node
;
30393 /* Perform access checks for template parameters. */
30394 cp_parser_perform_template_parameter_access_checks (checks
);
30396 /* Give a helpful diagnostic for
30397 template <class T> struct A { } a;
30398 if we aren't already recovering from an error. */
30399 if (!cp_parser_declares_only_class_p (parser
)
30402 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
30403 "a class template declaration must not declare "
30405 cp_parser_skip_to_end_of_block_or_statement (parser
);
30411 /* Complain about missing 'typename' or other invalid type names. */
30412 if (!decl_specifiers
.any_type_specifiers_p
30413 && cp_parser_parse_and_diagnose_invalid_type_name (parser
))
30415 /* cp_parser_parse_and_diagnose_invalid_type_name calls
30416 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
30417 the rest of this declaration. */
30418 decl
= error_mark_node
;
30422 /* If it's not a template class, try for a template function. If
30423 the next token is a `;', then this declaration does not declare
30424 anything. But, if there were errors in the decl-specifiers, then
30425 the error might well have come from an attempted class-specifier.
30426 In that case, there's no need to warn about a missing declarator. */
30428 && (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
)
30429 || decl_specifiers
.type
!= error_mark_node
))
30431 decl
= cp_parser_init_declarator (parser
,
30432 CP_PARSER_FLAGS_TYPENAME_OPTIONAL
,
30435 /*function_definition_allowed_p=*/true,
30437 declares_class_or_enum
,
30438 &function_definition_p
,
30441 /* 7.1.1-1 [dcl.stc]
30443 A storage-class-specifier shall not be specified in an explicit
30444 specialization... */
30446 && explicit_specialization_p
30447 && decl_specifiers
.storage_class
!= sc_none
)
30449 error_at (decl_spec_token_start
->location
,
30450 "explicit template specialization cannot have a storage class");
30451 decl
= error_mark_node
;
30454 if (decl
&& VAR_P (decl
))
30455 check_template_variable (decl
);
30458 /* Look for a trailing `;' after the declaration. */
30459 if (!function_definition_p
30460 && (decl
== error_mark_node
30461 || !cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
)))
30462 cp_parser_skip_to_end_of_block_or_statement (parser
);
30465 pop_deferring_access_checks ();
30467 /* Clear any current qualification; whatever comes next is the start
30468 of something new. */
30469 parser
->scope
= NULL_TREE
;
30470 parser
->qualifying_scope
= NULL_TREE
;
30471 parser
->object_scope
= NULL_TREE
;
30476 /* Parse a cast-expression that is not the operand of a unary "&". */
30479 cp_parser_simple_cast_expression (cp_parser
*parser
)
30481 return cp_parser_cast_expression (parser
, /*address_p=*/false,
30482 /*cast_p=*/false, /*decltype*/false, NULL
);
30485 /* Parse a functional cast to TYPE. Returns an expression
30486 representing the cast. */
30489 cp_parser_functional_cast (cp_parser
* parser
, tree type
)
30491 vec
<tree
, va_gc
> *vec
;
30492 tree expression_list
;
30496 location_t start_loc
= input_location
;
30499 type
= error_mark_node
;
30501 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
30503 cp_lexer_set_source_position (parser
->lexer
);
30504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
30505 expression_list
= cp_parser_braced_list (parser
, &nonconst_p
);
30506 CONSTRUCTOR_IS_DIRECT_INIT (expression_list
) = 1;
30507 if (TREE_CODE (type
) == TYPE_DECL
)
30508 type
= TREE_TYPE (type
);
30510 cast
= finish_compound_literal (type
, expression_list
,
30511 tf_warning_or_error
, fcl_functional
);
30512 /* Create a location of the form:
30515 with caret == start at the start of the type name,
30516 finishing at the closing brace. */
30517 location_t combined_loc
= make_location (start_loc
, start_loc
,
30519 cast
.set_location (combined_loc
);
30524 vec
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
30526 /*allow_expansion_p=*/true,
30527 /*non_constant_p=*/NULL
);
30529 expression_list
= error_mark_node
;
30532 expression_list
= build_tree_list_vec (vec
);
30533 release_tree_vector (vec
);
30536 /* Create a location of the form:
30539 with caret == start at the start of the type name,
30540 finishing at the closing paren. */
30541 location_t combined_loc
= make_location (start_loc
, start_loc
,
30543 cast
= build_functional_cast (combined_loc
, type
, expression_list
,
30544 tf_warning_or_error
);
30546 /* [expr.const]/1: In an integral constant expression "only type
30547 conversions to integral or enumeration type can be used". */
30548 if (TREE_CODE (type
) == TYPE_DECL
)
30549 type
= TREE_TYPE (type
);
30550 if (cast
!= error_mark_node
30551 && !cast_valid_in_integral_constant_expression_p (type
)
30552 && cp_parser_non_integral_constant_expression (parser
,
30554 return error_mark_node
;
30559 /* Save the tokens that make up the body of a member function defined
30560 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
30561 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
30562 specifiers applied to the declaration. Returns the FUNCTION_DECL
30563 for the member function. */
30566 cp_parser_save_member_function_body (cp_parser
* parser
,
30567 cp_decl_specifier_seq
*decl_specifiers
,
30568 cp_declarator
*declarator
,
30574 bool function_try_block
= false;
30576 /* Create the FUNCTION_DECL. */
30577 fn
= grokmethod (decl_specifiers
, declarator
, attributes
);
30578 cp_finalize_omp_declare_simd (parser
, fn
);
30579 cp_finalize_oacc_routine (parser
, fn
, true);
30580 /* If something went badly wrong, bail out now. */
30581 if (fn
== error_mark_node
)
30583 /* If there's a function-body, skip it. */
30584 if (cp_parser_token_starts_function_definition_p
30585 (cp_lexer_peek_token (parser
->lexer
)))
30586 cp_parser_skip_to_end_of_block_or_statement (parser
);
30587 return error_mark_node
;
30590 /* Remember it, if there are default args to post process. */
30591 cp_parser_save_default_args (parser
, fn
);
30593 /* Save away the tokens that make up the body of the
30595 first
= parser
->lexer
->next_token
;
30597 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRANSACTION_RELAXED
))
30598 cp_lexer_consume_token (parser
->lexer
);
30599 else if (cp_lexer_next_token_is_keyword (parser
->lexer
,
30600 RID_TRANSACTION_ATOMIC
))
30602 cp_lexer_consume_token (parser
->lexer
);
30603 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
30604 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
)
30605 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_SQUARE
)
30606 && (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
)
30607 || cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_KEYWORD
))
30608 && cp_lexer_nth_token_is (parser
->lexer
, 4, CPP_CLOSE_SQUARE
)
30609 && cp_lexer_nth_token_is (parser
->lexer
, 5, CPP_CLOSE_SQUARE
))
30611 cp_lexer_consume_token (parser
->lexer
);
30612 cp_lexer_consume_token (parser
->lexer
);
30613 cp_lexer_consume_token (parser
->lexer
);
30614 cp_lexer_consume_token (parser
->lexer
);
30615 cp_lexer_consume_token (parser
->lexer
);
30618 while (cp_next_tokens_can_be_gnu_attribute_p (parser
)
30619 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
30621 cp_lexer_consume_token (parser
->lexer
);
30622 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
30627 /* Handle function try blocks. */
30628 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
30630 cp_lexer_consume_token (parser
->lexer
);
30631 function_try_block
= true;
30633 /* We can have braced-init-list mem-initializers before the fn body. */
30634 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
30636 cp_lexer_consume_token (parser
->lexer
);
30637 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_BRACE
))
30639 /* cache_group will stop after an un-nested { } pair, too. */
30640 if (cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, /*depth=*/0))
30643 /* variadic mem-inits have ... after the ')'. */
30644 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
30645 cp_lexer_consume_token (parser
->lexer
);
30648 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
30649 /* Handle function try blocks. */
30650 if (function_try_block
)
30651 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_CATCH
))
30652 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
30653 last
= parser
->lexer
->next_token
;
30655 /* Save away the inline definition; we will process it when the
30656 class is complete. */
30657 DECL_PENDING_INLINE_INFO (fn
) = cp_token_cache_new (first
, last
);
30658 DECL_PENDING_INLINE_P (fn
) = 1;
30660 /* We need to know that this was defined in the class, so that
30661 friend templates are handled correctly. */
30662 DECL_INITIALIZED_IN_CLASS_P (fn
) = 1;
30664 /* Add FN to the queue of functions to be parsed later. */
30665 vec_safe_push (unparsed_funs_with_definitions
, fn
);
30670 /* Save the tokens that make up the in-class initializer for a non-static
30671 data member. Returns a DEFERRED_PARSE. */
30674 cp_parser_save_nsdmi (cp_parser
* parser
)
30676 return cp_parser_cache_defarg (parser
, /*nsdmi=*/true);
30679 /* Parse a template-argument-list, as well as the trailing ">" (but
30680 not the opening "<"). See cp_parser_template_argument_list for the
30684 cp_parser_enclosed_template_argument_list (cp_parser
* parser
)
30688 tree saved_qualifying_scope
;
30689 tree saved_object_scope
;
30690 bool saved_greater_than_is_operator_p
;
30694 When parsing a template-id, the first non-nested `>' is taken as
30695 the end of the template-argument-list rather than a greater-than
30697 saved_greater_than_is_operator_p
30698 = parser
->greater_than_is_operator_p
;
30699 parser
->greater_than_is_operator_p
= false;
30700 /* Parsing the argument list may modify SCOPE, so we save it
30702 saved_scope
= parser
->scope
;
30703 saved_qualifying_scope
= parser
->qualifying_scope
;
30704 saved_object_scope
= parser
->object_scope
;
30705 /* We need to evaluate the template arguments, even though this
30706 template-id may be nested within a "sizeof". */
30708 /* Parse the template-argument-list itself. */
30709 if (cp_lexer_next_token_is (parser
->lexer
, CPP_GREATER
)
30710 || cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
30711 arguments
= NULL_TREE
;
30713 arguments
= cp_parser_template_argument_list (parser
);
30714 /* Look for the `>' that ends the template-argument-list. If we find
30715 a '>>' instead, it's probably just a typo. */
30716 if (cp_lexer_next_token_is (parser
->lexer
, CPP_RSHIFT
))
30718 if (cxx_dialect
!= cxx98
)
30720 /* In C++0x, a `>>' in a template argument list or cast
30721 expression is considered to be two separate `>'
30722 tokens. So, change the current token to a `>', but don't
30723 consume it: it will be consumed later when the outer
30724 template argument list (or cast expression) is parsed.
30725 Note that this replacement of `>' for `>>' is necessary
30726 even if we are parsing tentatively: in the tentative
30727 case, after calling
30728 cp_parser_enclosed_template_argument_list we will always
30729 throw away all of the template arguments and the first
30730 closing `>', either because the template argument list
30731 was erroneous or because we are replacing those tokens
30732 with a CPP_TEMPLATE_ID token. The second `>' (which will
30733 not have been thrown away) is needed either to close an
30734 outer template argument list or to complete a new-style
30736 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30737 token
->type
= CPP_GREATER
;
30739 else if (!saved_greater_than_is_operator_p
)
30741 /* If we're in a nested template argument list, the '>>' has
30742 to be a typo for '> >'. We emit the error message, but we
30743 continue parsing and we push a '>' as next token, so that
30744 the argument list will be parsed correctly. Note that the
30745 global source location is still on the token before the
30746 '>>', so we need to say explicitly where we want it. */
30747 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
30748 gcc_rich_location
richloc (token
->location
);
30749 richloc
.add_fixit_replace ("> >");
30750 error_at (&richloc
, "%<>>%> should be %<> >%> "
30751 "within a nested template argument list");
30753 token
->type
= CPP_GREATER
;
30757 /* If this is not a nested template argument list, the '>>'
30758 is a typo for '>'. Emit an error message and continue.
30759 Same deal about the token location, but here we can get it
30760 right by consuming the '>>' before issuing the diagnostic. */
30761 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
30762 error_at (token
->location
,
30763 "spurious %<>>%>, use %<>%> to terminate "
30764 "a template argument list");
30768 cp_parser_skip_to_end_of_template_parameter_list (parser
);
30769 /* The `>' token might be a greater-than operator again now. */
30770 parser
->greater_than_is_operator_p
30771 = saved_greater_than_is_operator_p
;
30772 /* Restore the SAVED_SCOPE. */
30773 parser
->scope
= saved_scope
;
30774 parser
->qualifying_scope
= saved_qualifying_scope
;
30775 parser
->object_scope
= saved_object_scope
;
30780 /* MEMBER_FUNCTION is a member function, or a friend. If default
30781 arguments, or the body of the function have not yet been parsed,
30785 cp_parser_late_parsing_for_member (cp_parser
* parser
, tree member_function
)
30787 timevar_push (TV_PARSE_INMETH
);
30788 /* If this member is a template, get the underlying
30790 if (DECL_FUNCTION_TEMPLATE_P (member_function
))
30791 member_function
= DECL_TEMPLATE_RESULT (member_function
);
30793 /* There should not be any class definitions in progress at this
30794 point; the bodies of members are only parsed outside of all class
30796 gcc_assert (parser
->num_classes_being_defined
== 0);
30797 /* While we're parsing the member functions we might encounter more
30798 classes. We want to handle them right away, but we don't want
30799 them getting mixed up with functions that are currently in the
30801 push_unparsed_function_queues (parser
);
30803 /* Make sure that any template parameters are in scope. */
30804 maybe_begin_member_template_processing (member_function
);
30806 /* If the body of the function has not yet been parsed, parse it
30808 if (DECL_PENDING_INLINE_P (member_function
))
30810 tree function_scope
;
30811 cp_token_cache
*tokens
;
30813 /* The function is no longer pending; we are processing it. */
30814 tokens
= DECL_PENDING_INLINE_INFO (member_function
);
30815 DECL_PENDING_INLINE_INFO (member_function
) = NULL
;
30816 DECL_PENDING_INLINE_P (member_function
) = 0;
30818 /* If this is a local class, enter the scope of the containing
30820 function_scope
= current_function_decl
;
30821 if (function_scope
)
30822 push_function_context ();
30824 /* Push the body of the function onto the lexer stack. */
30825 cp_parser_push_lexer_for_tokens (parser
, tokens
);
30827 /* Let the front end know that we going to be defining this
30829 start_preparsed_function (member_function
, NULL_TREE
,
30830 SF_PRE_PARSED
| SF_INCLASS_INLINE
);
30832 /* #pragma omp declare reduction needs special parsing. */
30833 if (DECL_OMP_DECLARE_REDUCTION_P (member_function
))
30835 parser
->lexer
->in_pragma
= true;
30836 cp_parser_omp_declare_reduction_exprs (member_function
, parser
);
30837 finish_function (/*inline_p=*/true);
30838 cp_check_omp_declare_reduction (member_function
);
30841 /* Now, parse the body of the function. */
30842 cp_parser_function_definition_after_declarator (parser
,
30843 /*inline_p=*/true);
30845 /* Leave the scope of the containing function. */
30846 if (function_scope
)
30847 pop_function_context ();
30848 cp_parser_pop_lexer (parser
);
30851 /* Remove any template parameters from the symbol table. */
30852 maybe_end_member_template_processing ();
30854 /* Restore the queue. */
30855 pop_unparsed_function_queues (parser
);
30856 timevar_pop (TV_PARSE_INMETH
);
30859 /* If DECL contains any default args, remember it on the unparsed
30860 functions queue. */
30863 cp_parser_save_default_args (cp_parser
* parser
, tree decl
)
30867 for (probe
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
30869 probe
= TREE_CHAIN (probe
))
30870 if (TREE_PURPOSE (probe
))
30872 cp_default_arg_entry entry
= {current_class_type
, decl
};
30873 vec_safe_push (unparsed_funs_with_default_args
, entry
);
30877 /* Remember if there is a noexcept-specifier to post process. */
30878 tree spec
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (decl
));
30879 if (UNPARSED_NOEXCEPT_SPEC_P (spec
))
30880 vec_safe_push (unparsed_noexcepts
, decl
);
30883 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
30884 which is either a FIELD_DECL or PARM_DECL. Parse it and return
30885 the result. For a PARM_DECL, PARMTYPE is the corresponding type
30886 from the parameter-type-list. */
30889 cp_parser_late_parse_one_default_arg (cp_parser
*parser
, tree decl
,
30890 tree default_arg
, tree parmtype
)
30892 cp_token_cache
*tokens
;
30896 if (default_arg
== error_mark_node
)
30897 return error_mark_node
;
30899 /* Push the saved tokens for the default argument onto the parser's
30901 tokens
= DEFPARSE_TOKENS (default_arg
);
30902 cp_parser_push_lexer_for_tokens (parser
, tokens
);
30904 start_lambda_scope (decl
);
30906 /* Parse the default argument. */
30907 parsed_arg
= cp_parser_initializer (parser
, &dummy
, &dummy
);
30908 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg
))
30909 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS
);
30911 finish_lambda_scope ();
30913 if (parsed_arg
== error_mark_node
)
30914 cp_parser_skip_to_end_of_statement (parser
);
30916 if (!processing_template_decl
)
30918 /* In a non-template class, check conversions now. In a template,
30919 we'll wait and instantiate these as needed. */
30920 if (TREE_CODE (decl
) == PARM_DECL
)
30921 parsed_arg
= check_default_argument (parmtype
, parsed_arg
,
30922 tf_warning_or_error
);
30923 else if (maybe_reject_flexarray_init (decl
, parsed_arg
))
30924 parsed_arg
= error_mark_node
;
30926 parsed_arg
= digest_nsdmi_init (decl
, parsed_arg
, tf_warning_or_error
);
30929 /* If the token stream has not been completely used up, then
30930 there was extra junk after the end of the default
30932 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
30934 if (TREE_CODE (decl
) == PARM_DECL
)
30935 cp_parser_error (parser
, "expected %<,%>");
30937 cp_parser_error (parser
, "expected %<;%>");
30940 /* Revert to the main lexer. */
30941 cp_parser_pop_lexer (parser
);
30946 /* FIELD is a non-static data member with an initializer which we saved for
30947 later; parse it now. */
30950 cp_parser_late_parsing_nsdmi (cp_parser
*parser
, tree field
)
30954 maybe_begin_member_template_processing (field
);
30956 push_unparsed_function_queues (parser
);
30957 def
= cp_parser_late_parse_one_default_arg (parser
, field
,
30958 DECL_INITIAL (field
),
30960 pop_unparsed_function_queues (parser
);
30962 maybe_end_member_template_processing ();
30964 DECL_INITIAL (field
) = def
;
30967 /* FN is a FUNCTION_DECL which may contains a parameter with an
30968 unparsed DEFERRED_PARSE. Parse the default args now. This function
30969 assumes that the current scope is the scope in which the default
30970 argument should be processed. */
30973 cp_parser_late_parsing_default_args (cp_parser
*parser
, tree fn
)
30975 unsigned char saved_local_variables_forbidden_p
;
30977 /* While we're parsing the default args, we might (due to the
30978 statement expression extension) encounter more classes. We want
30979 to handle them right away, but we don't want them getting mixed
30980 up with default args that are currently in the queue. */
30981 push_unparsed_function_queues (parser
);
30983 /* Local variable names (and the `this' keyword) may not appear
30984 in a default argument. */
30985 saved_local_variables_forbidden_p
= parser
->local_variables_forbidden_p
;
30986 parser
->local_variables_forbidden_p
= LOCAL_VARS_AND_THIS_FORBIDDEN
;
30988 push_defarg_context (fn
);
30990 begin_scope (sk_function_parms
, fn
);
30992 /* Gather the PARM_DECLs into a vec so we can keep track of them when
30993 pushdecl clears DECL_CHAIN. */
30994 releasing_vec parms
;
30995 for (tree parmdecl
= DECL_ARGUMENTS (fn
); parmdecl
;
30996 parmdecl
= DECL_CHAIN (parmdecl
))
30997 vec_safe_push (parms
, parmdecl
);
30999 tree parm
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
31001 parm
&& parm
!= void_list_node
;
31002 parm
= TREE_CHAIN (parm
),
31005 tree default_arg
= TREE_PURPOSE (parm
);
31008 tree parmdecl
= parms
[i
];
31009 pushdecl (parmdecl
);
31014 if (TREE_CODE (default_arg
) != DEFERRED_PARSE
)
31015 /* This can happen for a friend declaration for a function
31016 already declared with default arguments. */
31020 = cp_parser_late_parse_one_default_arg (parser
, parmdecl
,
31022 TREE_VALUE (parm
));
31023 TREE_PURPOSE (parm
) = parsed_arg
;
31025 /* Update any instantiations we've already created. */
31026 for (tree copy
: DEFPARSE_INSTANTIATIONS (default_arg
))
31027 TREE_PURPOSE (copy
) = parsed_arg
;
31030 pop_bindings_and_leave_scope ();
31032 /* Restore DECL_CHAINs after clobbering by pushdecl. */
31034 for (int i
= parms
->length () - 1; i
>= 0; --i
)
31036 DECL_CHAIN (parms
[i
]) = parm
;
31040 pop_defarg_context ();
31042 /* Make sure no default arg is missing. */
31043 check_default_args (fn
);
31045 /* Restore the state of local_variables_forbidden_p. */
31046 parser
->local_variables_forbidden_p
= saved_local_variables_forbidden_p
;
31048 /* Restore the queue. */
31049 pop_unparsed_function_queues (parser
);
31052 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
31054 sizeof ... ( identifier )
31056 where the 'sizeof' token has already been consumed. */
31059 cp_parser_sizeof_pack (cp_parser
*parser
)
31061 /* Consume the `...'. */
31062 cp_lexer_consume_token (parser
->lexer
);
31063 maybe_warn_variadic_templates ();
31065 matching_parens parens
;
31066 bool paren
= cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
);
31068 parens
.consume_open (parser
);
31070 permerror (cp_lexer_peek_token (parser
->lexer
)->location
,
31071 "%<sizeof...%> argument must be surrounded by parentheses");
31073 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
31074 tree name
= cp_parser_identifier (parser
);
31075 if (name
== error_mark_node
)
31076 return error_mark_node
;
31077 /* The name is not qualified. */
31078 parser
->scope
= NULL_TREE
;
31079 parser
->qualifying_scope
= NULL_TREE
;
31080 parser
->object_scope
= NULL_TREE
;
31081 tree expr
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
31082 if (expr
== error_mark_node
)
31083 cp_parser_name_lookup_error (parser
, name
, expr
, NLE_NULL
,
31085 if (TREE_CODE (expr
) == TYPE_DECL
|| TREE_CODE (expr
) == TEMPLATE_DECL
)
31086 expr
= TREE_TYPE (expr
);
31087 else if (TREE_CODE (expr
) == CONST_DECL
)
31088 expr
= DECL_INITIAL (expr
);
31089 expr
= make_pack_expansion (expr
);
31090 PACK_EXPANSION_SIZEOF_P (expr
) = true;
31093 parens
.require_close (parser
);
31098 /* Parse the operand of `sizeof' (or a similar operator). Returns
31099 either a TYPE or an expression, depending on the form of the
31100 input. The KEYWORD indicates which kind of expression we have
31104 cp_parser_sizeof_operand (cp_parser
* parser
, enum rid keyword
)
31106 tree expr
= NULL_TREE
;
31107 const char *saved_message
;
31108 const char *saved_message_arg
;
31109 bool saved_integral_constant_expression_p
;
31110 bool saved_non_integral_constant_expression_p
;
31112 /* If it's a `...', then we are computing the length of a parameter
31114 if (keyword
== RID_SIZEOF
31115 && cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
31116 return cp_parser_sizeof_pack (parser
);
31118 /* Types cannot be defined in a `sizeof' expression. Save away the
31120 saved_message
= parser
->type_definition_forbidden_message
;
31121 saved_message_arg
= parser
->type_definition_forbidden_message_arg
;
31122 parser
->type_definition_forbidden_message
31123 = G_("types may not be defined in %qs expressions");
31124 parser
->type_definition_forbidden_message_arg
31125 = IDENTIFIER_POINTER (ridpointers
[keyword
]);
31127 /* The restrictions on constant-expressions do not apply inside
31128 sizeof expressions. */
31129 saved_integral_constant_expression_p
31130 = parser
->integral_constant_expression_p
;
31131 saved_non_integral_constant_expression_p
31132 = parser
->non_integral_constant_expression_p
;
31133 parser
->integral_constant_expression_p
= false;
31135 /* Do not actually evaluate the expression. */
31136 ++cp_unevaluated_operand
;
31137 ++c_inhibit_evaluation_warnings
;
31138 /* If it's a `(', then we might be looking at the type-id
31140 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
31142 tree type
= NULL_TREE
;
31144 tentative_firewall
firewall (parser
);
31146 /* We can't be sure yet whether we're looking at a type-id or an
31148 cp_parser_parse_tentatively (parser
);
31150 matching_parens parens
;
31151 parens
.consume_open (parser
);
31153 /* Note: as a GNU Extension, compound literals are considered
31154 postfix-expressions as they are in C99, so they are valid
31155 arguments to sizeof. See comment in cp_parser_cast_expression
31157 if (cp_parser_compound_literal_p (parser
))
31158 cp_parser_simulate_error (parser
);
31161 bool saved_in_type_id_in_expr_p
= parser
->in_type_id_in_expr_p
;
31162 parser
->in_type_id_in_expr_p
= true;
31163 /* Look for the type-id. */
31164 type
= cp_parser_type_id (parser
);
31165 /* Look for the closing `)'. */
31166 parens
.require_close (parser
);
31167 parser
->in_type_id_in_expr_p
= saved_in_type_id_in_expr_p
;
31170 /* If all went well, then we're done. */
31171 if (cp_parser_parse_definitely (parser
))
31175 /* Commit to the tentative_firewall so we get syntax errors. */
31176 cp_parser_commit_to_tentative_parse (parser
);
31178 expr
= cp_parser_unary_expression (parser
);
31182 expr
= cp_parser_unary_expression (parser
);
31184 /* Go back to evaluating expressions. */
31185 --cp_unevaluated_operand
;
31186 --c_inhibit_evaluation_warnings
;
31188 /* And restore the old one. */
31189 parser
->type_definition_forbidden_message
= saved_message
;
31190 parser
->type_definition_forbidden_message_arg
= saved_message_arg
;
31191 parser
->integral_constant_expression_p
31192 = saved_integral_constant_expression_p
;
31193 parser
->non_integral_constant_expression_p
31194 = saved_non_integral_constant_expression_p
;
31199 /* If the current declaration has no declarator, return true. */
31202 cp_parser_declares_only_class_p (cp_parser
*parser
)
31204 /* If the next token is a `;' or a `,' then there is no
31206 return (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
31207 || cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
));
31210 /* Update the DECL_SPECS to reflect the storage class indicated by
31214 cp_parser_set_storage_class (cp_parser
*parser
,
31215 cp_decl_specifier_seq
*decl_specs
,
31219 cp_storage_class storage_class
;
31221 if (parser
->in_unbraced_linkage_specification_p
)
31223 error_at (token
->location
, "invalid use of %qD in linkage specification",
31224 ridpointers
[keyword
]);
31227 else if (decl_specs
->storage_class
!= sc_none
)
31229 decl_specs
->conflicting_specifiers_p
= true;
31233 if ((keyword
== RID_EXTERN
|| keyword
== RID_STATIC
)
31234 && decl_spec_seq_has_spec_p (decl_specs
, ds_thread
)
31235 && decl_specs
->gnu_thread_keyword_p
)
31237 pedwarn (decl_specs
->locations
[ds_thread
], 0,
31238 "%<__thread%> before %qD", ridpointers
[keyword
]);
31244 storage_class
= sc_auto
;
31247 storage_class
= sc_register
;
31250 storage_class
= sc_static
;
31253 storage_class
= sc_extern
;
31256 storage_class
= sc_mutable
;
31259 gcc_unreachable ();
31261 decl_specs
->storage_class
= storage_class
;
31262 set_and_check_decl_spec_loc (decl_specs
, ds_storage_class
, token
);
31264 /* A storage class specifier cannot be applied alongside a typedef
31265 specifier. If there is a typedef specifier present then set
31266 conflicting_specifiers_p which will trigger an error later
31267 on in grokdeclarator. */
31268 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
))
31269 decl_specs
->conflicting_specifiers_p
= true;
31272 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
31273 is true, the type is a class or enum definition. */
31276 cp_parser_set_decl_spec_type (cp_decl_specifier_seq
*decl_specs
,
31279 bool type_definition_p
)
31281 decl_specs
->any_specifiers_p
= true;
31283 /* If the user tries to redeclare bool, char8_t, char16_t, char32_t, or
31284 wchar_t (with, for example, in "typedef int wchar_t;") we remember that
31285 this is what happened. In system headers, we ignore these
31286 declarations so that G++ can work with system headers that are not
31288 if (decl_spec_seq_has_spec_p (decl_specs
, ds_typedef
)
31289 && !type_definition_p
31290 && (type_spec
== boolean_type_node
31291 || type_spec
== char8_type_node
31292 || type_spec
== char16_type_node
31293 || type_spec
== char32_type_node
31294 || type_spec
== wchar_type_node
)
31295 && (decl_specs
->type
31296 || decl_spec_seq_has_spec_p (decl_specs
, ds_long
)
31297 || decl_spec_seq_has_spec_p (decl_specs
, ds_short
)
31298 || decl_spec_seq_has_spec_p (decl_specs
, ds_unsigned
)
31299 || decl_spec_seq_has_spec_p (decl_specs
, ds_signed
)))
31301 decl_specs
->redefined_builtin_type
= type_spec
;
31302 set_and_check_decl_spec_loc (decl_specs
,
31303 ds_redefined_builtin_type_spec
,
31305 if (!decl_specs
->type
)
31307 decl_specs
->type
= type_spec
;
31308 decl_specs
->type_definition_p
= false;
31309 set_and_check_decl_spec_loc (decl_specs
,ds_type_spec
, token
);
31312 else if (decl_specs
->type
)
31313 decl_specs
->multiple_types_p
= true;
31316 decl_specs
->type
= type_spec
;
31317 decl_specs
->type_definition_p
= type_definition_p
;
31318 decl_specs
->redefined_builtin_type
= NULL_TREE
;
31319 set_and_check_decl_spec_loc (decl_specs
, ds_type_spec
, token
);
31323 /* True iff TOKEN is the GNU keyword __thread. */
31326 token_is__thread (cp_token
*token
)
31328 gcc_assert (token
->keyword
== RID_THREAD
);
31329 return id_equal (token
->u
.value
, "__thread");
31332 /* Set the location for a declarator specifier and check if it is
31335 DECL_SPECS is the sequence of declarator specifiers onto which to
31338 DS is the single declarator specifier to set which location is to
31339 be set onto the existing sequence of declarators.
31341 LOCATION is the location for the declarator specifier to
31345 set_and_check_decl_spec_loc (cp_decl_specifier_seq
*decl_specs
,
31346 cp_decl_spec ds
, cp_token
*token
)
31348 gcc_assert (ds
< ds_last
);
31350 if (decl_specs
== NULL
)
31353 location_t location
= token
->location
;
31355 if (decl_specs
->locations
[ds
] == 0)
31357 decl_specs
->locations
[ds
] = location
;
31358 if (ds
== ds_thread
)
31359 decl_specs
->gnu_thread_keyword_p
= token_is__thread (token
);
31365 if (decl_specs
->locations
[ds_long_long
] != 0)
31366 error_at (location
,
31367 "%<long long long%> is too long for GCC");
31370 decl_specs
->locations
[ds_long_long
] = location
;
31371 pedwarn_cxx98 (location
,
31373 "ISO C++ 1998 does not support %<long long%>");
31376 else if (ds
== ds_thread
)
31378 bool gnu
= token_is__thread (token
);
31379 gcc_rich_location
richloc (location
);
31380 if (gnu
!= decl_specs
->gnu_thread_keyword_p
)
31382 richloc
.add_range (decl_specs
->locations
[ds_thread
]);
31383 error_at (&richloc
,
31384 "both %<__thread%> and %<thread_local%> specified");
31388 richloc
.add_fixit_remove ();
31389 error_at (&richloc
, "duplicate %qD", token
->u
.value
);
31394 static const char *const decl_spec_names
[] = {
31413 gcc_rich_location
richloc (location
);
31414 richloc
.add_fixit_remove ();
31415 error_at (&richloc
, "duplicate %qs", decl_spec_names
[ds
]);
31420 /* Return true iff the declarator specifier DS is present in the
31421 sequence of declarator specifiers DECL_SPECS. */
31424 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq
* decl_specs
,
31427 gcc_assert (ds
< ds_last
);
31429 if (decl_specs
== NULL
)
31432 return decl_specs
->locations
[ds
] != 0;
31435 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
31436 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
31439 cp_parser_friend_p (const cp_decl_specifier_seq
*decl_specifiers
)
31441 return decl_spec_seq_has_spec_p (decl_specifiers
, ds_friend
);
31444 /* Issue an error message indicating that TOKEN_DESC was expected.
31445 If KEYWORD is true, it indicated this function is called by
31446 cp_parser_require_keword and the required token can only be
31447 a indicated keyword.
31449 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31450 within any error as the location of an "opening" token matching
31451 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31452 RT_CLOSE_PAREN). */
31455 cp_parser_required_error (cp_parser
*parser
,
31456 required_token token_desc
,
31458 location_t matching_location
)
31460 if (cp_parser_simulate_error (parser
))
31463 const char *gmsgid
= NULL
;
31464 switch (token_desc
)
31467 gmsgid
= G_("expected %<new%>");
31470 gmsgid
= G_("expected %<delete%>");
31473 gmsgid
= G_("expected %<return%>");
31476 gmsgid
= G_("expected %<while%>");
31479 gmsgid
= G_("expected %<extern%>");
31481 case RT_STATIC_ASSERT
:
31482 gmsgid
= G_("expected %<static_assert%>");
31485 gmsgid
= G_("expected %<decltype%>");
31488 gmsgid
= G_("expected %<operator%>");
31491 gmsgid
= G_("expected %<class%>");
31494 gmsgid
= G_("expected %<template%>");
31497 gmsgid
= G_("expected %<namespace%>");
31500 gmsgid
= G_("expected %<using%>");
31503 gmsgid
= G_("expected %<asm%>");
31506 gmsgid
= G_("expected %<try%>");
31509 gmsgid
= G_("expected %<catch%>");
31512 gmsgid
= G_("expected %<throw%>");
31515 gmsgid
= G_("expected %<auto%>");
31518 gmsgid
= G_("expected %<__label__%>");
31521 gmsgid
= G_("expected %<@try%>");
31523 case RT_AT_SYNCHRONIZED
:
31524 gmsgid
= G_("expected %<@synchronized%>");
31527 gmsgid
= G_("expected %<@throw%>");
31529 case RT_TRANSACTION_ATOMIC
:
31530 gmsgid
= G_("expected %<__transaction_atomic%>");
31532 case RT_TRANSACTION_RELAXED
:
31533 gmsgid
= G_("expected %<__transaction_relaxed%>");
31536 gmsgid
= G_("expected %<co_yield%>");
31542 if (!gmsgid
&& !keyword
)
31544 switch (token_desc
)
31547 gmsgid
= G_("expected %<;%>");
31549 case RT_OPEN_PAREN
:
31550 gmsgid
= G_("expected %<(%>");
31552 case RT_CLOSE_BRACE
:
31553 gmsgid
= G_("expected %<}%>");
31555 case RT_OPEN_BRACE
:
31556 gmsgid
= G_("expected %<{%>");
31558 case RT_CLOSE_SQUARE
:
31559 gmsgid
= G_("expected %<]%>");
31561 case RT_OPEN_SQUARE
:
31562 gmsgid
= G_("expected %<[%>");
31565 gmsgid
= G_("expected %<,%>");
31568 gmsgid
= G_("expected %<::%>");
31571 gmsgid
= G_("expected %<<%>");
31574 gmsgid
= G_("expected %<>%>");
31577 gmsgid
= G_("expected %<=%>");
31580 gmsgid
= G_("expected %<...%>");
31583 gmsgid
= G_("expected %<*%>");
31586 gmsgid
= G_("expected %<~%>");
31589 gmsgid
= G_("expected %<:%>");
31591 case RT_COLON_SCOPE
:
31592 gmsgid
= G_("expected %<:%> or %<::%>");
31594 case RT_CLOSE_PAREN
:
31595 gmsgid
= G_("expected %<)%>");
31597 case RT_COMMA_CLOSE_PAREN
:
31598 gmsgid
= G_("expected %<,%> or %<)%>");
31600 case RT_PRAGMA_EOL
:
31601 gmsgid
= G_("expected end of line");
31604 gmsgid
= G_("expected identifier");
31607 gmsgid
= G_("expected selection-statement");
31610 gmsgid
= G_("expected iteration-statement");
31613 gmsgid
= G_("expected jump-statement");
31616 gmsgid
= G_("expected class-key");
31618 case RT_CLASS_TYPENAME_TEMPLATE
:
31619 gmsgid
= G_("expected %<class%>, %<typename%>, or %<template%>");
31622 gcc_unreachable ();
31627 cp_parser_error_1 (parser
, gmsgid
, token_desc
, matching_location
);
31631 /* If the next token is of the indicated TYPE, consume it. Otherwise,
31632 issue an error message indicating that TOKEN_DESC was expected.
31634 Returns the token consumed, if the token had the appropriate type.
31635 Otherwise, returns NULL.
31637 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
31638 within any error as the location of an "opening" token matching
31639 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
31640 RT_CLOSE_PAREN). */
31643 cp_parser_require (cp_parser
* parser
,
31644 enum cpp_ttype type
,
31645 required_token token_desc
,
31646 location_t matching_location
)
31648 if (cp_lexer_next_token_is (parser
->lexer
, type
))
31649 return cp_lexer_consume_token (parser
->lexer
);
31652 /* Output the MESSAGE -- unless we're parsing tentatively. */
31653 if (!cp_parser_simulate_error (parser
))
31654 cp_parser_required_error (parser
, token_desc
, /*keyword=*/false,
31655 matching_location
);
31660 /* An error message is produced if the next token is not '>'.
31661 All further tokens are skipped until the desired token is
31662 found or '{', '}', ';' or an unbalanced ')' or ']'. */
31665 cp_parser_skip_to_end_of_template_parameter_list (cp_parser
* parser
)
31667 /* Current level of '< ... >'. */
31668 unsigned level
= 0;
31669 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
31670 unsigned nesting_depth
= 0;
31672 /* Are we ready, yet? If not, issue error message. */
31673 if (cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
))
31676 /* Skip tokens until the desired token is found. */
31679 /* Peek at the next token. */
31680 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
31683 if (!nesting_depth
)
31688 if (cxx_dialect
== cxx98
)
31689 /* C++0x views the `>>' operator as two `>' tokens, but
31692 else if (!nesting_depth
&& level
-- == 0)
31694 /* We've hit a `>>' where the first `>' closes the
31695 template argument list, and the second `>' is
31696 spurious. Just consume the `>>' and stop; we've
31697 already produced at least one error. */
31698 cp_lexer_consume_token (parser
->lexer
);
31701 /* Fall through for C++0x, so we handle the second `>' in
31703 gcc_fallthrough ();
31706 if (!nesting_depth
&& level
-- == 0)
31708 /* We've reached the token we want, consume it and stop. */
31709 cp_lexer_consume_token (parser
->lexer
);
31714 case CPP_OPEN_PAREN
:
31715 case CPP_OPEN_SQUARE
:
31719 case CPP_CLOSE_PAREN
:
31720 case CPP_CLOSE_SQUARE
:
31721 if (nesting_depth
-- == 0)
31726 case CPP_PRAGMA_EOL
:
31727 case CPP_SEMICOLON
:
31728 case CPP_OPEN_BRACE
:
31729 case CPP_CLOSE_BRACE
:
31730 /* The '>' was probably forgotten, don't look further. */
31737 /* Consume this token. */
31738 cp_lexer_consume_token (parser
->lexer
);
31742 /* If the next token is the indicated keyword, consume it. Otherwise,
31743 issue an error message indicating that TOKEN_DESC was expected.
31745 Returns the token consumed, if the token had the appropriate type.
31746 Otherwise, returns NULL. */
31749 cp_parser_require_keyword (cp_parser
* parser
,
31751 required_token token_desc
)
31753 cp_token
*token
= cp_parser_require (parser
, CPP_KEYWORD
, token_desc
);
31755 if (token
&& token
->keyword
!= keyword
)
31757 cp_parser_required_error (parser
, token_desc
, /*keyword=*/true,
31765 /* Returns TRUE iff TOKEN is a token that can begin the body of a
31766 function-definition. */
31769 cp_parser_token_starts_function_definition_p (cp_token
* token
)
31771 return (/* An ordinary function-body begins with an `{'. */
31772 token
->type
== CPP_OPEN_BRACE
31773 /* A ctor-initializer begins with a `:'. */
31774 || token
->type
== CPP_COLON
31775 /* A function-try-block begins with `try'. */
31776 || token
->keyword
== RID_TRY
31777 /* A function-transaction-block begins with `__transaction_atomic'
31778 or `__transaction_relaxed'. */
31779 || token
->keyword
== RID_TRANSACTION_ATOMIC
31780 || token
->keyword
== RID_TRANSACTION_RELAXED
31781 /* The named return value extension begins with `return'. */
31782 || token
->keyword
== RID_RETURN
);
31785 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
31789 cp_parser_next_token_starts_class_definition_p (cp_parser
*parser
)
31793 token
= cp_lexer_peek_token (parser
->lexer
);
31794 return (token
->type
== CPP_OPEN_BRACE
31795 || (token
->type
== CPP_COLON
31796 && !parser
->colon_doesnt_start_class_def_p
));
31799 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
31800 C++0x) ending a template-argument. */
31803 cp_parser_next_token_ends_template_argument_p (cp_parser
*parser
)
31807 token
= cp_lexer_peek_token (parser
->lexer
);
31808 return (token
->type
== CPP_COMMA
31809 || token
->type
== CPP_GREATER
31810 || token
->type
== CPP_ELLIPSIS
31811 || ((cxx_dialect
!= cxx98
) && token
->type
== CPP_RSHIFT
));
31814 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
31815 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
31818 cp_parser_nth_token_starts_template_argument_list_p (cp_parser
* parser
,
31823 token
= cp_lexer_peek_nth_token (parser
->lexer
, n
);
31824 if (token
->type
== CPP_LESS
)
31826 /* Check for the sequence `<::' in the original code. It would be lexed as
31827 `[:', where `[' is a digraph, and there is no whitespace before
31829 if (token
->type
== CPP_OPEN_SQUARE
&& token
->flags
& DIGRAPH
)
31832 token2
= cp_lexer_peek_nth_token (parser
->lexer
, n
+1);
31833 if (token2
->type
== CPP_COLON
&& !(token2
->flags
& PREV_WHITE
))
31839 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
31840 or none_type otherwise. */
31842 static enum tag_types
31843 cp_parser_token_is_class_key (cp_token
* token
)
31845 switch (token
->keyword
)
31850 return record_type
;
31859 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
31860 or none_type otherwise or if the token is null. */
31862 static enum tag_types
31863 cp_parser_token_is_type_parameter_key (cp_token
* token
)
31868 switch (token
->keyword
)
31873 return typename_type
;
31880 /* Diagnose redundant enum-keys. */
31883 cp_parser_maybe_warn_enum_key (cp_parser
*parser
, location_t key_loc
,
31884 tree type
, rid scoped_key
)
31886 if (!warn_redundant_tags
)
31889 tree type_decl
= TYPE_MAIN_DECL (type
);
31890 tree name
= DECL_NAME (type_decl
);
31891 /* Look up the NAME to see if it unambiguously refers to the TYPE. */
31892 push_deferring_access_checks (dk_no_check
);
31893 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
31894 pop_deferring_access_checks ();
31896 /* The enum-key is redundant for uses of the TYPE that are not
31897 declarations and for which name lookup returns just the type
31899 if (decl
!= type_decl
)
31902 if (scoped_key
!= RID_CLASS
31903 && scoped_key
!= RID_STRUCT
31904 && current_lang_name
!= lang_name_cplusplus
31905 && current_namespace
== global_namespace
)
31907 /* Avoid issuing the diagnostic for apparently redundant (unscoped)
31908 enum tag in shared C/C++ code in files (such as headers) included
31909 in the main source file. */
31910 const line_map_ordinary
*map
= NULL
;
31911 linemap_resolve_location (line_table
, key_loc
,
31912 LRK_MACRO_DEFINITION_LOCATION
,
31914 if (!MAIN_FILE_P (map
))
31918 gcc_rich_location
richloc (key_loc
);
31919 richloc
.add_fixit_remove (key_loc
);
31920 warning_at (&richloc
, OPT_Wredundant_tags
,
31921 "redundant enum-key %<enum%s%> in reference to %q#T",
31922 (scoped_key
== RID_CLASS
? " class"
31923 : scoped_key
== RID_STRUCT
? " struct" : ""), type
);
31926 /* Describes the set of declarations of a struct, class, or class template
31927 or its specializations. Used for -Wmismatched-tags. */
31929 class class_decl_loc_t
31933 class_decl_loc_t ()
31934 : locvec (), idxdef (), def_class_key ()
31939 /* Constructs an object for a single declaration of a class with
31940 CLASS_KEY at the current location in the current function (or
31941 at another scope). KEY_REDUNDANT is true if the class-key may
31942 be omitted in the current context without an ambiguity with
31943 another symbol with the same name.
31944 DEF_P is true for a class declaration that is a definition.
31945 CURLOC is the associated location. */
31946 class_decl_loc_t (tag_types class_key
, bool key_redundant
, bool def_p
,
31947 location_t curloc
= input_location
)
31948 : locvec (), idxdef (def_p
? 0 : UINT_MAX
), def_class_key (class_key
)
31951 class_key_loc_t
ckl (current_function_decl
, curloc
, class_key
,
31953 locvec
.quick_push (ckl
);
31956 /* Copy, assign, and destroy the object. Necessary because LOCVEC
31957 isn't safely copyable and assignable and doesn't release storage
31959 class_decl_loc_t (const class_decl_loc_t
&rhs
)
31960 : locvec (rhs
.locvec
.copy ()), idxdef (rhs
.idxdef
),
31961 def_class_key (rhs
.def_class_key
)
31964 class_decl_loc_t
& operator= (const class_decl_loc_t
&rhs
)
31969 locvec
= rhs
.locvec
.copy ();
31970 idxdef
= rhs
.idxdef
;
31971 def_class_key
= rhs
.def_class_key
;
31975 ~class_decl_loc_t ()
31980 /* Issues -Wmismatched-tags for a single class. */
31981 void diag_mismatched_tags (tree
);
31983 /* Issues -Wmismatched-tags for all classes. */
31984 static void diag_mismatched_tags ();
31986 /* Adds TYPE_DECL to the collection of class decls and diagnoses
31987 redundant tags (if -Wredundant-tags is enabled). */
31988 static void add (cp_parser
*, location_t
, tag_types
, tree
, bool, bool);
31990 /* Either adds this decl to the collection of class decls
31991 or diagnoses it, whichever is appropriate. */
31992 void add_or_diag_mismatched_tag (tree
, tag_types
, bool, bool);
31996 tree
function (unsigned i
) const
31998 return locvec
[i
].func
;
32001 location_t
location (unsigned i
) const
32003 return locvec
[i
].loc
;
32006 bool key_redundant (unsigned i
) const
32008 return locvec
[i
].key_redundant
;
32011 tag_types
class_key (unsigned i
) const
32013 return locvec
[i
].class_key
;
32016 /* True if a definition for the class has been seen. */
32017 bool def_p () const
32019 return idxdef
< locvec
.length ();
32022 /* The location of a single mention of a class type with the given
32024 struct class_key_loc_t
32026 class_key_loc_t (tree func
, location_t loc
, tag_types key
, bool redundant
)
32027 : func (func
), loc (loc
), class_key (key
), key_redundant (redundant
)
32030 /* The function the type is mentioned in. */
32032 /* The exact location. */
32034 /* The class-key used in the mention of the type. */
32035 tag_types class_key
;
32036 /* True when the class-key could be omitted at this location
32037 without an ambiguity with another symbol of the same name. */
32038 bool key_redundant
;
32040 /* Avoid using auto_vec here since it's not safe to copy due to pr90904. */
32041 vec
<class_key_loc_t
> locvec
;
32042 /* LOCVEC index of the definition or UINT_MAX if none exists. */
32044 /* The class-key the class was last declared with or none_type when
32045 it has been declared with a mismatched key. */
32046 tag_types def_class_key
;
32048 /* A mapping between a TYPE_DECL for a class and the class_decl_loc_t
32049 description above. */
32050 typedef hash_map
<tree_decl_hash
, class_decl_loc_t
> class_to_loc_map_t
;
32051 static class_to_loc_map_t class2loc
;
32054 class_decl_loc_t::class_to_loc_map_t
class_decl_loc_t::class2loc
;
32056 /* Issue an error message if the CLASS_KEY does not match the TYPE.
32057 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32058 is set for a declaration of class TYPE and clear for a reference to
32059 it that is not a declaration of it. */
32062 cp_parser_check_class_key (cp_parser
*parser
, location_t key_loc
,
32063 tag_types class_key
, tree type
, bool def_p
,
32066 if (type
== error_mark_node
)
32069 bool seen_as_union
= TREE_CODE (type
) == UNION_TYPE
;
32070 if (seen_as_union
!= (class_key
== union_type
))
32072 if (permerror (input_location
, "%qs tag used in naming %q#T",
32073 class_key
== union_type
? "union"
32074 : class_key
== record_type
? "struct" : "class",
32076 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type
)),
32077 "%q#T was previously declared here", type
);
32081 if (!warn_mismatched_tags
&& !warn_redundant_tags
)
32084 /* Only consider the true class-keys below and ignore typename_type,
32085 etc. that are not C++ class-keys. */
32086 if (class_key
!= class_type
32087 && class_key
!= record_type
32088 && class_key
!= union_type
)
32091 class_decl_loc_t::add (parser
, key_loc
, class_key
, type
, def_p
, decl_p
);
32094 /* Returns the template or specialization of one to which the RECORD_TYPE
32095 TYPE corresponds. */
32098 specialization_of (tree type
)
32102 /* Determine the template or its partial specialization to which TYPE
32104 if (tree spec
= most_specialized_partial_spec (type
, tf_none
))
32105 if (spec
!= error_mark_node
)
32106 ret
= TREE_TYPE (TREE_VALUE (spec
));
32109 ret
= CLASSTYPE_PRIMARY_TEMPLATE_TYPE (type
);
32111 return TYPE_MAIN_DECL (ret
);
32115 /* Adds the class TYPE to the collection of class decls and diagnoses
32116 redundant tags (if -Wredundant-tags is enabled).
32117 DEF_P is expected to be set for a definition of class TYPE. DECL_P
32118 is set for a (likely, based on syntactic context) declaration of class
32119 TYPE and clear for a reference to it that is not a declaration of it. */
32122 class_decl_loc_t::add (cp_parser
*parser
, location_t key_loc
,
32123 tag_types class_key
, tree type
, bool def_p
, bool decl_p
)
32125 tree type_decl
= TYPE_MAIN_DECL (type
);
32126 tree name
= DECL_NAME (type_decl
);
32127 /* Look up the NAME to see if it unambiguously refers to the TYPE
32128 and set KEY_REDUNDANT if so. */
32129 push_deferring_access_checks (dk_no_check
);
32130 tree decl
= cp_parser_lookup_name_simple (parser
, name
, input_location
);
32131 pop_deferring_access_checks ();
32133 /* The class-key is redundant for uses of the CLASS_TYPE that are
32134 neither definitions of it nor declarations, and for which name
32135 lookup returns just the type itself. */
32136 bool key_redundant
= (!def_p
&& !decl_p
32137 && (decl
== type_decl
32138 || TREE_CODE (decl
) == TEMPLATE_DECL
32139 || TYPE_BEING_DEFINED (type
)));
32142 && class_key
!= class_type
32143 && current_lang_name
!= lang_name_cplusplus
32144 && current_namespace
== global_namespace
)
32146 /* Avoid issuing the diagnostic for apparently redundant struct
32147 and union class-keys in shared C/C++ code in files (such as
32148 headers) included in the main source file. */
32149 const line_map_ordinary
*map
= NULL
;
32150 linemap_resolve_location (line_table
, key_loc
,
32151 LRK_MACRO_DEFINITION_LOCATION
,
32153 if (!MAIN_FILE_P (map
))
32154 key_redundant
= false;
32157 /* Set if a declaration of TYPE has previously been seen or if it must
32158 exist in a precompiled header. */
32160 class_decl_loc_t
*rdl
= &class2loc
.get_or_insert (type_decl
, &exist
);
32163 tree type
= TREE_TYPE (type_decl
);
32164 if (def_p
|| !COMPLETE_TYPE_P (type
))
32166 /* TYPE_DECL is the first declaration or definition of the type
32167 (outside precompiled headers -- see below). Just create
32168 a new entry for it and return unless it's a declaration
32169 involving a template that may need to be diagnosed by
32170 -Wredundant-tags. */
32171 *rdl
= class_decl_loc_t (class_key
, false, def_p
);
32172 if (TREE_CODE (decl
) != TEMPLATE_DECL
)
32177 /* TYPE was previously defined in some unknown precompiled hdeader.
32178 Simply add a record of its definition at an unknown location and
32179 proceed below to add a reference to it at the current location.
32180 (Declarations in precompiled headers that are not definitions
32183 = CLASSTYPE_DECLARED_CLASS (type
) ? class_type
: record_type
;
32184 location_t def_loc
= DECL_SOURCE_LOCATION (type_decl
);
32185 *rdl
= class_decl_loc_t (def_key
, false, true, def_loc
);
32190 /* A prior declaration of TYPE_DECL has been seen. */
32194 gcc_rich_location
richloc (key_loc
);
32195 richloc
.add_fixit_remove (key_loc
);
32196 warning_at (&richloc
, OPT_Wredundant_tags
,
32197 "redundant class-key %qs in reference to %q#T",
32198 class_key
== union_type
? "union"
32199 : class_key
== record_type
? "struct" : "class",
32204 /* Do nothing if this is the first declaration of the type. */
32207 if (rdl
->idxdef
!= UINT_MAX
&& rdl
->def_class_key
== class_key
)
32208 /* Do nothing if the class-key in this declaration matches
32212 rdl
->add_or_diag_mismatched_tag (type_decl
, class_key
, key_redundant
,
32216 /* Either adds this DECL corresponding to the TYPE_DECL to the collection
32217 of class decls or diagnoses it, whichever is appropriate. */
32220 class_decl_loc_t::add_or_diag_mismatched_tag (tree type_decl
,
32221 tag_types class_key
,
32225 /* Reset the CLASS_KEY associated with this type on mismatch.
32226 This is an optimization that lets the diagnostic code skip
32227 over classes that use the same class-key in all declarations. */
32228 if (def_class_key
!= class_key
)
32229 def_class_key
= none_type
;
32231 /* Set IDXDEF to the index of the vector corresponding to
32234 idxdef
= locvec
.length ();
32236 /* Append a record of this declaration to the vector. */
32237 class_key_loc_t
ckl (current_function_decl
, input_location
, class_key
,
32239 locvec
.safe_push (ckl
);
32241 if (idxdef
== UINT_MAX
)
32244 /* As a space optimization diagnose declarations of a class
32245 whose definition has been seen and purge the LOCVEC of
32246 all entries except the definition. */
32247 diag_mismatched_tags (type_decl
);
32250 class_decl_loc_t::class_key_loc_t ent
= locvec
[idxdef
];
32252 locvec
.reserve (2);
32253 locvec
.safe_push (ent
);
32257 /* Pop the entry pushed above for this declaration. */
32261 /* Issues -Wmismatched-tags for a single class. */
32264 class_decl_loc_t::diag_mismatched_tags (tree type_decl
)
32266 if (!warn_mismatched_tags
)
32269 /* Number of uses of the class. */
32270 const unsigned ndecls
= locvec
.length ();
32272 /* The class (or template) declaration guiding the decisions about
32273 the diagnostic. For ordinary classes it's the same as THIS. For
32274 uses of instantiations of templates other than their declarations
32275 it points to the record for the declaration of the corresponding
32276 primary template or partial specialization. */
32277 class_decl_loc_t
*cdlguide
= this;
32279 tree type
= TREE_TYPE (type_decl
);
32280 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type
))
32282 /* For implicit instantiations of a primary template look up
32283 the primary or partial specialization and use it as
32284 the expected class-key rather than using the class-key of
32285 the first reference to the instantiation. The primary must
32286 be (and inevitably is) at index zero. */
32287 tree spec
= specialization_of (type
);
32288 cdlguide
= class2loc
.get (spec
);
32289 gcc_assert (cdlguide
!= NULL
);
32293 /* Skip declarations that consistently use the same class-key. */
32294 if (def_class_key
!= none_type
)
32298 /* Set if a definition for the class has been seen. */
32299 const bool def_p
= cdlguide
->def_p ();
32301 /* The index of the declaration whose class-key this declaration
32302 is expected to match. It's either the class-key of the class
32303 definition if one exists or the first declaration otherwise. */
32304 const unsigned idxguide
= def_p
? cdlguide
->idxdef
: 0;
32306 /* The class-key the class is expected to be declared with: it's
32307 either the key used in its definition or the first declaration
32308 if no definition has been provided.
32309 For implicit instantiations of a primary template it's
32310 the class-key used to declare the primary with. The primary
32311 must be at index zero. */
32312 const tag_types xpect_key
= cdlguide
->class_key (idxguide
);
32315 /* Advance IDX to the first declaration that either is not
32316 a definition or that doesn't match the first declaration
32317 if no definition is provided. */
32318 while (class_key (idx
) == xpect_key
)
32319 if (++idx
== ndecls
)
32322 /* Save the current function before changing it below. */
32323 tree save_func
= current_function_decl
;
32324 /* Set the function declaration to print in diagnostic context. */
32325 current_function_decl
= function (idx
);
32327 const char *xmatchkstr
= xpect_key
== record_type
? "class" : "struct";
32328 const char *xpectkstr
= xpect_key
== record_type
? "struct" : "class";
32330 location_t loc
= location (idx
);
32331 bool key_redundant_p
= key_redundant (idx
);
32332 auto_diagnostic_group d
;
32333 /* Issue a warning for the first mismatched declaration.
32334 Avoid using "%#qT" since the class-key for the same type will
32335 be the same regardless of which one was used in the declaraion. */
32336 if (warning_at (loc
, OPT_Wmismatched_tags
,
32337 "%qT declared with a mismatched class-key %qs",
32338 type_decl
, xmatchkstr
))
32340 /* Suggest how to avoid the warning for each instance since
32341 the guidance may be different depending on context. */
32344 ? G_("remove the class-key or replace it with %qs")
32345 : G_("replace the class-key with %qs")),
32348 /* Also point to the first declaration or definition that guided
32349 the decision to issue the warning above. */
32350 inform (cdlguide
->location (idxguide
),
32352 ? G_("%qT defined as %qs here")
32353 : G_("%qT first declared as %qs here")),
32354 type_decl
, xpectkstr
);
32357 /* Issue warnings for the remaining inconsistent declarations. */
32358 for (unsigned i
= idx
+ 1; i
!= ndecls
; ++i
)
32360 tag_types clskey
= class_key (i
);
32361 /* Skip over the declarations that match either the definition
32362 if one was provided or the first declaration. */
32363 if (clskey
== xpect_key
)
32366 loc
= location (i
);
32367 key_redundant_p
= key_redundant (i
);
32368 /* Set the function declaration to print in diagnostic context. */
32369 current_function_decl
= function (i
);
32370 if (warning_at (loc
, OPT_Wmismatched_tags
,
32371 "%qT declared with a mismatched class-key %qs",
32372 type_decl
, xmatchkstr
))
32373 /* Suggest how to avoid the warning for each instance since
32374 the guidance may be different depending on context. */
32377 ? G_("remove the class-key or replace it with %qs")
32378 : G_("replace the class-key with %qs")),
32382 /* Restore the current function in case it was replaced above. */
32383 current_function_decl
= save_func
;
32386 /* Issues -Wmismatched-tags for all classes. Called at the end
32387 of processing a translation unit, after declarations of all class
32388 types and their uses have been recorded. */
32391 class_decl_loc_t::diag_mismatched_tags ()
32393 /* CLASS2LOC should be empty if both -Wmismatched-tags and
32394 -Wredundant-tags are disabled. */
32395 gcc_assert (warn_mismatched_tags
32396 || warn_redundant_tags
32397 || class2loc
.is_empty ());
32399 /* Save the current function before changing on return. It should
32400 be null at this point. */
32401 temp_override
<tree
> cleanup (current_function_decl
);
32403 if (warn_mismatched_tags
)
32405 /* Iterate over the collected class/struct/template declarations. */
32406 typedef class_to_loc_map_t::iterator iter_t
;
32407 for (iter_t it
= class2loc
.begin (); it
!= class2loc
.end (); ++it
)
32409 tree type_decl
= (*it
).first
;
32410 class_decl_loc_t
&recloc
= (*it
).second
;
32411 recloc
.diag_mismatched_tags (type_decl
);
32415 class2loc
.empty ();
32418 /* Issue an error message if DECL is redeclared with different
32419 access than its original declaration [class.access.spec/3].
32420 This applies to nested classes, nested class templates and
32421 enumerations [class.mem/1]. */
32424 cp_parser_check_access_in_redeclaration (tree decl
, location_t location
)
32427 || (!CLASS_TYPE_P (TREE_TYPE (decl
))
32428 && TREE_CODE (TREE_TYPE (decl
)) != ENUMERAL_TYPE
))
32431 if ((TREE_PRIVATE (decl
)
32432 != (current_access_specifier
== access_private_node
))
32433 || (TREE_PROTECTED (decl
)
32434 != (current_access_specifier
== access_protected_node
)))
32435 error_at (location
, "%qD redeclared with different access", decl
);
32438 /* Look for the `template' keyword, as a syntactic disambiguator.
32439 Return TRUE iff it is present, in which case it will be
32443 cp_parser_optional_template_keyword (cp_parser
*parser
)
32445 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TEMPLATE
))
32447 /* In C++98 the `template' keyword can only be used within templates;
32448 outside templates the parser can always figure out what is a
32449 template and what is not. In C++11, per the resolution of DR 468,
32450 `template' is allowed in cases where it is not strictly necessary. */
32451 if (!processing_template_decl
32452 && pedantic
&& cxx_dialect
== cxx98
)
32454 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32455 pedwarn (token
->location
, OPT_Wpedantic
,
32456 "in C++98 %<template%> (as a disambiguator) is only "
32457 "allowed within templates");
32458 /* If this part of the token stream is rescanned, the same
32459 error message would be generated. So, we purge the token
32460 from the stream. */
32461 cp_lexer_purge_token (parser
->lexer
);
32466 /* Consume the `template' keyword. */
32467 cp_lexer_consume_token (parser
->lexer
);
32474 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
32475 set PARSER->SCOPE, and perform other related actions. */
32478 cp_parser_pre_parsed_nested_name_specifier (cp_parser
*parser
)
32480 struct tree_check
*check_value
;
32482 /* Get the stored value. */
32483 check_value
= cp_lexer_consume_token (parser
->lexer
)->u
.tree_check_value
;
32484 /* Set the scope from the stored value. */
32485 parser
->scope
= saved_checks_value (check_value
);
32486 parser
->qualifying_scope
= check_value
->qualifying_scope
;
32487 parser
->object_scope
= NULL_TREE
;
32490 /* Consume tokens up through a non-nested END token. Returns TRUE if we
32491 encounter the end of a block before what we were looking for. */
32494 cp_parser_cache_group (cp_parser
*parser
,
32495 enum cpp_ttype end
,
32500 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
32502 /* Abort a parenthesized expression if we encounter a semicolon. */
32503 if ((end
== CPP_CLOSE_PAREN
|| depth
== 0)
32504 && token
->type
== CPP_SEMICOLON
)
32506 /* If we've reached the end of the file, stop. */
32507 if (token
->type
== CPP_EOF
32508 || (end
!= CPP_PRAGMA_EOL
32509 && token
->type
== CPP_PRAGMA_EOL
))
32511 if (token
->type
== CPP_CLOSE_BRACE
&& depth
== 0)
32512 /* We've hit the end of an enclosing block, so there's been some
32513 kind of syntax error. */
32516 /* Consume the token. */
32517 cp_lexer_consume_token (parser
->lexer
);
32518 /* See if it starts a new group. */
32519 if (token
->type
== CPP_OPEN_BRACE
)
32521 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, depth
+ 1);
32522 /* In theory this should probably check end == '}', but
32523 cp_parser_save_member_function_body needs it to exit
32524 after either '}' or ')' when called with ')'. */
32528 else if (token
->type
== CPP_OPEN_PAREN
)
32530 cp_parser_cache_group (parser
, CPP_CLOSE_PAREN
, depth
+ 1);
32531 if (depth
== 0 && end
== CPP_CLOSE_PAREN
)
32534 else if (token
->type
== CPP_PRAGMA
)
32535 cp_parser_cache_group (parser
, CPP_PRAGMA_EOL
, depth
+ 1);
32536 else if (token
->type
== end
)
32541 /* Like above, for caching a default argument or NSDMI. Both of these are
32542 terminated by a non-nested comma, but it can be unclear whether or not a
32543 comma is nested in a template argument list unless we do more parsing.
32544 In order to handle this ambiguity, when we encounter a ',' after a '<'
32545 we try to parse what follows as a parameter-declaration-list (in the
32546 case of a default argument) or a member-declarator (in the case of an
32547 NSDMI). If that succeeds, then we stop caching. */
32550 cp_parser_cache_defarg (cp_parser
*parser
, bool nsdmi
)
32552 unsigned depth
= 0;
32553 int maybe_template_id
= 0;
32554 cp_token
*first_token
;
32556 tree default_argument
;
32558 /* Add tokens until we have processed the entire default
32559 argument. We add the range [first_token, token). */
32560 first_token
= cp_lexer_peek_token (parser
->lexer
);
32561 if (first_token
->type
== CPP_OPEN_BRACE
)
32563 /* For list-initialization, this is straightforward. */
32564 cp_parser_cache_group (parser
, CPP_CLOSE_BRACE
, /*depth=*/0);
32565 token
= cp_lexer_peek_token (parser
->lexer
);
32571 /* Peek at the next token. */
32572 token
= cp_lexer_peek_token (parser
->lexer
);
32573 /* What we do depends on what token we have. */
32574 switch (token
->type
)
32576 /* In valid code, a default argument must be
32577 immediately followed by a `,' `)', or `...'. */
32579 if (depth
== 0 && maybe_template_id
)
32581 /* If we've seen a '<', we might be in a
32582 template-argument-list. Until Core issue 325 is
32583 resolved, we don't know how this situation ought
32584 to be handled, so try to DTRT. We check whether
32585 what comes after the comma is a valid parameter
32586 declaration list. If it is, then the comma ends
32587 the default argument; otherwise the default
32588 argument continues. */
32589 bool error
= false;
32592 /* Set ITALP so cp_parser_parameter_declaration_list
32593 doesn't decide to commit to this parse. */
32594 bool saved_italp
= parser
->in_template_argument_list_p
;
32595 parser
->in_template_argument_list_p
= true;
32597 cp_parser_parse_tentatively (parser
);
32601 /* Parse declarators until we reach a non-comma or
32602 somthing that cannot be an initializer.
32603 Just checking whether we're looking at a single
32604 declarator is insufficient. Consider:
32605 int var = tuple<T,U>::x;
32606 The template parameter 'U' looks exactly like a
32610 int ctor_dtor_or_conv_p
;
32611 cp_lexer_consume_token (parser
->lexer
);
32612 cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
32613 CP_PARSER_FLAGS_NONE
,
32614 &ctor_dtor_or_conv_p
,
32615 /*parenthesized_p=*/NULL
,
32617 /*friend_p=*/false,
32618 /*static_p=*/false);
32619 peek
= cp_lexer_peek_token (parser
->lexer
);
32620 if (cp_parser_error_occurred (parser
))
32623 while (peek
->type
== CPP_COMMA
);
32624 /* If we met an '=' or ';' then the original comma
32625 was the end of the NSDMI. Otherwise assume
32626 we're still in the NSDMI. */
32627 error
= (peek
->type
!= CPP_EQ
32628 && peek
->type
!= CPP_SEMICOLON
);
32632 cp_lexer_consume_token (parser
->lexer
);
32633 begin_scope (sk_function_parms
, NULL_TREE
);
32634 tree t
= cp_parser_parameter_declaration_list
32635 (parser
, CP_PARSER_FLAGS_NONE
);
32636 if (t
== error_mark_node
)
32638 pop_bindings_and_leave_scope ();
32640 if (!cp_parser_error_occurred (parser
) && !error
)
32642 cp_parser_abort_tentative_parse (parser
);
32644 parser
->in_template_argument_list_p
= saved_italp
;
32648 case CPP_CLOSE_PAREN
:
32650 /* If we run into a non-nested `;', `}', or `]',
32651 then the code is invalid -- but the default
32652 argument is certainly over. */
32653 case CPP_SEMICOLON
:
32654 case CPP_CLOSE_BRACE
:
32655 case CPP_CLOSE_SQUARE
:
32657 /* Handle correctly int n = sizeof ... ( p ); */
32658 && token
->type
!= CPP_ELLIPSIS
)
32660 /* Update DEPTH, if necessary. */
32661 else if (token
->type
== CPP_CLOSE_PAREN
32662 || token
->type
== CPP_CLOSE_BRACE
32663 || token
->type
== CPP_CLOSE_SQUARE
)
32667 case CPP_OPEN_PAREN
:
32668 case CPP_OPEN_SQUARE
:
32669 case CPP_OPEN_BRACE
:
32675 /* This might be the comparison operator, or it might
32676 start a template argument list. */
32677 ++maybe_template_id
;
32681 if (cxx_dialect
== cxx98
)
32683 /* Fall through for C++0x, which treats the `>>'
32684 operator like two `>' tokens in certain
32686 gcc_fallthrough ();
32691 /* This might be an operator, or it might close a
32692 template argument list. But if a previous '<'
32693 started a template argument list, this will have
32694 closed it, so we can't be in one anymore. */
32695 maybe_template_id
-= 1 + (token
->type
== CPP_RSHIFT
);
32696 if (maybe_template_id
< 0)
32697 maybe_template_id
= 0;
32701 /* If we run out of tokens, issue an error message. */
32703 case CPP_PRAGMA_EOL
:
32704 error_at (token
->location
, "file ends in default argument");
32705 return error_mark_node
;
32709 /* In these cases, we should look for template-ids.
32710 For example, if the default argument is
32711 `X<int, double>()', we need to do name lookup to
32712 figure out whether or not `X' is a template; if
32713 so, the `,' does not end the default argument.
32715 That is not yet done. */
32722 /* If we've reached the end, stop. */
32726 /* Add the token to the token block. */
32727 token
= cp_lexer_consume_token (parser
->lexer
);
32730 /* Create a DEFERRED_PARSE to represent the unparsed default
32732 default_argument
= make_node (DEFERRED_PARSE
);
32733 DEFPARSE_TOKENS (default_argument
)
32734 = cp_token_cache_new (first_token
, token
);
32735 DEFPARSE_INSTANTIATIONS (default_argument
) = NULL
;
32737 return default_argument
;
32740 /* A location to use for diagnostics about an unparsed DEFERRED_PARSE. */
32743 defparse_location (tree default_argument
)
32745 cp_token_cache
*tokens
= DEFPARSE_TOKENS (default_argument
);
32746 location_t start
= tokens
->first
->location
;
32747 location_t end
= tokens
->last
->location
;
32748 return make_location (start
, start
, end
);
32751 /* Begin parsing tentatively. We always save tokens while parsing
32752 tentatively so that if the tentative parsing fails we can restore the
32756 cp_parser_parse_tentatively (cp_parser
* parser
)
32758 /* Enter a new parsing context. */
32759 parser
->context
= cp_parser_context_new (parser
->context
);
32760 /* Begin saving tokens. */
32761 cp_lexer_save_tokens (parser
->lexer
);
32762 /* In order to avoid repetitive access control error messages,
32763 access checks are queued up until we are no longer parsing
32765 push_deferring_access_checks (dk_deferred
);
32768 /* Commit to the currently active tentative parse. */
32771 cp_parser_commit_to_tentative_parse (cp_parser
* parser
)
32773 cp_parser_context
*context
;
32776 /* Mark all of the levels as committed. */
32777 lexer
= parser
->lexer
;
32778 for (context
= parser
->context
; context
->next
; context
= context
->next
)
32780 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
32782 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
32783 while (!cp_lexer_saving_tokens (lexer
))
32784 lexer
= lexer
->next
;
32785 cp_lexer_commit_tokens (lexer
);
32789 /* Commit to the topmost currently active tentative parse.
32791 Note that this function shouldn't be called when there are
32792 irreversible side-effects while in a tentative state. For
32793 example, we shouldn't create a permanent entry in the symbol
32794 table, or issue an error message that might not apply if the
32795 tentative parse is aborted. */
32798 cp_parser_commit_to_topmost_tentative_parse (cp_parser
* parser
)
32800 cp_parser_context
*context
= parser
->context
;
32801 cp_lexer
*lexer
= parser
->lexer
;
32805 if (context
->status
== CP_PARSER_STATUS_KIND_COMMITTED
)
32807 context
->status
= CP_PARSER_STATUS_KIND_COMMITTED
;
32809 while (!cp_lexer_saving_tokens (lexer
))
32810 lexer
= lexer
->next
;
32811 cp_lexer_commit_tokens (lexer
);
32815 /* Abort the currently active tentative parse. All consumed tokens
32816 will be rolled back, and no diagnostics will be issued. */
32819 cp_parser_abort_tentative_parse (cp_parser
* parser
)
32821 gcc_assert (parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
32822 || errorcount
> 0);
32823 cp_parser_simulate_error (parser
);
32824 /* Now, pretend that we want to see if the construct was
32825 successfully parsed. */
32826 cp_parser_parse_definitely (parser
);
32829 /* Stop parsing tentatively. If a parse error has occurred, restore the
32830 token stream. Otherwise, commit to the tokens we have consumed.
32831 Returns true if no error occurred; false otherwise. */
32834 cp_parser_parse_definitely (cp_parser
* parser
)
32836 bool error_occurred
;
32837 cp_parser_context
*context
;
32839 /* Remember whether or not an error occurred, since we are about to
32840 destroy that information. */
32841 error_occurred
= cp_parser_error_occurred (parser
);
32842 /* Remove the topmost context from the stack. */
32843 context
= parser
->context
;
32844 parser
->context
= context
->next
;
32845 /* If no parse errors occurred, commit to the tentative parse. */
32846 if (!error_occurred
)
32848 /* Commit to the tokens read tentatively, unless that was
32850 if (context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
)
32851 cp_lexer_commit_tokens (parser
->lexer
);
32853 pop_to_parent_deferring_access_checks ();
32855 /* Otherwise, if errors occurred, roll back our state so that things
32856 are just as they were before we began the tentative parse. */
32859 cp_lexer_rollback_tokens (parser
->lexer
);
32860 pop_deferring_access_checks ();
32862 /* Add the context to the front of the free list. */
32863 context
->next
= cp_parser_context_free_list
;
32864 cp_parser_context_free_list
= context
;
32866 return !error_occurred
;
32869 /* Returns true if we are parsing tentatively and are not committed to
32870 this tentative parse. */
32873 cp_parser_uncommitted_to_tentative_parse_p (cp_parser
* parser
)
32875 return (cp_parser_parsing_tentatively (parser
)
32876 && parser
->context
->status
!= CP_PARSER_STATUS_KIND_COMMITTED
);
32879 /* Returns nonzero iff an error has occurred during the most recent
32880 tentative parse. */
32883 cp_parser_error_occurred (cp_parser
* parser
)
32885 return (cp_parser_parsing_tentatively (parser
)
32886 && parser
->context
->status
== CP_PARSER_STATUS_KIND_ERROR
);
32889 /* Returns nonzero if GNU extensions are allowed. */
32892 cp_parser_allow_gnu_extensions_p (cp_parser
* parser
)
32894 return parser
->allow_gnu_extensions_p
;
32897 /* Objective-C++ Productions */
32900 /* Parse an Objective-C expression, which feeds into a primary-expression
32904 objc-message-expression
32905 objc-string-literal
32906 objc-encode-expression
32907 objc-protocol-expression
32908 objc-selector-expression
32910 Returns a tree representation of the expression. */
32913 cp_parser_objc_expression (cp_parser
* parser
)
32915 /* Try to figure out what kind of declaration is present. */
32916 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
32920 case CPP_OPEN_SQUARE
:
32921 return cp_parser_objc_message_expression (parser
);
32923 case CPP_OBJC_STRING
:
32924 kwd
= cp_lexer_consume_token (parser
->lexer
);
32925 return objc_build_string_object (kwd
->u
.value
);
32928 switch (kwd
->keyword
)
32930 case RID_AT_ENCODE
:
32931 return cp_parser_objc_encode_expression (parser
);
32933 case RID_AT_PROTOCOL
:
32934 return cp_parser_objc_protocol_expression (parser
);
32936 case RID_AT_SELECTOR
:
32937 return cp_parser_objc_selector_expression (parser
);
32944 error_at (kwd
->location
,
32945 "misplaced %<@%D%> Objective-C++ construct",
32947 cp_parser_skip_to_end_of_block_or_statement (parser
);
32950 return error_mark_node
;
32953 /* Parse an Objective-C message expression.
32955 objc-message-expression:
32956 [ objc-message-receiver objc-message-args ]
32958 Returns a representation of an Objective-C message. */
32961 cp_parser_objc_message_expression (cp_parser
* parser
)
32963 tree receiver
, messageargs
;
32965 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32966 cp_lexer_consume_token (parser
->lexer
); /* Eat '['. */
32967 receiver
= cp_parser_objc_message_receiver (parser
);
32968 messageargs
= cp_parser_objc_message_args (parser
);
32969 location_t end_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
32970 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
32972 tree result
= objc_build_message_expr (receiver
, messageargs
);
32974 /* Construct a location e.g.
32977 ranging from the '[' to the ']', with the caret at the start. */
32978 location_t combined_loc
= make_location (start_loc
, start_loc
, end_loc
);
32979 protected_set_expr_location (result
, combined_loc
);
32984 /* Parse an objc-message-receiver.
32986 objc-message-receiver:
32988 simple-type-specifier
32990 Returns a representation of the type or expression. */
32993 cp_parser_objc_message_receiver (cp_parser
* parser
)
32997 /* An Objective-C message receiver may be either (1) a type
32998 or (2) an expression. */
32999 cp_parser_parse_tentatively (parser
);
33000 rcv
= cp_parser_expression (parser
);
33002 /* If that worked out, fine. */
33003 if (cp_parser_parse_definitely (parser
))
33006 cp_parser_parse_tentatively (parser
);
33007 rcv
= cp_parser_simple_type_specifier (parser
,
33008 /*decl_specs=*/NULL
,
33009 CP_PARSER_FLAGS_NONE
);
33011 if (cp_parser_parse_definitely (parser
))
33012 return objc_get_class_reference (rcv
);
33014 cp_parser_error (parser
, "objective-c++ message receiver expected");
33015 return error_mark_node
;
33018 /* Parse the arguments and selectors comprising an Objective-C message.
33023 objc-selector-args , objc-comma-args
33025 objc-selector-args:
33026 objc-selector [opt] : assignment-expression
33027 objc-selector-args objc-selector [opt] : assignment-expression
33030 assignment-expression
33031 objc-comma-args , assignment-expression
33033 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
33034 selector arguments and TREE_VALUE containing a list of comma
33038 cp_parser_objc_message_args (cp_parser
* parser
)
33040 tree sel_args
= NULL_TREE
, addl_args
= NULL_TREE
;
33041 bool maybe_unary_selector_p
= true;
33042 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33044 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
33046 tree selector
= NULL_TREE
, arg
;
33048 if (token
->type
!= CPP_COLON
)
33049 selector
= cp_parser_objc_selector (parser
);
33051 /* Detect if we have a unary selector. */
33052 if (maybe_unary_selector_p
33053 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
33054 return build_tree_list (selector
, NULL_TREE
);
33056 maybe_unary_selector_p
= false;
33057 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
33058 arg
= cp_parser_assignment_expression (parser
);
33061 = chainon (sel_args
,
33062 build_tree_list (selector
, arg
));
33064 token
= cp_lexer_peek_token (parser
->lexer
);
33067 /* Handle non-selector arguments, if any. */
33068 while (token
->type
== CPP_COMMA
)
33072 cp_lexer_consume_token (parser
->lexer
);
33073 arg
= cp_parser_assignment_expression (parser
);
33076 = chainon (addl_args
,
33077 build_tree_list (NULL_TREE
, arg
));
33079 token
= cp_lexer_peek_token (parser
->lexer
);
33082 if (sel_args
== NULL_TREE
&& addl_args
== NULL_TREE
)
33084 cp_parser_error (parser
, "objective-c++ message argument(s) are expected");
33085 return build_tree_list (error_mark_node
, error_mark_node
);
33088 return build_tree_list (sel_args
, addl_args
);
33091 /* Parse an Objective-C encode expression.
33093 objc-encode-expression:
33094 @encode objc-typename
33096 Returns an encoded representation of the type argument. */
33099 cp_parser_objc_encode_expression (cp_parser
* parser
)
33103 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33105 cp_lexer_consume_token (parser
->lexer
); /* Eat '@encode'. */
33106 matching_parens parens
;
33107 parens
.require_open (parser
);
33108 token
= cp_lexer_peek_token (parser
->lexer
);
33109 type
= complete_type (cp_parser_type_id (parser
));
33110 parens
.require_close (parser
);
33114 error_at (token
->location
,
33115 "%<@encode%> must specify a type as an argument");
33116 return error_mark_node
;
33119 /* This happens if we find @encode(T) (where T is a template
33120 typename or something dependent on a template typename) when
33121 parsing a template. In that case, we can't compile it
33122 immediately, but we rather create an AT_ENCODE_EXPR which will
33123 need to be instantiated when the template is used.
33125 if (dependent_type_p (type
))
33127 tree value
= build_min (AT_ENCODE_EXPR
, size_type_node
, type
);
33128 TREE_READONLY (value
) = 1;
33133 /* Build a location of the form:
33136 with caret==start at the @ token, finishing at the close paren. */
33137 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
33139 return cp_expr (objc_build_encode_expr (type
), combined_loc
);
33142 /* Parse an Objective-C @defs expression. */
33145 cp_parser_objc_defs_expression (cp_parser
*parser
)
33149 cp_lexer_consume_token (parser
->lexer
); /* Eat '@defs'. */
33150 matching_parens parens
;
33151 parens
.require_open (parser
);
33152 name
= cp_parser_identifier (parser
);
33153 parens
.require_close (parser
);
33155 return objc_get_class_ivars (name
);
33158 /* Parse an Objective-C protocol expression.
33160 objc-protocol-expression:
33161 @protocol ( identifier )
33163 Returns a representation of the protocol expression. */
33166 cp_parser_objc_protocol_expression (cp_parser
* parser
)
33169 location_t start_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33171 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
33172 matching_parens parens
;
33173 parens
.require_open (parser
);
33174 proto
= cp_parser_identifier (parser
);
33175 parens
.require_close (parser
);
33177 /* Build a location of the form:
33180 with caret==start at the @ token, finishing at the close paren. */
33181 location_t combined_loc
= make_location (start_loc
, start_loc
, parser
->lexer
);
33182 tree result
= objc_build_protocol_expr (proto
);
33183 protected_set_expr_location (result
, combined_loc
);
33187 /* Parse an Objective-C selector expression.
33189 objc-selector-expression:
33190 @selector ( objc-method-signature )
33192 objc-method-signature:
33198 objc-selector-seq objc-selector :
33200 Returns a representation of the method selector. */
33203 cp_parser_objc_selector_expression (cp_parser
* parser
)
33205 tree sel_seq
= NULL_TREE
;
33206 bool maybe_unary_selector_p
= true;
33208 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
33210 cp_lexer_consume_token (parser
->lexer
); /* Eat '@selector'. */
33211 matching_parens parens
;
33212 parens
.require_open (parser
);
33213 token
= cp_lexer_peek_token (parser
->lexer
);
33215 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
33216 || token
->type
== CPP_SCOPE
)
33218 tree selector
= NULL_TREE
;
33220 if (token
->type
!= CPP_COLON
33221 || token
->type
== CPP_SCOPE
)
33222 selector
= cp_parser_objc_selector (parser
);
33224 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
)
33225 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_SCOPE
))
33227 /* Detect if we have a unary selector. */
33228 if (maybe_unary_selector_p
)
33230 sel_seq
= selector
;
33231 goto finish_selector
;
33235 cp_parser_error (parser
, "expected %<:%>");
33238 maybe_unary_selector_p
= false;
33239 token
= cp_lexer_consume_token (parser
->lexer
);
33241 if (token
->type
== CPP_SCOPE
)
33244 = chainon (sel_seq
,
33245 build_tree_list (selector
, NULL_TREE
));
33247 = chainon (sel_seq
,
33248 build_tree_list (NULL_TREE
, NULL_TREE
));
33252 = chainon (sel_seq
,
33253 build_tree_list (selector
, NULL_TREE
));
33255 token
= cp_lexer_peek_token (parser
->lexer
);
33259 parens
.require_close (parser
);
33262 /* Build a location of the form:
33265 with caret==start at the @ token, finishing at the close paren. */
33266 location_t combined_loc
= make_location (loc
, loc
, parser
->lexer
);
33267 tree result
= objc_build_selector_expr (combined_loc
, sel_seq
);
33268 /* TODO: objc_build_selector_expr doesn't always honor the location. */
33269 protected_set_expr_location (result
, combined_loc
);
33273 /* Parse a list of identifiers.
33275 objc-identifier-list:
33277 objc-identifier-list , identifier
33279 Returns a TREE_LIST of identifier nodes. */
33282 cp_parser_objc_identifier_list (cp_parser
* parser
)
33288 identifier
= cp_parser_identifier (parser
);
33289 if (identifier
== error_mark_node
)
33290 return error_mark_node
;
33292 list
= build_tree_list (NULL_TREE
, identifier
);
33293 sep
= cp_lexer_peek_token (parser
->lexer
);
33295 while (sep
->type
== CPP_COMMA
)
33297 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33298 identifier
= cp_parser_identifier (parser
);
33299 if (identifier
== error_mark_node
)
33302 list
= chainon (list
, build_tree_list (NULL_TREE
,
33304 sep
= cp_lexer_peek_token (parser
->lexer
);
33310 /* Parse an Objective-C alias declaration.
33312 objc-alias-declaration:
33313 @compatibility_alias identifier identifier ;
33315 This function registers the alias mapping with the Objective-C front end.
33316 It returns nothing. */
33319 cp_parser_objc_alias_declaration (cp_parser
* parser
)
33323 cp_lexer_consume_token (parser
->lexer
); /* Eat '@compatibility_alias'. */
33324 alias
= cp_parser_identifier (parser
);
33325 orig
= cp_parser_identifier (parser
);
33326 objc_declare_alias (alias
, orig
);
33327 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33330 /* Parse an Objective-C class forward-declaration.
33332 objc-class-declaration:
33333 @class objc-identifier-list ;
33335 The function registers the forward declarations with the Objective-C
33336 front end. It returns nothing. */
33339 cp_parser_objc_class_declaration (cp_parser
* parser
)
33341 cp_lexer_consume_token (parser
->lexer
); /* Eat '@class'. */
33346 id
= cp_parser_identifier (parser
);
33347 if (id
== error_mark_node
)
33350 objc_declare_class (id
);
33352 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
33353 cp_lexer_consume_token (parser
->lexer
);
33357 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33360 /* Parse a list of Objective-C protocol references.
33362 objc-protocol-refs-opt:
33363 objc-protocol-refs [opt]
33365 objc-protocol-refs:
33366 < objc-identifier-list >
33368 Returns a TREE_LIST of identifiers, if any. */
33371 cp_parser_objc_protocol_refs_opt (cp_parser
* parser
)
33373 tree protorefs
= NULL_TREE
;
33375 if(cp_lexer_next_token_is (parser
->lexer
, CPP_LESS
))
33377 cp_lexer_consume_token (parser
->lexer
); /* Eat '<'. */
33378 protorefs
= cp_parser_objc_identifier_list (parser
);
33379 cp_parser_require (parser
, CPP_GREATER
, RT_GREATER
);
33385 /* Parse a Objective-C visibility specification. */
33388 cp_parser_objc_visibility_spec (cp_parser
* parser
)
33390 cp_token
*vis
= cp_lexer_peek_token (parser
->lexer
);
33392 switch (vis
->keyword
)
33394 case RID_AT_PRIVATE
:
33395 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE
);
33397 case RID_AT_PROTECTED
:
33398 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED
);
33400 case RID_AT_PUBLIC
:
33401 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC
);
33403 case RID_AT_PACKAGE
:
33404 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE
);
33410 /* Eat '@private'/'@protected'/'@public'. */
33411 cp_lexer_consume_token (parser
->lexer
);
33414 /* Parse an Objective-C method type. Return 'true' if it is a class
33415 (+) method, and 'false' if it is an instance (-) method. */
33418 cp_parser_objc_method_type (cp_parser
* parser
)
33420 if (cp_lexer_consume_token (parser
->lexer
)->type
== CPP_PLUS
)
33426 /* Parse an Objective-C protocol qualifier. */
33429 cp_parser_objc_protocol_qualifiers (cp_parser
* parser
)
33431 tree quals
= NULL_TREE
, node
;
33432 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33434 node
= token
->u
.value
;
33436 while (node
&& identifier_p (node
)
33437 && (node
== ridpointers
[(int) RID_IN
]
33438 || node
== ridpointers
[(int) RID_OUT
]
33439 || node
== ridpointers
[(int) RID_INOUT
]
33440 || node
== ridpointers
[(int) RID_BYCOPY
]
33441 || node
== ridpointers
[(int) RID_BYREF
]
33442 || node
== ridpointers
[(int) RID_ONEWAY
]))
33444 quals
= tree_cons (NULL_TREE
, node
, quals
);
33445 cp_lexer_consume_token (parser
->lexer
);
33446 token
= cp_lexer_peek_token (parser
->lexer
);
33447 node
= token
->u
.value
;
33453 /* Parse an Objective-C typename. */
33456 cp_parser_objc_typename (cp_parser
* parser
)
33458 tree type_name
= NULL_TREE
;
33460 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
33462 tree proto_quals
, cp_type
= NULL_TREE
;
33464 matching_parens parens
;
33465 parens
.consume_open (parser
); /* Eat '('. */
33466 proto_quals
= cp_parser_objc_protocol_qualifiers (parser
);
33468 /* An ObjC type name may consist of just protocol qualifiers, in which
33469 case the type shall default to 'id'. */
33470 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
33472 cp_type
= cp_parser_type_id (parser
);
33474 /* If the type could not be parsed, an error has already
33475 been produced. For error recovery, behave as if it had
33476 not been specified, which will use the default type
33478 if (cp_type
== error_mark_node
)
33480 cp_type
= NULL_TREE
;
33481 /* We need to skip to the closing parenthesis as
33482 cp_parser_type_id() does not seem to do it for
33484 cp_parser_skip_to_closing_parenthesis (parser
,
33485 /*recovering=*/true,
33486 /*or_comma=*/false,
33487 /*consume_paren=*/false);
33491 parens
.require_close (parser
);
33492 type_name
= build_tree_list (proto_quals
, cp_type
);
33498 /* Check to see if TYPE refers to an Objective-C selector name. */
33501 cp_parser_objc_selector_p (enum cpp_ttype type
)
33503 return (type
== CPP_NAME
|| type
== CPP_KEYWORD
33504 || type
== CPP_AND_AND
|| type
== CPP_AND_EQ
|| type
== CPP_AND
33505 || type
== CPP_OR
|| type
== CPP_COMPL
|| type
== CPP_NOT
33506 || type
== CPP_NOT_EQ
|| type
== CPP_OR_OR
|| type
== CPP_OR_EQ
33507 || type
== CPP_XOR
|| type
== CPP_XOR_EQ
);
33510 /* Parse an Objective-C selector. */
33513 cp_parser_objc_selector (cp_parser
* parser
)
33515 cp_token
*token
= cp_lexer_consume_token (parser
->lexer
);
33517 if (!cp_parser_objc_selector_p (token
->type
))
33519 error_at (token
->location
, "invalid Objective-C++ selector name");
33520 return error_mark_node
;
33523 /* C++ operator names are allowed to appear in ObjC selectors. */
33524 switch (token
->type
)
33526 case CPP_AND_AND
: return get_identifier ("and");
33527 case CPP_AND_EQ
: return get_identifier ("and_eq");
33528 case CPP_AND
: return get_identifier ("bitand");
33529 case CPP_OR
: return get_identifier ("bitor");
33530 case CPP_COMPL
: return get_identifier ("compl");
33531 case CPP_NOT
: return get_identifier ("not");
33532 case CPP_NOT_EQ
: return get_identifier ("not_eq");
33533 case CPP_OR_OR
: return get_identifier ("or");
33534 case CPP_OR_EQ
: return get_identifier ("or_eq");
33535 case CPP_XOR
: return get_identifier ("xor");
33536 case CPP_XOR_EQ
: return get_identifier ("xor_eq");
33537 default: return token
->u
.value
;
33541 /* Parse an Objective-C params list. */
33544 cp_parser_objc_method_keyword_params (cp_parser
* parser
, tree
* attributes
)
33546 tree params
= NULL_TREE
;
33547 bool maybe_unary_selector_p
= true;
33548 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33550 while (cp_parser_objc_selector_p (token
->type
) || token
->type
== CPP_COLON
)
33552 tree selector
= NULL_TREE
, type_name
, identifier
;
33553 tree parm_attr
= NULL_TREE
;
33555 if (token
->keyword
== RID_ATTRIBUTE
)
33558 if (token
->type
!= CPP_COLON
)
33559 selector
= cp_parser_objc_selector (parser
);
33561 /* Detect if we have a unary selector. */
33562 if (maybe_unary_selector_p
33563 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
33565 params
= selector
; /* Might be followed by attributes. */
33569 maybe_unary_selector_p
= false;
33570 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
33572 /* Something went quite wrong. There should be a colon
33573 here, but there is not. Stop parsing parameters. */
33576 type_name
= cp_parser_objc_typename (parser
);
33577 /* New ObjC allows attributes on parameters too. */
33578 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
33579 parm_attr
= cp_parser_attributes_opt (parser
);
33580 identifier
= cp_parser_identifier (parser
);
33584 objc_build_keyword_decl (selector
,
33589 token
= cp_lexer_peek_token (parser
->lexer
);
33592 if (params
== NULL_TREE
)
33594 cp_parser_error (parser
, "objective-c++ method declaration is expected");
33595 return error_mark_node
;
33598 /* We allow tail attributes for the method. */
33599 if (token
->keyword
== RID_ATTRIBUTE
)
33601 *attributes
= cp_parser_attributes_opt (parser
);
33602 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
33603 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33605 cp_parser_error (parser
,
33606 "method attributes must be specified at the end");
33607 return error_mark_node
;
33610 if (params
== NULL_TREE
)
33612 cp_parser_error (parser
, "objective-c++ method declaration is expected");
33613 return error_mark_node
;
33618 /* Parse the non-keyword Objective-C params. */
33621 cp_parser_objc_method_tail_params_opt (cp_parser
* parser
, bool *ellipsisp
,
33624 tree params
= make_node (TREE_LIST
);
33625 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33626 *ellipsisp
= false; /* Initially, assume no ellipsis. */
33628 while (token
->type
== CPP_COMMA
)
33630 cp_parameter_declarator
*parmdecl
;
33633 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
33634 token
= cp_lexer_peek_token (parser
->lexer
);
33636 if (token
->type
== CPP_ELLIPSIS
)
33638 cp_lexer_consume_token (parser
->lexer
); /* Eat '...'. */
33640 token
= cp_lexer_peek_token (parser
->lexer
);
33644 /* TODO: parse attributes for tail parameters. */
33645 parmdecl
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
33647 parm
= grokdeclarator (parmdecl
->declarator
,
33648 &parmdecl
->decl_specifiers
,
33649 PARM
, /*initialized=*/0,
33650 /*attrlist=*/NULL
);
33652 chainon (params
, build_tree_list (NULL_TREE
, parm
));
33653 token
= cp_lexer_peek_token (parser
->lexer
);
33656 /* We allow tail attributes for the method. */
33657 if (token
->keyword
== RID_ATTRIBUTE
)
33659 if (*attributes
== NULL_TREE
)
33661 *attributes
= cp_parser_attributes_opt (parser
);
33662 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
)
33663 || cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
33667 /* We have an error, but parse the attributes, so that we can
33669 *attributes
= cp_parser_attributes_opt (parser
);
33671 cp_parser_error (parser
,
33672 "method attributes must be specified at the end");
33673 return error_mark_node
;
33679 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
33682 cp_parser_objc_interstitial_code (cp_parser
* parser
)
33684 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33686 /* If the next token is `extern' and the following token is a string
33687 literal, then we have a linkage specification. */
33688 if (token
->keyword
== RID_EXTERN
33689 && cp_parser_is_pure_string_literal
33690 (cp_lexer_peek_nth_token (parser
->lexer
, 2)))
33691 cp_parser_linkage_specification (parser
, NULL_TREE
);
33692 /* Handle #pragma, if any. */
33693 else if (token
->type
== CPP_PRAGMA
)
33694 cp_parser_pragma (parser
, pragma_objc_icode
, NULL
);
33695 /* Allow stray semicolons. */
33696 else if (token
->type
== CPP_SEMICOLON
)
33697 cp_lexer_consume_token (parser
->lexer
);
33698 /* Mark methods as optional or required, when building protocols. */
33699 else if (token
->keyword
== RID_AT_OPTIONAL
)
33701 cp_lexer_consume_token (parser
->lexer
);
33702 objc_set_method_opt (true);
33704 else if (token
->keyword
== RID_AT_REQUIRED
)
33706 cp_lexer_consume_token (parser
->lexer
);
33707 objc_set_method_opt (false);
33709 else if (token
->keyword
== RID_NAMESPACE
)
33710 cp_parser_namespace_definition (parser
);
33711 /* Other stray characters must generate errors. */
33712 else if (token
->type
== CPP_OPEN_BRACE
|| token
->type
== CPP_CLOSE_BRACE
)
33714 cp_lexer_consume_token (parser
->lexer
);
33715 error ("stray %qs between Objective-C++ methods",
33716 token
->type
== CPP_OPEN_BRACE
? "{" : "}");
33718 /* Finally, try to parse a block-declaration, or a function-definition. */
33720 cp_parser_block_declaration (parser
, /*statement_p=*/false);
33723 /* Parse a method signature. */
33726 cp_parser_objc_method_signature (cp_parser
* parser
, tree
* attributes
)
33728 tree rettype
, kwdparms
, optparms
;
33729 bool ellipsis
= false;
33730 bool is_class_method
;
33732 is_class_method
= cp_parser_objc_method_type (parser
);
33733 rettype
= cp_parser_objc_typename (parser
);
33734 *attributes
= NULL_TREE
;
33735 kwdparms
= cp_parser_objc_method_keyword_params (parser
, attributes
);
33736 if (kwdparms
== error_mark_node
)
33737 return error_mark_node
;
33738 optparms
= cp_parser_objc_method_tail_params_opt (parser
, &ellipsis
, attributes
);
33739 if (optparms
== error_mark_node
)
33740 return error_mark_node
;
33742 return objc_build_method_signature (is_class_method
, rettype
, kwdparms
, optparms
, ellipsis
);
33746 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser
* parser
)
33749 cp_lexer_save_tokens (parser
->lexer
);
33750 tattr
= cp_parser_attributes_opt (parser
);
33751 gcc_assert (tattr
) ;
33753 /* If the attributes are followed by a method introducer, this is not allowed.
33754 Dump the attributes and flag the situation. */
33755 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
)
33756 || cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
33759 /* Otherwise, the attributes introduce some interstitial code, possibly so
33760 rewind to allow that check. */
33761 cp_lexer_rollback_tokens (parser
->lexer
);
33765 /* Parse an Objective-C method prototype list. */
33768 cp_parser_objc_method_prototype_list (cp_parser
* parser
)
33770 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33772 while (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33774 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
33776 tree attributes
, sig
;
33777 bool is_class_method
;
33778 if (token
->type
== CPP_PLUS
)
33779 is_class_method
= true;
33781 is_class_method
= false;
33782 sig
= cp_parser_objc_method_signature (parser
, &attributes
);
33783 if (sig
== error_mark_node
)
33785 cp_parser_skip_to_end_of_block_or_statement (parser
);
33786 token
= cp_lexer_peek_token (parser
->lexer
);
33789 objc_add_method_declaration (is_class_method
, sig
, attributes
);
33790 cp_parser_consume_semicolon_at_end_of_statement (parser
);
33792 else if (token
->keyword
== RID_AT_PROPERTY
)
33793 cp_parser_objc_at_property_declaration (parser
);
33794 else if (token
->keyword
== RID_ATTRIBUTE
33795 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
33796 warning_at (cp_lexer_peek_token (parser
->lexer
)->location
,
33798 "prefix attributes are ignored for methods");
33800 /* Allow for interspersed non-ObjC++ code. */
33801 cp_parser_objc_interstitial_code (parser
);
33803 token
= cp_lexer_peek_token (parser
->lexer
);
33806 if (token
->type
!= CPP_EOF
)
33807 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33809 cp_parser_error (parser
, "expected %<@end%>");
33811 objc_finish_interface ();
33814 /* Parse an Objective-C method definition list. */
33817 cp_parser_objc_method_definition_list (cp_parser
* parser
)
33821 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33823 if (token
->keyword
== RID_AT_END
)
33825 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
33828 else if (token
->type
== CPP_EOF
)
33830 cp_parser_error (parser
, "expected %<@end%>");
33833 else if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
)
33835 bool is_class_method
= token
->type
== CPP_PLUS
;
33837 push_deferring_access_checks (dk_deferred
);
33839 tree sig
= cp_parser_objc_method_signature (parser
, &attribute
);
33840 if (sig
== error_mark_node
)
33841 cp_parser_skip_to_end_of_block_or_statement (parser
);
33844 objc_start_method_definition (is_class_method
, sig
,
33845 attribute
, NULL_TREE
);
33847 /* For historical reasons, we accept an optional semicolon. */
33848 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
33849 cp_lexer_consume_token (parser
->lexer
);
33851 perform_deferred_access_checks (tf_warning_or_error
);
33852 stop_deferring_access_checks ();
33854 = cp_parser_function_definition_after_declarator (parser
, false);
33855 pop_deferring_access_checks ();
33856 objc_finish_method_definition (meth
);
33859 /* The following case will be removed once @synthesize is
33860 completely implemented. */
33861 else if (token
->keyword
== RID_AT_PROPERTY
)
33862 cp_parser_objc_at_property_declaration (parser
);
33863 else if (token
->keyword
== RID_AT_SYNTHESIZE
)
33864 cp_parser_objc_at_synthesize_declaration (parser
);
33865 else if (token
->keyword
== RID_AT_DYNAMIC
)
33866 cp_parser_objc_at_dynamic_declaration (parser
);
33867 else if (token
->keyword
== RID_ATTRIBUTE
33868 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser
))
33869 warning_at (token
->location
, OPT_Wattributes
,
33870 "prefix attributes are ignored for methods");
33872 /* Allow for interspersed non-ObjC++ code. */
33873 cp_parser_objc_interstitial_code (parser
);
33876 objc_finish_implementation ();
33879 /* Parse Objective-C ivars. */
33882 cp_parser_objc_class_ivars (cp_parser
* parser
)
33884 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
33886 if (token
->type
!= CPP_OPEN_BRACE
)
33887 return; /* No ivars specified. */
33889 cp_lexer_consume_token (parser
->lexer
); /* Eat '{'. */
33890 token
= cp_lexer_peek_token (parser
->lexer
);
33892 while (token
->type
!= CPP_CLOSE_BRACE
33893 && token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
33895 cp_decl_specifier_seq declspecs
;
33896 int decl_class_or_enum_p
;
33897 tree prefix_attributes
;
33899 cp_parser_objc_visibility_spec (parser
);
33901 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
33904 cp_parser_decl_specifier_seq (parser
,
33905 CP_PARSER_FLAGS_OPTIONAL
,
33907 &decl_class_or_enum_p
);
33909 /* auto, register, static, extern, mutable. */
33910 if (declspecs
.storage_class
!= sc_none
)
33912 cp_parser_error (parser
, "invalid type for instance variable");
33913 declspecs
.storage_class
= sc_none
;
33916 /* thread_local. */
33917 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
33919 cp_parser_error (parser
, "invalid type for instance variable");
33920 declspecs
.locations
[ds_thread
] = 0;
33924 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
33926 cp_parser_error (parser
, "invalid type for instance variable");
33927 declspecs
.locations
[ds_typedef
] = 0;
33930 prefix_attributes
= declspecs
.attributes
;
33931 declspecs
.attributes
= NULL_TREE
;
33933 /* Keep going until we hit the `;' at the end of the
33935 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
33937 tree width
= NULL_TREE
, attributes
, first_attribute
, decl
;
33938 cp_declarator
*declarator
= NULL
;
33939 int ctor_dtor_or_conv_p
;
33941 /* Check for a (possibly unnamed) bitfield declaration. */
33942 token
= cp_lexer_peek_token (parser
->lexer
);
33943 if (token
->type
== CPP_COLON
)
33946 if (token
->type
== CPP_NAME
33947 && (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
33950 /* Get the name of the bitfield. */
33951 declarator
= make_id_declarator (NULL_TREE
,
33952 cp_parser_identifier (parser
),
33953 sfk_none
, token
->location
);
33956 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
33957 /* Get the width of the bitfield. */
33959 = cp_parser_constant_expression (parser
);
33963 /* Parse the declarator. */
33965 = cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
33966 CP_PARSER_FLAGS_NONE
,
33967 &ctor_dtor_or_conv_p
,
33968 /*parenthesized_p=*/NULL
,
33969 /*member_p=*/false,
33970 /*friend_p=*/false,
33971 /*static_p=*/false);
33974 /* Look for attributes that apply to the ivar. */
33975 attributes
= cp_parser_attributes_opt (parser
);
33976 /* Remember which attributes are prefix attributes and
33978 first_attribute
= attributes
;
33979 /* Combine the attributes. */
33980 attributes
= attr_chainon (prefix_attributes
, attributes
);
33983 /* Create the bitfield declaration. */
33984 decl
= grokbitfield (declarator
, &declspecs
,
33985 width
, NULL_TREE
, attributes
);
33987 decl
= grokfield (declarator
, &declspecs
,
33988 NULL_TREE
, /*init_const_expr_p=*/false,
33989 NULL_TREE
, attributes
);
33991 /* Add the instance variable. */
33992 if (decl
!= error_mark_node
&& decl
!= NULL_TREE
)
33993 objc_add_instance_variable (decl
);
33995 /* Reset PREFIX_ATTRIBUTES. */
33996 if (attributes
!= error_mark_node
)
33998 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
33999 attributes
= TREE_CHAIN (attributes
);
34001 TREE_CHAIN (attributes
) = NULL_TREE
;
34004 token
= cp_lexer_peek_token (parser
->lexer
);
34006 if (token
->type
== CPP_COMMA
)
34008 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
34014 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34015 token
= cp_lexer_peek_token (parser
->lexer
);
34018 if (token
->keyword
== RID_AT_END
)
34019 cp_parser_error (parser
, "expected %<}%>");
34021 /* Do not consume the RID_AT_END, so it will be read again as terminating
34022 the @interface of @implementation. */
34023 if (token
->keyword
!= RID_AT_END
&& token
->type
!= CPP_EOF
)
34024 cp_lexer_consume_token (parser
->lexer
); /* Eat '}'. */
34026 /* For historical reasons, we accept an optional semicolon. */
34027 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
34028 cp_lexer_consume_token (parser
->lexer
);
34031 /* Parse an Objective-C protocol declaration. */
34034 cp_parser_objc_protocol_declaration (cp_parser
* parser
, tree attributes
)
34036 tree proto
, protorefs
;
34039 cp_lexer_consume_token (parser
->lexer
); /* Eat '@protocol'. */
34040 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
34042 tok
= cp_lexer_peek_token (parser
->lexer
);
34043 error_at (tok
->location
, "identifier expected after %<@protocol%>");
34044 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34048 /* See if we have a forward declaration or a definition. */
34049 tok
= cp_lexer_peek_nth_token (parser
->lexer
, 2);
34051 /* Try a forward declaration first. */
34052 if (tok
->type
== CPP_COMMA
|| tok
->type
== CPP_SEMICOLON
)
34058 id
= cp_parser_identifier (parser
);
34059 if (id
== error_mark_node
)
34062 objc_declare_protocol (id
, attributes
);
34064 if(cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34065 cp_lexer_consume_token (parser
->lexer
);
34069 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34072 /* Ok, we got a full-fledged definition (or at least should). */
34075 proto
= cp_parser_identifier (parser
);
34076 protorefs
= cp_parser_objc_protocol_refs_opt (parser
);
34077 objc_start_protocol (proto
, protorefs
, attributes
);
34078 cp_parser_objc_method_prototype_list (parser
);
34082 /* Parse an Objective-C superclass or category. */
34085 cp_parser_objc_superclass_or_category (cp_parser
*parser
,
34088 tree
*categ
, bool *is_class_extension
)
34090 cp_token
*next
= cp_lexer_peek_token (parser
->lexer
);
34092 *super
= *categ
= NULL_TREE
;
34093 *is_class_extension
= false;
34094 if (next
->type
== CPP_COLON
)
34096 cp_lexer_consume_token (parser
->lexer
); /* Eat ':'. */
34097 *super
= cp_parser_identifier (parser
);
34099 else if (next
->type
== CPP_OPEN_PAREN
)
34101 matching_parens parens
;
34102 parens
.consume_open (parser
); /* Eat '('. */
34104 /* If there is no category name, and this is an @interface, we
34105 have a class extension. */
34106 if (iface_p
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
34108 *categ
= NULL_TREE
;
34109 *is_class_extension
= true;
34112 *categ
= cp_parser_identifier (parser
);
34114 parens
.require_close (parser
);
34118 /* Parse an Objective-C class interface. */
34121 cp_parser_objc_class_interface (cp_parser
* parser
, tree attributes
)
34123 tree name
, super
, categ
, protos
;
34124 bool is_class_extension
;
34126 cp_lexer_consume_token (parser
->lexer
); /* Eat '@interface'. */
34127 location_t nam_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34128 name
= cp_parser_identifier (parser
);
34129 if (name
== error_mark_node
)
34131 /* It's hard to recover because even if valid @interface stuff
34132 is to follow, we can't compile it (or validate it) if we
34133 don't even know which class it refers to. Let's assume this
34134 was a stray '@interface' token in the stream and skip it.
34138 cp_parser_objc_superclass_or_category (parser
, true, &super
, &categ
,
34139 &is_class_extension
);
34140 protos
= cp_parser_objc_protocol_refs_opt (parser
);
34142 /* We have either a class or a category on our hands. */
34143 if (categ
|| is_class_extension
)
34144 objc_start_category_interface (name
, categ
, protos
, attributes
);
34147 objc_start_class_interface (name
, nam_loc
, super
, protos
, attributes
);
34148 /* Handle instance variable declarations, if any. */
34149 cp_parser_objc_class_ivars (parser
);
34150 objc_continue_interface ();
34153 cp_parser_objc_method_prototype_list (parser
);
34156 /* Parse an Objective-C class implementation. */
34159 cp_parser_objc_class_implementation (cp_parser
* parser
)
34161 tree name
, super
, categ
;
34162 bool is_class_extension
;
34164 cp_lexer_consume_token (parser
->lexer
); /* Eat '@implementation'. */
34165 name
= cp_parser_identifier (parser
);
34166 if (name
== error_mark_node
)
34168 /* It's hard to recover because even if valid @implementation
34169 stuff is to follow, we can't compile it (or validate it) if
34170 we don't even know which class it refers to. Let's assume
34171 this was a stray '@implementation' token in the stream and
34176 cp_parser_objc_superclass_or_category (parser
, false, &super
, &categ
,
34177 &is_class_extension
);
34179 /* We have either a class or a category on our hands. */
34181 objc_start_category_implementation (name
, categ
);
34184 objc_start_class_implementation (name
, super
);
34185 /* Handle instance variable declarations, if any. */
34186 cp_parser_objc_class_ivars (parser
);
34187 objc_continue_implementation ();
34190 cp_parser_objc_method_definition_list (parser
);
34193 /* Consume the @end token and finish off the implementation. */
34196 cp_parser_objc_end_implementation (cp_parser
* parser
)
34198 cp_lexer_consume_token (parser
->lexer
); /* Eat '@end'. */
34199 objc_finish_implementation ();
34202 /* Parse an Objective-C declaration. */
34205 cp_parser_objc_declaration (cp_parser
* parser
, tree attributes
)
34207 /* Try to figure out what kind of declaration is present. */
34208 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
34211 switch (kwd
->keyword
)
34216 error_at (kwd
->location
, "attributes may not be specified before"
34217 " the %<@%D%> Objective-C++ keyword",
34221 case RID_AT_IMPLEMENTATION
:
34222 warning_at (kwd
->location
, OPT_Wattributes
,
34223 "prefix attributes are ignored before %<@%D%>",
34230 switch (kwd
->keyword
)
34233 cp_parser_objc_alias_declaration (parser
);
34236 cp_parser_objc_class_declaration (parser
);
34238 case RID_AT_PROTOCOL
:
34239 cp_parser_objc_protocol_declaration (parser
, attributes
);
34241 case RID_AT_INTERFACE
:
34242 cp_parser_objc_class_interface (parser
, attributes
);
34244 case RID_AT_IMPLEMENTATION
:
34245 cp_parser_objc_class_implementation (parser
);
34248 cp_parser_objc_end_implementation (parser
);
34251 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
34253 cp_parser_skip_to_end_of_block_or_statement (parser
);
34257 /* Parse an Objective-C try-catch-finally statement.
34259 objc-try-catch-finally-stmt:
34260 @try compound-statement objc-catch-clause-seq [opt]
34261 objc-finally-clause [opt]
34263 objc-catch-clause-seq:
34264 objc-catch-clause objc-catch-clause-seq [opt]
34267 @catch ( objc-exception-declaration ) compound-statement
34269 objc-finally-clause:
34270 @finally compound-statement
34272 objc-exception-declaration:
34273 parameter-declaration
34276 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
34280 PS: This function is identical to c_parser_objc_try_catch_finally_statement
34281 for C. Keep them in sync. */
34284 cp_parser_objc_try_catch_finally_statement (cp_parser
*parser
)
34286 location_t location
;
34289 cp_parser_require_keyword (parser
, RID_AT_TRY
, RT_AT_TRY
);
34290 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
34291 objc_maybe_warn_exceptions (location
);
34292 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
34293 node, lest it get absorbed into the surrounding block. */
34294 stmt
= push_stmt_list ();
34295 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
34296 objc_begin_try_stmt (location
, pop_stmt_list (stmt
));
34298 while (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_CATCH
))
34300 cp_parameter_declarator
*parm
;
34301 tree parameter_declaration
= error_mark_node
;
34302 bool seen_open_paren
= false;
34303 matching_parens parens
;
34305 cp_lexer_consume_token (parser
->lexer
);
34306 if (parens
.require_open (parser
))
34307 seen_open_paren
= true;
34308 if (cp_lexer_next_token_is (parser
->lexer
, CPP_ELLIPSIS
))
34310 /* We have "@catch (...)" (where the '...' are literally
34311 what is in the code). Skip the '...'.
34312 parameter_declaration is set to NULL_TREE, and
34313 objc_being_catch_clauses() knows that that means
34315 cp_lexer_consume_token (parser
->lexer
);
34316 parameter_declaration
= NULL_TREE
;
34320 /* We have "@catch (NSException *exception)" or something
34321 like that. Parse the parameter declaration. */
34322 parm
= cp_parser_parameter_declaration (parser
, CP_PARSER_FLAGS_NONE
,
34325 parameter_declaration
= error_mark_node
;
34327 parameter_declaration
= grokdeclarator (parm
->declarator
,
34328 &parm
->decl_specifiers
,
34329 PARM
, /*initialized=*/0,
34330 /*attrlist=*/NULL
);
34332 if (seen_open_paren
)
34333 parens
.require_close (parser
);
34336 /* If there was no open parenthesis, we are recovering from
34337 an error, and we are trying to figure out what mistake
34338 the user has made. */
34340 /* If there is an immediate closing parenthesis, the user
34341 probably forgot the opening one (ie, they typed "@catch
34342 NSException *e)". Parse the closing parenthesis and keep
34344 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
34345 cp_lexer_consume_token (parser
->lexer
);
34347 /* If these is no immediate closing parenthesis, the user
34348 probably doesn't know that parenthesis are required at
34349 all (ie, they typed "@catch NSException *e"). So, just
34350 forget about the closing parenthesis and keep going. */
34352 objc_begin_catch_clause (parameter_declaration
);
34353 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
34354 objc_finish_catch_clause ();
34356 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AT_FINALLY
))
34358 cp_lexer_consume_token (parser
->lexer
);
34359 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
34360 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
34361 node, lest it get absorbed into the surrounding block. */
34362 stmt
= push_stmt_list ();
34363 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
34364 objc_build_finally_clause (location
, pop_stmt_list (stmt
));
34367 return objc_finish_try_stmt ();
34370 /* Parse an Objective-C synchronized statement.
34372 objc-synchronized-stmt:
34373 @synchronized ( expression ) compound-statement
34375 Returns NULL_TREE. */
34378 cp_parser_objc_synchronized_statement (cp_parser
*parser
)
34380 location_t location
;
34383 cp_parser_require_keyword (parser
, RID_AT_SYNCHRONIZED
, RT_AT_SYNCHRONIZED
);
34385 location
= cp_lexer_peek_token (parser
->lexer
)->location
;
34386 objc_maybe_warn_exceptions (location
);
34387 matching_parens parens
;
34388 parens
.require_open (parser
);
34389 lock
= cp_parser_expression (parser
);
34390 parens
.require_close (parser
);
34392 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
34393 node, lest it get absorbed into the surrounding block. */
34394 stmt
= push_stmt_list ();
34395 cp_parser_compound_statement (parser
, NULL
, BCS_NORMAL
, false);
34397 return objc_build_synchronized (location
, lock
, pop_stmt_list (stmt
));
34400 /* Parse an Objective-C throw statement.
34403 @throw assignment-expression [opt] ;
34405 Returns a constructed '@throw' statement. */
34408 cp_parser_objc_throw_statement (cp_parser
*parser
)
34410 tree expr
= NULL_TREE
;
34411 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34413 cp_parser_require_keyword (parser
, RID_AT_THROW
, RT_AT_THROW
);
34415 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34416 expr
= cp_parser_expression (parser
);
34418 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34420 return objc_build_throw_stmt (loc
, expr
);
34423 /* Parse an Objective-C statement. */
34426 cp_parser_objc_statement (cp_parser
* parser
)
34428 /* Try to figure out what kind of declaration is present. */
34429 cp_token
*kwd
= cp_lexer_peek_token (parser
->lexer
);
34431 switch (kwd
->keyword
)
34434 return cp_parser_objc_try_catch_finally_statement (parser
);
34435 case RID_AT_SYNCHRONIZED
:
34436 return cp_parser_objc_synchronized_statement (parser
);
34438 return cp_parser_objc_throw_statement (parser
);
34440 error_at (kwd
->location
, "misplaced %<@%D%> Objective-C++ construct",
34442 cp_parser_skip_to_end_of_block_or_statement (parser
);
34445 return error_mark_node
;
34448 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
34449 look ahead to see if an objc keyword follows the attributes. This
34450 is to detect the use of prefix attributes on ObjC @interface and
34454 cp_parser_objc_valid_prefix_attributes (cp_parser
* parser
, tree
*attrib
)
34456 cp_lexer_save_tokens (parser
->lexer
);
34457 tree addon
= cp_parser_attributes_opt (parser
);
34459 && OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser
->lexer
)->keyword
))
34461 cp_lexer_commit_tokens (parser
->lexer
);
34463 TREE_CHAIN (*attrib
) = addon
;
34468 cp_lexer_rollback_tokens (parser
->lexer
);
34472 /* This routine is a minimal replacement for
34473 c_parser_struct_declaration () used when parsing the list of
34474 types/names or ObjC++ properties. For example, when parsing the
34477 @property (readonly) int a, b, c;
34479 this function is responsible for parsing "int a, int b, int c" and
34480 returning the declarations as CHAIN of DECLs.
34482 TODO: Share this code with cp_parser_objc_class_ivars. It's very
34483 similar parsing. */
34485 cp_parser_objc_struct_declaration (cp_parser
*parser
)
34487 tree decls
= NULL_TREE
;
34488 cp_decl_specifier_seq declspecs
;
34489 int decl_class_or_enum_p
;
34490 tree prefix_attributes
;
34492 cp_parser_decl_specifier_seq (parser
,
34493 CP_PARSER_FLAGS_NONE
,
34495 &decl_class_or_enum_p
);
34497 if (declspecs
.type
== error_mark_node
)
34498 return error_mark_node
;
34500 /* auto, register, static, extern, mutable. */
34501 if (declspecs
.storage_class
!= sc_none
)
34503 cp_parser_error (parser
, "invalid type for property");
34504 declspecs
.storage_class
= sc_none
;
34507 /* thread_local. */
34508 if (decl_spec_seq_has_spec_p (&declspecs
, ds_thread
))
34510 cp_parser_error (parser
, "invalid type for property");
34511 declspecs
.locations
[ds_thread
] = 0;
34515 if (decl_spec_seq_has_spec_p (&declspecs
, ds_typedef
))
34517 cp_parser_error (parser
, "invalid type for property");
34518 declspecs
.locations
[ds_typedef
] = 0;
34521 prefix_attributes
= declspecs
.attributes
;
34522 declspecs
.attributes
= NULL_TREE
;
34524 /* Keep going until we hit the `;' at the end of the declaration. */
34525 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
34527 tree attributes
, first_attribute
, decl
;
34528 cp_declarator
*declarator
;
34531 /* Parse the declarator. */
34532 declarator
= cp_parser_declarator (parser
, CP_PARSER_DECLARATOR_NAMED
,
34533 CP_PARSER_FLAGS_NONE
,
34534 NULL
, NULL
, false, false, false);
34536 /* Look for attributes that apply to the ivar. */
34537 attributes
= cp_parser_attributes_opt (parser
);
34538 /* Remember which attributes are prefix attributes and
34540 first_attribute
= attributes
;
34541 /* Combine the attributes. */
34542 attributes
= attr_chainon (prefix_attributes
, attributes
);
34544 decl
= grokfield (declarator
, &declspecs
,
34545 NULL_TREE
, /*init_const_expr_p=*/false,
34546 NULL_TREE
, attributes
);
34548 if (decl
== error_mark_node
|| decl
== NULL_TREE
)
34549 return error_mark_node
;
34551 /* Reset PREFIX_ATTRIBUTES. */
34552 if (attributes
!= error_mark_node
)
34554 while (attributes
&& TREE_CHAIN (attributes
) != first_attribute
)
34555 attributes
= TREE_CHAIN (attributes
);
34557 TREE_CHAIN (attributes
) = NULL_TREE
;
34560 DECL_CHAIN (decl
) = decls
;
34563 token
= cp_lexer_peek_token (parser
->lexer
);
34564 if (token
->type
== CPP_COMMA
)
34566 cp_lexer_consume_token (parser
->lexer
); /* Eat ','. */
34575 /* Parse an Objective-C @property declaration. The syntax is:
34577 objc-property-declaration:
34578 '@property' objc-property-attributes[opt] struct-declaration ;
34580 objc-property-attributes:
34581 '(' objc-property-attribute-list ')'
34583 objc-property-attribute-list:
34584 objc-property-attribute
34585 objc-property-attribute-list, objc-property-attribute
34587 objc-property-attribute
34588 'getter' = identifier
34589 'setter' = identifier
34598 @property NSString *name;
34599 @property (readonly) id object;
34600 @property (retain, nonatomic, getter=getTheName) id name;
34601 @property int a, b, c;
34603 PS: This function is identical to
34604 c_parser_objc_at_property_declaration for C. Keep them in sync. */
34606 cp_parser_objc_at_property_declaration (cp_parser
*parser
)
34608 /* Parse the optional attribute list.
34610 A list of parsed, but not verified, attributes. */
34611 vec
<property_attribute_info
*> prop_attr_list
= vNULL
;
34612 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34614 cp_lexer_consume_token (parser
->lexer
); /* Eat '@property'. */
34616 /* Parse the optional attribute list... */
34617 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
34620 matching_parens parens
;
34621 location_t attr_start
= cp_lexer_peek_token (parser
->lexer
)->location
;
34622 parens
.consume_open (parser
);
34623 bool syntax_error
= false;
34625 /* Allow empty @property attribute lists, but with a warning. */
34626 location_t attr_end
= cp_lexer_peek_token (parser
->lexer
)->location
;
34627 location_t attr_comb
;
34628 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
34630 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
34631 warning_at (attr_comb
, OPT_Wattributes
,
34632 "empty property attribute list");
34637 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
34638 attr_start
= token
->location
;
34639 attr_end
= get_finish (token
->location
);
34640 attr_comb
= make_location (attr_start
, attr_start
, attr_end
);
34642 if (token
->type
== CPP_CLOSE_PAREN
|| token
->type
== CPP_COMMA
)
34644 warning_at (attr_comb
, OPT_Wattributes
,
34645 "missing property attribute");
34646 if (token
->type
== CPP_CLOSE_PAREN
)
34648 cp_lexer_consume_token (parser
->lexer
);
34652 tree attr_name
= NULL_TREE
;
34653 if (identifier_p (token
->u
.value
))
34654 attr_name
= token
->u
.value
;
34657 if (token
->type
== CPP_NAME
)
34658 keyword
= C_RID_CODE (token
->u
.value
);
34659 else if (token
->type
== CPP_KEYWORD
34660 && token
->keyword
== RID_CLASS
)
34661 /* Account for accepting the 'class' keyword in this context. */
34662 keyword
= RID_CLASS
;
34664 keyword
= RID_MAX
; /* By definition, an unknown property. */
34665 cp_lexer_consume_token (parser
->lexer
);
34667 enum objc_property_attribute_kind prop_kind
34668 = objc_prop_attr_kind_for_rid (keyword
);
34669 property_attribute_info
*prop
34670 = new property_attribute_info (attr_name
, attr_comb
, prop_kind
);
34671 prop_attr_list
.safe_push (prop
);
34674 switch (prop
->prop_kind
)
34677 case OBJC_PROPERTY_ATTR_UNKNOWN
:
34679 error_at (attr_start
, "unknown property attribute %qE",
34682 error_at (attr_start
, "unknown property attribute");
34683 prop
->parse_error
= syntax_error
= true;
34686 case OBJC_PROPERTY_ATTR_GETTER
:
34687 case OBJC_PROPERTY_ATTR_SETTER
:
34688 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
34690 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
34691 error_at (attr_comb
, "expected %<=%> after Objective-C %qE",
34693 prop
->parse_error
= syntax_error
= true;
34697 token
= cp_lexer_peek_token (parser
->lexer
);
34698 attr_end
= token
->location
;
34699 cp_lexer_consume_token (parser
->lexer
); /* eat the = */
34701 if (!cp_parser_objc_selector_p
34702 (cp_lexer_peek_token (parser
->lexer
)->type
))
34704 attr_comb
= make_location (attr_end
, attr_start
, attr_end
);
34705 error_at (attr_comb
, "expected %qE selector name",
34707 prop
->parse_error
= syntax_error
= true;
34711 /* Get the end of the method name, and consume the name. */
34712 token
= cp_lexer_peek_token (parser
->lexer
);
34713 attr_end
= get_finish (token
->location
);
34714 /* Because method names may contain C++ keywords, we have a
34715 routine to fetch them (this also consumes the token). */
34716 meth_name
= cp_parser_objc_selector (parser
);
34718 if (prop
->prop_kind
== OBJC_PROPERTY_ATTR_SETTER
)
34720 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COLON
))
34722 attr_comb
= make_location (attr_end
, attr_start
,
34724 error_at (attr_comb
, "setter method names must"
34725 " terminate with %<:%>");
34726 prop
->parse_error
= syntax_error
= true;
34730 attr_end
= get_finish (cp_lexer_peek_token
34731 (parser
->lexer
)->location
);
34732 cp_lexer_consume_token (parser
->lexer
);
34734 attr_comb
= make_location (attr_start
, attr_start
,
34738 attr_comb
= make_location (attr_start
, attr_start
,
34740 prop
->ident
= meth_name
;
34741 /* Updated location including all that was successfully
34743 prop
->prop_loc
= attr_comb
;
34747 /* If we see a comma here, then keep going - even if we already
34748 saw a syntax error. For simple mistakes e.g. (asign, getter=x)
34749 this makes a more useful output and avoid spurious warnings
34750 about missing attributes that are, in fact, specified after the
34751 one with the syntax error. */
34752 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34753 cp_lexer_consume_token (parser
->lexer
);
34758 if (syntax_error
|| !parens
.require_close (parser
))
34759 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
34760 /*or_comma=*/false,
34761 /*consume_paren=*/true);
34764 /* 'properties' is the list of properties that we read. Usually a
34765 single one, but maybe more (eg, in "@property int a, b, c;" there
34767 TODO: Update this parsing so that it accepts (erroneous) bitfields so
34768 that we can issue a meaningful and consistent (between C/C++) error
34769 message from objc_add_property_declaration (). */
34770 tree properties
= cp_parser_objc_struct_declaration (parser
);
34772 if (properties
== error_mark_node
)
34773 cp_parser_skip_to_end_of_statement (parser
);
34774 else if (properties
== NULL_TREE
)
34775 cp_parser_error (parser
, "expected identifier");
34778 /* Comma-separated properties are chained together in reverse order;
34779 add them one by one. */
34780 properties
= nreverse (properties
);
34781 for (; properties
; properties
= TREE_CHAIN (properties
))
34782 objc_add_property_declaration (loc
, copy_node (properties
),
34786 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34788 while (!prop_attr_list
.is_empty())
34789 delete prop_attr_list
.pop ();
34790 prop_attr_list
.release ();
34793 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
34795 objc-synthesize-declaration:
34796 @synthesize objc-synthesize-identifier-list ;
34798 objc-synthesize-identifier-list:
34799 objc-synthesize-identifier
34800 objc-synthesize-identifier-list, objc-synthesize-identifier
34802 objc-synthesize-identifier
34804 identifier = identifier
34807 @synthesize MyProperty;
34808 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
34810 PS: This function is identical to c_parser_objc_at_synthesize_declaration
34811 for C. Keep them in sync.
34814 cp_parser_objc_at_synthesize_declaration (cp_parser
*parser
)
34816 tree list
= NULL_TREE
;
34818 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34820 cp_lexer_consume_token (parser
->lexer
); /* Eat '@synthesize'. */
34823 tree property
, ivar
;
34824 property
= cp_parser_identifier (parser
);
34825 if (property
== error_mark_node
)
34827 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EQ
))
34832 cp_lexer_consume_token (parser
->lexer
);
34833 ivar
= cp_parser_identifier (parser
);
34834 if (ivar
== error_mark_node
)
34836 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34842 list
= chainon (list
, build_tree_list (ivar
, property
));
34843 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34844 cp_lexer_consume_token (parser
->lexer
);
34848 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34849 objc_add_synthesize_declaration (loc
, list
);
34852 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
34854 objc-dynamic-declaration:
34855 @dynamic identifier-list ;
34858 @dynamic MyProperty;
34859 @dynamic MyProperty, AnotherProperty;
34861 PS: This function is identical to c_parser_objc_at_dynamic_declaration
34862 for C. Keep them in sync.
34865 cp_parser_objc_at_dynamic_declaration (cp_parser
*parser
)
34867 tree list
= NULL_TREE
;
34869 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
34871 cp_lexer_consume_token (parser
->lexer
); /* Eat '@dynamic'. */
34875 property
= cp_parser_identifier (parser
);
34876 if (property
== error_mark_node
)
34878 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34881 list
= chainon (list
, build_tree_list (NULL
, property
));
34882 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
34883 cp_lexer_consume_token (parser
->lexer
);
34887 cp_parser_consume_semicolon_at_end_of_statement (parser
);
34888 objc_add_dynamic_declaration (loc
, list
);
34892 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 / 4.5 / 5.0 parsing routines. */
34894 /* Returns name of the next clause.
34895 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
34896 the token is not consumed. Otherwise appropriate pragma_omp_clause is
34897 returned and the token is consumed. */
34899 static pragma_omp_clause
34900 cp_parser_omp_clause_name (cp_parser
*parser
)
34902 pragma_omp_clause result
= PRAGMA_OMP_CLAUSE_NONE
;
34904 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
34905 result
= PRAGMA_OACC_CLAUSE_AUTO
;
34906 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_IF
))
34907 result
= PRAGMA_OMP_CLAUSE_IF
;
34908 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
34909 result
= PRAGMA_OMP_CLAUSE_DEFAULT
;
34910 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
))
34911 result
= PRAGMA_OACC_CLAUSE_DELETE
;
34912 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_PRIVATE
))
34913 result
= PRAGMA_OMP_CLAUSE_PRIVATE
;
34914 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
34915 result
= PRAGMA_OMP_CLAUSE_FOR
;
34916 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
34918 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
34919 const char *p
= IDENTIFIER_POINTER (id
);
34924 if (!strcmp ("aligned", p
))
34925 result
= PRAGMA_OMP_CLAUSE_ALIGNED
;
34926 else if (!strcmp ("allocate", p
))
34927 result
= PRAGMA_OMP_CLAUSE_ALLOCATE
;
34928 else if (!strcmp ("async", p
))
34929 result
= PRAGMA_OACC_CLAUSE_ASYNC
;
34930 else if (!strcmp ("attach", p
))
34931 result
= PRAGMA_OACC_CLAUSE_ATTACH
;
34934 if (!strcmp ("bind", p
))
34935 result
= PRAGMA_OMP_CLAUSE_BIND
;
34938 if (!strcmp ("collapse", p
))
34939 result
= PRAGMA_OMP_CLAUSE_COLLAPSE
;
34940 else if (!strcmp ("copy", p
))
34941 result
= PRAGMA_OACC_CLAUSE_COPY
;
34942 else if (!strcmp ("copyin", p
))
34943 result
= PRAGMA_OMP_CLAUSE_COPYIN
;
34944 else if (!strcmp ("copyout", p
))
34945 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
34946 else if (!strcmp ("copyprivate", p
))
34947 result
= PRAGMA_OMP_CLAUSE_COPYPRIVATE
;
34948 else if (!strcmp ("create", p
))
34949 result
= PRAGMA_OACC_CLAUSE_CREATE
;
34952 if (!strcmp ("defaultmap", p
))
34953 result
= PRAGMA_OMP_CLAUSE_DEFAULTMAP
;
34954 else if (!strcmp ("depend", p
))
34955 result
= PRAGMA_OMP_CLAUSE_DEPEND
;
34956 else if (!strcmp ("detach", p
))
34957 result
= PRAGMA_OACC_CLAUSE_DETACH
;
34958 else if (!strcmp ("device", p
))
34959 result
= PRAGMA_OMP_CLAUSE_DEVICE
;
34960 else if (!strcmp ("deviceptr", p
))
34961 result
= PRAGMA_OACC_CLAUSE_DEVICEPTR
;
34962 else if (!strcmp ("device_resident", p
))
34963 result
= PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
;
34964 else if (!strcmp ("device_type", p
))
34965 result
= PRAGMA_OMP_CLAUSE_DEVICE_TYPE
;
34966 else if (!strcmp ("dist_schedule", p
))
34967 result
= PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
;
34970 if (!strcmp ("final", p
))
34971 result
= PRAGMA_OMP_CLAUSE_FINAL
;
34972 else if (!strcmp ("finalize", p
))
34973 result
= PRAGMA_OACC_CLAUSE_FINALIZE
;
34974 else if (!strcmp ("firstprivate", p
))
34975 result
= PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
;
34976 else if (!strcmp ("from", p
))
34977 result
= PRAGMA_OMP_CLAUSE_FROM
;
34980 if (!strcmp ("gang", p
))
34981 result
= PRAGMA_OACC_CLAUSE_GANG
;
34982 else if (!strcmp ("grainsize", p
))
34983 result
= PRAGMA_OMP_CLAUSE_GRAINSIZE
;
34986 if (!strcmp ("hint", p
))
34987 result
= PRAGMA_OMP_CLAUSE_HINT
;
34988 else if (!strcmp ("host", p
))
34989 result
= PRAGMA_OACC_CLAUSE_HOST
;
34992 if (!strcmp ("if_present", p
))
34993 result
= PRAGMA_OACC_CLAUSE_IF_PRESENT
;
34994 else if (!strcmp ("in_reduction", p
))
34995 result
= PRAGMA_OMP_CLAUSE_IN_REDUCTION
;
34996 else if (!strcmp ("inbranch", p
))
34997 result
= PRAGMA_OMP_CLAUSE_INBRANCH
;
34998 else if (!strcmp ("independent", p
))
34999 result
= PRAGMA_OACC_CLAUSE_INDEPENDENT
;
35000 else if (!strcmp ("is_device_ptr", p
))
35001 result
= PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
;
35004 if (!strcmp ("lastprivate", p
))
35005 result
= PRAGMA_OMP_CLAUSE_LASTPRIVATE
;
35006 else if (!strcmp ("linear", p
))
35007 result
= PRAGMA_OMP_CLAUSE_LINEAR
;
35008 else if (!strcmp ("link", p
))
35009 result
= PRAGMA_OMP_CLAUSE_LINK
;
35012 if (!strcmp ("map", p
))
35013 result
= PRAGMA_OMP_CLAUSE_MAP
;
35014 else if (!strcmp ("mergeable", p
))
35015 result
= PRAGMA_OMP_CLAUSE_MERGEABLE
;
35018 if (!strcmp ("no_create", p
))
35019 result
= PRAGMA_OACC_CLAUSE_NO_CREATE
;
35020 else if (!strcmp ("nogroup", p
))
35021 result
= PRAGMA_OMP_CLAUSE_NOGROUP
;
35022 else if (!strcmp ("nontemporal", p
))
35023 result
= PRAGMA_OMP_CLAUSE_NONTEMPORAL
;
35024 else if (!strcmp ("notinbranch", p
))
35025 result
= PRAGMA_OMP_CLAUSE_NOTINBRANCH
;
35026 else if (!strcmp ("nowait", p
))
35027 result
= PRAGMA_OMP_CLAUSE_NOWAIT
;
35028 else if (!strcmp ("num_gangs", p
))
35029 result
= PRAGMA_OACC_CLAUSE_NUM_GANGS
;
35030 else if (!strcmp ("num_tasks", p
))
35031 result
= PRAGMA_OMP_CLAUSE_NUM_TASKS
;
35032 else if (!strcmp ("num_teams", p
))
35033 result
= PRAGMA_OMP_CLAUSE_NUM_TEAMS
;
35034 else if (!strcmp ("num_threads", p
))
35035 result
= PRAGMA_OMP_CLAUSE_NUM_THREADS
;
35036 else if (!strcmp ("num_workers", p
))
35037 result
= PRAGMA_OACC_CLAUSE_NUM_WORKERS
;
35040 if (!strcmp ("ordered", p
))
35041 result
= PRAGMA_OMP_CLAUSE_ORDERED
;
35042 else if (!strcmp ("order", p
))
35043 result
= PRAGMA_OMP_CLAUSE_ORDER
;
35046 if (!strcmp ("parallel", p
))
35047 result
= PRAGMA_OMP_CLAUSE_PARALLEL
;
35048 else if (!strcmp ("present", p
))
35049 result
= PRAGMA_OACC_CLAUSE_PRESENT
;
35050 else if (!strcmp ("present_or_copy", p
)
35051 || !strcmp ("pcopy", p
))
35052 result
= PRAGMA_OACC_CLAUSE_COPY
;
35053 else if (!strcmp ("present_or_copyin", p
)
35054 || !strcmp ("pcopyin", p
))
35055 result
= PRAGMA_OACC_CLAUSE_COPYIN
;
35056 else if (!strcmp ("present_or_copyout", p
)
35057 || !strcmp ("pcopyout", p
))
35058 result
= PRAGMA_OACC_CLAUSE_COPYOUT
;
35059 else if (!strcmp ("present_or_create", p
)
35060 || !strcmp ("pcreate", p
))
35061 result
= PRAGMA_OACC_CLAUSE_CREATE
;
35062 else if (!strcmp ("priority", p
))
35063 result
= PRAGMA_OMP_CLAUSE_PRIORITY
;
35064 else if (!strcmp ("proc_bind", p
))
35065 result
= PRAGMA_OMP_CLAUSE_PROC_BIND
;
35068 if (!strcmp ("reduction", p
))
35069 result
= PRAGMA_OMP_CLAUSE_REDUCTION
;
35072 if (!strcmp ("safelen", p
))
35073 result
= PRAGMA_OMP_CLAUSE_SAFELEN
;
35074 else if (!strcmp ("schedule", p
))
35075 result
= PRAGMA_OMP_CLAUSE_SCHEDULE
;
35076 else if (!strcmp ("sections", p
))
35077 result
= PRAGMA_OMP_CLAUSE_SECTIONS
;
35078 else if (!strcmp ("self", p
)) /* "self" is a synonym for "host". */
35079 result
= PRAGMA_OACC_CLAUSE_HOST
;
35080 else if (!strcmp ("seq", p
))
35081 result
= PRAGMA_OACC_CLAUSE_SEQ
;
35082 else if (!strcmp ("shared", p
))
35083 result
= PRAGMA_OMP_CLAUSE_SHARED
;
35084 else if (!strcmp ("simd", p
))
35085 result
= PRAGMA_OMP_CLAUSE_SIMD
;
35086 else if (!strcmp ("simdlen", p
))
35087 result
= PRAGMA_OMP_CLAUSE_SIMDLEN
;
35090 if (!strcmp ("task_reduction", p
))
35091 result
= PRAGMA_OMP_CLAUSE_TASK_REDUCTION
;
35092 else if (!strcmp ("taskgroup", p
))
35093 result
= PRAGMA_OMP_CLAUSE_TASKGROUP
;
35094 else if (!strcmp ("thread_limit", p
))
35095 result
= PRAGMA_OMP_CLAUSE_THREAD_LIMIT
;
35096 else if (!strcmp ("threads", p
))
35097 result
= PRAGMA_OMP_CLAUSE_THREADS
;
35098 else if (!strcmp ("tile", p
))
35099 result
= PRAGMA_OACC_CLAUSE_TILE
;
35100 else if (!strcmp ("to", p
))
35101 result
= PRAGMA_OMP_CLAUSE_TO
;
35104 if (!strcmp ("uniform", p
))
35105 result
= PRAGMA_OMP_CLAUSE_UNIFORM
;
35106 else if (!strcmp ("untied", p
))
35107 result
= PRAGMA_OMP_CLAUSE_UNTIED
;
35108 else if (!strcmp ("use_device", p
))
35109 result
= PRAGMA_OACC_CLAUSE_USE_DEVICE
;
35110 else if (!strcmp ("use_device_addr", p
))
35111 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
;
35112 else if (!strcmp ("use_device_ptr", p
))
35113 result
= PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
;
35116 if (!strcmp ("vector", p
))
35117 result
= PRAGMA_OACC_CLAUSE_VECTOR
;
35118 else if (!strcmp ("vector_length", p
))
35119 result
= PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
;
35122 if (!strcmp ("wait", p
))
35123 result
= PRAGMA_OACC_CLAUSE_WAIT
;
35124 else if (!strcmp ("worker", p
))
35125 result
= PRAGMA_OACC_CLAUSE_WORKER
;
35130 if (result
!= PRAGMA_OMP_CLAUSE_NONE
)
35131 cp_lexer_consume_token (parser
->lexer
);
35136 /* Validate that a clause of the given type does not already exist. */
35139 check_no_duplicate_clause (tree clauses
, enum omp_clause_code code
,
35140 const char *name
, location_t location
)
35142 if (omp_find_clause (clauses
, code
))
35143 error_at (location
, "too many %qs clauses", name
);
35149 variable-list , identifier
35151 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
35152 colon). An opening parenthesis will have been consumed by the caller.
35154 If KIND is nonzero, create the appropriate node and install the decl
35155 in OMP_CLAUSE_DECL and add the node to the head of the list.
35157 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
35158 return the list created.
35160 COLON can be NULL if only closing parenthesis should end the list,
35161 or pointer to bool which will receive false if the list is terminated
35162 by closing parenthesis or true if the list is terminated by colon.
35164 The optional ALLOW_DEREF argument is true if list items can use the deref
35168 cp_parser_omp_var_list_no_open (cp_parser
*parser
, enum omp_clause_code kind
,
35169 tree list
, bool *colon
,
35170 bool allow_deref
= false)
35173 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
35176 parser
->colon_corrects_to_scope_p
= false;
35183 if (kind
== OMP_CLAUSE_DEPEND
)
35184 cp_parser_parse_tentatively (parser
);
35185 token
= cp_lexer_peek_token (parser
->lexer
);
35187 && current_class_ptr
35188 && cp_parser_is_keyword (token
, RID_THIS
))
35190 decl
= finish_this_expr ();
35191 if (TREE_CODE (decl
) == NON_LVALUE_EXPR
35192 || CONVERT_EXPR_P (decl
))
35193 decl
= TREE_OPERAND (decl
, 0);
35194 cp_lexer_consume_token (parser
->lexer
);
35196 else if (cp_parser_is_keyword (token
, RID_FUNCTION_NAME
)
35197 || cp_parser_is_keyword (token
, RID_PRETTY_FUNCTION_NAME
)
35198 || cp_parser_is_keyword (token
, RID_C99_FUNCTION_NAME
))
35201 decl
= cp_parser_primary_expression (parser
, false, false, false,
35206 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
35207 /*check_dependency_p=*/true,
35208 /*template_p=*/NULL
,
35209 /*declarator_p=*/false,
35210 /*optional_p=*/false);
35211 if (name
== error_mark_node
)
35213 if (kind
== OMP_CLAUSE_DEPEND
35214 && cp_parser_simulate_error (parser
))
35215 goto depend_lvalue
;
35219 if (identifier_p (name
))
35220 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
35223 if (decl
== error_mark_node
)
35225 if (kind
== OMP_CLAUSE_DEPEND
35226 && cp_parser_simulate_error (parser
))
35227 goto depend_lvalue
;
35228 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
35232 if (outer_automatic_var_p (decl
))
35233 decl
= process_outer_var_ref (decl
, tf_warning_or_error
);
35234 if (decl
== error_mark_node
)
35236 else if (kind
!= 0)
35240 case OMP_CLAUSE__CACHE_
:
35241 /* The OpenACC cache directive explicitly only allows "array
35242 elements or subarrays". */
35243 if (cp_lexer_peek_token (parser
->lexer
)->type
!= CPP_OPEN_SQUARE
)
35245 error_at (token
->location
, "expected %<[%>");
35246 decl
= error_mark_node
;
35250 case OMP_CLAUSE_MAP
:
35251 case OMP_CLAUSE_FROM
:
35252 case OMP_CLAUSE_TO
:
35253 while (cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
35255 && cp_lexer_next_token_is (parser
->lexer
, CPP_DEREF
)))
35258 = cp_lexer_next_token_is (parser
->lexer
, CPP_DOT
)
35259 ? CPP_DOT
: CPP_DEREF
;
35261 = cp_lexer_peek_token (parser
->lexer
)->location
;
35262 cp_id_kind idk
= CP_ID_KIND_NONE
;
35263 cp_lexer_consume_token (parser
->lexer
);
35264 decl
= convert_from_reference (decl
);
35266 = cp_parser_postfix_dot_deref_expression (parser
, ttype
,
35271 case OMP_CLAUSE_DEPEND
:
35272 case OMP_CLAUSE_REDUCTION
:
35273 case OMP_CLAUSE_IN_REDUCTION
:
35274 case OMP_CLAUSE_TASK_REDUCTION
:
35275 while (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_SQUARE
))
35277 tree low_bound
= NULL_TREE
, length
= NULL_TREE
;
35279 parser
->colon_corrects_to_scope_p
= false;
35280 cp_lexer_consume_token (parser
->lexer
);
35281 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
35283 low_bound
= cp_parser_expression (parser
);
35284 /* Later handling is not prepared to see through these. */
35285 gcc_checking_assert (!location_wrapper_p (low_bound
));
35288 parser
->colon_corrects_to_scope_p
35289 = saved_colon_corrects_to_scope_p
;
35290 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_SQUARE
))
35291 length
= integer_one_node
;
35294 /* Look for `:'. */
35295 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35297 if (kind
== OMP_CLAUSE_DEPEND
35298 && cp_parser_simulate_error (parser
))
35299 goto depend_lvalue
;
35302 if (kind
== OMP_CLAUSE_DEPEND
)
35303 cp_parser_commit_to_tentative_parse (parser
);
35304 if (!cp_lexer_next_token_is (parser
->lexer
,
35307 length
= cp_parser_expression (parser
);
35308 /* Later handling is not prepared to see through these. */
35309 gcc_checking_assert (!location_wrapper_p (length
));
35312 /* Look for the closing `]'. */
35313 if (!cp_parser_require (parser
, CPP_CLOSE_SQUARE
,
35316 if (kind
== OMP_CLAUSE_DEPEND
35317 && cp_parser_simulate_error (parser
))
35318 goto depend_lvalue
;
35322 decl
= tree_cons (low_bound
, length
, decl
);
35329 if (kind
== OMP_CLAUSE_DEPEND
)
35331 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
)
35332 && cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
)
35333 && cp_parser_simulate_error (parser
))
35336 cp_parser_abort_tentative_parse (parser
);
35337 decl
= cp_parser_assignment_expression (parser
, NULL
,
35341 cp_parser_parse_definitely (parser
);
35344 tree u
= build_omp_clause (token
->location
, kind
);
35345 OMP_CLAUSE_DECL (u
) = decl
;
35346 OMP_CLAUSE_CHAIN (u
) = list
;
35350 list
= tree_cons (decl
, NULL_TREE
, list
);
35353 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
35355 cp_lexer_consume_token (parser
->lexer
);
35359 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
35361 if (colon
!= NULL
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
35364 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
35368 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
35372 /* Try to resync to an unnested comma. Copied from
35373 cp_parser_parenthesized_expression_list. */
35376 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
35377 ending
= cp_parser_skip_to_closing_parenthesis (parser
,
35378 /*recovering=*/true,
35380 /*consume_paren=*/true);
35388 /* Similarly, but expect leading and trailing parenthesis. This is a very
35389 common case for omp clauses. */
35392 cp_parser_omp_var_list (cp_parser
*parser
, enum omp_clause_code kind
, tree list
,
35393 bool allow_deref
= false)
35395 if (cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35396 return cp_parser_omp_var_list_no_open (parser
, kind
, list
, NULL
,
35402 copy ( variable-list )
35403 copyin ( variable-list )
35404 copyout ( variable-list )
35405 create ( variable-list )
35406 delete ( variable-list )
35407 present ( variable-list )
35410 no_create ( variable-list )
35411 attach ( variable-list )
35412 detach ( variable-list ) */
35415 cp_parser_oacc_data_clause (cp_parser
*parser
, pragma_omp_clause c_kind
,
35418 enum gomp_map_kind kind
;
35421 case PRAGMA_OACC_CLAUSE_ATTACH
:
35422 kind
= GOMP_MAP_ATTACH
;
35424 case PRAGMA_OACC_CLAUSE_COPY
:
35425 kind
= GOMP_MAP_TOFROM
;
35427 case PRAGMA_OACC_CLAUSE_COPYIN
:
35428 kind
= GOMP_MAP_TO
;
35430 case PRAGMA_OACC_CLAUSE_COPYOUT
:
35431 kind
= GOMP_MAP_FROM
;
35433 case PRAGMA_OACC_CLAUSE_CREATE
:
35434 kind
= GOMP_MAP_ALLOC
;
35436 case PRAGMA_OACC_CLAUSE_DELETE
:
35437 kind
= GOMP_MAP_RELEASE
;
35439 case PRAGMA_OACC_CLAUSE_DETACH
:
35440 kind
= GOMP_MAP_DETACH
;
35442 case PRAGMA_OACC_CLAUSE_DEVICE
:
35443 kind
= GOMP_MAP_FORCE_TO
;
35445 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
35446 kind
= GOMP_MAP_DEVICE_RESIDENT
;
35448 case PRAGMA_OACC_CLAUSE_HOST
:
35449 kind
= GOMP_MAP_FORCE_FROM
;
35451 case PRAGMA_OACC_CLAUSE_LINK
:
35452 kind
= GOMP_MAP_LINK
;
35454 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
35455 kind
= GOMP_MAP_IF_PRESENT
;
35457 case PRAGMA_OACC_CLAUSE_PRESENT
:
35458 kind
= GOMP_MAP_FORCE_PRESENT
;
35461 gcc_unreachable ();
35464 nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_MAP
, list
, true);
35466 for (c
= nl
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
35467 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
35473 deviceptr ( variable-list ) */
35476 cp_parser_oacc_data_clause_deviceptr (cp_parser
*parser
, tree list
)
35478 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35481 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
35482 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
35483 variable-list must only allow for pointer variables. */
35484 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
35485 for (t
= vars
; t
; t
= TREE_CHAIN (t
))
35487 tree v
= TREE_PURPOSE (t
);
35488 tree u
= build_omp_clause (loc
, OMP_CLAUSE_MAP
);
35489 OMP_CLAUSE_SET_MAP_KIND (u
, GOMP_MAP_FORCE_DEVICEPTR
);
35490 OMP_CLAUSE_DECL (u
) = v
;
35491 OMP_CLAUSE_CHAIN (u
) = list
;
35506 cp_parser_oacc_simple_clause (location_t loc
, enum omp_clause_code code
,
35509 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], loc
);
35511 tree c
= build_omp_clause (loc
, code
);
35512 OMP_CLAUSE_CHAIN (c
) = list
;
35518 num_gangs ( expression )
35519 num_workers ( expression )
35520 vector_length ( expression ) */
35523 cp_parser_oacc_single_int_clause (cp_parser
*parser
, omp_clause_code code
,
35524 const char *str
, tree list
)
35526 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35528 matching_parens parens
;
35529 if (!parens
.require_open (parser
))
35532 tree t
= cp_parser_assignment_expression (parser
, NULL
, false, false);
35534 if (t
== error_mark_node
35535 || !parens
.require_close (parser
))
35537 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35538 /*or_comma=*/false,
35539 /*consume_paren=*/true);
35543 check_no_duplicate_clause (list
, code
, str
, loc
);
35545 tree c
= build_omp_clause (loc
, code
);
35546 OMP_CLAUSE_OPERAND (c
, 0) = t
;
35547 OMP_CLAUSE_CHAIN (c
) = list
;
35553 gang [( gang-arg-list )]
35554 worker [( [num:] int-expr )]
35555 vector [( [length:] int-expr )]
35557 where gang-arg is one of:
35562 and size-expr may be:
35569 cp_parser_oacc_shape_clause (cp_parser
*parser
, location_t loc
,
35570 omp_clause_code kind
,
35571 const char *str
, tree list
)
35573 const char *id
= "num";
35574 cp_lexer
*lexer
= parser
->lexer
;
35575 tree ops
[2] = { NULL_TREE
, NULL_TREE
}, c
;
35577 if (kind
== OMP_CLAUSE_VECTOR
)
35580 if (cp_lexer_next_token_is (lexer
, CPP_OPEN_PAREN
))
35582 matching_parens parens
;
35583 parens
.consume_open (parser
);
35587 cp_token
*next
= cp_lexer_peek_token (lexer
);
35590 /* Gang static argument. */
35591 if (kind
== OMP_CLAUSE_GANG
35592 && cp_lexer_next_token_is_keyword (lexer
, RID_STATIC
))
35594 cp_lexer_consume_token (lexer
);
35596 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
35597 goto cleanup_error
;
35600 if (ops
[idx
] != NULL
)
35602 cp_parser_error (parser
, "too many %<static%> arguments");
35603 goto cleanup_error
;
35606 /* Check for the '*' argument. */
35607 if (cp_lexer_next_token_is (lexer
, CPP_MULT
)
35608 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
35609 || cp_lexer_nth_token_is (parser
->lexer
, 2,
35612 cp_lexer_consume_token (lexer
);
35613 ops
[idx
] = integer_minus_one_node
;
35615 if (cp_lexer_next_token_is (lexer
, CPP_COMMA
))
35617 cp_lexer_consume_token (lexer
);
35623 /* Worker num: argument and vector length: arguments. */
35624 else if (cp_lexer_next_token_is (lexer
, CPP_NAME
)
35625 && id_equal (next
->u
.value
, id
)
35626 && cp_lexer_nth_token_is (lexer
, 2, CPP_COLON
))
35628 cp_lexer_consume_token (lexer
); /* id */
35629 cp_lexer_consume_token (lexer
); /* ':' */
35632 /* Now collect the actual argument. */
35633 if (ops
[idx
] != NULL_TREE
)
35635 cp_parser_error (parser
, "unexpected argument");
35636 goto cleanup_error
;
35639 tree expr
= cp_parser_assignment_expression (parser
, NULL
, false,
35641 if (expr
== error_mark_node
)
35642 goto cleanup_error
;
35644 mark_exp_read (expr
);
35647 if (kind
== OMP_CLAUSE_GANG
35648 && cp_lexer_next_token_is (lexer
, CPP_COMMA
))
35650 cp_lexer_consume_token (lexer
);
35657 if (!parens
.require_close (parser
))
35658 goto cleanup_error
;
35661 check_no_duplicate_clause (list
, kind
, str
, loc
);
35663 c
= build_omp_clause (loc
, kind
);
35666 OMP_CLAUSE_OPERAND (c
, 1) = ops
[1];
35668 OMP_CLAUSE_OPERAND (c
, 0) = ops
[0];
35669 OMP_CLAUSE_CHAIN (c
) = list
;
35674 cp_parser_skip_to_closing_parenthesis (parser
, false, false, true);
35679 tile ( size-expr-list ) */
35682 cp_parser_oacc_clause_tile (cp_parser
*parser
, location_t clause_loc
, tree list
)
35684 tree c
, expr
= error_mark_node
;
35685 tree tile
= NULL_TREE
;
35687 /* Collapse and tile are mutually exclusive. (The spec doesn't say
35688 so, but the spec authors never considered such a case and have
35689 differing opinions on what it might mean, including 'not
35691 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", clause_loc
);
35692 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse",
35695 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
35700 if (tile
&& !cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
))
35703 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MULT
)
35704 && (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
)
35705 || cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
)))
35707 cp_lexer_consume_token (parser
->lexer
);
35708 expr
= integer_zero_node
;
35711 expr
= cp_parser_constant_expression (parser
);
35713 tile
= tree_cons (NULL_TREE
, expr
, tile
);
35715 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
));
35717 /* Consume the trailing ')'. */
35718 cp_lexer_consume_token (parser
->lexer
);
35720 c
= build_omp_clause (clause_loc
, OMP_CLAUSE_TILE
);
35721 tile
= nreverse (tile
);
35722 OMP_CLAUSE_TILE_LIST (c
) = tile
;
35723 OMP_CLAUSE_CHAIN (c
) = list
;
35728 Parse wait clause or directive parameters. */
35731 cp_parser_oacc_wait_list (cp_parser
*parser
, location_t clause_loc
, tree list
)
35733 vec
<tree
, va_gc
> *args
;
35736 args
= cp_parser_parenthesized_expression_list (parser
, non_attr
,
35738 /*allow_expansion_p=*/true,
35739 /*non_constant_p=*/NULL
);
35741 if (args
== NULL
|| args
->length () == 0)
35745 cp_parser_error (parser
, "expected integer expression list");
35746 release_tree_vector (args
);
35751 args_tree
= build_tree_list_vec (args
);
35753 release_tree_vector (args
);
35755 for (t
= args_tree
; t
; t
= TREE_CHAIN (t
))
35757 tree targ
= TREE_VALUE (t
);
35759 if (targ
!= error_mark_node
)
35761 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ
)))
35762 error ("%<wait%> expression must be integral");
35765 tree c
= build_omp_clause (clause_loc
, OMP_CLAUSE_WAIT
);
35767 targ
= mark_rvalue_use (targ
);
35768 OMP_CLAUSE_DECL (c
) = targ
;
35769 OMP_CLAUSE_CHAIN (c
) = list
;
35779 wait [( int-expr-list )] */
35782 cp_parser_oacc_clause_wait (cp_parser
*parser
, tree list
)
35784 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
35786 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
35787 list
= cp_parser_oacc_wait_list (parser
, location
, list
);
35790 tree c
= build_omp_clause (location
, OMP_CLAUSE_WAIT
);
35792 OMP_CLAUSE_DECL (c
) = build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
35793 OMP_CLAUSE_CHAIN (c
) = list
;
35801 collapse ( constant-expression ) */
35804 cp_parser_omp_clause_collapse (cp_parser
*parser
, tree list
, location_t location
)
35810 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
35811 matching_parens parens
;
35812 if (!parens
.require_open (parser
))
35815 num
= cp_parser_constant_expression (parser
);
35817 if (!parens
.require_close (parser
))
35818 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35819 /*or_comma=*/false,
35820 /*consume_paren=*/true);
35822 if (num
== error_mark_node
)
35824 num
= fold_non_dependent_expr (num
);
35825 if (!tree_fits_shwi_p (num
)
35826 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
35827 || (n
= tree_to_shwi (num
)) <= 0
35830 error_at (loc
, "collapse argument needs positive constant integer expression");
35834 check_no_duplicate_clause (list
, OMP_CLAUSE_COLLAPSE
, "collapse", location
);
35835 check_no_duplicate_clause (list
, OMP_CLAUSE_TILE
, "tile", location
);
35836 c
= build_omp_clause (loc
, OMP_CLAUSE_COLLAPSE
);
35837 OMP_CLAUSE_CHAIN (c
) = list
;
35838 OMP_CLAUSE_COLLAPSE_EXPR (c
) = num
;
35844 default ( none | shared )
35847 default ( none | present ) */
35850 cp_parser_omp_clause_default (cp_parser
*parser
, tree list
,
35851 location_t location
, bool is_oacc
)
35853 enum omp_clause_default_kind kind
= OMP_CLAUSE_DEFAULT_UNSPECIFIED
;
35856 matching_parens parens
;
35857 if (!parens
.require_open (parser
))
35859 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35861 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35862 const char *p
= IDENTIFIER_POINTER (id
);
35867 if (strcmp ("none", p
) != 0)
35869 kind
= OMP_CLAUSE_DEFAULT_NONE
;
35873 if (strcmp ("present", p
) != 0 || !is_oacc
)
35875 kind
= OMP_CLAUSE_DEFAULT_PRESENT
;
35879 if (strcmp ("shared", p
) != 0 || is_oacc
)
35881 kind
= OMP_CLAUSE_DEFAULT_SHARED
;
35888 cp_lexer_consume_token (parser
->lexer
);
35894 cp_parser_error (parser
, "expected %<none%> or %<present%>");
35896 cp_parser_error (parser
, "expected %<none%> or %<shared%>");
35899 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
35900 || !parens
.require_close (parser
))
35901 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35902 /*or_comma=*/false,
35903 /*consume_paren=*/true);
35905 if (kind
== OMP_CLAUSE_DEFAULT_UNSPECIFIED
)
35908 check_no_duplicate_clause (list
, OMP_CLAUSE_DEFAULT
, "default", location
);
35909 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULT
);
35910 OMP_CLAUSE_CHAIN (c
) = list
;
35911 OMP_CLAUSE_DEFAULT_KIND (c
) = kind
;
35917 final ( expression ) */
35920 cp_parser_omp_clause_final (cp_parser
*parser
, tree list
, location_t location
)
35924 matching_parens parens
;
35925 if (!parens
.require_open (parser
))
35928 t
= cp_parser_assignment_expression (parser
);
35930 if (t
== error_mark_node
35931 || !parens
.require_close (parser
))
35932 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
35933 /*or_comma=*/false,
35934 /*consume_paren=*/true);
35936 check_no_duplicate_clause (list
, OMP_CLAUSE_FINAL
, "final", location
);
35938 c
= build_omp_clause (location
, OMP_CLAUSE_FINAL
);
35939 OMP_CLAUSE_FINAL_EXPR (c
) = t
;
35940 OMP_CLAUSE_CHAIN (c
) = list
;
35949 if ( directive-name-modifier : expression )
35951 directive-name-modifier:
35952 parallel | task | taskloop | target data | target | target update
35953 | target enter data | target exit data
35956 directive-name-modifier:
35957 ... | simd | cancel */
35960 cp_parser_omp_clause_if (cp_parser
*parser
, tree list
, location_t location
,
35964 enum tree_code if_modifier
= ERROR_MARK
;
35966 matching_parens parens
;
35967 if (!parens
.require_open (parser
))
35970 if (is_omp
&& cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
35972 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
35973 const char *p
= IDENTIFIER_POINTER (id
);
35976 if (strcmp ("cancel", p
) == 0)
35977 if_modifier
= VOID_CST
;
35978 else if (strcmp ("parallel", p
) == 0)
35979 if_modifier
= OMP_PARALLEL
;
35980 else if (strcmp ("simd", p
) == 0)
35981 if_modifier
= OMP_SIMD
;
35982 else if (strcmp ("task", p
) == 0)
35983 if_modifier
= OMP_TASK
;
35984 else if (strcmp ("taskloop", p
) == 0)
35985 if_modifier
= OMP_TASKLOOP
;
35986 else if (strcmp ("target", p
) == 0)
35988 if_modifier
= OMP_TARGET
;
35989 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
35991 id
= cp_lexer_peek_nth_token (parser
->lexer
, 2)->u
.value
;
35992 p
= IDENTIFIER_POINTER (id
);
35993 if (strcmp ("data", p
) == 0)
35994 if_modifier
= OMP_TARGET_DATA
;
35995 else if (strcmp ("update", p
) == 0)
35996 if_modifier
= OMP_TARGET_UPDATE
;
35997 else if (strcmp ("enter", p
) == 0)
35998 if_modifier
= OMP_TARGET_ENTER_DATA
;
35999 else if (strcmp ("exit", p
) == 0)
36000 if_modifier
= OMP_TARGET_EXIT_DATA
;
36001 if (if_modifier
!= OMP_TARGET
)
36006 = cp_lexer_peek_nth_token (parser
->lexer
, 2)->location
;
36007 error_at (loc
, "expected %<data%>, %<update%>, %<enter%> "
36009 if_modifier
= ERROR_MARK
;
36011 if (if_modifier
== OMP_TARGET_ENTER_DATA
36012 || if_modifier
== OMP_TARGET_EXIT_DATA
)
36014 if (cp_lexer_nth_token_is (parser
->lexer
, 3, CPP_NAME
))
36016 id
= cp_lexer_peek_nth_token (parser
->lexer
, 3)->u
.value
;
36017 p
= IDENTIFIER_POINTER (id
);
36018 if (strcmp ("data", p
) == 0)
36024 = cp_lexer_peek_nth_token (parser
->lexer
, 3)->location
;
36025 error_at (loc
, "expected %<data%>");
36026 if_modifier
= ERROR_MARK
;
36031 if (if_modifier
!= ERROR_MARK
)
36033 if (cp_lexer_nth_token_is (parser
->lexer
, n
, CPP_COLON
))
36036 cp_lexer_consume_token (parser
->lexer
);
36043 = cp_lexer_peek_nth_token (parser
->lexer
, n
)->location
;
36044 error_at (loc
, "expected %<:%>");
36046 if_modifier
= ERROR_MARK
;
36051 t
= cp_parser_assignment_expression (parser
);
36053 if (t
== error_mark_node
36054 || !parens
.require_close (parser
))
36055 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36056 /*or_comma=*/false,
36057 /*consume_paren=*/true);
36059 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36060 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IF
)
36062 if (if_modifier
!= ERROR_MARK
36063 && OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
36065 const char *p
= NULL
;
36066 switch (if_modifier
)
36068 case VOID_CST
: p
= "cancel"; break;
36069 case OMP_PARALLEL
: p
= "parallel"; break;
36070 case OMP_SIMD
: p
= "simd"; break;
36071 case OMP_TASK
: p
= "task"; break;
36072 case OMP_TASKLOOP
: p
= "taskloop"; break;
36073 case OMP_TARGET_DATA
: p
= "target data"; break;
36074 case OMP_TARGET
: p
= "target"; break;
36075 case OMP_TARGET_UPDATE
: p
= "target update"; break;
36076 case OMP_TARGET_ENTER_DATA
: p
= "target enter data"; break;
36077 case OMP_TARGET_EXIT_DATA
: p
= "target exit data"; break;
36078 default: gcc_unreachable ();
36080 error_at (location
, "too many %<if%> clauses with %qs modifier",
36084 else if (OMP_CLAUSE_IF_MODIFIER (c
) == if_modifier
)
36087 error_at (location
, "too many %<if%> clauses");
36089 error_at (location
, "too many %<if%> clauses without modifier");
36092 else if (if_modifier
== ERROR_MARK
36093 || OMP_CLAUSE_IF_MODIFIER (c
) == ERROR_MARK
)
36095 error_at (location
, "if any %<if%> clause has modifier, then all "
36096 "%<if%> clauses have to use modifier");
36101 c
= build_omp_clause (location
, OMP_CLAUSE_IF
);
36102 OMP_CLAUSE_IF_MODIFIER (c
) = if_modifier
;
36103 OMP_CLAUSE_IF_EXPR (c
) = t
;
36104 OMP_CLAUSE_CHAIN (c
) = list
;
36113 cp_parser_omp_clause_mergeable (cp_parser
* /*parser*/,
36114 tree list
, location_t location
)
36118 check_no_duplicate_clause (list
, OMP_CLAUSE_MERGEABLE
, "mergeable",
36121 c
= build_omp_clause (location
, OMP_CLAUSE_MERGEABLE
);
36122 OMP_CLAUSE_CHAIN (c
) = list
;
36130 cp_parser_omp_clause_nowait (cp_parser
* /*parser*/,
36131 tree list
, location_t location
)
36135 check_no_duplicate_clause (list
, OMP_CLAUSE_NOWAIT
, "nowait", location
);
36137 c
= build_omp_clause (location
, OMP_CLAUSE_NOWAIT
);
36138 OMP_CLAUSE_CHAIN (c
) = list
;
36143 num_threads ( expression ) */
36146 cp_parser_omp_clause_num_threads (cp_parser
*parser
, tree list
,
36147 location_t location
)
36151 matching_parens parens
;
36152 if (!parens
.require_open (parser
))
36155 t
= cp_parser_assignment_expression (parser
);
36157 if (t
== error_mark_node
36158 || !parens
.require_close (parser
))
36159 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36160 /*or_comma=*/false,
36161 /*consume_paren=*/true);
36163 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_THREADS
,
36164 "num_threads", location
);
36166 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_THREADS
);
36167 OMP_CLAUSE_NUM_THREADS_EXPR (c
) = t
;
36168 OMP_CLAUSE_CHAIN (c
) = list
;
36174 num_tasks ( expression ) */
36177 cp_parser_omp_clause_num_tasks (cp_parser
*parser
, tree list
,
36178 location_t location
)
36182 matching_parens parens
;
36183 if (!parens
.require_open (parser
))
36186 t
= cp_parser_assignment_expression (parser
);
36188 if (t
== error_mark_node
36189 || !parens
.require_close (parser
))
36190 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36191 /*or_comma=*/false,
36192 /*consume_paren=*/true);
36194 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TASKS
,
36195 "num_tasks", location
);
36197 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TASKS
);
36198 OMP_CLAUSE_NUM_TASKS_EXPR (c
) = t
;
36199 OMP_CLAUSE_CHAIN (c
) = list
;
36205 grainsize ( expression ) */
36208 cp_parser_omp_clause_grainsize (cp_parser
*parser
, tree list
,
36209 location_t location
)
36213 matching_parens parens
;
36214 if (!parens
.require_open (parser
))
36217 t
= cp_parser_assignment_expression (parser
);
36219 if (t
== error_mark_node
36220 || !parens
.require_close (parser
))
36221 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36222 /*or_comma=*/false,
36223 /*consume_paren=*/true);
36225 check_no_duplicate_clause (list
, OMP_CLAUSE_GRAINSIZE
,
36226 "grainsize", location
);
36228 c
= build_omp_clause (location
, OMP_CLAUSE_GRAINSIZE
);
36229 OMP_CLAUSE_GRAINSIZE_EXPR (c
) = t
;
36230 OMP_CLAUSE_CHAIN (c
) = list
;
36236 priority ( expression ) */
36239 cp_parser_omp_clause_priority (cp_parser
*parser
, tree list
,
36240 location_t location
)
36244 matching_parens parens
;
36245 if (!parens
.require_open (parser
))
36248 t
= cp_parser_assignment_expression (parser
);
36250 if (t
== error_mark_node
36251 || !parens
.require_close (parser
))
36252 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36253 /*or_comma=*/false,
36254 /*consume_paren=*/true);
36256 check_no_duplicate_clause (list
, OMP_CLAUSE_PRIORITY
,
36257 "priority", location
);
36259 c
= build_omp_clause (location
, OMP_CLAUSE_PRIORITY
);
36260 OMP_CLAUSE_PRIORITY_EXPR (c
) = t
;
36261 OMP_CLAUSE_CHAIN (c
) = list
;
36267 hint ( expression ) */
36270 cp_parser_omp_clause_hint (cp_parser
*parser
, tree list
, location_t location
)
36274 matching_parens parens
;
36275 if (!parens
.require_open (parser
))
36278 t
= cp_parser_assignment_expression (parser
);
36280 if (t
!= error_mark_node
)
36282 t
= fold_non_dependent_expr (t
);
36283 if (!value_dependent_expression_p (t
)
36284 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
36285 || !tree_fits_shwi_p (t
)
36286 || tree_int_cst_sgn (t
) == -1))
36287 error_at (location
, "expected constant integer expression with "
36288 "valid sync-hint value");
36290 if (t
== error_mark_node
36291 || !parens
.require_close (parser
))
36292 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36293 /*or_comma=*/false,
36294 /*consume_paren=*/true);
36295 check_no_duplicate_clause (list
, OMP_CLAUSE_HINT
, "hint", location
);
36297 c
= build_omp_clause (location
, OMP_CLAUSE_HINT
);
36298 OMP_CLAUSE_HINT_EXPR (c
) = t
;
36299 OMP_CLAUSE_CHAIN (c
) = list
;
36305 defaultmap ( tofrom : scalar )
36308 defaultmap ( implicit-behavior [ : variable-category ] ) */
36311 cp_parser_omp_clause_defaultmap (cp_parser
*parser
, tree list
,
36312 location_t location
)
36316 enum omp_clause_defaultmap_kind behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
36317 enum omp_clause_defaultmap_kind category
36318 = OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
;
36320 matching_parens parens
;
36321 if (!parens
.require_open (parser
))
36324 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
))
36326 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36329 cp_parser_error (parser
, "expected %<alloc%>, %<to%>, %<from%>, "
36330 "%<tofrom%>, %<firstprivate%>, %<none%> "
36336 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36337 p
= IDENTIFIER_POINTER (id
);
36343 if (strcmp ("alloc", p
) == 0)
36344 behavior
= OMP_CLAUSE_DEFAULTMAP_ALLOC
;
36346 goto invalid_behavior
;
36350 if (strcmp ("default", p
) == 0)
36351 behavior
= OMP_CLAUSE_DEFAULTMAP_DEFAULT
;
36353 goto invalid_behavior
;
36357 if (strcmp ("firstprivate", p
) == 0)
36358 behavior
= OMP_CLAUSE_DEFAULTMAP_FIRSTPRIVATE
;
36359 else if (strcmp ("from", p
) == 0)
36360 behavior
= OMP_CLAUSE_DEFAULTMAP_FROM
;
36362 goto invalid_behavior
;
36366 if (strcmp ("none", p
) == 0)
36367 behavior
= OMP_CLAUSE_DEFAULTMAP_NONE
;
36369 goto invalid_behavior
;
36373 if (strcmp ("tofrom", p
) == 0)
36374 behavior
= OMP_CLAUSE_DEFAULTMAP_TOFROM
;
36375 else if (strcmp ("to", p
) == 0)
36376 behavior
= OMP_CLAUSE_DEFAULTMAP_TO
;
36378 goto invalid_behavior
;
36382 goto invalid_behavior
;
36384 cp_lexer_consume_token (parser
->lexer
);
36386 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
36388 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36391 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36394 cp_parser_error (parser
, "expected %<scalar%>, %<aggregate%> or "
36398 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36399 p
= IDENTIFIER_POINTER (id
);
36404 if (strcmp ("aggregate", p
) == 0)
36405 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
;
36407 goto invalid_category
;
36411 if (strcmp ("pointer", p
) == 0)
36412 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
;
36414 goto invalid_category
;
36418 if (strcmp ("scalar", p
) == 0)
36419 category
= OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
;
36421 goto invalid_category
;
36425 goto invalid_category
;
36428 cp_lexer_consume_token (parser
->lexer
);
36430 if (!parens
.require_close (parser
))
36433 for (c
= list
; c
; c
= OMP_CLAUSE_CHAIN (c
))
36434 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEFAULTMAP
36435 && (category
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
36436 || OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
) == category
36437 || (OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
)
36438 == OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)))
36440 enum omp_clause_defaultmap_kind cat
= category
;
36441 location_t loc
= OMP_CLAUSE_LOCATION (c
);
36442 if (cat
== OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
)
36443 cat
= OMP_CLAUSE_DEFAULTMAP_CATEGORY (c
);
36447 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_UNSPECIFIED
:
36450 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_AGGREGATE
:
36453 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_POINTER
:
36456 case OMP_CLAUSE_DEFAULTMAP_CATEGORY_SCALAR
:
36460 gcc_unreachable ();
36463 error_at (loc
, "too many %<defaultmap%> clauses with %qs category",
36466 error_at (loc
, "too many %<defaultmap%> clauses with unspecified "
36471 c
= build_omp_clause (location
, OMP_CLAUSE_DEFAULTMAP
);
36472 OMP_CLAUSE_DEFAULTMAP_SET_KIND (c
, behavior
, category
);
36473 OMP_CLAUSE_CHAIN (c
) = list
;
36477 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36478 /*or_comma=*/false,
36479 /*consume_paren=*/true);
36484 order ( concurrent ) */
36487 cp_parser_omp_clause_order (cp_parser
*parser
, tree list
, location_t location
)
36492 matching_parens parens
;
36493 if (!parens
.require_open (parser
))
36496 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36498 cp_parser_error (parser
, "expected %<concurrent%>");
36503 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36504 p
= IDENTIFIER_POINTER (id
);
36506 if (strcmp (p
, "concurrent") != 0)
36508 cp_parser_error (parser
, "expected %<concurrent%>");
36511 cp_lexer_consume_token (parser
->lexer
);
36512 if (!parens
.require_close (parser
))
36515 /* check_no_duplicate_clause (list, OMP_CLAUSE_ORDER, "order", location); */
36516 c
= build_omp_clause (location
, OMP_CLAUSE_ORDER
);
36517 OMP_CLAUSE_CHAIN (c
) = list
;
36521 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36522 /*or_comma=*/false,
36523 /*consume_paren=*/true);
36528 bind ( teams | parallel | thread ) */
36531 cp_parser_omp_clause_bind (cp_parser
*parser
, tree list
,
36532 location_t location
)
36536 enum omp_clause_bind_kind kind
= OMP_CLAUSE_BIND_THREAD
;
36538 matching_parens parens
;
36539 if (!parens
.require_open (parser
))
36542 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36545 cp_parser_error (parser
,
36546 "expected %<teams%>, %<parallel%> or %<thread%>");
36551 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36552 p
= IDENTIFIER_POINTER (id
);
36554 if (strcmp (p
, "teams") == 0)
36555 kind
= OMP_CLAUSE_BIND_TEAMS
;
36556 else if (strcmp (p
, "parallel") == 0)
36557 kind
= OMP_CLAUSE_BIND_PARALLEL
;
36558 else if (strcmp (p
, "thread") != 0)
36560 cp_lexer_consume_token (parser
->lexer
);
36561 if (!parens
.require_close (parser
))
36564 /* check_no_duplicate_clause (list, OMP_CLAUSE_BIND, "bind", location); */
36565 c
= build_omp_clause (location
, OMP_CLAUSE_BIND
);
36566 OMP_CLAUSE_BIND_KIND (c
) = kind
;
36567 OMP_CLAUSE_CHAIN (c
) = list
;
36571 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36572 /*or_comma=*/false,
36573 /*consume_paren=*/true);
36581 ordered ( constant-expression ) */
36584 cp_parser_omp_clause_ordered (cp_parser
*parser
,
36585 tree list
, location_t location
)
36587 tree c
, num
= NULL_TREE
;
36590 check_no_duplicate_clause (list
, OMP_CLAUSE_ORDERED
,
36591 "ordered", location
);
36593 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
36595 matching_parens parens
;
36596 parens
.consume_open (parser
);
36598 num
= cp_parser_constant_expression (parser
);
36600 if (!parens
.require_close (parser
))
36601 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36602 /*or_comma=*/false,
36603 /*consume_paren=*/true);
36605 if (num
== error_mark_node
)
36607 num
= fold_non_dependent_expr (num
);
36608 if (!tree_fits_shwi_p (num
)
36609 || !INTEGRAL_TYPE_P (TREE_TYPE (num
))
36610 || (n
= tree_to_shwi (num
)) <= 0
36613 error_at (location
,
36614 "ordered argument needs positive constant integer "
36620 c
= build_omp_clause (location
, OMP_CLAUSE_ORDERED
);
36621 OMP_CLAUSE_ORDERED_EXPR (c
) = num
;
36622 OMP_CLAUSE_CHAIN (c
) = list
;
36627 reduction ( reduction-operator : variable-list )
36629 reduction-operator:
36630 One of: + * - & ^ | && ||
36634 reduction-operator:
36635 One of: + * - & ^ | && || min max
36639 reduction-operator:
36640 One of: + * - & ^ | && ||
36644 reduction ( reduction-modifier, reduction-operator : variable-list )
36645 in_reduction ( reduction-operator : variable-list )
36646 task_reduction ( reduction-operator : variable-list ) */
36649 cp_parser_omp_clause_reduction (cp_parser
*parser
, enum omp_clause_code kind
,
36650 bool is_omp
, tree list
)
36652 enum tree_code code
= ERROR_MARK
;
36653 tree nlist
, c
, id
= NULL_TREE
;
36655 bool inscan
= false;
36657 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
36660 if (kind
== OMP_CLAUSE_REDUCTION
&& is_omp
)
36662 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DEFAULT
)
36663 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
36665 cp_lexer_consume_token (parser
->lexer
);
36666 cp_lexer_consume_token (parser
->lexer
);
36668 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
36669 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COMMA
))
36671 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36672 const char *p
= IDENTIFIER_POINTER (id
);
36673 if (strcmp (p
, "task") == 0)
36675 else if (strcmp (p
, "inscan") == 0)
36677 if (task
|| inscan
)
36679 cp_lexer_consume_token (parser
->lexer
);
36680 cp_lexer_consume_token (parser
->lexer
);
36685 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
36687 case CPP_PLUS
: code
= PLUS_EXPR
; break;
36688 case CPP_MULT
: code
= MULT_EXPR
; break;
36689 case CPP_MINUS
: code
= MINUS_EXPR
; break;
36690 case CPP_AND
: code
= BIT_AND_EXPR
; break;
36691 case CPP_XOR
: code
= BIT_XOR_EXPR
; break;
36692 case CPP_OR
: code
= BIT_IOR_EXPR
; break;
36693 case CPP_AND_AND
: code
= TRUTH_ANDIF_EXPR
; break;
36694 case CPP_OR_OR
: code
= TRUTH_ORIF_EXPR
; break;
36698 if (code
!= ERROR_MARK
)
36699 cp_lexer_consume_token (parser
->lexer
);
36702 bool saved_colon_corrects_to_scope_p
;
36703 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
36704 parser
->colon_corrects_to_scope_p
= false;
36705 id
= cp_parser_id_expression (parser
, /*template_p=*/false,
36706 /*check_dependency_p=*/true,
36707 /*template_p=*/NULL
,
36708 /*declarator_p=*/false,
36709 /*optional_p=*/false);
36710 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
36711 if (identifier_p (id
))
36713 const char *p
= IDENTIFIER_POINTER (id
);
36715 if (strcmp (p
, "min") == 0)
36717 else if (strcmp (p
, "max") == 0)
36719 else if (id
== ovl_op_identifier (false, PLUS_EXPR
))
36721 else if (id
== ovl_op_identifier (false, MULT_EXPR
))
36723 else if (id
== ovl_op_identifier (false, MINUS_EXPR
))
36725 else if (id
== ovl_op_identifier (false, BIT_AND_EXPR
))
36726 code
= BIT_AND_EXPR
;
36727 else if (id
== ovl_op_identifier (false, BIT_IOR_EXPR
))
36728 code
= BIT_IOR_EXPR
;
36729 else if (id
== ovl_op_identifier (false, BIT_XOR_EXPR
))
36730 code
= BIT_XOR_EXPR
;
36731 else if (id
== ovl_op_identifier (false, TRUTH_ANDIF_EXPR
))
36732 code
= TRUTH_ANDIF_EXPR
;
36733 else if (id
== ovl_op_identifier (false, TRUTH_ORIF_EXPR
))
36734 code
= TRUTH_ORIF_EXPR
;
36735 id
= omp_reduction_id (code
, id
, NULL_TREE
);
36736 tree scope
= parser
->scope
;
36738 id
= build_qualified_name (NULL_TREE
, scope
, id
, false);
36739 parser
->scope
= NULL_TREE
;
36740 parser
->qualifying_scope
= NULL_TREE
;
36741 parser
->object_scope
= NULL_TREE
;
36745 error ("invalid reduction-identifier");
36747 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36748 /*or_comma=*/false,
36749 /*consume_paren=*/true);
36754 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
36757 nlist
= cp_parser_omp_var_list_no_open (parser
, kind
, list
,
36759 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
36761 OMP_CLAUSE_REDUCTION_CODE (c
) = code
;
36763 OMP_CLAUSE_REDUCTION_TASK (c
) = 1;
36765 OMP_CLAUSE_REDUCTION_INSCAN (c
) = 1;
36766 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c
) = id
;
36773 schedule ( schedule-kind )
36774 schedule ( schedule-kind , expression )
36777 static | dynamic | guided | runtime | auto
36780 schedule ( schedule-modifier : schedule-kind )
36781 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
36789 cp_parser_omp_clause_schedule (cp_parser
*parser
, tree list
, location_t location
)
36792 int modifiers
= 0, nmodifiers
= 0;
36794 matching_parens parens
;
36795 if (!parens
.require_open (parser
))
36798 c
= build_omp_clause (location
, OMP_CLAUSE_SCHEDULE
);
36800 location_t comma
= UNKNOWN_LOCATION
;
36801 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36803 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36804 const char *p
= IDENTIFIER_POINTER (id
);
36805 if (strcmp ("simd", p
) == 0)
36806 OMP_CLAUSE_SCHEDULE_SIMD (c
) = 1;
36807 else if (strcmp ("monotonic", p
) == 0)
36808 modifiers
|= OMP_CLAUSE_SCHEDULE_MONOTONIC
;
36809 else if (strcmp ("nonmonotonic", p
) == 0)
36810 modifiers
|= OMP_CLAUSE_SCHEDULE_NONMONOTONIC
;
36813 comma
= UNKNOWN_LOCATION
;
36814 cp_lexer_consume_token (parser
->lexer
);
36815 if (nmodifiers
++ == 0
36816 && cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36818 comma
= cp_lexer_peek_token (parser
->lexer
)->location
;
36819 cp_lexer_consume_token (parser
->lexer
);
36823 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
36827 if (comma
!= UNKNOWN_LOCATION
)
36828 error_at (comma
, "expected %<:%>");
36830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
36832 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
36833 const char *p
= IDENTIFIER_POINTER (id
);
36838 if (strcmp ("dynamic", p
) != 0)
36840 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_DYNAMIC
;
36844 if (strcmp ("guided", p
) != 0)
36846 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_GUIDED
;
36850 if (strcmp ("runtime", p
) != 0)
36852 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_RUNTIME
;
36859 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
36860 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_STATIC
;
36861 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_AUTO
))
36862 OMP_CLAUSE_SCHEDULE_KIND (c
) = OMP_CLAUSE_SCHEDULE_AUTO
;
36865 cp_lexer_consume_token (parser
->lexer
);
36867 if ((modifiers
& (OMP_CLAUSE_SCHEDULE_MONOTONIC
36868 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
36869 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
36870 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC
))
36872 error_at (location
, "both %<monotonic%> and %<nonmonotonic%> modifiers "
36877 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
36880 cp_lexer_consume_token (parser
->lexer
);
36882 token
= cp_lexer_peek_token (parser
->lexer
);
36883 t
= cp_parser_assignment_expression (parser
);
36885 if (t
== error_mark_node
)
36887 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_RUNTIME
)
36888 error_at (token
->location
, "schedule %<runtime%> does not take "
36889 "a %<chunk_size%> parameter");
36890 else if (OMP_CLAUSE_SCHEDULE_KIND (c
) == OMP_CLAUSE_SCHEDULE_AUTO
)
36891 error_at (token
->location
, "schedule %<auto%> does not take "
36892 "a %<chunk_size%> parameter");
36894 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c
) = t
;
36896 if (!parens
.require_close (parser
))
36899 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
36902 OMP_CLAUSE_SCHEDULE_KIND (c
)
36903 = (enum omp_clause_schedule_kind
)
36904 (OMP_CLAUSE_SCHEDULE_KIND (c
) | modifiers
);
36906 check_no_duplicate_clause (list
, OMP_CLAUSE_SCHEDULE
, "schedule", location
);
36907 OMP_CLAUSE_CHAIN (c
) = list
;
36911 cp_parser_error (parser
, "invalid schedule kind");
36913 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
36914 /*or_comma=*/false,
36915 /*consume_paren=*/true);
36923 cp_parser_omp_clause_untied (cp_parser
* /*parser*/,
36924 tree list
, location_t location
)
36928 check_no_duplicate_clause (list
, OMP_CLAUSE_UNTIED
, "untied", location
);
36930 c
= build_omp_clause (location
, OMP_CLAUSE_UNTIED
);
36931 OMP_CLAUSE_CHAIN (c
) = list
;
36940 cp_parser_omp_clause_branch (cp_parser
* /*parser*/, enum omp_clause_code code
,
36941 tree list
, location_t location
)
36943 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
36944 tree c
= build_omp_clause (location
, code
);
36945 OMP_CLAUSE_CHAIN (c
) = list
;
36956 cp_parser_omp_clause_cancelkind (cp_parser
* /*parser*/,
36957 enum omp_clause_code code
,
36958 tree list
, location_t location
)
36960 tree c
= build_omp_clause (location
, code
);
36961 OMP_CLAUSE_CHAIN (c
) = list
;
36969 cp_parser_omp_clause_nogroup (cp_parser
* /*parser*/,
36970 tree list
, location_t location
)
36972 check_no_duplicate_clause (list
, OMP_CLAUSE_NOGROUP
, "nogroup", location
);
36973 tree c
= build_omp_clause (location
, OMP_CLAUSE_NOGROUP
);
36974 OMP_CLAUSE_CHAIN (c
) = list
;
36983 cp_parser_omp_clause_orderedkind (cp_parser
* /*parser*/,
36984 enum omp_clause_code code
,
36985 tree list
, location_t location
)
36987 check_no_duplicate_clause (list
, code
, omp_clause_code_name
[code
], location
);
36988 tree c
= build_omp_clause (location
, code
);
36989 OMP_CLAUSE_CHAIN (c
) = list
;
36994 num_teams ( expression ) */
36997 cp_parser_omp_clause_num_teams (cp_parser
*parser
, tree list
,
36998 location_t location
)
37002 matching_parens parens
;
37003 if (!parens
.require_open (parser
))
37006 t
= cp_parser_assignment_expression (parser
);
37008 if (t
== error_mark_node
37009 || !parens
.require_close (parser
))
37010 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37011 /*or_comma=*/false,
37012 /*consume_paren=*/true);
37014 check_no_duplicate_clause (list
, OMP_CLAUSE_NUM_TEAMS
,
37015 "num_teams", location
);
37017 c
= build_omp_clause (location
, OMP_CLAUSE_NUM_TEAMS
);
37018 OMP_CLAUSE_NUM_TEAMS_EXPR (c
) = t
;
37019 OMP_CLAUSE_CHAIN (c
) = list
;
37025 thread_limit ( expression ) */
37028 cp_parser_omp_clause_thread_limit (cp_parser
*parser
, tree list
,
37029 location_t location
)
37033 matching_parens parens
;
37034 if (!parens
.require_open (parser
))
37037 t
= cp_parser_assignment_expression (parser
);
37039 if (t
== error_mark_node
37040 || !parens
.require_close (parser
))
37041 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37042 /*or_comma=*/false,
37043 /*consume_paren=*/true);
37045 check_no_duplicate_clause (list
, OMP_CLAUSE_THREAD_LIMIT
,
37046 "thread_limit", location
);
37048 c
= build_omp_clause (location
, OMP_CLAUSE_THREAD_LIMIT
);
37049 OMP_CLAUSE_THREAD_LIMIT_EXPR (c
) = t
;
37050 OMP_CLAUSE_CHAIN (c
) = list
;
37056 aligned ( variable-list )
37057 aligned ( variable-list : constant-expression ) */
37060 cp_parser_omp_clause_aligned (cp_parser
*parser
, tree list
)
37062 tree nlist
, c
, alignment
= NULL_TREE
;
37065 matching_parens parens
;
37066 if (!parens
.require_open (parser
))
37069 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALIGNED
, list
,
37074 alignment
= cp_parser_constant_expression (parser
);
37076 if (!parens
.require_close (parser
))
37077 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37078 /*or_comma=*/false,
37079 /*consume_paren=*/true);
37081 if (alignment
== error_mark_node
)
37082 alignment
= NULL_TREE
;
37085 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37086 OMP_CLAUSE_ALIGNED_ALIGNMENT (c
) = alignment
;
37092 allocate ( variable-list )
37093 allocate ( expression : variable-list ) */
37096 cp_parser_omp_clause_allocate (cp_parser
*parser
, tree list
)
37098 tree nlist
, c
, allocator
= NULL_TREE
;
37101 matching_parens parens
;
37102 if (!parens
.require_open (parser
))
37105 cp_parser_parse_tentatively (parser
);
37106 bool saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
37107 parser
->colon_corrects_to_scope_p
= false;
37108 allocator
= cp_parser_assignment_expression (parser
);
37109 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37110 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
37112 cp_parser_parse_definitely (parser
);
37113 cp_lexer_consume_token (parser
->lexer
);
37114 if (allocator
== error_mark_node
)
37115 allocator
= NULL_TREE
;
37119 cp_parser_abort_tentative_parse (parser
);
37120 allocator
= NULL_TREE
;
37123 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_ALLOCATE
, list
,
37126 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37127 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
37133 lastprivate ( variable-list )
37136 lastprivate ( [ lastprivate-modifier : ] variable-list ) */
37139 cp_parser_omp_clause_lastprivate (cp_parser
*parser
, tree list
)
37141 bool conditional
= false;
37143 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37146 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37147 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_COLON
))
37149 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37150 const char *p
= IDENTIFIER_POINTER (id
);
37152 if (strcmp ("conditional", p
) == 0)
37154 conditional
= true;
37155 cp_lexer_consume_token (parser
->lexer
);
37156 cp_lexer_consume_token (parser
->lexer
);
37160 tree nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LASTPRIVATE
,
37164 for (tree c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37165 OMP_CLAUSE_LASTPRIVATE_CONDITIONAL (c
) = 1;
37170 linear ( variable-list )
37171 linear ( variable-list : expression )
37174 linear ( modifier ( variable-list ) )
37175 linear ( modifier ( variable-list ) : expression ) */
37178 cp_parser_omp_clause_linear (cp_parser
*parser
, tree list
,
37181 tree nlist
, c
, step
= integer_one_node
;
37183 enum omp_clause_linear_kind kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
37185 matching_parens parens
;
37186 if (!parens
.require_open (parser
))
37189 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37191 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37192 const char *p
= IDENTIFIER_POINTER (id
);
37194 if (strcmp ("ref", p
) == 0)
37195 kind
= OMP_CLAUSE_LINEAR_REF
;
37196 else if (strcmp ("val", p
) == 0)
37197 kind
= OMP_CLAUSE_LINEAR_VAL
;
37198 else if (strcmp ("uval", p
) == 0)
37199 kind
= OMP_CLAUSE_LINEAR_UVAL
;
37200 if (cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
37201 cp_lexer_consume_token (parser
->lexer
);
37203 kind
= OMP_CLAUSE_LINEAR_DEFAULT
;
37206 if (kind
== OMP_CLAUSE_LINEAR_DEFAULT
)
37207 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_LINEAR
, list
,
37211 nlist
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINEAR
, list
);
37212 colon
= cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
);
37214 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
37215 else if (!parens
.require_close (parser
))
37216 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37217 /*or_comma=*/false,
37218 /*consume_paren=*/true);
37225 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37226 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_CLOSE_PAREN
))
37228 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
37229 cp_parser_parse_tentatively (parser
);
37230 step
= cp_parser_id_expression (parser
, /*template_p=*/false,
37231 /*check_dependency_p=*/true,
37232 /*template_p=*/NULL
,
37233 /*declarator_p=*/false,
37234 /*optional_p=*/false);
37235 if (step
!= error_mark_node
)
37236 step
= cp_parser_lookup_name_simple (parser
, step
, token
->location
);
37237 if (step
== error_mark_node
)
37240 cp_parser_abort_tentative_parse (parser
);
37242 else if (!cp_parser_parse_definitely (parser
))
37246 step
= cp_parser_assignment_expression (parser
);
37248 if (!parens
.require_close (parser
))
37249 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37250 /*or_comma=*/false,
37251 /*consume_paren=*/true);
37253 if (step
== error_mark_node
)
37257 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37259 OMP_CLAUSE_LINEAR_STEP (c
) = step
;
37260 OMP_CLAUSE_LINEAR_KIND (c
) = kind
;
37267 safelen ( constant-expression ) */
37270 cp_parser_omp_clause_safelen (cp_parser
*parser
, tree list
,
37271 location_t location
)
37275 matching_parens parens
;
37276 if (!parens
.require_open (parser
))
37279 t
= cp_parser_constant_expression (parser
);
37281 if (t
== error_mark_node
37282 || !parens
.require_close (parser
))
37283 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37284 /*or_comma=*/false,
37285 /*consume_paren=*/true);
37287 check_no_duplicate_clause (list
, OMP_CLAUSE_SAFELEN
, "safelen", location
);
37289 c
= build_omp_clause (location
, OMP_CLAUSE_SAFELEN
);
37290 OMP_CLAUSE_SAFELEN_EXPR (c
) = t
;
37291 OMP_CLAUSE_CHAIN (c
) = list
;
37297 simdlen ( constant-expression ) */
37300 cp_parser_omp_clause_simdlen (cp_parser
*parser
, tree list
,
37301 location_t location
)
37305 matching_parens parens
;
37306 if (!parens
.require_open (parser
))
37309 t
= cp_parser_constant_expression (parser
);
37311 if (t
== error_mark_node
37312 || !parens
.require_close (parser
))
37313 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37314 /*or_comma=*/false,
37315 /*consume_paren=*/true);
37317 check_no_duplicate_clause (list
, OMP_CLAUSE_SIMDLEN
, "simdlen", location
);
37319 c
= build_omp_clause (location
, OMP_CLAUSE_SIMDLEN
);
37320 OMP_CLAUSE_SIMDLEN_EXPR (c
) = t
;
37321 OMP_CLAUSE_CHAIN (c
) = list
;
37328 identifier [+/- integer]
37329 vec , identifier [+/- integer]
37333 cp_parser_omp_clause_depend_sink (cp_parser
*parser
, location_t clause_loc
,
37338 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
37340 cp_parser_error (parser
, "expected identifier");
37344 while (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37346 location_t id_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37347 tree t
, identifier
= cp_parser_identifier (parser
);
37348 tree addend
= NULL
;
37350 if (identifier
== error_mark_node
)
37351 t
= error_mark_node
;
37354 t
= cp_parser_lookup_name_simple
37355 (parser
, identifier
,
37356 cp_lexer_peek_token (parser
->lexer
)->location
);
37357 if (t
== error_mark_node
)
37358 cp_parser_name_lookup_error (parser
, identifier
, t
, NLE_NULL
,
37363 if (cp_lexer_next_token_is (parser
->lexer
, CPP_MINUS
))
37365 else if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PLUS
))
37367 addend
= integer_zero_node
;
37368 goto add_to_vector
;
37370 cp_lexer_consume_token (parser
->lexer
);
37372 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NUMBER
))
37374 cp_parser_error (parser
, "expected integer");
37378 addend
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37379 if (TREE_CODE (addend
) != INTEGER_CST
)
37381 cp_parser_error (parser
, "expected integer");
37384 cp_lexer_consume_token (parser
->lexer
);
37387 if (t
!= error_mark_node
)
37389 vec
= tree_cons (addend
, t
, vec
);
37391 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec
) = 1;
37394 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_COMMA
))
37397 cp_lexer_consume_token (parser
->lexer
);
37400 if (cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
) && vec
)
37402 tree u
= build_omp_clause (clause_loc
, OMP_CLAUSE_DEPEND
);
37403 OMP_CLAUSE_DEPEND_KIND (u
) = OMP_CLAUSE_DEPEND_SINK
;
37404 OMP_CLAUSE_DECL (u
) = nreverse (vec
);
37405 OMP_CLAUSE_CHAIN (u
) = list
;
37412 detach ( event-handle ) */
37415 cp_parser_omp_clause_detach (cp_parser
*parser
, tree list
)
37417 matching_parens parens
;
37419 if (!parens
.require_open (parser
))
37425 token
= cp_lexer_peek_token (parser
->lexer
);
37426 name
= cp_parser_id_expression (parser
, /*template_p=*/false,
37427 /*check_dependency_p=*/true,
37428 /*template_p=*/NULL
,
37429 /*declarator_p=*/false,
37430 /*optional_p=*/false);
37431 if (name
== error_mark_node
)
37432 decl
= error_mark_node
;
37435 if (identifier_p (name
))
37436 decl
= cp_parser_lookup_name_simple (parser
, name
, token
->location
);
37439 if (decl
== error_mark_node
)
37440 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
,
37444 if (decl
== error_mark_node
37445 || !parens
.require_close (parser
))
37446 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37447 /*or_comma=*/false,
37448 /*consume_paren=*/true);
37450 tree u
= build_omp_clause (token
->location
, OMP_CLAUSE_DETACH
);
37451 OMP_CLAUSE_DECL (u
) = decl
;
37452 OMP_CLAUSE_CHAIN (u
) = list
;
37458 iterators ( iterators-definition )
37460 iterators-definition:
37462 iterator-specifier , iterators-definition
37464 iterator-specifier:
37465 identifier = range-specification
37466 iterator-type identifier = range-specification
37468 range-specification:
37470 begin : end : step */
37473 cp_parser_omp_iterators (cp_parser
*parser
)
37475 tree ret
= NULL_TREE
, *last
= &ret
;
37476 cp_lexer_consume_token (parser
->lexer
);
37478 matching_parens parens
;
37479 if (!parens
.require_open (parser
))
37480 return error_mark_node
;
37482 bool saved_colon_corrects_to_scope_p
37483 = parser
->colon_corrects_to_scope_p
;
37484 bool saved_colon_doesnt_start_class_def_p
37485 = parser
->colon_doesnt_start_class_def_p
;
37490 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37491 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_EQ
))
37492 iter_type
= integer_type_node
;
37495 const char *saved_message
37496 = parser
->type_definition_forbidden_message
;
37497 parser
->type_definition_forbidden_message
37498 = G_("types may not be defined in iterator type");
37500 iter_type
= cp_parser_type_id (parser
);
37502 parser
->type_definition_forbidden_message
= saved_message
;
37505 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37506 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
37508 cp_parser_error (parser
, "expected identifier");
37512 tree id
= cp_parser_identifier (parser
);
37513 if (id
== error_mark_node
)
37516 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
37519 parser
->colon_corrects_to_scope_p
= false;
37520 parser
->colon_doesnt_start_class_def_p
= true;
37521 tree begin
= cp_parser_assignment_expression (parser
);
37523 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37526 tree end
= cp_parser_assignment_expression (parser
);
37528 tree step
= integer_one_node
;
37529 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
37531 cp_lexer_consume_token (parser
->lexer
);
37532 step
= cp_parser_assignment_expression (parser
);
37535 tree iter_var
= build_decl (loc
, VAR_DECL
, id
, iter_type
);
37536 DECL_ARTIFICIAL (iter_var
) = 1;
37537 DECL_CONTEXT (iter_var
) = current_function_decl
;
37538 pushdecl (iter_var
);
37540 *last
= make_tree_vec (6);
37541 TREE_VEC_ELT (*last
, 0) = iter_var
;
37542 TREE_VEC_ELT (*last
, 1) = begin
;
37543 TREE_VEC_ELT (*last
, 2) = end
;
37544 TREE_VEC_ELT (*last
, 3) = step
;
37545 last
= &TREE_CHAIN (*last
);
37547 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37549 cp_lexer_consume_token (parser
->lexer
);
37556 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
37557 parser
->colon_doesnt_start_class_def_p
37558 = saved_colon_doesnt_start_class_def_p
;
37560 if (!parens
.require_close (parser
))
37561 cp_parser_skip_to_closing_parenthesis (parser
,
37562 /*recovering=*/true,
37563 /*or_comma=*/false,
37564 /*consume_paren=*/true);
37566 return ret
? ret
: error_mark_node
;
37570 depend ( depend-kind : variable-list )
37578 depend ( sink : vec )
37581 depend ( depend-modifier , depend-kind: variable-list )
37584 in | out | inout | mutexinoutset | depobj
37587 iterator ( iterators-definition ) */
37590 cp_parser_omp_clause_depend (cp_parser
*parser
, tree list
, location_t loc
)
37592 tree nlist
, c
, iterators
= NULL_TREE
;
37593 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_LAST
;
37595 matching_parens parens
;
37596 if (!parens
.require_open (parser
))
37601 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_NAME
))
37604 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37605 const char *p
= IDENTIFIER_POINTER (id
);
37607 if (strcmp ("iterator", p
) == 0 && iterators
== NULL_TREE
)
37609 begin_scope (sk_omp
, NULL
);
37610 iterators
= cp_parser_omp_iterators (parser
);
37611 cp_parser_require (parser
, CPP_COMMA
, RT_COMMA
);
37614 if (strcmp ("in", p
) == 0)
37615 kind
= OMP_CLAUSE_DEPEND_IN
;
37616 else if (strcmp ("inout", p
) == 0)
37617 kind
= OMP_CLAUSE_DEPEND_INOUT
;
37618 else if (strcmp ("mutexinoutset", p
) == 0)
37619 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
37620 else if (strcmp ("out", p
) == 0)
37621 kind
= OMP_CLAUSE_DEPEND_OUT
;
37622 else if (strcmp ("depobj", p
) == 0)
37623 kind
= OMP_CLAUSE_DEPEND_DEPOBJ
;
37624 else if (strcmp ("sink", p
) == 0)
37625 kind
= OMP_CLAUSE_DEPEND_SINK
;
37626 else if (strcmp ("source", p
) == 0)
37627 kind
= OMP_CLAUSE_DEPEND_SOURCE
;
37634 cp_lexer_consume_token (parser
->lexer
);
37637 && (kind
== OMP_CLAUSE_DEPEND_SOURCE
|| kind
== OMP_CLAUSE_DEPEND_SINK
))
37639 poplevel (0, 1, 0);
37640 error_at (loc
, "%<iterator%> modifier incompatible with %qs",
37641 kind
== OMP_CLAUSE_DEPEND_SOURCE
? "source" : "sink");
37642 iterators
= NULL_TREE
;
37645 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
37647 c
= build_omp_clause (loc
, OMP_CLAUSE_DEPEND
);
37648 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
37649 OMP_CLAUSE_DECL (c
) = NULL_TREE
;
37650 OMP_CLAUSE_CHAIN (c
) = list
;
37651 if (!parens
.require_close (parser
))
37652 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37653 /*or_comma=*/false,
37654 /*consume_paren=*/true);
37658 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
37661 if (kind
== OMP_CLAUSE_DEPEND_SINK
)
37662 nlist
= cp_parser_omp_clause_depend_sink (parser
, loc
, list
);
37665 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_DEPEND
,
37670 tree block
= poplevel (1, 1, 0);
37671 if (iterators
== error_mark_node
)
37672 iterators
= NULL_TREE
;
37674 TREE_VEC_ELT (iterators
, 5) = block
;
37677 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37679 OMP_CLAUSE_DEPEND_KIND (c
) = kind
;
37681 OMP_CLAUSE_DECL (c
)
37682 = build_tree_list (iterators
, OMP_CLAUSE_DECL (c
));
37688 cp_parser_error (parser
, "invalid depend kind");
37691 poplevel (0, 1, 0);
37692 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37693 /*or_comma=*/false,
37694 /*consume_paren=*/true);
37699 map ( map-kind : variable-list )
37700 map ( variable-list )
37703 alloc | to | from | tofrom
37707 alloc | to | from | tofrom | release | delete
37709 map ( always [,] map-kind: variable-list ) */
37712 cp_parser_omp_clause_map (cp_parser
*parser
, tree list
)
37715 enum gomp_map_kind kind
= GOMP_MAP_TOFROM
;
37716 bool always
= false;
37718 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37721 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37723 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37724 const char *p
= IDENTIFIER_POINTER (id
);
37726 if (strcmp ("always", p
) == 0)
37729 if (cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COMMA
)
37731 if ((cp_lexer_peek_nth_token (parser
->lexer
, nth
)->type
== CPP_NAME
37732 || (cp_lexer_peek_nth_token (parser
->lexer
, nth
)->keyword
37734 && (cp_lexer_peek_nth_token (parser
->lexer
, nth
+ 1)->type
37738 cp_lexer_consume_token (parser
->lexer
);
37740 cp_lexer_consume_token (parser
->lexer
);
37745 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
37746 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
37748 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37749 const char *p
= IDENTIFIER_POINTER (id
);
37751 if (strcmp ("alloc", p
) == 0)
37752 kind
= GOMP_MAP_ALLOC
;
37753 else if (strcmp ("to", p
) == 0)
37754 kind
= always
? GOMP_MAP_ALWAYS_TO
: GOMP_MAP_TO
;
37755 else if (strcmp ("from", p
) == 0)
37756 kind
= always
? GOMP_MAP_ALWAYS_FROM
: GOMP_MAP_FROM
;
37757 else if (strcmp ("tofrom", p
) == 0)
37758 kind
= always
? GOMP_MAP_ALWAYS_TOFROM
: GOMP_MAP_TOFROM
;
37759 else if (strcmp ("release", p
) == 0)
37760 kind
= GOMP_MAP_RELEASE
;
37763 cp_parser_error (parser
, "invalid map kind");
37764 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37765 /*or_comma=*/false,
37766 /*consume_paren=*/true);
37769 cp_lexer_consume_token (parser
->lexer
);
37770 cp_lexer_consume_token (parser
->lexer
);
37772 else if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_DELETE
)
37773 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
== CPP_COLON
)
37775 kind
= GOMP_MAP_DELETE
;
37776 cp_lexer_consume_token (parser
->lexer
);
37777 cp_lexer_consume_token (parser
->lexer
);
37780 nlist
= cp_parser_omp_var_list_no_open (parser
, OMP_CLAUSE_MAP
, list
,
37783 for (c
= nlist
; c
!= list
; c
= OMP_CLAUSE_CHAIN (c
))
37784 OMP_CLAUSE_SET_MAP_KIND (c
, kind
);
37790 device ( expression ) */
37793 cp_parser_omp_clause_device (cp_parser
*parser
, tree list
,
37794 location_t location
)
37798 matching_parens parens
;
37799 if (!parens
.require_open (parser
))
37802 t
= cp_parser_assignment_expression (parser
);
37804 if (t
== error_mark_node
37805 || !parens
.require_close (parser
))
37806 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37807 /*or_comma=*/false,
37808 /*consume_paren=*/true);
37810 check_no_duplicate_clause (list
, OMP_CLAUSE_DEVICE
,
37811 "device", location
);
37813 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE
);
37814 OMP_CLAUSE_DEVICE_ID (c
) = t
;
37815 OMP_CLAUSE_CHAIN (c
) = list
;
37821 dist_schedule ( static )
37822 dist_schedule ( static , expression ) */
37825 cp_parser_omp_clause_dist_schedule (cp_parser
*parser
, tree list
,
37826 location_t location
)
37830 matching_parens parens
;
37831 if (!parens
.require_open (parser
))
37834 c
= build_omp_clause (location
, OMP_CLAUSE_DIST_SCHEDULE
);
37836 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_STATIC
))
37838 cp_lexer_consume_token (parser
->lexer
);
37840 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
37842 cp_lexer_consume_token (parser
->lexer
);
37844 t
= cp_parser_assignment_expression (parser
);
37846 if (t
== error_mark_node
)
37848 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c
) = t
;
37850 if (!parens
.require_close (parser
))
37853 else if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
37856 /* check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE,
37857 "dist_schedule", location); */
37858 if (omp_find_clause (list
, OMP_CLAUSE_DIST_SCHEDULE
))
37859 warning_at (location
, 0, "too many %qs clauses", "dist_schedule");
37860 OMP_CLAUSE_CHAIN (c
) = list
;
37864 cp_parser_error (parser
, "invalid dist_schedule kind");
37866 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37867 /*or_comma=*/false,
37868 /*consume_paren=*/true);
37873 proc_bind ( proc-bind-kind )
37876 master | close | spread */
37879 cp_parser_omp_clause_proc_bind (cp_parser
*parser
, tree list
,
37880 location_t location
)
37883 enum omp_clause_proc_bind_kind kind
;
37885 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37888 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37890 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37891 const char *p
= IDENTIFIER_POINTER (id
);
37893 if (strcmp ("master", p
) == 0)
37894 kind
= OMP_CLAUSE_PROC_BIND_MASTER
;
37895 else if (strcmp ("close", p
) == 0)
37896 kind
= OMP_CLAUSE_PROC_BIND_CLOSE
;
37897 else if (strcmp ("spread", p
) == 0)
37898 kind
= OMP_CLAUSE_PROC_BIND_SPREAD
;
37905 cp_lexer_consume_token (parser
->lexer
);
37906 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
37909 c
= build_omp_clause (location
, OMP_CLAUSE_PROC_BIND
);
37910 check_no_duplicate_clause (list
, OMP_CLAUSE_PROC_BIND
, "proc_bind",
37912 OMP_CLAUSE_PROC_BIND_KIND (c
) = kind
;
37913 OMP_CLAUSE_CHAIN (c
) = list
;
37917 cp_parser_error (parser
, "invalid depend kind");
37919 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37920 /*or_comma=*/false,
37921 /*consume_paren=*/true);
37926 device_type ( host | nohost | any ) */
37929 cp_parser_omp_clause_device_type (cp_parser
*parser
, tree list
,
37930 location_t location
)
37933 enum omp_clause_device_type_kind kind
;
37935 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
37938 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
37940 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
37941 const char *p
= IDENTIFIER_POINTER (id
);
37943 if (strcmp ("host", p
) == 0)
37944 kind
= OMP_CLAUSE_DEVICE_TYPE_HOST
;
37945 else if (strcmp ("nohost", p
) == 0)
37946 kind
= OMP_CLAUSE_DEVICE_TYPE_NOHOST
;
37947 else if (strcmp ("any", p
) == 0)
37948 kind
= OMP_CLAUSE_DEVICE_TYPE_ANY
;
37955 cp_lexer_consume_token (parser
->lexer
);
37956 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_COMMA_CLOSE_PAREN
))
37959 c
= build_omp_clause (location
, OMP_CLAUSE_DEVICE_TYPE
);
37960 /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, "device_type",
37962 OMP_CLAUSE_DEVICE_TYPE_KIND (c
) = kind
;
37963 OMP_CLAUSE_CHAIN (c
) = list
;
37967 cp_parser_error (parser
, "invalid depend kind");
37969 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37970 /*or_comma=*/false,
37971 /*consume_paren=*/true);
37976 async [( int-expr )] */
37979 cp_parser_oacc_clause_async (cp_parser
*parser
, tree list
)
37982 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
37984 t
= build_int_cst (integer_type_node
, GOMP_ASYNC_NOVAL
);
37986 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
37988 matching_parens parens
;
37989 parens
.consume_open (parser
);
37991 t
= cp_parser_expression (parser
);
37992 if (t
== error_mark_node
37993 || !parens
.require_close (parser
))
37994 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
37995 /*or_comma=*/false,
37996 /*consume_paren=*/true);
37999 check_no_duplicate_clause (list
, OMP_CLAUSE_ASYNC
, "async", loc
);
38001 c
= build_omp_clause (loc
, OMP_CLAUSE_ASYNC
);
38002 OMP_CLAUSE_ASYNC_EXPR (c
) = t
;
38003 OMP_CLAUSE_CHAIN (c
) = list
;
38009 /* Parse all OpenACC clauses. The set clauses allowed by the directive
38010 is a bitmask in MASK. Return the list of clauses found. */
38013 cp_parser_oacc_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
38014 const char *where
, cp_token
*pragma_tok
,
38015 bool finish_p
= true)
38017 tree clauses
= NULL
;
38020 /* Don't create location wrapper nodes within OpenACC clauses. */
38021 auto_suppress_location_wrappers sentinel
;
38023 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38026 pragma_omp_clause c_kind
;
38027 omp_clause_code code
;
38028 const char *c_name
;
38029 tree prev
= clauses
;
38031 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38032 cp_lexer_consume_token (parser
->lexer
);
38034 here
= cp_lexer_peek_token (parser
->lexer
)->location
;
38035 c_kind
= cp_parser_omp_clause_name (parser
);
38039 case PRAGMA_OACC_CLAUSE_ASYNC
:
38040 clauses
= cp_parser_oacc_clause_async (parser
, clauses
);
38043 case PRAGMA_OACC_CLAUSE_AUTO
:
38044 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_AUTO
,
38048 case PRAGMA_OACC_CLAUSE_ATTACH
:
38049 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38052 case PRAGMA_OACC_CLAUSE_COLLAPSE
:
38053 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
, here
);
38054 c_name
= "collapse";
38056 case PRAGMA_OACC_CLAUSE_COPY
:
38057 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38060 case PRAGMA_OACC_CLAUSE_COPYIN
:
38061 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38064 case PRAGMA_OACC_CLAUSE_COPYOUT
:
38065 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38066 c_name
= "copyout";
38068 case PRAGMA_OACC_CLAUSE_CREATE
:
38069 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38072 case PRAGMA_OACC_CLAUSE_DELETE
:
38073 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38076 case PRAGMA_OMP_CLAUSE_DEFAULT
:
38077 clauses
= cp_parser_omp_clause_default (parser
, clauses
, here
, true);
38078 c_name
= "default";
38080 case PRAGMA_OACC_CLAUSE_DETACH
:
38081 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38084 case PRAGMA_OACC_CLAUSE_DEVICE
:
38085 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38088 case PRAGMA_OACC_CLAUSE_DEVICEPTR
:
38089 clauses
= cp_parser_oacc_data_clause_deviceptr (parser
, clauses
);
38090 c_name
= "deviceptr";
38092 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT
:
38093 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38094 c_name
= "device_resident";
38096 case PRAGMA_OACC_CLAUSE_FINALIZE
:
38097 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_FINALIZE
,
38099 c_name
= "finalize";
38101 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE
:
38102 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
38104 c_name
= "firstprivate";
38106 case PRAGMA_OACC_CLAUSE_GANG
:
38108 clauses
= cp_parser_oacc_shape_clause (parser
, here
, OMP_CLAUSE_GANG
,
38111 case PRAGMA_OACC_CLAUSE_HOST
:
38112 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38115 case PRAGMA_OACC_CLAUSE_IF
:
38116 clauses
= cp_parser_omp_clause_if (parser
, clauses
, here
, false);
38119 case PRAGMA_OACC_CLAUSE_IF_PRESENT
:
38120 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_IF_PRESENT
,
38122 c_name
= "if_present";
38124 case PRAGMA_OACC_CLAUSE_INDEPENDENT
:
38125 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_INDEPENDENT
,
38127 c_name
= "independent";
38129 case PRAGMA_OACC_CLAUSE_LINK
:
38130 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38133 case PRAGMA_OACC_CLAUSE_NO_CREATE
:
38134 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38135 c_name
= "no_create";
38137 case PRAGMA_OACC_CLAUSE_NUM_GANGS
:
38138 code
= OMP_CLAUSE_NUM_GANGS
;
38139 c_name
= "num_gangs";
38140 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
38143 case PRAGMA_OACC_CLAUSE_NUM_WORKERS
:
38144 c_name
= "num_workers";
38145 code
= OMP_CLAUSE_NUM_WORKERS
;
38146 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
38149 case PRAGMA_OACC_CLAUSE_PRESENT
:
38150 clauses
= cp_parser_oacc_data_clause (parser
, c_kind
, clauses
);
38151 c_name
= "present";
38153 case PRAGMA_OACC_CLAUSE_PRIVATE
:
38154 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
38156 c_name
= "private";
38158 case PRAGMA_OACC_CLAUSE_REDUCTION
:
38160 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
38162 c_name
= "reduction";
38164 case PRAGMA_OACC_CLAUSE_SEQ
:
38165 clauses
= cp_parser_oacc_simple_clause (here
, OMP_CLAUSE_SEQ
,
38169 case PRAGMA_OACC_CLAUSE_TILE
:
38170 clauses
= cp_parser_oacc_clause_tile (parser
, here
, clauses
);
38173 case PRAGMA_OACC_CLAUSE_USE_DEVICE
:
38174 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
38176 c_name
= "use_device";
38178 case PRAGMA_OACC_CLAUSE_VECTOR
:
38180 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
38184 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH
:
38185 c_name
= "vector_length";
38186 code
= OMP_CLAUSE_VECTOR_LENGTH
;
38187 clauses
= cp_parser_oacc_single_int_clause (parser
, code
, c_name
,
38190 case PRAGMA_OACC_CLAUSE_WAIT
:
38191 clauses
= cp_parser_oacc_clause_wait (parser
, clauses
);
38194 case PRAGMA_OACC_CLAUSE_WORKER
:
38196 clauses
= cp_parser_oacc_shape_clause (parser
, here
,
38201 cp_parser_error (parser
, "expected %<#pragma acc%> clause");
38207 if (((mask
>> c_kind
) & 1) == 0)
38209 /* Remove the invalid clause(s) from the list to avoid
38210 confusing the rest of the compiler. */
38212 error_at (here
, "%qs is not valid for %qs", c_name
, where
);
38217 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38220 return finish_omp_clauses (clauses
, C_ORT_ACC
);
38225 /* Parse all OpenMP clauses. The set clauses allowed by the directive
38226 is a bitmask in MASK. Return the list of clauses found.
38227 FINISH_P set if finish_omp_clauses should be called.
38228 NESTED non-zero if clauses should be terminated by closing paren instead
38229 of end of pragma. If it is 2, additionally commas are required in between
38233 cp_parser_omp_all_clauses (cp_parser
*parser
, omp_clause_mask mask
,
38234 const char *where
, cp_token
*pragma_tok
,
38235 bool finish_p
= true, int nested
= 0)
38237 tree clauses
= NULL
;
38239 cp_token
*token
= NULL
;
38241 /* Don't create location wrapper nodes within OpenMP clauses. */
38242 auto_suppress_location_wrappers sentinel
;
38244 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38246 pragma_omp_clause c_kind
;
38247 const char *c_name
;
38248 tree prev
= clauses
;
38250 if (nested
&& cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
))
38255 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38256 cp_lexer_consume_token (parser
->lexer
);
38257 else if (nested
== 2)
38258 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
38259 "clauses in %<simd%> trait should be separated "
38263 token
= cp_lexer_peek_token (parser
->lexer
);
38264 c_kind
= cp_parser_omp_clause_name (parser
);
38268 case PRAGMA_OMP_CLAUSE_BIND
:
38269 clauses
= cp_parser_omp_clause_bind (parser
, clauses
,
38273 case PRAGMA_OMP_CLAUSE_COLLAPSE
:
38274 clauses
= cp_parser_omp_clause_collapse (parser
, clauses
,
38276 c_name
= "collapse";
38278 case PRAGMA_OMP_CLAUSE_COPYIN
:
38279 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYIN
, clauses
);
38282 case PRAGMA_OMP_CLAUSE_COPYPRIVATE
:
38283 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_COPYPRIVATE
,
38285 c_name
= "copyprivate";
38287 case PRAGMA_OMP_CLAUSE_DEFAULT
:
38288 clauses
= cp_parser_omp_clause_default (parser
, clauses
,
38289 token
->location
, false);
38290 c_name
= "default";
38292 case PRAGMA_OMP_CLAUSE_FINAL
:
38293 clauses
= cp_parser_omp_clause_final (parser
, clauses
, token
->location
);
38296 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE
:
38297 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FIRSTPRIVATE
,
38299 c_name
= "firstprivate";
38301 case PRAGMA_OMP_CLAUSE_GRAINSIZE
:
38302 clauses
= cp_parser_omp_clause_grainsize (parser
, clauses
,
38304 c_name
= "grainsize";
38306 case PRAGMA_OMP_CLAUSE_HINT
:
38307 clauses
= cp_parser_omp_clause_hint (parser
, clauses
,
38311 case PRAGMA_OMP_CLAUSE_DEFAULTMAP
:
38312 clauses
= cp_parser_omp_clause_defaultmap (parser
, clauses
,
38314 c_name
= "defaultmap";
38316 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR
:
38317 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_PTR
,
38319 c_name
= "use_device_ptr";
38321 case PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR
:
38322 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_USE_DEVICE_ADDR
,
38324 c_name
= "use_device_addr";
38326 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR
:
38327 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_IS_DEVICE_PTR
,
38329 c_name
= "is_device_ptr";
38331 case PRAGMA_OMP_CLAUSE_IF
:
38332 clauses
= cp_parser_omp_clause_if (parser
, clauses
, token
->location
,
38336 case PRAGMA_OMP_CLAUSE_IN_REDUCTION
:
38338 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_IN_REDUCTION
,
38340 c_name
= "in_reduction";
38342 case PRAGMA_OMP_CLAUSE_LASTPRIVATE
:
38343 clauses
= cp_parser_omp_clause_lastprivate (parser
, clauses
);
38344 c_name
= "lastprivate";
38346 case PRAGMA_OMP_CLAUSE_MERGEABLE
:
38347 clauses
= cp_parser_omp_clause_mergeable (parser
, clauses
,
38349 c_name
= "mergeable";
38351 case PRAGMA_OMP_CLAUSE_NOWAIT
:
38352 clauses
= cp_parser_omp_clause_nowait (parser
, clauses
,
38356 case PRAGMA_OMP_CLAUSE_NUM_TASKS
:
38357 clauses
= cp_parser_omp_clause_num_tasks (parser
, clauses
,
38359 c_name
= "num_tasks";
38361 case PRAGMA_OMP_CLAUSE_NUM_THREADS
:
38362 clauses
= cp_parser_omp_clause_num_threads (parser
, clauses
,
38364 c_name
= "num_threads";
38366 case PRAGMA_OMP_CLAUSE_ORDER
:
38367 clauses
= cp_parser_omp_clause_order (parser
, clauses
,
38371 case PRAGMA_OMP_CLAUSE_ORDERED
:
38372 clauses
= cp_parser_omp_clause_ordered (parser
, clauses
,
38374 c_name
= "ordered";
38376 case PRAGMA_OMP_CLAUSE_PRIORITY
:
38377 clauses
= cp_parser_omp_clause_priority (parser
, clauses
,
38379 c_name
= "priority";
38381 case PRAGMA_OMP_CLAUSE_PRIVATE
:
38382 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_PRIVATE
,
38384 c_name
= "private";
38386 case PRAGMA_OMP_CLAUSE_REDUCTION
:
38388 = cp_parser_omp_clause_reduction (parser
, OMP_CLAUSE_REDUCTION
,
38390 c_name
= "reduction";
38392 case PRAGMA_OMP_CLAUSE_SCHEDULE
:
38393 clauses
= cp_parser_omp_clause_schedule (parser
, clauses
,
38395 c_name
= "schedule";
38397 case PRAGMA_OMP_CLAUSE_SHARED
:
38398 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_SHARED
,
38402 case PRAGMA_OMP_CLAUSE_TASK_REDUCTION
:
38404 = cp_parser_omp_clause_reduction (parser
,
38405 OMP_CLAUSE_TASK_REDUCTION
,
38407 c_name
= "task_reduction";
38409 case PRAGMA_OMP_CLAUSE_UNTIED
:
38410 clauses
= cp_parser_omp_clause_untied (parser
, clauses
,
38414 case PRAGMA_OMP_CLAUSE_INBRANCH
:
38415 clauses
= cp_parser_omp_clause_branch (parser
, OMP_CLAUSE_INBRANCH
,
38416 clauses
, token
->location
);
38417 c_name
= "inbranch";
38419 case PRAGMA_OMP_CLAUSE_NONTEMPORAL
:
38420 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_NONTEMPORAL
,
38422 c_name
= "nontemporal";
38424 case PRAGMA_OMP_CLAUSE_NOTINBRANCH
:
38425 clauses
= cp_parser_omp_clause_branch (parser
,
38426 OMP_CLAUSE_NOTINBRANCH
,
38427 clauses
, token
->location
);
38428 c_name
= "notinbranch";
38430 case PRAGMA_OMP_CLAUSE_PARALLEL
:
38431 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_PARALLEL
,
38432 clauses
, token
->location
);
38433 c_name
= "parallel";
38437 error_at (token
->location
, "%qs must be the first clause of %qs",
38442 case PRAGMA_OMP_CLAUSE_FOR
:
38443 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_FOR
,
38444 clauses
, token
->location
);
38447 goto clause_not_first
;
38449 case PRAGMA_OMP_CLAUSE_SECTIONS
:
38450 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_SECTIONS
,
38451 clauses
, token
->location
);
38452 c_name
= "sections";
38454 goto clause_not_first
;
38456 case PRAGMA_OMP_CLAUSE_TASKGROUP
:
38457 clauses
= cp_parser_omp_clause_cancelkind (parser
, OMP_CLAUSE_TASKGROUP
,
38458 clauses
, token
->location
);
38459 c_name
= "taskgroup";
38461 goto clause_not_first
;
38463 case PRAGMA_OMP_CLAUSE_LINK
:
38464 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_LINK
, clauses
);
38467 case PRAGMA_OMP_CLAUSE_TO
:
38468 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINK
)) != 0)
38469 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
38472 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO
, clauses
);
38475 case PRAGMA_OMP_CLAUSE_FROM
:
38476 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_FROM
, clauses
);
38479 case PRAGMA_OMP_CLAUSE_UNIFORM
:
38480 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_UNIFORM
,
38482 c_name
= "uniform";
38484 case PRAGMA_OMP_CLAUSE_NUM_TEAMS
:
38485 clauses
= cp_parser_omp_clause_num_teams (parser
, clauses
,
38487 c_name
= "num_teams";
38489 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT
:
38490 clauses
= cp_parser_omp_clause_thread_limit (parser
, clauses
,
38492 c_name
= "thread_limit";
38494 case PRAGMA_OMP_CLAUSE_ALIGNED
:
38495 clauses
= cp_parser_omp_clause_aligned (parser
, clauses
);
38496 c_name
= "aligned";
38498 case PRAGMA_OMP_CLAUSE_ALLOCATE
:
38499 clauses
= cp_parser_omp_clause_allocate (parser
, clauses
);
38500 c_name
= "allocate";
38502 case PRAGMA_OMP_CLAUSE_LINEAR
:
38504 bool declare_simd
= false;
38505 if (((mask
>> PRAGMA_OMP_CLAUSE_UNIFORM
) & 1) != 0)
38506 declare_simd
= true;
38507 clauses
= cp_parser_omp_clause_linear (parser
, clauses
, declare_simd
);
38511 case PRAGMA_OMP_CLAUSE_DEPEND
:
38512 clauses
= cp_parser_omp_clause_depend (parser
, clauses
,
38516 case PRAGMA_OMP_CLAUSE_DETACH
:
38517 clauses
= cp_parser_omp_clause_detach (parser
, clauses
);
38520 case PRAGMA_OMP_CLAUSE_MAP
:
38521 clauses
= cp_parser_omp_clause_map (parser
, clauses
);
38524 case PRAGMA_OMP_CLAUSE_DEVICE
:
38525 clauses
= cp_parser_omp_clause_device (parser
, clauses
,
38529 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
:
38530 clauses
= cp_parser_omp_clause_dist_schedule (parser
, clauses
,
38532 c_name
= "dist_schedule";
38534 case PRAGMA_OMP_CLAUSE_PROC_BIND
:
38535 clauses
= cp_parser_omp_clause_proc_bind (parser
, clauses
,
38537 c_name
= "proc_bind";
38539 case PRAGMA_OMP_CLAUSE_DEVICE_TYPE
:
38540 clauses
= cp_parser_omp_clause_device_type (parser
, clauses
,
38542 c_name
= "device_type";
38544 case PRAGMA_OMP_CLAUSE_SAFELEN
:
38545 clauses
= cp_parser_omp_clause_safelen (parser
, clauses
,
38547 c_name
= "safelen";
38549 case PRAGMA_OMP_CLAUSE_SIMDLEN
:
38550 clauses
= cp_parser_omp_clause_simdlen (parser
, clauses
,
38552 c_name
= "simdlen";
38554 case PRAGMA_OMP_CLAUSE_NOGROUP
:
38555 clauses
= cp_parser_omp_clause_nogroup (parser
, clauses
,
38557 c_name
= "nogroup";
38559 case PRAGMA_OMP_CLAUSE_THREADS
:
38561 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_THREADS
,
38562 clauses
, token
->location
);
38563 c_name
= "threads";
38565 case PRAGMA_OMP_CLAUSE_SIMD
:
38567 = cp_parser_omp_clause_orderedkind (parser
, OMP_CLAUSE_SIMD
,
38568 clauses
, token
->location
);
38572 cp_parser_error (parser
, "expected %<#pragma omp%> clause");
38578 if (((mask
>> c_kind
) & 1) == 0)
38580 /* Remove the invalid clause(s) from the list to avoid
38581 confusing the rest of the compiler. */
38583 error_at (token
->location
, "%qs is not valid for %qs", c_name
, where
);
38588 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
38591 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_UNIFORM
)) != 0)
38592 return finish_omp_clauses (clauses
, C_ORT_OMP_DECLARE_SIMD
);
38594 return finish_omp_clauses (clauses
, C_ORT_OMP
);
38603 In practice, we're also interested in adding the statement to an
38604 outer node. So it is convenient if we work around the fact that
38605 cp_parser_statement calls add_stmt. */
38608 cp_parser_begin_omp_structured_block (cp_parser
*parser
)
38610 unsigned save
= parser
->in_statement
;
38612 /* Only move the values to IN_OMP_BLOCK if they weren't false.
38613 This preserves the "not within loop or switch" style error messages
38614 for nonsense cases like
38620 if (parser
->in_statement
)
38621 parser
->in_statement
= IN_OMP_BLOCK
;
38627 cp_parser_end_omp_structured_block (cp_parser
*parser
, unsigned save
)
38629 parser
->in_statement
= save
;
38633 cp_parser_omp_structured_block (cp_parser
*parser
, bool *if_p
)
38635 tree stmt
= begin_omp_structured_block ();
38636 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
38638 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
38640 cp_parser_end_omp_structured_block (parser
, save
);
38641 return finish_omp_structured_block (stmt
);
38645 # pragma omp allocate (list) [allocator(allocator)] */
38648 cp_parser_omp_allocate (cp_parser
*parser
, cp_token
*pragma_tok
)
38650 tree allocator
= NULL_TREE
;
38651 location_t loc
= pragma_tok
->location
;
38652 tree nl
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ALLOCATE
, NULL_TREE
);
38654 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38656 matching_parens parens
;
38657 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38658 const char *p
= IDENTIFIER_POINTER (id
);
38659 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38660 cp_lexer_consume_token (parser
->lexer
);
38661 if (strcmp (p
, "allocator") != 0)
38662 error_at (cloc
, "expected %<allocator%>");
38663 else if (parens
.require_open (parser
))
38665 allocator
= cp_parser_assignment_expression (parser
);
38666 if (allocator
== error_mark_node
)
38667 allocator
= NULL_TREE
;
38668 parens
.require_close (parser
);
38671 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38674 for (tree c
= nl
; c
!= NULL_TREE
; c
= OMP_CLAUSE_CHAIN (c
))
38675 OMP_CLAUSE_ALLOCATE_ALLOCATOR (c
) = allocator
;
38677 sorry_at (loc
, "%<#pragma omp allocate%> not yet supported");
38681 # pragma omp atomic new-line
38685 x binop= expr | x++ | ++x | x-- | --x
38687 +, *, -, /, &, ^, |, <<, >>
38689 where x is an lvalue expression with scalar type.
38692 # pragma omp atomic new-line
38695 # pragma omp atomic read new-line
38698 # pragma omp atomic write new-line
38701 # pragma omp atomic update new-line
38704 # pragma omp atomic capture new-line
38707 # pragma omp atomic capture new-line
38715 expression-stmt | x = x binop expr
38717 v = expression-stmt
38719 { v = x; update-stmt; } | { update-stmt; v = x; }
38723 expression-stmt | x = x binop expr | x = expr binop x
38727 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
38729 where x and v are lvalue expressions with scalar type. */
38732 cp_parser_omp_atomic (cp_parser
*parser
, cp_token
*pragma_tok
, bool openacc
)
38734 tree lhs
= NULL_TREE
, rhs
= NULL_TREE
, v
= NULL_TREE
, lhs1
= NULL_TREE
;
38735 tree rhs1
= NULL_TREE
, orig_lhs
;
38736 location_t loc
= pragma_tok
->location
;
38737 enum tree_code code
= ERROR_MARK
, opcode
= NOP_EXPR
;
38738 enum omp_memory_order memory_order
= OMP_MEMORY_ORDER_UNSPECIFIED
;
38739 bool structured_block
= false;
38741 tree clauses
= NULL_TREE
;
38743 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
38745 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
38746 cp_lexer_consume_token (parser
->lexer
);
38750 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
38752 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
38753 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
38754 const char *p
= IDENTIFIER_POINTER (id
);
38755 enum tree_code new_code
= ERROR_MARK
;
38756 enum omp_memory_order new_memory_order
38757 = OMP_MEMORY_ORDER_UNSPECIFIED
;
38759 if (!strcmp (p
, "read"))
38760 new_code
= OMP_ATOMIC_READ
;
38761 else if (!strcmp (p
, "write"))
38762 new_code
= NOP_EXPR
;
38763 else if (!strcmp (p
, "update"))
38764 new_code
= OMP_ATOMIC
;
38765 else if (!strcmp (p
, "capture"))
38766 new_code
= OMP_ATOMIC_CAPTURE_NEW
;
38770 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
38771 "or %<capture%> clause");
38773 else if (!strcmp (p
, "seq_cst"))
38774 new_memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38775 else if (!strcmp (p
, "acq_rel"))
38776 new_memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
38777 else if (!strcmp (p
, "release"))
38778 new_memory_order
= OMP_MEMORY_ORDER_RELEASE
;
38779 else if (!strcmp (p
, "acquire"))
38780 new_memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
38781 else if (!strcmp (p
, "relaxed"))
38782 new_memory_order
= OMP_MEMORY_ORDER_RELAXED
;
38783 else if (!strcmp (p
, "hint"))
38785 cp_lexer_consume_token (parser
->lexer
);
38786 clauses
= cp_parser_omp_clause_hint (parser
, clauses
, cloc
);
38792 error_at (cloc
, "expected %<read%>, %<write%>, %<update%>, "
38793 "%<capture%>, %<seq_cst%>, %<acq_rel%>, "
38794 "%<release%>, %<relaxed%> or %<hint%> clause");
38798 if (new_code
!= ERROR_MARK
)
38800 /* OpenACC permits 'update capture'. */
38802 && code
== OMP_ATOMIC
38803 && new_code
== OMP_ATOMIC_CAPTURE_NEW
)
38805 else if (code
!= ERROR_MARK
)
38806 error_at (cloc
, "too many atomic clauses");
38810 else if (new_memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
38812 if (memory_order
!= OMP_MEMORY_ORDER_UNSPECIFIED
)
38813 error_at (cloc
, "too many memory order clauses");
38815 memory_order
= new_memory_order
;
38817 cp_lexer_consume_token (parser
->lexer
);
38823 cp_parser_require_pragma_eol (parser
, pragma_tok
);
38825 if (code
== ERROR_MARK
)
38828 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
38829 else if (memory_order
== OMP_MEMORY_ORDER_UNSPECIFIED
)
38832 = (enum omp_requires
) (omp_requires_mask
38833 | OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
);
38834 switch ((enum omp_memory_order
)
38835 (omp_requires_mask
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
))
38837 case OMP_MEMORY_ORDER_UNSPECIFIED
:
38838 case OMP_MEMORY_ORDER_RELAXED
:
38839 memory_order
= OMP_MEMORY_ORDER_RELAXED
;
38841 case OMP_MEMORY_ORDER_SEQ_CST
:
38842 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38844 case OMP_MEMORY_ORDER_ACQ_REL
:
38847 case OMP_ATOMIC_READ
:
38848 memory_order
= OMP_MEMORY_ORDER_ACQUIRE
;
38850 case NOP_EXPR
: /* atomic write */
38852 memory_order
= OMP_MEMORY_ORDER_RELEASE
;
38855 memory_order
= OMP_MEMORY_ORDER_ACQ_REL
;
38860 gcc_unreachable ();
38866 case OMP_ATOMIC_READ
:
38867 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
38868 || memory_order
== OMP_MEMORY_ORDER_RELEASE
)
38870 error_at (loc
, "%<#pragma omp atomic read%> incompatible with "
38871 "%<acq_rel%> or %<release%> clauses");
38872 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38875 case NOP_EXPR
: /* atomic write */
38876 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
38877 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
38879 error_at (loc
, "%<#pragma omp atomic write%> incompatible with "
38880 "%<acq_rel%> or %<acquire%> clauses");
38881 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38885 if (memory_order
== OMP_MEMORY_ORDER_ACQ_REL
38886 || memory_order
== OMP_MEMORY_ORDER_ACQUIRE
)
38888 error_at (loc
, "%<#pragma omp atomic update%> incompatible with "
38889 "%<acq_rel%> or %<acquire%> clauses");
38890 memory_order
= OMP_MEMORY_ORDER_SEQ_CST
;
38899 case OMP_ATOMIC_READ
:
38900 case NOP_EXPR
: /* atomic write */
38901 v
= cp_parser_unary_expression (parser
);
38902 if (v
== error_mark_node
)
38904 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38906 if (code
== NOP_EXPR
)
38907 lhs
= cp_parser_expression (parser
);
38909 lhs
= cp_parser_unary_expression (parser
);
38910 if (lhs
== error_mark_node
)
38912 if (code
== NOP_EXPR
)
38914 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
38922 case OMP_ATOMIC_CAPTURE_NEW
:
38923 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
38925 cp_lexer_consume_token (parser
->lexer
);
38926 structured_block
= true;
38930 v
= cp_parser_unary_expression (parser
);
38931 if (v
== error_mark_node
)
38933 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
38941 lhs
= cp_parser_unary_expression (parser
);
38943 switch (TREE_CODE (lhs
))
38948 case POSTINCREMENT_EXPR
:
38949 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
38950 code
= OMP_ATOMIC_CAPTURE_OLD
;
38952 case PREINCREMENT_EXPR
:
38953 lhs
= TREE_OPERAND (lhs
, 0);
38954 opcode
= PLUS_EXPR
;
38955 rhs
= integer_one_node
;
38958 case POSTDECREMENT_EXPR
:
38959 if (code
== OMP_ATOMIC_CAPTURE_NEW
&& !structured_block
)
38960 code
= OMP_ATOMIC_CAPTURE_OLD
;
38962 case PREDECREMENT_EXPR
:
38963 lhs
= TREE_OPERAND (lhs
, 0);
38964 opcode
= MINUS_EXPR
;
38965 rhs
= integer_one_node
;
38968 case COMPOUND_EXPR
:
38969 if (TREE_CODE (TREE_OPERAND (lhs
, 0)) == SAVE_EXPR
38970 && TREE_CODE (TREE_OPERAND (lhs
, 1)) == COMPOUND_EXPR
38971 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0)) == MODIFY_EXPR
38972 && TREE_OPERAND (TREE_OPERAND (lhs
, 1), 1) == TREE_OPERAND (lhs
, 0)
38973 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
38974 (TREE_OPERAND (lhs
, 1), 0), 0)))
38976 /* Undo effects of boolean_increment for post {in,de}crement. */
38977 lhs
= TREE_OPERAND (TREE_OPERAND (lhs
, 1), 0);
38980 if (TREE_CODE (lhs
) == MODIFY_EXPR
38981 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs
, 0))) == BOOLEAN_TYPE
)
38983 /* Undo effects of boolean_increment. */
38984 if (integer_onep (TREE_OPERAND (lhs
, 1)))
38986 /* This is pre or post increment. */
38987 rhs
= TREE_OPERAND (lhs
, 1);
38988 lhs
= TREE_OPERAND (lhs
, 0);
38990 if (code
== OMP_ATOMIC_CAPTURE_NEW
38991 && !structured_block
38992 && TREE_CODE (orig_lhs
) == COMPOUND_EXPR
)
38993 code
= OMP_ATOMIC_CAPTURE_OLD
;
38999 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
39002 opcode
= MULT_EXPR
;
39005 opcode
= TRUNC_DIV_EXPR
;
39008 opcode
= PLUS_EXPR
;
39011 opcode
= MINUS_EXPR
;
39013 case CPP_LSHIFT_EQ
:
39014 opcode
= LSHIFT_EXPR
;
39016 case CPP_RSHIFT_EQ
:
39017 opcode
= RSHIFT_EXPR
;
39020 opcode
= BIT_AND_EXPR
;
39023 opcode
= BIT_IOR_EXPR
;
39026 opcode
= BIT_XOR_EXPR
;
39029 enum cp_parser_prec oprec
;
39031 cp_lexer_consume_token (parser
->lexer
);
39032 cp_parser_parse_tentatively (parser
);
39033 rhs1
= cp_parser_simple_cast_expression (parser
);
39034 if (rhs1
== error_mark_node
)
39036 cp_parser_abort_tentative_parse (parser
);
39037 cp_parser_simple_cast_expression (parser
);
39040 token
= cp_lexer_peek_token (parser
->lexer
);
39041 if (token
->type
!= CPP_SEMICOLON
&& !cp_tree_equal (lhs
, rhs1
))
39043 cp_parser_abort_tentative_parse (parser
);
39044 cp_parser_parse_tentatively (parser
);
39045 rhs
= cp_parser_binary_expression (parser
, false, true,
39046 PREC_NOT_OPERATOR
, NULL
);
39047 if (rhs
== error_mark_node
)
39049 cp_parser_abort_tentative_parse (parser
);
39050 cp_parser_binary_expression (parser
, false, true,
39051 PREC_NOT_OPERATOR
, NULL
);
39054 switch (TREE_CODE (rhs
))
39057 case TRUNC_DIV_EXPR
:
39066 if (cp_tree_equal (lhs
, TREE_OPERAND (rhs
, 1)))
39068 if (cp_parser_parse_definitely (parser
))
39070 opcode
= TREE_CODE (rhs
);
39071 rhs1
= TREE_OPERAND (rhs
, 0);
39072 rhs
= TREE_OPERAND (rhs
, 1);
39082 cp_parser_abort_tentative_parse (parser
);
39083 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_OLD
)
39085 rhs
= cp_parser_expression (parser
);
39086 if (rhs
== error_mark_node
)
39092 cp_parser_error (parser
,
39093 "invalid form of %<#pragma omp atomic%>");
39096 if (!cp_parser_parse_definitely (parser
))
39098 switch (token
->type
)
39100 case CPP_SEMICOLON
:
39101 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
39103 code
= OMP_ATOMIC_CAPTURE_OLD
;
39108 cp_lexer_consume_token (parser
->lexer
);
39111 else if (structured_block
)
39118 cp_parser_error (parser
,
39119 "invalid form of %<#pragma omp atomic%>");
39122 opcode
= MULT_EXPR
;
39125 opcode
= TRUNC_DIV_EXPR
;
39128 opcode
= PLUS_EXPR
;
39131 opcode
= MINUS_EXPR
;
39134 opcode
= LSHIFT_EXPR
;
39137 opcode
= RSHIFT_EXPR
;
39140 opcode
= BIT_AND_EXPR
;
39143 opcode
= BIT_IOR_EXPR
;
39146 opcode
= BIT_XOR_EXPR
;
39149 cp_parser_error (parser
,
39150 "invalid operator for %<#pragma omp atomic%>");
39153 oprec
= TOKEN_PRECEDENCE (token
);
39154 gcc_assert (oprec
!= PREC_NOT_OPERATOR
);
39155 if (commutative_tree_code (opcode
))
39156 oprec
= (enum cp_parser_prec
) (oprec
- 1);
39157 cp_lexer_consume_token (parser
->lexer
);
39158 rhs
= cp_parser_binary_expression (parser
, false, false,
39160 if (rhs
== error_mark_node
)
39165 cp_parser_error (parser
,
39166 "invalid operator for %<#pragma omp atomic%>");
39169 cp_lexer_consume_token (parser
->lexer
);
39171 rhs
= cp_parser_expression (parser
);
39172 if (rhs
== error_mark_node
)
39177 if (structured_block
&& code
== OMP_ATOMIC_CAPTURE_NEW
)
39179 if (!cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
))
39181 v
= cp_parser_unary_expression (parser
);
39182 if (v
== error_mark_node
)
39184 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
39186 lhs1
= cp_parser_unary_expression (parser
);
39187 if (lhs1
== error_mark_node
)
39190 if (structured_block
)
39192 cp_parser_consume_semicolon_at_end_of_statement (parser
);
39193 cp_parser_require (parser
, CPP_CLOSE_BRACE
, RT_CLOSE_BRACE
);
39196 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
39197 finish_omp_atomic (pragma_tok
->location
, code
, opcode
, lhs
, rhs
, v
, lhs1
,
39198 rhs1
, clauses
, memory_order
);
39199 if (!structured_block
)
39200 cp_parser_consume_semicolon_at_end_of_statement (parser
);
39204 cp_parser_skip_to_end_of_block_or_statement (parser
);
39205 if (structured_block
)
39207 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
39208 cp_lexer_consume_token (parser
->lexer
);
39209 else if (code
== OMP_ATOMIC_CAPTURE_NEW
)
39211 cp_parser_skip_to_end_of_block_or_statement (parser
);
39212 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
39213 cp_lexer_consume_token (parser
->lexer
);
39220 # pragma omp barrier new-line */
39223 cp_parser_omp_barrier (cp_parser
*parser
, cp_token
*pragma_tok
)
39225 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39226 finish_omp_barrier ();
39230 # pragma omp critical [(name)] new-line
39234 # pragma omp critical [(name) [hint(expression)]] new-line
39235 structured-block */
39237 #define OMP_CRITICAL_CLAUSE_MASK \
39238 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
39241 cp_parser_omp_critical (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
39243 tree stmt
, name
= NULL_TREE
, clauses
= NULL_TREE
;
39245 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39247 matching_parens parens
;
39248 parens
.consume_open (parser
);
39250 name
= cp_parser_identifier (parser
);
39252 if (name
== error_mark_node
39253 || !parens
.require_close (parser
))
39254 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39255 /*or_comma=*/false,
39256 /*consume_paren=*/true);
39257 if (name
== error_mark_node
)
39260 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
)
39261 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_NAME
))
39262 cp_lexer_consume_token (parser
->lexer
);
39265 clauses
= cp_parser_omp_all_clauses (parser
, OMP_CRITICAL_CLAUSE_MASK
,
39266 "#pragma omp critical", pragma_tok
);
39268 stmt
= cp_parser_omp_structured_block (parser
, if_p
);
39269 return c_finish_omp_critical (input_location
, stmt
, name
, clauses
);
39273 # pragma omp depobj ( depobj ) depobj-clause new-line
39276 depend (dependence-type : locator)
39278 update (dependence-type)
39287 cp_parser_omp_depobj (cp_parser
*parser
, cp_token
*pragma_tok
)
39289 location_t loc
= pragma_tok
->location
;
39290 matching_parens parens
;
39291 if (!parens
.require_open (parser
))
39293 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
39297 tree depobj
= cp_parser_assignment_expression (parser
);
39299 if (!parens
.require_close (parser
))
39300 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
39301 /*or_comma=*/false,
39302 /*consume_paren=*/true);
39304 tree clause
= NULL_TREE
;
39305 enum omp_clause_depend_kind kind
= OMP_CLAUSE_DEPEND_SOURCE
;
39306 location_t c_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
39307 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39309 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39310 const char *p
= IDENTIFIER_POINTER (id
);
39312 cp_lexer_consume_token (parser
->lexer
);
39313 if (!strcmp ("depend", p
))
39315 /* Don't create location wrapper nodes within the depend clause. */
39316 auto_suppress_location_wrappers sentinel
;
39317 clause
= cp_parser_omp_clause_depend (parser
, NULL_TREE
, c_loc
);
39319 clause
= finish_omp_clauses (clause
, C_ORT_OMP
);
39321 clause
= error_mark_node
;
39323 else if (!strcmp ("destroy", p
))
39324 kind
= OMP_CLAUSE_DEPEND_LAST
;
39325 else if (!strcmp ("update", p
))
39327 matching_parens c_parens
;
39328 if (c_parens
.require_open (parser
))
39331 = cp_lexer_peek_token (parser
->lexer
)->location
;
39332 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39334 tree id2
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39335 const char *p2
= IDENTIFIER_POINTER (id2
);
39337 cp_lexer_consume_token (parser
->lexer
);
39338 if (!strcmp ("in", p2
))
39339 kind
= OMP_CLAUSE_DEPEND_IN
;
39340 else if (!strcmp ("out", p2
))
39341 kind
= OMP_CLAUSE_DEPEND_OUT
;
39342 else if (!strcmp ("inout", p2
))
39343 kind
= OMP_CLAUSE_DEPEND_INOUT
;
39344 else if (!strcmp ("mutexinoutset", p2
))
39345 kind
= OMP_CLAUSE_DEPEND_MUTEXINOUTSET
;
39347 if (kind
== OMP_CLAUSE_DEPEND_SOURCE
)
39349 clause
= error_mark_node
;
39350 error_at (c2_loc
, "expected %<in%>, %<out%>, %<inout%> or "
39351 "%<mutexinoutset%>");
39353 if (!c_parens
.require_close (parser
))
39354 cp_parser_skip_to_closing_parenthesis (parser
,
39355 /*recovering=*/true,
39356 /*or_comma=*/false,
39357 /*consume_paren=*/true);
39360 clause
= error_mark_node
;
39363 if (!clause
&& kind
== OMP_CLAUSE_DEPEND_SOURCE
)
39365 clause
= error_mark_node
;
39366 error_at (c_loc
, "expected %<depend%>, %<destroy%> or %<update%> clause");
39368 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39370 finish_omp_depobj (loc
, depobj
, kind
, clause
);
39375 # pragma omp flush flush-vars[opt] new-line
39381 # pragma omp flush memory-order-clause new-line */
39384 cp_parser_omp_flush (cp_parser
*parser
, cp_token
*pragma_tok
)
39386 enum memmodel mo
= MEMMODEL_LAST
;
39387 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39389 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39390 const char *p
= IDENTIFIER_POINTER (id
);
39391 if (!strcmp (p
, "acq_rel"))
39392 mo
= MEMMODEL_ACQ_REL
;
39393 else if (!strcmp (p
, "release"))
39394 mo
= MEMMODEL_RELEASE
;
39395 else if (!strcmp (p
, "acquire"))
39396 mo
= MEMMODEL_ACQUIRE
;
39398 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39399 "expected %<acq_rel%>, %<release%> or %<acquire%>");
39400 cp_lexer_consume_token (parser
->lexer
);
39402 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
39404 if (mo
!= MEMMODEL_LAST
)
39405 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
39406 "%<flush%> list specified together with memory order "
39408 (void) cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
39410 cp_parser_require_pragma_eol (parser
, pragma_tok
);
39412 finish_omp_flush (mo
);
39415 /* Helper function, to parse omp for increment expression. */
39418 cp_parser_omp_for_cond (cp_parser
*parser
, tree decl
, enum tree_code code
)
39420 tree cond
= cp_parser_binary_expression (parser
, false, true,
39421 PREC_NOT_OPERATOR
, NULL
);
39422 if (cond
== error_mark_node
39423 || cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
39425 cp_parser_skip_to_end_of_statement (parser
);
39426 return error_mark_node
;
39429 switch (TREE_CODE (cond
))
39437 if (code
!= OACC_LOOP
)
39439 gcc_fallthrough ();
39441 return error_mark_node
;
39444 /* If decl is an iterator, preserve LHS and RHS of the relational
39445 expr until finish_omp_for. */
39447 && (type_dependent_expression_p (decl
)
39448 || CLASS_TYPE_P (TREE_TYPE (decl
))))
39451 return build_x_binary_op (cp_expr_loc_or_input_loc (cond
),
39453 TREE_OPERAND (cond
, 0), ERROR_MARK
,
39454 TREE_OPERAND (cond
, 1), ERROR_MARK
,
39455 /*overload=*/NULL
, tf_warning_or_error
);
39458 /* Helper function, to parse omp for increment expression. */
39461 cp_parser_omp_for_incr (cp_parser
*parser
, tree decl
)
39463 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
39469 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
39471 op
= (token
->type
== CPP_PLUS_PLUS
39472 ? PREINCREMENT_EXPR
: PREDECREMENT_EXPR
);
39473 cp_lexer_consume_token (parser
->lexer
);
39474 lhs
= cp_parser_simple_cast_expression (parser
);
39476 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
39477 return error_mark_node
;
39478 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
39481 lhs
= cp_parser_primary_expression (parser
, false, false, false, &idk
);
39483 && (!processing_template_decl
|| !cp_tree_equal (lhs
, decl
)))
39484 return error_mark_node
;
39486 token
= cp_lexer_peek_token (parser
->lexer
);
39487 if (token
->type
== CPP_PLUS_PLUS
|| token
->type
== CPP_MINUS_MINUS
)
39489 op
= (token
->type
== CPP_PLUS_PLUS
39490 ? POSTINCREMENT_EXPR
: POSTDECREMENT_EXPR
);
39491 cp_lexer_consume_token (parser
->lexer
);
39492 return build2 (op
, TREE_TYPE (decl
), decl
, NULL_TREE
);
39495 op
= cp_parser_assignment_operator_opt (parser
);
39496 if (op
== ERROR_MARK
)
39497 return error_mark_node
;
39499 if (op
!= NOP_EXPR
)
39501 rhs
= cp_parser_assignment_expression (parser
);
39502 rhs
= build2 (op
, TREE_TYPE (decl
), decl
, rhs
);
39503 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
39506 lhs
= cp_parser_binary_expression (parser
, false, false,
39507 PREC_ADDITIVE_EXPRESSION
, NULL
);
39508 token
= cp_lexer_peek_token (parser
->lexer
);
39509 decl_first
= (lhs
== decl
39510 || (processing_template_decl
&& cp_tree_equal (lhs
, decl
)));
39513 if (token
->type
!= CPP_PLUS
39514 && token
->type
!= CPP_MINUS
)
39515 return error_mark_node
;
39519 op
= token
->type
== CPP_PLUS
? PLUS_EXPR
: MINUS_EXPR
;
39520 cp_lexer_consume_token (parser
->lexer
);
39521 rhs
= cp_parser_binary_expression (parser
, false, false,
39522 PREC_ADDITIVE_EXPRESSION
, NULL
);
39523 token
= cp_lexer_peek_token (parser
->lexer
);
39524 if (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
|| decl_first
)
39526 if (lhs
== NULL_TREE
)
39528 if (op
== PLUS_EXPR
)
39531 lhs
= build_x_unary_op (input_location
, NEGATE_EXPR
, rhs
,
39532 tf_warning_or_error
);
39535 lhs
= build_x_binary_op (input_location
, op
, lhs
, ERROR_MARK
, rhs
,
39536 ERROR_MARK
, NULL
, tf_warning_or_error
);
39539 while (token
->type
== CPP_PLUS
|| token
->type
== CPP_MINUS
);
39544 && (!processing_template_decl
|| !cp_tree_equal (rhs
, decl
)))
39545 || op
== MINUS_EXPR
)
39546 return error_mark_node
;
39547 rhs
= build2 (op
, TREE_TYPE (decl
), lhs
, decl
);
39550 rhs
= build2 (PLUS_EXPR
, TREE_TYPE (decl
), decl
, lhs
);
39552 return build2 (MODIFY_EXPR
, TREE_TYPE (decl
), decl
, rhs
);
39555 /* Parse the initialization statement of an OpenMP for loop.
39557 Return true if the resulting construct should have an
39558 OMP_CLAUSE_PRIVATE added to it. */
39561 cp_parser_omp_for_loop_init (cp_parser
*parser
,
39562 tree
&this_pre_body
,
39563 releasing_vec
&for_block
,
39569 if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
39572 tree add_private_clause
= NULL_TREE
;
39574 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
39578 integer-type var = lb
39579 random-access-iterator-type var = lb
39580 pointer-type var = lb
39582 cp_decl_specifier_seq type_specifiers
;
39584 /* First, try to parse as an initialized declaration. See
39585 cp_parser_condition, from whence the bulk of this is copied. */
39587 cp_parser_parse_tentatively (parser
);
39588 cp_parser_type_specifier_seq (parser
, CP_PARSER_FLAGS_NONE
,
39589 /*is_declaration=*/true,
39590 /*is_trailing_return=*/false,
39592 if (cp_parser_parse_definitely (parser
))
39594 /* If parsing a type specifier seq succeeded, then this
39595 MUST be a initialized declaration. */
39596 tree asm_specification
, attributes
;
39597 cp_declarator
*declarator
;
39599 declarator
= cp_parser_declarator (parser
,
39600 CP_PARSER_DECLARATOR_NAMED
,
39601 CP_PARSER_FLAGS_NONE
,
39602 /*ctor_dtor_or_conv_p=*/NULL
,
39603 /*parenthesized_p=*/NULL
,
39604 /*member_p=*/false,
39605 /*friend_p=*/false,
39606 /*static_p=*/false);
39607 attributes
= cp_parser_attributes_opt (parser
);
39608 asm_specification
= cp_parser_asm_specification_opt (parser
);
39610 if (declarator
== cp_error_declarator
)
39611 cp_parser_skip_to_end_of_statement (parser
);
39615 tree pushed_scope
, auto_node
;
39617 decl
= start_decl (declarator
, &type_specifiers
,
39618 SD_INITIALIZED
, attributes
,
39619 /*prefix_attributes=*/NULL_TREE
,
39622 auto_node
= type_uses_auto (TREE_TYPE (decl
));
39623 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_EQ
))
39625 if (cp_lexer_next_token_is (parser
->lexer
,
39627 error ("parenthesized initialization is not allowed in "
39628 "OpenMP %<for%> loop");
39630 /* Trigger an error. */
39631 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
39633 init
= error_mark_node
;
39634 cp_parser_skip_to_end_of_statement (parser
);
39636 else if (CLASS_TYPE_P (TREE_TYPE (decl
))
39637 || type_dependent_expression_p (decl
)
39640 bool is_direct_init
, is_non_constant_init
;
39642 init
= cp_parser_initializer (parser
,
39644 &is_non_constant_init
);
39649 = do_auto_deduction (TREE_TYPE (decl
), init
,
39652 if (!CLASS_TYPE_P (TREE_TYPE (decl
))
39653 && !type_dependent_expression_p (decl
))
39657 cp_finish_decl (decl
, init
, !is_non_constant_init
,
39659 LOOKUP_ONLYCONVERTING
);
39661 if (CLASS_TYPE_P (TREE_TYPE (decl
)))
39663 vec_safe_push (for_block
, this_pre_body
);
39668 init
= pop_stmt_list (this_pre_body
);
39669 if (init
&& TREE_CODE (init
) == STATEMENT_LIST
)
39671 tree_stmt_iterator i
= tsi_start (init
);
39672 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
39673 while (!tsi_end_p (i
))
39675 tree t
= tsi_stmt (i
);
39676 if (TREE_CODE (t
) == DECL_EXPR
39677 && TREE_CODE (DECL_EXPR_DECL (t
)) == TYPE_DECL
)
39680 vec_safe_push (for_block
, t
);
39685 if (tsi_one_before_end_p (i
))
39687 tree t
= tsi_stmt (i
);
39689 free_stmt_list (init
);
39694 this_pre_body
= NULL_TREE
;
39699 cp_lexer_consume_token (parser
->lexer
);
39700 init
= cp_parser_assignment_expression (parser
);
39703 if (TYPE_REF_P (TREE_TYPE (decl
)))
39704 init
= error_mark_node
;
39706 cp_finish_decl (decl
, NULL_TREE
,
39707 /*init_const_expr_p=*/false,
39709 LOOKUP_ONLYCONVERTING
);
39713 pop_scope (pushed_scope
);
39719 /* If parsing a type specifier sequence failed, then
39720 this MUST be a simple expression. */
39721 cp_parser_parse_tentatively (parser
);
39722 decl
= cp_parser_primary_expression (parser
, false, false,
39724 cp_token
*last_tok
= cp_lexer_peek_token (parser
->lexer
);
39725 if (!cp_parser_error_occurred (parser
)
39727 && (TREE_CODE (decl
) == COMPONENT_REF
39728 || (TREE_CODE (decl
) == SCOPE_REF
&& TREE_TYPE (decl
))))
39730 cp_parser_abort_tentative_parse (parser
);
39731 cp_parser_parse_tentatively (parser
);
39732 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
39733 tree name
= cp_parser_id_expression (parser
, /*template_p=*/false,
39734 /*check_dependency_p=*/true,
39735 /*template_p=*/NULL
,
39736 /*declarator_p=*/false,
39737 /*optional_p=*/false);
39738 if (name
!= error_mark_node
39739 && last_tok
== cp_lexer_peek_token (parser
->lexer
))
39741 decl
= cp_parser_lookup_name_simple (parser
, name
,
39743 if (TREE_CODE (decl
) == FIELD_DECL
)
39744 add_private_clause
= omp_privatize_field (decl
, false);
39746 cp_parser_abort_tentative_parse (parser
);
39747 cp_parser_parse_tentatively (parser
);
39748 decl
= cp_parser_primary_expression (parser
, false, false,
39751 if (!cp_parser_error_occurred (parser
)
39754 && CLASS_TYPE_P (TREE_TYPE (decl
)))
39758 cp_parser_parse_definitely (parser
);
39759 cp_parser_require (parser
, CPP_EQ
, RT_EQ
);
39760 rhs
= cp_parser_assignment_expression (parser
);
39762 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs
),
39765 tf_warning_or_error
));
39766 if (!add_private_clause
)
39767 add_private_clause
= decl
;
39772 cp_parser_abort_tentative_parse (parser
);
39773 init
= cp_parser_expression (parser
);
39776 if (TREE_CODE (init
) == MODIFY_EXPR
39777 || TREE_CODE (init
) == MODOP_EXPR
)
39778 real_decl
= TREE_OPERAND (init
, 0);
39782 return add_private_clause
;
39785 /* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
39788 cp_convert_omp_range_for (tree
&this_pre_body
, vec
<tree
, va_gc
> *for_block
,
39789 tree
&decl
, tree
&orig_decl
, tree
&init
,
39790 tree
&orig_init
, tree
&cond
, tree
&incr
)
39792 tree begin
, end
, range_temp_decl
= NULL_TREE
;
39793 tree iter_type
, begin_expr
, end_expr
;
39795 if (processing_template_decl
)
39797 if (check_for_bare_parameter_packs (init
))
39798 init
= error_mark_node
;
39799 if (!type_dependent_expression_p (init
)
39800 /* do_auto_deduction doesn't mess with template init-lists. */
39801 && !BRACE_ENCLOSED_INITIALIZER_P (init
))
39804 if (decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (decl
))
39806 tree v
= DECL_VALUE_EXPR (decl
);
39807 if (TREE_CODE (v
) == ARRAY_REF
39808 && VAR_P (TREE_OPERAND (v
, 0))
39809 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
39810 d
= TREE_OPERAND (v
, 0);
39812 do_range_for_auto_deduction (d
, init
);
39814 cond
= global_namespace
;
39818 this_pre_body
= pop_stmt_list (this_pre_body
);
39822 init
= mark_lvalue_use (init
);
39824 if (decl
== error_mark_node
|| init
== error_mark_node
)
39825 /* If an error happened previously do nothing or else a lot of
39826 unhelpful errors would be issued. */
39827 begin_expr
= end_expr
= iter_type
= error_mark_node
;
39833 && array_of_runtime_bound_p (TREE_TYPE (init
)))
39834 /* Can't bind a reference to an array of runtime bound. */
39838 range_temp
= build_range_temp (init
);
39839 DECL_NAME (range_temp
) = NULL_TREE
;
39840 pushdecl (range_temp
);
39841 cp_finish_decl (range_temp
, init
,
39842 /*is_constant_init*/false, NULL_TREE
,
39843 LOOKUP_ONLYCONVERTING
);
39844 range_temp_decl
= range_temp
;
39845 range_temp
= convert_from_reference (range_temp
);
39847 iter_type
= cp_parser_perform_range_for_lookup (range_temp
,
39848 &begin_expr
, &end_expr
);
39851 tree end_iter_type
= iter_type
;
39852 if (cxx_dialect
>= cxx17
)
39853 end_iter_type
= cv_unqualified (TREE_TYPE (end_expr
));
39854 end
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, end_iter_type
);
39855 TREE_USED (end
) = 1;
39856 DECL_ARTIFICIAL (end
) = 1;
39858 cp_finish_decl (end
, end_expr
,
39859 /*is_constant_init*/false, NULL_TREE
,
39860 LOOKUP_ONLYCONVERTING
);
39862 /* The new for initialization statement. */
39863 begin
= build_decl (input_location
, VAR_DECL
, NULL_TREE
, iter_type
);
39864 TREE_USED (begin
) = 1;
39865 DECL_ARTIFICIAL (begin
) = 1;
39868 if (CLASS_TYPE_P (iter_type
))
39873 begin_expr
= NULL_TREE
;
39875 cp_finish_decl (begin
, begin_expr
,
39876 /*is_constant_init*/false, NULL_TREE
,
39877 LOOKUP_ONLYCONVERTING
);
39879 /* The new for condition. */
39880 if (CLASS_TYPE_P (iter_type
))
39881 cond
= build2 (NE_EXPR
, boolean_type_node
, begin
, end
);
39883 cond
= build_x_binary_op (input_location
, NE_EXPR
,
39886 NULL
, tf_warning_or_error
);
39888 /* The new increment expression. */
39889 if (CLASS_TYPE_P (iter_type
))
39890 incr
= build2 (PREINCREMENT_EXPR
, iter_type
, begin
, NULL_TREE
);
39892 incr
= finish_unary_op_expr (input_location
,
39893 PREINCREMENT_EXPR
, begin
,
39894 tf_warning_or_error
);
39900 vec_safe_push (for_block
, this_pre_body
);
39901 this_pre_body
= NULL_TREE
;
39904 tree decomp_first_name
= NULL_TREE
;
39905 unsigned decomp_cnt
= 0;
39906 if (orig_decl
!= error_mark_node
&& DECL_HAS_VALUE_EXPR_P (orig_decl
))
39908 tree v
= DECL_VALUE_EXPR (orig_decl
);
39909 if (TREE_CODE (v
) == ARRAY_REF
39910 && VAR_P (TREE_OPERAND (v
, 0))
39911 && DECL_DECOMPOSITION_P (TREE_OPERAND (v
, 0)))
39913 tree d
= orig_decl
;
39914 orig_decl
= TREE_OPERAND (v
, 0);
39915 decomp_cnt
= tree_to_uhwi (TREE_OPERAND (v
, 1)) + 1;
39916 decomp_first_name
= d
;
39920 tree auto_node
= type_uses_auto (TREE_TYPE (orig_decl
));
39923 tree t
= build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
39925 if (!error_operand_p (t
))
39926 TREE_TYPE (orig_decl
) = do_auto_deduction (TREE_TYPE (orig_decl
),
39930 tree v
= make_tree_vec (decomp_cnt
+ 3);
39931 TREE_VEC_ELT (v
, 0) = range_temp_decl
;
39932 TREE_VEC_ELT (v
, 1) = end
;
39933 TREE_VEC_ELT (v
, 2) = orig_decl
;
39934 for (unsigned i
= 0; i
< decomp_cnt
; i
++)
39936 TREE_VEC_ELT (v
, i
+ 3) = decomp_first_name
;
39937 decomp_first_name
= DECL_CHAIN (decomp_first_name
);
39939 orig_decl
= tree_cons (NULL_TREE
, NULL_TREE
, v
);
39942 /* Helper for cp_parser_omp_for_loop, finalize part of range for
39943 inside of the collapsed body. */
39946 cp_finish_omp_range_for (tree orig
, tree begin
)
39948 gcc_assert (TREE_CODE (orig
) == TREE_LIST
39949 && TREE_CODE (TREE_CHAIN (orig
)) == TREE_VEC
);
39950 tree decl
= TREE_VEC_ELT (TREE_CHAIN (orig
), 2);
39951 tree decomp_first_name
= NULL_TREE
;
39952 unsigned int decomp_cnt
= 0;
39954 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
39956 decomp_first_name
= TREE_VEC_ELT (TREE_CHAIN (orig
), 3);
39957 decomp_cnt
= TREE_VEC_LENGTH (TREE_CHAIN (orig
)) - 3;
39958 cp_maybe_mangle_decomp (decl
, decomp_first_name
, decomp_cnt
);
39961 /* The declaration is initialized with *__begin inside the loop body. */
39962 cp_finish_decl (decl
,
39963 build_x_indirect_ref (input_location
, begin
, RO_UNARY_STAR
,
39964 tf_warning_or_error
),
39965 /*is_constant_init*/false, NULL_TREE
,
39966 LOOKUP_ONLYCONVERTING
);
39967 if (VAR_P (decl
) && DECL_DECOMPOSITION_P (decl
))
39968 cp_finish_decomp (decl
, decomp_first_name
, decomp_cnt
);
39974 { structured-block scan-directive structured-block } */
39977 cp_parser_omp_scan_loop_body (cp_parser
*parser
)
39979 tree substmt
, clauses
= NULL_TREE
;
39981 matching_braces braces
;
39982 if (!braces
.require_open (parser
))
39985 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
39986 substmt
= build2 (OMP_SCAN
, void_type_node
, substmt
, NULL_TREE
);
39987 add_stmt (substmt
);
39989 cp_token
*tok
= cp_lexer_peek_token (parser
->lexer
);
39990 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SCAN
)
39992 enum omp_clause_code clause
= OMP_CLAUSE_ERROR
;
39994 cp_lexer_consume_token (parser
->lexer
);
39996 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
39998 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
39999 const char *p
= IDENTIFIER_POINTER (id
);
40000 if (strcmp (p
, "inclusive") == 0)
40001 clause
= OMP_CLAUSE_INCLUSIVE
;
40002 else if (strcmp (p
, "exclusive") == 0)
40003 clause
= OMP_CLAUSE_EXCLUSIVE
;
40005 if (clause
!= OMP_CLAUSE_ERROR
)
40007 cp_lexer_consume_token (parser
->lexer
);
40008 clauses
= cp_parser_omp_var_list (parser
, clause
, NULL_TREE
);
40011 cp_parser_error (parser
, "expected %<inclusive%> or "
40012 "%<exclusive%> clause");
40014 cp_parser_require_pragma_eol (parser
, tok
);
40017 error ("expected %<#pragma omp scan%>");
40019 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
40020 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
40021 substmt
= build2_loc (tok
->location
, OMP_SCAN
, void_type_node
, substmt
,
40023 add_stmt (substmt
);
40025 braces
.require_close (parser
);
40028 /* Parse the restricted form of the for statement allowed by OpenMP. */
40031 cp_parser_omp_for_loop (cp_parser
*parser
, enum tree_code code
, tree clauses
,
40032 tree
*cclauses
, bool *if_p
)
40034 tree init
, orig_init
, cond
, incr
, body
, decl
, pre_body
= NULL_TREE
, ret
;
40036 tree real_decl
, initv
, condv
, incrv
, declv
, orig_declv
;
40037 tree this_pre_body
, cl
, ordered_cl
= NULL_TREE
;
40038 location_t loc_first
;
40039 bool collapse_err
= false;
40040 int i
, collapse
= 1, ordered
= 0, count
, nbraces
= 0;
40041 releasing_vec for_block
;
40042 auto_vec
<tree
, 4> orig_inits
;
40043 bool tiling
= false;
40044 bool inscan
= false;
40046 for (cl
= clauses
; cl
; cl
= OMP_CLAUSE_CHAIN (cl
))
40047 if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_COLLAPSE
)
40048 collapse
= tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl
));
40049 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_TILE
)
40052 collapse
= list_length (OMP_CLAUSE_TILE_LIST (cl
));
40054 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_ORDERED
40055 && OMP_CLAUSE_ORDERED_EXPR (cl
))
40058 ordered
= tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl
));
40060 else if (OMP_CLAUSE_CODE (cl
) == OMP_CLAUSE_REDUCTION
40061 && OMP_CLAUSE_REDUCTION_INSCAN (cl
)
40062 && (code
== OMP_SIMD
|| code
== OMP_FOR
))
40065 if (ordered
&& ordered
< collapse
)
40067 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
40068 "%<ordered%> clause parameter is less than %<collapse%>");
40069 OMP_CLAUSE_ORDERED_EXPR (ordered_cl
)
40070 = build_int_cst (NULL_TREE
, collapse
);
40071 ordered
= collapse
;
40075 for (tree
*pc
= &clauses
; *pc
; )
40076 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_LINEAR
)
40078 error_at (OMP_CLAUSE_LOCATION (*pc
),
40079 "%<linear%> clause may not be specified together "
40080 "with %<ordered%> clause with a parameter");
40081 *pc
= OMP_CLAUSE_CHAIN (*pc
);
40084 pc
= &OMP_CLAUSE_CHAIN (*pc
);
40087 gcc_assert (tiling
|| (collapse
>= 1 && ordered
>= 0));
40088 count
= ordered
? ordered
: collapse
;
40090 declv
= make_tree_vec (count
);
40091 initv
= make_tree_vec (count
);
40092 condv
= make_tree_vec (count
);
40093 incrv
= make_tree_vec (count
);
40094 orig_declv
= NULL_TREE
;
40096 loc_first
= cp_lexer_peek_token (parser
->lexer
)->location
;
40098 for (i
= 0; i
< count
; i
++)
40100 int bracecount
= 0;
40101 tree add_private_clause
= NULL_TREE
;
40104 if (!cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
40107 cp_parser_error (parser
, "for statement expected");
40110 loc
= cp_lexer_consume_token (parser
->lexer
)->location
;
40112 /* Don't create location wrapper nodes within an OpenMP "for"
40114 auto_suppress_location_wrappers sentinel
;
40116 matching_parens parens
;
40117 if (!parens
.require_open (parser
))
40120 init
= orig_init
= decl
= real_decl
= orig_decl
= NULL_TREE
;
40121 this_pre_body
= push_stmt_list ();
40123 if (code
!= OACC_LOOP
&& cxx_dialect
>= cxx11
)
40125 /* Save tokens so that we can put them back. */
40126 cp_lexer_save_tokens (parser
->lexer
);
40128 /* Look for ':' that is not nested in () or {}. */
40130 = (cp_parser_skip_to_closing_parenthesis_1 (parser
,
40131 /*recovering=*/false,
40136 /* Roll back the tokens we skipped. */
40137 cp_lexer_rollback_tokens (parser
->lexer
);
40141 bool saved_colon_corrects_to_scope_p
40142 = parser
->colon_corrects_to_scope_p
;
40144 /* A colon is used in range-based for. */
40145 parser
->colon_corrects_to_scope_p
= false;
40147 /* Parse the declaration. */
40148 cp_parser_simple_declaration (parser
,
40149 /*function_definition_allowed_p=*/
40151 parser
->colon_corrects_to_scope_p
40152 = saved_colon_corrects_to_scope_p
;
40154 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
40156 init
= cp_parser_range_for (parser
, NULL_TREE
, NULL_TREE
, decl
,
40159 cp_convert_omp_range_for (this_pre_body
, for_block
, decl
,
40160 orig_decl
, init
, orig_init
,
40167 pre_body
= push_stmt_list ();
40169 add_stmt (this_pre_body
);
40170 pre_body
= pop_stmt_list (pre_body
);
40173 pre_body
= this_pre_body
;
40177 error_at (OMP_CLAUSE_LOCATION (ordered_cl
),
40178 "%<ordered%> clause with parameter on "
40179 "range-based %<for%> loop");
40181 goto parse_close_paren
;
40186 = cp_parser_omp_for_loop_init (parser
, this_pre_body
, for_block
,
40187 init
, orig_init
, decl
, real_decl
);
40189 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40192 this_pre_body
= pop_stmt_list (this_pre_body
);
40196 pre_body
= push_stmt_list ();
40198 add_stmt (this_pre_body
);
40199 pre_body
= pop_stmt_list (pre_body
);
40202 pre_body
= this_pre_body
;
40207 if (cclauses
!= NULL
40208 && cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
] != NULL
40209 && real_decl
!= NULL_TREE
40210 && code
!= OMP_LOOP
)
40213 for (c
= &cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
]; *c
; )
40214 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_FIRSTPRIVATE
40215 && OMP_CLAUSE_DECL (*c
) == real_decl
)
40217 error_at (loc
, "iteration variable %qD"
40218 " should not be firstprivate", real_decl
);
40219 *c
= OMP_CLAUSE_CHAIN (*c
);
40221 else if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_LASTPRIVATE
40222 && OMP_CLAUSE_DECL (*c
) == real_decl
)
40224 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
40226 *c
= OMP_CLAUSE_CHAIN (*c
);
40227 if (code
== OMP_SIMD
)
40229 OMP_CLAUSE_CHAIN (l
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
40230 cclauses
[C_OMP_CLAUSE_SPLIT_FOR
] = l
;
40234 OMP_CLAUSE_CHAIN (l
) = clauses
;
40237 add_private_clause
= NULL_TREE
;
40241 if (OMP_CLAUSE_CODE (*c
) == OMP_CLAUSE_PRIVATE
40242 && OMP_CLAUSE_DECL (*c
) == real_decl
)
40243 add_private_clause
= NULL_TREE
;
40244 c
= &OMP_CLAUSE_CHAIN (*c
);
40248 if (add_private_clause
)
40251 for (c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
40253 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_PRIVATE
40254 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LASTPRIVATE
)
40255 && OMP_CLAUSE_DECL (c
) == decl
)
40257 else if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_FIRSTPRIVATE
40258 && OMP_CLAUSE_DECL (c
) == decl
)
40259 error_at (loc
, "iteration variable %qD "
40260 "should not be firstprivate",
40262 else if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_REDUCTION
40263 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_IN_REDUCTION
)
40264 && OMP_CLAUSE_DECL (c
) == decl
)
40265 error_at (loc
, "iteration variable %qD should not be reduction",
40270 if ((code
== OMP_SIMD
&& collapse
!= 1) || code
== OMP_LOOP
)
40271 c
= build_omp_clause (loc
, OMP_CLAUSE_LASTPRIVATE
);
40272 else if (code
!= OMP_SIMD
)
40273 c
= build_omp_clause (loc
, OMP_CLAUSE_PRIVATE
);
40275 c
= build_omp_clause (loc
, OMP_CLAUSE_LINEAR
);
40276 OMP_CLAUSE_DECL (c
) = add_private_clause
;
40277 c
= finish_omp_clauses (c
, C_ORT_OMP
);
40280 OMP_CLAUSE_CHAIN (c
) = clauses
;
40282 /* For linear, signal that we need to fill up
40283 the so far unknown linear step. */
40284 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINEAR
)
40285 OMP_CLAUSE_LINEAR_STEP (c
) = NULL_TREE
;
40291 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_SEMICOLON
))
40292 cond
= cp_parser_omp_for_cond (parser
, decl
, code
);
40293 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
40296 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_CLOSE_PAREN
))
40298 /* If decl is an iterator, preserve the operator on decl
40299 until finish_omp_for. */
40301 && ((processing_template_decl
40302 && (TREE_TYPE (real_decl
) == NULL_TREE
40303 || !INDIRECT_TYPE_P (TREE_TYPE (real_decl
))))
40304 || CLASS_TYPE_P (TREE_TYPE (real_decl
))))
40305 incr
= cp_parser_omp_for_incr (parser
, real_decl
);
40307 incr
= cp_parser_expression (parser
);
40308 protected_set_expr_location_if_unset (incr
, input_location
);
40312 if (!parens
.require_close (parser
))
40313 cp_parser_skip_to_closing_parenthesis (parser
, /*recovering=*/true,
40314 /*or_comma=*/false,
40315 /*consume_paren=*/true);
40317 TREE_VEC_ELT (declv
, i
) = decl
;
40318 TREE_VEC_ELT (initv
, i
) = init
;
40319 TREE_VEC_ELT (condv
, i
) = cond
;
40320 TREE_VEC_ELT (incrv
, i
) = incr
;
40323 orig_inits
.safe_grow_cleared (i
+ 1, true);
40324 orig_inits
[i
] = orig_init
;
40329 orig_declv
= copy_node (declv
);
40330 TREE_VEC_ELT (orig_declv
, i
) = orig_decl
;
40332 else if (orig_declv
)
40333 TREE_VEC_ELT (orig_declv
, i
) = decl
;
40335 if (i
== count
- 1)
40338 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
40339 in between the collapsed for loops to be still considered perfectly
40340 nested. Hopefully the final version clarifies this.
40341 For now handle (multiple) {'s and empty statements. */
40342 cp_parser_parse_tentatively (parser
);
40345 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
40347 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_BRACE
))
40349 cp_lexer_consume_token (parser
->lexer
);
40352 else if (bracecount
40353 && cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
40354 cp_lexer_consume_token (parser
->lexer
);
40357 loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40358 error_at (loc
, "not enough for loops to collapse");
40359 collapse_err
= true;
40360 cp_parser_abort_tentative_parse (parser
);
40368 cp_parser_parse_definitely (parser
);
40369 nbraces
+= bracecount
;
40376 /* Note that we saved the original contents of this flag when we entered
40377 the structured block, and so we don't need to re-save it here. */
40378 parser
->in_statement
= IN_OMP_FOR
;
40380 /* Note that the grammar doesn't call for a structured block here,
40381 though the loop as a whole is a structured block. */
40384 body
= begin_omp_structured_block ();
40385 for (i
= 0; i
< count
; i
++)
40386 if (TREE_VEC_ELT (orig_declv
, i
) != TREE_VEC_ELT (declv
, i
))
40387 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv
, i
),
40388 TREE_VEC_ELT (declv
, i
));
40391 body
= push_stmt_list ();
40393 cp_parser_omp_scan_loop_body (parser
);
40395 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
40397 body
= finish_omp_structured_block (body
);
40399 body
= pop_stmt_list (body
);
40401 if (declv
== NULL_TREE
)
40404 ret
= finish_omp_for (loc_first
, code
, declv
, orig_declv
, initv
, condv
,
40405 incrv
, body
, pre_body
, &orig_inits
, clauses
);
40409 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_BRACE
))
40411 cp_lexer_consume_token (parser
->lexer
);
40414 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_SEMICOLON
))
40415 cp_lexer_consume_token (parser
->lexer
);
40420 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
40421 "collapsed loops not perfectly nested");
40423 collapse_err
= true;
40424 cp_parser_statement_seq_opt (parser
, NULL
);
40425 if (cp_lexer_next_token_is (parser
->lexer
, CPP_EOF
))
40430 while (!for_block
->is_empty ())
40432 tree t
= for_block
->pop ();
40433 if (TREE_CODE (t
) == STATEMENT_LIST
)
40434 add_stmt (pop_stmt_list (t
));
40442 /* Helper function for OpenMP parsing, split clauses and call
40443 finish_omp_clauses on each of the set of clauses afterwards. */
40446 cp_omp_split_clauses (location_t loc
, enum tree_code code
,
40447 omp_clause_mask mask
, tree clauses
, tree
*cclauses
)
40450 c_omp_split_clauses (loc
, code
, mask
, clauses
, cclauses
);
40451 for (i
= 0; i
< C_OMP_CLAUSE_SPLIT_COUNT
; i
++)
40453 cclauses
[i
] = finish_omp_clauses (cclauses
[i
], C_ORT_OMP
);
40457 #pragma omp loop loop-clause[optseq] new-line
40460 #define OMP_LOOP_CLAUSE_MASK \
40461 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_BIND) \
40466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40469 cp_parser_omp_loop (cp_parser
*parser
, cp_token
*pragma_tok
,
40470 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40473 tree clauses
, sb
, ret
;
40475 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40477 strcat (p_name
, " loop");
40478 mask
|= OMP_LOOP_CLAUSE_MASK
;
40480 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40484 cp_omp_split_clauses (loc
, OMP_LOOP
, mask
, clauses
, cclauses
);
40485 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_LOOP
];
40488 keep_next_level (true);
40489 sb
= begin_omp_structured_block ();
40490 save
= cp_parser_begin_omp_structured_block (parser
);
40492 ret
= cp_parser_omp_for_loop (parser
, OMP_LOOP
, clauses
, cclauses
, if_p
);
40494 cp_parser_end_omp_structured_block (parser
, save
);
40495 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40501 #pragma omp simd simd-clause[optseq] new-line
40504 #define OMP_SIMD_CLAUSE_MASK \
40505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
40506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
40507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
40509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NONTEMPORAL) \
40515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40518 cp_parser_omp_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
40519 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40522 tree clauses
, sb
, ret
;
40524 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40526 strcat (p_name
, " simd");
40527 mask
|= OMP_SIMD_CLAUSE_MASK
;
40529 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40533 cp_omp_split_clauses (loc
, OMP_SIMD
, mask
, clauses
, cclauses
);
40534 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SIMD
];
40535 tree c
= omp_find_clause (cclauses
[C_OMP_CLAUSE_SPLIT_FOR
],
40536 OMP_CLAUSE_ORDERED
);
40537 if (c
&& OMP_CLAUSE_ORDERED_EXPR (c
))
40539 error_at (OMP_CLAUSE_LOCATION (c
),
40540 "%<ordered%> clause with parameter may not be specified "
40541 "on %qs construct", p_name
);
40542 OMP_CLAUSE_ORDERED_EXPR (c
) = NULL_TREE
;
40546 keep_next_level (true);
40547 sb
= begin_omp_structured_block ();
40548 save
= cp_parser_begin_omp_structured_block (parser
);
40550 ret
= cp_parser_omp_for_loop (parser
, OMP_SIMD
, clauses
, cclauses
, if_p
);
40552 cp_parser_end_omp_structured_block (parser
, save
);
40553 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40559 #pragma omp for for-clause[optseq] new-line
40563 #pragma omp for simd for-simd-clause[optseq] new-line
40566 #define OMP_FOR_CLAUSE_MASK \
40567 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
40571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
40573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
40574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
40575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
40576 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDER))
40580 cp_parser_omp_for (cp_parser
*parser
, cp_token
*pragma_tok
,
40581 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40584 tree clauses
, sb
, ret
;
40586 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40588 strcat (p_name
, " for");
40589 mask
|= OMP_FOR_CLAUSE_MASK
;
40590 /* parallel for{, simd} disallows nowait clause, but for
40591 target {teams distribute ,}parallel for{, simd} it should be accepted. */
40592 if (cclauses
&& (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) == 0)
40593 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
40594 /* Composite distribute parallel for{, simd} disallows ordered clause. */
40595 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
40596 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_ORDERED
);
40598 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40600 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40601 const char *p
= IDENTIFIER_POINTER (id
);
40603 if (strcmp (p
, "simd") == 0)
40605 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40606 if (cclauses
== NULL
)
40607 cclauses
= cclauses_buf
;
40609 cp_lexer_consume_token (parser
->lexer
);
40610 if (!flag_openmp
) /* flag_openmp_simd */
40611 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40613 sb
= begin_omp_structured_block ();
40614 save
= cp_parser_begin_omp_structured_block (parser
);
40615 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
40617 cp_parser_end_omp_structured_block (parser
, save
);
40618 tree body
= finish_omp_structured_block (sb
);
40621 ret
= make_node (OMP_FOR
);
40622 TREE_TYPE (ret
) = void_type_node
;
40623 OMP_FOR_BODY (ret
) = body
;
40624 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
40625 SET_EXPR_LOCATION (ret
, loc
);
40630 if (!flag_openmp
) /* flag_openmp_simd */
40632 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40636 /* Composite distribute parallel for disallows linear clause. */
40637 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
40638 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_LINEAR
);
40640 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40644 cp_omp_split_clauses (loc
, OMP_FOR
, mask
, clauses
, cclauses
);
40645 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_FOR
];
40648 keep_next_level (true);
40649 sb
= begin_omp_structured_block ();
40650 save
= cp_parser_begin_omp_structured_block (parser
);
40652 ret
= cp_parser_omp_for_loop (parser
, OMP_FOR
, clauses
, cclauses
, if_p
);
40654 cp_parser_end_omp_structured_block (parser
, save
);
40655 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
40660 static tree
cp_parser_omp_taskloop (cp_parser
*, cp_token
*, char *,
40661 omp_clause_mask
, tree
*, bool *);
40664 # pragma omp master new-line
40665 structured-block */
40668 cp_parser_omp_master (cp_parser
*parser
, cp_token
*pragma_tok
,
40669 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40672 tree clauses
, sb
, ret
;
40674 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40676 strcat (p_name
, " master");
40678 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40680 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40681 const char *p
= IDENTIFIER_POINTER (id
);
40683 if (strcmp (p
, "taskloop") == 0)
40685 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40686 if (cclauses
== NULL
)
40687 cclauses
= cclauses_buf
;
40689 cp_lexer_consume_token (parser
->lexer
);
40690 if (!flag_openmp
) /* flag_openmp_simd */
40691 return cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
40693 sb
= begin_omp_structured_block ();
40694 save
= cp_parser_begin_omp_structured_block (parser
);
40695 ret
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
,
40697 cp_parser_end_omp_structured_block (parser
, save
);
40698 tree body
= finish_omp_structured_block (sb
);
40701 return c_finish_omp_master (loc
, body
);
40704 if (!flag_openmp
) /* flag_openmp_simd */
40706 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40712 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40714 cp_omp_split_clauses (loc
, OMP_MASTER
, mask
, clauses
, cclauses
);
40717 cp_parser_require_pragma_eol (parser
, pragma_tok
);
40719 return c_finish_omp_master (loc
,
40720 cp_parser_omp_structured_block (parser
, if_p
));
40724 # pragma omp ordered new-line
40728 # pragma omp ordered ordered-clauses new-line
40729 structured-block */
40731 #define OMP_ORDERED_CLAUSE_MASK \
40732 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
40733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
40735 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
40736 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
40739 cp_parser_omp_ordered (cp_parser
*parser
, cp_token
*pragma_tok
,
40740 enum pragma_context context
, bool *if_p
)
40742 location_t loc
= pragma_tok
->location
;
40744 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40746 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40747 const char *p
= IDENTIFIER_POINTER (id
);
40749 if (strcmp (p
, "depend") == 0)
40751 if (!flag_openmp
) /* flag_openmp_simd */
40753 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40756 if (context
== pragma_stmt
)
40758 error_at (pragma_tok
->location
, "%<#pragma omp ordered%> with "
40759 "%<depend%> clause may only be used in compound "
40761 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40765 = cp_parser_omp_all_clauses (parser
,
40766 OMP_ORDERED_DEPEND_CLAUSE_MASK
,
40767 "#pragma omp ordered", pragma_tok
);
40768 c_finish_omp_ordered (loc
, clauses
, NULL_TREE
);
40774 = cp_parser_omp_all_clauses (parser
, OMP_ORDERED_CLAUSE_MASK
,
40775 "#pragma omp ordered", pragma_tok
);
40777 if (!flag_openmp
/* flag_openmp_simd */
40778 && omp_find_clause (clauses
, OMP_CLAUSE_SIMD
) == NULL_TREE
)
40781 c_finish_omp_ordered (loc
, clauses
,
40782 cp_parser_omp_structured_block (parser
, if_p
));
40789 { section-sequence }
40792 section-directive[opt] structured-block
40793 section-sequence section-directive structured-block */
40796 cp_parser_omp_sections_scope (cp_parser
*parser
)
40798 tree stmt
, substmt
;
40799 bool error_suppress
= false;
40802 matching_braces braces
;
40803 if (!braces
.require_open (parser
))
40806 stmt
= push_stmt_list ();
40808 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser
->lexer
))
40809 != PRAGMA_OMP_SECTION
)
40811 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
40812 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
40813 add_stmt (substmt
);
40818 tok
= cp_lexer_peek_token (parser
->lexer
);
40819 if (tok
->type
== CPP_CLOSE_BRACE
)
40821 if (tok
->type
== CPP_EOF
)
40824 if (cp_parser_pragma_kind (tok
) == PRAGMA_OMP_SECTION
)
40826 cp_lexer_consume_token (parser
->lexer
);
40827 cp_parser_require_pragma_eol (parser
, tok
);
40828 error_suppress
= false;
40830 else if (!error_suppress
)
40832 cp_parser_error (parser
, "expected %<#pragma omp section%> or %<}%>");
40833 error_suppress
= true;
40836 substmt
= cp_parser_omp_structured_block (parser
, NULL
);
40837 substmt
= build1 (OMP_SECTION
, void_type_node
, substmt
);
40838 add_stmt (substmt
);
40840 braces
.require_close (parser
);
40842 substmt
= pop_stmt_list (stmt
);
40844 stmt
= make_node (OMP_SECTIONS
);
40845 TREE_TYPE (stmt
) = void_type_node
;
40846 OMP_SECTIONS_BODY (stmt
) = substmt
;
40853 # pragma omp sections sections-clause[optseq] newline
40856 #define OMP_SECTIONS_CLAUSE_MASK \
40857 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
40860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
40865 cp_parser_omp_sections (cp_parser
*parser
, cp_token
*pragma_tok
,
40866 char *p_name
, omp_clause_mask mask
, tree
*cclauses
)
40869 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40871 strcat (p_name
, " sections");
40872 mask
|= OMP_SECTIONS_CLAUSE_MASK
;
40874 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NOWAIT
);
40876 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
40880 cp_omp_split_clauses (loc
, OMP_SECTIONS
, mask
, clauses
, cclauses
);
40881 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_SECTIONS
];
40884 ret
= cp_parser_omp_sections_scope (parser
);
40886 OMP_SECTIONS_CLAUSES (ret
) = clauses
;
40892 # pragma omp parallel parallel-clause[optseq] new-line
40894 # pragma omp parallel for parallel-for-clause[optseq] new-line
40896 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
40900 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
40901 structured-block */
40903 #define OMP_PARALLEL_CLAUSE_MASK \
40904 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
40905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
40906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
40907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
40908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
40909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
40910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
40911 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
40912 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
40913 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
40916 cp_parser_omp_parallel (cp_parser
*parser
, cp_token
*pragma_tok
,
40917 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
40920 tree stmt
, clauses
, block
;
40922 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
40924 strcat (p_name
, " parallel");
40925 mask
|= OMP_PARALLEL_CLAUSE_MASK
;
40926 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
40927 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_MAP
)) != 0
40928 && (mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) == 0)
40929 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_COPYIN
);
40931 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_FOR
))
40933 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40934 if (cclauses
== NULL
)
40935 cclauses
= cclauses_buf
;
40937 cp_lexer_consume_token (parser
->lexer
);
40938 if (!flag_openmp
) /* flag_openmp_simd */
40939 return cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
40941 block
= begin_omp_parallel ();
40942 save
= cp_parser_begin_omp_structured_block (parser
);
40943 tree ret
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, cclauses
,
40945 cp_parser_end_omp_structured_block (parser
, save
);
40946 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
40948 if (ret
== NULL_TREE
)
40950 OMP_PARALLEL_COMBINED (stmt
) = 1;
40953 /* When combined with distribute, parallel has to be followed by for.
40954 #pragma omp target parallel is allowed though. */
40956 && (mask
& (OMP_CLAUSE_MASK_1
40957 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE
)) != 0)
40959 error_at (loc
, "expected %<for%> after %qs", p_name
);
40960 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
40963 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
40965 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
40966 const char *p
= IDENTIFIER_POINTER (id
);
40967 if (cclauses
== NULL
&& strcmp (p
, "master") == 0)
40969 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40970 cclauses
= cclauses_buf
;
40972 cp_lexer_consume_token (parser
->lexer
);
40973 if (!flag_openmp
) /* flag_openmp_simd */
40974 return cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
40976 block
= begin_omp_parallel ();
40977 save
= cp_parser_begin_omp_structured_block (parser
);
40978 tree ret
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
,
40980 cp_parser_end_omp_structured_block (parser
, save
);
40981 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
40983 if (ret
== NULL_TREE
)
40985 OMP_PARALLEL_COMBINED (stmt
) = 1;
40988 else if (strcmp (p
, "loop") == 0)
40990 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
40991 if (cclauses
== NULL
)
40992 cclauses
= cclauses_buf
;
40994 cp_lexer_consume_token (parser
->lexer
);
40995 if (!flag_openmp
) /* flag_openmp_simd */
40996 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
40998 block
= begin_omp_parallel ();
40999 save
= cp_parser_begin_omp_structured_block (parser
);
41000 tree ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
41002 cp_parser_end_omp_structured_block (parser
, save
);
41003 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
41005 if (ret
== NULL_TREE
)
41007 OMP_PARALLEL_COMBINED (stmt
) = 1;
41010 else if (!flag_openmp
) /* flag_openmp_simd */
41012 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41015 else if (cclauses
== NULL
&& strcmp (p
, "sections") == 0)
41017 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
41018 cclauses
= cclauses_buf
;
41020 cp_lexer_consume_token (parser
->lexer
);
41021 block
= begin_omp_parallel ();
41022 save
= cp_parser_begin_omp_structured_block (parser
);
41023 cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, cclauses
);
41024 cp_parser_end_omp_structured_block (parser
, save
);
41025 stmt
= finish_omp_parallel (cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
],
41027 OMP_PARALLEL_COMBINED (stmt
) = 1;
41031 else if (!flag_openmp
) /* flag_openmp_simd */
41033 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41037 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
41041 cp_omp_split_clauses (loc
, OMP_PARALLEL
, mask
, clauses
, cclauses
);
41042 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_PARALLEL
];
41045 block
= begin_omp_parallel ();
41046 save
= cp_parser_begin_omp_structured_block (parser
);
41047 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
41048 cp_parser_end_omp_structured_block (parser
, save
);
41049 stmt
= finish_omp_parallel (clauses
, block
);
41054 # pragma omp single single-clause[optseq] new-line
41055 structured-block */
41057 #define OMP_SINGLE_CLAUSE_MASK \
41058 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
41061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41065 cp_parser_omp_single (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41067 tree stmt
= make_node (OMP_SINGLE
);
41068 TREE_TYPE (stmt
) = void_type_node
;
41069 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41071 OMP_SINGLE_CLAUSES (stmt
)
41072 = cp_parser_omp_all_clauses (parser
, OMP_SINGLE_CLAUSE_MASK
,
41073 "#pragma omp single", pragma_tok
);
41074 OMP_SINGLE_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
41076 return add_stmt (stmt
);
41080 # pragma omp task task-clause[optseq] new-line
41081 structured-block */
41083 #define OMP_TASK_CLAUSE_MASK \
41084 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41085 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
41086 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
41087 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41088 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41089 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
41091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
41092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
41094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION) \
41096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DETACH))
41099 cp_parser_omp_task (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41101 tree clauses
, block
;
41104 clauses
= cp_parser_omp_all_clauses (parser
, OMP_TASK_CLAUSE_MASK
,
41105 "#pragma omp task", pragma_tok
);
41106 block
= begin_omp_task ();
41107 save
= cp_parser_begin_omp_structured_block (parser
);
41108 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
41109 cp_parser_end_omp_structured_block (parser
, save
);
41110 return finish_omp_task (clauses
, block
);
41114 # pragma omp taskwait new-line
41117 # pragma omp taskwait taskwait-clause[opt] new-line */
41119 #define OMP_TASKWAIT_CLAUSE_MASK \
41120 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
41123 cp_parser_omp_taskwait (cp_parser
*parser
, cp_token
*pragma_tok
)
41126 = cp_parser_omp_all_clauses (parser
, OMP_TASKWAIT_CLAUSE_MASK
,
41127 "#pragma omp taskwait", pragma_tok
);
41131 tree stmt
= make_node (OMP_TASK
);
41132 TREE_TYPE (stmt
) = void_node
;
41133 OMP_TASK_CLAUSES (stmt
) = clauses
;
41134 OMP_TASK_BODY (stmt
) = NULL_TREE
;
41135 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41139 finish_omp_taskwait ();
41143 # pragma omp taskyield new-line */
41146 cp_parser_omp_taskyield (cp_parser
*parser
, cp_token
*pragma_tok
)
41148 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41149 finish_omp_taskyield ();
41153 # pragma omp taskgroup new-line
41157 # pragma omp taskgroup taskgroup-clause[optseq] new-line */
41159 #define OMP_TASKGROUP_CLAUSE_MASK \
41160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASK_REDUCTION))
41164 cp_parser_omp_taskgroup (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41167 = cp_parser_omp_all_clauses (parser
, OMP_TASKGROUP_CLAUSE_MASK
,
41168 "#pragma omp taskgroup", pragma_tok
);
41169 return c_finish_omp_taskgroup (input_location
,
41170 cp_parser_omp_structured_block (parser
,
41177 # pragma omp threadprivate (variable-list) */
41180 cp_parser_omp_threadprivate (cp_parser
*parser
, cp_token
*pragma_tok
)
41184 vars
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_ERROR
, NULL
);
41185 cp_parser_require_pragma_eol (parser
, pragma_tok
);
41187 finish_omp_threadprivate (vars
);
41191 # pragma omp cancel cancel-clause[optseq] new-line */
41193 #define OMP_CANCEL_CLAUSE_MASK \
41194 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41195 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
41198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
41201 cp_parser_omp_cancel (cp_parser
*parser
, cp_token
*pragma_tok
)
41203 tree clauses
= cp_parser_omp_all_clauses (parser
, OMP_CANCEL_CLAUSE_MASK
,
41204 "#pragma omp cancel", pragma_tok
);
41205 finish_omp_cancel (clauses
);
41209 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
41211 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
41212 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
41213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
41214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
41215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
41218 cp_parser_omp_cancellation_point (cp_parser
*parser
, cp_token
*pragma_tok
,
41219 enum pragma_context context
)
41222 bool point_seen
= false;
41224 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41226 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41227 const char *p
= IDENTIFIER_POINTER (id
);
41229 if (strcmp (p
, "point") == 0)
41231 cp_lexer_consume_token (parser
->lexer
);
41237 cp_parser_error (parser
, "expected %<point%>");
41238 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41242 if (context
!= pragma_compound
)
41244 if (context
== pragma_stmt
)
41245 error_at (pragma_tok
->location
,
41246 "%<#pragma %s%> may only be used in compound statements",
41247 "omp cancellation point");
41249 cp_parser_error (parser
, "expected declaration specifiers");
41250 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41254 clauses
= cp_parser_omp_all_clauses (parser
,
41255 OMP_CANCELLATION_POINT_CLAUSE_MASK
,
41256 "#pragma omp cancellation point",
41258 finish_omp_cancellation_point (clauses
);
41262 #pragma omp distribute distribute-clause[optseq] new-line
41265 #define OMP_DISTRIBUTE_CLAUSE_MASK \
41266 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
41269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
41270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
41274 cp_parser_omp_distribute (cp_parser
*parser
, cp_token
*pragma_tok
,
41275 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
41278 tree clauses
, sb
, ret
;
41280 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41282 strcat (p_name
, " distribute");
41283 mask
|= OMP_DISTRIBUTE_CLAUSE_MASK
;
41285 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41287 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41288 const char *p
= IDENTIFIER_POINTER (id
);
41290 bool parallel
= false;
41292 if (strcmp (p
, "simd") == 0)
41295 parallel
= strcmp (p
, "parallel") == 0;
41296 if (parallel
|| simd
)
41298 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
41299 if (cclauses
== NULL
)
41300 cclauses
= cclauses_buf
;
41301 cp_lexer_consume_token (parser
->lexer
);
41302 if (!flag_openmp
) /* flag_openmp_simd */
41305 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
41308 return cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
41311 sb
= begin_omp_structured_block ();
41312 save
= cp_parser_begin_omp_structured_block (parser
);
41314 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
41317 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
,
41319 cp_parser_end_omp_structured_block (parser
, save
);
41320 tree body
= finish_omp_structured_block (sb
);
41323 ret
= make_node (OMP_DISTRIBUTE
);
41324 TREE_TYPE (ret
) = void_type_node
;
41325 OMP_FOR_BODY (ret
) = body
;
41326 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
41327 SET_EXPR_LOCATION (ret
, loc
);
41332 if (!flag_openmp
) /* flag_openmp_simd */
41334 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41338 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
41342 cp_omp_split_clauses (loc
, OMP_DISTRIBUTE
, mask
, clauses
, cclauses
);
41343 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_DISTRIBUTE
];
41346 keep_next_level (true);
41347 sb
= begin_omp_structured_block ();
41348 save
= cp_parser_begin_omp_structured_block (parser
);
41350 ret
= cp_parser_omp_for_loop (parser
, OMP_DISTRIBUTE
, clauses
, NULL
, if_p
);
41352 cp_parser_end_omp_structured_block (parser
, save
);
41353 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
41359 # pragma omp teams teams-clause[optseq] new-line
41360 structured-block */
41362 #define OMP_TEAMS_CLAUSE_MASK \
41363 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41365 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
41366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
41367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
41368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
41369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
41373 cp_parser_omp_teams (cp_parser
*parser
, cp_token
*pragma_tok
,
41374 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
41377 tree clauses
, sb
, ret
;
41379 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
41381 strcat (p_name
, " teams");
41382 mask
|= OMP_TEAMS_CLAUSE_MASK
;
41384 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41386 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41387 const char *p
= IDENTIFIER_POINTER (id
);
41388 if (strcmp (p
, "distribute") == 0)
41390 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
41391 if (cclauses
== NULL
)
41392 cclauses
= cclauses_buf
;
41394 cp_lexer_consume_token (parser
->lexer
);
41395 if (!flag_openmp
) /* flag_openmp_simd */
41396 return cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
41398 keep_next_level (true);
41399 sb
= begin_omp_structured_block ();
41400 save
= cp_parser_begin_omp_structured_block (parser
);
41401 ret
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
,
41403 cp_parser_end_omp_structured_block (parser
, save
);
41404 tree body
= finish_omp_structured_block (sb
);
41407 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
41408 ret
= make_node (OMP_TEAMS
);
41409 TREE_TYPE (ret
) = void_type_node
;
41410 OMP_TEAMS_CLAUSES (ret
) = clauses
;
41411 OMP_TEAMS_BODY (ret
) = body
;
41412 OMP_TEAMS_COMBINED (ret
) = 1;
41413 SET_EXPR_LOCATION (ret
, loc
);
41414 return add_stmt (ret
);
41416 else if (strcmp (p
, "loop") == 0)
41418 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
41419 if (cclauses
== NULL
)
41420 cclauses
= cclauses_buf
;
41422 cp_lexer_consume_token (parser
->lexer
);
41423 if (!flag_openmp
) /* flag_openmp_simd */
41424 return cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
41426 keep_next_level (true);
41427 sb
= begin_omp_structured_block ();
41428 save
= cp_parser_begin_omp_structured_block (parser
);
41429 ret
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
,
41431 cp_parser_end_omp_structured_block (parser
, save
);
41432 tree body
= finish_omp_structured_block (sb
);
41435 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
41436 ret
= make_node (OMP_TEAMS
);
41437 TREE_TYPE (ret
) = void_type_node
;
41438 OMP_TEAMS_CLAUSES (ret
) = clauses
;
41439 OMP_TEAMS_BODY (ret
) = body
;
41440 OMP_TEAMS_COMBINED (ret
) = 1;
41441 SET_EXPR_LOCATION (ret
, loc
);
41442 return add_stmt (ret
);
41445 if (!flag_openmp
) /* flag_openmp_simd */
41447 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41451 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
41455 cp_omp_split_clauses (loc
, OMP_TEAMS
, mask
, clauses
, cclauses
);
41456 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
41459 tree stmt
= make_node (OMP_TEAMS
);
41460 TREE_TYPE (stmt
) = void_type_node
;
41461 OMP_TEAMS_CLAUSES (stmt
) = clauses
;
41462 keep_next_level (true);
41463 OMP_TEAMS_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
41464 SET_EXPR_LOCATION (stmt
, loc
);
41466 return add_stmt (stmt
);
41470 # pragma omp target data target-data-clause[optseq] new-line
41471 structured-block */
41473 #define OMP_TARGET_DATA_CLAUSE_MASK \
41474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR) \
41478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_ADDR))
41481 cp_parser_omp_target_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
41484 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_DATA_CLAUSE_MASK
,
41485 "#pragma omp target data", pragma_tok
);
41486 c_omp_adjust_map_clauses (clauses
, false);
41488 for (tree
*pc
= &clauses
; *pc
;)
41490 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
41491 switch (OMP_CLAUSE_MAP_KIND (*pc
))
41494 case GOMP_MAP_ALWAYS_TO
:
41495 case GOMP_MAP_FROM
:
41496 case GOMP_MAP_ALWAYS_FROM
:
41497 case GOMP_MAP_TOFROM
:
41498 case GOMP_MAP_ALWAYS_TOFROM
:
41499 case GOMP_MAP_ALLOC
:
41502 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41503 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
41504 case GOMP_MAP_ALWAYS_POINTER
:
41505 case GOMP_MAP_ATTACH_DETACH
:
41509 error_at (OMP_CLAUSE_LOCATION (*pc
),
41510 "%<#pragma omp target data%> with map-type other "
41511 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41512 "on %<map%> clause");
41513 *pc
= OMP_CLAUSE_CHAIN (*pc
);
41516 else if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_PTR
41517 || OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_USE_DEVICE_ADDR
)
41519 pc
= &OMP_CLAUSE_CHAIN (*pc
);
41525 error_at (pragma_tok
->location
,
41526 "%<#pragma omp target data%> must contain at least "
41527 "one %<map%>, %<use_device_ptr%> or %<use_device_addr%> "
41532 tree stmt
= make_node (OMP_TARGET_DATA
);
41533 TREE_TYPE (stmt
) = void_type_node
;
41534 OMP_TARGET_DATA_CLAUSES (stmt
) = clauses
;
41536 keep_next_level (true);
41537 OMP_TARGET_DATA_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
41539 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41540 return add_stmt (stmt
);
41544 # pragma omp target enter data target-enter-data-clause[optseq] new-line
41545 structured-block */
41547 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
41548 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41555 cp_parser_omp_target_enter_data (cp_parser
*parser
, cp_token
*pragma_tok
,
41556 enum pragma_context context
)
41558 bool data_seen
= false;
41559 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41561 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41562 const char *p
= IDENTIFIER_POINTER (id
);
41564 if (strcmp (p
, "data") == 0)
41566 cp_lexer_consume_token (parser
->lexer
);
41572 cp_parser_error (parser
, "expected %<data%>");
41573 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41577 if (context
== pragma_stmt
)
41579 error_at (pragma_tok
->location
,
41580 "%<#pragma %s%> may only be used in compound statements",
41581 "omp target enter data");
41582 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41587 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_ENTER_DATA_CLAUSE_MASK
,
41588 "#pragma omp target enter data", pragma_tok
);
41589 c_omp_adjust_map_clauses (clauses
, false);
41591 for (tree
*pc
= &clauses
; *pc
;)
41593 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
41594 switch (OMP_CLAUSE_MAP_KIND (*pc
))
41597 case GOMP_MAP_ALWAYS_TO
:
41598 case GOMP_MAP_ALLOC
:
41601 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41602 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
41603 case GOMP_MAP_ALWAYS_POINTER
:
41604 case GOMP_MAP_ATTACH_DETACH
:
41608 error_at (OMP_CLAUSE_LOCATION (*pc
),
41609 "%<#pragma omp target enter data%> with map-type other "
41610 "than %<to%> or %<alloc%> on %<map%> clause");
41611 *pc
= OMP_CLAUSE_CHAIN (*pc
);
41614 pc
= &OMP_CLAUSE_CHAIN (*pc
);
41620 error_at (pragma_tok
->location
,
41621 "%<#pragma omp target enter data%> must contain at least "
41622 "one %<map%> clause");
41626 tree stmt
= make_node (OMP_TARGET_ENTER_DATA
);
41627 TREE_TYPE (stmt
) = void_type_node
;
41628 OMP_TARGET_ENTER_DATA_CLAUSES (stmt
) = clauses
;
41629 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41630 return add_stmt (stmt
);
41634 # pragma omp target exit data target-enter-data-clause[optseq] new-line
41635 structured-block */
41637 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
41638 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41645 cp_parser_omp_target_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
41646 enum pragma_context context
)
41648 bool data_seen
= false;
41649 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41651 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41652 const char *p
= IDENTIFIER_POINTER (id
);
41654 if (strcmp (p
, "data") == 0)
41656 cp_lexer_consume_token (parser
->lexer
);
41662 cp_parser_error (parser
, "expected %<data%>");
41663 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41667 if (context
== pragma_stmt
)
41669 error_at (pragma_tok
->location
,
41670 "%<#pragma %s%> may only be used in compound statements",
41671 "omp target exit data");
41672 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41677 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_EXIT_DATA_CLAUSE_MASK
,
41678 "#pragma omp target exit data", pragma_tok
);
41679 c_omp_adjust_map_clauses (clauses
, false);
41681 for (tree
*pc
= &clauses
; *pc
;)
41683 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
41684 switch (OMP_CLAUSE_MAP_KIND (*pc
))
41686 case GOMP_MAP_FROM
:
41687 case GOMP_MAP_ALWAYS_FROM
:
41688 case GOMP_MAP_RELEASE
:
41689 case GOMP_MAP_DELETE
:
41692 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41693 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
41694 case GOMP_MAP_ALWAYS_POINTER
:
41695 case GOMP_MAP_ATTACH_DETACH
:
41699 error_at (OMP_CLAUSE_LOCATION (*pc
),
41700 "%<#pragma omp target exit data%> with map-type other "
41701 "than %<from%>, %<release%> or %<delete%> on %<map%>"
41703 *pc
= OMP_CLAUSE_CHAIN (*pc
);
41706 pc
= &OMP_CLAUSE_CHAIN (*pc
);
41712 error_at (pragma_tok
->location
,
41713 "%<#pragma omp target exit data%> must contain at least "
41714 "one %<map%> clause");
41718 tree stmt
= make_node (OMP_TARGET_EXIT_DATA
);
41719 TREE_TYPE (stmt
) = void_type_node
;
41720 OMP_TARGET_EXIT_DATA_CLAUSES (stmt
) = clauses
;
41721 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41722 return add_stmt (stmt
);
41726 # pragma omp target update target-update-clause[optseq] new-line */
41728 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
41729 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
41730 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
41731 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41732 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41733 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41734 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
41737 cp_parser_omp_target_update (cp_parser
*parser
, cp_token
*pragma_tok
,
41738 enum pragma_context context
)
41740 if (context
== pragma_stmt
)
41742 error_at (pragma_tok
->location
,
41743 "%<#pragma %s%> may only be used in compound statements",
41744 "omp target update");
41745 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41750 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_UPDATE_CLAUSE_MASK
,
41751 "#pragma omp target update", pragma_tok
);
41752 if (omp_find_clause (clauses
, OMP_CLAUSE_TO
) == NULL_TREE
41753 && omp_find_clause (clauses
, OMP_CLAUSE_FROM
) == NULL_TREE
)
41755 error_at (pragma_tok
->location
,
41756 "%<#pragma omp target update%> must contain at least one "
41757 "%<from%> or %<to%> clauses");
41761 tree stmt
= make_node (OMP_TARGET_UPDATE
);
41762 TREE_TYPE (stmt
) = void_type_node
;
41763 OMP_TARGET_UPDATE_CLAUSES (stmt
) = clauses
;
41764 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41770 # pragma omp target target-clause[optseq] new-line
41771 structured-block */
41773 #define OMP_TARGET_CLAUSE_MASK \
41774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
41775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
41776 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
41777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
41778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
41779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
41780 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
41781 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
41782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
41783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
41786 cp_parser_omp_target (cp_parser
*parser
, cp_token
*pragma_tok
,
41787 enum pragma_context context
, bool *if_p
)
41789 tree
*pc
= NULL
, stmt
;
41793 = (enum omp_requires
) (omp_requires_mask
| OMP_REQUIRES_TARGET_USED
);
41795 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
41797 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
41798 const char *p
= IDENTIFIER_POINTER (id
);
41799 enum tree_code ccode
= ERROR_MARK
;
41801 if (strcmp (p
, "teams") == 0)
41803 else if (strcmp (p
, "parallel") == 0)
41804 ccode
= OMP_PARALLEL
;
41805 else if (strcmp (p
, "simd") == 0)
41807 if (ccode
!= ERROR_MARK
)
41809 tree cclauses
[C_OMP_CLAUSE_SPLIT_COUNT
];
41810 char p_name
[sizeof ("#pragma omp target teams distribute "
41811 "parallel for simd")];
41813 cp_lexer_consume_token (parser
->lexer
);
41814 strcpy (p_name
, "#pragma omp target");
41815 if (!flag_openmp
) /* flag_openmp_simd */
41821 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
41822 OMP_TARGET_CLAUSE_MASK
,
41826 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
41827 OMP_TARGET_CLAUSE_MASK
,
41831 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
41832 OMP_TARGET_CLAUSE_MASK
,
41836 gcc_unreachable ();
41838 return stmt
!= NULL_TREE
;
41840 keep_next_level (true);
41841 tree sb
= begin_omp_structured_block (), ret
;
41842 unsigned save
= cp_parser_begin_omp_structured_block (parser
);
41846 ret
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
,
41847 OMP_TARGET_CLAUSE_MASK
, cclauses
,
41851 ret
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
,
41852 OMP_TARGET_CLAUSE_MASK
, cclauses
,
41856 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
,
41857 OMP_TARGET_CLAUSE_MASK
, cclauses
,
41861 gcc_unreachable ();
41863 cp_parser_end_omp_structured_block (parser
, save
);
41864 tree body
= finish_omp_structured_block (sb
);
41865 if (ret
== NULL_TREE
)
41867 if (ccode
== OMP_TEAMS
&& !processing_template_decl
)
41869 /* For combined target teams, ensure the num_teams and
41870 thread_limit clause expressions are evaluated on the host,
41871 before entering the target construct. */
41873 for (c
= cclauses
[C_OMP_CLAUSE_SPLIT_TEAMS
];
41874 c
; c
= OMP_CLAUSE_CHAIN (c
))
41875 if ((OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_NUM_TEAMS
41876 || OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_THREAD_LIMIT
)
41877 && TREE_CODE (OMP_CLAUSE_OPERAND (c
, 0)) != INTEGER_CST
)
41879 tree expr
= OMP_CLAUSE_OPERAND (c
, 0);
41880 expr
= force_target_expr (TREE_TYPE (expr
), expr
, tf_none
);
41881 if (expr
== error_mark_node
)
41883 tree tmp
= TARGET_EXPR_SLOT (expr
);
41885 OMP_CLAUSE_OPERAND (c
, 0) = expr
;
41886 tree tc
= build_omp_clause (OMP_CLAUSE_LOCATION (c
),
41887 OMP_CLAUSE_FIRSTPRIVATE
);
41888 OMP_CLAUSE_DECL (tc
) = tmp
;
41889 OMP_CLAUSE_CHAIN (tc
)
41890 = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
41891 cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
] = tc
;
41894 tree stmt
= make_node (OMP_TARGET
);
41895 TREE_TYPE (stmt
) = void_type_node
;
41896 OMP_TARGET_CLAUSES (stmt
) = cclauses
[C_OMP_CLAUSE_SPLIT_TARGET
];
41897 OMP_TARGET_BODY (stmt
) = body
;
41898 OMP_TARGET_COMBINED (stmt
) = 1;
41899 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41901 pc
= &OMP_TARGET_CLAUSES (stmt
);
41902 goto check_clauses
;
41904 else if (!flag_openmp
) /* flag_openmp_simd */
41906 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41909 else if (strcmp (p
, "data") == 0)
41911 cp_lexer_consume_token (parser
->lexer
);
41912 cp_parser_omp_target_data (parser
, pragma_tok
, if_p
);
41915 else if (strcmp (p
, "enter") == 0)
41917 cp_lexer_consume_token (parser
->lexer
);
41918 cp_parser_omp_target_enter_data (parser
, pragma_tok
, context
);
41921 else if (strcmp (p
, "exit") == 0)
41923 cp_lexer_consume_token (parser
->lexer
);
41924 cp_parser_omp_target_exit_data (parser
, pragma_tok
, context
);
41927 else if (strcmp (p
, "update") == 0)
41929 cp_lexer_consume_token (parser
->lexer
);
41930 return cp_parser_omp_target_update (parser
, pragma_tok
, context
);
41933 if (!flag_openmp
) /* flag_openmp_simd */
41935 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
41939 stmt
= make_node (OMP_TARGET
);
41940 TREE_TYPE (stmt
) = void_type_node
;
41942 OMP_TARGET_CLAUSES (stmt
)
41943 = cp_parser_omp_all_clauses (parser
, OMP_TARGET_CLAUSE_MASK
,
41944 "#pragma omp target", pragma_tok
);
41945 c_omp_adjust_map_clauses (OMP_TARGET_CLAUSES (stmt
), true);
41947 pc
= &OMP_TARGET_CLAUSES (stmt
);
41948 keep_next_level (true);
41949 OMP_TARGET_BODY (stmt
) = cp_parser_omp_structured_block (parser
, if_p
);
41951 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
41957 if (OMP_CLAUSE_CODE (*pc
) == OMP_CLAUSE_MAP
)
41958 switch (OMP_CLAUSE_MAP_KIND (*pc
))
41961 case GOMP_MAP_ALWAYS_TO
:
41962 case GOMP_MAP_FROM
:
41963 case GOMP_MAP_ALWAYS_FROM
:
41964 case GOMP_MAP_TOFROM
:
41965 case GOMP_MAP_ALWAYS_TOFROM
:
41966 case GOMP_MAP_ALLOC
:
41967 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
41968 case GOMP_MAP_FIRSTPRIVATE_REFERENCE
:
41969 case GOMP_MAP_ALWAYS_POINTER
:
41970 case GOMP_MAP_ATTACH_DETACH
:
41973 error_at (OMP_CLAUSE_LOCATION (*pc
),
41974 "%<#pragma omp target%> with map-type other "
41975 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
41976 "on %<map%> clause");
41977 *pc
= OMP_CLAUSE_CHAIN (*pc
);
41980 pc
= &OMP_CLAUSE_CHAIN (*pc
);
41986 # pragma acc cache (variable-list) new-line
41990 cp_parser_oacc_cache (cp_parser
*parser
, cp_token
*pragma_tok
)
41992 /* Don't create location wrapper nodes within 'OMP_CLAUSE__CACHE_'
41994 auto_suppress_location_wrappers sentinel
;
41996 tree stmt
, clauses
;
41998 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE__CACHE_
, NULL_TREE
);
41999 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
42001 cp_parser_require_pragma_eol (parser
, cp_lexer_peek_token (parser
->lexer
));
42003 stmt
= make_node (OACC_CACHE
);
42004 TREE_TYPE (stmt
) = void_type_node
;
42005 OACC_CACHE_CLAUSES (stmt
) = clauses
;
42006 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
42013 # pragma acc data oacc-data-clause[optseq] new-line
42014 structured-block */
42016 #define OACC_DATA_CLAUSE_MASK \
42017 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42018 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42019 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42020 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42021 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42022 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42023 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42024 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42025 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42026 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42029 cp_parser_oacc_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
42031 tree stmt
, clauses
, block
;
42034 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DATA_CLAUSE_MASK
,
42035 "#pragma acc data", pragma_tok
);
42037 block
= begin_omp_parallel ();
42038 save
= cp_parser_begin_omp_structured_block (parser
);
42039 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
42040 cp_parser_end_omp_structured_block (parser
, save
);
42041 stmt
= finish_oacc_data (clauses
, block
);
42046 # pragma acc host_data <clauses> new-line
42047 structured-block */
42049 #define OACC_HOST_DATA_CLAUSE_MASK \
42050 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) \
42051 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42052 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) )
42055 cp_parser_oacc_host_data (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
42057 tree stmt
, clauses
, block
;
42060 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_HOST_DATA_CLAUSE_MASK
,
42061 "#pragma acc host_data", pragma_tok
);
42063 block
= begin_omp_parallel ();
42064 save
= cp_parser_begin_omp_structured_block (parser
);
42065 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
42066 cp_parser_end_omp_structured_block (parser
, save
);
42067 stmt
= finish_oacc_host_data (clauses
, block
);
42072 # pragma acc declare oacc-data-clause[optseq] new-line
42075 #define OACC_DECLARE_CLAUSE_MASK \
42076 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42077 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42078 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42079 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42080 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42081 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
42082 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
42083 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) )
42086 cp_parser_oacc_declare (cp_parser
*parser
, cp_token
*pragma_tok
)
42088 tree clauses
, stmt
;
42089 bool error
= false;
42090 bool found_in_scope
= global_bindings_p ();
42092 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_DECLARE_CLAUSE_MASK
,
42093 "#pragma acc declare", pragma_tok
, true);
42096 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
42098 error_at (pragma_tok
->location
,
42099 "no valid clauses specified in %<#pragma acc declare%>");
42103 for (tree t
= clauses
; t
; t
= OMP_CLAUSE_CHAIN (t
))
42105 location_t loc
= OMP_CLAUSE_LOCATION (t
);
42106 tree decl
= OMP_CLAUSE_DECL (t
);
42107 if (!DECL_P (decl
))
42109 error_at (loc
, "array section in %<#pragma acc declare%>");
42113 gcc_assert (OMP_CLAUSE_CODE (t
) == OMP_CLAUSE_MAP
);
42114 switch (OMP_CLAUSE_MAP_KIND (t
))
42116 case GOMP_MAP_FIRSTPRIVATE_POINTER
:
42117 case GOMP_MAP_ALLOC
:
42119 case GOMP_MAP_FORCE_DEVICEPTR
:
42120 case GOMP_MAP_DEVICE_RESIDENT
:
42123 case GOMP_MAP_LINK
:
42124 if (!global_bindings_p ()
42125 && (TREE_STATIC (decl
)
42126 || !DECL_EXTERNAL (decl
)))
42129 "%qD must be a global variable in "
42130 "%<#pragma acc declare link%>",
42138 if (global_bindings_p ())
42140 error_at (loc
, "invalid OpenACC clause at file scope");
42144 if (DECL_EXTERNAL (decl
))
42147 "invalid use of %<extern%> variable %qD "
42148 "in %<#pragma acc declare%>", decl
);
42152 else if (TREE_PUBLIC (decl
))
42155 "invalid use of %<global%> variable %qD "
42156 "in %<#pragma acc declare%>", decl
);
42163 if (!found_in_scope
)
42164 /* This seems to ignore the existence of cleanup scopes?
42165 What is the meaning for local extern decls? The local
42166 extern is in this scope, but it is referring to a decl that
42167 is namespace scope. */
42168 for (tree d
= current_binding_level
->names
; d
; d
= TREE_CHAIN (d
))
42171 found_in_scope
= true;
42174 if (!found_in_scope
)
42177 "%qD must be a variable declared in the same scope as "
42178 "%<#pragma acc declare%>", decl
);
42183 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl
))
42184 || lookup_attribute ("omp declare target link",
42185 DECL_ATTRIBUTES (decl
)))
42187 error_at (loc
, "variable %qD used more than once with "
42188 "%<#pragma acc declare%>", decl
);
42197 if (DECL_LOCAL_DECL_P (decl
))
42198 /* We need to mark the aliased decl, as that is the entity
42199 that is being referred to. This won't work for
42200 dependent variables, but it didn't work for them before
42201 DECL_LOCAL_DECL_P was a thing either. But then
42202 dependent local extern variable decls are as rare as
42204 if (auto alias
= DECL_LOCAL_DECL_ALIAS (decl
))
42207 if (OMP_CLAUSE_MAP_KIND (t
) == GOMP_MAP_LINK
)
42208 id
= get_identifier ("omp declare target link");
42210 id
= get_identifier ("omp declare target");
42212 DECL_ATTRIBUTES (decl
)
42213 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (decl
));
42214 if (current_binding_level
->kind
== sk_namespace
)
42216 symtab_node
*node
= symtab_node::get (decl
);
42219 node
->offloadable
= 1;
42220 if (ENABLE_OFFLOADING
)
42222 g
->have_offload
= true;
42223 if (is_a
<varpool_node
*> (node
))
42224 vec_safe_push (offload_vars
, decl
);
42231 if (error
|| current_binding_level
->kind
== sk_namespace
)
42234 stmt
= make_node (OACC_DECLARE
);
42235 TREE_TYPE (stmt
) = void_type_node
;
42236 OACC_DECLARE_CLAUSES (stmt
) = clauses
;
42237 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
42245 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
42249 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
42251 LOC is the location of the #pragma token.
42254 #define OACC_ENTER_DATA_CLAUSE_MASK \
42255 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42256 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42257 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42258 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42262 #define OACC_EXIT_DATA_CLAUSE_MASK \
42263 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
42267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DETACH) \
42268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FINALIZE) \
42269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42272 cp_parser_oacc_enter_exit_data (cp_parser
*parser
, cp_token
*pragma_tok
,
42275 location_t loc
= pragma_tok
->location
;
42276 tree stmt
, clauses
;
42277 const char *p
= "";
42279 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42280 p
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
42282 if (strcmp (p
, "data") != 0)
42284 error_at (loc
, "expected %<data%> after %<#pragma acc %s%>",
42285 enter
? "enter" : "exit");
42286 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
42290 cp_lexer_consume_token (parser
->lexer
);
42293 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_ENTER_DATA_CLAUSE_MASK
,
42294 "#pragma acc enter data", pragma_tok
);
42296 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_EXIT_DATA_CLAUSE_MASK
,
42297 "#pragma acc exit data", pragma_tok
);
42299 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
42301 error_at (loc
, "%<#pragma acc %s data%> has no data movement clause",
42302 enter
? "enter" : "exit");
42306 stmt
= enter
? make_node (OACC_ENTER_DATA
) : make_node (OACC_EXIT_DATA
);
42307 TREE_TYPE (stmt
) = void_type_node
;
42308 OMP_STANDALONE_CLAUSES (stmt
) = clauses
;
42309 SET_EXPR_LOCATION (stmt
, loc
);
42315 # pragma acc loop oacc-loop-clause[optseq] new-line
42316 structured-block */
42318 #define OACC_LOOP_CLAUSE_MASK \
42319 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
42320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
42323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
42324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
42325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
42326 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
42327 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
42328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
42331 cp_parser_oacc_loop (cp_parser
*parser
, cp_token
*pragma_tok
, char *p_name
,
42332 omp_clause_mask mask
, tree
*cclauses
, bool *if_p
)
42334 bool is_parallel
= ((mask
>> PRAGMA_OACC_CLAUSE_REDUCTION
) & 1) == 1;
42336 strcat (p_name
, " loop");
42337 mask
|= OACC_LOOP_CLAUSE_MASK
;
42339 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
,
42343 clauses
= c_oacc_split_loop_clauses (clauses
, cclauses
, is_parallel
);
42345 *cclauses
= finish_omp_clauses (*cclauses
, C_ORT_ACC
);
42347 clauses
= finish_omp_clauses (clauses
, C_ORT_ACC
);
42350 tree block
= begin_omp_structured_block ();
42351 int save
= cp_parser_begin_omp_structured_block (parser
);
42352 tree stmt
= cp_parser_omp_for_loop (parser
, OACC_LOOP
, clauses
, NULL
, if_p
);
42353 cp_parser_end_omp_structured_block (parser
, save
);
42354 add_stmt (finish_omp_structured_block (block
));
42360 # pragma acc kernels oacc-kernels-clause[optseq] new-line
42365 # pragma acc parallel oacc-parallel-clause[optseq] new-line
42370 # pragma acc serial oacc-serial-clause[optseq] new-line
42373 #define OACC_KERNELS_CLAUSE_MASK \
42374 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42375 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42376 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42390 #define OACC_PARALLEL_CLAUSE_MASK \
42391 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
42403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
42404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
42408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42410 #define OACC_SERIAL_CLAUSE_MASK \
42411 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ATTACH) \
42413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
42414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
42415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
42416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
42417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
42418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
42419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NO_CREATE) \
42421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
42422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
42423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
42424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
42425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
42428 cp_parser_oacc_compute (cp_parser
*parser
, cp_token
*pragma_tok
,
42429 char *p_name
, bool *if_p
)
42431 omp_clause_mask mask
;
42432 enum tree_code code
;
42433 switch (cp_parser_pragma_kind (pragma_tok
))
42435 case PRAGMA_OACC_KERNELS
:
42436 strcat (p_name
, " kernels");
42437 mask
= OACC_KERNELS_CLAUSE_MASK
;
42438 code
= OACC_KERNELS
;
42440 case PRAGMA_OACC_PARALLEL
:
42441 strcat (p_name
, " parallel");
42442 mask
= OACC_PARALLEL_CLAUSE_MASK
;
42443 code
= OACC_PARALLEL
;
42445 case PRAGMA_OACC_SERIAL
:
42446 strcat (p_name
, " serial");
42447 mask
= OACC_SERIAL_CLAUSE_MASK
;
42448 code
= OACC_SERIAL
;
42451 gcc_unreachable ();
42454 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42457 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
42458 if (strcmp (p
, "loop") == 0)
42460 cp_lexer_consume_token (parser
->lexer
);
42461 tree block
= begin_omp_parallel ();
42463 tree stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
,
42465 protected_set_expr_location (stmt
, pragma_tok
->location
);
42466 return finish_omp_construct (code
, block
, clauses
);
42470 tree clauses
= cp_parser_oacc_all_clauses (parser
, mask
, p_name
, pragma_tok
);
42472 tree block
= begin_omp_parallel ();
42473 unsigned int save
= cp_parser_begin_omp_structured_block (parser
);
42474 cp_parser_statement (parser
, NULL_TREE
, false, if_p
);
42475 cp_parser_end_omp_structured_block (parser
, save
);
42476 return finish_omp_construct (code
, block
, clauses
);
42480 # pragma acc update oacc-update-clause[optseq] new-line
42483 #define OACC_UPDATE_CLAUSE_MASK \
42484 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
42485 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
42486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
42487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
42488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF_PRESENT) \
42489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
42492 cp_parser_oacc_update (cp_parser
*parser
, cp_token
*pragma_tok
)
42494 tree stmt
, clauses
;
42496 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_UPDATE_CLAUSE_MASK
,
42497 "#pragma acc update", pragma_tok
);
42499 if (omp_find_clause (clauses
, OMP_CLAUSE_MAP
) == NULL_TREE
)
42501 error_at (pragma_tok
->location
,
42502 "%<#pragma acc update%> must contain at least one "
42503 "%<device%> or %<host%> or %<self%> clause");
42507 stmt
= make_node (OACC_UPDATE
);
42508 TREE_TYPE (stmt
) = void_type_node
;
42509 OACC_UPDATE_CLAUSES (stmt
) = clauses
;
42510 SET_EXPR_LOCATION (stmt
, pragma_tok
->location
);
42516 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
42518 LOC is the location of the #pragma token.
42521 #define OACC_WAIT_CLAUSE_MASK \
42522 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
42525 cp_parser_oacc_wait (cp_parser
*parser
, cp_token
*pragma_tok
)
42527 tree clauses
, list
= NULL_TREE
, stmt
= NULL_TREE
;
42528 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
42530 if (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
42531 list
= cp_parser_oacc_wait_list (parser
, loc
, list
);
42533 clauses
= cp_parser_oacc_all_clauses (parser
, OACC_WAIT_CLAUSE_MASK
,
42534 "#pragma acc wait", pragma_tok
);
42536 stmt
= c_finish_oacc_wait (loc
, list
, clauses
);
42537 stmt
= finish_expr_stmt (stmt
);
42543 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
42545 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
42546 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
42547 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
42548 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
42549 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
42550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
42551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
42554 cp_parser_omp_declare_simd (cp_parser
*parser
, cp_token
*pragma_tok
,
42555 enum pragma_context context
,
42558 bool first_p
= parser
->omp_declare_simd
== NULL
;
42559 cp_omp_declare_simd_data data
;
42562 data
.error_seen
= false;
42563 data
.fndecl_seen
= false;
42564 data
.variant_p
= variant_p
;
42565 data
.tokens
= vNULL
;
42566 data
.clauses
= NULL_TREE
;
42567 /* It is safe to take the address of a local variable; it will only be
42568 used while this scope is live. */
42569 parser
->omp_declare_simd
= &data
;
42571 else if (parser
->omp_declare_simd
->variant_p
!= variant_p
)
42573 error_at (pragma_tok
->location
,
42574 "%<#pragma omp declare %s%> followed by "
42575 "%<#pragma omp declare %s%>",
42576 parser
->omp_declare_simd
->variant_p
? "variant" : "simd",
42577 parser
->omp_declare_simd
->variant_p
? "simd" : "variant");
42578 parser
->omp_declare_simd
->error_seen
= true;
42581 /* Store away all pragma tokens. */
42582 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
42583 cp_lexer_consume_token (parser
->lexer
);
42584 cp_parser_require_pragma_eol (parser
, pragma_tok
);
42585 struct cp_token_cache
*cp
42586 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
42587 parser
->omp_declare_simd
->tokens
.safe_push (cp
);
42591 while (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
42592 cp_parser_pragma (parser
, context
, NULL
);
42595 case pragma_external
:
42596 cp_parser_declaration (parser
, NULL_TREE
);
42598 case pragma_member
:
42599 cp_parser_member_declaration (parser
);
42601 case pragma_objc_icode
:
42602 cp_parser_block_declaration (parser
, /*statement_p=*/false);
42605 cp_parser_declaration_statement (parser
);
42608 if (parser
->omp_declare_simd
42609 && !parser
->omp_declare_simd
->error_seen
42610 && !parser
->omp_declare_simd
->fndecl_seen
)
42611 error_at (pragma_tok
->location
,
42612 "%<#pragma omp declare %s%> not immediately followed by "
42613 "function declaration or definition",
42614 parser
->omp_declare_simd
->variant_p
? "variant" : "simd");
42615 data
.tokens
.release ();
42616 parser
->omp_declare_simd
= NULL
;
42620 static const char *const omp_construct_selectors
[] = {
42621 "simd", "target", "teams", "parallel", "for", NULL
};
42622 static const char *const omp_device_selectors
[] = {
42623 "kind", "isa", "arch", NULL
};
42624 static const char *const omp_implementation_selectors
[] = {
42625 "vendor", "extension", "atomic_default_mem_order", "unified_address",
42626 "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL
};
42627 static const char *const omp_user_selectors
[] = {
42628 "condition", NULL
};
42633 trait-selector-name[([trait-score:]trait-property[,trait-property[,...]])]
42636 score(score-expression) */
42639 cp_parser_omp_context_selector (cp_parser
*parser
, tree set
, bool has_parms_p
)
42641 tree ret
= NULL_TREE
;
42645 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
42646 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42647 selector
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42650 cp_parser_error (parser
, "expected trait selector name");
42651 return error_mark_node
;
42654 tree properties
= NULL_TREE
;
42655 const char *const *selectors
= NULL
;
42656 bool allow_score
= true;
42657 bool allow_user
= false;
42658 int property_limit
= 0;
42659 enum { CTX_PROPERTY_NONE
, CTX_PROPERTY_USER
, CTX_PROPERTY_NAME_LIST
,
42660 CTX_PROPERTY_ID
, CTX_PROPERTY_EXPR
,
42661 CTX_PROPERTY_SIMD
} property_kind
= CTX_PROPERTY_NONE
;
42662 switch (IDENTIFIER_POINTER (set
)[0])
42664 case 'c': /* construct */
42665 selectors
= omp_construct_selectors
;
42666 allow_score
= false;
42667 property_limit
= 1;
42668 property_kind
= CTX_PROPERTY_SIMD
;
42670 case 'd': /* device */
42671 selectors
= omp_device_selectors
;
42672 allow_score
= false;
42674 property_limit
= 3;
42675 property_kind
= CTX_PROPERTY_NAME_LIST
;
42677 case 'i': /* implementation */
42678 selectors
= omp_implementation_selectors
;
42680 property_limit
= 3;
42681 property_kind
= CTX_PROPERTY_NAME_LIST
;
42683 case 'u': /* user */
42684 selectors
= omp_user_selectors
;
42685 property_limit
= 1;
42686 property_kind
= CTX_PROPERTY_EXPR
;
42689 gcc_unreachable ();
42691 for (int i
= 0; ; i
++)
42693 if (selectors
[i
] == NULL
)
42697 property_kind
= CTX_PROPERTY_USER
;
42702 error ("selector %qs not allowed for context selector "
42703 "set %qs", IDENTIFIER_POINTER (selector
),
42704 IDENTIFIER_POINTER (set
));
42705 cp_lexer_consume_token (parser
->lexer
);
42706 return error_mark_node
;
42709 if (i
== property_limit
)
42710 property_kind
= CTX_PROPERTY_NONE
;
42711 if (strcmp (selectors
[i
], IDENTIFIER_POINTER (selector
)) == 0)
42714 if (property_kind
== CTX_PROPERTY_NAME_LIST
42715 && IDENTIFIER_POINTER (set
)[0] == 'i'
42716 && strcmp (IDENTIFIER_POINTER (selector
),
42717 "atomic_default_mem_order") == 0)
42718 property_kind
= CTX_PROPERTY_ID
;
42720 cp_lexer_consume_token (parser
->lexer
);
42722 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
42724 if (property_kind
== CTX_PROPERTY_NONE
)
42726 error ("selector %qs does not accept any properties",
42727 IDENTIFIER_POINTER (selector
));
42728 return error_mark_node
;
42731 matching_parens parens
;
42732 parens
.consume_open (parser
);
42734 cp_token
*token
= cp_lexer_peek_token (parser
->lexer
);
42736 && cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
)
42737 && strcmp (IDENTIFIER_POINTER (token
->u
.value
), "score") == 0
42738 && cp_lexer_nth_token_is (parser
->lexer
, 2, CPP_OPEN_PAREN
))
42740 cp_lexer_save_tokens (parser
->lexer
);
42741 cp_lexer_consume_token (parser
->lexer
);
42742 cp_lexer_consume_token (parser
->lexer
);
42743 if (cp_parser_skip_to_closing_parenthesis (parser
, false, false,
42745 && cp_lexer_next_token_is (parser
->lexer
, CPP_COLON
))
42747 cp_lexer_rollback_tokens (parser
->lexer
);
42748 cp_lexer_consume_token (parser
->lexer
);
42750 matching_parens parens2
;
42751 parens2
.require_open (parser
);
42752 tree score
= cp_parser_constant_expression (parser
);
42753 if (!parens2
.require_close (parser
))
42754 cp_parser_skip_to_closing_parenthesis (parser
, true,
42756 cp_parser_require (parser
, CPP_COLON
, RT_COLON
);
42757 if (score
!= error_mark_node
)
42759 score
= fold_non_dependent_expr (score
);
42760 if (value_dependent_expression_p (score
))
42761 properties
= tree_cons (get_identifier (" score"),
42762 score
, properties
);
42763 else if (!INTEGRAL_TYPE_P (TREE_TYPE (score
))
42764 || TREE_CODE (score
) != INTEGER_CST
)
42765 error_at (token
->location
, "score argument must be "
42766 "constant integer expression");
42767 else if (tree_int_cst_sgn (score
) < 0)
42768 error_at (token
->location
, "score argument must be "
42771 properties
= tree_cons (get_identifier (" score"),
42772 score
, properties
);
42776 cp_lexer_rollback_tokens (parser
->lexer
);
42778 token
= cp_lexer_peek_token (parser
->lexer
);
42781 switch (property_kind
)
42784 case CTX_PROPERTY_USER
:
42787 t
= cp_parser_constant_expression (parser
);
42788 if (t
!= error_mark_node
)
42790 t
= fold_non_dependent_expr (t
);
42791 if (TREE_CODE (t
) == STRING_CST
)
42792 properties
= tree_cons (NULL_TREE
, t
, properties
);
42793 else if (!value_dependent_expression_p (t
)
42794 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
42795 || !tree_fits_shwi_p (t
)))
42796 error_at (token
->location
, "property must be "
42797 "constant integer expression or string "
42800 properties
= tree_cons (NULL_TREE
, t
, properties
);
42803 return error_mark_node
;
42805 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42806 cp_lexer_consume_token (parser
->lexer
);
42812 case CTX_PROPERTY_ID
:
42813 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
42814 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42816 tree prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42817 cp_lexer_consume_token (parser
->lexer
);
42818 properties
= tree_cons (prop
, NULL_TREE
, properties
);
42822 cp_parser_error (parser
, "expected identifier");
42823 return error_mark_node
;
42826 case CTX_PROPERTY_NAME_LIST
:
42829 tree prop
= NULL_TREE
, value
= NULL_TREE
;
42830 if (cp_lexer_next_token_is (parser
->lexer
, CPP_KEYWORD
)
42831 || cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42833 prop
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42834 cp_lexer_consume_token (parser
->lexer
);
42836 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_STRING
))
42837 value
= cp_parser_string_literal (parser
, false, false);
42840 cp_parser_error (parser
, "expected identifier or "
42842 return error_mark_node
;
42845 properties
= tree_cons (prop
, value
, properties
);
42847 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42848 cp_lexer_consume_token (parser
->lexer
);
42854 case CTX_PROPERTY_EXPR
:
42855 t
= cp_parser_constant_expression (parser
);
42856 if (t
!= error_mark_node
)
42858 t
= fold_non_dependent_expr (t
);
42859 if (!value_dependent_expression_p (t
)
42860 && (!INTEGRAL_TYPE_P (TREE_TYPE (t
))
42861 || !tree_fits_shwi_p (t
)))
42862 error_at (token
->location
, "property must be "
42863 "constant integer expression");
42865 properties
= tree_cons (NULL_TREE
, t
, properties
);
42868 return error_mark_node
;
42870 case CTX_PROPERTY_SIMD
:
42873 error_at (token
->location
, "properties for %<simd%> "
42874 "selector may not be specified in "
42875 "%<metadirective%>");
42876 return error_mark_node
;
42879 = cp_parser_omp_all_clauses (parser
,
42880 OMP_DECLARE_SIMD_CLAUSE_MASK
,
42881 "simd", NULL
, true, 2);
42884 gcc_unreachable ();
42887 if (!parens
.require_close (parser
))
42888 cp_parser_skip_to_closing_parenthesis (parser
, true, false, true);
42890 properties
= nreverse (properties
);
42892 else if (property_kind
== CTX_PROPERTY_NAME_LIST
42893 || property_kind
== CTX_PROPERTY_ID
42894 || property_kind
== CTX_PROPERTY_EXPR
)
42896 cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
);
42897 return error_mark_node
;
42900 ret
= tree_cons (selector
, properties
, ret
);
42902 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42903 cp_lexer_consume_token (parser
->lexer
);
42909 return nreverse (ret
);
42914 trait-set-selector[,trait-set-selector[,...]]
42916 trait-set-selector:
42917 trait-set-selector-name = { trait-selector[, trait-selector[, ...]] }
42919 trait-set-selector-name:
42926 cp_parser_omp_context_selector_specification (cp_parser
*parser
,
42929 tree ret
= NULL_TREE
;
42932 const char *setp
= "";
42933 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
42935 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
42939 if (strcmp (setp
, "construct") == 0)
42943 if (strcmp (setp
, "device") == 0)
42947 if (strcmp (setp
, "implementation") == 0)
42951 if (strcmp (setp
, "user") == 0)
42959 cp_parser_error (parser
, "expected %<construct%>, %<device%>, "
42960 "%<implementation%> or %<user%>");
42961 return error_mark_node
;
42964 tree set
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
42965 cp_lexer_consume_token (parser
->lexer
);
42967 if (!cp_parser_require (parser
, CPP_EQ
, RT_EQ
))
42968 return error_mark_node
;
42970 matching_braces braces
;
42971 if (!braces
.require_open (parser
))
42972 return error_mark_node
;
42975 = cp_parser_omp_context_selector (parser
, set
, has_parms_p
);
42976 if (selectors
== error_mark_node
)
42978 cp_parser_skip_to_closing_brace (parser
);
42979 ret
= error_mark_node
;
42981 else if (ret
!= error_mark_node
)
42982 ret
= tree_cons (set
, selectors
, ret
);
42984 braces
.require_close (parser
);
42986 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
42987 cp_lexer_consume_token (parser
->lexer
);
42993 if (ret
== error_mark_node
)
42995 return nreverse (ret
);
42998 /* Finalize #pragma omp declare variant after a fndecl has been parsed, and put
42999 that into "omp declare variant base" attribute. */
43002 cp_finish_omp_declare_variant (cp_parser
*parser
, cp_token
*pragma_tok
,
43005 matching_parens parens
;
43006 if (!parens
.require_open (parser
))
43009 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43014 cp_id_kind idk
= CP_ID_KIND_NONE
;
43015 cp_token
*varid_token
= cp_lexer_peek_token (parser
->lexer
);
43017 = cp_parser_id_expression (parser
, /*template_keyword_p=*/false,
43018 /*check_dependency_p=*/true,
43019 /*template_p=*/&template_p
,
43020 /*declarator_p=*/false,
43021 /*optional_p=*/false);
43022 parens
.require_close (parser
);
43025 if (TREE_CODE (varid
) == TEMPLATE_ID_EXPR
43026 || TREE_CODE (varid
) == TYPE_DECL
43027 || varid
== error_mark_node
)
43029 else if (varid_token
->type
== CPP_NAME
&& varid_token
->error_reported
)
43030 variant
= NULL_TREE
;
43033 tree ambiguous_decls
;
43034 variant
= cp_parser_lookup_name (parser
, varid
, none_type
,
43035 template_p
, /*is_namespace=*/false,
43036 /*check_dependency=*/true,
43038 varid
.get_location ());
43039 if (ambiguous_decls
)
43040 variant
= NULL_TREE
;
43042 if (variant
== NULL_TREE
)
43043 variant
= error_mark_node
;
43044 else if (TREE_CODE (variant
) != SCOPE_REF
)
43046 const char *error_msg
;
43048 = finish_id_expression (varid
, variant
, parser
->scope
,
43050 &parser
->non_integral_constant_expression_p
,
43051 template_p
, true, false, false, &error_msg
,
43052 varid
.get_location ());
43054 cp_parser_error (parser
, error_msg
);
43056 location_t caret_loc
= get_pure_location (varid
.get_location ());
43057 location_t start_loc
= get_start (varid_token
->location
);
43058 location_t finish_loc
= get_finish (varid
.get_location ());
43059 location_t varid_loc
= make_location (caret_loc
, start_loc
, finish_loc
);
43061 const char *clause
= "";
43062 location_t match_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43063 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43064 clause
= IDENTIFIER_POINTER (cp_lexer_peek_token (parser
->lexer
)->u
.value
);
43065 if (strcmp (clause
, "match"))
43067 cp_parser_error (parser
, "expected %<match%>");
43071 cp_lexer_consume_token (parser
->lexer
);
43073 if (!parens
.require_open (parser
))
43076 tree ctx
= cp_parser_omp_context_selector_specification (parser
, true);
43077 if (ctx
== error_mark_node
)
43079 ctx
= c_omp_check_context_selector (match_loc
, ctx
);
43080 if (ctx
!= error_mark_node
&& variant
!= error_mark_node
)
43082 tree match_loc_node
= maybe_wrap_with_location (integer_zero_node
,
43084 tree loc_node
= maybe_wrap_with_location (integer_zero_node
, varid_loc
);
43085 loc_node
= tree_cons (match_loc_node
,
43086 build_int_cst (integer_type_node
, idk
),
43087 build_tree_list (loc_node
, integer_zero_node
));
43088 attrs
= tree_cons (get_identifier ("omp declare variant base"),
43089 tree_cons (variant
, ctx
, loc_node
), attrs
);
43090 if (processing_template_decl
)
43091 ATTR_IS_DEPENDENT (attrs
) = 1;
43094 parens
.require_close (parser
);
43095 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43100 /* Finalize #pragma omp declare simd clauses after direct declarator has
43101 been parsed, and put that into "omp declare simd" attribute. */
43104 cp_parser_late_parsing_omp_declare_simd (cp_parser
*parser
, tree attrs
)
43106 struct cp_token_cache
*ce
;
43107 cp_omp_declare_simd_data
*data
= parser
->omp_declare_simd
;
43110 if (!data
->error_seen
&& data
->fndecl_seen
)
43112 error ("%<#pragma omp declare %s%> not immediately followed by "
43113 "a single function declaration or definition",
43114 data
->variant_p
? "variant" : "simd");
43115 data
->error_seen
= true;
43117 if (data
->error_seen
)
43120 FOR_EACH_VEC_ELT (data
->tokens
, i
, ce
)
43124 cp_parser_push_lexer_for_tokens (parser
, ce
);
43125 parser
->lexer
->in_pragma
= true;
43126 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
43127 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
43128 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43129 const char *kind
= IDENTIFIER_POINTER (id
);
43130 cp_lexer_consume_token (parser
->lexer
);
43131 if (strcmp (kind
, "simd") == 0)
43133 cl
= cp_parser_omp_all_clauses (parser
, OMP_DECLARE_SIMD_CLAUSE_MASK
,
43134 "#pragma omp declare simd",
43137 cl
= tree_cons (NULL_TREE
, cl
, NULL_TREE
);
43138 c
= build_tree_list (get_identifier ("omp declare simd"), cl
);
43139 TREE_CHAIN (c
) = attrs
;
43140 if (processing_template_decl
)
43141 ATTR_IS_DEPENDENT (c
) = 1;
43146 gcc_assert (strcmp (kind
, "variant") == 0);
43147 attrs
= cp_finish_omp_declare_variant (parser
, pragma_tok
, attrs
);
43149 cp_parser_pop_lexer (parser
);
43152 data
->fndecl_seen
= true;
43158 # pragma omp declare target new-line
43159 declarations and definitions
43160 # pragma omp end declare target new-line
43163 # pragma omp declare target ( extended-list ) new-line
43165 # pragma omp declare target declare-target-clauses[seq] new-line */
43167 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
43168 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
43169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK) \
43170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE))
43173 cp_parser_omp_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
43175 tree clauses
= NULL_TREE
;
43176 int device_type
= 0;
43177 bool only_device_type
= true;
43178 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43180 = cp_parser_omp_all_clauses (parser
, OMP_DECLARE_TARGET_CLAUSE_MASK
,
43181 "#pragma omp declare target", pragma_tok
);
43182 else if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
43184 clauses
= cp_parser_omp_var_list (parser
, OMP_CLAUSE_TO_DECLARE
,
43186 clauses
= finish_omp_clauses (clauses
, C_ORT_OMP
);
43187 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43191 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43192 scope_chain
->omp_declare_target_attribute
++;
43195 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
43196 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
43197 device_type
|= OMP_CLAUSE_DEVICE_TYPE_KIND (c
);
43198 for (tree c
= clauses
; c
; c
= OMP_CLAUSE_CHAIN (c
))
43200 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_DEVICE_TYPE
)
43202 tree t
= OMP_CLAUSE_DECL (c
), id
;
43203 tree at1
= lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t
));
43204 tree at2
= lookup_attribute ("omp declare target link",
43205 DECL_ATTRIBUTES (t
));
43206 only_device_type
= false;
43207 if (OMP_CLAUSE_CODE (c
) == OMP_CLAUSE_LINK
)
43209 id
= get_identifier ("omp declare target link");
43210 std::swap (at1
, at2
);
43213 id
= get_identifier ("omp declare target");
43216 error_at (OMP_CLAUSE_LOCATION (c
),
43217 "%qD specified both in declare target %<link%> and %<to%>"
43223 DECL_ATTRIBUTES (t
) = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
43224 if (TREE_CODE (t
) != FUNCTION_DECL
&& !is_global_var (t
))
43227 symtab_node
*node
= symtab_node::get (t
);
43230 node
->offloadable
= 1;
43231 if (ENABLE_OFFLOADING
)
43233 g
->have_offload
= true;
43234 if (is_a
<varpool_node
*> (node
))
43235 vec_safe_push (offload_vars
, t
);
43239 if (TREE_CODE (t
) != FUNCTION_DECL
)
43241 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_HOST
) != 0)
43243 tree at3
= lookup_attribute ("omp declare target host",
43244 DECL_ATTRIBUTES (t
));
43245 if (at3
== NULL_TREE
)
43247 id
= get_identifier ("omp declare target host");
43248 DECL_ATTRIBUTES (t
)
43249 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
43252 if ((device_type
& OMP_CLAUSE_DEVICE_TYPE_NOHOST
) != 0)
43254 tree at3
= lookup_attribute ("omp declare target nohost",
43255 DECL_ATTRIBUTES (t
));
43256 if (at3
== NULL_TREE
)
43258 id
= get_identifier ("omp declare target nohost");
43259 DECL_ATTRIBUTES (t
)
43260 = tree_cons (id
, NULL_TREE
, DECL_ATTRIBUTES (t
));
43264 if (device_type
&& only_device_type
)
43265 warning_at (OMP_CLAUSE_LOCATION (clauses
), 0,
43266 "directive with only %<device_type%> clauses ignored");
43270 cp_parser_omp_end_declare_target (cp_parser
*parser
, cp_token
*pragma_tok
)
43272 const char *p
= "";
43273 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43275 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43276 p
= IDENTIFIER_POINTER (id
);
43278 if (strcmp (p
, "declare") == 0)
43280 cp_lexer_consume_token (parser
->lexer
);
43282 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43284 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43285 p
= IDENTIFIER_POINTER (id
);
43287 if (strcmp (p
, "target") == 0)
43288 cp_lexer_consume_token (parser
->lexer
);
43291 cp_parser_error (parser
, "expected %<target%>");
43292 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43298 cp_parser_error (parser
, "expected %<declare%>");
43299 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43302 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43303 if (!scope_chain
->omp_declare_target_attribute
)
43304 error_at (pragma_tok
->location
,
43305 "%<#pragma omp end declare target%> without corresponding "
43306 "%<#pragma omp declare target%>");
43308 scope_chain
->omp_declare_target_attribute
--;
43311 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
43312 expression and optional initializer clause of
43313 #pragma omp declare reduction. We store the expression(s) as
43314 either 3, 6 or 7 special statements inside of the artificial function's
43315 body. The first two statements are DECL_EXPRs for the artificial
43316 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
43317 expression that uses those variables.
43318 If there was any INITIALIZER clause, this is followed by further statements,
43319 the fourth and fifth statements are DECL_EXPRs for the artificial
43320 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
43321 constructor variant (first token after open paren is not omp_priv),
43322 then the sixth statement is a statement with the function call expression
43323 that uses the OMP_PRIV and optionally OMP_ORIG variable.
43324 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
43325 to initialize the OMP_PRIV artificial variable and there is seventh
43326 statement, a DECL_EXPR of the OMP_PRIV statement again. */
43329 cp_parser_omp_declare_reduction_exprs (tree fndecl
, cp_parser
*parser
)
43331 tree type
= TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl
)));
43332 gcc_assert (TYPE_REF_P (type
));
43333 type
= TREE_TYPE (type
);
43334 tree omp_out
= build_lang_decl (VAR_DECL
, get_identifier ("omp_out"), type
);
43335 DECL_ARTIFICIAL (omp_out
) = 1;
43336 pushdecl (omp_out
);
43337 add_decl_expr (omp_out
);
43338 tree omp_in
= build_lang_decl (VAR_DECL
, get_identifier ("omp_in"), type
);
43339 DECL_ARTIFICIAL (omp_in
) = 1;
43341 add_decl_expr (omp_in
);
43343 tree omp_priv
= NULL_TREE
, omp_orig
= NULL_TREE
, initializer
= NULL_TREE
;
43345 keep_next_level (true);
43346 tree block
= begin_omp_structured_block ();
43347 combiner
= cp_parser_expression (parser
);
43348 finish_expr_stmt (combiner
);
43349 block
= finish_omp_structured_block (block
);
43350 if (processing_template_decl
)
43351 block
= build_stmt (input_location
, EXPR_STMT
, block
);
43354 if (!cp_parser_require (parser
, CPP_CLOSE_PAREN
, RT_CLOSE_PAREN
))
43357 const char *p
= "";
43358 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43360 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43361 p
= IDENTIFIER_POINTER (id
);
43364 if (strcmp (p
, "initializer") == 0)
43366 cp_lexer_consume_token (parser
->lexer
);
43367 matching_parens parens
;
43368 if (!parens
.require_open (parser
))
43372 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43374 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43375 p
= IDENTIFIER_POINTER (id
);
43378 omp_priv
= build_lang_decl (VAR_DECL
, get_identifier ("omp_priv"), type
);
43379 DECL_ARTIFICIAL (omp_priv
) = 1;
43380 pushdecl (omp_priv
);
43381 add_decl_expr (omp_priv
);
43382 omp_orig
= build_lang_decl (VAR_DECL
, get_identifier ("omp_orig"), type
);
43383 DECL_ARTIFICIAL (omp_orig
) = 1;
43384 pushdecl (omp_orig
);
43385 add_decl_expr (omp_orig
);
43387 keep_next_level (true);
43388 block
= begin_omp_structured_block ();
43391 if (strcmp (p
, "omp_priv") == 0)
43393 bool is_direct_init
, is_non_constant_init
;
43395 cp_lexer_consume_token (parser
->lexer
);
43396 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
43397 if (cp_lexer_next_token_is (parser
->lexer
, CPP_CLOSE_PAREN
)
43398 || (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
43399 && cp_lexer_peek_nth_token (parser
->lexer
, 2)->type
43401 && cp_lexer_peek_nth_token (parser
->lexer
, 3)->type
43402 == CPP_CLOSE_PAREN
))
43404 finish_omp_structured_block (block
);
43405 error ("invalid initializer clause");
43408 initializer
= cp_parser_initializer (parser
, &is_direct_init
,
43409 &is_non_constant_init
);
43410 cp_finish_decl (omp_priv
, initializer
, !is_non_constant_init
,
43411 NULL_TREE
, LOOKUP_ONLYCONVERTING
);
43415 cp_parser_parse_tentatively (parser
);
43416 /* Don't create location wrapper nodes here. */
43417 auto_suppress_location_wrappers sentinel
;
43418 tree fn_name
= cp_parser_id_expression (parser
, /*template_p=*/false,
43419 /*check_dependency_p=*/true,
43420 /*template_p=*/NULL
,
43421 /*declarator_p=*/false,
43422 /*optional_p=*/false);
43423 vec
<tree
, va_gc
> *args
;
43424 if (fn_name
== error_mark_node
43425 || cp_parser_error_occurred (parser
)
43426 || !cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
)
43427 || ((args
= cp_parser_parenthesized_expression_list
43428 (parser
, non_attr
, /*cast_p=*/false,
43429 /*allow_expansion_p=*/true,
43430 /*non_constant_p=*/NULL
)),
43431 cp_parser_error_occurred (parser
)))
43433 finish_omp_structured_block (block
);
43434 cp_parser_abort_tentative_parse (parser
);
43435 cp_parser_error (parser
, "expected id-expression (arguments)");
43440 FOR_EACH_VEC_SAFE_ELT (args
, i
, arg
)
43441 if (arg
== omp_priv
43442 || (TREE_CODE (arg
) == ADDR_EXPR
43443 && TREE_OPERAND (arg
, 0) == omp_priv
))
43445 cp_parser_abort_tentative_parse (parser
);
43446 if (arg
== NULL_TREE
)
43447 error ("one of the initializer call arguments should be %<omp_priv%>"
43448 " or %<&omp_priv%>");
43449 initializer
= cp_parser_postfix_expression (parser
, false, false, false,
43451 finish_expr_stmt (initializer
);
43454 block
= finish_omp_structured_block (block
);
43455 cp_walk_tree (&block
, cp_remove_omp_priv_cleanup_stmt
, omp_priv
, NULL
);
43456 if (processing_template_decl
)
43457 block
= build_stmt (input_location
, EXPR_STMT
, block
);
43461 add_decl_expr (omp_orig
);
43463 if (!parens
.require_close (parser
))
43467 if (!cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA_EOL
))
43468 cp_parser_required_error (parser
, RT_PRAGMA_EOL
, /*keyword=*/false,
43475 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43476 initializer-clause[opt] new-line
43478 initializer-clause:
43479 initializer (omp_priv initializer)
43480 initializer (function-name (argument-list)) */
43483 cp_parser_omp_declare_reduction (cp_parser
*parser
, cp_token
*pragma_tok
,
43484 enum pragma_context
)
43486 auto_vec
<tree
> types
;
43487 enum tree_code reduc_code
= ERROR_MARK
;
43488 tree reduc_id
= NULL_TREE
, orig_reduc_id
= NULL_TREE
, type
;
43490 cp_token
*first_token
;
43491 cp_token_cache
*cp
;
43495 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
43496 p
= obstack_alloc (&declarator_obstack
, 0);
43498 if (!cp_parser_require (parser
, CPP_OPEN_PAREN
, RT_OPEN_PAREN
))
43501 switch (cp_lexer_peek_token (parser
->lexer
)->type
)
43504 reduc_code
= PLUS_EXPR
;
43507 reduc_code
= MULT_EXPR
;
43510 reduc_code
= MINUS_EXPR
;
43513 reduc_code
= BIT_AND_EXPR
;
43516 reduc_code
= BIT_XOR_EXPR
;
43519 reduc_code
= BIT_IOR_EXPR
;
43522 reduc_code
= TRUTH_ANDIF_EXPR
;
43525 reduc_code
= TRUTH_ORIF_EXPR
;
43528 reduc_id
= orig_reduc_id
= cp_parser_identifier (parser
);
43531 cp_parser_error (parser
, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
43532 "%<|%>, %<&&%>, %<||%> or identifier");
43536 if (reduc_code
!= ERROR_MARK
)
43537 cp_lexer_consume_token (parser
->lexer
);
43539 reduc_id
= omp_reduction_id (reduc_code
, reduc_id
, NULL_TREE
);
43540 if (reduc_id
== error_mark_node
)
43543 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
))
43546 /* Types may not be defined in declare reduction type list. */
43547 const char *saved_message
;
43548 saved_message
= parser
->type_definition_forbidden_message
;
43549 parser
->type_definition_forbidden_message
43550 = G_("types may not be defined in declare reduction type list");
43551 bool saved_colon_corrects_to_scope_p
;
43552 saved_colon_corrects_to_scope_p
= parser
->colon_corrects_to_scope_p
;
43553 parser
->colon_corrects_to_scope_p
= false;
43554 bool saved_colon_doesnt_start_class_def_p
;
43555 saved_colon_doesnt_start_class_def_p
43556 = parser
->colon_doesnt_start_class_def_p
;
43557 parser
->colon_doesnt_start_class_def_p
= true;
43561 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43562 type
= cp_parser_type_id (parser
);
43563 if (type
== error_mark_node
)
43565 else if (ARITHMETIC_TYPE_P (type
)
43566 && (orig_reduc_id
== NULL_TREE
43567 || (TREE_CODE (type
) != COMPLEX_TYPE
43568 && (id_equal (orig_reduc_id
, "min")
43569 || id_equal (orig_reduc_id
, "max")))))
43570 error_at (loc
, "predeclared arithmetic type %qT in "
43571 "%<#pragma omp declare reduction%>", type
);
43572 else if (FUNC_OR_METHOD_TYPE_P (type
)
43573 || TREE_CODE (type
) == ARRAY_TYPE
)
43574 error_at (loc
, "function or array type %qT in "
43575 "%<#pragma omp declare reduction%>", type
);
43576 else if (TYPE_REF_P (type
))
43577 error_at (loc
, "reference type %qT in "
43578 "%<#pragma omp declare reduction%>", type
);
43579 else if (TYPE_QUALS_NO_ADDR_SPACE (type
))
43580 error_at (loc
, "%<const%>, %<volatile%> or %<__restrict%>-qualified "
43581 "type %qT in %<#pragma omp declare reduction%>", type
);
43583 types
.safe_push (type
);
43585 if (cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
43586 cp_lexer_consume_token (parser
->lexer
);
43591 /* Restore the saved message. */
43592 parser
->type_definition_forbidden_message
= saved_message
;
43593 parser
->colon_corrects_to_scope_p
= saved_colon_corrects_to_scope_p
;
43594 parser
->colon_doesnt_start_class_def_p
43595 = saved_colon_doesnt_start_class_def_p
;
43597 if (!cp_parser_require (parser
, CPP_COLON
, RT_COLON
)
43598 || types
.is_empty ())
43601 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43605 first_token
= cp_lexer_peek_token (parser
->lexer
);
43608 FOR_EACH_VEC_ELT (types
, i
, type
)
43611 = build_function_type_list (void_type_node
,
43612 cp_build_reference_type (type
, false),
43614 tree this_reduc_id
= reduc_id
;
43615 if (!dependent_type_p (type
))
43616 this_reduc_id
= omp_reduction_id (ERROR_MARK
, reduc_id
, type
);
43617 tree fndecl
= build_lang_decl (FUNCTION_DECL
, this_reduc_id
, fntype
);
43618 DECL_SOURCE_LOCATION (fndecl
) = pragma_tok
->location
;
43619 DECL_ARTIFICIAL (fndecl
) = 1;
43620 DECL_EXTERNAL (fndecl
) = 1;
43621 DECL_DECLARED_INLINE_P (fndecl
) = 1;
43622 DECL_IGNORED_P (fndecl
) = 1;
43623 DECL_OMP_DECLARE_REDUCTION_P (fndecl
) = 1;
43624 SET_DECL_ASSEMBLER_NAME (fndecl
, get_identifier ("<udr>"));
43625 DECL_ATTRIBUTES (fndecl
)
43626 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE
,
43627 DECL_ATTRIBUTES (fndecl
));
43628 bool block_scope
= false;
43629 if (current_function_decl
)
43631 block_scope
= true;
43632 DECL_CONTEXT (fndecl
) = current_function_decl
;
43633 DECL_LOCAL_DECL_P (fndecl
) = true;
43636 if (processing_template_decl
)
43637 fndecl
= push_template_decl (fndecl
);
43641 if (!processing_template_decl
)
43644 else if (current_class_type
)
43648 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
43649 cp_lexer_consume_token (parser
->lexer
);
43650 cp
= cp_token_cache_new (first_token
,
43651 cp_lexer_peek_nth_token (parser
->lexer
,
43654 DECL_STATIC_FUNCTION_P (fndecl
) = 1;
43655 finish_member_declaration (fndecl
);
43656 DECL_PENDING_INLINE_INFO (fndecl
) = cp
;
43657 DECL_PENDING_INLINE_P (fndecl
) = 1;
43658 vec_safe_push (unparsed_funs_with_definitions
, fndecl
);
43663 DECL_CONTEXT (fndecl
) = current_namespace
;
43664 tree d
= pushdecl (fndecl
);
43665 /* We should never meet a matched duplicate decl. */
43666 gcc_checking_assert (d
== error_mark_node
|| d
== fndecl
);
43669 tree block
= NULL_TREE
;
43671 start_preparsed_function (fndecl
, NULL_TREE
, SF_PRE_PARSED
);
43673 block
= begin_omp_structured_block ();
43676 cp_parser_push_lexer_for_tokens (parser
, cp
);
43677 parser
->lexer
->in_pragma
= true;
43680 bool ok
= cp_parser_omp_declare_reduction_exprs (fndecl
, parser
);
43683 cp_parser_pop_lexer (parser
);
43685 finish_function (/*inline_p=*/false);
43688 DECL_CONTEXT (fndecl
) = current_function_decl
;
43689 if (DECL_TEMPLATE_INFO (fndecl
))
43690 DECL_CONTEXT (DECL_TI_TEMPLATE (fndecl
)) = current_function_decl
;
43697 block
= finish_omp_structured_block (block
);
43698 if (TREE_CODE (block
) == BIND_EXPR
)
43699 DECL_SAVED_TREE (fndecl
) = BIND_EXPR_BODY (block
);
43700 else if (TREE_CODE (block
) == STATEMENT_LIST
)
43701 DECL_SAVED_TREE (fndecl
) = block
;
43702 if (processing_template_decl
)
43703 add_decl_expr (fndecl
);
43706 cp_check_omp_declare_reduction (fndecl
);
43707 if (cp
== NULL
&& types
.length () > 1)
43708 cp
= cp_token_cache_new (first_token
,
43709 cp_lexer_peek_nth_token (parser
->lexer
, 2));
43710 if (errs
!= errorcount
)
43714 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43717 /* Free any declarators allocated. */
43718 obstack_free (&declarator_obstack
, p
);
43722 #pragma omp declare simd declare-simd-clauses[optseq] new-line
43723 #pragma omp declare reduction (reduction-id : typename-list : expression) \
43724 initializer-clause[opt] new-line
43725 #pragma omp declare target new-line
43728 #pragma omp declare variant (identifier) match (context-selector) */
43731 cp_parser_omp_declare (cp_parser
*parser
, cp_token
*pragma_tok
,
43732 enum pragma_context context
)
43734 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43736 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43737 const char *p
= IDENTIFIER_POINTER (id
);
43739 if (strcmp (p
, "simd") == 0)
43741 cp_lexer_consume_token (parser
->lexer
);
43742 cp_parser_omp_declare_simd (parser
, pragma_tok
,
43746 if (flag_openmp
&& strcmp (p
, "variant") == 0)
43748 cp_lexer_consume_token (parser
->lexer
);
43749 cp_parser_omp_declare_simd (parser
, pragma_tok
,
43753 cp_ensure_no_omp_declare_simd (parser
);
43754 if (strcmp (p
, "reduction") == 0)
43756 cp_lexer_consume_token (parser
->lexer
);
43757 cp_parser_omp_declare_reduction (parser
, pragma_tok
,
43761 if (!flag_openmp
) /* flag_openmp_simd */
43763 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43766 if (strcmp (p
, "target") == 0)
43768 cp_lexer_consume_token (parser
->lexer
);
43769 cp_parser_omp_declare_target (parser
, pragma_tok
);
43773 cp_parser_error (parser
, "expected %<simd%>, %<reduction%>, "
43774 "%<target%> or %<variant%>");
43775 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43780 #pragma omp requires clauses[optseq] new-line */
43783 cp_parser_omp_requires (cp_parser
*parser
, cp_token
*pragma_tok
)
43786 enum omp_requires new_req
= (enum omp_requires
) 0;
43788 location_t loc
= pragma_tok
->location
;
43789 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
43791 if (!first
&& cp_lexer_next_token_is (parser
->lexer
, CPP_COMMA
))
43792 cp_lexer_consume_token (parser
->lexer
);
43796 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43798 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43799 const char *p
= IDENTIFIER_POINTER (id
);
43800 location_t cloc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43801 enum omp_requires this_req
= (enum omp_requires
) 0;
43803 if (!strcmp (p
, "unified_address"))
43804 this_req
= OMP_REQUIRES_UNIFIED_ADDRESS
;
43805 else if (!strcmp (p
, "unified_shared_memory"))
43806 this_req
= OMP_REQUIRES_UNIFIED_SHARED_MEMORY
;
43807 else if (!strcmp (p
, "dynamic_allocators"))
43808 this_req
= OMP_REQUIRES_DYNAMIC_ALLOCATORS
;
43809 else if (!strcmp (p
, "reverse_offload"))
43810 this_req
= OMP_REQUIRES_REVERSE_OFFLOAD
;
43811 else if (!strcmp (p
, "atomic_default_mem_order"))
43813 cp_lexer_consume_token (parser
->lexer
);
43815 matching_parens parens
;
43816 if (parens
.require_open (parser
))
43818 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43820 id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43821 p
= IDENTIFIER_POINTER (id
);
43823 if (!strcmp (p
, "seq_cst"))
43825 = (enum omp_requires
) OMP_MEMORY_ORDER_SEQ_CST
;
43826 else if (!strcmp (p
, "relaxed"))
43828 = (enum omp_requires
) OMP_MEMORY_ORDER_RELAXED
;
43829 else if (!strcmp (p
, "acq_rel"))
43831 = (enum omp_requires
) OMP_MEMORY_ORDER_ACQ_REL
;
43835 error_at (cp_lexer_peek_token (parser
->lexer
)->location
,
43836 "expected %<seq_cst%>, %<relaxed%> or "
43838 if (cp_lexer_nth_token_is (parser
->lexer
, 2,
43840 cp_lexer_consume_token (parser
->lexer
);
43843 cp_lexer_consume_token (parser
->lexer
);
43845 if (!parens
.require_close (parser
))
43846 cp_parser_skip_to_closing_parenthesis (parser
,
43847 /*recovering=*/true,
43848 /*or_comma=*/false,
43854 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43862 error_at (cloc
, "expected %<unified_address%>, "
43863 "%<unified_shared_memory%>, "
43864 "%<dynamic_allocators%>, "
43865 "%<reverse_offload%> "
43866 "or %<atomic_default_mem_order%> clause");
43867 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
43871 sorry_at (cloc
, "%qs clause on %<requires%> directive not "
43872 "supported yet", p
);
43874 cp_lexer_consume_token (parser
->lexer
);
43877 if ((this_req
& ~OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
43879 if ((this_req
& new_req
) != 0)
43880 error_at (cloc
, "too many %qs clauses", p
);
43881 if (this_req
!= OMP_REQUIRES_DYNAMIC_ALLOCATORS
43882 && (omp_requires_mask
& OMP_REQUIRES_TARGET_USED
) != 0)
43883 error_at (cloc
, "%qs clause used lexically after first "
43884 "target construct or offloading API", p
);
43886 else if ((new_req
& OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
43888 error_at (cloc
, "too many %qs clauses",
43889 "atomic_default_mem_order");
43890 this_req
= (enum omp_requires
) 0;
43892 else if ((omp_requires_mask
43893 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
) != 0)
43895 error_at (cloc
, "more than one %<atomic_default_mem_order%>"
43896 " clause in a single compilation unit");
43898 = (enum omp_requires
)
43900 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER
);
43902 else if ((omp_requires_mask
43903 & OMP_REQUIRES_ATOMIC_DEFAULT_MEM_ORDER_USED
) != 0)
43904 error_at (cloc
, "%<atomic_default_mem_order%> clause used "
43905 "lexically after first %<atomic%> construct "
43906 "without memory order clause");
43907 new_req
= (enum omp_requires
) (new_req
| this_req
);
43909 = (enum omp_requires
) (omp_requires_mask
| this_req
);
43915 cp_parser_require_pragma_eol (parser
, pragma_tok
);
43918 error_at (loc
, "%<pragma omp requires%> requires at least one clause");
43924 #pragma omp taskloop taskloop-clause[optseq] new-line
43927 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
43930 #define OMP_TASKLOOP_CLAUSE_MASK \
43931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
43932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
43933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
43934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
43935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
43936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
43937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
43938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
43939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
43940 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
43941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
43942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
43943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
43944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY) \
43945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALLOCATE) \
43946 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
43947 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IN_REDUCTION))
43950 cp_parser_omp_taskloop (cp_parser
*parser
, cp_token
*pragma_tok
,
43951 char *p_name
, omp_clause_mask mask
, tree
*cclauses
,
43954 tree clauses
, sb
, ret
;
43956 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
43958 strcat (p_name
, " taskloop");
43959 mask
|= OMP_TASKLOOP_CLAUSE_MASK
;
43960 /* #pragma omp parallel master taskloop{, simd} disallow in_reduction
43962 if ((mask
& (OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_NUM_THREADS
)) != 0)
43963 mask
&= ~(OMP_CLAUSE_MASK_1
<< PRAGMA_OMP_CLAUSE_IN_REDUCTION
);
43965 if (cp_lexer_next_token_is (parser
->lexer
, CPP_NAME
))
43967 tree id
= cp_lexer_peek_token (parser
->lexer
)->u
.value
;
43968 const char *p
= IDENTIFIER_POINTER (id
);
43970 if (strcmp (p
, "simd") == 0)
43972 tree cclauses_buf
[C_OMP_CLAUSE_SPLIT_COUNT
];
43973 if (cclauses
== NULL
)
43974 cclauses
= cclauses_buf
;
43976 cp_lexer_consume_token (parser
->lexer
);
43977 if (!flag_openmp
) /* flag_openmp_simd */
43978 return cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43980 sb
= begin_omp_structured_block ();
43981 save
= cp_parser_begin_omp_structured_block (parser
);
43982 ret
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
,
43984 cp_parser_end_omp_structured_block (parser
, save
);
43985 tree body
= finish_omp_structured_block (sb
);
43988 ret
= make_node (OMP_TASKLOOP
);
43989 TREE_TYPE (ret
) = void_type_node
;
43990 OMP_FOR_BODY (ret
) = body
;
43991 OMP_FOR_CLAUSES (ret
) = cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
43992 SET_EXPR_LOCATION (ret
, loc
);
43997 if (!flag_openmp
) /* flag_openmp_simd */
43999 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44003 clauses
= cp_parser_omp_all_clauses (parser
, mask
, p_name
, pragma_tok
,
44007 cp_omp_split_clauses (loc
, OMP_TASKLOOP
, mask
, clauses
, cclauses
);
44008 clauses
= cclauses
[C_OMP_CLAUSE_SPLIT_TASKLOOP
];
44011 keep_next_level (true);
44012 sb
= begin_omp_structured_block ();
44013 save
= cp_parser_begin_omp_structured_block (parser
);
44015 ret
= cp_parser_omp_for_loop (parser
, OMP_TASKLOOP
, clauses
, cclauses
,
44018 cp_parser_end_omp_structured_block (parser
, save
);
44019 add_stmt (finish_omp_for_block (finish_omp_structured_block (sb
), ret
));
44026 # pragma acc routine oacc-routine-clause[optseq] new-line
44027 function-definition
44029 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
44032 #define OACC_ROUTINE_CLAUSE_MASK \
44033 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
44034 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
44035 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
44036 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
44039 /* Parse the OpenACC routine pragma. This has an optional '( name )'
44040 component, which must resolve to a declared namespace-scope
44041 function. The clauses are either processed directly (for a named
44042 function), or defered until the immediatley following declaration
44046 cp_parser_oacc_routine (cp_parser
*parser
, cp_token
*pragma_tok
,
44047 enum pragma_context context
)
44049 gcc_checking_assert (context
== pragma_external
);
44050 /* The checking for "another pragma following this one" in the "no optional
44051 '( name )'" case makes sure that we dont re-enter. */
44052 gcc_checking_assert (parser
->oacc_routine
== NULL
);
44054 cp_oacc_routine_data data
;
44055 data
.error_seen
= false;
44056 data
.fndecl_seen
= false;
44057 data
.tokens
= vNULL
;
44058 data
.clauses
= NULL_TREE
;
44059 data
.loc
= pragma_tok
->location
;
44060 /* It is safe to take the address of a local variable; it will only be
44061 used while this scope is live. */
44062 parser
->oacc_routine
= &data
;
44064 /* Look for optional '( name )'. */
44065 if (cp_lexer_next_token_is (parser
->lexer
, CPP_OPEN_PAREN
))
44067 matching_parens parens
;
44068 parens
.consume_open (parser
); /* '(' */
44070 /* We parse the name as an id-expression. If it resolves to
44071 anything other than a non-overloaded function at namespace
44072 scope, it's an error. */
44073 location_t name_loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
44074 tree name
= cp_parser_id_expression (parser
,
44075 /*template_keyword_p=*/false,
44076 /*check_dependency_p=*/false,
44077 /*template_p=*/NULL
,
44078 /*declarator_p=*/false,
44079 /*optional_p=*/false);
44080 tree decl
= (identifier_p (name
)
44081 ? cp_parser_lookup_name_simple (parser
, name
, name_loc
)
44083 if (name
!= error_mark_node
&& decl
== error_mark_node
)
44084 cp_parser_name_lookup_error (parser
, name
, decl
, NLE_NULL
, name_loc
);
44086 if (decl
== error_mark_node
44087 || !parens
.require_close (parser
))
44089 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44090 parser
->oacc_routine
= NULL
;
44095 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
44096 "#pragma acc routine",
44097 cp_lexer_peek_token (parser
->lexer
));
44098 /* The clauses are in reverse order; fix that to make later diagnostic
44099 emission easier. */
44100 data
.clauses
= nreverse (data
.clauses
);
44102 if (decl
&& is_overloaded_fn (decl
)
44103 && (TREE_CODE (decl
) != FUNCTION_DECL
44104 || DECL_FUNCTION_TEMPLATE_P (decl
)))
44106 error_at (name_loc
,
44107 "%<#pragma acc routine%> names a set of overloads");
44108 parser
->oacc_routine
= NULL
;
44112 /* Perhaps we should use the same rule as declarations in different
44114 if (!DECL_NAMESPACE_SCOPE_P (decl
))
44116 error_at (name_loc
,
44117 "%qD does not refer to a namespace scope function", decl
);
44118 parser
->oacc_routine
= NULL
;
44122 if (TREE_CODE (decl
) != FUNCTION_DECL
)
44124 error_at (name_loc
, "%qD does not refer to a function", decl
);
44125 parser
->oacc_routine
= NULL
;
44129 cp_finalize_oacc_routine (parser
, decl
, false);
44130 parser
->oacc_routine
= NULL
;
44132 else /* No optional '( name )'. */
44134 /* Store away all pragma tokens. */
44135 while (cp_lexer_next_token_is_not (parser
->lexer
, CPP_PRAGMA_EOL
))
44136 cp_lexer_consume_token (parser
->lexer
);
44137 cp_parser_require_pragma_eol (parser
, pragma_tok
);
44138 struct cp_token_cache
*cp
44139 = cp_token_cache_new (pragma_tok
, cp_lexer_peek_token (parser
->lexer
));
44140 parser
->oacc_routine
->tokens
.safe_push (cp
);
44142 /* Emit a helpful diagnostic if there's another pragma following this
44144 if (cp_lexer_next_token_is (parser
->lexer
, CPP_PRAGMA
))
44146 cp_ensure_no_oacc_routine (parser
);
44147 data
.tokens
.release ();
44148 /* ..., and then just keep going. */
44152 /* We only have to consider the pragma_external case here. */
44153 cp_parser_declaration (parser
, NULL_TREE
);
44154 if (parser
->oacc_routine
44155 && !parser
->oacc_routine
->fndecl_seen
)
44156 cp_ensure_no_oacc_routine (parser
);
44158 parser
->oacc_routine
= NULL
;
44159 data
.tokens
.release ();
44163 /* Finalize #pragma acc routine clauses after direct declarator has
44167 cp_parser_late_parsing_oacc_routine (cp_parser
*parser
, tree attrs
)
44169 struct cp_token_cache
*ce
;
44170 cp_oacc_routine_data
*data
= parser
->oacc_routine
;
44172 if (!data
->error_seen
&& data
->fndecl_seen
)
44174 error_at (data
->loc
,
44175 "%<#pragma acc routine%> not immediately followed by "
44176 "a single function declaration or definition");
44177 data
->error_seen
= true;
44179 if (data
->error_seen
)
44182 gcc_checking_assert (data
->tokens
.length () == 1);
44183 ce
= data
->tokens
[0];
44185 cp_parser_push_lexer_for_tokens (parser
, ce
);
44186 parser
->lexer
->in_pragma
= true;
44187 gcc_assert (cp_lexer_peek_token (parser
->lexer
)->type
== CPP_PRAGMA
);
44189 cp_token
*pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
44190 gcc_checking_assert (parser
->oacc_routine
->clauses
== NULL_TREE
);
44191 parser
->oacc_routine
->clauses
44192 = cp_parser_oacc_all_clauses (parser
, OACC_ROUTINE_CLAUSE_MASK
,
44193 "#pragma acc routine", pragma_tok
);
44194 /* The clauses are in reverse order; fix that to make later diagnostic
44195 emission easier. */
44196 parser
->oacc_routine
->clauses
= nreverse (parser
->oacc_routine
->clauses
);
44197 cp_parser_pop_lexer (parser
);
44198 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
44204 /* Apply any saved OpenACC routine clauses to a just-parsed
44208 cp_finalize_oacc_routine (cp_parser
*parser
, tree fndecl
, bool is_defn
)
44210 if (__builtin_expect (parser
->oacc_routine
!= NULL
, 0))
44212 /* Keep going if we're in error reporting mode. */
44213 if (parser
->oacc_routine
->error_seen
44214 || fndecl
== error_mark_node
)
44217 if (parser
->oacc_routine
->fndecl_seen
)
44219 error_at (parser
->oacc_routine
->loc
,
44220 "%<#pragma acc routine%> not immediately followed by"
44221 " a single function declaration or definition");
44222 parser
->oacc_routine
= NULL
;
44225 if (TREE_CODE (fndecl
) != FUNCTION_DECL
)
44227 cp_ensure_no_oacc_routine (parser
);
44232 = oacc_verify_routine_clauses (fndecl
, &parser
->oacc_routine
->clauses
,
44233 parser
->oacc_routine
->loc
,
44234 "#pragma acc routine");
44235 if (compatible
< 0)
44237 parser
->oacc_routine
= NULL
;
44240 if (compatible
> 0)
44245 if (TREE_USED (fndecl
) || (!is_defn
&& DECL_SAVED_TREE (fndecl
)))
44247 error_at (parser
->oacc_routine
->loc
,
44249 ? G_("%<#pragma acc routine%> must be applied before"
44251 : G_("%<#pragma acc routine%> must be applied before"
44253 parser
->oacc_routine
= NULL
;
44257 /* Set the routine's level of parallelism. */
44258 tree dims
= oacc_build_routine_dims (parser
->oacc_routine
->clauses
);
44259 oacc_replace_fn_attrib (fndecl
, dims
);
44261 /* Add an "omp declare target" attribute. */
44262 DECL_ATTRIBUTES (fndecl
)
44263 = tree_cons (get_identifier ("omp declare target"),
44264 parser
->oacc_routine
->clauses
,
44265 DECL_ATTRIBUTES (fndecl
));
44268 /* Don't unset parser->oacc_routine here: we may still need it to
44269 diagnose wrong usage. But, remember that we've used this "#pragma acc
44271 parser
->oacc_routine
->fndecl_seen
= true;
44275 /* Main entry point to OpenMP statement pragmas. */
44278 cp_parser_omp_construct (cp_parser
*parser
, cp_token
*pragma_tok
, bool *if_p
)
44281 char p_name
[sizeof "#pragma omp teams distribute parallel for simd"];
44282 omp_clause_mask
mask (0);
44284 switch (cp_parser_pragma_kind (pragma_tok
))
44286 case PRAGMA_OACC_ATOMIC
:
44287 cp_parser_omp_atomic (parser
, pragma_tok
, true);
44289 case PRAGMA_OACC_CACHE
:
44290 stmt
= cp_parser_oacc_cache (parser
, pragma_tok
);
44292 case PRAGMA_OACC_DATA
:
44293 stmt
= cp_parser_oacc_data (parser
, pragma_tok
, if_p
);
44295 case PRAGMA_OACC_ENTER_DATA
:
44296 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, true);
44298 case PRAGMA_OACC_EXIT_DATA
:
44299 stmt
= cp_parser_oacc_enter_exit_data (parser
, pragma_tok
, false);
44301 case PRAGMA_OACC_HOST_DATA
:
44302 stmt
= cp_parser_oacc_host_data (parser
, pragma_tok
, if_p
);
44304 case PRAGMA_OACC_KERNELS
:
44305 case PRAGMA_OACC_PARALLEL
:
44306 case PRAGMA_OACC_SERIAL
:
44307 strcpy (p_name
, "#pragma acc");
44308 stmt
= cp_parser_oacc_compute (parser
, pragma_tok
, p_name
, if_p
);
44310 case PRAGMA_OACC_LOOP
:
44311 strcpy (p_name
, "#pragma acc");
44312 stmt
= cp_parser_oacc_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
44315 case PRAGMA_OACC_UPDATE
:
44316 stmt
= cp_parser_oacc_update (parser
, pragma_tok
);
44318 case PRAGMA_OACC_WAIT
:
44319 stmt
= cp_parser_oacc_wait (parser
, pragma_tok
);
44321 case PRAGMA_OMP_ALLOCATE
:
44322 cp_parser_omp_allocate (parser
, pragma_tok
);
44324 case PRAGMA_OMP_ATOMIC
:
44325 cp_parser_omp_atomic (parser
, pragma_tok
, false);
44327 case PRAGMA_OMP_CRITICAL
:
44328 stmt
= cp_parser_omp_critical (parser
, pragma_tok
, if_p
);
44330 case PRAGMA_OMP_DISTRIBUTE
:
44331 strcpy (p_name
, "#pragma omp");
44332 stmt
= cp_parser_omp_distribute (parser
, pragma_tok
, p_name
, mask
, NULL
,
44335 case PRAGMA_OMP_FOR
:
44336 strcpy (p_name
, "#pragma omp");
44337 stmt
= cp_parser_omp_for (parser
, pragma_tok
, p_name
, mask
, NULL
,
44340 case PRAGMA_OMP_LOOP
:
44341 strcpy (p_name
, "#pragma omp");
44342 stmt
= cp_parser_omp_loop (parser
, pragma_tok
, p_name
, mask
, NULL
,
44345 case PRAGMA_OMP_MASTER
:
44346 strcpy (p_name
, "#pragma omp");
44347 stmt
= cp_parser_omp_master (parser
, pragma_tok
, p_name
, mask
, NULL
,
44350 case PRAGMA_OMP_PARALLEL
:
44351 strcpy (p_name
, "#pragma omp");
44352 stmt
= cp_parser_omp_parallel (parser
, pragma_tok
, p_name
, mask
, NULL
,
44355 case PRAGMA_OMP_SECTIONS
:
44356 strcpy (p_name
, "#pragma omp");
44357 stmt
= cp_parser_omp_sections (parser
, pragma_tok
, p_name
, mask
, NULL
);
44359 case PRAGMA_OMP_SIMD
:
44360 strcpy (p_name
, "#pragma omp");
44361 stmt
= cp_parser_omp_simd (parser
, pragma_tok
, p_name
, mask
, NULL
,
44364 case PRAGMA_OMP_SINGLE
:
44365 stmt
= cp_parser_omp_single (parser
, pragma_tok
, if_p
);
44367 case PRAGMA_OMP_TASK
:
44368 stmt
= cp_parser_omp_task (parser
, pragma_tok
, if_p
);
44370 case PRAGMA_OMP_TASKGROUP
:
44371 stmt
= cp_parser_omp_taskgroup (parser
, pragma_tok
, if_p
);
44373 case PRAGMA_OMP_TASKLOOP
:
44374 strcpy (p_name
, "#pragma omp");
44375 stmt
= cp_parser_omp_taskloop (parser
, pragma_tok
, p_name
, mask
, NULL
,
44378 case PRAGMA_OMP_TEAMS
:
44379 strcpy (p_name
, "#pragma omp");
44380 stmt
= cp_parser_omp_teams (parser
, pragma_tok
, p_name
, mask
, NULL
,
44384 gcc_unreachable ();
44387 protected_set_expr_location (stmt
, pragma_tok
->location
);
44390 /* Transactional Memory parsing routines. */
44392 /* Parse a transaction attribute.
44398 We use this instead of cp_parser_attributes_opt for transactions to avoid
44399 the pedwarn in C++98 mode. */
44402 cp_parser_txn_attribute_opt (cp_parser
*parser
)
44405 tree attr_name
, attr
= NULL
;
44407 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_ATTRIBUTE
))
44408 return cp_parser_attributes_opt (parser
);
44410 if (cp_lexer_next_token_is_not (parser
->lexer
, CPP_OPEN_SQUARE
))
44412 cp_lexer_consume_token (parser
->lexer
);
44413 if (!cp_parser_require (parser
, CPP_OPEN_SQUARE
, RT_OPEN_SQUARE
))
44416 token
= cp_lexer_peek_token (parser
->lexer
);
44417 if (token
->type
== CPP_NAME
|| token
->type
== CPP_KEYWORD
)
44419 token
= cp_lexer_consume_token (parser
->lexer
);
44421 attr_name
= (token
->type
== CPP_KEYWORD
44422 /* For keywords, use the canonical spelling,
44423 not the parsed identifier. */
44424 ? ridpointers
[(int) token
->keyword
]
44426 attr
= build_tree_list (attr_name
, NULL_TREE
);
44429 cp_parser_error (parser
, "expected identifier");
44431 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
44433 cp_parser_require (parser
, CPP_CLOSE_SQUARE
, RT_CLOSE_SQUARE
);
44437 /* Parse a __transaction_atomic or __transaction_relaxed statement.
44439 transaction-statement:
44440 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
44442 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
44446 cp_parser_transaction (cp_parser
*parser
, cp_token
*token
)
44448 unsigned char old_in
= parser
->in_transaction
;
44449 unsigned char this_in
= 1, new_in
;
44450 enum rid keyword
= token
->keyword
;
44451 tree stmt
, attrs
, noex
;
44453 cp_lexer_consume_token (parser
->lexer
);
44455 if (keyword
== RID_TRANSACTION_RELAXED
44456 || keyword
== RID_SYNCHRONIZED
)
44457 this_in
|= TM_STMT_ATTR_RELAXED
;
44460 attrs
= cp_parser_txn_attribute_opt (parser
);
44462 this_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
44465 /* Parse a noexcept specification. */
44466 if (keyword
== RID_ATOMIC_NOEXCEPT
)
44467 noex
= boolean_true_node
;
44468 else if (keyword
== RID_ATOMIC_CANCEL
)
44470 /* cancel-and-throw is unimplemented. */
44471 sorry ("%<atomic_cancel%>");
44475 noex
= cp_parser_noexcept_specification_opt (parser
,
44476 CP_PARSER_FLAGS_NONE
,
44477 /*require_constexpr=*/true,
44478 /*consumed_expr=*/NULL
,
44479 /*return_cond=*/true);
44481 /* Keep track if we're in the lexical scope of an outer transaction. */
44482 new_in
= this_in
| (old_in
& TM_STMT_ATTR_OUTER
);
44484 stmt
= begin_transaction_stmt (token
->location
, NULL
, this_in
);
44486 parser
->in_transaction
= new_in
;
44487 cp_parser_compound_statement (parser
, NULL
, BCS_TRANSACTION
, false);
44488 parser
->in_transaction
= old_in
;
44490 finish_transaction_stmt (stmt
, NULL
, this_in
, noex
);
44495 /* Parse a __transaction_atomic or __transaction_relaxed expression.
44497 transaction-expression:
44498 __transaction_atomic txn-noexcept-spec[opt] ( expression )
44499 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
44503 cp_parser_transaction_expression (cp_parser
*parser
, enum rid keyword
)
44505 unsigned char old_in
= parser
->in_transaction
;
44506 unsigned char this_in
= 1;
44510 location_t loc
= cp_lexer_peek_token (parser
->lexer
)->location
;
44512 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
44513 || keyword
== RID_TRANSACTION_RELAXED
);
44517 keyword
== RID_TRANSACTION_RELAXED
44518 ? G_("%<__transaction_relaxed%> without transactional memory "
44520 : G_("%<__transaction_atomic%> without transactional memory "
44521 "support enabled"));
44523 token
= cp_parser_require_keyword (parser
, keyword
,
44524 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
44525 : RT_TRANSACTION_RELAXED
));
44526 gcc_assert (token
!= NULL
);
44528 if (keyword
== RID_TRANSACTION_RELAXED
)
44529 this_in
|= TM_STMT_ATTR_RELAXED
;
44531 /* Set this early. This might mean that we allow transaction_cancel in
44532 an expression that we find out later actually has to be a constexpr.
44533 However, we expect that cxx_constant_value will be able to deal with
44534 this; also, if the noexcept has no constexpr, then what we parse next
44535 really is a transaction's body. */
44536 parser
->in_transaction
= this_in
;
44538 /* Parse a noexcept specification. */
44539 noex
= cp_parser_noexcept_specification_opt (parser
,
44540 CP_PARSER_FLAGS_NONE
,
44541 /*require_constexpr=*/false,
44543 /*return_cond=*/true);
44545 if (!noex
|| !noex_expr
44546 || cp_lexer_peek_token (parser
->lexer
)->type
== CPP_OPEN_PAREN
)
44548 matching_parens parens
;
44549 parens
.require_open (parser
);
44551 expr
= cp_parser_expression (parser
);
44552 expr
= finish_parenthesized_expr (expr
);
44554 parens
.require_close (parser
);
44558 /* The only expression that is available got parsed for the noexcept
44559 already. noexcept is true then. */
44561 noex
= boolean_true_node
;
44564 expr
= build_transaction_expr (token
->location
, expr
, this_in
, noex
);
44565 parser
->in_transaction
= old_in
;
44567 if (cp_parser_non_integral_constant_expression (parser
, NIC_TRANSACTION
))
44568 return error_mark_node
;
44570 return (flag_tm
? expr
: error_mark_node
);
44573 /* Parse a function-transaction-block.
44575 function-transaction-block:
44576 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
44578 __transaction_atomic txn-attribute[opt] function-try-block
44579 __transaction_relaxed ctor-initializer[opt] function-body
44580 __transaction_relaxed function-try-block
44584 cp_parser_function_transaction (cp_parser
*parser
, enum rid keyword
)
44586 unsigned char old_in
= parser
->in_transaction
;
44587 unsigned char new_in
= 1;
44588 tree compound_stmt
, stmt
, attrs
;
44591 gcc_assert (keyword
== RID_TRANSACTION_ATOMIC
44592 || keyword
== RID_TRANSACTION_RELAXED
);
44593 token
= cp_parser_require_keyword (parser
, keyword
,
44594 (keyword
== RID_TRANSACTION_ATOMIC
? RT_TRANSACTION_ATOMIC
44595 : RT_TRANSACTION_RELAXED
));
44596 gcc_assert (token
!= NULL
);
44598 if (keyword
== RID_TRANSACTION_RELAXED
)
44599 new_in
|= TM_STMT_ATTR_RELAXED
;
44602 attrs
= cp_parser_txn_attribute_opt (parser
);
44604 new_in
|= parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
);
44607 stmt
= begin_transaction_stmt (token
->location
, &compound_stmt
, new_in
);
44609 parser
->in_transaction
= new_in
;
44611 if (cp_lexer_next_token_is_keyword (parser
->lexer
, RID_TRY
))
44612 cp_parser_function_try_block (parser
);
44614 cp_parser_ctor_initializer_opt_and_function_body
44615 (parser
, /*in_function_try_block=*/false);
44617 parser
->in_transaction
= old_in
;
44619 finish_transaction_stmt (stmt
, compound_stmt
, new_in
, NULL_TREE
);
44622 /* Parse a __transaction_cancel statement.
44625 __transaction_cancel txn-attribute[opt] ;
44626 __transaction_cancel txn-attribute[opt] throw-expression ;
44628 ??? Cancel and throw is not yet implemented. */
44631 cp_parser_transaction_cancel (cp_parser
*parser
)
44634 bool is_outer
= false;
44637 token
= cp_parser_require_keyword (parser
, RID_TRANSACTION_CANCEL
,
44638 RT_TRANSACTION_CANCEL
);
44639 gcc_assert (token
!= NULL
);
44641 attrs
= cp_parser_txn_attribute_opt (parser
);
44643 is_outer
= (parse_tm_stmt_attr (attrs
, TM_STMT_ATTR_OUTER
) != 0);
44645 /* ??? Parse cancel-and-throw here. */
44647 cp_parser_require (parser
, CPP_SEMICOLON
, RT_SEMICOLON
);
44651 error_at (token
->location
, "%<__transaction_cancel%> without "
44652 "transactional memory support enabled");
44653 return error_mark_node
;
44655 else if (parser
->in_transaction
& TM_STMT_ATTR_RELAXED
)
44657 error_at (token
->location
, "%<__transaction_cancel%> within a "
44658 "%<__transaction_relaxed%>");
44659 return error_mark_node
;
44663 if ((parser
->in_transaction
& TM_STMT_ATTR_OUTER
) == 0
44664 && !is_tm_may_cancel_outer (current_function_decl
))
44666 error_at (token
->location
, "outer %<__transaction_cancel%> not "
44667 "within outer %<__transaction_atomic%>");
44668 error_at (token
->location
,
44669 " or a %<transaction_may_cancel_outer%> function");
44670 return error_mark_node
;
44673 else if (parser
->in_transaction
== 0)
44675 error_at (token
->location
, "%<__transaction_cancel%> not within "
44676 "%<__transaction_atomic%>");
44677 return error_mark_node
;
44680 stmt
= build_tm_abort_call (token
->location
, is_outer
);
44688 static GTY (()) cp_parser
*the_parser
;
44691 /* Special handling for the first token or line in the file. The first
44692 thing in the file might be #pragma GCC pch_preprocess, which loads a
44693 PCH file, which is a GC collection point. So we need to handle this
44694 first pragma without benefit of an existing lexer structure.
44696 Always returns one token to the caller in *FIRST_TOKEN. This is
44697 either the true first token of the file, or the first token after
44698 the initial pragma. */
44701 cp_parser_initial_pragma (cp_token
*first_token
)
44703 if (cp_parser_pragma_kind (first_token
) != PRAGMA_GCC_PCH_PREPROCESS
)
44706 cp_lexer_get_preprocessor_token (0, first_token
);
44709 if (first_token
->type
== CPP_STRING
)
44711 name
= first_token
->u
.value
;
44713 cp_lexer_get_preprocessor_token (0, first_token
);
44716 /* Skip to the end of the pragma. */
44717 if (first_token
->type
!= CPP_PRAGMA_EOL
)
44719 error_at (first_token
->location
,
44720 "malformed %<#pragma GCC pch_preprocess%>");
44722 cp_lexer_get_preprocessor_token (0, first_token
);
44723 while (first_token
->type
!= CPP_PRAGMA_EOL
);
44726 /* Now actually load the PCH file. */
44728 c_common_pch_pragma (parse_in
, TREE_STRING_POINTER (name
));
44730 /* Read one more token to return to our caller. We have to do this
44731 after reading the PCH file in, since its pointers have to be
44733 cp_lexer_get_preprocessor_token (0, first_token
);
44736 /* Parse a pragma GCC ivdep. */
44739 cp_parser_pragma_ivdep (cp_parser
*parser
, cp_token
*pragma_tok
)
44741 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44745 /* Parse a pragma GCC unroll. */
44747 static unsigned short
44748 cp_parser_pragma_unroll (cp_parser
*parser
, cp_token
*pragma_tok
)
44750 location_t location
= cp_lexer_peek_token (parser
->lexer
)->location
;
44751 tree expr
= cp_parser_constant_expression (parser
);
44752 unsigned short unroll
;
44753 expr
= maybe_constant_value (expr
);
44754 HOST_WIDE_INT lunroll
= 0;
44755 if (!INTEGRAL_TYPE_P (TREE_TYPE (expr
))
44756 || TREE_CODE (expr
) != INTEGER_CST
44757 || (lunroll
= tree_to_shwi (expr
)) < 0
44758 || lunroll
>= USHRT_MAX
)
44760 error_at (location
, "%<#pragma GCC unroll%> requires an"
44761 " assignment-expression that evaluates to a non-negative"
44762 " integral constant less than %u", USHRT_MAX
);
44767 unroll
= (unsigned short)lunroll
;
44771 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
44775 /* Normal parsing of a pragma token. Here we can (and must) use the
44779 cp_parser_pragma (cp_parser
*parser
, enum pragma_context context
, bool *if_p
)
44781 cp_token
*pragma_tok
;
44786 pragma_tok
= cp_lexer_consume_token (parser
->lexer
);
44787 gcc_assert (pragma_tok
->type
== CPP_PRAGMA
);
44788 parser
->lexer
->in_pragma
= true;
44790 id
= cp_parser_pragma_kind (pragma_tok
);
44791 if (id
!= PRAGMA_OMP_DECLARE
&& id
!= PRAGMA_OACC_ROUTINE
)
44792 cp_ensure_no_omp_declare_simd (parser
);
44795 case PRAGMA_GCC_PCH_PREPROCESS
:
44796 error_at (pragma_tok
->location
,
44797 "%<#pragma GCC pch_preprocess%> must be first");
44800 case PRAGMA_OMP_BARRIER
:
44803 case pragma_compound
:
44804 cp_parser_omp_barrier (parser
, pragma_tok
);
44807 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
44808 "used in compound statements", "omp barrier");
44815 case PRAGMA_OMP_DEPOBJ
:
44818 case pragma_compound
:
44819 cp_parser_omp_depobj (parser
, pragma_tok
);
44822 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
44823 "used in compound statements", "omp depobj");
44830 case PRAGMA_OMP_FLUSH
:
44833 case pragma_compound
:
44834 cp_parser_omp_flush (parser
, pragma_tok
);
44837 error_at (pragma_tok
->location
, "%<#pragma %s%> may only be "
44838 "used in compound statements", "omp flush");
44845 case PRAGMA_OMP_TASKWAIT
:
44848 case pragma_compound
:
44849 cp_parser_omp_taskwait (parser
, pragma_tok
);
44852 error_at (pragma_tok
->location
,
44853 "%<#pragma %s%> may only be used in compound statements",
44861 case PRAGMA_OMP_TASKYIELD
:
44864 case pragma_compound
:
44865 cp_parser_omp_taskyield (parser
, pragma_tok
);
44868 error_at (pragma_tok
->location
,
44869 "%<#pragma %s%> may only be used in compound statements",
44877 case PRAGMA_OMP_CANCEL
:
44880 case pragma_compound
:
44881 cp_parser_omp_cancel (parser
, pragma_tok
);
44884 error_at (pragma_tok
->location
,
44885 "%<#pragma %s%> may only be used in compound statements",
44893 case PRAGMA_OMP_CANCELLATION_POINT
:
44894 cp_parser_omp_cancellation_point (parser
, pragma_tok
, context
);
44897 case PRAGMA_OMP_THREADPRIVATE
:
44898 cp_parser_omp_threadprivate (parser
, pragma_tok
);
44901 case PRAGMA_OMP_DECLARE
:
44902 return cp_parser_omp_declare (parser
, pragma_tok
, context
);
44904 case PRAGMA_OACC_DECLARE
:
44905 cp_parser_oacc_declare (parser
, pragma_tok
);
44908 case PRAGMA_OACC_ENTER_DATA
:
44909 if (context
== pragma_stmt
)
44911 error_at (pragma_tok
->location
,
44912 "%<#pragma %s%> may only be used in compound statements",
44916 else if (context
!= pragma_compound
)
44918 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44921 case PRAGMA_OACC_EXIT_DATA
:
44922 if (context
== pragma_stmt
)
44924 error_at (pragma_tok
->location
,
44925 "%<#pragma %s%> may only be used in compound statements",
44929 else if (context
!= pragma_compound
)
44931 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44934 case PRAGMA_OACC_ROUTINE
:
44935 if (context
!= pragma_external
)
44937 error_at (pragma_tok
->location
,
44938 "%<#pragma acc routine%> must be at file scope");
44941 cp_parser_oacc_routine (parser
, pragma_tok
, context
);
44944 case PRAGMA_OACC_UPDATE
:
44945 if (context
== pragma_stmt
)
44947 error_at (pragma_tok
->location
,
44948 "%<#pragma %s%> may only be used in compound statements",
44952 else if (context
!= pragma_compound
)
44954 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44957 case PRAGMA_OACC_WAIT
:
44958 if (context
== pragma_stmt
)
44960 error_at (pragma_tok
->location
,
44961 "%<#pragma %s%> may only be used in compound statements",
44965 else if (context
!= pragma_compound
)
44967 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44969 case PRAGMA_OMP_ALLOCATE
:
44970 cp_parser_omp_allocate (parser
, pragma_tok
);
44972 case PRAGMA_OACC_ATOMIC
:
44973 case PRAGMA_OACC_CACHE
:
44974 case PRAGMA_OACC_DATA
:
44975 case PRAGMA_OACC_HOST_DATA
:
44976 case PRAGMA_OACC_KERNELS
:
44977 case PRAGMA_OACC_LOOP
:
44978 case PRAGMA_OACC_PARALLEL
:
44979 case PRAGMA_OACC_SERIAL
:
44980 case PRAGMA_OMP_ATOMIC
:
44981 case PRAGMA_OMP_CRITICAL
:
44982 case PRAGMA_OMP_DISTRIBUTE
:
44983 case PRAGMA_OMP_FOR
:
44984 case PRAGMA_OMP_LOOP
:
44985 case PRAGMA_OMP_MASTER
:
44986 case PRAGMA_OMP_PARALLEL
:
44987 case PRAGMA_OMP_SECTIONS
:
44988 case PRAGMA_OMP_SIMD
:
44989 case PRAGMA_OMP_SINGLE
:
44990 case PRAGMA_OMP_TASK
:
44991 case PRAGMA_OMP_TASKGROUP
:
44992 case PRAGMA_OMP_TASKLOOP
:
44993 case PRAGMA_OMP_TEAMS
:
44994 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
44996 stmt
= push_omp_privatization_clauses (false);
44997 cp_parser_omp_construct (parser
, pragma_tok
, if_p
);
44998 pop_omp_privatization_clauses (stmt
);
45001 case PRAGMA_OMP_REQUIRES
:
45002 if (context
!= pragma_external
)
45004 error_at (pragma_tok
->location
,
45005 "%<#pragma omp requires%> may only be used at file or "
45006 "namespace scope");
45009 return cp_parser_omp_requires (parser
, pragma_tok
);
45011 case PRAGMA_OMP_ORDERED
:
45012 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
45014 stmt
= push_omp_privatization_clauses (false);
45015 ret
= cp_parser_omp_ordered (parser
, pragma_tok
, context
, if_p
);
45016 pop_omp_privatization_clauses (stmt
);
45019 case PRAGMA_OMP_TARGET
:
45020 if (context
!= pragma_stmt
&& context
!= pragma_compound
)
45022 stmt
= push_omp_privatization_clauses (false);
45023 ret
= cp_parser_omp_target (parser
, pragma_tok
, context
, if_p
);
45024 pop_omp_privatization_clauses (stmt
);
45027 case PRAGMA_OMP_END_DECLARE_TARGET
:
45028 cp_parser_omp_end_declare_target (parser
, pragma_tok
);
45031 case PRAGMA_OMP_SCAN
:
45032 error_at (pragma_tok
->location
,
45033 "%<#pragma omp scan%> may only be used in "
45034 "a loop construct with %<inscan%> %<reduction%> clause");
45037 case PRAGMA_OMP_SECTION
:
45038 error_at (pragma_tok
->location
,
45039 "%<#pragma omp section%> may only be used in "
45040 "%<#pragma omp sections%> construct");
45045 if (context
== pragma_external
)
45047 error_at (pragma_tok
->location
,
45048 "%<#pragma GCC ivdep%> must be inside a function");
45051 const bool ivdep
= cp_parser_pragma_ivdep (parser
, pragma_tok
);
45052 unsigned short unroll
;
45053 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
45054 if (tok
->type
== CPP_PRAGMA
45055 && cp_parser_pragma_kind (tok
) == PRAGMA_UNROLL
)
45057 tok
= cp_lexer_consume_token (parser
->lexer
);
45058 unroll
= cp_parser_pragma_unroll (parser
, tok
);
45059 tok
= cp_lexer_peek_token (the_parser
->lexer
);
45063 if (tok
->type
!= CPP_KEYWORD
45064 || (tok
->keyword
!= RID_FOR
45065 && tok
->keyword
!= RID_WHILE
45066 && tok
->keyword
!= RID_DO
))
45068 cp_parser_error (parser
, "for, while or do statement expected");
45071 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
45075 case PRAGMA_UNROLL
:
45077 if (context
== pragma_external
)
45079 error_at (pragma_tok
->location
,
45080 "%<#pragma GCC unroll%> must be inside a function");
45083 const unsigned short unroll
45084 = cp_parser_pragma_unroll (parser
, pragma_tok
);
45086 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
45087 if (tok
->type
== CPP_PRAGMA
45088 && cp_parser_pragma_kind (tok
) == PRAGMA_IVDEP
)
45090 tok
= cp_lexer_consume_token (parser
->lexer
);
45091 ivdep
= cp_parser_pragma_ivdep (parser
, tok
);
45092 tok
= cp_lexer_peek_token (the_parser
->lexer
);
45096 if (tok
->type
!= CPP_KEYWORD
45097 || (tok
->keyword
!= RID_FOR
45098 && tok
->keyword
!= RID_WHILE
45099 && tok
->keyword
!= RID_DO
))
45101 cp_parser_error (parser
, "for, while or do statement expected");
45104 cp_parser_iteration_statement (parser
, if_p
, ivdep
, unroll
);
45109 gcc_assert (id
>= PRAGMA_FIRST_EXTERNAL
);
45110 c_invoke_pragma_handler (id
);
45114 cp_parser_error (parser
, "expected declaration specifiers");
45118 cp_parser_skip_to_pragma_eol (parser
, pragma_tok
);
45122 /* The interface the pragma parsers have to the lexer. */
45125 pragma_lex (tree
*value
, location_t
*loc
)
45127 cp_token
*tok
= cp_lexer_peek_token (the_parser
->lexer
);
45128 enum cpp_ttype ret
= tok
->type
;
45130 *value
= tok
->u
.value
;
45132 *loc
= tok
->location
;
45134 if (ret
== CPP_PRAGMA_EOL
)
45136 else if (ret
== CPP_STRING
)
45137 *value
= cp_parser_string_literal (the_parser
, false, false);
45140 if (ret
== CPP_KEYWORD
)
45142 cp_lexer_consume_token (the_parser
->lexer
);
45149 /* External interface. */
45151 /* Parse one entire translation unit. */
45154 c_parse_file (void)
45156 static bool already_called
= false;
45158 if (already_called
)
45159 fatal_error (input_location
,
45160 "multi-source compilation not implemented for C++");
45161 already_called
= true;
45163 /* cp_lexer_new_main is called before doing any GC allocation
45164 because tokenization might load a PCH file. */
45165 cp_lexer
*lexer
= cp_lexer_new_main ();
45167 the_parser
= cp_parser_new (lexer
);
45169 cp_parser_translation_unit (the_parser
);
45170 class_decl_loc_t::diag_mismatched_tags ();
45174 finish_translation_unit ();
45177 /* Create an identifier for a generic parameter type (a synthesized
45178 template parameter implied by `auto' or a concept identifier). */
45180 static GTY(()) int generic_parm_count
;
45182 make_generic_type_name ()
45185 sprintf (buf
, "auto:%d", ++generic_parm_count
);
45186 return get_identifier (buf
);
45189 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
45190 (creating a new template parameter list if necessary). Returns the newly
45191 created template type parm. */
45194 synthesize_implicit_template_parm (cp_parser
*parser
, tree constr
)
45196 /* A requires-clause is not a function and cannot have placeholders. */
45197 if (current_binding_level
->kind
== sk_block
)
45199 error ("placeholder type not allowed in this context");
45200 return error_mark_node
;
45203 gcc_assert (current_binding_level
->kind
== sk_function_parms
);
45205 /* We are either continuing a function template that already contains implicit
45206 template parameters, creating a new fully-implicit function template, or
45207 extending an existing explicit function template with implicit template
45210 cp_binding_level
*const entry_scope
= current_binding_level
;
45212 bool become_template
= false;
45213 cp_binding_level
*parent_scope
= 0;
45215 if (parser
->implicit_template_scope
)
45217 gcc_assert (parser
->implicit_template_parms
);
45219 current_binding_level
= parser
->implicit_template_scope
;
45223 /* Roll back to the existing template parameter scope (in the case of
45224 extending an explicit function template) or introduce a new template
45225 parameter scope ahead of the function parameter scope (or class scope
45226 in the case of out-of-line member definitions). The function scope is
45227 added back after template parameter synthesis below. */
45229 cp_binding_level
*scope
= entry_scope
;
45231 while (scope
->kind
== sk_function_parms
)
45233 parent_scope
= scope
;
45234 scope
= scope
->level_chain
;
45236 if (current_class_type
&& !LAMBDA_TYPE_P (current_class_type
))
45238 /* If not defining a class, then any class scope is a scope level in
45239 an out-of-line member definition. In this case simply wind back
45240 beyond the first such scope to inject the template parameter list.
45241 Otherwise wind back to the class being defined. The latter can
45242 occur in class member friend declarations such as:
45248 friend void A::foo (auto);
45251 The template parameter list synthesized for the friend declaration
45252 must be injected in the scope of 'B'. This can also occur in
45253 erroneous cases such as:
45259 void B::foo (auto) {}
45262 Here the attempted definition of 'B::foo' within 'A' is ill-formed
45263 but, nevertheless, the template parameter list synthesized for the
45264 declarator should be injected into the scope of 'A' as if the
45265 ill-formed template was specified explicitly. */
45267 while (scope
->kind
== sk_class
&& !scope
->defining_class_p
)
45269 parent_scope
= scope
;
45270 scope
= scope
->level_chain
;
45274 current_binding_level
= scope
;
45276 if (scope
->kind
!= sk_template_parms
45277 || !function_being_declared_is_template_p (parser
))
45279 /* Introduce a new template parameter list for implicit template
45282 become_template
= true;
45284 parser
->implicit_template_scope
45285 = begin_scope (sk_template_parms
, NULL
);
45287 ++processing_template_decl
;
45289 parser
->fully_implicit_function_template_p
= true;
45290 ++parser
->num_template_parameter_lists
;
45294 /* Synthesize implicit template parameters at the end of the explicit
45295 template parameter list. */
45297 gcc_assert (current_template_parms
);
45299 parser
->implicit_template_scope
= scope
;
45301 tree v
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
45302 parser
->implicit_template_parms
45303 = TREE_VEC_ELT (v
, TREE_VEC_LENGTH (v
) - 1);
45307 /* Synthesize a new template parameter and track the current template
45308 parameter chain with implicit_template_parms. */
45310 tree proto
= constr
? DECL_INITIAL (constr
) : NULL_TREE
;
45311 tree synth_id
= make_generic_type_name ();
45312 tree synth_tmpl_parm
;
45313 bool non_type
= false;
45315 /* Synthesize the type template parameter. */
45316 gcc_assert(!proto
|| TREE_CODE (proto
) == TYPE_DECL
);
45317 synth_tmpl_parm
= finish_template_type_parm (class_type_node
, synth_id
);
45319 /* Attach the constraint to the parm before processing. */
45320 tree node
= build_tree_list (NULL_TREE
, synth_tmpl_parm
);
45321 TREE_TYPE (node
) = constr
;
45323 = process_template_parm (parser
->implicit_template_parms
,
45326 /*non_type=*/non_type
,
45327 /*param_pack=*/false);
45329 /* Mark the synthetic declaration "virtual". This is used when
45330 comparing template-heads to determine if whether an abbreviated
45331 function template is equivalent to an explicit template.
45333 Note that DECL_ARTIFICIAL is used elsewhere for template parameters. */
45334 DECL_VIRTUAL_P (TREE_VALUE (new_parm
)) = true;
45336 // Chain the new parameter to the list of implicit parameters.
45337 if (parser
->implicit_template_parms
)
45338 parser
->implicit_template_parms
45339 = TREE_CHAIN (parser
->implicit_template_parms
);
45341 parser
->implicit_template_parms
= new_parm
;
45343 tree new_decl
= get_local_decls ();
45345 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
45346 new_decl
= DECL_INITIAL (new_decl
);
45348 /* If creating a fully implicit function template, start the new implicit
45349 template parameter list with this synthesized type, otherwise grow the
45350 current template parameter list. */
45352 if (become_template
)
45354 parent_scope
->level_chain
= current_binding_level
;
45356 tree new_parms
= make_tree_vec (1);
45357 TREE_VEC_ELT (new_parms
, 0) = parser
->implicit_template_parms
;
45358 current_template_parms
= tree_cons (size_int (processing_template_decl
),
45359 new_parms
, current_template_parms
);
45363 tree
& new_parms
= INNERMOST_TEMPLATE_PARMS (current_template_parms
);
45364 int new_parm_idx
= TREE_VEC_LENGTH (new_parms
);
45365 new_parms
= grow_tree_vec (new_parms
, new_parm_idx
+ 1);
45366 TREE_VEC_ELT (new_parms
, new_parm_idx
) = parser
->implicit_template_parms
;
45369 /* If the new parameter was constrained, we need to add that to the
45370 constraints in the template parameter list. */
45371 if (tree req
= TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm
)))
45373 tree reqs
= TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
);
45374 reqs
= combine_constraint_expressions (reqs
, req
);
45375 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms
) = reqs
;
45378 current_binding_level
= entry_scope
;
45383 /* Finish the declaration of a fully implicit function template. Such a
45384 template has no explicit template parameter list so has not been through the
45385 normal template head and tail processing. synthesize_implicit_template_parm
45386 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
45387 provided if the declaration is a class member such that its template
45388 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
45389 form is returned. Otherwise NULL_TREE is returned. */
45392 finish_fully_implicit_template (cp_parser
*parser
, tree member_decl_opt
)
45394 gcc_assert (parser
->fully_implicit_function_template_p
);
45396 if (member_decl_opt
&& member_decl_opt
!= error_mark_node
45397 && DECL_VIRTUAL_P (member_decl_opt
))
45399 error_at (DECL_SOURCE_LOCATION (member_decl_opt
),
45400 "implicit templates may not be %<virtual%>");
45401 DECL_VIRTUAL_P (member_decl_opt
) = false;
45404 if (member_decl_opt
)
45405 member_decl_opt
= finish_member_template_decl (member_decl_opt
);
45406 end_template_decl ();
45408 parser
->fully_implicit_function_template_p
= false;
45409 parser
->implicit_template_parms
= 0;
45410 parser
->implicit_template_scope
= 0;
45411 --parser
->num_template_parameter_lists
;
45413 return member_decl_opt
;
45416 /* Like finish_fully_implicit_template, but to be used in error
45417 recovery, rearranging scopes so that we restore the state we had
45418 before synthesize_implicit_template_parm inserted the implement
45419 template parms scope. */
45422 abort_fully_implicit_template (cp_parser
*parser
)
45424 cp_binding_level
*return_to_scope
= current_binding_level
;
45426 if (parser
->implicit_template_scope
45427 && return_to_scope
!= parser
->implicit_template_scope
)
45429 cp_binding_level
*child
= return_to_scope
;
45430 for (cp_binding_level
*scope
= child
->level_chain
;
45431 scope
!= parser
->implicit_template_scope
;
45432 scope
= child
->level_chain
)
45434 child
->level_chain
= parser
->implicit_template_scope
->level_chain
;
45435 parser
->implicit_template_scope
->level_chain
= return_to_scope
;
45436 current_binding_level
= parser
->implicit_template_scope
;
45439 return_to_scope
= return_to_scope
->level_chain
;
45441 finish_fully_implicit_template (parser
, NULL
);
45443 gcc_assert (current_binding_level
== return_to_scope
);
45446 /* Helper function for diagnostics that have complained about things
45447 being used with 'extern "C"' linkage.
45449 Attempt to issue a note showing where the 'extern "C"' linkage began. */
45452 maybe_show_extern_c_location (void)
45454 if (the_parser
->innermost_linkage_specification_location
!= UNKNOWN_LOCATION
)
45455 inform (the_parser
->innermost_linkage_specification_location
,
45456 "%<extern \"C\"%> linkage started here");
45459 #include "gt-cp-parser.h"